diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/README.md b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/README.md new file mode 100644 index 000000000000..bdf9ca7b6018 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/README.md @@ -0,0 +1,138 @@ + + +# broadcastScalarLike + +> Broadcast a scalar value to an [ndarray][@stdlib/ndarray/base/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/base/ctor]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var broadcastScalarLike = require( '@stdlib/ndarray/base/broadcast-scalar-like' ); +``` + +#### broadcastScalarLike( x, value ) + +Broadcasts a scalar value to an [ndarray][@stdlib/ndarray/base/ctor] having the same shape and [data type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/base/ctor]. + +```javascript +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var getDType = require( '@stdlib/ndarray/dtype' ); + +var x = zeros( 'float32', [ 2, 2 ], 'row-major' ); +// returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] + +var y = broadcastScalarLike( x, 1.0 ); +// returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + +var dt = String( getDType( y ) ); +// returns 'float32' +``` + +
+ + + + + +
+ +## Notes + +- Along with data type, shape, and order, the function infers the "class" of the returned [ndarray][@stdlib/ndarray/base/ctor] from the provided [ndarray][@stdlib/ndarray/base/ctor]. For example, if provided a "base" [ndarray][@stdlib/ndarray/base/ctor], the function returns a base [ndarray][@stdlib/ndarray/base/ctor]. If provided a non-base [ndarray][@stdlib/ndarray/ctor], the function returns a non-base [ndarray][@stdlib/ndarray/ctor]. +- If `value` is a number and the [data type][@stdlib/ndarray/dtypes] of the input ndarray is a complex [data type][@stdlib/ndarray/dtypes], the function returns an [ndarray][@stdlib/ndarray/base/ctor] containing a complex number whose real component equals the provided scalar `value` and whose imaginary component is zero. +- The returned [ndarray][@stdlib/ndarray/base/ctor] is a view on an [ndarray][@stdlib/ndarray/base/ctor] data buffer containing a single element. The view is **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned [ndarray][@stdlib/ndarray/base/ctor], copy the [ndarray][@stdlib/ndarray/base/ctor] **before** performing operations which may mutate elements. + +
+ + + + + +
+ +## Examples + + + +```javascript +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var empty = require( '@stdlib/ndarray/base/empty' ); +var broadcastScalarLike = require( '@stdlib/ndarray/base/broadcast-scalar-like' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Generate broadcasted arrays... +var x; +var y; +var i; +for ( i = 0; i < dt.length; i++ ) { + x = empty( dt[ i ], [ 2, 2 ], 'row-major' ); + y = broadcastScalarLike( x, i ); + console.log( y.get( 0, 0 ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/benchmark/benchmark.js new file mode 100644 index 000000000000..8c9d75a21057 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/benchmark/benchmark.js @@ -0,0 +1,323 @@ +/** +* @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/base/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var broadcastScalarLike = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::base:dtype=float64', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=float32', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'float32', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=complex128', pkg ), function benchmark( b ) { + var x; + var y; + var v; + var i; + + x = empty( 'complex128', [ 2, 2 ], 'row-major' ); + v = new Complex128( 1.0, 2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, v ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=complex64', pkg ), function benchmark( b ) { + var x; + var y; + var v; + var i; + + x = empty( 'complex64', [ 2, 2 ], 'row-major' ); + v = new Complex64( 1.0, 2.0 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, v ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=int32', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'int32', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=uint32', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'uint32', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=int16', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'int16', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=uint16', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'uint16', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=int8', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'int8', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=uint8', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'uint8', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=uint8c', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'uint8c', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=generic', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'generic', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, i ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::base:dtype=bool', pkg ), function benchmark( b ) { + var x; + var y; + var i; + + x = empty( 'bool', [ 2, 2 ], 'row-major' ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = broadcastScalarLike( x, ( i%2 ) === 0 ); + if ( y.length !== 4 ) { + b.fail( 'should have expected number of elements' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/repl.txt new file mode 100644 index 000000000000..6a4dd9ea936a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/repl.txt @@ -0,0 +1,44 @@ + +{{alias}}( x, value ) + Broadcasts a scalar value to an ndarray having the same shape and data type + as a provided input ndarray. + + Along with data type, shape, and order, the function infers the "class" of + the returned ndarray from the provided ndarray. For example, if provided a + "base" ndarray, the function returns a base ndarray. If provided a non-base + ndarray, the function returns a non-base ndarray. + + If `value` is a number and a provided ndarray has 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. + + The returned ndarray is a view on a single-element buffer. The returned + ndarray is not contiguous. As more than one element of a returned ndarray + refers to the same memory location, writing to the returned ndarray may + affect multiple elements. If you need to write to the returned ndarray, copy + the ndarray before performing operations which may mutate elements. + + Parameters + ---------- + x: ndarray + Input array. + + value: any + Scalar value. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/base/zeros}}( 'float64', [ 2, 2 ], 'row-major' ) + [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] + > var y = {{alias}}( x, 1.0 ) + [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/types/index.d.ts new file mode 100644 index 000000000000..2e58dd31b2a1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/types/index.d.ts @@ -0,0 +1,99 @@ +/* +* @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 { typedndarray, genericndarray, complexndarray } from '@stdlib/types/ndarray'; +import { ComplexLike } from '@stdlib/types/complex'; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* ## 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 x - input array +* @param value - scalar value +* @returns output ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var x = zeros( 'complex128', [ 2, 2 ], 'row-major' ); +* // returns [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ], [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] +* +* var y = broadcastScalarLike( x, 1.0 ); +* // returns [ [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ], [ [ 1.0, 0.0 ], [ 1.0, 0.0 ] ] ] +* +* var dt = String( getDType( y ) ); +* // returns 'complex128' +*/ +declare function broadcastScalarLike( x: T, value: ComplexLike | number ): T; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input array +* @param value - scalar value +* @returns output ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var x = zeros( 'generic', [ 2, 2 ], 'row-major' ); +* // returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] +* +* var y = broadcastScalarLike( x, 1.0 ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = String( getDType( y ) ); +* // returns 'generic' +*/ +declare function broadcastScalarLike( x: genericndarray, value: T ): genericndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input array +* @param value - scalar value +* @returns output ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var x = zeros( 'float64', [ 2, 2 ], 'row-major' ); +* // returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] +* +* var y = broadcastScalarLike( x, 1.0 ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = String( getDType( y ) ); +* // returns 'float64' +*/ +declare function broadcastScalarLike = typedndarray>( x: U, value: T ): U; + + +// EXPORTS // + +export = broadcastScalarLike; diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/types/test.ts new file mode 100644 index 000000000000..c5c3fa4faa24 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @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 zeros = require( '@stdlib/ndarray/base/zeros' ); +import broadcastScalarLike = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + broadcastScalarLike( zeros( 'float64', [ 2, 2 ], 'row-major' ), 1.0 ); // $ExpectType float64ndarray + broadcastScalarLike( zeros( 'float32', [ 2, 2 ], 'row-major' ), 1.0 ); // $ExpectType float32ndarray + broadcastScalarLike( zeros( 'complex128', [ 2, 2 ], 'row-major' ), 1.0 ); // $ExpectType complex128ndarray + broadcastScalarLike( zeros( 'complex64', [ 2, 2 ], 'row-major' ), 1.0 ); // $ExpectType complex64ndarray + broadcastScalarLike( zeros( 'int32', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectType int32ndarray + broadcastScalarLike( zeros( 'int16', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectType int16ndarray + broadcastScalarLike( zeros( 'int8', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectType int8ndarray + broadcastScalarLike( zeros( 'uint32', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectType uint32ndarray + broadcastScalarLike( zeros( 'uint16', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectType uint16ndarray + broadcastScalarLike( zeros( 'uint8', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectType uint8ndarray + broadcastScalarLike( zeros( 'uint8c', [ 2, 2 ], 'row-major' ), 1 ); // $ExpectType uint8cndarray + broadcastScalarLike( zeros( 'generic', [ 2, 2 ], 'row-major' ), 1.0 ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + broadcastScalarLike( '5', 1.0 ); // $ExpectError + broadcastScalarLike( 5, 1.0 ); // $ExpectError + broadcastScalarLike( true, 1.0 ); // $ExpectError + broadcastScalarLike( false, 1.0 ); // $ExpectError + broadcastScalarLike( null, 1.0 ); // $ExpectError + broadcastScalarLike( [], 1.0 ); // $ExpectError + broadcastScalarLike( {}, 1.0 ); // $ExpectError + broadcastScalarLike( ( x: number ): number => x, 1.0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + broadcastScalarLike(); // $ExpectError + broadcastScalarLike( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + broadcastScalarLike( zeros( 'float64', [ 2, 2 ], 'row-major' ), 1.0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/examples/index.js new file mode 100644 index 000000000000..e4143f74d357 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/examples/index.js @@ -0,0 +1,36 @@ +/** +* @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/base/empty' ); +var broadcastScalarLike = require( './../lib' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Generate broadcasted arrays... +var x; +var y; +var i; +for ( i = 0; i < dt.length; i++ ) { + x = empty( dt[ i ], [ 2, 2 ], 'row-major' ); + y = broadcastScalarLike( x, i ); + console.log( y.get( 0, 0 ) ); +} diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/lib/index.js new file mode 100644 index 000000000000..10d3810cbffc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/lib/index.js @@ -0,0 +1,48 @@ +/** +* @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 having the same shape and data type as a provided input ndarray. +* +* @module @stdlib/ndarray/base/broadcast-scalar-like +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var broadcastScalarLike = require( '@stdlib/ndarray/base/broadcast-scalar-like' ); +* +* var x = zeros( 'float32', [ 2, 2 ], 'row-major' ); +* // returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] +* +* var y = broadcastScalarLike( x, 1.0 ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = String( getDType( y ) ); +* // returns 'float32' +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/lib/main.js new file mode 100644 index 000000000000..e36c39e5f899 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/lib/main.js @@ -0,0 +1,89 @@ +/** +* @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 isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var accessorSetter = require( '@stdlib/array/base/accessor-setter' ); +var setter = require( '@stdlib/array/base/setter' ); +var azeros = require( '@stdlib/array/base/zeros' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var buffer = require( '@stdlib/ndarray/base/buffer' ); +var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param {ndarray} x - input array +* @param {*} value - scalar value +* @throws {TypeError} first argument must have a recognized data type +* @returns {ndarray} ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var getDType = require( '@stdlib/ndarray/dtype' ); +* +* var x = zeros( 'float32', [ 2, 2 ], 'row-major' ); +* // returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] +* +* var y = broadcastScalarLike( x, 1.0 ); +* // returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] +* +* var dt = String( getDType( y ) ); +* // returns 'float32' +*/ +function broadcastScalarLike( x, value ) { + var buf; + var set; + var sh; + var dt; + var N; + + dt = resolveStr( getDType( x ) ); + buf = buffer( dt, 1 ); + if ( buf === null ) { + throw new TypeError( format( 'invalid argument. First argument must have a recognized data type. Value: `%s`.', dt ) ); + } + if ( isComplexDataType( dt ) && isNumber( value ) ) { + value = [ value, 0.0 ]; // note: we're assuming that the ComplexXXArray setter accepts an array of interleaved real and imaginary components + } + if ( isAccessorArray( buf ) ) { + set = accessorSetter( dt ); + } else { + set = setter( dt ); + } + set( buf, 0, value ); + sh = getShape( x, true ); + N = sh.length || 1; + return new x.constructor( dt, buf, sh, azeros( N ), 0, getOrder( x ) ); +} + + +// EXPORTS // + +module.exports = broadcastScalarLike; diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/package.json b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/package.json new file mode 100644 index 000000000000..1dca8716b075 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/package.json @@ -0,0 +1,64 @@ +{ + "name": "@stdlib/ndarray/base/broadcast-scalar-like", + "version": "0.0.0", + "description": "Broadcast a scalar value to an ndarray having the same shape and data type as a provided input 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", + "base", + "data", + "structure", + "ndarray", + "scalar", + "broadcast", + "wrap", + "convert" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/test/test.js b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/test/test.js new file mode 100644 index 000000000000..d111e1b4e268 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/broadcast-scalar-like/test/test.js @@ -0,0 +1,863 @@ +/** +* @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 Buffer = require( '@stdlib/buffer/ctor' ); +var allocUnsafe = require( '@stdlib/buffer/alloc-unsafe' ); +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 base = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); +var zeros = require( '@stdlib/ndarray/base/zeros' ); +var empty = require( '@stdlib/ndarray/base/empty' ); +var numel = require( '@stdlib/ndarray/base/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 broadcastScalarLike = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof broadcastScalarLike, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument having an unrecognized data type', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'data': true + }, + { + 'shape': [ 1, 2, 3 ], + 'order': 'row-major', + 'dtype': 'foo_bar_beep_boop' + } + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( value, 1.0 ); + }; + } +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=float64)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'float64', [ 2, 2 ], 'row-major' ); + + expected = new Float64Array( [ 1.0 ] ); + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=float64)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Float64Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'float64', + 'order': 'column-major' + }); + + expected = new Float64Array( [ 1.0 ] ); + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=float32)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'float32', [ 3, 3 ], 'column-major' ); + + expected = new Float32Array( [ 1.0 ] ); + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'float32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 3 ], '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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 9, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=float32)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Float32Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'float32', + 'order': 'row-major' + }); + + expected = new Float32Array( [ 1.0 ] ); + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=int32)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'int32', [ 2, 1 ], 'row-major' ); + + expected = new Int32Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'int32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 1 ], '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( getShape( arr ) ), 2, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=int32)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Int32Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'int32', + 'order': 'column-major' + }); + + expected = new Int32Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=int16)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'int16', [ 1, 2 ], 'column-major' ); + + expected = new Int16Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'int16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 1, 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 2, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=int16)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Int16Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'int16', + 'order': 'row-major' + }); + + expected = new Int16Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=int8)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'int8', [ 3, 3, 3 ], 'row-major' ); + + expected = new Int8Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'int8', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 3, 3 ], '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( getShape( arr ) ), 27, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=int8)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Int8Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'int8', + 'order': 'column-major' + }); + + expected = new Int8Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=uint32)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'uint32', [ 1, 2, 3 ], 'column-major' ); + + expected = new Uint32Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'uint32', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 1, 2, 3 ], '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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 6, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=uint32)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Uint32Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'uint32', + 'order': 'row-major' + }); + + expected = new Uint32Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=uint16)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'uint16', [ 3, 2, 1 ], 'row-major' ); + + expected = new Uint16Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'uint16', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2, 1 ], '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( getShape( arr ) ), 6, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=uint16)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Uint16Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'uint16', + 'order': 'column-major' + }); + + expected = new Uint16Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=uint8)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'uint8', [ 1, 1, 1, 1 ], 'column-major' ); + + expected = new Uint8Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'uint8', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 1, 1, 1, 1 ], '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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=uint8)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Uint8Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'uint8', + 'order': 'row-major' + }); + + expected = new Uint8Array( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=uint8c)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'uint8c', [ 2, 0, 2 ], 'row-major' ); + + expected = new Uint8ClampedArray( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'uint8c', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 0, 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( getShape( arr ) ), 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=uint8c)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Uint8ClampedArray( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'uint8c', + 'order': 'row-major' + }); + + expected = new Uint8ClampedArray( [ 1 ] ); + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=complex128, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = zeros( 'complex128', [ 1 ], 'column-major' ); + + expected = new Float64Array( [ 1.0, 2.0 ] ); + + v = new Complex128( 1.0, 2.0 ); + arr = broadcastScalarLike( x, v ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 1 ], '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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=complex128, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = array( new Complex128Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'complex128', + 'order': 'row-major' + }); + + expected = new Float64Array( [ 1.0, 2.0 ] ); + + v = new Complex128( 1.0, 2.0 ); + arr = broadcastScalarLike( x, v ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=complex128, real)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'complex128', [ 2, 2 ], 'row-major' ); + + expected = new Float64Array( [ 1.0, 0.0 ] ); + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=complex128, real)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Complex128Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'complex128', + 'order': 'column-major' + }); + + expected = new Float64Array( [ 1.0, 0.0 ] ); + + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=complex64, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = zeros( 'complex64', [ 3, 3 ], 'column-major' ); + + expected = new Float32Array( [ 1.0, 2.0 ] ); + + v = new Complex64( 1.0, 2.0 ); + arr = broadcastScalarLike( x, v ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 3 ], '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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 9, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=complex64, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = array( new Complex64Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'complex64', + 'order': 'row-major' + }); + + expected = new Float32Array( [ 1.0, 2.0 ] ); + + v = new Complex64( 1.0, 2.0 ); + arr = broadcastScalarLike( x, v ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=complex64, real)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'complex64', [ 4, 4 ], 'row-major' ); + + expected = new Float32Array( [ 1.0, 0.0 ] ); + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'complex64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 4, 4 ], '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( getShape( arr ) ), 16, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=complex64, real)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new Complex64Array( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'complex64', + 'order': 'column-major' + }); + + expected = new Float32Array( [ 1.0, 0.0 ] ); + + arr = broadcastScalarLike( x, 1.0 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=generic)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'generic', [ 0, 2, 0 ], 'column-major' ); + + expected = [ 1 ]; + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'generic', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 0, 2, 0 ], '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( getShape( arr ) ), 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=generic)', function test( t ) { + var expected; + var arr; + var x; + + x = array( [ 1, 2, 3, 4 ], { + 'shape': [ 2, 2 ], + 'dtype': 'generic', + 'order': 'row-major' + }); + + expected = [ 1 ]; + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=generic, ndims=0)', function test( t ) { + var expected; + var arr; + var x; + + x = zeros( 'generic', [], 'column-major' ); + + expected = [ 1 ]; + arr = broadcastScalarLike( x, 1 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=bool)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( 'bool', [ 2, 2 ], 'row-major' ); + + expected = new Uint8Array( [ 1 ] ); + arr = broadcastScalarLike( x, true ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( 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( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=bool)', function test( t ) { + var expected; + var arr; + var x; + + x = array( new BooleanArray( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'bool', + 'order': 'column-major' + }); + + expected = new Uint8Array( [ 1 ] ); + arr = broadcastScalarLike( x, true ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( 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 ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (base, dtype=binary)', function test( t ) { + var arr; + var x; + + x = zeros( 'binary', [ 2, 2 ], 'row-major' ); + arr = broadcastScalarLike( x, 127 ); + + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'binary', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Buffer ), true, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a broadcasted ndarray (non-base, dtype=binary)', function test( t ) { + var arr; + var x; + + x = array( allocUnsafe( 4 ), { + 'shape': [ 2, 2 ], + 'dtype': 'binary', + 'order': 'column-major' + }); + + arr = broadcastScalarLike( x, true ); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'binary', 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( instanceOf( getData( arr ), Buffer ), true, 'returns expected value' ); + t.strictEqual( getOrder( arr ), 'column-major', 'returns expected value' ); + t.strictEqual( numel( getShape( arr ) ), 4, 'returns expected value' ); + + t.end(); +});