diff --git a/from-scalar/README.md b/from-scalar/README.md index a7ae282b..3e95205a 100644 --- a/from-scalar/README.md +++ b/from-scalar/README.md @@ -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. @@ -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 + [@stdlib/ndarray/array]: https://github.com/stdlib-js/ndarray/tree/main/array diff --git a/from-scalar/docs/repl.txt b/from-scalar/docs/repl.txt index 55a0c1d2..916f91ff 100644 --- a/from-scalar/docs/repl.txt +++ b/from-scalar/docs/repl.txt @@ -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) diff --git a/from-scalar/docs/types/index.d.ts b/from-scalar/docs/types/index.d.ts index 366d2acb..83163f4f 100644 --- a/from-scalar/docs/types/index.d.ts +++ b/from-scalar/docs/types/index.d.ts @@ -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 diff --git a/from-scalar/lib/main.js b/from-scalar/lib/main.js index 1ab3fd6c..8480f097 100644 --- a/from-scalar/lib/main.js +++ b/from-scalar/lib/main.js @@ -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 // @@ -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 @@ -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'; } diff --git a/from-scalar/test/test.js b/from-scalar/test/test.js index 5ccb7759..ace56785 100644 --- a/from-scalar/test/test.js +++ b/from-scalar/test/test.js @@ -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' ); @@ -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;