diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/README.md new file mode 100644 index 000000000000..c4957f9858c3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/README.md @@ -0,0 +1,134 @@ + + +# prependSingletonDimensions + +> Return a read-only view of an input [ndarray][@stdlib/ndarray/ctor] with a specified number of prepended singleton dimensions. + + + +
+ +
+ + + + + +
+ +## Usage + + + +```javascript +var prependSingletonDimensions = require( '@stdlib/ndarray/prepend-singleton-dimensions' ); +``` + +#### prependSingletonDimensions( x, n ) + +Returns a read-only view of an input [ndarray][@stdlib/ndarray/ctor] with a specified number of prepended singleton dimensions. + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create a 2x2 ndarray: +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +// returns [ [ 1, 2 ], [ 3, 4 ] ] + +// Prepend singleton dimensions: +var y = prependSingletonDimensions( x, 3 ); +// returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **n**: number of singleton dimensions to prepend. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var prependSingletonDimensions = require( '@stdlib/ndarray/prepend-singleton-dimensions' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = prependSingletonDimensions( x, 3 ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..6caaa01f8f40 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-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 prependSingletonDimensions = require( './../lib' ); // eslint-disable-line id-length + + +// 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 = prependSingletonDimensions( 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 = prependSingletonDimensions( 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::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 = prependSingletonDimensions( 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(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/repl.txt new file mode 100644 index 000000000000..7c56cf18eae1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/repl.txt @@ -0,0 +1,28 @@ + +{{alias}}( x, n ) + Returns a read-only view of an input ndarray with a specified number of + prepended singleton dimensions. + + Parameters + ---------- + x: ndarray + Input array. + + n: integer + Number of singleton dimensions to prepend. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x, 3 ) + [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..4743e82b5dd6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @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'; + +/** +* Returns a read-only view of an input ndarray with a specified number of prepended singleton dimensions. +* +* @param x - input array +* @param n - number of singleton dimensions to prepend +* @returns output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = prependSingletonDimensions( x, 3 ); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +*/ +declare function prependSingletonDimensions = typedndarray>( x: U, n: number ): U; + + +// EXPORTS // + +export = prependSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..c7558f48b836 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/docs/types/test.ts @@ -0,0 +1,72 @@ +/* +* @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 prependSingletonDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + prependSingletonDimensions( x, 3 ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + prependSingletonDimensions( '5', 3 ); // $ExpectError + prependSingletonDimensions( 5, 3 ); // $ExpectError + prependSingletonDimensions( true, 3 ); // $ExpectError + prependSingletonDimensions( false, 3 ); // $ExpectError + prependSingletonDimensions( null, 3 ); // $ExpectError + prependSingletonDimensions( {}, 3 ); // $ExpectError + prependSingletonDimensions( [ '5' ], 3 ); // $ExpectError + prependSingletonDimensions( ( x: number ): number => x, 3 ); // $ExpectError +} + +// The compiler throws an error if the function is not provided a second argument which is a number... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + prependSingletonDimensions( x, '5' ); // $ExpectError + prependSingletonDimensions( x, true ); // $ExpectError + prependSingletonDimensions( x, false ); // $ExpectError + prependSingletonDimensions( x, null ); // $ExpectError + prependSingletonDimensions( x, {} ); // $ExpectError + prependSingletonDimensions( x, [ '5' ] ); // $ExpectError + prependSingletonDimensions( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + prependSingletonDimensions(); // $ExpectError + prependSingletonDimensions( x ); // $ExpectError + prependSingletonDimensions( x, 3, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/examples/index.js new file mode 100644 index 000000000000..a417f1066385 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-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 prependSingletonDimensions = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = prependSingletonDimensions( x, 1 ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/lib/index.js new file mode 100644 index 000000000000..700a3118ca3e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/lib/index.js @@ -0,0 +1,44 @@ +/** +* @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 with a specified number of prepended singleton dimensions. +* +* @module @stdlib/ndarray/prepend-singleton-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var prependSingletonDimensions = require( '@stdlib/ndarray/prepend-singleton-dimensions' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = prependSingletonDimensions( x, 3 ); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/lib/main.js new file mode 100644 index 000000000000..d8c806d08cd5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/lib/main.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'; + +// MODULES // + +var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var base = require( '@stdlib/ndarray/base/prepend-singleton-dimensions' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of an input ndarray with a specified number of prepended singleton dimensions. +* +* @param {ndarray} x - input array +* @param {NonNegativeInteger} n - number of singleton dimensions to prepend +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} second argument must be a nonnegative integer +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = prependSingletonDimensions( x, 3 ); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +*/ +function prependSingletonDimensions( x, n ) { // eslint-disable-line id-length + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isNonNegativeInteger( n ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) ); + } + return base( x, n, false ); +} + + +// EXPORTS // + +module.exports = prependSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/package.json new file mode 100644 index 000000000000..c39bdd739f93 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/prepend-singleton-dimensions", + "version": "0.0.0", + "description": "Return a read-only view of an input ndarray with a specified number of prepended singleton 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", + "prepend", + "add", + "reshape", + "dimensions", + "multidimensional" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/test/test.js new file mode 100644 index 000000000000..ae1def3d1e42 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/prepend-singleton-dimensions/test/test.js @@ -0,0 +1,135 @@ +/** +* @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 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 prependSingletonDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof prependSingletonDimensions, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + prependSingletonDimensions( value, 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a second 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() { + prependSingletonDimensions( x, value ); + }; + } +}); + +tape( 'the function prepends singleton dimensions', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = prependSingletonDimensions( x, 3 ); + + expected = [ 1, 1, 1, 2, 2 ]; + + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = prependSingletonDimensions( x, 2 ); + + expected = [ 1, 1, 2, 2 ]; + + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = prependSingletonDimensions( x, 1 ); + + expected = [ 1, 2, 2 ]; + + t.deepEqual( getShape( y ), expected, 'returns expected value' ); + t.strictEqual( getData( y ), getData( x ), 'returns expected value' ); + t.notEqual( y, x, 'returns expected value' ); + t.strictEqual( isReadOnly( y ), true, 'returns expected value' ); + + t.end(); +});