Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# reinterpretBoolean

> Reinterpret a boolean [ndarray][@stdlib/ndarray/base/ctor] as an unsigned 8-bit integer [ndarray][@stdlib/ndarray/base/ctor].

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var reinterpretBoolean = require( '@stdlib/ndarray/base/reinterpret-boolean' );
```

#### reinterpretBoolean( x )

Reinterprets a boolean [ndarray][@stdlib/ndarray/base/ctor] as an unsigned 8-bit integer [ndarray][@stdlib/ndarray/base/ctor].

```javascript
var falses = require( '@stdlib/ndarray/base/falses' );

var x = falses( 'bool', [ 2, 2 ], 'row-major' );
// returns <ndarray>[ [ false, false ], [ false, false ] ]

var out = reinterpretBoolean( x );
// returns <ndarray>[ [ 0, 0 ], [ 0, 0 ] ]
```

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- The returned [ndarray][@stdlib/ndarray/base/ctor] is a view on the input [ndarray][@stdlib/ndarray/base/ctor] data buffer.
- The returned [ndarray][@stdlib/ndarray/base/ctor] is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var bernoulli = require( '@stdlib/random/array/bernoulli' );
var BooleanArray = require( '@stdlib/array/bool' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var reinterpretBoolean = require( '@stdlib/ndarray/base/reinterpret-boolean' );

// Create a boolean ndarray:
var buf = new BooleanArray( bernoulli( 4, 0.5 ) );
var x = ndarray( 'bool', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );

// Reinterpret as an unsigned 8-bit integer ndarray:
var out = reinterpretBoolean( x );
console.log( ndarray2array( out ) );
```

</section>

<!-- /.examples -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<section class="links">

[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var BooleanArray = require( '@stdlib/array/bool' );
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var reinterpretBoolean = require( './../lib' );


// MAIN //

bench( format( '%s::base_ndarray,2d', pkg ), function benchmark( b ) {
var strides;
var values;
var buffer;
var offset;
var dtype;
var shape;
var order;
var out;
var i;

dtype = 'bool';
buffer = new BooleanArray( 4 );
shape = [ 2, 2 ];
strides = [ 2, 1 ];
offset = 0;
order = 'row-major';

values = [
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
ndarrayBase( dtype, buffer, shape, strides, offset, order ),
ndarrayBase( dtype, buffer, shape, strides, offset, order )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = reinterpretBoolean( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isndarrayLike( out ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::ndarray,2d', pkg ), function benchmark( b ) {
var strides;
var values;
var buffer;
var offset;
var dtype;
var shape;
var order;
var out;
var i;

dtype = 'bool';
buffer = new BooleanArray( 4 );
shape = [ 2, 2 ];
strides = [ 2, 1 ];
offset = 0;
order = 'row-major';

values = [
ndarray( dtype, buffer, shape, strides, offset, order ),
ndarray( dtype, buffer, shape, strides, offset, order ),
ndarray( dtype, buffer, shape, strides, offset, order ),
ndarray( dtype, buffer, shape, strides, offset, order ),
ndarray( dtype, buffer, shape, strides, offset, order )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = reinterpretBoolean( values[ i%values.length ] );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isndarrayLike( out ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

{{alias}}( x )
Reinterprets a boolean ndarray as an unsigned 8-bit integer ndarray.

The returned ndarray is a view on the input ndarray data buffer.

The returned ndarray is a "base" ndarray, and, thus, the returned ndarray
does not perform bounds checking or afford any of the guarantees of the
non-base ndarray constructor. The primary intent of this function is to
reinterpret an ndarray-like object within internal implementations and to
do so with minimal overhead.

Parameters
----------
x: ndarray
Input ndarray.

Returns
-------
out: ndarray
Unsigned 8-bit integer ndarray view.

Examples
--------
> var x = {{alias:@stdlib/ndarray/base/zeros}}( 'bool', [ 2, 2 ], 'row-major' );
> var out = {{alias}}( x )
<ndarray>[ [ 0, 0 ], [ 0, 0 ] ]

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { boolndarray, uint8ndarray } from '@stdlib/types/ndarray';

/**
* Reinterprets a boolean ndarray as an unsigned 8-bit integer ndarray.
*
* ## Notes
*
* - The returned ndarray is a view on the input ndarray data buffer.
* - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead.
*
* @param x - input ndarray
* @returns unsigned 8-bit integer ndarray view
*
* @example
* var zeros = require( '@stdlib/ndarray/base/zeros' );
*
* var x = zeros( 'bool', [ 2, 2 ], 'row-major' );
* // returns <ndarray>[ [ false, false ], [ false, false ] ]
*
* var out = reinterpretBoolean( x );
* // returns <ndarray>[ [ 0, 0 ], [ 0, 0 ] ]
*/
declare function reinterpretBoolean( x: boolndarray ): uint8ndarray;


// EXPORTS //

export = reinterpretBoolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-disable space-in-parens */

import zeros = require( '@stdlib/ndarray/base/zeros' );
import reinterpretBoolean = require( './index' );


// TESTS //

// The function returns a uint8ndarray...
{
const x = zeros( 'bool', [ 2, 2 ], 'row-major' );

reinterpretBoolean( x ); // $ExpectType uint8ndarray
}

// The compiler throws an error if the function is not provided a first argument which is a boolndarray...
{
reinterpretBoolean( '5' ); // $ExpectError
reinterpretBoolean( 5 ); // $ExpectError
reinterpretBoolean( true ); // $ExpectError
reinterpretBoolean( false ); // $ExpectError
reinterpretBoolean( null ); // $ExpectError
reinterpretBoolean( {} ); // $ExpectError
reinterpretBoolean( [ '5' ] ); // $ExpectError
reinterpretBoolean( ( x: number ): number => x ); // $ExpectError
reinterpretBoolean( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError
reinterpretBoolean( zeros( 'float32', [ 2, 2 ], 'row-major' ) ); // $ExpectError
reinterpretBoolean( zeros( 'int32', [ 2, 2 ], 'row-major' ) ); // $ExpectError
reinterpretBoolean( zeros( 'uint8', [ 2, 2 ], 'row-major' ) ); // $ExpectError
reinterpretBoolean( zeros( 'complex64', [ 2, 2 ], 'row-major' ) ); // $ExpectError
reinterpretBoolean( zeros( 'complex128', [ 2, 2 ], 'row-major' ) ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
const x = zeros( 'bool', [ 2, 2 ], 'row-major' );

reinterpretBoolean(); // $ExpectError
reinterpretBoolean( x, {} ); // $ExpectError
}
Loading