From 7f97ab389fcf155b078d1d5f8baf8232bffab042 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Tue, 27 Jan 2026 09:06:25 +0500 Subject: [PATCH] feat: add ndarray/broadcast-scalar --- 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/broadcast-scalar/README.md | 169 ++++ .../broadcast-scalar/benchmark/benchmark.js | 314 ++++++++ .../ndarray/broadcast-scalar/docs/repl.txt | 54 ++ .../broadcast-scalar/docs/types/index.d.ts | 136 ++++ .../broadcast-scalar/docs/types/test.ts | 110 +++ .../broadcast-scalar/examples/index.js | 35 + .../ndarray/broadcast-scalar/lib/index.js | 44 ++ .../ndarray/broadcast-scalar/lib/main.js | 156 ++++ .../ndarray/broadcast-scalar/package.json | 67 ++ .../ndarray/broadcast-scalar/test/test.js | 729 ++++++++++++++++++ 10 files changed, 1814 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/broadcast-scalar/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/README.md b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/README.md new file mode 100644 index 000000000000..959af012b62e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/README.md @@ -0,0 +1,169 @@ + + +# broadcastScalar + +> Broadcast a scalar value to an [ndarray][@stdlib/ndarray/ctor] of a specified shape. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var broadcastScalar = require( '@stdlib/ndarray/broadcast-scalar' ); +``` + +#### broadcastScalar( shape, value\[, options] ) + +Broadcast a scalar value to an [`ndarray`][@stdlib/ndarray/ctor] of a specified shape. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); + +var x = broadcastScalar( [ 2, 2 ], 1.0 ); +// returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + +var dt = getDType( x ); +// returns 'float64' +``` + +The function accepts the following arguments: + +- **shape**: shape of the ouput [`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 getDType = require( '@stdlib/ndarray/dtype' ); + +var x = broadcastScalar( [ 2, 2 ], 1.0, { + 'dtype': 'float32' +}); +// returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + +var dt = getDType( x ); +// returns 'float32' +``` + +
+ + + + + +
+ +## Notes + +- If `value` is a number and [`dtype`][@stdlib/ndarray/dtypes] is a complex [data type][@stdlib/ndarray/dtypes], the function returns an [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 broadcastScalar = require( '@stdlib/ndarray/broadcast-scalar' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Generate zero-dimensional arrays... +var x; +var i; +for ( i = 0; i < dt.length; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': dt[ i ] + }); + console.log( x.get( 0, 1 ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/benchmark/benchmark.js new file mode 100644 index 000000000000..1105ee1cfa44 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/benchmark/benchmark.js @@ -0,0 +1,314 @@ +/** +* @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 format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var broadcastScalar = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:dtype=float64', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'float64' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'float32' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) { + var x; + var v; + var i; + + v = new Complex128( 1.0, 2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], v, { + 'dtype': 'complex128' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex64', pkg ), function benchmark( b ) { + var x; + var v; + var i; + + v = new Complex64( 1.0, 2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], v, { + 'dtype': 'complex64' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=bool', pkg ), function benchmark( b ) { + var x; + var v; + var i; + + v = [ true, false ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], v[ i%2 ], { + 'dtype': 'bool' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int32', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'int32' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint32', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'uint32' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int16', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'int16' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint16', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'uint16' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int8', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'int8' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'uint8' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8c', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'uint8c' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=generic', pkg ), function benchmark( b ) { + var x; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': 'generic' + }); + if ( x.length !== 4 ) { + b.fail( 'should have length 4' ); + } + } + b.toc(); + if ( !isndarrayLike( x ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/repl.txt new file mode 100644 index 000000000000..33f6dbabb275 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/repl.txt @@ -0,0 +1,54 @@ + +{{alias}}( shape, value[, options] ) + Broadcasts a scalar value to an ndarray of a specified shape. + + If `value` is a number and `options.dtype` is a complex number data type, + the function returns an ndarray containing a complex number whose real + component equals the provided scalar value and whose imaginary component is + zero. + + Parameters + ---------- + shape: ArrayLike + Shape of the output 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}}( [ 2, 2 ], 1.0 ) + [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + > var dt = {{alias:@stdlib/ndarray/dtype}}( x ) + 'float64' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/index.d.ts new file mode 100644 index 000000000000..013dcd10174e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/index.d.ts @@ -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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ComplexLike } from '@stdlib/types/complex'; +import { typedndarray, complexndarray, genericndarray, 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; +} + +/** +* Broadcasts a scalar value to an ndarray of a specified shape. +* +* @param shape - shape of output ndarray +* @param value - scalar value +* @param options - options +* @returns ndarray +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var x = broadcastScalar( [ 2, 2 ], 1.0, { +* 'dtype': 'float64' +* }); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = getDType( x ); +* // returns 'float64' +*/ +declare function broadcastScalar( shape: Array, value: number, options?: Options ): typedndarray; + +/** +* Broadcasts a scalar value to an ndarray of a specified shape. +* +* ## Notes +* +* - If provided a number, the function returns an ndarray containing a complex number whose real component equals the provided scalar value and whose imaginary component is zero. +* +* @param shape - shape of output ndarray +* @param value - scalar value +* @param options - options +* @returns ndarray +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var Complex64 = require( '@stdlib/complex/float64/ctor' ); +* var realf = require( '@stdlib/complex/float32/real' ); +* var imagf = require( '@stdlib/complex/float32/imag' ); +* +* var v = new Complex64( 1.0, 2.0 ); +* +* var x = broadcastScalar( [ 2, 2 ], v, { +* 'dtype': 'complex64' +* }); +* // returns [ [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ], [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ] ] +* +* var dt = getDType( x ); +* // returns 'complex64' +*/ +declare function broadcastScalar( shape: Array, value: number | ComplexLike, options?: Options ): complexndarray; + +/** +* Broadcasts a scalar value to an ndarray of a specified shape. +* +* ## 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 shape - shape of output ndarray +* @param value - scalar value +* @param options - options +* @returns ndarray +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var x = broadcastScalar( [ 2, 2 ], 1.0, { +* 'dtype': 'generic' +* }); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = getDType( x ); +* // returns 'generic' +*/ +declare function broadcastScalar( shape: Array, value: T, options?: Options ): genericndarray; + + +// EXPORTS // + +export = broadcastScalar; diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/test.ts new file mode 100644 index 000000000000..8a526a69da6e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/docs/types/test.ts @@ -0,0 +1,110 @@ +/* +* @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 broadcastScalar = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + broadcastScalar( [ 2, 2 ], 1.0 ); // $ExpectType typedndarray + + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'float64' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'float32' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'complex128' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'complex64' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], true, { 'dtype': 'bool' } ); // $ExpectType genericndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'int32' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'int16' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'int8' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'uint32' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'uint16' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'uint8' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'uint8c' } ); // $ExpectType typedndarray + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 'generic' } ); // $ExpectType typedndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of integers... +{ + broadcastScalar( '10', 1.0 ); // $ExpectError + broadcastScalar( 5, 1.0 ); // $ExpectError + broadcastScalar( false, 1.0 ); // $ExpectError + broadcastScalar( true, 1.0 ); // $ExpectError + broadcastScalar( null, 1.0 ); // $ExpectError + broadcastScalar( ( x: number ): number => x, 1.0 ); // $ExpectError + + broadcastScalar( '10', 1.0, {} ); // $ExpectError + broadcastScalar( 5, 1.0, {} ); // $ExpectError + broadcastScalar( false, 1.0, {} ); // $ExpectError + broadcastScalar( true, 1.0, {} ); // $ExpectError + broadcastScalar( null, 1.0, {} ); // $ExpectError + broadcastScalar( ( 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... +{ + broadcastScalar( [ 2, 2 ], 1.0, '10' ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, 5 ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, false ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, true ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, null ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, [] ); // $ExpectError + broadcastScalar( [ 2, 2 ], 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... +{ + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': '10' } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': 5 } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': false } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': true } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': null } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': [] } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'dtype': {} } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 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... +{ + broadcastScalar( [ 2, 2 ], 1.0, { 'order': '10' } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'order': 5 } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'order': false } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'order': true } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'order': null } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'order': [] } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'order': {} } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 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... +{ + broadcastScalar( [ 2, 2 ], 1.0, { 'readonly': '10' } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'readonly': 5 } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'readonly': null } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'readonly': [] } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'readonly': {} } ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, { 'readonly': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + broadcastScalar(); // $ExpectError + broadcastScalar( [ 2, 2 ] ); // $ExpectError + broadcastScalar( [ 2, 2 ], 1.0, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/examples/index.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/examples/index.js new file mode 100644 index 000000000000..7c64bdce08b7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/examples/index.js @@ -0,0 +1,35 @@ +/** +* @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 broadcastScalar = require( './../lib' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Generate two-dimensional arrays... +var x; +var i; +for ( i = 0; i < dt.length; i++ ) { + x = broadcastScalar( [ 2, 2 ], i, { + 'dtype': dt[ i ] + }); + console.log( x.get( 0, 1 ) ); +} diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/index.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/index.js new file mode 100644 index 000000000000..2ed4071dfdf2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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'; + +/** +* Broadcast a scalar value to an ndarray of a specified shape. +* +* @module @stdlib/ndarray/broadcast-scalar +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var broadcastScalar = require( '@stdlib/ndarray/broadcast-scalar' ); +* +* var x = broadcastScalar( [ 2, 2 ], 1.0 ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = getDType( x ); +* // returns 'float64' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/main.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/main.js new file mode 100644 index 000000000000..c313a2594e8d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/lib/main.js @@ -0,0 +1,156 @@ +/** +* @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 isPositiveIntegerArray = require( '@stdlib/assert/is-positive-integer-array' ); +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive; +var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var accessorSetter = require( '@stdlib/array/base/accessor-setter' ); +var zeros = require( '@stdlib/array/zeros' ); +var setter = require( '@stdlib/array/base/setter' ); +var buffer = require( '@stdlib/ndarray/base/buffer' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var defaults = require( '@stdlib/ndarray/defaults' ); +var dtype = require( '@stdlib/complex/dtype' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var ORDER = defaults.get( 'order' ); +var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' ); +var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' ); +var DEFAULT_BOOL = defaults.get( 'dtypes.boolean' ); + + +// MAIN // + +/** +* Broadcasts a scalar value to an ndarray of a specified shape. +* +* ## 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 {Array} shape - shape of the output 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 +* @param {TypeError} - first argument must be an array of non-negative integers +* @throws {TypeError} second argument must be an object +* @throws {TypeError} `dtype` option must be a recognized data type +* @returns {ndarray} ndarray +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var x = broadcastScalar( [ 2, 2 ], 1.0 ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = getDType( x ); +* // returns 'float64' +*/ +function broadcastScalar( shape, value, options ) { + var opts; + var buf; + var flg; + var set; + var dt; + var v; + var N; + + opts = { + 'dtype': '', + 'order': ORDER, + 'readonly': false + }; + if ( !isPositiveIntegerArray( shape ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an array of non-negative integers. Value: `%s`.', shape ) ); + } + if ( arguments.length > 2 ) { + options = arguments[ 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 ); + if ( opts.dtype === '' ) { + if ( flg ) { + dt = DEFAULT_REAL; + } else if ( isBoolean( value ) ) { + dt = DEFAULT_BOOL; + } else if ( isComplexLike( value ) ) { + dt = dtype( value ); + if ( dt === null ) { + dt = DEFAULT_CMPLX; + } + } else { + dt = 'generic'; + } + } else { + dt = opts.dtype; + } + buf = buffer( dt, numel( shape ) ); + 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 ); + N = shape.length || 1; + return new ndarray( dt, buf, shape, zeros( N ), 0, opts.order, opts ); +} + + +// EXPORTS // + +module.exports = broadcastScalar; diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/package.json b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/package.json new file mode 100644 index 000000000000..40f47e3368ca --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ndarray/broadcast-scalar", + "version": "0.0.0", + "description": "Broadcast a scalar value to an ndarray of a specified shape.", + "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", + "ndarray", + "broadcast", + "broadcasting", + "reshape", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar/test/test.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/test/test.js new file mode 100644 index 000000000000..ca658b79f145 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar/test/test.js @@ -0,0 +1,729 @@ +/** +* @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 numel = require( '@stdlib/ndarray/numel' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var broadcastScalar = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof broadcastScalar, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an array of non-negative integers', function test( t ) { + var values; + var i; + + values = [ + [ -1, 2, 3], + [ -1, -2, -3 ], + '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() { + broadcastScalar( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an array of non-negative integers(options)', function test( t ) { + var values; + var i; + + values = [ + [ -1, 2, 3], + [ -1, -2, -3 ], + '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() { + broadcastScalar( 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 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() { + broadcastScalar( [ 2, 2 ], 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 i; + + values = [ + '5', + 'beepboop', + 'foo', + 'bar', + 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() { + broadcastScalar( [ 2, 2 ], 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; + + values = [ + '5', + 'beepboop', + 'foo', + 'bar', + 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() { + broadcastScalar( [ 2, 2 ], 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; + + values = [ + '5', + 5, + 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() { + broadcastScalar( [ 2, 2 ], 1.0, { + 'readonly': value + }); + }; + } +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (default, number)', function test( t ) { + var expected; + var arr; + + expected = new Float64Array( [ 1.0, 0.0, 0.0, 0.0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1.0 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (default, complex128)', function test( t ) { + var expected; + var arr; + var v; + + expected = new Float64Array( [ 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + + v = new Complex128( 1.0, 2.0 ); + arr = broadcastScalar( [ 2, 2 ], v ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (default, complex64)', function test( t ) { + var expected; + var arr; + var v; + + expected = new Float32Array( [ 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + + v = new Complex64( 1.0, 2.0 ); + arr = broadcastScalar( [ 2, 2 ], v ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (default, bool)', function test( t ) { + var expected; + var arr; + + expected = new Uint8Array( [ 1, 0, 0, 0 ] ); + + arr = broadcastScalar( [ 2, 2 ], true ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + expected = new Uint8Array( [ 0, 0, 0, 0 ] ); + + arr = broadcastScalar( [ 2, 2 ], false ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (default, other)', function test( t ) { + var expected; + var arr; + + expected = [ 'beep', 0, 0, 0 ]; + arr = broadcastScalar( [ 2, 2 ], 'beep' ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=float64)', function test( t ) { + var expected; + var arr; + + expected = new Float64Array( [ 1.0, 0.0, 0.0, 0.0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1.0, { + 'dtype': 'float64' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=float32)', function test( t ) { + var expected; + var arr; + + expected = new Float32Array( [ 1.0, 0.0, 0.0, 0.0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1.0, { + 'dtype': 'float32' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'float32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=int32)', function test( t ) { + var expected; + var arr; + + expected = new Int32Array( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'int32' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'int32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=int16)', function test( t ) { + var expected; + var arr; + + expected = new Int16Array( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'int16' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'int16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=int8)', function test( t ) { + var expected; + var arr; + + expected = new Int8Array( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'int8' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'int8', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=uint32)', function test( t ) { + var expected; + var arr; + + expected = new Uint32Array( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'uint32' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=uint16)', function test( t ) { + var expected; + var arr; + + expected = new Uint16Array( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'uint16' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=uint8)', function test( t ) { + var expected; + var arr; + + expected = new Uint8Array( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'uint8' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint8', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=uint8c)', function test( t ) { + var expected; + var arr; + + expected = new Uint8ClampedArray( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'uint8c' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'uint8c', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=bool)', function test( t ) { + var expected; + var arr; + + expected = new Uint8Array( [ 1, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], true, { + 'dtype': 'bool' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + expected = new Uint8Array( [ 0, 0, 0, 0 ] ); + arr = broadcastScalar( [ 2, 2 ], false, { + 'dtype': 'bool' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'bool', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=complex128, complex)', function test( t ) { + var expected; + var arr; + var v; + + expected = new Float64Array( [ 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + + v = new Complex128( 1.0, 2.0 ); + arr = broadcastScalar( [ 2, 2 ], v, { + 'dtype': 'complex128' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=complex128, real)', function test( t ) { + var expected; + var arr; + + expected = new Float64Array( [ 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1.0, { + 'dtype': 'complex128' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=complex64, complex)', function test( t ) { + var expected; + var arr; + var v; + + expected = new Float32Array( [ 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + + v = new Complex64( 1.0, 2.0 ); + arr = broadcastScalar( [ 2, 2 ], v, { + 'dtype': 'complex64' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=complex64, real)', function test( t ) { + var expected; + var arr; + + expected = new Float32Array( [ 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); + arr = broadcastScalar( [ 2, 2 ], 1.0, { + 'dtype': 'complex64' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function broadcasts a scalar value to an ndarray of a specified shape (dtype=generic)', function test( t ) { + var expected; + var arr; + + expected = [ 1, 0, 0, 0 ]; + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'generic' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the array order', function test( t ) { + var expected; + var arr; + + expected = [ 1, 0, 0, 0 ]; + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'generic', + 'order': 'column-major' + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning an array which is read-only', function test( t ) { + var expected; + var arr; + + expected = [ 1, 0, 0, 0 ]; + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'generic', + 'readonly': true + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + t.strictEqual( arr.flags.READONLY, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning an array which is writable', function test( t ) { + var expected; + var arr; + + expected = [ 1, 0, 0, 0 ]; + arr = broadcastScalar( [ 2, 2 ], 1, { + 'dtype': 'generic', + 'readonly': false + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( getDType( arr ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], '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 ), 4, 'returns expected value' ); + t.strictEqual( arr.flags.READONLY, false, 'returns expected value' ); + + t.end(); +});