diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/README.md b/lib/node_modules/@stdlib/blas/ext/unitspace/README.md new file mode 100644 index 000000000000..e1ca609b7351 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/README.md @@ -0,0 +1,184 @@ + + +# unitspace + +> Return a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +
+ +## Usage + +```javascript +var unitspace = require( '@stdlib/blas/ext/unitspace' ); +``` + +#### unitspace( shape, start\[, options] ) + +Returns a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +```javascript +var x = unitspace( [ 4 ], 1.0 ); +// returns [ 1.0, 2.0, 3.0, 4.0 ] +``` + +The function has the following parameters: + +- **shape**: array shape. +- **start**: starting value. May be either a number, a complex number, or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, a start [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input shape, a start [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`. +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If a data type is provided, `start` is cast to the specified data type. If a data type is not provided, the default output array data type is the same as the data type of `start`. +- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). If `start` is a scalar value, the default order is `'row-major'`. If `start` is an [ndarray][@stdlib/ndarray/ctor], the default order is the same as the memory layout of `start`. +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. +- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`. + +When provided a scalar or zero-dimensional [ndarray][@stdlib/ndarray/ctor] `start` argument, the value is broadcast across all elements in the shape defined by the complement of those dimensions specified by `options.dims`. To specify separate sub-array starting values, provide a non-zero-dimensional [ndarray][@stdlib/ndarray/ctor] argument. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var start = array( [ 1.0, 5.0 ] ); +// returns [ 1.0, 5.0 ] + +var x = unitspace( [ 2, 3 ], start ); +// returns [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ] +``` + +By default, the function generates linearly spaced values along the last dimension of an output [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. + +```javascript +var x = unitspace( [ 2, 2 ], 1.0, { + 'dims': [ 0, 1 ] +}); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +``` + +To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option. + +```javascript +var x = unitspace( [ 4 ], 1.0, { + 'dtype': 'float32' +}); +// returns [ 1.0, 2.0, 3.0, 4.0 ] +``` + +#### unitspace.assign( x, start\[, options] ) + +Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. + +```javascript +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = zeros( [ 4 ] ); +// returns [ 0.0, 0.0, 0.0, 0.0 ] + +var out = unitspace.assign( x, 1.0 ); +// returns [ 1.0, 2.0, 3.0, 4.0 ] + +var bool = ( x === out ); +// returns true +``` + +The function has the following parameters: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. +- **start**: starting value. May be either a number, a complex number, or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, a start [ndarray][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], a start [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`. + +
+ + + +
+ +## Notes + +- When writing to a complex floating-point output [ndarray][@stdlib/ndarray/ctor], a real-valued `start` value is treated as a complex number having a real component equaling the provided value and having an imaginary component equaling zero. +- The `start` argument is cast to the data type of the output [ndarray][@stdlib/ndarray/ctor]. +- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of an output [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional output [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an output [ndarray][@stdlib/ndarray/ctor] to contiguous memory before filling with linearly spaced values. + +
+ + + +
+ +## Examples + + + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var unitspace = require( '@stdlib/blas/ext/unitspace' ); + +// Create a vector of starting values: +var start = unitspace( [ 5 ], 1 ); + +// Create a grid: +var out = unitspace( [ 5, 5 ], start ); +console.log( ndarray2array( out ) ); + +// Generate values over multiple dimensions: +out = unitspace( [ 5, 5 ], 1, { + 'dims': [ 0, 1 ] +}); +console.log( ndarray2array( out ) ); + +// Generate values over multiple dimensions in column-major order: +out = unitspace( [ 5, 5 ], 1, { + 'dims': [ 0, 1 ], + 'order': 'column-major' +}); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/blas/ext/unitspace/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..4b8e6c71f71e --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/benchmark/benchmark.assign.js @@ -0,0 +1,103 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var assign = require( './../lib/assign.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = zeros( [ len ], options ); + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = assign( x, i ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( x.get( i%len ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/ext/unitspace/benchmark/benchmark.js new file mode 100644 index 000000000000..f4e8ca1abe57 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/benchmark/benchmark.js @@ -0,0 +1,101 @@ +/** +* @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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var unitspace = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = unitspace( [ len ], i, options ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get( i%len ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/docs/repl.txt b/lib/node_modules/@stdlib/blas/ext/unitspace/docs/repl.txt new file mode 100644 index 000000000000..4d5b126b6ed6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/docs/repl.txt @@ -0,0 +1,136 @@ + +{{alias}}( shape, start[, options] ) + Returns a new ndarray filled with linearly spaced numeric elements which + increment by 1 starting from a specified value along one or more ndarray + dimensions. + + When writing to a complex floating-point output array, a real-valued `start` + value is treated as a complex number having a real component equaling the + provided value and having an imaginary component equaling zero. + + The `start` argument is cast to the data type of the output array. + + Parameters + ---------- + shape: Array|integer + Array shape. + + start: ndarray|number|Complex + Starting value. May be either a number, a complex number, or an ndarray + having a numeric or "generic" data type. If provided an ndarray, the + value must have a shape which is broadcast compatible with the + complement of the shape defined by `options.dims`. For example, given + the input shape `[2, 3, 4]` and `options.dims=[0]`, a start ndarray must + have a shape which is broadcast compatible with the shape `[3, 4]`. + Similarly, when performing the operation over all elements in a provided + input shape, a start ndarray must be a zero-dimensional ndarray. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform operation. If not provided, + the function generates linearly spaced values along the last dimension. + Default: [-1]. + + options.dtype: string|DataType (optional) + Output ndarray data type. Must be a numeric or "generic" data type. If + provided, `start` is cast to the specified data type. If not provided, + the default output array data type is the same as the data type of + `start`. + + options.order: string (optional) + Specifies whether an array is row-major (C-style) or column-major + (Fortran-style). If `start` is a scalar value, the default order is + 'row-major'. If `start` is an ndarray, the default order is the same as + the memory layout of `start`. + + options.mode: string (optional) + Specifies how to handle indices which exceed array dimensions. If equal + to 'throw', an ndarray instance throws an error when an index exceeds + array dimensions. If equal to 'normalize', an ndarray instance + normalizes negative indices and throws an error when an index exceeds + array dimensions. If equal to 'wrap', an ndarray instance wraps around + indices exceeding array dimensions using modulo arithmetic. If equal to + 'clamp', an ndarray instance sets an index exceeding array dimensions + to either `0` (minimum index) or the maximum index. Default: 'throw'. + + options.submode: Array (optional) + Specifies how to handle subscripts which exceed array dimensions. If a + mode for a corresponding dimension is equal to 'throw', an ndarray + instance throws an error when a subscript exceeds array dimensions. If + equal to 'normalize', an ndarray instance normalizes negative + subscripts and throws an error when a subscript exceeds array + dimensions. If equal to 'wrap', an ndarray instance wraps around + subscripts exceeding array dimensions using modulo arithmetic. If equal + to 'clamp', an ndarray instance sets a subscript exceeding array + dimensions to either `0` (minimum index) or the maximum index. If the + number of modes is fewer than the number of dimensions, the function + recycles modes using modulo arithmetic. Default: [ options.mode ]. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var out = {{alias}}( [ 4 ], 1.0 ) + [ 1.0, 2.0, 3.0, 4.0 ] + + +{{alias}}.assign( x, start[, options] ) + Fills an ndarray with linearly spaced numeric elements which increment by + 1 starting from a specified value along one or more ndarray dimensions. + + The function fills an ndarray in-place and thus mutates the input ndarray. + + When writing to a complex floating-point output array, a real-valued `start` + value is treated as a complex number having a real component equaling the + provided value and having an imaginary component equaling zero. + + The `start` argument is cast to the data type of the output array. + + The function iterates over ndarray elements according to the memory layout + of an output ndarray. Accordingly, performance degradation is possible when + operating over multiple dimensions of a large non-contiguous multi- + dimensional output ndarray. + + Parameters + ---------- + x: ndarray + Input array. Must have a numeric or "generic" data type. + + start: ndarray|number|Complex + Starting value. May be either a number, a complex number, or an ndarray + having a numeric or "generic" data type. If provided an ndarray, the + value must have a shape which is broadcast compatible with the + complement of the shape defined by `options.dims`. For example, given + the input shape `[2, 3, 4]` and `options.dims=[0]`, a start ndarray must + have a shape which is broadcast compatible with the shape `[3, 4]`. + Similarly, when performing the operation over all elements in a provided + input array, a start ndarray must be a zero-dimensional ndarray. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform operation. If not provided, + the function generates linearly spaced values along the last dimension. + Default: [-1]. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/zeros}}( [ 4 ] ); + > var out = {{alias}}.assign( x, 1.0 ) + [ 1.0, 2.0, 3.0, 4.0 ] + > var bool = ( out === x ) + true + + See Also + -------- diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/ext/unitspace/docs/types/index.d.ts new file mode 100644 index 000000000000..7f034127f9df --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/docs/types/index.d.ts @@ -0,0 +1,217 @@ +/* +* @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 { ArrayLike } from '@stdlib/types/array'; +import { NumericAndGenericDataType as DataType, realcomplexndarray, realndarray, complexndarray, genericndarray, Mode, Order } from '@stdlib/types/ndarray'; +import { ComplexLike } from '@stdlib/types/complex'; + +/** +* Starting value. +*/ +type Start = number | ComplexLike | realcomplexndarray | genericndarray; + +/** +* Starting value. +*/ +type RealStart = number | realndarray | genericndarray; + +/** +* Starting value. +*/ +type ComplexStart = ComplexLike | complexndarray | genericndarray; + +/** +* Output array. +*/ +type OutputArray = realcomplexndarray | genericndarray; + +/** +* Output array. +*/ +type RealOutputArray = realndarray | genericndarray; + +/** +* Output array. +*/ +type ComplexOutputArray = complexndarray | genericndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform operation. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Array order (either 'row-major' (C-style) or 'column-major' (Fortran-style)). + */ + order?: Order; + + /** + * Specifies how to handle a linear index which exceeds array dimensions (default: 'throw'). + */ + mode?: Mode; + + /** + * Specifies how to handle subscripts which exceed array dimensions on a per dimension basis (default: ['throw']). + */ + submode?: Array; +} + +/** +* Interface defining options. +*/ +interface OptionsWithDataType extends Options { + /** + * Output ndarray data type. + */ + dtype: DataType; +} + +/** +* Interface for performing an operation on an ndarray. +*/ +interface Unitspace { + /** + * Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. + * + * @param shape - array shape + * @param start - starting value + * @param options - function options + * @returns output ndarray + * + * @example + * var out = unitspace( [ 2, 3 ], 1.0, { + * 'dtype': 'float64' + * }); + * // returns [ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ] + */ + ( shape: number | ArrayLike, start: T, options: OptionsWithDataType ): OutputArray; // TODO: we lose some type specificity here. We could likely improve specificity here by using type maps + + /** + * Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. + * + * @param shape - array shape + * @param start - starting value + * @param options - function options + * @returns output ndarray + * + * @example + * var out = unitspace( [ 2, 3 ], 1.0 ); + * // returns [ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ] + */ + ( shape: number | ArrayLike, start: T, options?: Options ): RealOutputArray; // NOTE: we lose some type specificity here, as the output ndarray data type is determined according to the data type of `start` + + /** + * Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. + * + * @param shape - array shape + * @param start - starting value + * @param options - function options + * @returns output ndarray + * + * @example + * var Complex128 = require( '@stdlib/complex/float64/ctor' ); + * + * var out = unitspace( [ 2, 3 ], new Complex128( 1.0, 0.0 ), { + * 'dtype': 'complex128' + * }); + * // returns + */ + ( shape: number | ArrayLike, start: T, options: OptionsWithDataType ): ComplexOutputArray; // TODO: we lose some type specificity here. We could likely improve specificity here by using type maps + + /** + * Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. + * + * @param shape - array shape + * @param start - starting value + * @param options - function options + * @returns output ndarray + * + * @example + * var Complex128 = require( '@stdlib/complex/float64/ctor' ); + * + * var out = unitspace( [ 2, 3 ], new Complex128( 1.0, 0.0 ) ); + * // returns + */ + ( shape: number | ArrayLike, start: T, options?: Options ): ComplexOutputArray; // NOTE: we lose some type specificity here, as the output ndarray data type is determined according to the data type of `start` + + /** + * Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. + * + * @param x - input ndarray + * @param start - starting value + * @param options - function options + * @returns input ndarray + * + * @example + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var x = zeros( [ 2, 3 ] ); + * // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] + * + * var out = unitspace.assign( x, 1.0 ); + * // returns [ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ] + * + * var bool = ( out === x ); + * // returns true + */ + assign( x: T, start: Start, options?: BaseOptions ): T; +} + +/** +* Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. +* +* @param shape - array shape +* @param start - starting value +* @param options - function options +* @returns output ndarray +* +* @example +* var out = unitspace( [ 2, 3 ], 1.0 ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ] +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = zeros( [ 2, 3 ] ); +* // returns [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] +* +* var out = unitspace.assign( x, 1.0 ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ] +* +* var bool = ( out === x ); +* // returns true +*/ +declare const unitspace: Unitspace; + + +// EXPORTS // + +export = unitspace; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/docs/types/test.ts b/lib/node_modules/@stdlib/blas/ext/unitspace/docs/types/test.ts new file mode 100644 index 000000000000..30eaf340f9ad --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/docs/types/test.ts @@ -0,0 +1,212 @@ +/* +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +import unitspace = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + unitspace( [ 4 ], 1.0 ); // $ExpectType RealOutputArray + unitspace( [ 4 ], 1.0, {} ); // $ExpectType RealOutputArray + unitspace( [ 4 ], 1.0, { 'dtype': 'float32' } ); // $ExpectType OutputArray +} + +// The compiler throws an error if the function is provided a first argument which is not a number or an array of numbers... +{ + unitspace( '5', 1.0 ); // $ExpectError + unitspace( true, 1.0 ); // $ExpectError + unitspace( false, 1.0 ); // $ExpectError + unitspace( null, 1.0 ); // $ExpectError + unitspace( void 0, 1.0 ); // $ExpectError + unitspace( {}, 1.0 ); // $ExpectError + unitspace( ( x: number ): number => x, 1.0 ); // $ExpectError + + unitspace( '5', 1.0, {} ); // $ExpectError + unitspace( true, 1.0, {} ); // $ExpectError + unitspace( false, 1.0, {} ); // $ExpectError + unitspace( null, 1.0, {} ); // $ExpectError + unitspace( void 0, 1.0, {} ); // $ExpectError + unitspace( {}, 1.0, {} ); // $ExpectError + unitspace( ( x: number ): number => x, 1.0, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an ndarray or supported scalar value... +{ + unitspace( [ 4 ], 'foo' ); // $ExpectError + unitspace( [ 4 ], true ); // $ExpectError + unitspace( [ 4 ], false ); // $ExpectError + unitspace( [ 4 ], null ); // $ExpectError + unitspace( [ 4 ], void 0 ); // $ExpectError + unitspace( [ 4 ], [] ); // $ExpectError + unitspace( [ 4 ], {} ); // $ExpectError + unitspace( [ 4 ], ( x: number ): number => x ); // $ExpectError + + unitspace( [ 4 ], 'foo', {} ); // $ExpectError + unitspace( [ 4 ], true, {} ); // $ExpectError + unitspace( [ 4 ], false, {} ); // $ExpectError + unitspace( [ 4 ], null, {} ); // $ExpectError + unitspace( [ 4 ], void 0, {} ); // $ExpectError + unitspace( [ 4 ], [], {} ); // $ExpectError + unitspace( [ 4 ], {}, {} ); // $ExpectError + unitspace( [ 4 ], ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + unitspace( [ 4 ], 1.0, '5' ); // $ExpectError + unitspace( [ 4 ], 1.0, 5 ); // $ExpectError + unitspace( [ 4 ], 1.0, true ); // $ExpectError + unitspace( [ 4 ], 1.0, false ); // $ExpectError + unitspace( [ 4 ], 1.0, null ); // $ExpectError + unitspace( [ 4 ], 1.0, [] ); // $ExpectError + unitspace( [ 4 ], 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dtype` option... +{ + unitspace( [ 4 ], 1.0, { 'dtype': '5' } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dtype': 5 } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dtype': true } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dtype': false } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dtype': null } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dtype': [] } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dtype': {} } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dtype': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an invalid `dims` option... +{ + unitspace( [ 4 ], 1.0, { 'dims': '5' } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dims': 5 } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dims': true } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dims': false } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dims': null } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dims': {} } ); // $ExpectError + unitspace( [ 4 ], 1.0, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + unitspace(); // $ExpectError + unitspace( [ 4 ] ); // $ExpectError + unitspace( [ 4 ], 1.0, {}, {} ); // $ExpectError +} + +// Attached to the function is an `assign` method which returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + unitspace.assign( x, 1.0 ); // $ExpectType float64ndarray + unitspace.assign( x, 1.0, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an ndarray... +{ + unitspace.assign( '5', 1.0 ); // $ExpectError + unitspace.assign( 5, 1.0 ); // $ExpectError + unitspace.assign( true, 1.0 ); // $ExpectError + unitspace.assign( false, 1.0 ); // $ExpectError + unitspace.assign( null, 1.0 ); // $ExpectError + unitspace.assign( void 0, 1.0 ); // $ExpectError + unitspace.assign( {}, 1.0 ); // $ExpectError + unitspace.assign( ( x: number ): number => x, 1.0 ); // $ExpectError + + unitspace.assign( '5', 1.0, {} ); // $ExpectError + unitspace.assign( 5, 1.0, {} ); // $ExpectError + unitspace.assign( true, 1.0, {} ); // $ExpectError + unitspace.assign( false, 1.0, {} ); // $ExpectError + unitspace.assign( null, 1.0, {} ); // $ExpectError + unitspace.assign( void 0, 1.0, {} ); // $ExpectError + unitspace.assign( {}, 1.0, {} ); // $ExpectError + unitspace.assign( ( x: number ): number => x, 1.0, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an ndarray or supported scalar value... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + unitspace.assign( x, '5' ); // $ExpectError + unitspace.assign( x, true ); // $ExpectError + unitspace.assign( x, false ); // $ExpectError + unitspace.assign( x, null ); // $ExpectError + unitspace.assign( x, void 0 ); // $ExpectError + unitspace.assign( x, [] ); // $ExpectError + unitspace.assign( x, {} ); // $ExpectError + unitspace.assign( x, ( x: number ): number => x ); // $ExpectError + + unitspace.assign( x, '5', {} ); // $ExpectError + unitspace.assign( x, true, {} ); // $ExpectError + unitspace.assign( x, false, {} ); // $ExpectError + unitspace.assign( x, null, {} ); // $ExpectError + unitspace.assign( x, void 0, {} ); // $ExpectError + unitspace.assign( x, [], {} ); // $ExpectError + unitspace.assign( x, {}, {} ); // $ExpectError + unitspace.assign( x, ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an options argument which is not an object... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + unitspace.assign( x, 1.0, '5' ); // $ExpectError + unitspace.assign( x, 1.0, 5 ); // $ExpectError + unitspace.assign( x, 1.0, true ); // $ExpectError + unitspace.assign( x, 1.0, false ); // $ExpectError + unitspace.assign( x, 1.0, null ); // $ExpectError + unitspace.assign( x, 1.0, [] ); // $ExpectError + unitspace.assign( x, 1.0, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an invalid `dims` option... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + unitspace.assign( x, 1.0, { 'dims': '5' } ); // $ExpectError + unitspace.assign( x, 1.0, { 'dims': 5 } ); // $ExpectError + unitspace.assign( x, 1.0, { 'dims': true } ); // $ExpectError + unitspace.assign( x, 1.0, { 'dims': false } ); // $ExpectError + unitspace.assign( x, 1.0, { 'dims': null } ); // $ExpectError + unitspace.assign( x, 1.0, { 'dims': {} } ); // $ExpectError + unitspace.assign( x, 1.0, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + unitspace.assign(); // $ExpectError + unitspace.assign( x ); // $ExpectError + unitspace.assign( x, 1.0, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/examples/index.js b/lib/node_modules/@stdlib/blas/ext/unitspace/examples/index.js new file mode 100644 index 000000000000..6522b212528c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/examples/index.js @@ -0,0 +1,42 @@ +/** +* @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 ndarray2array = require( '@stdlib/ndarray/to-array' ); +var unitspace = require( './../lib' ); + +// Create a vector of starting values: +var start = unitspace( [ 5 ], 1 ); + +// Create a grid: +var out = unitspace( [ 5, 5 ], start ); +console.log( ndarray2array( out ) ); + +// Generate values over multiple dimensions: +out = unitspace( [ 5, 5 ], 1, { + 'dims': [ 0, 1 ] +}); +console.log( ndarray2array( out ) ); + +// Generate values over multiple dimensions in column-major order: +out = unitspace( [ 5, 5 ], 1, { + 'dims': [ 0, 1 ], + 'order': 'column-major' +}); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/assign.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/assign.js new file mode 100644 index 000000000000..e54aadb3d4d9 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/assign.js @@ -0,0 +1,142 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); +var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var contains = require( '@stdlib/array/base/assert/contains' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); +var DTYPES = require( './dtypes.js' ); +var ENUMS = require( './type_enums.js' ); +var resolveDataType = require( './resolve_data_type.js' ); +var normalizeArgument = require( './normalize_argument.js' ); +var defaults = require( './defaults.js' ); +var base = require( './base.js' ); + + +// MAIN // + +/** +* Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. +* +* @param {ndarrayLike} x - input ndarray +* @param {(number|ComplexLike|ndarrayLike)} start - starting value +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims=[-1]] - list of dimensions over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object having at least one dimension +* @throws {TypeError} first argument must have a supported data type +* @throws {TypeError} second argument must be either a number, complex number, or an ndarray-like object +* @throws {TypeError} second argument must have a supported data type +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} input ndarray +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = zeros( [ 4 ] ); +* // returns [ 0.0, 0.0, 0.0, 0.0 ] +* +* var out = assign( x, 1.0 ); +* // returns [ 1.0, 2.0, 3.0, 4.0 ] +* +* var bool = ( out === x ); +* // returns true +*/ +function assign( x, start ) { + var options; + var dtypes; + var type; + var opts; + var ncsh; + var arg; + var sh; + var dt; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + sh = getShape( x ); + if ( sh.length < 1 ) { + throw new TypeError( 'invalid argument. First argument must be an ndarray having at least one dimension.' ); + } + dt = resolveStr( getDType( x ) ); + if ( !contains( DTYPES.odtypes, dt ) ) { + throw new TypeError( format( 'invalid argument. First argument must have one of the following data types: "%s". Data type: `%s`.', join( DTYPES.odtypes, '", "' ), dt ) ); + } + if ( isNumber( start ) ) { + type = ENUMS.NUMBER; + } else if ( isComplexLike( start ) ) { + type = ENUMS.COMPLEX; + } else if ( isndarrayLike( start ) ) { + type = ENUMS.NDARRAY; + dt = resolveStr( getDType( start ) ); + if ( !contains( DTYPES.idtypes0, dt ) ) { + throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( DTYPES.idtypes0, '", "' ), dt ) ); + } + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either a number, complex number, or an ndarray. Value: `%s`.', start ) ); + } + options = defaults(); + + if ( arguments.length > 2 ) { + opts = arguments[ 2 ]; + if ( !isPlainObject( opts ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) ); + } + if ( hasOwnProp( opts, 'dims' ) ) { + if ( !isIntegerArray( opts.dims ) && !isEmptyCollection( opts.dims ) ) { // eslint-disable-line max-len + throw new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', opts.dims ) ); + } + options.dims = opts.dims; + } + } + // Resolve argument data types: + dtypes = resolveDataType( start, type ); + dtypes[ 1 ] = resolveStr( getDType( x ) ); + + // Resolve the complement of the operation dimensions: + ncsh = nonCoreShape( sh, options.dims ); + + // Normalize the provided `start` argument to an ndarray: + arg = normalizeArgument( start, type, dtypes, ncsh, getOrder( x ) ); + + // Perform operation: + return base( x, arg, options ); +} + + +// EXPORTS // + +module.exports = assign; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/base.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/base.js new file mode 100644 index 000000000000..69ea5189f636 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/base.js @@ -0,0 +1,111 @@ +/** +* @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 gunitspace = require( '@stdlib/blas/ext/base/ndarray/gunitspace' ); +var dunitspace = require( '@stdlib/blas/ext/base/ndarray/dunitspace' ); +var sunitspace = require( '@stdlib/blas/ext/base/ndarray/sunitspace' ); +var zunitspace = require( '@stdlib/blas/ext/base/ndarray/zunitspace' ); +var cunitspace = require( '@stdlib/blas/ext/base/ndarray/cunitspace' ); +var factory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' ); +var DTYPES = require( './dtypes.js' ); + + +// VARIABLES // + +var table = { + 'types': [ + 'float64', // output + 'float32', // output + 'complex128', // output + 'complex64' // output + ], + 'fcns': [ + dunitspace, + sunitspace, + zunitspace, + cunitspace + ], + 'default': gunitspace +}; +var options = { + 'strictTraversalOrder': true +}; + + +// MAIN // + +/** +* Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. +* +* @private +* @name unitspace +* @type {Function} +* @param {ndarray} x - input ndarray +* @param {ndarray} start - starting value +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform operation +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} first argument must have a supported data type +* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} input ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var array = require( '@stdlib/ndarray/array' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* // Create a data buffer: +* var xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); +* +* // Define the shape of the input array: +* var sh = [ 2, 1, 3 ]; +* +* // Define the array strides: +* var sx = [ 3, 3, 1 ]; +* +* // Define the index offset: +* var ox = 0; +* +* // Create an input ndarray: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* +* // Create an ndarray containing the starting value: +* var start = array( new Float64Array( [ 1.0, 4.0 ] ), { +* 'shape': [ 2, 1 ] +* }); +* +* // Perform operation: +* var out = unitspace( x, start, { +* 'dims': [ -1 ] +* }); +* // returns [ [ [ 1.0, 2.0, 3.0 ] ], [ [ 4.0, 5.0, 6.0 ] ] ] +*/ +var unitspace = factory( table, [ DTYPES.idtypes0 ], DTYPES.odtypes, options ); + + +// EXPORTS // + +module.exports = unitspace; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/defaults.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/defaults.js new file mode 100644 index 000000000000..8cd28bef85dd --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/defaults.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MAIN // + +/** +* Returns default options. +* +* @private +* @returns {Object} default options +*/ +function defaults() { + return { + 'dims': [ -1 ] // by default, generate linearly spaced values along the last dimension + }; +} + + +// EXPORTS // + +module.exports = defaults; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/dtypes.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/dtypes.js new file mode 100644 index 000000000000..830f423b50ed --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/dtypes.js @@ -0,0 +1,37 @@ +/** +* @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 dtypes2strings = require( '@stdlib/ndarray/base/dtypes2strings' ); +var dtypes = require( '@stdlib/ndarray/dtypes' ); + + +// MAIN // + +var dt = { + 'idtypes0': dtypes2strings( dtypes( 'numeric_and_generic' ) ), // start + 'odtypes': dtypes2strings( dtypes( 'numeric_and_generic' ) ) +}; + + +// EXPORTS // + +module.exports = dt; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/index.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/index.js new file mode 100644 index 000000000000..daedbe57d96d --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/index.js @@ -0,0 +1,62 @@ +/** +* @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'; + +/** +* Return a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. +* +* @module @stdlib/blas/ext/unitspace +* +* @example +* var unitspace = require( '@stdlib/blas/ext/unitspace' ); +* +* var out = unitspace( [ 4 ], 1.0 ); +* // returns [ 1.0, 2.0, 3.0, 4.0 ] +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var unitspace = require( '@stdlib/blas/ext/unitspace' ); +* +* var x = zeros( [ 4 ] ); +* // returns [ 0.0, 0.0, 0.0, 0.0 ] +* +* var out = unitspace.assign( x, 1.0 ); +* // returns [ 1.0, 2.0, 3.0, 4.0 ] +* +* var bool = ( out === x ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/main.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/main.js new file mode 100644 index 000000000000..daf7d03b2cc4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/main.js @@ -0,0 +1,166 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives; +var isEmptyCollection = require( '@stdlib/assert/is-empty-collection' ); +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; +var isComplexLike = require( '@stdlib/assert/is-complex-like' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isOrder = require( '@stdlib/ndarray/base/assert/is-order' ); +var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); +var nonCoreShape = require( '@stdlib/ndarray/base/complement-shape' ); +var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var empty = require( '@stdlib/ndarray/empty' ); +var contains = require( '@stdlib/array/base/assert/contains' ); +var join = require( '@stdlib/array/base/join' ); +var format = require( '@stdlib/string/format' ); +var DTYPES = require( './dtypes.js' ); +var ENUMS = require( './type_enums.js' ); +var resolveDataType = require( './resolve_data_type.js' ); +var resolveOrder = require( './resolve_order.js' ); +var normalizeArgument = require( './normalize_argument.js' ); +var defaults = require( './defaults.js' ); +var base = require( './base.js' ); + + +// MAIN // + +/** +* Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions. +* +* @param {(NonNegativeInteger|NonNegativeIntegerArray)} shape - array shape +* @param {(number|ComplexLike|ndarrayLike)} start - starting value +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims=[-1]] - list of dimensions over which to perform operation +* @param {*} [options.dtype] - output ndarray data type +* @param {string} [options.order] - ndarray order +* @param {string} [options.mode="throw"] - specifies how to handle indices which exceed ndarray dimensions +* @param {ArrayLikeObject} [options.submode=["throw"]] - specifies how to handle subscripts which exceed ndarray dimensions on a per dimension basis +* @throws {TypeError} first argument must be either a nonnegative integer or an array of nonnegative integers +* @throws {TypeError} second argument must be either a number, complex number, or an ndarray-like object +* @throws {TypeError} second argument must have a supported data type +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +* +* @example +* var out = unitspace( [ 4 ], 1.0 ); +* // returns [ 1.0, 2.0, 3.0, 4.0 ] +*/ +function unitspace( shape, start ) { + var options; + var dtypes; + var type; + var opts; + var ncsh; + var arg; + var out; + var sh; + var dt; + + if ( isNonNegativeInteger( shape ) ) { + sh = [ shape ]; + } else if ( isNonNegativeIntegerArray( shape ) ) { + sh = shape; // Note: empty shape (i.e., a shape for a zero-dimensional ndarray) is not allowed + } else { + throw new TypeError( format( 'invalid argument. First argument must be a nonnegative integer or an array of nonnegative integers. Value: `%s`.', shape ) ); + } + if ( isNumber( start ) ) { + type = ENUMS.NUMBER; + } else if ( isComplexLike( start ) ) { + type = ENUMS.COMPLEX; + } else if ( isndarrayLike( start ) ) { + type = ENUMS.NDARRAY; + dt = resolveStr( getDType( start ) ); + if ( !contains( DTYPES.idtypes0, dt ) ) { + throw new TypeError( format( 'invalid argument. Second argument must have one of the following data types: "%s". Data type: `%s`.', join( DTYPES.idtypes0, '", "' ), dt ) ); + } + } else { + throw new TypeError( format( 'invalid argument. Second argument must be either a number, complex number, or an ndarray. Value: `%s`.', start ) ); + } + options = defaults(); + + if ( arguments.length > 2 ) { + opts = arguments[ 2 ]; + if ( !isPlainObject( opts ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) ); + } + if ( hasOwnProp( opts, 'dtype' ) ) { + dt = resolveStr( opts.dtype ); + if ( !isDataType( opts.dtype ) || !contains( DTYPES.odtypes, dt ) ) { // eslint-disable-line max-len + throw new TypeError( format( 'invalid option. `%s` option must be one of the following: "%s". Option: `%s`.', 'dtype', join( DTYPES.odtypes, '", "' ), opts.dtype ) ); + } + options.dtype = dt; + } + if ( hasOwnProp( opts, 'order' ) ) { + if ( !isOrder( opts.order ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a supported order. Option: `%s`.', 'order', opts.order ) ); + } + options.order = opts.order; + } + if ( hasOwnProp( opts, 'mode' ) ) { + // Defer to `empty` to validate below... + options.mode = opts.mode; + } + if ( hasOwnProp( opts, 'submode' ) ) { + // Defer to `empty` to validate below... + options.submode = opts.submode; + } + if ( hasOwnProp( opts, 'dims' ) ) { + if ( !isIntegerArray( opts.dims ) && !isEmptyCollection( opts.dims ) ) { // eslint-disable-line max-len + throw new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', opts.dims ) ); + } + options.dims = opts.dims; + } + } + // Resolve argument data types: + dtypes = resolveDataType( start, type ); + options.dtype = options.dtype || dtypes[ 1 ]; + dtypes[ 1 ] = options.dtype; + + // Resolve the output array order: + options.order = options.order || resolveOrder( start, type ); + + // Resolve the complement of the operation dimensions: + ncsh = nonCoreShape( sh, options.dims ); + + // Normalize the provided `start` argument to an ndarray: + arg = normalizeArgument( start, type, dtypes, ncsh, options.order ); + + // Create an output ndarray: + out = empty( sh, options ); + + // Perform operation: + return base( out, arg, options ); +} + + +// EXPORTS // + +module.exports = unitspace; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/normalize_argument.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/normalize_argument.js new file mode 100644 index 000000000000..c0de6a08e386 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/normalize_argument.js @@ -0,0 +1,93 @@ +/** +* @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 isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' ); +var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var baseEmpty = require( '@stdlib/ndarray/base/empty' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var format = require( '@stdlib/string/format' ); +var ENUMS = require( './type_enums.js' ); + + +// MAIN // + +/** +* Normalizes a `start` argument to an ndarray. +* +* @private +* @param {*} start - starting value +* @param {NonNegativeInteger} type - argument type +* @param {Array} dtypes - argument data types +* @param {NonNegativeIntegerArray} shape - array shape +* @param {string} order - array order +* @throws {TypeError} only (mostly) safe casts are supported +* @returns {ndarray} starting value as an ndarray +*/ +function normalizeArgument( start, type, dtypes, shape, order ) { + var odt; + var out; + var v; + + odt = dtypes[ 1 ]; + + // Case: start === + if ( type === ENUMS.NUMBER ) { + // A number primitive should be able to cast to any supported output data type (real or complex): + out = broadcastScalar( start, odt, shape, order ); + } + // Case: start === + else if ( type === ENUMS.NDARRAY ) { + // Case: start has the same dtype as the output dtype + if ( isEqualDataType( dtypes[ 0 ], odt ) ) { + out = maybeBroadcastArray( start, shape ); + } + // Case: start has a different dtype than the output dtype + else if ( isMostlySafeCast( dtypes[ 0 ], odt ) ) { + // If we have unequal data types, we need to perform a copy... + v = baseEmpty( odt, getShape( start, false ), getOrder( start ) ); + assign( [ start, v ] ); + out = maybeBroadcastArray( v, shape ); + } + // Case: disallowed complex-to-real data type cast + else { + throw new TypeError( format( 'invalid argument. Second argument cannot be safely cast to the desired output data type. Output data type: %s. Argument data type: %s.', String( odt ), String( dtypes[ 0 ] ) ) ); + } + } + // Case: start === + else { + // Complex number scalars should only be able to cast to complex data types... + if ( !isMostlySafeCast( dtypes[ 0 ], odt ) ) { + throw new TypeError( format( 'invalid argument. Second argument cannot be safely cast to the desired output data type. Output data type: %s. Argument data type: %s.', String( odt ), String( dtypes[ 0 ] ) ) ); + } + out = broadcastScalar( start, odt, shape, order ); + } + return out; +} + + +// EXPORTS // + +module.exports = normalizeArgument; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/resolve_data_type.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/resolve_data_type.js new file mode 100644 index 000000000000..d15ce7d4549f --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/resolve_data_type.js @@ -0,0 +1,69 @@ +/** +* @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 getDType = require( '@stdlib/ndarray/base/dtype' ); +var resolveStr = require( '@stdlib/ndarray/base/dtype-resolve-str' ); +var complexDataType = require( '@stdlib/complex/dtype' ); +var ENUMS = require( './type_enums.js' ); + + +// MAIN // + +/** +* Resolves argument data types. +* +* @private +* @param {*} start - starting value +* @param {NonNegativeInteger} type - argument type +* @returns {Array} data types +*/ +function resolveDataType( start, type ) { + var out; + var dt; + + // Initialize a data type array: [ start, out ] + out = [ '', '' ]; + + if ( type === ENUMS.NUMBER ) { + // Why 'float64'? Because we don't have any way of knowing whether a number primitive is intended to be 'float32' or 'float64', and, in order to preserve precision, we simply assume 'float64'... + dt = 'float64'; + } else if ( type === ENUMS.NDARRAY ) { + dt = resolveStr( getDType( start ) ); + } else { // type === ENUMS.COMPLEX + dt = complexDataType( start ); + if ( dt === null ) { + // Why 'complex128'? Because we don't have any way of knowing whether a complex-like object is intended to be 'complex64' or 'complex128', and, in order to preserve precision, we simply assume 'complex128'... + dt = 'complex128'; + } + } + out[ 0 ] = dt; + + // By default, the output array data type matches the data type of `start`: + out[ 1 ] = dt; + + return out; +} + + +// EXPORTS // + +module.exports = resolveDataType; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/resolve_order.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/resolve_order.js new file mode 100644 index 000000000000..51df32235eb4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/resolve_order.js @@ -0,0 +1,53 @@ +/** +* @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 getOrder = require( '@stdlib/ndarray/base/order' ); +var defaults = require( '@stdlib/ndarray/defaults' ); +var ENUMS = require( './type_enums.js' ); + + +// VARIABLES // + +var ORDER = defaults.get( 'order' ); + + +// MAIN // + +/** +* Resolves an output array order based on a provided `start` argument. +* +* @private +* @param {*} start - starting value +* @param {NonNegativeInteger} type - argument type +* @returns {string} order +*/ +function resolveOrder( start, type ) { + if ( type === ENUMS.NDARRAY ) { + return getOrder( start ); + } + return ORDER; +} + + +// EXPORTS // + +module.exports = resolveOrder; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/lib/type_enums.js b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/type_enums.js new file mode 100644 index 000000000000..fcad6de5ce25 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/lib/type_enums.js @@ -0,0 +1,32 @@ +/** +* @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'; + +// MAIN // + +var ENUMS = { + 'NUMBER': 1, + 'COMPLEX': 2, + 'NDARRAY': 3 +}; + + +// EXPORTS // + +module.exports = ENUMS; diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/package.json b/lib/node_modules/@stdlib/blas/ext/unitspace/package.json new file mode 100644 index 000000000000..6b1c12858e7b --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/blas/ext/unitspace", + "version": "0.0.0", + "description": "Return a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions.", + "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", + "stdmath", + "mathematics", + "math", + "blas", + "extended", + "unitspace", + "fill", + "iota", + "sequence", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.assign.js b/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.assign.js new file mode 100644 index 000000000000..6f3dcdac14d6 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.assign.js @@ -0,0 +1,658 @@ +/** +* @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 Complex128Array = require( '@stdlib/array/complex128' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var array = require( '@stdlib/ndarray/array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var assign = require( './../lib/assign.js' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof assign, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + 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() { + assign( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (start=ndarray)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + 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() { + assign( value, scalar2ndarray( 1.0 ) ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + 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() { + assign( value, 1.0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray having a supported data type', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 1.0 ), + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + 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() { + assign( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray having a supported data type (start=ndarray)', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 1.0 ), + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + 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() { + assign( value, scalar2ndarray( 1.0 ) ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 1.0 ), + empty( [ 2, 2 ], { + 'dtype': 'bool' + }) + ]; + 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() { + assign( value, 1.0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a number, complex number, or an ndarray having a supported data type', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + scalar2ndarray( true ) + ]; + 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() { + assign( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a number, complex number, or an ndarray having a supported data type (options)', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + scalar2ndarray( true ) + ]; + 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() { + assign( x, value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + 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() { + assign( x, 1.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + '5', + 5, + NaN, + 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() { + assign( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 5 ], + [ -5 ], + [ 2 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 1, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2 ], { + 'dtype': 'generic' + }); + + values = [ + [ 0, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + assign( x, 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if a complex `start` value cannot be safely cast to the output data type', function test( t ) { + var x; + + x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + t.throws( badValue, TypeError, 'throws an error' ); + t.end(); + + function badValue() { + assign( x, new Complex128( 1.0, 2.0 ) ); + } +}); + +tape( 'the function fills an ndarray with linearly spaced values incrementing by 1 from a specified value', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + expected = [ 1.0, 2.0, 3.0 ]; + + actual = assign( x, 1.0 ); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + expected = [ [ 2.0, 3.0 ], [ 2.0, 3.0 ] ]; + + actual = assign( x, 2.0 ); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + expected = [ [ 2.0, 3.0 ], [ 2.0, 3.0 ] ]; + + actual = assign( x, 2.0 ); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying operation dimensions (row-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [ -1 ] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + expected = [ [ 1.0, 1.0 ], [ 2.0, 2.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [ 0 ] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [ 0, 1 ] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying operation dimensions (column-major)', function test( t ) { + var expected; + var actual; + var xbuf; + var x; + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [ -1 ] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + expected = [ [ 1.0, 1.0 ], [ 2.0, 2.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [ 0 ] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + expected = [ [ 1.0, 3.0 ], [ 2.0, 4.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [ 0, 1 ] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + xbuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0 ] ); + x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + + actual = assign( x, 1.0, { + 'dims': [] + }); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `start` ndarray', function test( t ) { + var expected; + var actual; + var start; + var x; + + x = zeros( [ 2, 3 ], { + 'dtype': 'float64' + }); + start = array( [ 1.0, 5.0 ] ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]; + + actual = assign( x, start ); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + x = zeros( [ 2, 3 ], { + 'dtype': 'float64' + }); + start = scalar2ndarray( 4.0 ); + expected = [ [ 4.0, 5.0, 6.0 ], [ 4.0, 5.0, 6.0 ] ]; + + actual = assign( x, start ); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a complex `start` value', function test( t ) { + var actual; + var v; + var x; + + x = zeros( [ 3 ], { + 'dtype': 'complex128' + }); + actual = assign( x, new Complex128( 1.0, 2.0 ) ); + + t.strictEqual( actual, x, 'returns expected value' ); + + v = actual.iget( 0 ); + t.strictEqual( real( v ), 1.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + + v = actual.iget( 1 ); + t.strictEqual( real( v ), 2.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + + v = actual.iget( 2 ); + t.strictEqual( real( v ), 3.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function casts a `start` ndarray to the output data type', function test( t ) { + var expected; + var actual; + var start; + var x; + + x = zeros( [ 2, 3 ], { + 'dtype': 'float64' + }); + start = array( new Float32Array( [ 1.0, 5.0 ] ), { + 'dtype': 'float32' + }); + expected = [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]; + + actual = assign( x, start ); + + t.strictEqual( actual, x, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function throws an error if a `start` ndarray cannot be safely cast to the output data type', function test( t ) { + var x; + + x = zeros( [ 4 ], { + 'dtype': 'float64' + }); + t.throws( badValue, TypeError, 'throws an error' ); + t.end(); + + function badValue() { + var start = array( new Complex128Array( [ 1.0, 2.0 ] ), { + 'dtype': 'complex128' + }); + assign( x, start ); + } +}); diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.js b/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.js new file mode 100644 index 000000000000..2ecc74c357c1 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.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'; + +// MODULES // + +var tape = require( 'tape' ); +var isMethod = require( '@stdlib/assert/is-method' ); +var unitspace = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unitspace, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( unitspace, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.main.js b/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.main.js new file mode 100644 index 000000000000..3e4089123b48 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/ext/unitspace/test/test.main.js @@ -0,0 +1,650 @@ +/** +* @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 ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Int32Array = require( '@stdlib/array/int32' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var unitspace = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof unitspace, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not a nonnegative integer or an array of nonnegative integers', function test( t ) { + var values; + var i; + + values = [ + '5', + -1, + 3.14, + NaN, + 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() { + unitspace( value, 1.0 ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not a nonnegative integer or an array of nonnegative integers (start=ndarray)', function test( t ) { + var values; + var i; + + values = [ + '5', + -1, + 3.14, + NaN, + 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() { + unitspace( value, scalar2ndarray( 1.0 ) ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not a nonnegative integer or an array of nonnegative integers (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + -1, + 3.14, + NaN, + 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() { + unitspace( value, 1.0, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a number, complex number, or an ndarray having a supported data type', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + scalar2ndarray( true ) + ]; + 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() { + unitspace( [ 2, 2 ], value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not a number, complex number, or an ndarray having a supported data type (options)', function test( t ) { + var values; + var i; + + values = [ + '5', + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + scalar2ndarray( true ) + ]; + 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() { + unitspace( [ 2, 2 ], value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an object', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + 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() { + unitspace( [ 2, 2 ], 1.0, value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `dtype` option', function test( t ) { + var values; + var i; + + values = [ + 'foo', + 'bool', + 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() { + unitspace( [ 2, 2 ], 1.0, { + 'dtype': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an invalid `order` option', function test( t ) { + var values; + var i; + + values = [ + 'foo', + 5, + NaN, + 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() { + unitspace( [ 2, 2 ], 1.0, { + 'order': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object of integers', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + NaN, + 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() { + unitspace( [ 2, 2 ], 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains out-of-bounds indices', function test( t ) { + var values; + var i; + + values = [ + [ 5 ], + [ -5 ], + [ 2 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + unitspace( [ 2, 2 ], 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains too many indices', function test( t ) { + var values; + var i; + + values = [ + [ 0, 1, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + unitspace( [ 2, 2 ], 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which contains duplicate indices', function test( t ) { + var values; + var i; + + values = [ + [ 0, 0 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + unitspace( [ 2, 2 ], 1.0, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if a complex `start` value cannot be safely cast to a real-valued output data type', function test( t ) { + t.throws( badValue, TypeError, 'throws an error' ); + t.end(); + + function badValue() { + unitspace( [ 4 ], new Complex128( 1.0, 2.0 ), { + 'dtype': 'float64' + }); + } +}); + +tape( 'the function returns an ndarray containing linearly spaced values incrementing by 1 from a specified value', function test( t ) { + var expected; + var actual; + + expected = [ 1.0, 2.0, 3.0 ]; + actual = unitspace( 3, 1.0 ); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + expected = [ 1.0, 2.0, 3.0 ]; + actual = unitspace( [ 3 ], 1.0 ); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + expected = [ [ 2.0, 3.0 ], [ 2.0, 3.0 ] ]; + actual = unitspace( [ 2, 2 ], 2.0 ); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a `start` ndarray', function test( t ) { + var expected; + var actual; + var start; + + start = array( [ 1.0, 5.0 ] ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]; + actual = unitspace( [ 2, 3 ], start ); + + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + start = scalar2ndarray( 4.0 ); + expected = [ [ 4.0, 5.0, 6.0 ], [ 4.0, 5.0, 6.0 ] ]; + actual = unitspace( [ 2, 3 ], start ); + + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying an output data type', function test( t ) { + var expected; + var actual; + + expected = [ 1.0, 2.0, 3.0 ]; + actual = unitspace( [ 3 ], 1.0, { + 'dtype': 'float32' + }); + + t.strictEqual( String( getDType( actual ) ), 'float32', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + expected = [ 1, 2, 3 ]; + actual = unitspace( [ 3 ], 1, { + 'dtype': 'int32' + }); + + t.strictEqual( String( getDType( actual ) ), 'int32', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying an output array order', function test( t ) { + var expected; + var actual; + + expected = [ [ 2.0, 3.0 ], [ 2.0, 3.0 ] ]; + actual = unitspace( [ 2, 2 ], 2.0, { + 'order': 'row-major' + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + expected = [ [ 2.0, 3.0 ], [ 2.0, 3.0 ] ]; + actual = unitspace( [ 2, 2 ], 2.0, { + 'order': 'column-major' + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying operation dimensions', function test( t ) { + var expected; + var actual; + + expected = [ [ 1.0, 2.0 ], [ 1.0, 2.0 ] ]; + actual = unitspace( [ 2, 2 ], 1.0, { + 'dims': [ -1 ] + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + expected = [ [ 1.0, 1.0 ], [ 2.0, 2.0 ] ]; + actual = unitspace( [ 2, 2 ], 1.0, { + 'dims': [ 0 ] + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + expected = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]; + actual = unitspace( [ 2, 2 ], 1.0, { + 'dims': [ 0, 1 ] + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + expected = [ [ 1.0, 1.0 ], [ 1.0, 1.0 ] ]; + actual = unitspace( [ 2, 2 ], 1.0, { + 'dims': [] + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a complex `start` value', function test( t ) { + var actual; + var v; + + actual = unitspace( [ 3 ], new Complex128( 1.0, 2.0 ) ); + + t.strictEqual( String( getDType( actual ) ), 'complex128', 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3 ], 'returns expected value' ); + + v = actual.iget( 0 ); + t.strictEqual( real( v ), 1.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + + v = actual.iget( 1 ); + t.strictEqual( real( v ), 2.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + + v = actual.iget( 2 ); + t.strictEqual( real( v ), 3.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying array index modes and submodes', function test( t ) { + var expected; + var actual; + var opts; + + opts = { + 'mode': 'clamp', + 'submode': [ 'wrap' ] + }; + actual = unitspace( [ 2, 2, 3 ], 1.0, opts ); + expected = [ + [ + [ 1.0, 2.0, 3.0 ], + [ 1.0, 2.0, 3.0 ] + ], + [ + [ 1.0, 2.0, 3.0 ], + [ 1.0, 2.0, 3.0 ] + ] + ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // Clamped: + t.strictEqual( actual.iget( actual.length+10 ), 3.0, 'returns expected value' ); + actual.iset( actual.length+10, 10.0 ); + t.strictEqual( actual.iget( actual.length+10 ), 10.0, 'returns expected value' ); + + // Wrapped: + t.strictEqual( actual.get( 2, 2, 3 ), 1.0, 'returns expected value' ); + actual.set( 2, 2, 3, 30.0 ); + t.strictEqual( actual.get( 0, 0, 0 ), 30.0, 'returns expected value' ); + t.strictEqual( actual.get( 2, 2, 3 ), 30.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function infers the output array order from a `start` ndarray (row-major)', function test( t ) { + var actual; + var start; + + start = array( [ 1.0, 5.0 ], { + 'order': 'row-major' + }); + actual = unitspace( [ 2, 3 ], start ); + + t.strictEqual( getOrder( actual ), 'row-major', 'returns expected value' ); + t.end(); +}); + +tape( 'the function infers the output array order from a `start` ndarray (column-major)', function test( t ) { + var actual; + var start; + + start = array( [ 1.0, 5.0 ], { + 'order': 'column-major' + }); + actual = unitspace( [ 2, 3 ], start ); + + t.strictEqual( getOrder( actual ), 'column-major', 'returns expected value' ); + t.end(); +}); + +tape( 'the function casts a `start` ndarray to the output data type', function test( t ) { + var expected; + var actual; + var start; + + start = array( new Float32Array( [ 1.0, 5.0 ] ), { + 'dtype': 'float32' + }); + expected = [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]; + actual = unitspace( [ 2, 3 ], start, { + 'dtype': 'float64' + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + start = array( new Int32Array( [ 1, 5 ] ), { + 'dtype': 'int32' + }); + expected = [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]; + actual = unitspace( [ 2, 3 ], start, { + 'dtype': 'float64' + }); + + t.strictEqual( String( getDType( actual ) ), 'float64', 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function throws an error if a `start` ndarray cannot be safely cast to the output data type', function test( t ) { + t.throws( badValue, TypeError, 'throws an error' ); + t.end(); + + function badValue() { + var start = array( new Complex128Array( [ 1.0, 2.0 ] ), { + 'dtype': 'complex128' + }); + unitspace( [ 4 ], start, { + 'dtype': 'float64' + }); + } +});