Skip to content

Commit f766a56

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to ndarray/from-scalar
PR-URL: #2589 Ref: #2547 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 86ce889 commit f766a56

File tree

7 files changed

+145
-10
lines changed

7 files changed

+145
-10
lines changed

lib/node_modules/@stdlib/ndarray/from-scalar/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2022 The Stdlib Authors.
5+
Copyright (c) 2024 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -66,7 +66,8 @@ The function accepts the following `options`:
6666

6767
If a `dtype` option is not provided and `value`
6868

69-
- is a `number`, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] real-valued floating-point data type.
69+
- is a number, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] real-valued floating-point data type.
70+
- is a boolean, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] boolean data type.
7071
- is a complex number object of a known data type, the data type is the same as the provided value.
7172
- is a complex number object of an unknown data type, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] complex-valued floating-point data type.
7273
- is any other value type, the default [data type][@stdlib/ndarray/dtypes] is `'generic'`.
@@ -100,6 +101,7 @@ var v = x.get();
100101
## Notes
101102

102103
- If `value` is a number and `options.dtype` is a complex [data type][@stdlib/ndarray/dtypes], the function returns a zero-dimensional [`ndarray`][@stdlib/ndarray/ctor] containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero.
104+
- The function does not guard against precision loss when `value` is a number and the `dtype` argument is an integer [data type][@stdlib/ndarray/dtypes].
103105

104106
</section>
105107

lib/node_modules/@stdlib/ndarray/from-scalar/benchmark/benchmark.js

Lines changed: 25 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) 2022 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.
@@ -120,6 +120,30 @@ bench( pkg+':dtype=complex64', function benchmark( b ) {
120120
b.end();
121121
});
122122

123+
bench( pkg+':dtype=bool', function benchmark( b ) {
124+
var x;
125+
var v;
126+
var i;
127+
128+
v = [ true, false ];
129+
130+
b.tic();
131+
for ( i = 0; i < b.iterations; i++ ) {
132+
x = scalar2ndarray( v[ i%2 ], {
133+
'dtype': 'bool'
134+
});
135+
if ( x.length !== 1 ) {
136+
b.fail( 'should have length 1' );
137+
}
138+
}
139+
b.toc();
140+
if ( !isndarrayLike( x ) ) {
141+
b.fail( 'should return an ndarray' );
142+
}
143+
b.pass( 'benchmark finished' );
144+
b.end();
145+
});
146+
123147
bench( pkg+':dtype=int32', function benchmark( b ) {
124148
var x;
125149
var i;

lib/node_modules/@stdlib/ndarray/from-scalar/docs/repl.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
- is a number, the default data type is the default real-valued
2222
floating-point data type.
23+
- is a boolean, the default data type is the default boolean data type.
2324
- is a complex number object of a known complex data type, the data type
2425
is the same as the provided value.
2526
- is a complex number object of an unknown data type, the default data

lib/node_modules/@stdlib/ndarray/from-scalar/docs/types/index.d.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2022 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.
@@ -21,7 +21,7 @@
2121
/// <reference types="@stdlib/types"/>
2222

2323
import { ComplexLike } from '@stdlib/types/complex';
24-
import { ndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, DataType, Order } from '@stdlib/types/ndarray';
24+
import { ndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, boolndarray, DataType, Order } from '@stdlib/types/ndarray';
2525

2626
/**
2727
* Interface defining common options.
@@ -88,6 +88,16 @@ interface Complex64Options extends BaseOptions {
8888
dtype: 'complex64';
8989
}
9090

91+
/**
92+
* Interface defining options when `dtype` is `'bool'`.
93+
*/
94+
interface BoolOptions extends BaseOptions {
95+
/**
96+
* Output array data type.
97+
*/
98+
dtype: 'bool';
99+
}
100+
91101
/**
92102
* Interface defining options when `dtype` is `'int32'`.
93103
*/
@@ -286,6 +296,30 @@ declare function scalar2ndarray( value: number | ComplexLike, options: Complex12
286296
*/
287297
declare function scalar2ndarray( value: number | ComplexLike, options: Complex64Options ): complex64ndarray;
288298

299+
/**
300+
* Returns a zero-dimensional ndarray containing a provided scalar value.
301+
*
302+
* @param value - scalar value
303+
* @param options - options
304+
* @returns zero-dimensional ndarray
305+
*
306+
* @example
307+
* var x = scalar2ndarray( true, {
308+
* 'dtype': bool'
309+
* };
310+
* // returns <ndarray>
311+
*
312+
* var sh = x.shape;
313+
* // returns []
314+
*
315+
* var dt = x.dtype;
316+
* // returns 'bool'
317+
*
318+
* var v = x.get();
319+
* // returns true
320+
*/
321+
declare function scalar2ndarray( value: boolean, options: BoolOptions ): boolndarray;
322+
289323
/**
290324
* Returns a zero-dimensional ndarray containing a provided scalar value.
291325
*
@@ -461,7 +495,8 @@ declare function scalar2ndarray( value: number, options: Uint8cOptions ): uint8c
461495
*
462496
* - If a `dtype` option is not provided and `value`
463497
*
464-
* - is a `number`, the default data type is the default real-valued floating-point data type.
498+
* - is a number, the default data type is the default real-valued floating-point data type.
499+
* - is a boolean, the default data type is the default boolean data type.
465500
* - is a complex number object of a known complex data type, the data type is the same as the provided value.
466501
* - is a complex number object of an unknown complex data type, the default data type is the default complex-valued floating-point data type.
467502
* - is any other value type, the default data type is `'generic'`.

lib/node_modules/@stdlib/ndarray/from-scalar/docs/types/test.ts

Lines changed: 2 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) 2022 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.
@@ -29,6 +29,7 @@ import scalar2ndarray = require( './index' );
2929
scalar2ndarray( 1.0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray
3030
scalar2ndarray( 1.0, { 'dtype': 'complex128' } ); // $ExpectType complex128ndarray
3131
scalar2ndarray( 1.0, { 'dtype': 'complex64' } ); // $ExpectType complex64ndarray
32+
scalar2ndarray( true, { 'dtype': 'bool' } ); // $ExpectType boolndarray
3233
scalar2ndarray( 1.0, { 'dtype': 'int32' } ); // $ExpectType int32ndarray
3334
scalar2ndarray( 1.0, { 'dtype': 'int16' } ); // $ExpectType int16ndarray
3435
scalar2ndarray( 1.0, { 'dtype': 'int8' } ); // $ExpectType int8ndarray

lib/node_modules/@stdlib/ndarray/from-scalar/lib/main.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
2424
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
2525
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
26+
var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
2627
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
28+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2729
var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
2830
var accessorSetter = require( '@stdlib/array/base/accessor-setter' );
2931
var setter = require( '@stdlib/array/base/setter' );
@@ -39,6 +41,7 @@ var format = require( '@stdlib/string/format' );
3941
var ORDER = defaults.get( 'order' );
4042
var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' );
4143
var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
44+
var DEFAULT_BOOL = defaults.get( 'dtypes.boolean' );
4245

4346

4447
// MAIN //
@@ -50,7 +53,8 @@ var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
5053
*
5154
* - If a `dtype` option is not provided and `value`
5255
*
53-
* - is a `number`, the default data type is the default real-valued floating-point data type.
56+
* - is a number, the default data type is the default real-valued floating-point data type.
57+
* - is a boolean, the default data type is the default boolean data type.
5458
* - is a complex number object of a known complex data type, the data type is the same as the provided value.
5559
* - is a complex number object of an unknown complex data type, the default data type is the default complex-valued floating-point data type.
5660
* - is any other value type, the default data type is `'generic'`.
@@ -125,6 +129,8 @@ function scalar2ndarray( value ) {
125129
if ( opts.dtype === '' ) {
126130
if ( flg ) {
127131
dt = DEFAULT_REAL;
132+
} else if ( isBoolean( value ) ) {
133+
dt = DEFAULT_BOOL;
128134
} else if ( isComplexLike( value ) ) {
129135
dt = dtype( value );
130136
if ( dt === null ) {
@@ -140,7 +146,7 @@ function scalar2ndarray( value ) {
140146
if ( buf === null ) {
141147
throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', dt ) );
142148
}
143-
if ( /^complex/.test( dt ) && flg ) {
149+
if ( isComplexDataType( dt ) && flg ) {
144150
v = [ value, 0.0 ]; // note: we're assuming that the ComplexXXArray setter accepts an array of interleaved real and imaginary components
145151
} else {
146152
v = value;

lib/node_modules/@stdlib/ndarray/from-scalar/test/test.js

Lines changed: 67 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) 2022 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,8 +32,10 @@ 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 reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
3637
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
38+
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
3739
var Complex128 = require( '@stdlib/complex/float64/ctor' );
3840
var Complex64 = require( '@stdlib/complex/float32/ctor' );
3941
var instanceOf = require( '@stdlib/assert/instance-of' );
@@ -230,6 +232,37 @@ tape( 'the function returns a zero-dimensional ndarray (default, complex64)', fu
230232
t.end();
231233
});
232234

235+
tape( 'the function returns a zero-dimensional ndarray (default, bool)', function test( t ) {
236+
var expected;
237+
var arr;
238+
239+
expected = new Uint8Array( [ 1 ] );
240+
241+
arr = scalar2ndarray( true );
242+
243+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
244+
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
245+
t.deepEqual( arr.shape, [], 'returns expected value' );
246+
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
247+
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
248+
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
249+
t.strictEqual( arr.length, 1, 'returns expected value' );
250+
251+
expected = new Uint8Array( [ 0 ] );
252+
253+
arr = scalar2ndarray( false );
254+
255+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
256+
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
257+
t.deepEqual( arr.shape, [], 'returns expected value' );
258+
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
259+
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
260+
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
261+
t.strictEqual( arr.length, 1, 'returns expected value' );
262+
263+
t.end();
264+
});
265+
233266
tape( 'the function returns a zero-dimensional ndarray (default, other)', function test( t ) {
234267
var expected;
235268
var arr;
@@ -428,6 +461,39 @@ tape( 'the function returns a zero-dimensional ndarray (dtype=uint8c)', function
428461
t.end();
429462
});
430463

464+
tape( 'the function returns a zero-dimensional ndarray (dtype=bool)', function test( t ) {
465+
var expected;
466+
var arr;
467+
468+
expected = new Uint8Array( [ 1 ] );
469+
arr = scalar2ndarray( true, {
470+
'dtype': 'bool'
471+
});
472+
473+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
474+
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
475+
t.deepEqual( arr.shape, [], 'returns expected value' );
476+
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
477+
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
478+
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
479+
t.strictEqual( arr.length, 1, 'returns expected value' );
480+
481+
expected = new Uint8Array( [ 0 ] );
482+
arr = scalar2ndarray( false, {
483+
'dtype': 'bool'
484+
});
485+
486+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
487+
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
488+
t.deepEqual( arr.shape, [], 'returns expected value' );
489+
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
490+
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
491+
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
492+
t.strictEqual( arr.length, 1, 'returns expected value' );
493+
494+
t.end();
495+
});
496+
431497
tape( 'the function returns a zero-dimensional ndarray (dtype=complex128, complex)', function test( t ) {
432498
var expected;
433499
var arr;

0 commit comments

Comments
 (0)