diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/README.md new file mode 100644 index 000000000000..745c6893ec62 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/README.md @@ -0,0 +1,131 @@ + + +# removeSingletonDimensions + +> Return a read-only view of an input [ndarray][@stdlib/ndarray/ctor] with singleton dimensions removed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var removeSingletonDimensions = require( '@stdlib/ndarray/remove-singleton-dimensions' ); +``` + +#### removeSingletonDimensions( x ) + +Returns a read-only view of an input [ndarray][@stdlib/ndarray/ctor] with singleton dimensions removed. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +// Create a 1x2x2 ndarray: +var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ] ] ); +// returns [ [ [ 1, 2 ], [ 3, 4 ] ] ] + +// Remove singleton dimensions: +var y = removeSingletonDimensions( x ); +// returns [ [ 1, 2 ], [ 3, 4 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + + + +
+ +## Notes + +- The function always returns a new ndarray instance even if the input ndarray does not contain any singleton dimensions. + +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var removeSingletonDimensions = require( '@stdlib/ndarray/remove-singleton-dimensions' ); + +var x = uniform( [ 1, 1, 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = removeSingletonDimensions( x ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..f37ffe56630f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/benchmark/benchmark.js @@ -0,0 +1,159 @@ +/** +* @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 removeSingletonDimensions = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::no_singletons,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 = removeSingletonDimensions( values[ i%values.length ] ); + 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::no_singletons,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 = removeSingletonDimensions( values[ i%values.length ] ); + 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::singletons,2d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 1, 2 ], { 'dtype': 'float64' } ), + empty( [ 1, 2 ], { 'dtype': 'float32' } ), + empty( [ 1, 2 ], { 'dtype': 'int32' } ), + empty( [ 1, 2 ], { 'dtype': 'complex128' } ), + empty( [ 1, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = removeSingletonDimensions( values[ i%values.length ] ); + 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::singletons,3d', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 1, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 1, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 1, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 1, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 1, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = removeSingletonDimensions( values[ i%values.length ] ); + 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/remove-singleton-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/repl.txt new file mode 100644 index 000000000000..4444613df3fc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/repl.txt @@ -0,0 +1,29 @@ + +{{alias}}( x ) + Returns a read-only view of an input ndarray with singleton dimensions + removed. + + The function always returns a new ndarray instance even if the input ndarray + does not contain any singleton dimensions. + + Parameters + ---------- + x: ndarray + Input array. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var opts = { 'ndmin': 5 }; + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ], opts ) + [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] + > var y = {{alias}}( x ) + [ [ 1, 2 ], [ 3, 4 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..e8fa1fb222fe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/types/index.d.ts @@ -0,0 +1,47 @@ +/* +* @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 singleton dimensions removed. +* +* @param x - input array +* @returns output array +* +* @example +* var array = require( `@stdlib/ndarray/array` ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { +* 'ndmin': 5 +* }); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +* +* var y = removeSingletonDimensions( x ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +*/ +declare function removeSingletonDimensions = typedndarray>( x: U ): U; + + +// EXPORTS // + +export = removeSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..5356e4bc6eea --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @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 removeSingletonDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 1, 1, 2, 2 ], { + 'dtype': 'float64' + }); + + removeSingletonDimensions( x ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is an ndarray... +{ + removeSingletonDimensions( '5' ); // $ExpectError + removeSingletonDimensions( 5 ); // $ExpectError + removeSingletonDimensions( true ); // $ExpectError + removeSingletonDimensions( false ); // $ExpectError + removeSingletonDimensions( null ); // $ExpectError + removeSingletonDimensions( {} ); // $ExpectError + removeSingletonDimensions( [ '5' ] ); // $ExpectError + removeSingletonDimensions( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 1, 1, 2, 2 ], { + 'dtype': 'float64' + }); + + removeSingletonDimensions(); // $ExpectError + removeSingletonDimensions( x, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/examples/index.js new file mode 100644 index 000000000000..4d9daac8dfd6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-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 removeSingletonDimensions = require( './../lib' ); + +var x = uniform( [ 1, 1, 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = removeSingletonDimensions( x ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/lib/index.js new file mode 100644 index 000000000000..d6efae763959 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/lib/index.js @@ -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. +*/ + +'use strict'; + +/** +* Return a read-only view of an input ndarray with singleton dimensions removed. +* +* @module @stdlib/ndarray/remove-singleton-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var removeSingletonDimensions = require( '@stdlib/ndarray/remove-singleton-dimensions' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { +* 'ndmin': 5 +* }); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +* +* var y = removeSingletonDimensions( x ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/lib/main.js new file mode 100644 index 000000000000..ec5992ee6e62 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/lib/main.js @@ -0,0 +1,58 @@ +/** +* @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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var base = require( '@stdlib/ndarray/base/remove-singleton-dimensions' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of an input ndarray with singleton dimensions removed. +* +* @param {ndarray} x - input array +* @throws {TypeError} first argument must be an ndarray +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ], { +* 'ndmin': 5 +* }); +* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ] ] ] ] ] +* +* var y = removeSingletonDimensions( x ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +*/ +function removeSingletonDimensions( x ) { + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + return base( x, false ); +} + + +// EXPORTS // + +module.exports = removeSingletonDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/package.json new file mode 100644 index 000000000000..a61f4097ad6e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ndarray/remove-singleton-dimensions", + "version": "0.0.0", + "description": "Return a read-only view of an input ndarray with singleton dimensions removed.", + "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", + "remove", + "squeeze", + "reshape", + "singleton", + "dimensions", + "multidimensional" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/test/test.js new file mode 100644 index 000000000000..968c90425c33 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/remove-singleton-dimensions/test/test.js @@ -0,0 +1,117 @@ +/** +* @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 removeSingletonDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof removeSingletonDimensions, '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() { + removeSingletonDimensions( value ); + }; + } +}); + +tape( 'the function removes singleton dimensions', function test( t ) { + var expected; + var x; + var y; + + x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); + y = removeSingletonDimensions( x ); + + expected = [ 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 ] ], { + 'ndmin': 5 + }); + y = removeSingletonDimensions( x ); + + expected = [ 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 = removeSingletonDimensions( x ); + + expected = [ 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 ] ], { + 'ndmin': 3 + }); + y = removeSingletonDimensions( x ); + + expected = [ 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(); +});