diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/README.md new file mode 100644 index 000000000000..222ed3364e72 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/README.md @@ -0,0 +1,126 @@ + + +# toReversedDimensions + +> Return a new [`ndarray`][@stdlib/ndarray/ctor] where the order of elements of an input [`ndarray`][@stdlib/ndarray/ctor] along specified dimensions is reversed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toReversedDimensions = require( '@stdlib/ndarray/to-reversed-dimensions' ); +``` + +#### toReversedDimensions( x, dims ) + +Returns a new [`ndarray`][@stdlib/ndarray/ctor] where the order of elements of an input [`ndarray`][@stdlib/ndarray/ctor] 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 = toReversedDimensions( 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`][@stdlib/ndarray/ctor]. +- **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/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toReversedDimensions = require( '@stdlib/ndarray/to-reversed-dimensions' ); + +var x = uniform( [ 3, 3, 3 ], -100, 100 ); +console.log( ndarray2array( x ) ); + +var y = toReversedDimensions( x, [ 0, 2 ] ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..73ccd8bb9c85 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-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 toReversedDimensions = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ndims=1,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 = toReversedDimensions( 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:ndims=2,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 = toReversedDimensions( 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:ndims=2,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 = toReversedDimensions( 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:ndims=3,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 = toReversedDimensions( 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:ndims=3,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 = toReversedDimensions( 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:ndims=3,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 = toReversedDimensions( 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/to-reversed-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/docs/repl.txt new file mode 100644 index 000000000000..f25fc6bf5a39 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( x, dims ) + Returns a new ndarray where the order of elements of an input ndarray along + specified dimensions is reversed. + + Parameters + ---------- + x: ndarray + Input array. + + dims: ArrayLike + 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`. + + Returns + ------- + out: ndarray + Output array. + + 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/to-reversed-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..a38e1e0b12ab --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-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 new ndarray where the order of elements of an input ndarray 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 = toReversedDimensions( x, [ 0, 1 ] ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +declare function toReversedDimensions( x: T, dims: Collection ): T; + + +// EXPORTS // + +export = toReversedDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..289a7b9c0fc7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-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 toReversedDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + toReversedDimensions( empty( 'float64', sh, order ), [ 0 ] ); // $ExpectType float64ndarray + toReversedDimensions( empty( 'complex64', sh, order ), [ 1 ] ); // $ExpectType complex64ndarray + toReversedDimensions( 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... +{ + toReversedDimensions( '10', [ 0 ] ); // $ExpectError + toReversedDimensions( 10, [ 0 ] ); // $ExpectError + toReversedDimensions( false, [ 0 ] ); // $ExpectError + toReversedDimensions( true, [ 0 ] ); // $ExpectError + toReversedDimensions( null, [ 0 ] ); // $ExpectError + toReversedDimensions( [], [ 0 ] ); // $ExpectError + toReversedDimensions( {}, [ 0 ] ); // $ExpectError + toReversedDimensions( ( 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' ); + + toReversedDimensions( x, '1' ); // $ExpectError + toReversedDimensions( x, 1 ); // $ExpectError + toReversedDimensions( x, false ); // $ExpectError + toReversedDimensions( x, true ); // $ExpectError + toReversedDimensions( x, null ); // $ExpectError + toReversedDimensions( x, {} ); // $ExpectError + toReversedDimensions( 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' ); + + toReversedDimensions(); // $ExpectError + toReversedDimensions( x ); // $ExpectError + toReversedDimensions( x, [ 0 ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/examples/index.js new file mode 100644 index 000000000000..a59c3d1922e2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-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/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toReversedDimensions = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -100, 100 ); +console.log( ndarray2array( x ) ); + +var y = toReversedDimensions( x, [ 0, 2 ] ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/lib/index.js new file mode 100644 index 000000000000..5fad7121cc40 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-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 new ndarray where the order of elements of an input ndarray along specified dimensions is reversed. +* +* @module @stdlib/ndarray/to-reversed-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var toReversedDimensions = require( '@stdlib/ndarray/to-reversed-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 = toReversedDimensions( 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/to-reversed-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/lib/main.js new file mode 100644 index 000000000000..94e0f2193fa3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/lib/main.js @@ -0,0 +1,74 @@ +/** +* @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 ndarraylike2ndarray = require( '@stdlib/ndarray/base/ndarraylike2ndarray' ); +var reverseDimensions = require( '@stdlib/ndarray/base/reverse-dimensions' ); +var emptyLike = require( '@stdlib/ndarray/empty-like' ); +var assign = require( '@stdlib/ndarray/base/assign' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a new ndarray where the order of elements of an input ndarray 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} 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 = toReversedDimensions( x, [ 0, 1 ] ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +function toReversedDimensions( x, dims ) { + var out; + var bv; + + 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 ) ); + } + bv = ndarraylike2ndarray( x ); + bv = reverseDimensions( bv, dims, false ); + out = emptyLike( bv ); + assign( [ bv, out ] ); + return out; +} + + +// EXPORTS // + +module.exports = toReversedDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/package.json new file mode 100644 index 000000000000..49d50fbd24d3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/to-reversed-dimensions", + "version": "0.0.0", + "description": "Return a new ndarray where the order of elements of an input ndarray 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", + "reverse", + "dimensions", + "flip", + "numpy.flip" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/test/test.js new file mode 100644 index 000000000000..7625424e57ea --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/to-reversed-dimensions/test/test.js @@ -0,0 +1,341 @@ +/** +* @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 instanceOf = require( '@stdlib/assert/instance-of' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var baseCtor = require( '@stdlib/ndarray/base/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +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 toReversedDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toReversedDimensions, '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() { + toReversedDimensions( 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 opts; + var x; + var i; + + opts = { + 'dtype': 'float64' + }; + x = zeroTo( [ 2, 2 ], opts ); + + 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() { + toReversedDimensions( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided an invalid dimension index', 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() { + toReversedDimensions( 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() { + toReversedDimensions( x, dims ); + }; + } +}); + +tape( 'the function throws an error if provided a zero-dimensional ndarray', function test( t ) { + var x = zeros( [] ); + t.throws( badValue, TypeError, 'throws an error' ); + t.end(); + + function badValue() { + toReversedDimensions( x, [ 0 ] ); + } +}); + +tape( 'the function returns a new ndarray where the order of elements of an input ndarray along specified dimensions is reversed (1D)', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic' + }; + x = zeroTo( [ 5 ], opts ); + + expected = [ 4, 3, 2, 1, 0 ]; + + y = toReversedDimensions( x, [ 0 ] ); + t.deepEqual( getShape( y ), [ 5 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where the order of elements of an input ndarray along specified dimensions is reversed (2D)', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'dims': [ 0, 1 ] + }; + x = zeroTo( [ 3, 2 ], opts ); + + expected = [ + [ 5, 4 ], + [ 3, 2 ], + [ 1, 0 ] + ]; + + y = toReversedDimensions( x, [ 0, 1 ] ); + t.deepEqual( getShape( y ), [ 3, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where the order of elements of an input ndarray along specified dimensions is reversed (3D)', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'dims': [ 0, 1, 2 ] + }; + x = zeroTo( [ 2, 3, 2 ], opts ); + + expected = [ + [ + [ 11, 10 ], + [ 9, 8 ], + [ 7, 6 ] + ], + [ + [ 5, 4 ], + [ 3, 2 ], + [ 1, 0 ] + ] + ]; + + y = toReversedDimensions( x, [ 0, 1, 2 ] ); + t.deepEqual( getShape( y ), [ 2, 3, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports reversing a subset of dimensions', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'dims': [ 0, 1, 2 ] + }; + x = zeroTo( [ 2, 3, 2 ], opts ); + + expected = [ + [ + [ 7, 6 ], + [ 9, 8 ], + [ 11, 10 ] + ], + [ + [ 1, 0 ], + [ 3, 2 ], + [ 5, 4 ] + ] + ]; + + y = toReversedDimensions( x, [ 0, 2 ] ); + t.deepEqual( getShape( y ), [ 2, 3, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative dimension indices', function test( t ) { + var expected; + var opts; + var x; + var y; + + opts = { + 'dtype': 'generic', + 'dims': [ 0, 1, 2 ] + }; + x = zeroTo( [ 2, 3, 2 ], opts ); + + expected = [ + [ + [ 1, 0 ], + [ 3, 2 ], + [ 5, 4 ] + ], + [ + [ 7, 6 ], + [ 9, 8 ], + [ 11, 10 ] + ] + ]; + + y = toReversedDimensions( x, [ -1 ] ); + t.deepEqual( getShape( y ), [ 2, 3, 2 ], 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( y ), getDType( x ) ), true, 'returns expected value' ); + t.notEqual( getData( y ), getData( x ), 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a top-level ndarray even when provided a base ndarray as input', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new baseCtor( 'float64', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + expected = [ + [ 6.0, 5.0, 4.0 ], + [ 3.0, 2.0, 1.0 ] + ]; + + y = toReversedDimensions( x, [ 0, 1 ] ); + t.strictEqual( instanceOf( y, ndarray ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +});