Skip to content

feat: add boolean dtype support to ndarray/empty* and ndarray/base/empty* packages #2588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 13, 2024
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
4 changes: 2 additions & 2 deletions lib/node_modules/@stdlib/ndarray/base/empty-like/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ var sh = y.shape;

```javascript
var dtypes = require( '@stdlib/ndarray/dtypes' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
var empty = require( '@stdlib/ndarray/base/empty' );
var emptyLike = require( '@stdlib/ndarray/base/empty-like' );

// Get a list of data types:
Expand All @@ -96,7 +96,7 @@ var x;
var y;
var i;
for ( i = 0; i < dt.length; i++ ) {
x = zeros( dt[ i ], [ 2, 2 ], 'row-major' );
x = empty( dt[ i ], [ 2, 2 ], 'row-major' );
y = emptyLike( x );
console.log( y.data );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
var bench = require( '@stdlib/bench' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
var empty = require( '@stdlib/ndarray/base/empty' );
var pkg = require( './../package.json' ).name;
var emptyLike = require( './../lib' );

Expand Down Expand Up @@ -271,6 +272,28 @@ bench( pkg+'::base:dtype=uint8c', function benchmark( b ) {
b.end();
});

bench( pkg+'::base:dtype=bool', function benchmark( b ) {
var x;
var y;
var i;

x = empty( 'bool', [ 0 ], 'row-major' );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = emptyLike( x );
if ( y.length !== 0 ) {
b.fail( 'should have length 0' );
}
}
b.toc();
if ( !isndarrayLike( y ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::base:dtype=generic', function benchmark( b ) {
var x;
var y;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 pow = require( '@stdlib/math/base/special/pow' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var empty = require( '@stdlib/ndarray/base/empty' );
var pkg = require( './../package.json' ).name;
var emptyLike = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = empty( 'bool', [ len ], 'row-major' );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var arr;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = emptyLike( x );
if ( arr.length !== len ) {
b.fail( 'unexpected length' );
}
}
b.toc();
if ( !isndarrayLike( arr ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+'::base:dtype=bool,size='+len, f );
}
}

main();
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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

import { ndarray, typedndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, DataType } from '@stdlib/types/ndarray';
import { typedndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, complex128ndarray, complex64ndarray } from '@stdlib/types/ndarray';

/**
* Creates an uninitialized array having the same shape and data type as a provided input ndarray.
Expand Down Expand Up @@ -341,6 +341,35 @@ declare function emptyLike( x: uint8ndarray ): uint8ndarray;
*/
declare function emptyLike( x: uint8cndarray ): uint8cndarray;

/**
* Creates an uninitialized array having the same shape and data type as a provided input ndarray.
*
* @param x - input array
* @returns output array
*
* @example
* var empty = require( '@stdlib/ndarray/base/empty' );
*
* var x = empty( 'bool', [ 2, 2 ], 'row-major' );
* // returns <ndarray>
*
* var sh = x.shape;
* // returns [ 2, 2 ]
*
* var dt = x.dtype;
* // returns 'bool'
*
* var y = emptyLike( x );
* // returns <ndarray>
*
* sh = y.shape;
* // returns [ 2, 2 ]
*
* dt = y.dtype;
* // returns 'bool'
*/
declare function emptyLike( x: boolndarray ): boolndarray;

/**
* Creates an uninitialized array having the same shape and data type as a provided input ndarray.
*
Expand Down Expand Up @@ -368,7 +397,7 @@ declare function emptyLike( x: uint8cndarray ): uint8cndarray;
* dt = y.dtype;
* // returns 'generic'
*/
declare function emptyLike( x: ndarray ): typedndarray<number>;
declare function emptyLike<T = unknown>( x: typedndarray<T> ): typedndarray<T>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

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


Expand All @@ -38,6 +39,7 @@ import emptyLike = require( './index' );
emptyLike( zeros( 'uint16', sh, ord ) ); // $ExpectType uint16ndarray
emptyLike( zeros( 'uint8', sh, ord ) ); // $ExpectType uint8ndarray
emptyLike( zeros( 'uint8c', sh, ord ) ); // $ExpectType uint8cndarray
emptyLike( empty( 'bool', sh, ord ) ); // $ExpectType boolndarray
emptyLike( zeros( 'generic', sh, ord ) ); // $ExpectType typedndarray<number>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'use strict';

var dtypes = require( '@stdlib/ndarray/dtypes' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
var empty = require( '@stdlib/ndarray/base/empty' );
var emptyLike = require( './../lib' );

// Get a list of data types:
Expand All @@ -30,7 +30,7 @@ var x;
var y;
var i;
for ( i = 0; i < dt.length; i++ ) {
x = zeros( dt[ i ], [ 2, 2 ], 'row-major' );
x = empty( dt[ i ], [ 2, 2 ], 'row-major' );
y = emptyLike( x );
console.log( y.data );
}
40 changes: 39 additions & 1 deletion lib/node_modules/@stdlib/ndarray/base/empty-like/test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand Down Expand Up @@ -32,13 +32,15 @@ var Uint8Array = require( '@stdlib/array/uint8' );
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var Buffer = require( '@stdlib/buffer/ctor' );
var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
var instanceOf = require( '@stdlib/assert/instance-of' );
var base = require( '@stdlib/ndarray/base/ctor' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var array = require( '@stdlib/ndarray/array' );
var zeros = require( '@stdlib/ndarray/base/zeros' );
var empty = require( '@stdlib/ndarray/base/empty' );
var emptyLike = require( './../lib' );


Expand Down Expand Up @@ -518,6 +520,42 @@ tape( 'the function returns an uninitialized array (dtype=complex64, non-base)',
t.end();
});

tape( 'the function returns an uninitialized array (dtype=bool, base)', function test( t ) {
var arr;
var x;

x = empty( 'bool', [ 2, 2 ], 'row-major' );
arr = emptyLike( x );

t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
t.deepEqual( arr.shape, [ 2, 2 ], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
t.strictEqual( arr.order, 'row-major', 'returns expected value' );

t.end();
});

tape( 'the function returns an uninitialized array (dtype=bool, non-base)', function test( t ) {
var arr;
var x;

x = array( new BooleanArray( 4 ), {
'shape': [ 2, 2 ],
'dtype': 'bool',
'order': 'column-major'
});
arr = emptyLike( x );

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
t.deepEqual( arr.shape, [ 2, 2 ], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
t.strictEqual( arr.order, 'column-major', 'returns expected value' );

t.end();
});

tape( 'the function returns an uninitialized array (dtype=generic, base)', function test( t ) {
var expected;
var arr;
Expand Down
18 changes: 18 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/empty/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ bench( pkg+':dtype=uint8c', function benchmark( b ) {
b.end();
});

bench( pkg+':dtype=bool', function benchmark( b ) {
var arr;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = empty( 'bool', [ 0 ], 'row-major' );
if ( arr.length !== 0 ) {
b.fail( 'should have length 0' );
}
}
b.toc();
if ( !isndarrayLike( arr ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=generic', function benchmark( b ) {
var arr;
var i;
Expand Down
Loading
Loading