diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md new file mode 100644 index 000000000000..b011039b06c3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md @@ -0,0 +1,138 @@ + + +# spreadDimensions + +> Return a read-only view of an input [ndarray][@stdlib/ndarray/ctor] where the dimensions of the input [ndarray][@stdlib/ndarray/ctor] are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var spreadDimensions = require( '@stdlib/ndarray/spread-dimensions' ); +``` + +#### spreadDimensions( ndims, x, dims ) + +Returns a read-only view of an input [ndarray][@stdlib/ndarray/ctor] where the dimensions of the input [ndarray][@stdlib/ndarray/ctor] are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions. + +```javascript +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); + +// Create a 2x2 ndarray: +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +// returns [ [ 1, 2 ], [ 3, 4 ] ] + +// Prepend a singleton dimension: +var y = spreadDimensions( 3, x, [ 1, 2 ] ); +// returns [ [ [ 1, 2 ], [ 3, 4 ] ] ] + +// Append a singleton dimension: +y = spreadDimensions( 3, x, [ 0, 1 ] ); +// returns [ [ [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ] ] ] + +// Insert a singleton dimension: +y = spreadDimensions( 3, x, [ 0, 2 ] ); +// returns [ [ [ 1, 2 ] ], [ [ 3, 4 ] ] ] +``` + +The function accepts the following arguments: + +- **ndims**: number of dimensions in the output [ndarray][@stdlib/ndarray/ctor]. Must be greater than or equal to the number of dimensions in the input [ndarray][@stdlib/ndarray/ctor]. +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **dims**: dimension indices specifying where to place the dimensions of the input [ndarray][@stdlib/ndarray/ctor]. Must resolve to normalized indices arranged in ascending order. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var spreadDimensions = require( '@stdlib/ndarray/spread-dimensions' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = spreadDimensions( 5, x, [ 0, 1, 2 ] ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..606ef82ef4c3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js @@ -0,0 +1,127 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var spreadDimensions = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::1d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2 ], { 'dtype': 'float64' } ), + empty( [ 2 ], { 'dtype': 'float32' } ), + empty( [ 2 ], { 'dtype': 'int32' } ), + empty( [ 2 ], { 'dtype': 'complex128' } ), + empty( [ 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = spreadDimensions( 3, values[ i%values.length ], [ 1 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::2d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = spreadDimensions( 4, values[ i%values.length ], [ 1, 2 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = spreadDimensions( 5, values[ i%values.length ], [ 1, 2, 3 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/repl.txt new file mode 100644 index 000000000000..d81ea97f318f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( ndims, x, dims ) + Returns a read-only view of an input ndarray where the dimensions of the + input ndarray are expanded to a specified dimensionality by spreading + dimensions to specified dimension indices and inserting dimensions of size + one for the remaining dimensions. + + Parameters + ---------- + ndims: integer + Number of dimensions in the output ndarray. Must be greater than or + equal to the number of dimensions in the input ndarray. + + x: ndarray + Input ndarray. + + dims: ArrayLikeObject + Dimension indices at which to spread array dimensions. Must resolve to + normalized indices arranged in ascending order. If provided an integer + less than zero, the dimension index is resolved relative to the last + dimension, with the last dimension corresponding to the value `-1`. + + Returns + ------- + out: ndarray + A read-only view of an input ndarray where the dimensions of the input + ndarray are expanded to a specified dimensionality. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( 3, x, [ 0, 1 ] ) + [ [ [ 1 ], [ 2 ] ], [ [ 3 ], [ 4 ] ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..d06655944cf7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/index.d.ts @@ -0,0 +1,54 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { typedndarray } from '@stdlib/types/ndarray'; +import { Collection } from '@stdlib/types/array'; + +/** +* Returns a read-only view of an input ndarray where the dimensions of the input ndarray are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions. +* +* @param ndims - number of dimensions in the output array +* @param x - input ndarray +* @param dims - dimension indices +* @returns output array +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +* var shape = [ 2, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = spreadDimensions( 5, x, [ 1, 3 ] ); +* // returns [ [ [ [ [ 1.0 ], [ 2.0 ] ] ], [ [ [ 3.0 ], [ 4.0 ] ] ] ] ] +*/ +declare function spreadDimensions = typedndarray>( ndims: number, x: U, dims: Collection ): U; + + +// EXPORTS // + +export = spreadDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..c9f92d40366c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/test.ts @@ -0,0 +1,81 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import empty = require( '@stdlib/ndarray/base/empty' ); +import spreadDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + spreadDimensions( 3, empty( 'float64', sh, order ), [ 0, 1 ] ); // $ExpectType float64ndarray + spreadDimensions( 5, empty( 'complex64', sh, order ), [ 1, 3 ] ); // $ExpectType complex64ndarray + spreadDimensions( 4, empty( 'int32', sh, order ), [ -2, -1 ] ); // $ExpectType int32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + spreadDimensions( '5', x, [ 0, 1 ] ); // $ExpectError + spreadDimensions( false, x, [ 0, 1 ] ); // $ExpectError + spreadDimensions( true, x, [ 0, 1 ] ); // $ExpectError + spreadDimensions( null, x, [ 0, 1 ] ); // $ExpectError + spreadDimensions( [], x, [ 0, 1 ] ); // $ExpectError + spreadDimensions( {}, x, [ 0, 1 ] ); // $ExpectError + spreadDimensions( ( x: number ): number => x, x, [ 0, 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an ndarray... +{ + spreadDimensions( 5, '10', [ 0, 1 ] ); // $ExpectError + spreadDimensions( 5, 10, [ 0, 1 ] ); // $ExpectError + spreadDimensions( 5, false, [ 0, 1 ] ); // $ExpectError + spreadDimensions( 5, true, [ 0, 1 ] ); // $ExpectError + spreadDimensions( 5, null, [ 0, 1 ] ); // $ExpectError + spreadDimensions( 5, [], [ 0, 1 ] ); // $ExpectError + spreadDimensions( 5, {}, [ 0, 1 ] ); // $ExpectError + spreadDimensions( 5, ( x: number ): number => x, [ 0, 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a collection of numbers... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + spreadDimensions( 5, x, '1' ); // $ExpectError + spreadDimensions( 5, x, 1 ); // $ExpectError + spreadDimensions( 5, x, false ); // $ExpectError + spreadDimensions( 5, x, true ); // $ExpectError + spreadDimensions( 5, x, null ); // $ExpectError + spreadDimensions( 5, x, {} ); // $ExpectError + spreadDimensions( 5, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + spreadDimensions(); // $ExpectError + spreadDimensions( 5 ); // $ExpectError + spreadDimensions( 5, x ); // $ExpectError + spreadDimensions( 5, x, [ 0, 1 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js new file mode 100644 index 000000000000..fcaac3815ae9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js @@ -0,0 +1,29 @@ +/** +* @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 uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var spreadDimensions = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = spreadDimensions( 5, x, [ 0, 1, 2 ] ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js new file mode 100644 index 000000000000..ffbfca9a1735 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a read-only view of an input ndarray where the dimensions of the input ndarray are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions. +* +* @module @stdlib/ndarray/spread-dimensions +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var spreadDimensions = require( '@stdlib/ndarray/spread-dimensions' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +* var shape = [ 2, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = spreadDimensions( 5, x, [ 1, 3 ] ); +* // returns [ [ [ [ [ 1.0 ], [ 2.0 ] ] ], [ [ [ 3.0 ], [ 4.0 ] ] ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js new file mode 100644 index 000000000000..4b80889d01ad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js @@ -0,0 +1,74 @@ +/** +* @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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var base = require( '@stdlib/ndarray/base/spread-dimensions' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of an input ndarray where the dimensions of the input ndarray are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions. +* +* @param {NonNegativeInteger} ndims - number of dimensions in the output array +* @param {ndarray} x - input array +* @param {IntegerArray} dims - dimension indices at which to spread array dimensions +* @throws {TypeError} first argument must be a nonnegative integer +* @throws {TypeError} second argument must be an ndarray +* @throws {TypeError} third argument must be an array of integers +* @returns {ndarray} ndarray view +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0 ]; +* var shape = [ 2, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = spreadDimensions( 5, x, [ 1, 3 ] ); +* // returns [ [ [ [ [ 1.0 ], [ 2.0 ] ] ], [ [ [ 3.0 ], [ 4.0 ] ] ] ] ] +*/ +function spreadDimensions( ndims, x, dims ) { + if ( !isNonNegativeInteger( ndims ) ) { + throw new TypeError( format( 'invalid argument. First argument must be a nonnegative integer. Value: `%s`.', ndims ) ); + } + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isIntegerArray( dims ) ) { + throw new TypeError( format( 'invalid argument. Third argument must be an array of integers. Value: `%s`.', dims ) ); + } + return base( ndims, x, dims, false ); +} + + +// EXPORTS // + +module.exports = spreadDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/spread-dimensions/package.json new file mode 100644 index 000000000000..6f78f1fe3cc5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/spread-dimensions", + "version": "0.0.0", + "description": "Return a read-only view of an input ndarray where the dimensions of the input ndarray are expanded to a specified dimensionality by spreading dimensions to specified dimension indices and inserting dimensions of size one for the remaining 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", + "stdtypes", + "types", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "expand", + "spread", + "reshape", + "dimensions", + "multidimensional" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js new file mode 100644 index 000000000000..906b2be98b1d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js @@ -0,0 +1,269 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var array = require( '@stdlib/ndarray/array' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var spreadDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof spreadDimensions, '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', function test( t ) { + var values; + var x; + var i; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + values = [ + '5', + 5.5, + -1, + 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() { + spreadDimensions( value, x, [ 0, 1 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is less than the number of dimensions of the input ndarray', function test( t ) { + var values; + var x; + var i; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + values = [ + 0, + 1 + ]; + + 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() { + spreadDimensions( value, x, [ 0, 1 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + spreadDimensions( 5, value, [ 0, 1 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an array of integers', function test( t ) { + var values; + var x; + var i; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + function noop() {}, + [ '1', '2' ], + [ 1.5, 2.5 ] + ]; + + 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() { + spreadDimensions( 5, x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a third argument which is not an array of integers which resolve to dimension indices sorted in ascending order', function test( t ) { + var values; + var x; + var i; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + + values = [ + [ 3, 1 ], + [ -1, -3 ] + ]; + + 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() { + spreadDimensions( 5, x, value ); + }; + } +}); + +tape( 'the function returns a read-only ndarray view', function test( t ) { + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = spreadDimensions( 3, x, [ 0, 1 ] ); + + t.strictEqual( isndarrayLike( y ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function prepends dimensions', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = spreadDimensions( 3, x, [ 1, 2 ] ); + + expected = [ 1, 2, 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + t.strictEqual( y.get( 0, 0, 0 ), 1, 'returns expected value' ); + t.strictEqual( y.get( 0, 0, 1 ), 2, 'returns expected value' ); + t.strictEqual( y.get( 0, 1, 0 ), 3, 'returns expected value' ); + t.strictEqual( y.get( 0, 1, 1 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function appends dimensions', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = spreadDimensions( 3, x, [ 0, 1 ] ); + + expected = [ 2, 2, 1 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + t.strictEqual( y.get( 0, 0, 0 ), 1, 'returns expected value' ); + t.strictEqual( y.get( 0, 1, 0 ), 2, 'returns expected value' ); + t.strictEqual( y.get( 1, 0, 0 ), 3, 'returns expected value' ); + t.strictEqual( y.get( 1, 1, 0 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function inserts dimensions', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = spreadDimensions( 3, x, [ 0, 2 ] ); + + expected = [ 2, 1, 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + t.strictEqual( y.get( 0, 0, 0 ), 1, 'returns expected value' ); + t.strictEqual( y.get( 0, 0, 1 ), 2, 'returns expected value' ); + t.strictEqual( y.get( 1, 0, 0 ), 3, 'returns expected value' ); + t.strictEqual( y.get( 1, 0, 1 ), 4, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function expands dimensions (negative index)', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = spreadDimensions( 3, x, [ -2, -1 ] ); + + expected = [ 1, 2, 2 ]; + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + + t.strictEqual( y.get( 0, 0, 0 ), 1, 'returns expected value' ); + t.strictEqual( y.get( 0, 0, 1 ), 2, 'returns expected value' ); + t.strictEqual( y.get( 0, 1, 0 ), 3, 'returns expected value' ); + t.strictEqual( y.get( 0, 1, 1 ), 4, 'returns expected value' ); + + t.end(); +});