From 45285513b4c57af06aa811745d2c1cc4314a561a Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Thu, 8 Feb 2024 03:33:36 +0000 Subject: [PATCH] Auto-generated commit --- from-scalar/README.md | 138 ++++++++ from-scalar/benchmark/benchmark.js | 544 +++++++++++++++++++++++++++++ from-scalar/docs/repl.txt | 38 ++ from-scalar/docs/types/index.d.ts | 270 ++++++++++++++ from-scalar/docs/types/test.ts | 64 ++++ from-scalar/examples/index.js | 38 ++ from-scalar/lib/index.js | 46 +++ from-scalar/lib/main.js | 106 ++++++ from-scalar/package.json | 70 ++++ from-scalar/test/test.js | 263 ++++++++++++++ lib/index.js | 9 + 11 files changed, 1586 insertions(+) create mode 100644 from-scalar/README.md create mode 100644 from-scalar/benchmark/benchmark.js create mode 100644 from-scalar/docs/repl.txt create mode 100644 from-scalar/docs/types/index.d.ts create mode 100644 from-scalar/docs/types/test.ts create mode 100644 from-scalar/examples/index.js create mode 100644 from-scalar/lib/index.js create mode 100644 from-scalar/lib/main.js create mode 100644 from-scalar/package.json create mode 100644 from-scalar/test/test.js diff --git a/from-scalar/README.md b/from-scalar/README.md new file mode 100644 index 00000000..d9e30f13 --- /dev/null +++ b/from-scalar/README.md @@ -0,0 +1,138 @@ + + +# scalar2array + +> Create a single-element array containing a provided scalar value. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var scalar2array = require( '@stdlib/array/from-scalar' ); +``` + +#### scalar2array( value\[, dtype] ) + +Returns a single-element array containing a provided scalar value. + +```javascript +var x = scalar2array( 3.0 ); +// returns [ 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 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'`. + +To explicitly specify the [data type][@stdlib/array/dtypes] of the returned array, provide a `dtype` argument. + +```javascript +var x = scalar2array( 3.0, 'float32' ); +// returns [ 3.0 ] +``` + +
+ + + + + +
+ +## Notes + +- If `value` is a number and the `dtype` argument is a complex [data type][@stdlib/array/dtypes], the function returns a complex number array 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/array/dtypes]. + +
+ + + + + +
+ +## Examples + + + +```javascript +var Complex128 = require( '@stdlib/complex/float64' ); +var array2scalar = require( '@stdlib/array/from-scalar' ); + +var x = array2scalar( 3.0 ); +// returns [ 3.0 ] + +x = array2scalar( 3, 'int32' ); +// returns [ 3 ] + +x = array2scalar( new Complex128( 3.0, 4.0 ) ); +// returns + +x = array2scalar( {} ); +// returns [ {} ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/from-scalar/benchmark/benchmark.js b/from-scalar/benchmark/benchmark.js new file mode 100644 index 00000000..95a5a8bf --- /dev/null +++ b/from-scalar/benchmark/benchmark.js @@ -0,0 +1,544 @@ +/** +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var complex = require( '@stdlib/complex/cmplx' ); +var pkg = require( './../package.json' ).name; +var scalar2array = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::default,number', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + 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' ); + } + } + b.toc(); + if ( !isCollection ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::default,non-numeric', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + true, + false, + null, + {} + ]; + + 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' ); + } + } + b.toc(); + if ( !isCollection ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::default,complex128', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + complex( 3.0, 4.0, 'float64' ), + complex( 1.0, 2.0, 'float64' ), + complex( 0.0, 0.0, 'float64' ) + ]; + + 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' ); + } + } + b.toc(); + if ( !isCollection ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::default,complex64', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + complex( 3.0, 4.0, 'float32' ), + complex( 1.0, 2.0, 'float32' ), + complex( 0.0, 0.0, 'float32' ) + ]; + + 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' ); + } + } + b.toc(); + if ( !isCollection ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+'::default,complex-like', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + { + 're': 3.0, + 'im': 4.0 + }, + { + 're': 1.0, + 'im': 2.0 + }, + { + 're': 0.0, + 'im': 0.0 + } + ]; + + 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' ); + } + } + 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; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'float64' ); + 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+':dtype=float32', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'float32' ); + 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+':dtype=int32', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'int32' ); + 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+':dtype=int16', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'int16' ); + 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+':dtype=int8', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'int8' ); + 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+':dtype=uint32', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'uint32' ); + 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+':dtype=uint16', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'uint16' ); + 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+':dtype=uint8', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'uint8' ); + 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+':dtype=uint8c', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'uint8c' ); + 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; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'complex128' ); + 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=complex64', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3 + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'complex64' ); + 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+'::complex:dtype=complex128', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + complex( 3.0, 4.0, 'float64' ), + complex( 1.0, 2.0, 'float32' ), + { + 're': 0.0, + 'im': 0.0 + } + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'complex128' ); + 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+'::complex:dtype=complex64', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + complex( 3.0, 4.0, 'float64' ), + complex( 1.0, 2.0, 'float32' ), + { + 're': 0.0, + 'im': 0.0 + } + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'complex64' ); + 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+':dtype=generic', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + 1, + 2, + 3, + true, + false, + [], + {} + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = scalar2array( values[ i%values.length ], 'generic' ); + 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(); +}); diff --git a/from-scalar/docs/repl.txt b/from-scalar/docs/repl.txt new file mode 100644 index 00000000..43fa375c --- /dev/null +++ b/from-scalar/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( value[, dtype] ) + Returns a single-element array containing a provided scalar value. + + If `value` is a number and `dtype` is a complex number data type, the + function returns a complex number array containing a complex number whose + real component equals the provided scalar value and whose imaginary + component is zero. + + Parameters + ---------- + value: any + Scalar value. + + dtype: string (optional) + Data type. If not provided and `value` + + - 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'. + + Returns + ------- + out: Array|TypedArray + Output array. + + Examples + -------- + > var x = {{alias}}( 1.0 ) + [ 1.0 ] + + See Also + -------- + diff --git a/from-scalar/docs/types/index.d.ts b/from-scalar/docs/types/index.d.ts new file mode 100644 index 00000000..6681994b --- /dev/null +++ b/from-scalar/docs/types/index.d.ts @@ -0,0 +1,270 @@ +/* +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ComplexLike, Complex64, Complex128 } from '@stdlib/types/complex'; +import { DataType, Complex128Array, Complex64Array } from '@stdlib/types/array'; + +/** +* 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( 1.0, 'float64' ); +* // returns [ 1.0 ] +*/ +declare function scalar2array( value: number, dtype: 'float64' ): Float64Array; // eslint-disable-line @typescript-eslint/unified-signatures + +/** +* 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( 1.0, 'float32' ); +* // returns [ 1.0 ] +*/ +declare function scalar2array( value: number, dtype: 'float32' ): Float32Array; + +/** +* Returns a single-element array containing a provided scalar value. +* +* ## Notes +* +* - If provided a number, the function returns a complex number array containing a complex number whose real component equals the provided scalar value and whose imaginary component is zero. +* +* @param value - scalar value +* @param dtype - output array data type +* @returns output array +* +* @example +* var Complex128 = require( '@stdlib/complex/float64' ); +* +* var v = new Complex128( 1.0, 2.0 ); +* +* var x = scalar2array( v, 'complex128' ); +* // returns [ 1.0, 2.0 ] +*/ +declare function scalar2array( value: number | ComplexLike, dtype: 'complex128' ): Complex128Array; + +/** +* Returns a single-element array containing a provided scalar value. +* +* ## Notes +* +* - If provided a number, the function returns a complex number array containing a complex number whose real component equals the provided scalar value and whose imaginary component is zero. +* +* @param value - scalar value +* @param dtype - output array data type +* @returns output array +* +* @example +* var Complex64 = require( '@stdlib/complex/float64' ); +* +* var v = new Complex64( 1.0, 2.0 ); +* +* var x = scalar2array( v, 'complex64' ); +* // returns [ 1.0, 2.0 ] +*/ +declare function scalar2array( value: number | ComplexLike, dtype: 'complex64' ): Complex64Array; + +/** +* 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( 1, int32 }; +* // returns [ 1 ] +*/ +declare function scalar2array( value: number, dtype: 'int32' ): Int32Array; + +/** +* 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( 1, int16 }; +* // returns [ 1 ] +*/ +declare function scalar2array( value: number, dtype: 'int16' ): Int16Array; + +/** +* 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( 1, int8' } +* // returns [ 1 ] +*/ +declare function scalar2array( value: number, dtype: 'int8' ): Int8Array; + +/** +* 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( 1, uint32' ); +* // returns [ 1 ] +*/ +declare function scalar2array( value: number, dtype: 'uint32' ): Uint32Array; + +/** +* 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( 1, uint16' ); +* // returns [ 1 ] +*/ +declare function scalar2array( value: number, dtype: 'uint16' ): Uint16Array; + +/** +* 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( 1, uint8 }; +* // returns [ 1 ] +*/ +declare function scalar2array( value: number, dtype: 'uint8' ): Uint8Array; + +/** +* 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( 1, uint8c' ); +* // returns [] +*/ +declare function scalar2array( value: number, dtype: 'uint8c' ): Uint8ClampedArray; + +/** +* 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( 1.0, generic' ); +* // returns [ 1.0 ] +*/ +declare function scalar2array( value: T, dtype: 'generic' ): Array; + +/** +* 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( 1.0 ); +* // returns [ 1.0 ] +*/ +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 Complex64 = require( '@stdlib/complex/float32' ); +* +* var z = new Complex64( 3.0, 4.0 ); +* +* var x = scalar2array( z ); +* // returns [ 3.0, 4.0 ] +*/ +declare function scalar2array( value: Complex64 ): Complex64Array; + +/** +* 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 Complex128 = require( '@stdlib/complex/float64' ); +* +* var z = new Complex128( 3.0, 4.0 ); +* +* var x = scalar2array( z ); +* // returns [ 3.0, 4.0 ] +*/ +declare function scalar2array( value: Complex128 | ComplexLike ): Complex128Array; + +/** +* Returns a single-element array containing a provided scalar value. +* +* ## Notes +* +* - 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 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 +* @param dtype - output array data type +* @returns output array +* +* @example +* var x = scalar2array( 1.0, generic' ); +* // returns [ 1.0 ] +*/ +declare function scalar2array( value: T, dtype?: DataType ): Array; + + +// EXPORTS // + +export = scalar2array; diff --git a/from-scalar/docs/types/test.ts b/from-scalar/docs/types/test.ts new file mode 100644 index 00000000..0234b3dd --- /dev/null +++ b/from-scalar/docs/types/test.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import Complex128 = require( '@stdlib/complex/float64' ); +import Complex64 = require( '@stdlib/complex/float32' ); +import array2scalar = require( './index' ); + + +// TESTS // + +// The function returns an array... +{ + array2scalar( 1.0 ); // $ExpectType Float64Array + 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( 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( 1.0, 'int32' ); // $ExpectType Int32Array + array2scalar( 1.0, 'int16' ); // $ExpectType Int16Array + array2scalar( 1.0, 'int8' ); // $ExpectType Int8Array + array2scalar( 1.0, 'uint32' ); // $ExpectType Uint32Array + array2scalar( 1.0, 'uint16' ); // $ExpectType Uint16Array + array2scalar( 1.0, 'uint8' ); // $ExpectType Uint8Array + array2scalar( 1.0, 'uint8c' ); // $ExpectType Uint8ClampedArray + array2scalar( 1.0, 'generic' ); // $ExpectType number[] +} + +// The compiler throws an error if the function is provided a second argument which is not a recognized/supported data type... +{ + array2scalar( 1.0, '10' ); // $ExpectError + array2scalar( 1.0, 5 ); // $ExpectError + array2scalar( 1.0, false ); // $ExpectError + array2scalar( 1.0, true ); // $ExpectError + array2scalar( 1.0, null ); // $ExpectError + array2scalar( 1.0, [] ); // $ExpectError + array2scalar( 1.0, {} ); // $ExpectError + array2scalar( 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + array2scalar(); // $ExpectError + array2scalar( 1.0, 'float64', 1 ); // $ExpectError +} diff --git a/from-scalar/examples/index.js b/from-scalar/examples/index.js new file mode 100644 index 00000000..8447537d --- /dev/null +++ b/from-scalar/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var Complex128 = require( '@stdlib/complex/float64' ); +var array2scalar = require( './../lib' ); + +var x = array2scalar( 3.0 ); +console.log( x ); +// => [ 3.0 ] + +x = array2scalar( 3, 'int32' ); +console.log( x ); +// => [ 3 ] + +x = array2scalar( new Complex128( 3.0, 4.0 ) ); +console.log( x ); +// => + +x = array2scalar( {} ); +console.log( x ); +// => [ {} ] diff --git a/from-scalar/lib/index.js b/from-scalar/lib/index.js new file mode 100644 index 00000000..e8133bc1 --- /dev/null +++ b/from-scalar/lib/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Create a single-element array containing a provided scalar value. +* +* @module @stdlib/array/from-scalar +* +* @example +* var array2scalar = require( '@stdlib/array/from-scalar' ); +* +* var x = scalar2array( 1.0 ); +* // returns [ 1.0 ] +* +* @example +* var array2scalar = require( '@stdlib/array/from-scalar' ); +* +* var x = scalar2array( 1.0, 'float32' ); +* // returns [ 1.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/from-scalar/lib/main.js b/from-scalar/lib/main.js new file mode 100644 index 00000000..267ec3d1 --- /dev/null +++ b/from-scalar/lib/main.js @@ -0,0 +1,106 @@ +/** +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +var isAccessorArray = require( './../../base/assert/is-accessor-array' ); +var accessorSetter = require( './../../base/accessor-setter' ); +var setter = require( './../../base/setter' ); +var zeros = require( './../../zeros' ); +var dtype = require( '@stdlib/complex/dtype' ); +var defaults = require( './../../defaults' ); + + +// VARIABLES // + +var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' ); +var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' ); + + +// MAIN // + +/** +* Returns a single-element array containing a provided scalar value. +* +* ## Notes +* +* - 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 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 +* @param {string} dtype - output array data type +* @throws {TypeError} second argument must be a recognized data type +* @returns {Collection} output array +* +* @example +* var x = scalar2array( 1.0 ); +* // returns [ 1.0 ] +* +* @example +* var x = scalar2array( 1.0, 'float32' ); +* // returns [ 1.0 ] +*/ +function scalar2array( value ) { + var flg; + var out; + var set; + var dt; + var v; + + flg = isNumber( value ); + if ( arguments.length < 2 ) { + if ( flg ) { + dt = DEFAULT_REAL; + } else if ( isComplexLike( value ) ) { + dt = dtype( value ); + if ( dt === null ) { + dt = DEFAULT_CMPLX; + } + } else { + dt = 'generic'; + } + } else { + dt = arguments[ 1 ]; + } + out = zeros( 1, dt ); // delegate dtype validation to `zeros` + if ( /^complex/.test( 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; + } + if ( isAccessorArray( out ) ) { + set = accessorSetter( dt ); + } else { + set = setter( dt ); + } + set( out, 0, v ); + return out; +} + + +// EXPORTS // + +module.exports = scalar2array; diff --git a/from-scalar/package.json b/from-scalar/package.json new file mode 100644 index 00000000..f95da0ad --- /dev/null +++ b/from-scalar/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/array/from-scalar", + "version": "0.0.0", + "description": "Create a single-element array containing a provided scalar value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "utils", + "util", + "utilities", + "utility", + "typed", + "array", + "arr", + "typed-array", + "typed array", + "scalar", + "to", + "convert", + "cast", + "fill", + "wrap" + ] +} diff --git a/from-scalar/test/test.js b/from-scalar/test/test.js new file mode 100644 index 00000000..ef4a0c6e --- /dev/null +++ b/from-scalar/test/test.js @@ -0,0 +1,263 @@ +/** +* @license Apache-2.0 +* +* 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Complex128 = require( '@stdlib/complex/float64' ); +var Complex64 = require( '@stdlib/complex/float32' ); +var Complex128Array = require( './../../complex128' ); +var Complex64Array = require( './../../complex64' ); +var Float64Array = require( './../../float64' ); +var Float32Array = require( './../../float32' ); +var Int32Array = require( './../../int32' ); +var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); +var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var isSameArray = require( '@stdlib/assert/is-same-array' ); +var isInt32Array = require( '@stdlib/assert/is-int32array' ); +var array2scalar = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof array2scalar, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an unsupported data type', function test( t ) { + var values; + var i; + + values = [ + 'foo', + 'bar', + 'beep', + 'boop' + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + array2scalar( 3, value ); + }; + } +}); + +tape( 'the function returns a single element containing a provided scalar value (default, number)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( 3.0 ); + expected = new Float64Array( [ 3.0 ] ); + + t.strictEqual( isSameFloat64Array( 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; + + actual = array2scalar( new Complex128( 3.0, 4.0 ) ); + expected = new Complex128Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (default, complex64)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( new Complex64( 3.0, 4.0 ) ); + expected = new Complex64Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (default, complex-like)', function test( t ) { + var expected; + var actual; + + actual = array2scalar({ + 're': 3.0, + 'im': 4.0 + }); + expected = new Complex128Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (default, object)', function test( t ) { + var expected; + var actual; + var o; + + o = {}; + actual = array2scalar( o ); + expected = [ o ]; + + t.strictEqual( isSameArray( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=float64)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( 3.0, 'float64' ); + expected = new Float64Array( [ 3.0 ] ); + + t.strictEqual( isSameFloat64Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=float32)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( 3.0, 'float32' ); + expected = new Float32Array( [ 3.0 ] ); + + t.strictEqual( isSameFloat32Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=int32)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( 3, 'int32' ); + expected = new Int32Array( [ 3 ] ); + + t.strictEqual( isInt32Array( actual ), true, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=generic)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( 3.0, 'generic' ); + expected = [ 3.0 ]; + + t.strictEqual( isSameArray( 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; + var z; + + actual = array2scalar( new Complex128( 3.0, 4.0 ), 'complex128' ); + expected = new Complex128Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + actual = array2scalar( new Complex64( 3.0, 4.0 ), 'complex128' ); + expected = new Complex128Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + + z = { + 're': 3.0, + 'im': 4.0 + }; + actual = array2scalar( z, 'complex128' ); + expected = new Complex128Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=complex64, complex)', function test( t ) { + var expected; + var actual; + var z; + + actual = array2scalar( new Complex64( 3.0, 4.0 ), 'complex64' ); + expected = new Complex64Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + actual = array2scalar( new Complex128( 3.0, 4.0 ), 'complex64' ); + expected = new Complex64Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + + z = { + 're': 3.0, + 'im': 4.0 + }; + actual = array2scalar( z, 'complex64' ); + expected = new Complex64Array( [ 3.0, 4.0 ] ); + + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=generic, complex-like)', function test( t ) { + var expected; + var actual; + var z; + + z = { + 're': 3.0, + 'im': 4.0 + }; + actual = array2scalar( z, 'generic' ); + expected = [ z ]; + + t.strictEqual( isSameArray( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=complex128, real)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( 3.0, 'complex128' ); + expected = new Complex128Array( [ 3.0, 0.0 ] ); + + t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a single element containing a provided scalar value (dtype=complex64, real)', function test( t ) { + var expected; + var actual; + + actual = array2scalar( 3.0, 'complex64' ); + expected = new Complex64Array( [ 3.0, 0.0 ] ); + + t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/index.js b/lib/index.js index 64a0598d..7f99f9b1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -238,6 +238,15 @@ setReadOnly( ns, 'Float64Array', require( './../float64' ) ); */ setReadOnly( ns, 'iterator2array', require( './../from-iterator' ) ); +/** +* @name scalar2array +* @memberof ns +* @readonly +* @constructor +* @see {@link module:@stdlib/array/from-scalar} +*/ +setReadOnly( ns, 'scalar2array', require( './../from-scalar' ) ); + /** * @name afull * @memberof ns