Skip to content

feat: add boolean dtype support to array/from-scalar #2470

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 10 commits into from
Jun 29, 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
3 changes: 2 additions & 1 deletion lib/node_modules/@stdlib/array/from-scalar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ var x = scalar2array( 3.0 );

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

- is a `number`, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] real-valued floating-point data type.
- is a number, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] real-valued floating-point data type.
- is a boolean, the default [data type][@stdlib/array/dtypes] is the [default][@stdlib/array/defaults] boolean data type.
- is a complex number object of a known data type, the data type is the same as the provided value.
- 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.
- is any other value type, the default [data type][@stdlib/array/dtypes] is `'generic'`.
Expand Down
51 changes: 51 additions & 0 deletions lib/node_modules/@stdlib/array/from-scalar/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@
b.end();
});

bench( pkg+'::default,bool', function benchmark( b ) {
var values;
var v;
var i;

values = [
true,
false
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = scalar2array( values[ i%values.length ] );
if ( v.length !== 1 ) {
b.fail( 'should return a single-element array' );
}
}
console.log( v );

Check warning on line 189 in lib/node_modules/@stdlib/array/from-scalar/benchmark/benchmark.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected console statement
b.toc();
if ( !isCollection ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=float64', function benchmark( b ) {
var values;
var v;
Expand Down Expand Up @@ -403,6 +429,31 @@
b.end();
});

bench( pkg+':dtype=bool', function benchmark( b ) {
var values;
var v;
var i;

values = [
true,
false
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = scalar2array( values[ i%values.length ], 'bool' );
if ( v.length !== 1 ) {
b.fail( 'should return a single-element array' );
}
}
b.toc();
if ( !isCollection ) {
b.fail( 'should return an array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::real:dtype=complex128', function benchmark( b ) {
var values;
var v;
Expand Down
1 change: 1 addition & 0 deletions lib/node_modules/@stdlib/array/from-scalar/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

- is a number, the default data type is the default real-valued
floating-point data type.
- is a boolean, the default data type is the default boolean data type.
- is a complex number object of a known complex data type, the data type
is the same as the provided value.
- is a complex number object of an unknown data type, the default data
Expand Down
31 changes: 29 additions & 2 deletions lib/node_modules/@stdlib/array/from-scalar/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/// <reference types="@stdlib/types"/>

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

/**
* Returns a single-element array containing a provided scalar value.
Expand Down Expand Up @@ -49,6 +49,19 @@
*/
declare function scalar2array( value: number, dtype: 'float32' ): Float32Array;

/**
* Returns a single-element array containing a provided scalar value.
*
* @param value - scalar value
* @param dtype - output array data type
* @returns output array
*
* @example
* var x = scalar2array( true, 'bool' );
* // returns <BooleanArray>
*/
declare function scalar2array( value: any, dtype: 'bool' ): BooleanArray;

Check warning on line 63 in lib/node_modules/@stdlib/array/from-scalar/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Returns a single-element array containing a provided scalar value.
*
Expand Down Expand Up @@ -208,6 +221,19 @@
*/
declare function scalar2array( value: number ): Float64Array;

/**
* Returns a single-element array containing a provided scalar value.
*
* @param value - scalar value
* @param dtype - output array data type
* @returns output array
*
* @example
* var x = scalar2array( true );
* // returns <BooleanArray>
*/
declare function scalar2array( value: boolean ): BooleanArray;

/**
* Returns a single-element array containing a provided scalar value.
*
Expand Down Expand Up @@ -249,7 +275,8 @@
*
* - If a `dtype` argument is not provided and `value`
*
* - is a `number`, the default data type is the default real-valued floating-point data type.
* - is a number, the default data type is the default real-valued floating-point data type.
* - is a boolean, the default data type is the default boolean data type.
* - is a complex number object of a known complex data type, the data type is the same as the provided value.
* - is a complex number object of an unknown complex data type, the default data type is the default complex-valued floating-point data type.
* - is any other value type, the default data type is `'generic'`.
Expand Down
2 changes: 2 additions & 0 deletions lib/node_modules/@stdlib/array/from-scalar/docs/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import array2scalar = require( './index' );
array2scalar( new Complex128( 3.0, 4.0 ) ); // $ExpectType Complex128Array
array2scalar( new Complex64( 3.0, 4.0 ) ); // $ExpectType Complex64Array
array2scalar( { 're': 3.0, 'im': 4.0 } ); // $ExpectType Complex128Array
array2scalar( true ); // $ExpectType BooleanArray
array2scalar( null ); // $ExpectType null[]

array2scalar( 1.0, 'float64' ); // $ExpectType Float64Array
array2scalar( 1.0, 'float32' ); // $ExpectType Float32Array
array2scalar( 1.0, 'complex128' ); // $ExpectType Complex128Array
array2scalar( 1.0, 'complex64' ); // $ExpectType Complex64Array
array2scalar( true, 'bool' ); // $ExpectType BooleanArray
array2scalar( 1.0, 'int32' ); // $ExpectType Int32Array
array2scalar( 1.0, 'int16' ); // $ExpectType Int16Array
array2scalar( 1.0, 'int8' ); // $ExpectType Int8Array
Expand Down
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/array/from-scalar/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

// MODULES //

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

var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' );
var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
var DEFAULT_BOOL = defaults.get( 'dtypes.boolean' );


// MAIN //
Expand All @@ -45,7 +48,8 @@ var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
*
* - If a `dtype` option is not provided and `value`
*
* - is a `number`, the default data type is the default real-valued floating-point data type.
* - is a number, the default data type is the default real-valued floating-point data type.
* - is a boolean, the default data type is the default boolean data type.
* - is a complex number object of a known complex data type, the data type is the same as the provided value.
* - is a complex number object of an unknown complex data type, the default data type is the default complex-valued floating-point data type.
* - is any other value type, the default data type is `'generic'`.
Expand Down Expand Up @@ -74,6 +78,8 @@ function scalar2array( value ) {
if ( arguments.length < 2 ) {
if ( flg ) {
dt = DEFAULT_REAL;
} else if ( isBoolean( value ) ) {
dt = DEFAULT_BOOL;
} else if ( isComplexLike( value ) ) {
dt = dtype( value );
if ( dt === null ) {
Expand All @@ -86,7 +92,7 @@ function scalar2array( value ) {
dt = arguments[ 1 ];
}
out = zeros( 1, dt ); // delegate dtype validation to `zeros`
if ( /^complex/.test( dt ) && flg ) {
if ( flg && isComplexDataType( dt ) ) {
v = [ value, 0.0 ]; // note: we're assuming that the ComplexXXArray setter accepts an array of interleaved real and imaginary components
} else {
v = value;
Expand Down
29 changes: 29 additions & 0 deletions lib/node_modules/@stdlib/array/from-scalar/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ var Complex128 = require( '@stdlib/complex/float64/ctor' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var Complex128Array = require( '@stdlib/array/complex128' );
var Complex64Array = require( '@stdlib/array/complex64' );
var BooleanArray = require( '@stdlib/array/bool' );
var Float64Array = require( '@stdlib/array/float64' );
var Float32Array = require( '@stdlib/array/float32' );
var Int32Array = require( '@stdlib/array/int32' );
var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' );
var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' );
var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' );
var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' );
Expand Down Expand Up @@ -78,6 +80,17 @@ tape( 'the function returns a single element containing a provided scalar value
t.end();
});

tape( 'the function returns a single element containing a provided scalar value (default, bool)', function test( t ) {
var expected;
var actual;

actual = array2scalar( true );
expected = new BooleanArray( [ true ] );

t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
t.end();
});

tape( 'the function returns a single element containing a provided scalar value (default, complex128)', function test( t ) {
var expected;
var actual;
Expand Down Expand Up @@ -172,6 +185,22 @@ tape( 'the function returns a single element containing a provided scalar value
t.end();
});

tape( 'the function returns a single element containing a provided scalar value (dtype=bool)', function test( t ) {
var expected;
var actual;

actual = array2scalar( false, 'bool' );
expected = new BooleanArray( [ false ] );

t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );

actual = array2scalar( true, 'bool' );
expected = new BooleanArray( [ true ] );

t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
t.end();
});

tape( 'the function returns a single element containing a provided scalar value (dtype=complex128, complex)', function test( t ) {
var expected;
var actual;
Expand Down
Loading