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 Feb 8, 2024
1 parent 8843c9d commit bc8b428
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
7 changes: 5 additions & 2 deletions from-scalar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ 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 `'float64'`.
- is a complex number object, the default [data type][@stdlib/ndarray/dtypes] is `'complex128'`.
- is a `number`, the default [data type][@stdlib/ndarray/dtypes] is the [default][@stdlib/ndarray/defaults] real-valued floating-point 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'`.

To explicitly specify the [data type][@stdlib/ndarray/dtypes] of the returned [`ndarray`][@stdlib/ndarray/ctor], provide a `dtype` option.
Expand Down Expand Up @@ -165,6 +166,8 @@ for ( i = 0; i < dt.length; i++ ) {

[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes

[@stdlib/ndarray/defaults]: https://github.com/stdlib-js/ndarray/tree/main/defaults

<!-- <related-links> -->

[@stdlib/ndarray/array]: https://github.com/stdlib-js/ndarray/tree/main/array
Expand Down
8 changes: 6 additions & 2 deletions from-scalar/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
options.dtype: string (optional)
Data type. If not provided and `value`

- is a number, the default data type is 'float64'.
- is a complex number object, the default data type is 'complex128'.
- is a number, the default data type is the default real-valued
floating-point 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
type is the default complex-valued floating-point data type.
- is any other value type, the default data type is 'generic'.

options.order: string (optional)
Expand Down
5 changes: 3 additions & 2 deletions from-scalar/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,9 @@ 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 `'float64'`.
* - is a complex number object, the default data type is `'complex128'`.
* - is a `number`, the default data type is the default real-valued floating-point 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'`.
*
* @param value - scalar value
Expand Down
15 changes: 11 additions & 4 deletions from-scalar/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ var setter = require( '@stdlib/array/base/setter' );
var buffer = require( './../../base/buffer' );
var ndarray = require( './../../ctor' );
var defaults = require( './../../defaults' );
var dtype = require( '@stdlib/complex/dtype' );
var format = require( '@stdlib/string/format' );


// VARIABLES //

var ORDER = defaults.get( 'order' );
var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' );
var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );


// MAIN //
Expand All @@ -47,8 +50,9 @@ var ORDER = defaults.get( 'order' );
*
* - If a `dtype` option is not provided and `value`
*
* - is a `number`, the default data type is `'float64'`.
* - is a complex number object, the default data type is `'complex128'`.
* - is a `number`, the default data type is the default real-valued floating-point 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'`.
*
* @param {*} value - scalar value
Expand Down Expand Up @@ -120,9 +124,12 @@ function scalar2ndarray( value ) {
flg = isNumber( value );
if ( opts.dtype === '' ) {
if ( flg ) {
dt = 'float64';
dt = DEFAULT_REAL;
} else if ( isComplexLike( value ) ) {
dt = 'complex128';
dt = dtype( value );
if ( dt === null ) {
dt = DEFAULT_CMPLX;
}
} else {
dt = 'generic';
}
Expand Down
25 changes: 23 additions & 2 deletions from-scalar/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ tape( 'the function returns a zero-dimensional ndarray (default, number)', funct
t.end();
});

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

expected = new Float64Array( [ 1.0, 2.0 ] );

v = new Complex64( 1.0, 2.0 );
v = new Complex128( 1.0, 2.0 );
arr = scalar2ndarray( v );

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
Expand All @@ -209,6 +209,27 @@ tape( 'the function returns a zero-dimensional ndarray (default, complex)', func
t.end();
});

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

expected = new Float32Array( [ 1.0, 2.0 ] );

v = new Complex64( 1.0, 2.0 );
arr = scalar2ndarray( v );

t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
t.strictEqual( arr.dtype, 'complex64', 'returns expected value' );
t.deepEqual( arr.shape, [], 'returns expected value' );
t.strictEqual( instanceOf( arr.data, Complex64Array ), true, 'returns expected value' );
t.deepEqual( reinterpret64( 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

0 comments on commit bc8b428

Please sign in to comment.