From fa370d330d8c0dc037b400b98ae493c631f6ab31 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 29 Dec 2025 10:50:37 +0500 Subject: [PATCH 1/3] feat: add ndarray/spread-dimensions --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/spread-dimensions/README.md | 130 +++++++++++ .../spread-dimensions/benchmark/benchmark.js | 126 ++++++++++ .../ndarray/spread-dimensions/docs/repl.txt | 38 +++ .../spread-dimensions/docs/types/index.d.ts | 54 +++++ .../spread-dimensions/docs/types/test.ts | 81 +++++++ .../spread-dimensions/examples/index.js | 29 +++ .../ndarray/spread-dimensions/lib/index.js | 50 ++++ .../ndarray/spread-dimensions/lib/main.js | 74 ++++++ .../ndarray/spread-dimensions/package.json | 66 ++++++ .../ndarray/spread-dimensions/test/test.js | 221 ++++++++++++++++++ 10 files changed, 869 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js 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..84a394964492 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md @@ -0,0 +1,130 @@ + + +# 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 ] ] ] +``` + +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, [ 1, 3 ] ); +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..d7b3829b88ef --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 pkg = require( './../package.json' ).name; +var spreadDimensions = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d', 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( pkg+'::2d', 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( 5, values[ i%values.length ], [ 1, 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(); +}); + +bench( pkg+'::3d', 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( 6, values[ i%values.length ], [ 0, 2, 4 ] ); + 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..17bcd24814c1 --- /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) 2025 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 { ndarray } 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( ndims: number, x: T, dims: Collection ): T; + + +// 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..faaa484da037 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/test.ts @@ -0,0 +1,81 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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..760a80a38eed --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js @@ -0,0 +1,29 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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, [ 1, 3 ] ); +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..bb18c2eea3e7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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..186712cf87e1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js @@ -0,0 +1,74 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isInteger = require( '@stdlib/assert/is-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 ( !isInteger( ndims ) || ndims < 0 ) { + 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..fffe42e9b650 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js @@ -0,0 +1,221 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 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 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 axis)', 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(); +}); From 9bb556cdef7ca7fcdf6348bc7a003412ec8897ec Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 29 Dec 2025 11:02:31 +0500 Subject: [PATCH 2/3] fix: examples --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md | 2 +- .../@stdlib/ndarray/spread-dimensions/examples/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md index 84a394964492..f2d403c0cf60 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md @@ -91,7 +91,7 @@ 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, [ 1, 3 ] ); +var y = spreadDimensions( 5, x, [ 0, 1, 2 ] ); console.log( ndarray2array( y ) ); ``` diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js index 760a80a38eed..d88dfee345f0 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js @@ -25,5 +25,5 @@ var spreadDimensions = require( './../lib' ); var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); console.log( ndarray2array( x ) ); -var y = spreadDimensions( 5, x, [ 1, 3 ] ); +var y = spreadDimensions( 5, x, [ 0, 1, 2 ] ); console.log( ndarray2array( y ) ); From 5b5e730e13d961c16d343b62e7813b20f926d9bb Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 8 Jan 2026 20:23:39 -0800 Subject: [PATCH 3/3] chore: clean-up --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/spread-dimensions/README.md | 20 ++++--- .../spread-dimensions/benchmark/benchmark.js | 13 ++--- .../spread-dimensions/docs/types/index.d.ts | 6 +-- .../spread-dimensions/docs/types/test.ts | 2 +- .../spread-dimensions/examples/index.js | 2 +- .../ndarray/spread-dimensions/lib/index.js | 2 +- .../ndarray/spread-dimensions/lib/main.js | 6 +-- .../ndarray/spread-dimensions/test/test.js | 52 ++++++++++++++++++- 8 files changed, 80 insertions(+), 23 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md index f2d403c0cf60..b011039b06c3 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/README.md @@ -2,7 +2,7 @@ @license Apache-2.0 -Copyright (c) 2025 The Stdlib Authors. +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. @@ -20,7 +20,7 @@ limitations under the License. # 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. +> 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. @@ -42,7 +42,7 @@ 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. +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' ); @@ -55,13 +55,21 @@ var x = array( [ [ 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. +- **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. diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js index d7b3829b88ef..606ef82ef4c3 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/benchmark/benchmark.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. @@ -23,13 +23,14 @@ 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( pkg+'::1d', function benchmark( b ) { +bench( format( '%s::1d', pkg ), function benchmark( b ) { var values; var v; var i; @@ -61,7 +62,7 @@ bench( pkg+'::1d', function benchmark( b ) { b.end(); }); -bench( pkg+'::2d', function benchmark( b ) { +bench( format( '%s::2d', pkg ), function benchmark( b ) { var values; var v; var i; @@ -80,7 +81,7 @@ bench( pkg+'::2d', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = spreadDimensions( 5, values[ i%values.length ], [ 1, 3 ] ); + v = spreadDimensions( 4, values[ i%values.length ], [ 1, 2 ] ); if ( typeof v !== 'object' ) { b.fail( 'should return an object' ); } @@ -93,7 +94,7 @@ bench( pkg+'::2d', function benchmark( b ) { b.end(); }); -bench( pkg+'::3d', function benchmark( b ) { +bench( format( '%s::3d', pkg ), function benchmark( b ) { var values; var v; var i; @@ -112,7 +113,7 @@ bench( pkg+'::3d', function benchmark( b ) { b.tic(); for ( i = 0; i < b.iterations; i++ ) { - v = spreadDimensions( 6, values[ i%values.length ], [ 0, 2, 4 ] ); + v = spreadDimensions( 5, values[ i%values.length ], [ 1, 2, 3 ] ); if ( typeof v !== 'object' ) { b.fail( 'should return an object' ); } 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 index 17bcd24814c1..d06655944cf7 100644 --- 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 @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. @@ -20,7 +20,7 @@ /// -import { ndarray } from '@stdlib/types/ndarray'; +import { typedndarray } from '@stdlib/types/ndarray'; import { Collection } from '@stdlib/types/array'; /** @@ -46,7 +46,7 @@ import { Collection } from '@stdlib/types/array'; * var y = spreadDimensions( 5, x, [ 1, 3 ] ); * // returns [ [ [ [ [ 1.0 ], [ 2.0 ] ] ], [ [ [ 3.0 ], [ 4.0 ] ] ] ] ] */ -declare function spreadDimensions( ndims: number, x: T, dims: Collection ): T; +declare function spreadDimensions = typedndarray>( ndims: number, x: U, dims: Collection ): U; // EXPORTS // 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 index faaa484da037..c9f92d40366c 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/docs/types/test.ts @@ -1,7 +1,7 @@ /* * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js index d88dfee345f0..fcaac3815ae9 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/examples/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js index bb18c2eea3e7..ffbfca9a1735 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/index.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js index 186712cf87e1..4b80889d01ad 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/lib/main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. @@ -20,7 +20,7 @@ // MODULES // -var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +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' ); @@ -56,7 +56,7 @@ var format = require( '@stdlib/string/format' ); * // returns [ [ [ [ [ 1.0 ], [ 2.0 ] ] ], [ [ [ 3.0 ], [ 4.0 ] ] ] ] ] */ function spreadDimensions( ndims, x, dims ) { - if ( !isInteger( ndims ) || ndims < 0 ) { + if ( !isNonNegativeInteger( ndims ) ) { throw new TypeError( format( 'invalid argument. First argument must be a nonnegative integer. Value: `%s`.', ndims ) ); } if ( !isndarrayLike( x ) ) { diff --git a/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js index fffe42e9b650..906b2be98b1d 100644 --- a/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/spread-dimensions/test/test.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2025 The Stdlib Authors. +* 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. @@ -69,6 +69,30 @@ tape( 'the function throws an error if provided a first argument which is not a } }); +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; @@ -129,6 +153,30 @@ tape( 'the function throws an error if provided a third argument which is not an } }); +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; @@ -201,7 +249,7 @@ tape( 'the function inserts dimensions', function test( t ) { t.end(); }); -tape( 'the function expands dimensions (negative axis)', function test( t ) { +tape( 'the function expands dimensions (negative index)', function test( t ) { var expected; var x; var y;