diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/README.md b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/README.md new file mode 100644 index 000000000000..41a5bf227432 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/README.md @@ -0,0 +1,188 @@ + + +# broadcastScalarLike + +> Broadcast a scalar value to an [ndarray][@stdlib/ndarray/ctor] having the same shape and [data-type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/ctor]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var broadcastScalarLike = require( '@stdlib/ndarray/broadcast-scalar-like' ); +``` + +#### broadcastScalarLike( x, value\[, options] ) + +Broadcasts a scalar value to an [ndarray][@stdlib/ndarray/ctor] having the same shape and [data-type][@stdlib/ndarray/dtypes] as a provided input [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var getDType = require( '@stdlib/ndarray/dtype' ); +var empty = require( '@stdlib/ndarray/empty' ); + +var x = empty( [ 2, 2 ], { + 'dtype': 'float32' +}); +// returns + +var y = broadcastScalarLike( x, 1.0 ); +// returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + +var dt = String( getDType( y ) ); +// returns 'float32' +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **value**: scalar value. + +The function accepts the following options: + +- **dtype**: output array [data type][@stdlib/ndarray/dtypes]. By default, the output array has the same [data type][@stdlib/ndarray/dtypes] as the provided input ndarray. +- **shape**: output array shape. By default, the output array has the same shape as the provided input ndarray. +- **order**: array order (i.e., memory layout). Must be either `row-major` (C-style) or `column-major` (Fortran-style). By default, the output array has the same order as the provided input ndarray. +- **readonly**: boolean indicating whether an array should be **read-only**. Default: `false`. + +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 zeros = require( '@stdlib/ndarray/zeros' ); + +var x = zeros( [ 2, 2 ] ); +// returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] + +var y = broadcastScalarLike( x, 1.0, { + 'dtype': 'float32' +}); +// returns [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + +var dt = String( getDType( y ) ); +// returns 'float32' +``` + +To explicitly specify the shape of the returned [ndarray][@stdlib/ndarray/ctor], provide a `shape` option. + +```javascript +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = zeros( [ 2, 2 ] ); +// returns [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] + +var y = broadcastScalarLike( x, 1.0, { + 'shape': [ 3, 3 ] +}); +// returns [ [ 1.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0 ] ] +``` + +
+ + + + + +
+ +## Notes + +- If `value` is a number and the resolved [data type][@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 resolved [data type][@stdlib/ndarray/dtypes] is an integer [data type][@stdlib/ndarray/dtypes]. + +
+ + + + + +
+ +## Examples + + + +```javascript +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var empty = require( '@stdlib/ndarray/empty' ); +var broadcastScalarLike = require( '@stdlib/ndarray/broadcast-scalar-like' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Create an input array: +var x = empty( [ 2, 2 ] ); + +// Generate broadcasted arrays... +var y; +var i; +for ( i = 0; i < dt.length; i++ ) { + y = broadcastScalarLike( x, i, { + 'dtype': dt[ i ] + }); + console.log( y.get( 0, 1 ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/benchmark/benchmark.js new file mode 100644 index 000000000000..f171ca433ffc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/benchmark/benchmark.js @@ -0,0 +1,354 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var broadcastScalarLike = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:dtype=float64', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'float64' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=float32', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'float32' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex128', pkg ), function benchmark( b ) { + var x; + var o; + var v; + var i; + + v = new Complex128( 1.0, 2.0 ); + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, v, { + 'dtype': 'complex128' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=complex64', pkg ), function benchmark( b ) { + var x; + var o; + var v; + var i; + + v = new Complex64( 1.0, 2.0 ); + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, v, { + 'dtype': 'complex64' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=bool', pkg ), function benchmark( b ) { + var x; + var o; + var v; + var i; + + v = [ true, false ]; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, v[ i%2 ], { + 'dtype': 'bool' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int32', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'int32' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint32', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'uint32' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int16', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'int16' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint16', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'uint16' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=int8', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'int8' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'uint8' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=uint8c', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'uint8c' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:dtype=generic', pkg ), function benchmark( b ) { + var x; + var o; + var i; + + x = empty( [ 2, 2 ] ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = broadcastScalarLike( x, i, { + 'dtype': 'generic' + }); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( o ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/repl.txt new file mode 100644 index 000000000000..b105675e11e9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/repl.txt @@ -0,0 +1,58 @@ + +{{alias}}( x, value[, options] ) + Broadcasts a scalar value to an ndarray having the same shape and data type + as a provided input ndarray. + + If `value` is a number and the resolved data type 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. + + The function does not guard against precision loss when `value` is a number + and the resolved data type is an integer data type. + + Parameters + ---------- + x: ndarray + Input ndarray. + + value: any + Scalar value. + + options: Object (optional) + Options. + + options.dtype: string|DataType (optional) + Data type. By default, the output array has the same data type as the + provided input ndarray. + + options.shape: ArrayLike (optional) + Output array shape. By default, the output array has the same shape as + the provided input ndarray. + + options.order: string (optional) + Specifies whether an array is row-major (C-style) or column-major + (Fortran-style). By default, the output array has the same order as the + provided input ndarray. + + options.readonly: boolean (optional) + Boolean indicating whether an array should be read-only. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var o = { 'dtype': 'float32' }; + > var x = {{alias:@stdlib/ndarray/empty}}( [ 2, 2 ], o ) + + > var y = {{alias}}( x, 1.0 ) + [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ] + > var dt = String( {{alias:@stdlib/ndarray/dtype}}( y ) ) + 'float32' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/types/index.d.ts new file mode 100644 index 000000000000..3c000b27fcaf --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/types/index.d.ts @@ -0,0 +1,760 @@ +/* +* @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 { ndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray, genericndarray, boolndarray, Shape, DataType, Order, Float64DataType, Float32DataType, Complex128DataType, Complex64DataType, BooleanDataType, Int32DataType, Int16DataType, Int8DataType, Uint32DataType, Uint16DataType, Uint8DataType, Uint8cDataType, GenericDataType } from '@stdlib/types/ndarray'; + +/** +* Interface defining function options. +*/ +interface BaseOptions { + /** + * Output array shape. + */ + shape?: Shape; + + /** + * Specifies whether an array is row-major (C-style) or column-major (Fortran-style). + */ + order?: Order; + + /** + * Boolean indicating whether an array should be read-only. Default: false. + */ + readonly?: boolean; +} + +/** +* Interface defining function options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; +} + +/** +* Interface defining function options. +*/ +interface Float64Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Float64DataType; +} + +/** +* Interface defining function options. +*/ +interface Float32Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Float32DataType; +} + +/** +* Interface defining function options. +*/ +interface Complex128Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Complex128DataType; +} + +/** +* Interface defining function options. +*/ +interface Complex64Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Complex64DataType; +} + +/** +* Interface defining function options. +*/ +interface BoolOptions extends BaseOptions { + /** + * Output array data type. + */ + dtype: BooleanDataType; +} + +/** +* Interface defining function options. +*/ +interface Int32Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Int32DataType; +} + +/** +* Interface defining function options. +*/ +interface Int16Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Int16DataType; +} + +/** +* Interface defining function options. +*/ +interface Int8Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Int8DataType; +} + +/** +* Interface defining function options. +*/ +interface Uint32Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Uint32DataType; +} + +/** +* Interface defining function options. +*/ +interface Uint16Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Uint16DataType; +} + +/** +* Interface defining function options. +*/ +interface Uint8Options extends BaseOptions { + /** + * Output array data type. + */ + dtype: Uint8DataType; +} + +/** +* Interface defining function options. +*/ +interface Uint8cOptions extends BaseOptions { + /** + * Output array data type. + */ + dtype: Uint8cDataType; +} + +/** +* Interface defining function options. +*/ +interface GenericOptions extends BaseOptions { + /** + * Output array data type. + */ + dtype: GenericDataType; +} + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'float64' +* }); +* // returns +* +* 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( x: float64ndarray, value: number, options?: BaseOptions ): float64ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'float64' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Float64Options ): float64ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'float32' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: float32ndarray, value: number, options?: BaseOptions ): float32ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'float32' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Float32Options ): float32ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'complex128' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: complex128ndarray, value: number | ComplexLike, options?: BaseOptions ): complex128ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'complex128' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number | ComplexLike, options: Complex128Options ): complex128ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'complex64' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: complex64ndarray, value: number | ComplexLike, options?: BaseOptions ): complex64ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'complex64' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number | ComplexLike, options: Complex64Options ): complex64ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'bool' +* }); +* // returns +* +* var out = broadcastScalarLike( x, true ); +* // returns +*/ +declare function broadcastScalarLike( x: boolndarray, value: boolean, options?: BaseOptions ): boolndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, true, { +* 'dtype': 'bool' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: boolean, options: BoolOptions ): boolndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'int32' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: int32ndarray, value: number, options?: BaseOptions ): int32ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'int32' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Int32Options ): int32ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'int16' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: int16ndarray, value: number, options?: BaseOptions ): int16ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'int16' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Int16Options ): int16ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'int8' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: int8ndarray, value: number, options?: BaseOptions ): int8ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'int8' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Int8Options ): int8ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'uint32' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: uint32ndarray, value: number, options?: BaseOptions ): uint32ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'uint32' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Uint32Options ): uint32ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'uint16' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: uint16ndarray, value: number, options?: BaseOptions ): uint16ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'uint16' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Uint16Options ): uint16ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'uint8' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: uint8ndarray, value: number, options?: BaseOptions ): uint8ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'uint8' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Uint8Options ): uint8ndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'uint8c' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: uint8cndarray, value: number, options?: BaseOptions ): uint8cndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'uint8c' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: number, options: Uint8cOptions ): uint8cndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'generic' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: genericndarray, value: T, options?: BaseOptions ): genericndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ] ); +* // returns +* +* var out = broadcastScalarLike( x, 1.0, { +* 'dtype': 'generic' +* }); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: T, options?: GenericOptions ): genericndarray; + +/** +* Broadcasts a scalar value to an ndarray having the same shape and data type as a provided input ndarray. +* +* @param x - input ndarray +* @param value - scalar value +* @param options - function options +* @returns ndarray +* +* @example +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'generic' +* }); +* // returns +* +* var out = broadcastScalarLike( x, 1.0 ); +* // returns +*/ +declare function broadcastScalarLike( x: ndarray, value: T, options?: Options ): ndarray; + + +// EXPORTS // + +export = broadcastScalarLike; diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/types/test.ts new file mode 100644 index 000000000000..f0c20e2ff153 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/docs/types/test.ts @@ -0,0 +1,133 @@ +/* +* @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. +*/ + +/* eslint-disable space-in-parens */ + +import empty = require( '@stdlib/ndarray/empty' ); +import broadcastScalarLike = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + var x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + broadcastScalarLike( x, 1.0 ); // $ExpectType float64ndarray + + broadcastScalarLike( x, 1.0, { 'dtype': 'float64' } ); // $ExpectType float64ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'float32' } ); // $ExpectType float32ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'complex128' } ); // $ExpectType complex128ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'complex64' } ); // $ExpectType complex64ndarray + broadcastScalarLike( x, true, { 'dtype': 'bool' } ); // $ExpectType boolndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'int32' } ); // $ExpectType int32ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'int16' } ); // $ExpectType int16ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'int8' } ); // $ExpectType int8ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'uint32' } ); // $ExpectType uint32ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'uint16' } ); // $ExpectType uint16ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'uint8' } ); // $ExpectType uint8ndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'uint8c' } ); // $ExpectType uint8cndarray + broadcastScalarLike( x, 1.0, { 'dtype': 'generic' } ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + broadcastScalarLike( '10', 1.0 ); // $ExpectError + broadcastScalarLike( 5, 1.0 ); // $ExpectError + broadcastScalarLike( false, 1.0 ); // $ExpectError + broadcastScalarLike( true, 1.0 ); // $ExpectError + broadcastScalarLike( null, 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 options argument which is not an object... +{ + var x = empty( [ 2, 2 ] ); + + broadcastScalarLike( x, 1.0, '10' ); // $ExpectError + broadcastScalarLike( x, 1.0, 5 ); // $ExpectError + broadcastScalarLike( x, 1.0, false ); // $ExpectError + broadcastScalarLike( x, 1.0, true ); // $ExpectError + broadcastScalarLike( x, 1.0, null ); // $ExpectError + broadcastScalarLike( x, 1.0, [] ); // $ExpectError + broadcastScalarLike( x, 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dtype` option which is not a recognized/supported data type... +{ + var x = empty( [ 2, 2 ] ); + + broadcastScalarLike( x, 1.0, { 'dtype': '10' } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'dtype': 5 } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'dtype': false } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'dtype': true } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'dtype': null } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'dtype': [] } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'dtype': {} } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `shape` option which is not an array of numbers... +{ + var x = empty( [ 2, 2 ] ); + + broadcastScalarLike( x, 1.0, { 'shape': '10' } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'shape': 5 } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'shape': false } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'shape': true } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'shape': null } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'shape': {} } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'shape': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `order` option which is not a recognized/supported data order... +{ + var x = empty( [ 2, 2 ] ); + + broadcastScalarLike( x, 1.0, { 'order': '10' } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'order': 5 } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'order': false } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'order': true } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'order': null } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'order': [] } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'order': {} } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'order': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `readonly` option which is not a boolean... +{ + var x = empty( [ 2, 2 ] ); + + broadcastScalarLike( x, 1.0, { 'readonly': '10' } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'readonly': 5 } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'readonly': null } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'readonly': [] } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'readonly': {} } ); // $ExpectError + broadcastScalarLike( x, 1.0, { 'readonly': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + var x = empty( [ 2, 2 ] ); + + broadcastScalarLike(); // $ExpectError + broadcastScalarLike( x, 1.0, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/examples/index.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/examples/index.js new file mode 100644 index 000000000000..f4e1a44449a4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/examples/index.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var empty = require( '@stdlib/ndarray/empty' ); +var broadcastScalarLike = require( './../lib' ); + +// Get a list of data types: +var dt = dtypes( 'integer_and_generic' ); + +// Create an input array: +var x = empty( [ 2, 2 ] ); + +// Generate broadcasted arrays... +var y; +var i; +for ( i = 0; i < dt.length; i++ ) { + y = broadcastScalarLike( x, i, { + 'dtype': dt[ i ] + }); + console.log( y.get( 0, 1 ) ); +} diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/lib/index.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/lib/index.js new file mode 100644 index 000000000000..ae36fa4056b0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/lib/index.js @@ -0,0 +1,50 @@ +/** +* @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/broadcast-scalar-like +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* var broadcastScalarLike = require( '@stdlib/ndarray/broadcast-scalar-like' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'float32' +* }); +* // returns +* +* 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/broadcast-scalar-like/lib/main.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/lib/main.js new file mode 100644 index 000000000000..93050562fec0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/lib/main.js @@ -0,0 +1,145 @@ +/** +* @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 isComplexDataType = require( '@stdlib/ndarray/base/assert/is-complex-floating-point-data-type' ); +var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); +var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives; +var isAccessorArray = require( '@stdlib/array/base/assert/is-accessor-array' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +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 getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +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. +* +* ## Notes +* +* - If `value` is a number and the resolved data type is a complex 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 function does not guard against precision loss when `value` is a number and the resolved data type is an integer data type. +* +* @param {ndarray} x - input ndarray +* @param {*} value - scalar value +* @param {Options} [options] - function options +* @param {*} [options.dtype] - output array data type +* @param {NonNegativeIntegerArray} [options.shape] - output array shape +* @param {string} [options.order] - memory layout (either row-major or column-major) +* @param {boolean} [options.readonly=false] - boolean indicating whether an array should be read-only +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} options argument must be an object +* @throws {TypeError} `shape` option must be an array of nonnegative integers +* @throws {TypeError} `dtype` option must be a recognized data type +* @returns {ndarray} ndarray +* +* @example +* var getDType = require( '@stdlib/ndarray/dtype' ); +* var empty = require( '@stdlib/ndarray/empty' ); +* +* var x = empty( [ 2, 2 ], { +* 'dtype': 'float32' +* }); +* // returns +* +* 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, options ) { + var opts; + var buf; + var set; + var sh; + var dt; + var v; + var N; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + opts = { + 'dtype': getDType( x ), + 'shape': getShape( x ), + 'order': getOrder( x ), + 'readonly': false + }; + if ( arguments.length > 2 ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'dtype' ) ) { + opts.dtype = options.dtype; + } + if ( hasOwnProp( options, 'shape' ) ) { + if ( + !isNonNegativeIntegerArray( options.shape ) && + !isEmptyCollection( options.shape ) + ) { + throw new TypeError( format( 'invalid option. `%s` option must be an array of nonnegative integers. Option: `%s`.', 'shape', options.shape ) ); + } + opts.shape = options.shape; + } + if ( hasOwnProp( options, 'order' ) ) { + opts.order = options.order; + } + if ( hasOwnProp( options, 'readonly' ) ) { + opts.readonly = options.readonly; + } + } + sh = opts.shape; + dt = opts.dtype; + buf = buffer( dt, 1 ); + if ( buf === null ) { + throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', dt ) ); + } + if ( isComplexDataType( dt ) && isNumber( value ) ) { + 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 = sh.length || 1; + return new ndarray( dt, buf, sh, zeros( N ), 0, opts.order, opts ); +} + + +// EXPORTS // + +module.exports = broadcastScalarLike; diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/package.json b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/package.json new file mode 100644 index 000000000000..7ff0b1aa29aa --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/ndarray/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", + "ndarray", + "broadcast", + "broadcasting", + "scalar", + "like", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/test/test.js b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/test/test.js new file mode 100644 index 000000000000..a58871a5f335 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/broadcast-scalar-like/test/test.js @@ -0,0 +1,908 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Uint32Array = require( '@stdlib/array/uint32' ); +var Int16Array = require( '@stdlib/array/int16' ); +var Uint16Array = require( '@stdlib/array/uint16' ); +var Int8Array = require( '@stdlib/array/int8' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); +var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var empty = require( '@stdlib/ndarray/empty' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var numel = require( '@stdlib/ndarray/numel' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +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 which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( value, 1.0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + x = empty( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( x, 1.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `shape` option which is not an array of nonnegative integers', function test( t ) { + var values; + var x; + var i; + + values = [ + [ -1, 2, 3 ], + [ -1, -2, -3 ], + '5', + 5, + true, + false, + null, + void 0, + [ 'foo' ], + function noop() {} + ]; + x = empty( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( x, 1.0, { + 'shape': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dtype` option which is not a recognized data type', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 'beepboop', + 'foo', + 'bar', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + x = empty( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( x, 1.0, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an `order` option which is not a recognized order', function test( t ) { + var values; + var i; + var x; + + values = [ + '5', + 'beepboop', + 'foo', + 'bar', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + x = empty( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( x, 1.0, { + 'order': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `readonly` option which is not a boolean', function test( t ) { + var values; + var i; + var x; + + values = [ + '5', + 5, + null, + void 0, + [], + {}, + function noop() {} + ]; + x = empty( [ 2, 2 ] ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + broadcastScalarLike( x, 1.0, { + 'readonly': value + }); + }; + } +}); + +tape( 'the function returns an ndarray having the same shape and data type as a provided ndarray (default, float64)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + arr = broadcastScalarLike( x, 1.0 ); + expected = new Float64Array( [ 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + t.deepEqual( ndarray2array( arr ), [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the same shape and data type as a provided ndarray (default, float32)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ], { + 'dtype': 'float32' + }); + arr = broadcastScalarLike( x, 1.0 ); + expected = new Float32Array( [ 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( arr ), 4, 'returns expected value' ); + t.deepEqual( ndarray2array( arr ), [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ], 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the same shape and data type as a provided ndarray (default, complex128)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2, 2 ], { + 'dtype': 'complex128' + }); + v = new Complex128( 1.0, 2.0 ); + + arr = broadcastScalarLike( x, v ); + expected = new Float64Array( [ 1.0, 2.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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the same shape and data type as a provided ndarray (default, complex64)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2, 2 ], { + 'dtype': 'complex64' + }); + v = new Complex64( 1.0, 2.0 ); + + arr = broadcastScalarLike( x, v ); + expected = new Float32Array( [ 1.0, 2.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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the same shape and data type as a provided ndarray (default, bool)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ], { + 'dtype': 'bool' + }); + + arr = broadcastScalarLike( x, true ); + expected = new Uint8Array( [ 1 ] ); + + 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + arr = broadcastScalarLike( x, false ); + expected = new Uint8Array( [ 0 ] ); + + 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the same shape and data type as a provided ndarray (default, generic)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ], { + 'dtype': 'generic' + }); + + arr = broadcastScalarLike( x, 'beep' ); + expected = [ 'beep' ]; + + 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( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=float64)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1.0, { + 'dtype': 'float64' + }); + expected = new Float64Array( [ 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=float32)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1.0, { + 'dtype': 'float32' + }); + expected = new Float32Array( [ 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( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=int32)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'int32' + }); + expected = new Int32Array( [ 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=int16)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'int16' + }); + expected = new Int16Array( [ 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( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=int8)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'int8' + }); + expected = new Int8Array( [ 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=uint32)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'uint32' + }); + expected = new Uint32Array( [ 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( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=uint16)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'uint16' + }); + expected = new Uint16Array( [ 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=uint8)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'uint8' + }); + expected = new Uint8Array( [ 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( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=uint8c)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'uint8c' + }); + expected = new Uint8ClampedArray( [ 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( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=bool)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, true, { + 'dtype': 'bool' + }); + expected = new Uint8Array( [ 1 ] ); + + 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + arr = broadcastScalarLike( x, false, { + 'dtype': 'bool' + }); + expected = new Uint8Array( [ 0 ] ); + + 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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=complex128, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2, 2 ] ); + v = new Complex128( 1.0, 2.0 ); + + arr = broadcastScalarLike( x, v, { + 'dtype': 'complex128' + }); + expected = new Float64Array( [ 1.0, 2.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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=complex128, real)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ], { + 'dtype': 'float64' + }); + + arr = broadcastScalarLike( x, 1.0, { + 'dtype': 'complex128' + }); + expected = new Float64Array( [ 1.0, 0.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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=complex64, complex)', function test( t ) { + var expected; + var arr; + var x; + var v; + + x = empty( [ 2, 2 ] ); + v = new Complex64( 1.0, 2.0 ); + + arr = broadcastScalarLike( x, v, { + 'dtype': 'complex64' + }); + expected = new Float32Array( [ 1.0, 2.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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=complex64, real)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1.0, { + 'dtype': 'complex64' + }); + expected = new Float32Array( [ 1.0, 0.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 ), 'row-major', 'returns expected value' ); + t.strictEqual( numel( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns an ndarray having the specified data type (dtype=generic)', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'generic' + }); + expected = [ 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( arr ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the output array shape', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1.0, { + 'shape': [ 2, 2, 2 ] + }); + expected = [ + [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ], + [ [ 1.0, 1.0 ], [ 1.0, 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, 2 ], 'returns expected value' ); + t.strictEqual( numel( arr ), 8, 'returns expected value' ); + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying an empty output array shape', function test( t ) { + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1.0, { + 'shape': [] + }); + + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( arr ), [], 'returns expected value' ); + t.strictEqual( numel( arr ), 1, 'returns expected value' ); + t.strictEqual( arr.get(), 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying the array order', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'generic', + 'order': 'column-major' + }); + expected = [ 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 ), '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; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'generic', + 'readonly': true + }); + expected = [ 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( arr ), 4, 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports returning an array which is writable', function test( t ) { + var expected; + var arr; + var x; + + x = empty( [ 2, 2 ] ); + + arr = broadcastScalarLike( x, 1, { + 'dtype': 'generic', + 'readonly': false + }); + expected = [ 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( arr ), 4, 'returns expected value' ); + t.strictEqual( isReadOnly( arr ), false, 'returns expected value' ); + + t.end(); +});