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..900d0e0b9dcc --- /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] along a specified dimension 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] along a specified dimension 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][@stdlib/ndarray/ctor]. +- **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 toReversedDimension = require( '@stdlib/ndarray/to-reversed-dimension' ); + +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/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..712dcc68ed2f --- /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 along + a specified dimension 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..c73dca03cec5 --- /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'; + +/** +* Interface describing function options. +*/ +interface Options { + /** + * Index of dimension to reverse. Default: `-1`. + */ + dim?: number; +} + +/** +* Returns a new ndarray where the order of elements of an input ndarray along a specified dimension 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..070dd699d169 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/docs/types/test.ts @@ -0,0 +1,85 @@ +/* +* @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 `dim` option 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': {} } ); // $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..808a928e1994 --- /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 along a specified dimension is reversed. +* +* @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..52b944696af7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/lib/main.js @@ -0,0 +1,88 @@ +/** +* @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 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 normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' ); +var base = require( '@stdlib/ndarray/base/to-reversed-dimension' ); +var ndims = require( '@stdlib/ndarray/ndims' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a new ndarray where the order of elements of an input ndarray along a specified dimension is reversed. +* +* @param {ndarrayLike} x - input array +* @param {Object} [options] - function options +* @param {integer} [options.dim=-1] - index of dimension to reverse +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} options argument must be an object +* @throws {TypeError} must provide valid options +* @throws {RangeError} `dim` option is out-of-bounds +* @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 N; + var d; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', 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; + } + } + N = ndims( x ); + d = normalizeIndex( opts.dim, N-1 ); + if ( d === -1 ) { + throw new RangeError( format( 'invalid option. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%d`.', N, d ) ); + } + 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..12d04f978f01 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/to-reversed-dimension", + "version": "0.0.0", + "description": "Return a new ndarray where the order of elements of an input ndarray along a specified dimension 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", + "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..8bda759641c2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimension/test/test.js @@ -0,0 +1,344 @@ +/** +* @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 throws 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 throws an error if provided a `dim` option which is out-of-bounds', function test( t ) { + var values; + var x; + var i; + + x = empty( [ 2, 2 ] ); + values = [ + -4, + -3, + 2, + 3, + 4 + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + toReversedDimension( x, { + 'dim': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a zero-dimensional ndarray', function test( t ) { + var x = empty( [] ); + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + toReversedDimension( x ); + } +}); + +tape( 'the function returns a new ndarray where the order of elements of an input ndarray along a specified dimension 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 along a specified dimension 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 along a specified dimension 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(); +});