Skip to content

Commit 3a3116e

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to array/from-scalar
PR-URL: #2470 Ref: #2304 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 8a55ea2 commit 3a3116e

File tree

7 files changed

+122
-5
lines changed

7 files changed

+122
-5
lines changed

Diff for: lib/node_modules/@stdlib/array/from-scalar/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ var x = scalar2array( 3.0 );
5151

5252
If not provided a `dtype` argument and `value`
5353

54-
- is a `number`, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] real-valued floating-point data type.
54+
- is a number, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] real-valued floating-point data type.
55+
- is a boolean, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] boolean data type.
5556
- is a complex number object of a known data type, the data type is the same as the provided value.
5657
- is a complex number object of an unknown data type, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] complex-valued floating-point data type.
5758
- is any other value type, the default [data type][@stdlib/array/dtypes] is `'generic'`.

Diff for: lib/node_modules/@stdlib/array/from-scalar/benchmark/benchmark.js

+51
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,32 @@ bench( pkg+'::default,complex-like', function benchmark( b ) {
169169
b.end();
170170
});
171171

172+
bench( pkg+'::default,bool', function benchmark( b ) {
173+
var values;
174+
var v;
175+
var i;
176+
177+
values = [
178+
true,
179+
false
180+
];
181+
182+
b.tic();
183+
for ( i = 0; i < b.iterations; i++ ) {
184+
v = scalar2array( values[ i%values.length ] );
185+
if ( v.length !== 1 ) {
186+
b.fail( 'should return a single-element array' );
187+
}
188+
}
189+
console.log( v );
190+
b.toc();
191+
if ( !isCollection ) {
192+
b.fail( 'should return an array' );
193+
}
194+
b.pass( 'benchmark finished' );
195+
b.end();
196+
});
197+
172198
bench( pkg+':dtype=float64', function benchmark( b ) {
173199
var values;
174200
var v;
@@ -403,6 +429,31 @@ bench( pkg+':dtype=uint8c', function benchmark( b ) {
403429
b.end();
404430
});
405431

432+
bench( pkg+':dtype=bool', function benchmark( b ) {
433+
var values;
434+
var v;
435+
var i;
436+
437+
values = [
438+
true,
439+
false
440+
];
441+
442+
b.tic();
443+
for ( i = 0; i < b.iterations; i++ ) {
444+
v = scalar2array( values[ i%values.length ], 'bool' );
445+
if ( v.length !== 1 ) {
446+
b.fail( 'should return a single-element array' );
447+
}
448+
}
449+
b.toc();
450+
if ( !isCollection ) {
451+
b.fail( 'should return an array' );
452+
}
453+
b.pass( 'benchmark finished' );
454+
b.end();
455+
});
456+
406457
bench( pkg+'::real:dtype=complex128', function benchmark( b ) {
407458
var values;
408459
var v;

Diff for: lib/node_modules/@stdlib/array/from-scalar/docs/repl.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
- is a number, the default data type is the default real-valued
1919
floating-point data type.
20+
- is a boolean, the default data type is the default boolean data type.
2021
- is a complex number object of a known complex data type, the data type
2122
is the same as the provided value.
2223
- is a complex number object of an unknown data type, the default data

Diff for: lib/node_modules/@stdlib/array/from-scalar/docs/types/index.d.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/// <reference types="@stdlib/types"/>
2222

2323
import { ComplexLike, Complex64, Complex128 } from '@stdlib/types/complex';
24-
import { DataType, Complex128Array, Complex64Array } from '@stdlib/types/array';
24+
import { DataType, Complex128Array, Complex64Array, BooleanArray } from '@stdlib/types/array';
2525

2626
/**
2727
* Returns a single-element array containing a provided scalar value.
@@ -49,6 +49,19 @@ declare function scalar2array( value: number, dtype: 'float64' ): Float64Array;
4949
*/
5050
declare function scalar2array( value: number, dtype: 'float32' ): Float32Array;
5151

52+
/**
53+
* Returns a single-element array containing a provided scalar value.
54+
*
55+
* @param value - scalar value
56+
* @param dtype - output array data type
57+
* @returns output array
58+
*
59+
* @example
60+
* var x = scalar2array( true, 'bool' );
61+
* // returns <BooleanArray>
62+
*/
63+
declare function scalar2array( value: any, dtype: 'bool' ): BooleanArray;
64+
5265
/**
5366
* Returns a single-element array containing a provided scalar value.
5467
*
@@ -208,6 +221,19 @@ declare function scalar2array<T = unknown>( value: T, dtype: 'generic' ): Array<
208221
*/
209222
declare function scalar2array( value: number ): Float64Array;
210223

224+
/**
225+
* Returns a single-element array containing a provided scalar value.
226+
*
227+
* @param value - scalar value
228+
* @param dtype - output array data type
229+
* @returns output array
230+
*
231+
* @example
232+
* var x = scalar2array( true );
233+
* // returns <BooleanArray>
234+
*/
235+
declare function scalar2array( value: boolean ): BooleanArray;
236+
211237
/**
212238
* Returns a single-element array containing a provided scalar value.
213239
*
@@ -249,7 +275,8 @@ declare function scalar2array( value: Complex128 | ComplexLike ): Complex128Arra
249275
*
250276
* - If a `dtype` argument is not provided and `value`
251277
*
252-
* - is a `number`, the default data type is the default real-valued floating-point data type.
278+
* - is a number, the default data type is the default real-valued floating-point data type.
279+
* - is a boolean, the default data type is the default boolean data type.
253280
* - is a complex number object of a known complex data type, the data type is the same as the provided value.
254281
* - is a complex number object of an unknown complex data type, the default data type is the default complex-valued floating-point data type.
255282
* - is any other value type, the default data type is `'generic'`.

Diff for: lib/node_modules/@stdlib/array/from-scalar/docs/types/test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ import array2scalar = require( './index' );
2929
array2scalar( new Complex128( 3.0, 4.0 ) ); // $ExpectType Complex128Array
3030
array2scalar( new Complex64( 3.0, 4.0 ) ); // $ExpectType Complex64Array
3131
array2scalar( { 're': 3.0, 'im': 4.0 } ); // $ExpectType Complex128Array
32+
array2scalar( true ); // $ExpectType BooleanArray
3233
array2scalar( null ); // $ExpectType null[]
3334

3435
array2scalar( 1.0, 'float64' ); // $ExpectType Float64Array
3536
array2scalar( 1.0, 'float32' ); // $ExpectType Float32Array
3637
array2scalar( 1.0, 'complex128' ); // $ExpectType Complex128Array
3738
array2scalar( 1.0, 'complex64' ); // $ExpectType Complex64Array
39+
array2scalar( true, 'bool' ); // $ExpectType BooleanArray
3840
array2scalar( 1.0, 'int32' ); // $ExpectType Int32Array
3941
array2scalar( 1.0, 'int16' ); // $ExpectType Int16Array
4042
array2scalar( 1.0, 'int8' ); // $ExpectType Int8Array

Diff for: lib/node_modules/@stdlib/array/from-scalar/lib/main.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
// MODULES //
2222

23+
var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
2324
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
2425
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
26+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2527
var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' );
2628
var accessorSetter = require( '@stdlib/array/base/accessor-setter' );
2729
var setter = require( '@stdlib/array/base/setter' );
@@ -34,6 +36,7 @@ var defaults = require( '@stdlib/array/defaults' );
3436

3537
var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' );
3638
var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
39+
var DEFAULT_BOOL = defaults.get( 'dtypes.boolean' );
3740

3841

3942
// MAIN //
@@ -45,7 +48,8 @@ var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
4548
*
4649
* - If a `dtype` option is not provided and `value`
4750
*
48-
* - is a `number`, the default data type is the default real-valued floating-point data type.
51+
* - is a number, the default data type is the default real-valued floating-point data type.
52+
* - is a boolean, the default data type is the default boolean data type.
4953
* - is a complex number object of a known complex data type, the data type is the same as the provided value.
5054
* - is a complex number object of an unknown complex data type, the default data type is the default complex-valued floating-point data type.
5155
* - is any other value type, the default data type is `'generic'`.
@@ -74,6 +78,8 @@ function scalar2array( value ) {
7478
if ( arguments.length < 2 ) {
7579
if ( flg ) {
7680
dt = DEFAULT_REAL;
81+
} else if ( isBoolean( value ) ) {
82+
dt = DEFAULT_BOOL;
7783
} else if ( isComplexLike( value ) ) {
7884
dt = dtype( value );
7985
if ( dt === null ) {
@@ -86,7 +92,7 @@ function scalar2array( value ) {
8692
dt = arguments[ 1 ];
8793
}
8894
out = zeros( 1, dt ); // delegate dtype validation to `zeros`
89-
if ( /^complex/.test( dt ) && flg ) {
95+
if ( flg && isComplexDataType( dt ) ) {
9096
v = [ value, 0.0 ]; // note: we're assuming that the ComplexXXArray setter accepts an array of interleaved real and imaginary components
9197
} else {
9298
v = value;

Diff for: lib/node_modules/@stdlib/array/from-scalar/test/test.js

+29
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ var Complex128 = require( '@stdlib/complex/float64/ctor' );
2525
var Complex64 = require( '@stdlib/complex/float32/ctor' );
2626
var Complex128Array = require( '@stdlib/array/complex128' );
2727
var Complex64Array = require( '@stdlib/array/complex64' );
28+
var BooleanArray = require( '@stdlib/array/bool' );
2829
var Float64Array = require( '@stdlib/array/float64' );
2930
var Float32Array = require( '@stdlib/array/float32' );
3031
var Int32Array = require( '@stdlib/array/int32' );
32+
var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' );
3133
var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' );
3234
var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' );
3335
var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
@@ -78,6 +80,17 @@ tape( 'the function returns a single element containing a provided scalar value
7880
t.end();
7981
});
8082

83+
tape( 'the function returns a single element containing a provided scalar value (default, bool)', function test( t ) {
84+
var expected;
85+
var actual;
86+
87+
actual = array2scalar( true );
88+
expected = new BooleanArray( [ true ] );
89+
90+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
91+
t.end();
92+
});
93+
8194
tape( 'the function returns a single element containing a provided scalar value (default, complex128)', function test( t ) {
8295
var expected;
8396
var actual;
@@ -172,6 +185,22 @@ tape( 'the function returns a single element containing a provided scalar value
172185
t.end();
173186
});
174187

188+
tape( 'the function returns a single element containing a provided scalar value (dtype=bool)', function test( t ) {
189+
var expected;
190+
var actual;
191+
192+
actual = array2scalar( false, 'bool' );
193+
expected = new BooleanArray( [ false ] );
194+
195+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
196+
197+
actual = array2scalar( true, 'bool' );
198+
expected = new BooleanArray( [ true ] );
199+
200+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
201+
t.end();
202+
});
203+
175204
tape( 'the function returns a single element containing a provided scalar value (dtype=complex128, complex)', function test( t ) {
176205
var expected;
177206
var actual;

0 commit comments

Comments
 (0)