From 848748236ccafb1af3ce7b0eb801d38b0b63f327 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 2 Feb 2026 22:08:37 +0500 Subject: [PATCH] feat: add ndarray/to-reversed-dimension --- 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/to-reversed-dimension/README.md | 130 ++++++++ .../benchmark/benchmark.js | 201 ++++++++++++ .../to-reversed-dimension/docs/repl.txt | 34 ++ .../docs/types/index.d.ts | 57 ++++ .../to-reversed-dimension/docs/types/test.ts | 84 +++++ .../to-reversed-dimension/examples/index.js | 29 ++ .../to-reversed-dimension/lib/index.js | 44 +++ .../ndarray/to-reversed-dimension/lib/main.js | 84 +++++ .../to-reversed-dimension/package.json | 66 ++++ .../to-reversed-dimension/test/test.js | 306 ++++++++++++++++++ 10 files changed, 1035 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/to-reversed-dimension/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/README.md b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/README.md new file mode 100644 index 000000000000..d254cd511524 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/README.md @@ -0,0 +1,130 @@ + + +# toReversedDimension + +> Return a new [`ndarray`][@stdlib/ndarray/ctor] where the order of elements of an input [`ndarray`][@stdlib/ndarray/ctor] is reversed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toReversedDimension = require( '@stdlib/ndarray/to-reversed-dimension' ); +``` + +#### toReversedDimension( x\[, options] ) + +Returns a new [`ndarray`][@stdlib/ndarray/ctor] where the order of elements of an input [`ndarray`][@stdlib/ndarray/ctor] is reversed. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +var y = toReversedDimension( x ); +// returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **options**: function options (_optional_). + +The function supports the following options: + +- **dim**: index of dimension along which to reverse elements. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. Default: `-1`. + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toReversed = require( '@stdlib/ndarray/to-reversed-dimension' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = toReversed( x ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/benchmark/benchmark.js new file mode 100644 index 000000000000..ea93ed42237c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/benchmark/benchmark.js @@ -0,0 +1,201 @@ +/** +* @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 toReversedDimension = require( './../lib' ); + + +// 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 = toReversedDimension( values[ i%values.length ], { + 'dim': 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', 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 = toReversedDimension( values[ i%values.length ], { + 'dim': 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', 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 = toReversedDimension( values[ i%values.length ], { + 'dim': 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::4d', 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, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 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 = toReversedDimension( values[ i%values.length ], { + 'dim': 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::5d', 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, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 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 = toReversedDimension( values[ i%values.length ], { + 'dim': 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(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/repl.txt new file mode 100644 index 000000000000..4ed2ac36be9d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( x[, options] ) + Returns a new ndarray where the order of elements of an input ndarray is + reversed. + + Parameters + ---------- + x: ndarray + Input array. + + options: object (optional) + Function options. + + options.dim: integer (optional) + Index of dimension along which to reverse elements. If provided an + integer less than zero, the dimension index is resolved relative to the + last dimension, with the last dimension corresponding to the value `-1`. + Default: `-1`. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x ) + [ [ 2, 1 ], [ 4, 3 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/index.d.ts new file mode 100644 index 000000000000..95f1e7a33178 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @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'; + +/** +* Base options. +*/ +interface Options { + /** + * Index of dimension to reverse. + */ + dim?: number; +} + +/** +* Returns a new ndarray where the order of elements of an input ndarray is reversed. +* +* @param x - input array +* @param options - function options +* @param options.dim - index of dimension to reverse +* @returns output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = toReversedDimension( x ); +* // returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ] ] +*/ +declare function toReversedDimension( x: T, options?: Options ): T; + + +// EXPORTS // + +export = toReversedDimension; diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/test.ts new file mode 100644 index 000000000000..e7ebe0f19580 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/test.ts @@ -0,0 +1,84 @@ +/* +* @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 empty = require( '@stdlib/ndarray/empty' ); +import toReversedDimension = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + var x = empty( [ 2, 2 ] ); + + toReversedDimension( x ); // $ExpectType float64ndarray + toReversedDimension( x, {} ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + toReversedDimension( '10' ); // $ExpectError + toReversedDimension( 10 ); // $ExpectError + toReversedDimension( false ); // $ExpectError + toReversedDimension( true ); // $ExpectError + toReversedDimension( null ); // $ExpectError + toReversedDimension( [] ); // $ExpectError + toReversedDimension( {} ); // $ExpectError + toReversedDimension( ( x: number ): number => x ); // $ExpectError + + toReversedDimension( '10', {} ); // $ExpectError + toReversedDimension( 10, {} ); // $ExpectError + toReversedDimension( false, {} ); // $ExpectError + toReversedDimension( true, {} ); // $ExpectError + toReversedDimension( null, {} ); // $ExpectError + toReversedDimension( [], {} ); // $ExpectError + toReversedDimension( {}, {} ); // $ExpectError + toReversedDimension( ( x: number ): number => x, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = empty( [ 2, 2 ] ); + + toReversedDimension( x, '5' ); // $ExpectError + toReversedDimension( x, true ); // $ExpectError + toReversedDimension( x, false ); // $ExpectError + toReversedDimension( x, null ); // $ExpectError + toReversedDimension( x, [ '5' ] ); // $ExpectError + toReversedDimension( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `options.dim` property which is not a number... +{ + const x = empty( [ 2, 2 ] ); + + toReversedDimension( x, { 'dim': '5'} ); // $ExpectError + toReversedDimension( x, { 'dim': true } ); // $ExpectError + toReversedDimension( x, { 'dim': false } ); // $ExpectError + toReversedDimension( x, { 'dim': null } ); // $ExpectError + toReversedDimension( x, { 'dim': [ '5' ] } ); // $ExpectError + toReversedDimension( x, { 'dim': ( x: number ): number => x} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = empty( [ 2, 2 ] ); + + toReversedDimension(); // $ExpectError + toReversedDimension( x, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/examples/index.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/examples/index.js new file mode 100644 index 000000000000..e962359ac097 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/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 toReversedDimension = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = toReversedDimension( x ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/index.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/index.js new file mode 100644 index 000000000000..9958c874dc9a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/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 new ndarray where the order of elements of an input ndarray is reversed along a specified dimension. +* +* @module @stdlib/ndarray/to-reversed-dimension +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var toReversedDimension = require( '@stdlib/ndarray/to-reversed-dimension' ); +* +*var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = toReversedDimension( x ); +* // returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/main.js new file mode 100644 index 000000000000..3b681991b25c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/main.js @@ -0,0 +1,84 @@ +/** +* @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 normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var base = require( '@stdlib/ndarray/base/to-reversed-dimension' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isObject = require( '@stdlib/assert/is-plain-object' ); +var isInteger = require( '@stdlib/assert/is-integer' ); +var format = require( '@stdlib/string/format' ); +var ndims = require( '@stdlib/ndarray/ndims' ); + + +// MAIN // + +/** +* Returns a new ndarray where the order of elements of an input ndarray is reversed. +* +* @param {ndarray} x - input array +* @param {Object} options - function options +* @param {integer} options.dim - index of dimension to reverse +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} options argument must be an object +* @returns {ndarray} output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +*var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = toReversedDimension( x ); +* // returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ] ] +*/ +function toReversedDimension( x, options ) { + var opts; + var d; + var N; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + N = ndims( x ); + opts = { + 'dim': -1 + }; + if ( arguments.length > 1 ) { + if ( !isObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'dim' ) ) { + if ( !isInteger( options.dim ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an integer. Option: `%s`.', 'dim', options.dim ) ); + } + opts.dim = options.dim; + } + } + d = normalizeIndex( opts.dim, N-1 ); + return base( x, d ); +} + + +// EXPORTS // + +module.exports = toReversedDimension; diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/package.json b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/package.json new file mode 100644 index 000000000000..8754189a4190 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/to-reversed-dimension", + "version": "0.0.0", + "description": "Return a new ndarray where the order of elements of an input ndarray 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", + "base", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "reverse", + "dimension", + "flip", + "numpy.flip" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/test/test.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/test/test.js new file mode 100644 index 000000000000..9cb8d80fb19a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/test/test.js @@ -0,0 +1,306 @@ +/** +* @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 empty = require( '@stdlib/ndarray/empty' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var ndims = require( '@stdlib/ndarray/base/ndims' ); +var getDType = require( '@stdlib/ndarray/base/dtype' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var toReversedDimension = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toReversedDimension, '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() { + toReversedDimension( value ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray (options)', 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() { + toReversedDimension( value, {} ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ] ); + 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() { + toReversedDimension( x, value ); + }; + } +}); + +tape( 'the function returns an error if provided a `dim` option which is not an integer', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ] ); + values = [ + '5', + -3.14, + 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() { + toReversedDimension( x, { + 'dim': value + }); + }; + } +}); + +tape( 'the function returns a new ndarray where the order of elements of an input ndarray is reversed (ndims=1)', function test( t ) { + var expected; + var actual; + var x; + + x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + + actual = toReversedDimension( x ); + expected = [ 6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.strictEqual( numel( getShape( actual ) ), 6, 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = toReversedDimension( x, { + 'dim': -1 + }); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 6 ], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where the order of elements of an input ndarray is reversed (ndims=2)', function test( t ) { + var expected; + var actual; + var x; + + x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); + + actual = toReversedDimension( x ); + expected = [ [ 2.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 5.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = toReversedDimension( x, { + 'dim': -2 + }); + expected = [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where the order of elements of an input ndarray is reversed (ndims=3)', function test( t ) { + var expected; + var actual; + var x; + + x = array([ + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ], + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ] + ]); + + actual = toReversedDimension( x ); + expected = [ + [ + [ 2.0, 1.0 ], + [ 4.0, 3.0 ] + ], + [ + [ 6.0, 5.0 ], + [ 8.0, 7.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = toReversedDimension( x, { + 'dim': -2 + }); + expected = [ + [ + [ 3.0, 4.0 ], + [ 1.0, 2.0 ] + ], + [ + [ 7.0, 8.0 ], + [ 5.0, 6.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = toReversedDimension( x, { + 'dim': -3 + }); + expected = [ + [ + [ 5.0, 6.0 ], + [ 7.0, 8.0 ] + ], + [ + [ 1.0, 2.0 ], + [ 3.0, 4.0 ] + ] + ]; + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2, 2 ], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +});