From 21e72a62c218e9d9dc100504a9baf8a7fb5a26ea Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 18 Mar 2026 02:27:17 +0500 Subject: [PATCH] feat: add ndarray/base/reduce-shape --- .../ndarray/base/reduced-shape/README.md | 132 ++++++++++++++++ .../base/reduced-shape/benchmark/benchmark.js | 63 ++++++++ .../ndarray/base/reduced-shape/docs/repl.txt | 25 +++ .../base/reduced-shape/docs/types/index.d.ts | 47 ++++++ .../base/reduced-shape/docs/types/test.ts | 60 +++++++ .../base/reduced-shape/examples/index.js | 38 +++++ .../ndarray/base/reduced-shape/lib/index.js | 44 ++++++ .../ndarray/base/reduced-shape/lib/main.js | 60 +++++++ .../ndarray/base/reduced-shape/package.json | 71 +++++++++ .../ndarray/base/reduced-shape/test/test.js | 146 ++++++++++++++++++ 10 files changed, 686 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/reduced-shape/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/README.md b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/README.md new file mode 100644 index 000000000000..13628a3acd1a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/README.md @@ -0,0 +1,132 @@ + + +# reducedShape + +> Return a reduced shape of an [ndarray][@stdlib/ndarray/base/ctor]. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var reducedShape = require( '@stdlib/ndarray/base/reduced-shape' ); +``` + +#### reducedShape( x, dims ) + +Returns a reduced shape of an [ndarray][@stdlib/ndarray/base/ctor]. + +```javascript +var zeros = require( '@stdlib/ndarray/zeros' ); + +var x = zeros( [ 3, 2, 3 ] ); +// returns + +var sh = reducedShape( x, [ 0, 1 ] ); +// returns [ 3 ] +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + + + +```javascript +var zeros = require( '@stdlib/ndarray/zeros' ); +var reducedShape = require( '@stdlib/ndarray/base/reduced-shape' ); + +// Create an array: +var x = zeros( [ 1, 2, 3, 2 ] ); +// returns + +var sh = reducedShape( x, [ 0, 1 ] ); +console.log( sh ); +// => [ 3, 2 ] + +sh = reducedShape( x, [ 0, 3 ] ); +console.log( sh ); +// => [ 2, 3 ] + +sh = reducedShape( x, [ 1, 3 ] ); +console.log( sh ); +// => [ 1, 3 ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/benchmark/benchmark.js new file mode 100644 index 000000000000..cc35ed9f0a0a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/benchmark/benchmark.js @@ -0,0 +1,63 @@ +/** +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +var isCollection = require( '@stdlib/assert/is-collection' ); +var pkg = require( './../package.json' ).name; +var reducedShape = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var dims; + var out; + var i; + + values = [ + zeros( [ 10, 10, 10, 1 ] ), + zeros( [ 5, 5, 5, 1, 1 ] ), + zeros( [ 3, 4, 5 ] ) + ]; + + dims = [ + [ 0, 1 ], + [ 0, 2 ], + [ 1 ] + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reducedShape( values[ i%values.length ], dims[ i%dims.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isCollection( out ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/repl.txt new file mode 100644 index 000000000000..9473aae720f4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/repl.txt @@ -0,0 +1,25 @@ + +{{alias}}( x, dims ) + Returns a reduced shape of a provided ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + dims: Array + List of dimensions to reduce. + + Returns + ------- + out: Array + Reduced shape. + + Examples + -------- + > var out = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 1, 2, 3, 2 ] ), [ 0, 1 ] ) + [ 3, 2 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/types/index.d.ts new file mode 100644 index 000000000000..2743cffae055 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/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 { ndarray, Shape } from '@stdlib/types/ndarray'; +import { Collection } from '@stdlib/types/array'; + +/** +* Returns a reduced shape of a provided ndarray. +* +* @param x - input ndarray +* @param dims - list of dimensions to reduce +* @returns reduced shape +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = zeros( [ 1, 2, 3, 2 ] ); +* // returns +* +* var sh = reducedShape( x, [ 0, 1 ] ); +* // returns [ 3, 2 ] +*/ +declare function reducedShape( x: ndarray, dims: Collection ): Shape; + + +// EXPORTS // + +export = reducedShape; diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/types/test.ts new file mode 100644 index 000000000000..0c494a892b79 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/docs/types/test.ts @@ -0,0 +1,60 @@ +/* +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +import reducedShape = require( './index' ); + + +// TESTS // + +// The function returns a shape array... +{ + reducedShape( zeros( [ 3, 2, 3 ] ), [ 0, 1 ] ); // $ExpectType Shape +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + reducedShape( '10', [ 0 ] ); // $ExpectError + reducedShape( true, [ 0 ] ); // $ExpectError + reducedShape( false, [ 0 ] ); // $ExpectError + reducedShape( 10, [ 0 ] ); // $ExpectError + reducedShape( null, [ 0 ] ); // $ExpectError + reducedShape( undefined, [ 0 ] ); // $ExpectError + reducedShape( [], [ 0 ] ); // $ExpectError + reducedShape( {}, [ 0 ] ); // $ExpectError + reducedShape( ( x: number ): number => x, [ 0 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object of numbers... +{ + reducedShape( zeros( [ 3, 2, 3 ] ), '10' ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), true ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), false ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), 10 ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), null ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), undefined ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), {} ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + reducedShape(); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ) ); // $ExpectError + reducedShape( zeros( [ 3, 2, 3 ] ), [ 0 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/examples/index.js new file mode 100644 index 000000000000..c7c837a7d608 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/examples/index.js @@ -0,0 +1,38 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var zeros = require( '@stdlib/ndarray/zeros' ); +var reducedShape = require( './../lib' ); + +// Create an array: +var x = zeros( [ 1, 2, 3, 2 ] ); +// returns + +var sh = reducedShape( x, [ 0, 1 ] ); +console.log( sh ); +// => [ 3, 2 ] + +sh = reducedShape( x, [ 0, 3 ] ); +console.log( sh ); +// => [ 2, 3 ] + +sh = reducedShape( x, [ 1, 3 ] ); +console.log( sh ); +// => [ 1, 3 ] diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/lib/index.js new file mode 100644 index 000000000000..e482dacaf75c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/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 reduced shape of a provided ndarray. +* +* @module @stdlib/ndarray/base/reduced-shape +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* var reducedShape = require( '@stdlib/ndarray/base/reduced-shape' ); +* +* var x = zeros( [ 3, 2, 3 ] ); +* // returns +* +* var sh = reducedShape( x, [ 0, 1 ] ); +* // returns [ 3 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/lib/main.js new file mode 100644 index 000000000000..01def8d60c9b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/lib/main.js @@ -0,0 +1,60 @@ +/** +* @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 indicesComplement = require( '@stdlib/array/base/indices-complement' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); + + +// MAIN // + +/** +* Returns a reduced shape of a provided ndarray. +* +* @param {ndarrayLike} x - input ndarray +* @param {IntegerArray} dims - list of dimensions to reduce +* @returns {IntegerArray} shape +* +* @example +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var x = zeros( [ 3, 2, 3 ] ); +* // returns +* +* var sh = reducedShape( x, [ 0, 1 ] ); +* // returns [ 3 ] +*/ +function reducedShape( x, dims ) { + var idx; + var sh; + var N; + + sh = getShape( x, false ); + N = sh.length; + idx = indicesComplement( N, dims ); + return takeIndexed( sh, idx ); +} + + +// EXPORTS // + +module.exports = reducedShape; diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/package.json b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/package.json new file mode 100644 index 000000000000..e1035fb69ef6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/package.json @@ -0,0 +1,71 @@ +{ + "name": "@stdlib/ndarray/base/reduced-shape", + "version": "0.0.0", + "description": "Return a reduced shape of an ndarray.", + "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", + "base", + "ndarray", + "shape", + "reduced", + "dimensions", + "collapsed", + "dims", + "dimensionality", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reduced-shape/test/test.js b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/test/test.js new file mode 100644 index 000000000000..2563015d15f3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reduced-shape/test/test.js @@ -0,0 +1,146 @@ +/** +* @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 zeros = require( '@stdlib/ndarray/zeros' ); +var reducedShape = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reducedShape, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'if provided a zero-dimensional ndarray, the function returns an empty array', function test( t ) { + t.deepEqual( reducedShape( zeros( [] ), [] ), [], 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an empty list of dimensions, the function returns the full shape', function test( t ) { + var x = zeros( [ 3, 2, 3 ] ); + t.deepEqual( reducedShape( x, [] ), [ 3, 2, 3 ], 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns a reduced shape', function test( t ) { + var expected; + var values; + var dims; + var out; + var i; + + values = [ + zeros( [ 1, 2, 3, 2 ] ), + zeros( [ 1, 2, 3, 2 ] ), + zeros( [ 1, 2, 3, 2 ] ), + zeros( [ 3, 2, 3 ] ), + zeros( [ 5 ] ), + zeros( [ 3, 3, 3 ] ) + ]; + + dims = [ + [ 0, 1 ], + [ 0, 3 ], + [ 1, 3 ], + [ 0, 1 ], + [ 0 ], + [ 0, 1, 2 ] + ]; + + expected = [ + [ 3, 2 ], + [ 2, 3 ], + [ 1, 3 ], + [ 3 ], + [], + [] + ]; + + for ( i = 0; i < values.length; i++ ) { + out = reducedShape( values[ i ], dims[ i ] ); + t.deepEqual( out, expected[ i ], 'returns expected value for dims=[' + dims[ i ].join( ',' ) + ']' ); + } + t.end(); +}); + +tape( 'the function accepts minimal ndarray-like objects (shape)', function test( t ) { + var expected; + var values; + var dims; + var out; + var i; + + values = [ + { + 'shape': [ 1, 2, 3, 2 ] + }, + { + 'shape': [ 1, 2, 3, 2 ] + }, + { + 'shape': [ 3, 3, 3 ] + }, + { + 'shape': [ 5, 10 ] + } + ]; + + dims = [ + [ 0, 1 ], + [ 2 ], + [ 1 ], + [ 0 ] + ]; + + expected = [ + [ 3, 2 ], + [ 1, 2, 2 ], + [ 3, 3 ], + [ 10 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + out = reducedShape( values[ i ], dims[ i ] ); + t.deepEqual( out, expected[ i ], 'returns expected value for dims=[' + dims[ i ].join( ',' ) + ']' ); + } + t.end(); +}); + +tape( 'the function reduces a single dimension', function test( t ) { + var x = zeros( [ 2, 3, 4, 5 ] ); + + t.deepEqual( reducedShape( x, [ 0 ] ), [ 3, 4, 5 ], 'returns expected value' ); + t.deepEqual( reducedShape( x, [ 1 ] ), [ 2, 4, 5 ], 'returns expected value' ); + t.deepEqual( reducedShape( x, [ 2 ] ), [ 2, 3, 5 ], 'returns expected value' ); + t.deepEqual( reducedShape( x, [ 3 ] ), [ 2, 3, 4 ], 'returns expected value' ); + t.end(); +}); + +tape( 'the function reduces all dimensions', function test( t ) { + var x = zeros( [ 2, 3, 4 ] ); + + t.deepEqual( reducedShape( x, [ 0, 1, 2 ] ), [], 'returns expected value' ); + t.end(); +});