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 Jul 13, 2024
1 parent c6e6577 commit 2898013
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@

##### Features

- [`f766a56`](https://github.com/stdlib-js/stdlib/commit/f766a563e112098dc229991c0eedb5f5b7417811) - add boolean dtype support to `ndarray/from-scalar` [(#2589)](https://github.com/stdlib-js/stdlib/pull/2589)
- [`9e08caf`](https://github.com/stdlib-js/stdlib/commit/9e08caf0e897040e9b82ff104cb5a09d6b03465e) - return a `complex64` dtype ndarray if provided a Complex64 scalar

</section>
Expand Down Expand Up @@ -527,6 +528,7 @@ A total of 3 people contributed to this release. Thank you to the following cont

<details>

- [`f766a56`](https://github.com/stdlib-js/stdlib/commit/f766a563e112098dc229991c0eedb5f5b7417811) - **feat:** add boolean dtype support to `ndarray/from-scalar` [(#2589)](https://github.com/stdlib-js/stdlib/pull/2589) _(by Jaysukh Makvana, Athan Reines)_
- [`a360f04`](https://github.com/stdlib-js/stdlib/commit/a360f048dde8429a3ffcc60d36abe9ad33038c73) - **feat:** add boolean dtype support to `ndarray/base/unary` [(#2587)](https://github.com/stdlib-js/stdlib/pull/2587) _(by Jaysukh Makvana)_
- [`19d4a8d`](https://github.com/stdlib-js/stdlib/commit/19d4a8da27facd0cc72b6162dc7e6b0d66d7a63c) - **feat:** add boolean dtype support to `ndarray/base/nullary` [(#2586)](https://github.com/stdlib-js/stdlib/pull/2586) _(by Jaysukh Makvana)_
- [`c067b6c`](https://github.com/stdlib-js/stdlib/commit/c067b6c990c99b8f4cf315b5378af8574098962b) - **docs:** update namespace table of contents [(#2576)](https://github.com/stdlib-js/stdlib/pull/2576) _(by stdlib-bot, Philipp Burckhardt)_
Expand Down
6 changes: 4 additions & 2 deletions 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
26 changes: 25 additions & 1 deletion from-scalar/benchmark/benchmark.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 @@ -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 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 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 @@ interface Complex64Options extends BaseOptions {
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: Complex12
*/
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 @@ declare function scalar2ndarray( value: number, options: Uint8cOptions ): uint8c
*
* - 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
3 changes: 2 additions & 1 deletion from-scalar/docs/types/test.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 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 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 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

0 comments on commit 2898013

Please sign in to comment.