Skip to content

Commit 71cf5a0

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to ndarray/empty* and ndarray/base/empty* packages
This commit further makes changes to `ndarray/zero*` and `ndarray/base/zero*` packages in order to improve type specificity and clarify that only numeric data types are explicitly supported. PR-URL: #2588 Ref: #2547 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 67858b3 commit 71cf5a0

File tree

46 files changed

+1132
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1132
-66
lines changed

lib/node_modules/@stdlib/ndarray/base/empty-like/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ var sh = y.shape;
8585

8686
```javascript
8787
var dtypes = require( '@stdlib/ndarray/dtypes' );
88-
var zeros = require( '@stdlib/ndarray/base/zeros' );
88+
var empty = require( '@stdlib/ndarray/base/empty' );
8989
var emptyLike = require( '@stdlib/ndarray/base/empty-like' );
9090

9191
// Get a list of data types:
@@ -96,7 +96,7 @@ var x;
9696
var y;
9797
var i;
9898
for ( i = 0; i < dt.length; i++ ) {
99-
x = zeros( dt[ i ], [ 2, 2 ], 'row-major' );
99+
x = empty( dt[ i ], [ 2, 2 ], 'row-major' );
100100
y = emptyLike( x );
101101
console.log( y.data );
102102
}

lib/node_modules/@stdlib/ndarray/base/empty-like/benchmark/benchmark.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
2525
var zeros = require( '@stdlib/ndarray/base/zeros' );
26+
var empty = require( '@stdlib/ndarray/base/empty' );
2627
var pkg = require( './../package.json' ).name;
2728
var emptyLike = require( './../lib' );
2829

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

275+
bench( pkg+'::base:dtype=bool', function benchmark( b ) {
276+
var x;
277+
var y;
278+
var i;
279+
280+
x = empty( 'bool', [ 0 ], 'row-major' );
281+
282+
b.tic();
283+
for ( i = 0; i < b.iterations; i++ ) {
284+
y = emptyLike( x );
285+
if ( y.length !== 0 ) {
286+
b.fail( 'should have length 0' );
287+
}
288+
}
289+
b.toc();
290+
if ( !isndarrayLike( y ) ) {
291+
b.fail( 'should return an ndarray' );
292+
}
293+
b.pass( 'benchmark finished' );
294+
b.end();
295+
});
296+
274297
bench( pkg+'::base:dtype=generic', function benchmark( b ) {
275298
var x;
276299
var y;
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var empty = require( '@stdlib/ndarray/base/empty' );
27+
var pkg = require( './../package.json' ).name;
28+
var emptyLike = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x = empty( 'bool', [ len ], 'row-major' );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var arr;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
arr = emptyLike( x );
57+
if ( arr.length !== len ) {
58+
b.fail( 'unexpected length' );
59+
}
60+
}
61+
b.toc();
62+
if ( !isndarrayLike( arr ) ) {
63+
b.fail( 'should return an ndarray' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+'::base:dtype=bool,size='+len, f );
92+
}
93+
}
94+
95+
main();

lib/node_modules/@stdlib/ndarray/base/empty-like/docs/types/index.d.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

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

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

344+
/**
345+
* Creates an uninitialized array having the same shape and data type as a provided input ndarray.
346+
*
347+
* @param x - input array
348+
* @returns output array
349+
*
350+
* @example
351+
* var empty = require( '@stdlib/ndarray/base/empty' );
352+
*
353+
* var x = empty( 'bool', [ 2, 2 ], 'row-major' );
354+
* // returns <ndarray>
355+
*
356+
* var sh = x.shape;
357+
* // returns [ 2, 2 ]
358+
*
359+
* var dt = x.dtype;
360+
* // returns 'bool'
361+
*
362+
* var y = emptyLike( x );
363+
* // returns <ndarray>
364+
*
365+
* sh = y.shape;
366+
* // returns [ 2, 2 ]
367+
*
368+
* dt = y.dtype;
369+
* // returns 'bool'
370+
*/
371+
declare function emptyLike( x: boolndarray ): boolndarray;
372+
344373
/**
345374
* Creates an uninitialized array having the same shape and data type as a provided input ndarray.
346375
*
@@ -368,7 +397,7 @@ declare function emptyLike( x: uint8cndarray ): uint8cndarray;
368397
* dt = y.dtype;
369398
* // returns 'generic'
370399
*/
371-
declare function emptyLike( x: ndarray ): typedndarray<number>;
400+
declare function emptyLike<T = unknown>( x: typedndarray<T> ): typedndarray<T>;
372401

373402

374403
// EXPORTS //

lib/node_modules/@stdlib/ndarray/base/empty-like/docs/types/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
import zeros = require( '@stdlib/ndarray/base/zeros' );
20+
import empty = require( '@stdlib/ndarray/base/empty' );
2021
import emptyLike = require( './index' );
2122

2223

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

lib/node_modules/@stdlib/ndarray/base/empty-like/examples/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
var dtypes = require( '@stdlib/ndarray/dtypes' );
22-
var zeros = require( '@stdlib/ndarray/base/zeros' );
22+
var empty = require( '@stdlib/ndarray/base/empty' );
2323
var emptyLike = require( './../lib' );
2424

2525
// Get a list of data types:
@@ -30,7 +30,7 @@ var x;
3030
var y;
3131
var i;
3232
for ( i = 0; i < dt.length; i++ ) {
33-
x = zeros( dt[ i ], [ 2, 2 ], 'row-major' );
33+
x = empty( dt[ i ], [ 2, 2 ], 'row-major' );
3434
y = emptyLike( x );
3535
console.log( y.data );
3636
}

lib/node_modules/@stdlib/ndarray/base/empty-like/test/test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2023 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -32,13 +32,15 @@ var Uint8Array = require( '@stdlib/array/uint8' );
3232
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
3333
var Complex64Array = require( '@stdlib/array/complex64' );
3434
var Complex128Array = require( '@stdlib/array/complex128' );
35+
var BooleanArray = require( '@stdlib/array/bool' );
3536
var Buffer = require( '@stdlib/buffer/ctor' );
3637
var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' );
3738
var instanceOf = require( '@stdlib/assert/instance-of' );
3839
var base = require( '@stdlib/ndarray/base/ctor' );
3940
var ndarray = require( '@stdlib/ndarray/ctor' );
4041
var array = require( '@stdlib/ndarray/array' );
4142
var zeros = require( '@stdlib/ndarray/base/zeros' );
43+
var empty = require( '@stdlib/ndarray/base/empty' );
4244
var emptyLike = require( './../lib' );
4345

4446

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

523+
tape( 'the function returns an uninitialized array (dtype=bool, base)', function test( t ) {
524+
var arr;
525+
var x;
526+
527+
x = empty( 'bool', [ 2, 2 ], 'row-major' );
528+
arr = emptyLike( x );
529+
530+
t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' );
531+
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
532+
t.deepEqual( arr.shape, [ 2, 2 ], 'returns expected value' );
533+
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
534+
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
535+
536+
t.end();
537+
});
538+
539+
tape( 'the function returns an uninitialized array (dtype=bool, non-base)', function test( t ) {
540+
var arr;
541+
var x;
542+
543+
x = array( new BooleanArray( 4 ), {
544+
'shape': [ 2, 2 ],
545+
'dtype': 'bool',
546+
'order': 'column-major'
547+
});
548+
arr = emptyLike( x );
549+
550+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
551+
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
552+
t.deepEqual( arr.shape, [ 2, 2 ], 'returns expected value' );
553+
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
554+
t.strictEqual( arr.order, 'column-major', 'returns expected value' );
555+
556+
t.end();
557+
});
558+
521559
tape( 'the function returns an uninitialized array (dtype=generic, base)', function test( t ) {
522560
var expected;
523561
var arr;

lib/node_modules/@stdlib/ndarray/base/empty/benchmark/benchmark.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ bench( pkg+':dtype=uint8c', function benchmark( b ) {
226226
b.end();
227227
});
228228

229+
bench( pkg+':dtype=bool', function benchmark( b ) {
230+
var arr;
231+
var i;
232+
b.tic();
233+
for ( i = 0; i < b.iterations; i++ ) {
234+
arr = empty( 'bool', [ 0 ], 'row-major' );
235+
if ( arr.length !== 0 ) {
236+
b.fail( 'should have length 0' );
237+
}
238+
}
239+
b.toc();
240+
if ( !isndarrayLike( arr ) ) {
241+
b.fail( 'should return an ndarray' );
242+
}
243+
b.pass( 'benchmark finished' );
244+
b.end();
245+
});
246+
229247
bench( pkg+':dtype=generic', function benchmark( b ) {
230248
var arr;
231249
var i;

0 commit comments

Comments
 (0)