From 3581c168cbf9eeb59d589692b97a28038b7ec8f4 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sun, 3 May 2026 22:20:24 +0500 Subject: [PATCH 1/2] feat: add ndarray/reverse-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/reverse-dimensions/README.md | 126 ++++++ .../reverse-dimensions/benchmark/benchmark.js | 223 ++++++++++ .../ndarray/reverse-dimensions/docs/repl.txt | 30 ++ .../reverse-dimensions/docs/types/index.d.ts | 47 ++ .../reverse-dimensions/docs/types/test.ts | 69 +++ .../reverse-dimensions/examples/index.js | 29 ++ .../ndarray/reverse-dimensions/lib/index.js | 44 ++ .../ndarray/reverse-dimensions/lib/main.js | 64 +++ .../ndarray/reverse-dimensions/package.json | 65 +++ .../ndarray/reverse-dimensions/test/test.js | 410 ++++++++++++++++++ 10 files changed, 1107 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimensions/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/README.md new file mode 100644 index 000000000000..a070720b1db8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/README.md @@ -0,0 +1,126 @@ + + +# reverseDimensions + +> Return a **read-only** view of an input [`ndarray`][@stdlib/ndarray/ctor] in which the order of elements along specified dimensions is reversed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var reverseDimensions = require( '@stdlib/ndarray/reverse-dimensions' ); +``` + +#### reverseDimensions( x, dims ) + +Returns a read-only view of an input [`ndarray`][@stdlib/ndarray/ctor] in which the order of elements along specified dimensions is reversed. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +var y = reverseDimensions( x, [ 0, 1 ] ); +// returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **dims**: indices of dimensions along which to reverse elements. If a dimension index is provided as an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var reverseDimensions = require( '@stdlib/ndarray/reverse-dimensions' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = reverseDimensions( x, [ 0, 2 ] ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..0b83b6cdc90b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js @@ -0,0 +1,223 @@ +/** +* @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 reverseDimensions = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::1d,dims=[0]', 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 = reverseDimensions( values[ i%values.length ], [ 0 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::2d,dims=[0]', 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 = reverseDimensions( values[ i%values.length ], [ 0 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::2d,dims=[0,1]', 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 = reverseDimensions( values[ i%values.length ], [ 0, 1 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,dims=[0]', 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 = reverseDimensions( values[ i%values.length ], [ 0 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,dims=[0,2]', 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 = reverseDimensions( values[ i%values.length ], [ 0, 2 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,dims=[0,1,2]', 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 = reverseDimensions( values[ i%values.length ], [ 0, 1, 2 ] ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + 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/reverse-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/repl.txt new file mode 100644 index 000000000000..20771101db90 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/repl.txt @@ -0,0 +1,30 @@ + +{{alias}}( x, dims ) + Returns a read-only view of an input ndarray in which the order of elements + along specified dimensions is reversed. + + Parameters + ---------- + x: ndarray + Input array. + + dims: ArrayLike + Indices of dimensions to reverse. If a dimension index is provided as 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 in which the order of elements + along specified dimensions is reversed. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) + [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + > var y = {{alias}}( x, [ 0, 1 ] ) + [ [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..d64c1b86b6ad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-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 { ndarray } from '@stdlib/types/ndarray'; +import { Collection } from '@stdlib/types/array'; + +/** +* Returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed. +* +* @param x - input array +* @param dims - indices of dimensions to reverse +* @returns output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimensions( x, [ 0, 1 ] ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +declare function reverseDimensions( x: T, dims: Collection ): T; + + +// EXPORTS // + +export = reverseDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..7a38c53d19cf --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/docs/types/test.ts @@ -0,0 +1,69 @@ +/* +* @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 empty = require( '@stdlib/ndarray/base/empty' ); +import reverseDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + reverseDimensions( empty( 'float64', sh, order ), [ 0 ] ); // $ExpectType float64ndarray + reverseDimensions( empty( 'complex64', sh, order ), [ 1 ] ); // $ExpectType complex64ndarray + reverseDimensions( empty( 'int32', sh, order ), [ -1 ] ); // $ExpectType int32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + reverseDimensions( '10', [ 0 ] ); // $ExpectError + reverseDimensions( 10, [ 0 ] ); // $ExpectError + reverseDimensions( false, [ 0 ] ); // $ExpectError + reverseDimensions( true, [ 0 ] ); // $ExpectError + reverseDimensions( null, [ 0 ] ); // $ExpectError + reverseDimensions( [], [ 0 ] ); // $ExpectError + reverseDimensions( {}, [ 0 ] ); // $ExpectError + reverseDimensions( ( x: number ): number => x, [ 0 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array of integers... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + reverseDimensions( x, '1' ); // $ExpectError + reverseDimensions( x, 1 ); // $ExpectError + reverseDimensions( x, false ); // $ExpectError + reverseDimensions( x, true ); // $ExpectError + reverseDimensions( x, null ); // $ExpectError + reverseDimensions( x, {} ); // $ExpectError + reverseDimensions( 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' ); + + reverseDimensions(); // $ExpectError + reverseDimensions( x ); // $ExpectError + reverseDimensions( x, [ 0 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/examples/index.js new file mode 100644 index 000000000000..f83ae8eda0dc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-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 reverseDimensions = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = reverseDimensions( x, [ 0, 2 ] ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/lib/index.js new file mode 100644 index 000000000000..ce5e124093fd --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-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 in which the order of elements along specified dimensions is reversed. +* +* @module @stdlib/ndarray/reverse-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var reverseDimensions = require( '@stdlib/ndarray/reverse-dimensions' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimensions( x, [ 0, 1 ] ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/lib/main.js new file mode 100644 index 000000000000..4aabae781e80 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/lib/main.js @@ -0,0 +1,64 @@ +/** +* @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 isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var base = require( '@stdlib/ndarray/base/reverse-dimensions' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed. +* +* @param {ndarray} x - input array +* @param {IntegerArray} dims - indices of dimensions to reverse +* @throws {TypeError} first argument must be an ndarray having one or more dimensions +* @throws {TypeError} second argument must be an array of integers +* @throws {RangeError} dimension index exceeds the number of dimensions +* @throws {Error} must provide unique dimension indices +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimensions( x, [ 0, 1 ] ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +function reverseDimensions( x, dims ) { + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isIntegerArray( dims ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an array of integers. Value: `%s`.', dims ) ); + } + return base( x, dims, false ); +} + + +// EXPORTS // + +module.exports = reverseDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/package.json new file mode 100644 index 000000000000..3640bd782db2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/reverse-dimensions", + "version": "0.0.0", + "description": "Return a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed.", + "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", + "view", + "reverse", + "flip", + "dimensions" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/test/test.js new file mode 100644 index 000000000000..36d939a12277 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/test/test.js @@ -0,0 +1,410 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var zeroTo = require( '@stdlib/blas/ext/zero-to' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var reverseDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reverseDimensions, '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, + NaN, + 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() { + reverseDimensions( value, [ 0 ] ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an array of integers', function test( t ) { + var values; + var x; + var i; + + x = zeroTo( [ 2, 2 ], { + 'dims': [ 0, 1 ] + }); + + values = [ + '5', + 5, + 3.14, + NaN, + true, + false, + null, + void 0, + [], + [ '1', '2' ], + [ 1.5, 2.5 ], + {}, + 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() { + reverseDimensions( x, value ); + }; + } +}); + +tape( 'the function throws an error if a dimension index exceeds the number of dimensions', function test( t ) { + var values; + var i; + + values = [ + zeros( [ 1 ] ), + zeros( [ 1, 1 ] ), + zeros( [ 1, 1, 1 ] ), + zeros( [ 1, 1, 1, 1 ] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ], [ 10 ] ), RangeError, 'throws an error when provided ' + values[ i ].shape.join( 'x' ) ); + t.throws( badValue( values[ i ], [ -10 ] ), RangeError, 'throws an error when provided ' + values[ i ].shape.join( 'x' ) ); + } + t.end(); + + function badValue( x, dims ) { + return function badValue() { + reverseDimensions( x, dims ); + }; + } +}); + +tape( 'the function throws an error if provided duplicate dimension indices', function test( t ) { + var values; + var x; + var i; + + x = zeros( [ 2, 2, 2 ] ); + + values = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, -3 ], + [ -1, 2 ] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( dims ) { + return function badValue() { + reverseDimensions( x, dims ); + }; + } +}); + +tape( 'the function throws an error if provided a zero-dimensional array', function test( t ) { + var values; + var i; + + values = [ + zeros( [] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error' ); + } + t.end(); + + function badValue( x ) { + return function badValue() { + reverseDimensions( x, [ 0 ] ); + }; + } +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed (ndims=1)', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 5 ] ); + + actual = reverseDimensions( x, [ 0 ] ); + expected = [ 4.0, 3.0, 2.0, 1.0, 0.0 ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed (ndims=2, dims=[0])', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 2 ], { + 'dims': [ 0, 1 ] + }); + + actual = reverseDimensions( x, [ 0 ] ); + expected = [ + [ 4.0, 5.0 ], + [ 2.0, 3.0 ], + [ 0.0, 1.0 ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed (ndims=2, dims=[1])', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 2 ], { + 'dims': [ 0, 1 ] + }); + + actual = reverseDimensions( x, [ 1 ] ); + expected = [ + [ 1.0, 0.0 ], + [ 3.0, 2.0 ], + [ 5.0, 4.0 ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed (ndims=2, dims=[0,1])', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 3, 2 ], { + 'dims': [ 0, 1 ] + }); + + actual = reverseDimensions( x, [ 0, 1 ] ); + expected = [ + [ 5.0, 4.0 ], + [ 3.0, 2.0 ], + [ 1.0, 0.0 ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed (ndims=3, dims=[0])', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 3, 2 ], { + 'dims': [ 0, 1, 2 ] + }); + + actual = reverseDimensions( x, [ 0 ] ); + expected = [ + [ + [ 6.0, 7.0 ], + [ 8.0, 9.0 ], + [ 10.0, 11.0 ] + ], + [ + [ 0.0, 1.0 ], + [ 2.0, 3.0 ], + [ 4.0, 5.0 ] + ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed (ndims=3, dims=[0,2])', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 3, 2 ], { + 'dims': [ 0, 1, 2 ] + }); + + actual = reverseDimensions( x, [ 0, 2 ] ); + expected = [ + [ + [ 7.0, 6.0 ], + [ 9.0, 8.0 ], + [ 11.0, 10.0 ] + ], + [ + [ 1.0, 0.0 ], + [ 3.0, 2.0 ], + [ 5.0, 4.0 ] + ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along specified dimensions is reversed (ndims=3, dims=[0,1,2])', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 3, 2 ], { + 'dims': [ 0, 1, 2 ] + }); + + actual = reverseDimensions( x, [ 0, 1, 2 ] ); + expected = [ + [ + [ 11.0, 10.0 ], + [ 9.0, 8.0 ], + [ 7.0, 6.0 ] + ], + [ + [ 5.0, 4.0 ], + [ 3.0, 2.0 ], + [ 1.0, 0.0 ] + ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative dimension indices', function test( t ) { + var expected; + var actual; + var x; + + x = zeroTo( [ 2, 3, 2 ], { + 'dims': [ 0, 1, 2 ] + }); + + actual = reverseDimensions( x, [ -1 ] ); + expected = [ + [ + [ 1.0, 0.0 ], + [ 3.0, 2.0 ], + [ 5.0, 4.0 ] + ], + [ + [ 7.0, 6.0 ], + [ 9.0, 8.0 ], + [ 11.0, 10.0 ] + ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); From 14c19b010503707bc4381809e48136d259a84284 Mon Sep 17 00:00:00 2001 From: Athan Date: Sun, 3 May 2026 14:43:04 -0700 Subject: [PATCH 2/2] bench: fix descriptions Signed-off-by: Athan --- .../reverse-dimensions/benchmark/benchmark.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js index 0b83b6cdc90b..dca923225e68 100644 --- a/lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimensions/benchmark/benchmark.js @@ -30,7 +30,7 @@ var reverseDimensions = require( './../lib' ); // MAIN // -bench( format( '%s::1d,dims=[0]', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=1,dims=[0]', pkg ), function benchmark( b ) { var values; var v; var i; @@ -62,7 +62,7 @@ bench( format( '%s::1d,dims=[0]', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::2d,dims=[0]', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=2,dims=[0]', pkg ), function benchmark( b ) { var values; var v; var i; @@ -94,7 +94,7 @@ bench( format( '%s::2d,dims=[0]', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::2d,dims=[0,1]', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=2,dims=[0,1]', pkg ), function benchmark( b ) { var values; var v; var i; @@ -126,7 +126,7 @@ bench( format( '%s::2d,dims=[0,1]', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::3d,dims=[0]', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=3,dims=[0]', pkg ), function benchmark( b ) { var values; var v; var i; @@ -158,7 +158,7 @@ bench( format( '%s::3d,dims=[0]', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::3d,dims=[0,2]', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=3,dims=[0,2]', pkg ), function benchmark( b ) { var values; var v; var i; @@ -190,7 +190,7 @@ bench( format( '%s::3d,dims=[0,2]', pkg ), function benchmark( b ) { b.end(); }); -bench( format( '%s::3d,dims=[0,1,2]', pkg ), function benchmark( b ) { +bench( format( '%s:ndims=3,dims=[0,1,2]', pkg ), function benchmark( b ) { var values; var v; var i;