From d18e98aaf9815c3eaa514beb785c4db37662aa9d Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 28 Jan 2026 14:03:21 +0500 Subject: [PATCH 1/3] feat: add ndarray/from-scalar-like --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/from-scalar-like/README.md | 189 ++++ .../from-scalar-like/benchmark/benchmark.js | 354 ++++++++ .../ndarray/from-scalar-like/docs/repl.txt | 59 ++ .../from-scalar-like/docs/types/index.d.ts | 669 +++++++++++++++ .../from-scalar-like/docs/types/test.ts | 116 +++ .../from-scalar-like/examples/index.js | 38 + .../ndarray/from-scalar-like/lib/index.js | 54 ++ .../ndarray/from-scalar-like/lib/main.js | 136 +++ .../ndarray/from-scalar-like/package.json | 62 ++ .../ndarray/from-scalar-like/test/test.js | 807 ++++++++++++++++++ 10 files changed, 2484 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/from-scalar-like/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md b/lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md new file mode 100644 index 000000000000..c972296d498a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md @@ -0,0 +1,189 @@ + + +# scalar2ndarrayLike + +> Convert a scalar value to a zero-dimensional [ndarray][@stdlib/ndarray/ctor] having the same [data-type][@stdlib/ndarray/dtypes] as a provided [ndarray][@stdlib/ndarray/ctor]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var scalar2ndarrayLike = require( '@stdlib/ndarray/from-scalar-like' ); +``` + +#### scalar2ndarrayLike( x, value\[, options] ) + +Converts a scalar value to a zero-dimensional [ndarray][@stdlib/ndarray/ctor] having the same [data-type][@stdlib/ndarray/dtypes] as a provided [ndarray][@stdlib/ndarray/ctor] + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( new Float64Array( [ 1.0, 2.0, 3.0 ] ) ); +// returns [ 1.0, 2.0, 3.0 ] + +var out = scalar2ndarrayLike( x, 1.0 ); +// returns [ 1.0 ] + +var sh = getShape( out ); +// returns [] + +var dt = getDType( out ); +// returns 'float64' +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **value**: scalar value. + +The function accepts the following `options`: + +- **dtype**: output array [data type][@stdlib/ndarray/dtypes]. +- **order**: array order (i.e., memory layout), which is either `row-major` (C-style) or `column-major` (Fortran-style). Default: `'row-major'`. +- **readonly**: `boolean` indicating whether an array should be **read-only**. Default: `false`. + +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 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'`. + +To explicitly specify the [data type][@stdlib/ndarray/dtypes] of the returned [`ndarray`][@stdlib/ndarray/ctor], provide a `dtype` option. + +```javascript +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ 1.0, 2.0, 3.0 ] ); +// returns [ 1.0, 2.0, 3.0 ] + +var out = scalar2ndarrayLike( x, 1.0, { + 'dtype': 'float32' +}); +// returns [ 1.0 ] + +var sh = getShape( out ); +// returns [] + +var dt = getDType( out ); +// returns 'float32' +``` + +
+ + + + + +
+ +## 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]. + +
+ + + + + +
+ +## Examples + + + +```javascript +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var empty = require( '@stdlib/ndarray/empty' ); +var scalar2ndarrayLike = require( '@stdlib/ndarray/from-scalar-like' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Generate zero-dimensional arrays... +var x; +var y; +var i; +for ( i = 0; i < dt.length; i++ ) { + x = empty( [ 2, 2 ] ); + y = scalar2ndarrayLike( x, i, { + 'dtype': dt[ i ] + }); + console.log( x.get() ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/from-scalar-like/benchmark/benchmark.js new file mode 100644 index 000000000000..490473ba2feb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/benchmark/benchmark.js @@ -0,0 +1,354 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var scalar2ndarrayLike = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:dtype=float64', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'float64' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'float32' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) { + var x; + var o; + var v; + var i; + + v = new Complex128( 1.0, 2.0 ); + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, v, { + 'dtype': 'complex128' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex64', pkg ), function benchmark( b ) { + var x; + var o; + var v; + var i; + + v = new Complex64( 1.0, 2.0 ); + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, v, { + 'dtype': 'complex64' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=bool', pkg ), function benchmark( b ) { + var x; + var o; + var v; + var i; + + v = [ true, false ]; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, v[ i%2 ], { + 'dtype': 'bool' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int32', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'int32' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint32', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'uint32' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int16', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'int16' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint16', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'uint16' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int8', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'int8' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'uint8' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8c', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'uint8c' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=generic', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = scalar2ndarrayLike( x, i, { + 'dtype': 'generic' + }); + if ( o.length !== 1 ) { + b.fail( 'should have length 1' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/repl.txt new file mode 100644 index 000000000000..9bdf39924ccc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/repl.txt @@ -0,0 +1,59 @@ + +{{alias}}( x, value[, options] ) + Converts a scalar value to a zero-dimensional ndarray having the same data + type as a provided ndarray. + + If `value` is a number and `options.dtype` is a complex number data type, + the function returns a zero-dimensional ndarray containing a complex number + whose real component equals the provided scalar value and whose imaginary + component is zero. + + Parameters + ---------- + x: ndarray + Input ndarray. + + value: any + Scalar value. + + options: Object (optional) + Options. + + options.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 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 + 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) + Specifies whether an array is row-major (C-style) or column-major + (Fortran-style). Default: 'row-major'. + + options.readonly: boolean (optional) + Boolean indicating whether an array should be read-only. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, 2.0, 3.0 ] ) + [ 1.0, 2.0, 3.0 ] + > var x = {{alias}}( x, 1.0, { 'dtype': 'float64' } ) + [ 1.0 ] + > var sh = {{alias:@stdlib/ndarray/shape}}( x ) + [] + > var dt = {{alias:@stdlib/ndarray/dtype}}( x ) + 'float64' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts new file mode 100644 index 000000000000..6d5792555e85 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts @@ -0,0 +1,669 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 } from '@stdlib/types/complex'; +import { float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, complexndarray, typedndarray, genericndarray, boolndarray, DataType, Order } from '@stdlib/types/ndarray'; + +/** +* Interface defining common options. +*/ +interface BaseOptions { + /** + * Specifies whether an array is row-major (C-style) or column-major (Fortran-style). Default: 'row-major'. + */ + order?: Order; + + /** + * Boolean indicating whether an array should be read-only. Default: false. + */ + readonly?: boolean; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; +} + +/** +* Interface defining options when `dtype` is `'float64'`. +*/ +interface Float64Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'float64'; +} + +/** +* Interface defining options when `dtype` is `'float32'`. +*/ +interface Float32Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'float32'; +} + +/** +* Interface defining options when `dtype` is `'complex128'`. +*/ +interface Complex128Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'complex128'; +} + +/** +* Interface defining options when `dtype` is `'complex64'`. +*/ +interface Complex64Options extends BaseOptions { + /** + * Output array data type. + */ + 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'`. +*/ +interface Int32Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'int32'; +} + +/** +* Interface defining options when `dtype` is `'int16'`. +*/ +interface Int16Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'int16'; +} + +/** +* Interface defining options when `dtype` is `'int8'`. +*/ +interface Int8Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'int8'; +} + +/** +* Interface defining options when `dtype` is `'uint32'`. +*/ +interface Uint32Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'uint32'; +} + +/** +* Interface defining options when `dtype` is `'uint16'`. +*/ +interface Uint16Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'uint16'; +} + +/** +* Interface defining options when `dtype` is `'uint8'`. +*/ +interface Uint8Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'uint8'; +} + +/** +* Interface defining options when `dtype` is `'uint8c'`. +*/ +interface Uint8cOptions extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'uint8c'; +} + +/** +* Interface defining options when `dtype` is `'generic'`. +*/ +interface GenericOptions extends BaseOptions { + /** + * Output array data type. + */ + dtype: 'generic'; +} + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'float64' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number ): typedndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'float64' +* }); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'float64' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Float64Options ): float64ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'float32' +* }); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'float32' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Float32Options ): float32ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'float64' +*/ +declare function scalar2ndarrayLike( x: complexndarray, value: number | ComplexLike ): complexndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'complex128' +* }); +* // returns [ [ 1.0, 0.0 ] ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'complex128' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number | ComplexLike, options: Complex128Options ): complex128ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'complex64' +* }); +* // returns [ [ 1.0, 0.0 ] ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'complex64' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number | ComplexLike, options: Complex64Options ): complex64ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'bool' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ true ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'bool' +*/ +declare function scalar2ndarrayLike( x: boolndarray, value: number | boolean ): boolndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'bool' +* }); +* // returns [ true ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'bool' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number | boolean, options: BoolOptions ): boolndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'int32' +* }); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'int32' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Int32Options ): int32ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'int16' +* }); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'int16' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Int16Options ): int16ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'int8' +* }); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'int8' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Int8Options ): int8ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'uint32' +* }); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint32' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint32Options ): uint32ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'uint16' +* }); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint16' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint16Options ): uint16ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'uint8' +* }); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint8' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint8Options ): uint8ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'uint8c' +* }); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint8c' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint8cOptions ): uint8cndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'generic' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'generic' +*/ +declare function scalar2ndarrayLike( x: genericndarray, value: U ): genericndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ] ); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'generic' +* }); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'generic' +*/ +declare function scalar2ndarrayLike( x: typedndarray, value: U, options?: GenericOptions ): genericndarray; + + +// EXPORTS // + +export = scalar2ndarrayLike; diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts new file mode 100644 index 000000000000..184b44e65fb4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts @@ -0,0 +1,116 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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 empty = require( '@stdlib/ndarray/empty' ); +import scalar2ndarrayLike = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + var x = empty( [ 1 ] ); + + scalar2ndarrayLike( x, 1.0 ); // $ExpectType typedndarray + + scalar2ndarrayLike( x, 1.0, { 'dtype': 'float64' } ); // $ExpectType float64ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'complex128' } ); // $ExpectType complex128ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'complex64' } ); // $ExpectType complex64ndarray + scalar2ndarrayLike( x, true, { 'dtype': 'bool' } ); // $ExpectType boolndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'int32' } ); // $ExpectType int32ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'int16' } ); // $ExpectType int16ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'int8' } ); // $ExpectType int8ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'uint32' } ); // $ExpectType uint32ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'uint16' } ); // $ExpectType uint16ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'uint8' } ); // $ExpectType uint8ndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'uint8c' } ); // $ExpectType uint8cndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'generic' } ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + scalar2ndarrayLike( '10', 1.0 ); // $ExpectError + scalar2ndarrayLike( 5, 1.0 ); // $ExpectError + scalar2ndarrayLike( false, 1.0 ); // $ExpectError + scalar2ndarrayLike( true, 1.0 ); // $ExpectError + scalar2ndarrayLike( null, 1.0 ); // $ExpectError + scalar2ndarrayLike( [], 1.0 ); // $ExpectError + scalar2ndarrayLike( ( x: number ): number => x, 1.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + var x = empty( [ 1 ] ); + + scalar2ndarrayLike( x, 1.0, '10' ); // $ExpectError + scalar2ndarrayLike( x, 1.0, 5 ); // $ExpectError + scalar2ndarrayLike( x, 1.0, false ); // $ExpectError + scalar2ndarrayLike( x, 1.0, true ); // $ExpectError + scalar2ndarrayLike( x, 1.0, null ); // $ExpectError + scalar2ndarrayLike( x, 1.0, [] ); // $ExpectError + scalar2ndarrayLike( x, 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dtype` option which is not a recognized/supported data type... +{ + var x = empty( [ 1 ] ); + + scalar2ndarrayLike( x, 1.0, { 'dtype': '10' } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'dtype': 5 } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'dtype': false } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'dtype': true } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'dtype': null } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'dtype': [] } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'dtype': {} } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `order` option which is not a recognized/supported data order... +{ + var x = empty( [ 1 ] ); + + scalar2ndarrayLike( x, 1.0, { 'order': '10' } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'order': 5 } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'order': false } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'order': true } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'order': null } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'order': [] } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'order': {} } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'order': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `readonly` option which is not a boolean... +{ + var x = empty( [ 1 ] ); + + scalar2ndarrayLike( x, 1.0, { 'readonly': '10' } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'readonly': 5 } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'readonly': null } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'readonly': [] } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'readonly': {} } ); // $ExpectError + scalar2ndarrayLike( x, 1.0, { 'readonly': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + var x = empty( [ 1 ] ); + + scalar2ndarrayLike(); // $ExpectError + scalar2ndarrayLike( x, 1.0, {}, 1 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/examples/index.js b/lib/node_modules/@stdlib/ndarray/from-scalar-like/examples/index.js new file mode 100644 index 000000000000..8fa17b1ed953 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 dtypes = require( '@stdlib/ndarray/dtypes' ); +var empty = require( '@stdlib/ndarray/empty' ); +var scalar2ndarrayLike = require( './../lib' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Generate zero-dimensional arrays... +var x; +var y; +var i; +for ( i = 0; i < dt.length; i++ ) { + x = empty( [ 2, 2 ] ); + y = scalar2ndarrayLike( x, i, { + 'dtype': dt[ i ] + }); + console.log( y.get() ); +} diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/index.js b/lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/index.js new file mode 100644 index 000000000000..bc8e47d55d36 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/index.js @@ -0,0 +1,54 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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'; + +/** +* Convert a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @module @stdlib/ndarray/from-scalar-like +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var array = require( '@stdlib/ndarray/array' ); +* var scalar2ndarrayLike = require( '@stdlib/ndarray/from-scalar-like' ); +* +* var x = array( [ 1.0, 2.0, 3.0 ] ); +* // returns [ 1.0, 2.0, 3.0 ] +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'float64' +* }); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'float64' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/main.js b/lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/main.js new file mode 100644 index 000000000000..b8af9a03b3aa --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/lib/main.js @@ -0,0 +1,136 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 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 isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var accessorSetter = require( '@stdlib/array/base/accessor-setter' ); +var setter = require( '@stdlib/array/base/setter' ); +var buffer = require( '@stdlib/ndarray/base/buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Convert a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* ## 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 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'`. +* +* @param {ndarray} x - input ndarray +* @param {*} value - scalar value +* @param {Options} [options] - function options +* @param {string} [options.dtype] - output array data type +* @param {string} [options.order="row-major"] - memory layout (either row-major or column-major) +* @param {boolean} [options.readonly=false] - boolean indicating whether an array should be read-only +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} options argument must be an object +* @throws {TypeError} `dtype` option must be a recognized data type +* @returns {ndarray} ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ 1.0, 2.0, 3.0 ] ); +* // returns [ 1.0, 2.0, 3.0 ] +* +* var out = scalar2ndarrayLike( x, 1.0, { +* 'dtype': 'float64' +* }); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'float64' +*/ +function scalar2ndarrayLike( x, value, options ) { + var opts; + var buf; + var flg; + var set; + var dt; + var v; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + opts = { + 'dtype': getDType( x ), + 'order': getOrder( x ), + 'readonly': false + }; + if ( arguments.length > 2 ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'dtype' ) ) { + opts.dtype = options.dtype; + } + if ( hasOwnProp( options, 'order' ) ) { + opts.order = options.order; + } + if ( hasOwnProp( options, 'readonly' ) ) { + opts.readonly = options.readonly; + } + } + flg = isNumber( value ); + dt = opts.dtype; + buf = buffer( dt, 1 ); + if ( buf === null ) { + throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', dt ) ); + } + 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; + } + if ( isAccessorArray( buf ) ) { + set = accessorSetter( dt ); + } else { + set = setter( dt ); + } + set( buf, 0, v ); + return new ndarray( dt, buf, [], [ 0 ], 0, opts.order, opts ); +} + + +// EXPORTS // + +module.exports = scalar2ndarrayLike; diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/package.json b/lib/node_modules/@stdlib/ndarray/from-scalar-like/package.json new file mode 100644 index 000000000000..eb624af886e8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/package.json @@ -0,0 +1,62 @@ +{ + "name": "@stdlib/ndarray/from-scalar-like", + "version": "0.0.0", + "description": "Convert a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray.", + "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", + "types", + "data", + "structure", + "ndarray", + "scalar", + "wrap", + "convert" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/test/test.js b/lib/node_modules/@stdlib/ndarray/from-scalar-like/test/test.js new file mode 100644 index 000000000000..c822d4bea77c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/test/test.js @@ -0,0 +1,807 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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 Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int8Array = require( '@stdlib/array/int8' ); +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' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var numel = require( '@stdlib/ndarray/numel' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var scalar2ndarrayLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof scalar2ndarrayLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + 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() { + scalar2ndarrayLike( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray(options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + 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() { + scalar2ndarrayLike( value, 1.0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + x = empty( [ 2 ] ); + + 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() { + scalar2ndarrayLike( x, 1.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a recognized data type', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 'beepboop', + 'foo', + 'bar', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + x = empty( [ 2 ] ); + + 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() { + scalar2ndarrayLike( x, 1.0, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an `order` option which is not a recognized order', function test( t ) { + var values; + var i; + var x; + + values = [ + '5', + 'beepboop', + 'foo', + 'bar', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + x = empty( [ 2 ] ); + + 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() { + scalar2ndarrayLike( x, 1.0, { + 'order': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `readonly` option which is not a boolean', function test( t ) { + var values; + var i; + var x; + + values = [ + '5', + 5, + null, + void 0, + [], + {}, + function noop() {} + ]; + x = empty( [ 2 ] ); + + 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() { + scalar2ndarrayLike( x, 1.0, { + 'readonly': value + }); + }; + } +}); + +tape( 'the function returns a zero-dimensional ndarray (default, number)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + arr = scalar2ndarrayLike( x, 1.0 ); + expected = new Float64Array( [ 1.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Float64Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (default, complex128)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2 ], { + 'dtype': 'complex128' + }); + v = new Complex128( 1.0, 2.0 ); + + arr = scalar2ndarrayLike( x, v ); + expected = new Float64Array( [ 1.0, 2.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Complex128Array ), true, 'returns expected value' ); + t.deepEqual( reinterpret128( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (default, complex64)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2 ], { + 'dtype': 'complex64' + }); + v = new Complex64( 1.0, 2.0 ); + + arr = scalar2ndarrayLike( x, v ); + expected = new Complex64Array( [ 1.0, 2.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Complex64Array ), true, 'returns expected value' ); + t.deepEqual( reinterpret64( getDType( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (default, bool)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ], { + 'dtype': 'bool' + }); + + arr = scalar2ndarrayLike( x, true ); + expected = new Uint8Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), BooleanArray ), true, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + arr = scalar2ndarrayLike( x, false ); + expected = new Uint8Array( [ 0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), BooleanArray ), true, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (default, other)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ], { + 'dtype': 'generic' + }); + + arr = scalar2ndarrayLike( x, 'beep' ); + expected = [ 'beep' ]; + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=float64)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1.0, { + 'dtype': 'float64' + }); + expected = new Float64Array( [ 1.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Float64Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=float32)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1.0, { + 'dtype': 'float32' + }); + expected = new Float32Array( [ 1.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'float32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Float32Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=int32)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'int32' + }); + expected = new Int32Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'int32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Int32Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=int16)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'int16' + }); + expected = new Int16Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'int16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Int16Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=int8)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'int8' + }); + expected = new Int8Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'int8', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Int8Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=uint32)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'uint32' + }); + expected = new Uint32Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Uint32Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=uint16)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'uint16' + }); + expected = new Uint16Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Uint16Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=uint8)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'uint8' + }); + expected = new Uint8Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint8', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Uint8Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=uint8c)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'uint8c' + }); + expected = new Uint8ClampedArray( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint8c', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Uint8ClampedArray ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=bool)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, true, { + 'dtype': 'bool' + }); + expected = new Uint8Array( [ 1 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), BooleanArray ), true, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + arr = scalar2ndarrayLike( x, false, { + 'dtype': 'bool' + }); + expected = new Uint8Array( [ 0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), BooleanArray ), true, 'returns expected value' ); + t.deepEqual( reinterpretBoolean( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=complex128, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2 ] ); + v = new Complex128( 1.0, 2.0 ); + + arr = scalar2ndarrayLike( x, v, { + 'dtype': 'complex128' + }); + expected = new Float64Array( [ 1.0, 2.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Complex128Array ), true, 'returns expected value' ); + t.deepEqual( reinterpret128( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=complex128, real)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ], { + 'dtype': 'float64' + }); + + arr = scalar2ndarrayLike( x, 1.0, { + 'dtype': 'complex128' + }); + expected = new Float64Array( [ 1.0, 0.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Complex128Array ), true, 'returns expected value' ); + t.deepEqual( reinterpret128( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=complex64, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2 ] ); + v = new Complex64( 1.0, 2.0 ); + + arr = scalar2ndarrayLike( x, v, { + 'dtype': 'complex64' + }); + expected = new Float32Array( [ 1.0, 2.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Complex64Array ), true, 'returns expected value' ); + t.deepEqual( reinterpret64( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=complex64, real)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1.0, { + 'dtype': 'complex64' + }); + expected = new Float32Array( [ 1.0, 0.0 ] ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Complex64Array ), true, 'returns expected value' ); + t.deepEqual( reinterpret64( getData( arr ), 0 ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a zero-dimensional ndarray (dtype=generic)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'generic' + }); + expected = [ 1 ]; + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the array order', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'generic', + 'order': 'column-major' + }); + expected = [ 1 ]; + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning an array which is read-only', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'generic', + 'readonly': true + }); + expected = [ 1 ]; + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning an array which is writable', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2 ] ); + + arr = scalar2ndarrayLike( x, 1, { + 'dtype': 'generic', + 'readonly': false + }); + expected = [ 1 ]; + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Array ), true, 'returns expected value' ); + t.deepEqual( getData( arr ), expected, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), false, 'returns expected value' ); + + t.end(); +}); From 7b42488299166a66e8fa844ee156ba6d2760cb89 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 28 Jan 2026 14:25:50 +0500 Subject: [PATCH 2/3] fix: typescript defs --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../from-scalar-like/docs/types/index.d.ts | 294 ++++++++++++++++-- .../from-scalar-like/docs/types/test.ts | 4 +- 2 files changed, 276 insertions(+), 22 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts index 6d5792555e85..e320c381b583 100644 --- a/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/index.d.ts @@ -21,7 +21,7 @@ /// import { ComplexLike } from '@stdlib/types/complex'; -import { float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, complexndarray, typedndarray, genericndarray, boolndarray, DataType, Order } from '@stdlib/types/ndarray'; +import { ndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, genericndarray, boolndarray, DataType, Order } from '@stdlib/types/ndarray'; /** * Interface defining common options. @@ -202,7 +202,7 @@ interface GenericOptions extends BaseOptions { * var dt = getDType( out ); * // returns 'float64' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number ): typedndarray; +declare function scalar2ndarrayLike( x: float64ndarray, value: number ): float64ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -231,7 +231,35 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number ): t * var dt = getDType( out ); * // returns 'float64' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Float64Options ): float64ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Float64Options ): float64ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'float32' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1.0 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'float32' +*/ +declare function scalar2ndarrayLike( x: float32ndarray, value: number ): float32ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -260,7 +288,7 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'float32' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Float32Options ): float32ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Float32Options ): float32ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -274,19 +302,21 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var getDType = require( '@stdlib/ndarray/dtype' ); * var empty = require( '@stdlib/ndarray/empty' ); * -* var x = empty( [ 2 ] ); +* var x = empty( [ 2 ], { +* 'dtype': 'complex128' +* }); * // returns * * var out = scalar2ndarrayLike( x, 1.0 ); -* // returns [ 1.0 ] +* // returns [ [ 1.0, 0.0 ] ] * * var sh = getShape( out ); * // returns [] * * var dt = getDType( out ); -* // returns 'float64' +* // returns 'complex128' */ -declare function scalar2ndarrayLike( x: complexndarray, value: number | ComplexLike ): complexndarray; +declare function scalar2ndarrayLike( x: complex128ndarray, value: number | ComplexLike ): complex128ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -315,7 +345,35 @@ declare function scalar2ndarrayLike( x: complexndarray, value: number | ComplexL * var dt = getDType( out ); * // returns 'complex128' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number | ComplexLike, options: Complex128Options ): complex128ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number | ComplexLike, options: Complex128Options ): complex128ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'complex64' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ [ 1.0, 0.0 ] ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'complex64' +*/ +declare function scalar2ndarrayLike( x: complex64ndarray, value: number | ComplexLike ): complex64ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -344,7 +402,7 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number | Co * var dt = getDType( out ); * // returns 'complex64' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number | ComplexLike, options: Complex64Options ): complex64ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number | ComplexLike, options: Complex64Options ): complex64ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -401,7 +459,35 @@ declare function scalar2ndarrayLike( x: boolndarray, value: number | boolean ): * var dt = getDType( out ); * // returns 'bool' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number | boolean, options: BoolOptions ): boolndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number | boolean, options: BoolOptions ): boolndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'int32' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'int32' +*/ +declare function scalar2ndarrayLike( x: int32ndarray, value: number ): int32ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -430,7 +516,35 @@ declare function scalar2ndarrayLike( x: typedndarray, value: num * var dt = getDType( out ); * // returns 'int32' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Int32Options ): int32ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Int32Options ): int32ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'int16' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'int16' +*/ +declare function scalar2ndarrayLike( x: int16ndarray, value: number ): int16ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -459,7 +573,35 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'int16' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Int16Options ): int16ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Int16Options ): int16ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'int8' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'int8' +*/ +declare function scalar2ndarrayLike( x: int8ndarray, value: number ): int8ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -488,7 +630,35 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'int8' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Int8Options ): int8ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Int8Options ): int8ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'uint32' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint32' +*/ +declare function scalar2ndarrayLike( x: uint32ndarray, value: number ): uint32ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -517,7 +687,35 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'uint32' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint32Options ): uint32ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Uint32Options ): uint32ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'uint16' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint16' +*/ +declare function scalar2ndarrayLike( x: uint16ndarray, value: number ): uint16ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -546,7 +744,35 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'uint16' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint16Options ): uint16ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Uint16Options ): uint16ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'uint8' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint8' +*/ +declare function scalar2ndarrayLike( x: uint8ndarray, value: number ): uint8ndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -575,7 +801,35 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'uint8' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint8Options ): uint8ndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Uint8Options ): uint8ndarray; + +/** +* Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @returns zero-dimensional ndarray +* +* @example +* var getShape = require( '@stdlib/ndarray/shape' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2 ], { +* 'dtype': 'uint8c' +* }); +* // returns +* +* var out = scalar2ndarrayLike( x, 1.0 ); +* // returns [ 1 ] +* +* var sh = getShape( out ); +* // returns [] +* +* var dt = getDType( out ); +* // returns 'uint8c' +*/ +declare function scalar2ndarrayLike( x: uint8cndarray, value: number ): uint8cndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -604,7 +858,7 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'uint8c' */ -declare function scalar2ndarrayLike( x: typedndarray, value: number, options: Uint8cOptions ): uint8cndarray; +declare function scalar2ndarrayLike( x: ndarray, value: number, options: Uint8cOptions ): uint8cndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -632,7 +886,7 @@ declare function scalar2ndarrayLike( x: typedndarray, value: number, opt * var dt = getDType( out ); * // returns 'generic' */ -declare function scalar2ndarrayLike( x: genericndarray, value: U ): genericndarray; +declare function scalar2ndarrayLike( x: genericndarray, value: number ): genericndarray; /** * Converts a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray. @@ -661,7 +915,7 @@ declare function scalar2ndarrayLike( x: genericndarray * var dt = getDType( out ); * // returns 'generic' */ -declare function scalar2ndarrayLike( x: typedndarray, value: U, options?: GenericOptions ): genericndarray; +declare function scalar2ndarrayLike( x: ndarray, value: T, options?: GenericOptions ): genericndarray; // EXPORTS // diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts index 184b44e65fb4..52528d860411 100644 --- a/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/docs/types/test.ts @@ -26,7 +26,7 @@ import scalar2ndarrayLike = require( './index' ); { var x = empty( [ 1 ] ); - scalar2ndarrayLike( x, 1.0 ); // $ExpectType typedndarray + scalar2ndarrayLike( x, 1.0 ); // $ExpectType float64ndarray scalar2ndarrayLike( x, 1.0, { 'dtype': 'float64' } ); // $ExpectType float64ndarray scalar2ndarrayLike( x, 1.0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray @@ -40,7 +40,7 @@ import scalar2ndarrayLike = require( './index' ); scalar2ndarrayLike( x, 1.0, { 'dtype': 'uint16' } ); // $ExpectType uint16ndarray scalar2ndarrayLike( x, 1.0, { 'dtype': 'uint8' } ); // $ExpectType uint8ndarray scalar2ndarrayLike( x, 1.0, { 'dtype': 'uint8c' } ); // $ExpectType uint8cndarray - scalar2ndarrayLike( x, 1.0, { 'dtype': 'generic' } ); // $ExpectType genericndarray + scalar2ndarrayLike( x, 1.0, { 'dtype': 'generic' } ); // $ExpectType genericndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... From db2e2f5569d926b06ce1dbc616f6da9c412a16bb Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Wed, 28 Jan 2026 22:17:26 +0500 Subject: [PATCH 3/3] docs: apply suggestion from code review Signed-off-by: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> --- lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md b/lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md index c972296d498a..250560b16e0d 100644 --- a/lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md +++ b/lib/node_modules/@stdlib/ndarray/from-scalar-like/README.md @@ -146,7 +146,7 @@ for ( i = 0; i < dt.length; i++ ) { y = scalar2ndarrayLike( x, i, { 'dtype': dt[ i ] }); - console.log( x.get() ); + console.log( y.get() ); } ```