Skip to content

feat: add boolean dtype support to ndarray/from-scalar #2589

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 3 commits into from
Jul 13, 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
6 changes: 4 additions & 2 deletions lib/node_modules/@stdlib/ndarray/from-scalar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2022 The Stdlib Authors.
Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,7 +66,8 @@ The function accepts the following `options`:

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

- is a `number`, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] real-valued floating-point data type.
- is a number, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] real-valued floating-point data type.
- is a boolean, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/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/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] complex-valued floating-point data type.
- is any other value type, the default [data type][@stdlib/ndarray/dtypes] is `'generic'`.
Expand Down Expand Up @@ -100,6 +101,7 @@ var v = x.get();
## Notes

- 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.
- 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].

</section>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -120,6 +120,30 @@ bench( pkg+':dtype=complex64', function benchmark( b ) {
b.end();
});

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

v = [ true, false ];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = scalar2ndarray( v[ i%2 ], {
'dtype': 'bool'
});
if ( x.length !== 1 ) {
b.fail( 'should have length 1' );
}
}
b.toc();
if ( !isndarrayLike( x ) ) {
b.fail( 'should return an ndarray' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=int32', function benchmark( b ) {
var x;
var i;
Expand Down
1 change: 1 addition & 0 deletions lib/node_modules/@stdlib/ndarray/from-scalar/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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
41 changes: 38 additions & 3 deletions lib/node_modules/@stdlib/ndarray/from-scalar/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,7 @@
/// <reference types="@stdlib/types"/>

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

/**
* Interface defining common options.
Expand Down Expand Up @@ -88,6 +88,16 @@
dtype: 'complex64';
}

/**
* Interface defining options when `dtype` is `'bool'`.
*/
interface BoolOptions extends BaseOptions {
/**
* Output array data type.
*/
dtype: 'bool';
}

/**
* Interface defining options when `dtype` is `'int32'`.
*/
Expand Down Expand Up @@ -286,6 +296,30 @@
*/
declare function scalar2ndarray( value: number | ComplexLike, options: Complex64Options ): complex64ndarray;

/**
* Returns a zero-dimensional ndarray containing a provided scalar value.
*
* @param value - scalar value
* @param options - options
* @returns zero-dimensional ndarray
*
* @example
* var x = scalar2ndarray( true, {
* 'dtype': bool'
* };
* // returns <ndarray>
*
* var sh = x.shape;
* // returns []
*
* var dt = x.dtype;
* // returns 'bool'
*
* var v = x.get();
* // returns true
*/
declare function scalar2ndarray( value: boolean, options: BoolOptions ): boolndarray;

/**
* Returns a zero-dimensional ndarray containing a provided scalar value.
*
Expand Down Expand Up @@ -461,7 +495,8 @@
*
* - 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 All @@ -485,7 +520,7 @@
* var v = x.get();
* // returns 1.0
*/
declare function scalar2ndarray( value: any, options?: Options ): ndarray;

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

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@ import scalar2ndarray = require( './index' );
scalar2ndarray( 1.0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray
scalar2ndarray( 1.0, { 'dtype': 'complex128' } ); // $ExpectType complex128ndarray
scalar2ndarray( 1.0, { 'dtype': 'complex64' } ); // $ExpectType complex64ndarray
scalar2ndarray( true, { 'dtype': 'bool' } ); // $ExpectType boolndarray
scalar2ndarray( 1.0, { 'dtype': 'int32' } ); // $ExpectType int32ndarray
scalar2ndarray( 1.0, { 'dtype': 'int16' } ); // $ExpectType int16ndarray
scalar2ndarray( 1.0, { 'dtype': 'int8' } ); // $ExpectType int8ndarray
Expand Down
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/ndarray/from-scalar/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
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 @@ -39,6 +41,7 @@ var format = require( '@stdlib/string/format' );
var ORDER = defaults.get( 'order' );
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 @@ -50,7 +53,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 @@ -125,6 +129,8 @@ function scalar2ndarray( value ) {
if ( opts.dtype === '' ) {
if ( flg ) {
dt = DEFAULT_REAL;
} else if ( isBoolean( value ) ) {
dt = DEFAULT_BOOL;
} else if ( isComplexLike( value ) ) {
dt = dtype( value );
if ( dt === null ) {
Expand All @@ -140,7 +146,7 @@ function scalar2ndarray( value ) {
if ( buf === null ) {
throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', dt ) );
}
if ( /^complex/.test( dt ) && flg ) {
if ( isComplexDataType( dt ) && flg ) {
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
68 changes: 67 additions & 1 deletion lib/node_modules/@stdlib/ndarray/from-scalar/test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,8 +32,10 @@ var Uint8Array = require( '@stdlib/array/uint8' );
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var instanceOf = require( '@stdlib/assert/instance-of' );
Expand Down Expand Up @@ -230,6 +232,37 @@ tape( 'the function returns a zero-dimensional ndarray (default, complex64)', fu
t.end();
});

tape( 'the function returns a zero-dimensional ndarray (default, bool)', function test( t ) {
var expected;
var arr;

expected = new Uint8Array( [ 1 ] );

arr = scalar2ndarray( true );

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
t.deepEqual( arr.shape, [], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
t.strictEqual( arr.length, 1, 'returns expected value' );

expected = new Uint8Array( [ 0 ] );

arr = scalar2ndarray( false );

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
t.deepEqual( arr.shape, [], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
t.strictEqual( arr.length, 1, 'returns expected value' );

t.end();
});

tape( 'the function returns a zero-dimensional ndarray (default, other)', function test( t ) {
var expected;
var arr;
Expand Down Expand Up @@ -428,6 +461,39 @@ tape( 'the function returns a zero-dimensional ndarray (dtype=uint8c)', function
t.end();
});

tape( 'the function returns a zero-dimensional ndarray (dtype=bool)', function test( t ) {
var expected;
var arr;

expected = new Uint8Array( [ 1 ] );
arr = scalar2ndarray( true, {
'dtype': 'bool'
});

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
t.deepEqual( arr.shape, [], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
t.strictEqual( arr.length, 1, 'returns expected value' );

expected = new Uint8Array( [ 0 ] );
arr = scalar2ndarray( false, {
'dtype': 'bool'
});

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'bool', 'returns expected value' );
t.deepEqual( arr.shape, [], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, BooleanArray ), true, 'returns expected value' );
t.deepEqual( reinterpretBoolean( arr.data, 0 ), expected, 'returns expected value' );
t.strictEqual( arr.order, 'row-major', 'returns expected value' );
t.strictEqual( arr.length, 1, 'returns expected value' );

t.end();
});

tape( 'the function returns a zero-dimensional ndarray (dtype=complex128, complex)', function test( t ) {
var expected;
var arr;
Expand Down
Loading