Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Jun 29, 2024
1 parent 1a8eb3e commit efa6d78
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

</section>
Expand Down Expand Up @@ -2290,6 +2291,7 @@ A total of 13 people contributed to this release. Thank you to the following con

<details>

- [`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)_
Expand Down
3 changes: 2 additions & 1 deletion 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 from-scalar/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions 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 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: '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 <BooleanArray>
*/
declare function scalar2array( value: any, dtype: 'bool' ): BooleanArray;

/**
* Returns a single-element array containing a provided scalar value.
*
Expand Down Expand Up @@ -208,6 +221,19 @@ declare function scalar2array<T = unknown>( 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 <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 @@ 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'`.
Expand Down
2 changes: 2 additions & 0 deletions 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 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( './../../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' );
Expand All @@ -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 //
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 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( './../../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' );
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

0 comments on commit efa6d78

Please sign in to comment.