diff --git a/CHANGELOG.md b/CHANGELOG.md index cd5974b3..b3c62511 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1580,6 +1580,7 @@ This release closes the following issue: ##### Features +- [`3a3116e`](https://github.com/stdlib-js/stdlib/commit/3a3116e3ff5bef42e4b4f39e5375b89a877ccff0) - add boolean dtype support to `array/from-scalar` [(#2470)](https://github.com/stdlib-js/stdlib/pull/2470) - [`d48e651`](https://github.com/stdlib-js/stdlib/commit/d48e65119020146552bc6229133ef0cd2f4cc611) - add `array/from-scalar` @@ -2290,6 +2291,7 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`3a3116e`](https://github.com/stdlib-js/stdlib/commit/3a3116e3ff5bef42e4b4f39e5375b89a877ccff0) - **feat:** add boolean dtype support to `array/from-scalar` [(#2470)](https://github.com/stdlib-js/stdlib/pull/2470) _(by Jaysukh Makvana, Athan Reines)_ - [`8a55ea2`](https://github.com/stdlib-js/stdlib/commit/8a55ea29ae7cc04e9ebef428ee90900641bbabe1) - **feat:** add boolean dtype support to `array/filled` [(#2471)](https://github.com/stdlib-js/stdlib/pull/2471) _(by Jaysukh Makvana, Athan Reines)_ - [`d1ef4ee`](https://github.com/stdlib-js/stdlib/commit/d1ef4ee589cb2a7a0d8bbc0b37be3cf3b7a6aad2) - **feat:** add boolean dtype support to `array/base/count-same-value-zero` [(#2474)](https://github.com/stdlib-js/stdlib/pull/2474) _(by Jaysukh Makvana, Athan Reines)_ - [`fd396b3`](https://github.com/stdlib-js/stdlib/commit/fd396b360cbca9c4c000d3f33eed5dfc18de6d6b) - **feat:** add boolean dtype support to `array/base/count-same-value` [(#2473)](https://github.com/stdlib-js/stdlib/pull/2473) _(by Jaysukh Makvana, Athan Reines)_ diff --git a/from-scalar/README.md b/from-scalar/README.md index fa7aeed8..c2aff723 100644 --- a/from-scalar/README.md +++ b/from-scalar/README.md @@ -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'`. diff --git a/from-scalar/benchmark/benchmark.js b/from-scalar/benchmark/benchmark.js index 95a5a8bf..92463741 100644 --- a/from-scalar/benchmark/benchmark.js +++ b/from-scalar/benchmark/benchmark.js @@ -169,6 +169,32 @@ bench( pkg+'::default,complex-like', function benchmark( b ) { 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 ); + 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; @@ -403,6 +429,31 @@ bench( pkg+':dtype=uint8c', function benchmark( b ) { 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; diff --git a/from-scalar/docs/repl.txt b/from-scalar/docs/repl.txt index 43fa375c..0a66ea6b 100644 --- a/from-scalar/docs/repl.txt +++ b/from-scalar/docs/repl.txt @@ -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 diff --git a/from-scalar/docs/types/index.d.ts b/from-scalar/docs/types/index.d.ts index f86459a1..dcc45a44 100644 --- a/from-scalar/docs/types/index.d.ts +++ b/from-scalar/docs/types/index.d.ts @@ -21,7 +21,7 @@ /// 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. @@ -49,6 +49,19 @@ declare function scalar2array( value: number, dtype: 'float64' ): Float64Array; */ 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 +*/ +declare function scalar2array( value: any, dtype: 'bool' ): BooleanArray; + /** * Returns a single-element array containing a provided scalar value. * @@ -208,6 +221,19 @@ declare function scalar2array( value: T, dtype: 'generic' ): Array< */ 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 +*/ +declare function scalar2array( value: boolean ): BooleanArray; + /** * Returns a single-element array containing a provided scalar value. * @@ -249,7 +275,8 @@ declare function scalar2array( value: Complex128 | ComplexLike ): Complex128Arra * * - 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'`. diff --git a/from-scalar/docs/types/test.ts b/from-scalar/docs/types/test.ts index f13cbe63..700493ab 100644 --- a/from-scalar/docs/types/test.ts +++ b/from-scalar/docs/types/test.ts @@ -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 diff --git a/from-scalar/lib/main.js b/from-scalar/lib/main.js index 267ec3d1..2f7dd8b5 100644 --- a/from-scalar/lib/main.js +++ b/from-scalar/lib/main.js @@ -20,8 +20,10 @@ // MODULES // +var isComplexDataType = require( './../../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( './../../base/assert/is-accessor-array' ); var accessorSetter = require( './../../base/accessor-setter' ); var setter = require( './../../base/setter' ); @@ -34,6 +36,7 @@ var defaults = require( './../../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 // @@ -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'`. @@ -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 ) { @@ -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; diff --git a/from-scalar/test/test.js b/from-scalar/test/test.js index 2656f4dc..07158afc 100644 --- a/from-scalar/test/test.js +++ b/from-scalar/test/test.js @@ -25,9 +25,11 @@ var Complex128 = require( '@stdlib/complex/float64/ctor' ); var Complex64 = require( '@stdlib/complex/float32/ctor' ); var Complex128Array = require( './../../complex128' ); var Complex64Array = require( './../../complex64' ); +var BooleanArray = require( './../../bool' ); var Float64Array = require( './../../float64' ); var Float32Array = require( './../../float32' ); var Int32Array = require( './../../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' ); @@ -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; @@ -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;