From 81f2bfbfb867a1f00a06a303f15e0572061641fe Mon Sep 17 00:00:00 2001 From: headlessNode Date: Mon, 26 Jan 2026 00:59:08 +0500 Subject: [PATCH] feat: add ndarray/base/reverse-dimensions --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/base/reverse-dimensions/README.md | 135 ++++ .../reverse-dimensions/benchmark/benchmark.js | 332 ++++++++++ .../base/reverse-dimensions/docs/repl.txt | 35 ++ .../reverse-dimensions/docs/types/index.d.ts | 104 +++ .../reverse-dimensions/docs/types/test.ts | 119 ++++ .../base/reverse-dimensions/examples/index.js | 39 ++ .../base/reverse-dimensions/lib/index.js | 44 ++ .../base/reverse-dimensions/lib/main.js | 84 +++ .../base/reverse-dimensions/package.json | 67 ++ .../base/reverse-dimensions/test/test.js | 593 ++++++++++++++++++ 10 files changed, 1552 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/README.md b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/README.md new file mode 100644 index 000000000000..ed1589cb29e8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/README.md @@ -0,0 +1,135 @@ + + +# reverseDimensions + +> Return a view of an input ndarray in which the order of elements along specified dimensions is reversed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var reverseDimensions = require( '@stdlib/ndarray/base/reverse-dimensions' ); +``` + +#### reverseDimensions( x, dims, writable ) + +Returns a view of an input ndarray in which the order of elements along specified dimensions is reversed. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +var y = reverseDimensions( x, [ 0, 1 ], false ); +// returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **dims**: dimension indices 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`. +- **writable**: boolean indicating whether a returned ndarray should be writable. + +
+ + + + + +
+ +## Notes + +- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances. + +
+ + + + + +
+ +## Examples + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var reverseDimensions = require( '@stdlib/ndarray/base/reverse-dimensions' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 16 ); + +// Create a three-dimensional ndarray: +var x = array( buf, { + 'shape': [ 2, 4, 2 ] +}); +// returns [ [ [ 0.0, 1.0 ], [ 2.0, 3.0 ], [ 4.0, 5.0 ], [ 6.0, 7.0 ] ], [ [ 8.0, 9.0 ], [ 10.0, 11.0 ], [ 12.0, 13.0 ], [ 14.0, 15.0 ] ] ] + +// Reverse elements across all dimensions: +var y = reverseDimensions( x, [ 0, 1, 2 ], false ); +// returns [ [ [ 15.0, 14.0 ], [ 13.0, 12.0 ], [ 11.0, 10.0 ], [ 9.0, 8.0 ] ], [ [ 7.0, 6.0 ], [ 5.0, 4.0 ], [ 3.0, 2.0 ], [ 1.0, 0.0 ] ] ] + +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/benchmark/benchmark.js new file mode 100644 index 000000000000..82bb05ba3395 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/benchmark/benchmark.js @@ -0,0 +1,332 @@ +/** +* @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 baseEmpty = require( '@stdlib/ndarray/base/empty' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var reverseDimensions = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::1d,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0 ], false ); + 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::1d,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0 ], false ); + 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,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1 ], false ); + 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,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1 ], false ); + 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,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1, 2 ], false ); + 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,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1, 2 ], false ); + 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,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2, 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1, 2, 3 ], false ); // eslint-disable-line max-len + 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,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1, 2, 3 ], false ); // eslint-disable-line max-len + 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,base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'float32', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'int32', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'complex128', [ 2, 2, 2, 2, 2 ], 'row-major' ), + baseEmpty( 'generic', [ 2, 2, 2, 2, 2 ], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1, 2, 3, 4 ], false ); // eslint-disable-line max-len + 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,non-base', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + 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 */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = reverseDimensions( values[ i%values.length ], [ 0, 1, 2, 3, 4 ], false ); // eslint-disable-line max-len + 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/base/reverse-dimensions/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/repl.txt new file mode 100644 index 000000000000..ca9c3fc9a880 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( x, dims, writable ) + Returns a view of an input ndarray in which the order of elements along + specified dimensions is reversed. + + Parameters + ---------- + x: ndarray + Input array. + + dims: ArrayLikeObject + Dimension indices along which to reverse elements. If less than zero, + the index is resolved relative to the last dimension, with the last + dimension corresponding to the value `-1`. + + writable: boolean + Boolean indicating whether a returned ndarray should be writable. This + parameter only applies to ndarray constructors which support read-only + instances. + + Returns + ------- + out: ndarray + Output array view. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x, [ 0, 1 ], false ) + [ [ 4, 3 ], [ 2, 1 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/index.d.ts new file mode 100644 index 000000000000..88b211566789 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/index.d.ts @@ -0,0 +1,104 @@ +/* +* @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 { typedndarray, genericndarray, complexndarray } from '@stdlib/types/ndarray'; + +/** +* Returns a view of an input ndarray in which the order of elements along specified dimensions is reversed. +* +* @param x - input array +* @param dims - indices of dimension to reverse +* @param writable - boolean indicating whether a returned array should be writable +* @returns output array +* +* @example +* var typedarray = require( '@stdlib/array/typed' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = [ 1, 2, 3, 4, 5, 6 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +* +* var y = reverseDimensions( x, [ 0, 1 ], false ); +* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] +*/ +declare function reverseDimensions( x: typedndarray, dims: Array, writable: boolean ): typedndarray; + +/** +* Returns a view of an input ndarray in which the order of elements along specified dimensions is reversed. +* +* @param x - input array +* @param dims - indices of dimension to reverse +* @param writable - boolean indicating whether a returned array should be writable +* @returns output array +* +* @example +* var typedarray = require( '@stdlib/array/typed' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* var buffer = typedarray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ], 'complex64' ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = reverseDimensions( x, [ 0 ], false ); +* // returns +*/ +declare function reverseDimensions( x: complexndarray, dims: Array, writable: boolean ): complexndarray; + +/** +* Returns a view of an input ndarray in which the order of elements along specified dimensions is reversed. +* +* @param x - input array +* @param dims - indices of dimension to reverse +* @param writable - boolean indicating whether a returned array should be writable +* @returns output array +* +* @example +* var typedarray = require( '@stdlib/array/typed' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1, 2, 3, 4, 5, 6 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +* +* var y = reverseDimensions( x, [ 0, 1 ], false ); +* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] +*/ +declare function reverseDimensions( x: genericndarray, dims: Array, writable: boolean ): genericndarray; + + +// EXPORTS // + +export = reverseDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/test.ts new file mode 100644 index 000000000000..cdf56eb2dfe6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/docs/types/test.ts @@ -0,0 +1,119 @@ +/* +* @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/base/empty' ); +import reverseDimensions = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + reverseDimensions( empty( 'float64', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'float32', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'complex128', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'complex64', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'int32', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'int16', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'int8', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint32', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint16', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint8', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint8c', sh, order ), [ 0 ], false ); // $ExpectType typedndarray + + reverseDimensions( empty( 'float64', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'float32', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'complex128', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'complex64', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'int32', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'int16', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'int8', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint32', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint16', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint8', sh, order ), [ 0 ], true ); // $ExpectType typedndarray + reverseDimensions( empty( 'uint8c', sh, order ), [ 0 ], true ); // $ExpectType typedndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + reverseDimensions( '10', [ 0 ], false ); // $ExpectError + reverseDimensions( 10, [ 0 ], false ); // $ExpectError + reverseDimensions( false, [ 0 ], false ); // $ExpectError + reverseDimensions( true, [ 0 ], false ); // $ExpectError + reverseDimensions( null, [ 0 ], false ); // $ExpectError + reverseDimensions( [], [ 0 ], false ); // $ExpectError + reverseDimensions( {}, [ 0 ], false ); // $ExpectError + reverseDimensions( ( x: number ): number => x, [ 0 ], false ); // $ExpectError + + reverseDimensions( '10', [ 0 ], true ); // $ExpectError + reverseDimensions( 10, [ 0 ], true ); // $ExpectError + reverseDimensions( false, [ 0 ], true ); // $ExpectError + reverseDimensions( true, [ 0 ], true ); // $ExpectError + reverseDimensions( null, [ 0 ], true ); // $ExpectError + reverseDimensions( [], [ 0 ], true ); // $ExpectError + reverseDimensions( {}, [ 0 ], true ); // $ExpectError + reverseDimensions( ( x: number ): number => x, [ 0 ], true ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array of integers... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + reverseDimensions( x, '5', false ); // $ExpectError + reverseDimensions( x, true, false ); // $ExpectError + reverseDimensions( x, false, false ); // $ExpectError + reverseDimensions( x, null, false ); // $ExpectError + reverseDimensions( x, undefined, false ); // $ExpectError + reverseDimensions( x, [ '5' ], false ); // $ExpectError + reverseDimensions( x, {}, false ); // $ExpectError + reverseDimensions( x, ( x: number ): number => x, false ); // $ExpectError + + reverseDimensions( x, '5', true ); // $ExpectError + reverseDimensions( x, true, true ); // $ExpectError + reverseDimensions( x, false, true ); // $ExpectError + reverseDimensions( x, null, true ); // $ExpectError + reverseDimensions( x, undefined, true ); // $ExpectError + reverseDimensions( x, [ '5' ], true ); // $ExpectError + reverseDimensions( x, {}, true ); // $ExpectError + reverseDimensions( x, ( x: number ): number => x, true ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a boolean... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + reverseDimensions( x, [ 0 ], '5' ); // $ExpectError + reverseDimensions( x, [ 0 ], 5 ); // $ExpectError + reverseDimensions( x, [ 0 ], null ); // $ExpectError + reverseDimensions( x, [ 0 ], undefined ); // $ExpectError + reverseDimensions( x, [ 0 ], [ '5' ] ); // $ExpectError + reverseDimensions( x, [ 0 ], {} ); // $ExpectError + reverseDimensions( x, [ 0 ], ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + reverseDimensions( x ); // $ExpectError + reverseDimensions( x, 0 ); // $ExpectError + reverseDimensions( x, [ 0 ], false, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/examples/index.js new file mode 100644 index 000000000000..c8baef9dd505 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/examples/index.js @@ -0,0 +1,39 @@ +/** +* @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 array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var reverseDimensions = require( './../lib' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 16 ); + +// Create a three-dimensional ndarray: +var x = array( buf, { + 'shape': [ 2, 4, 2 ] +}); +// returns [ [ [ 0.0, 1.0 ], [ 2.0, 3.0 ], [ 4.0, 5.0 ], [ 6.0, 7.0 ] ], [ [ 8.0, 9.0 ], [ 10.0, 11.0 ], [ 12.0, 13.0 ], [ 14.0, 15.0 ] ] ] + +// Reverse elements across all dimensions: +var y = reverseDimensions( x, [ 0, 1, 2 ], false ); +// returns [ [ [ 15.0, 14.0 ], [ 13.0, 12.0 ], [ 11.0, 10.0 ], [ 9.0, 8.0 ] ], [ [ 7.0, 6.0 ], [ 5.0, 4.0 ], [ 3.0, 2.0 ], [ 1.0, 0.0 ] ] ] + +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/lib/index.js new file mode 100644 index 000000000000..11b69ac4155f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a view of an input ndarray in which the order of elements along specified dimensions is reversed. +* +* @module @stdlib/ndarray/base/reverse-dimensions +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var reverseDimensions = require( '@stdlib/ndarray/base/reverse-dimensions' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimensions( x, [ 0, 1 ], false ); +* // 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/base/reverse-dimensions/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/lib/main.js new file mode 100644 index 000000000000..fc3cad486b24 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/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 args2multislice = require( '@stdlib/slice/base/args2multislice' ); +var Slice = require( '@stdlib/slice/ctor' ); +var slice = require( '@stdlib/ndarray/base/slice' ); +var nulls = require( '@stdlib/array/base/nulls' ); +var ndims = require( '@stdlib/ndarray/base/ndims' ); +var toNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' ); +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a view of an input ndarray in which the order of elements along specified dimensions is reversed. +* +* @param {ndarray} x - input array +* @param {IntegerArray} dims - indices of dimension to reverse +* @param {boolean} writable - boolean indicating whether a returned array should be writable +* @throws {TypeError} first argument must be an ndarray having one or more dimensions +* @throws {RangeError} dimension index exceeds the number of dimensions +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimensions( x, [ 0, 1 ], false ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +function reverseDimensions( x, dims, writable ) { + var args; + var N; + var d; + var i; + + // Retrieve the number of array dimensions: + N = ndims( x ); + + // Check whether we were provided a zero-dimensional array... + if ( N === 0 ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray having one or more dimensions. Number of dimensions: %d.', N ) ); + } + // Normalize the dimension index... + d = toNormalizedIndices( dims, N-1 ); + if ( !isIntegerArray( d ) ) { + throw new RangeError( format( 'invalid argument. Dimension index exceeds the number of dimensions. Number of dimensions: %d. Value: `%s`.', N, dims ) ); + } + // Define a list of MultiSlice constructor arguments: + args = nulls( N ); + for ( i = 0; i < d.length; i++ ) { + args[ d[ i ] ] = new Slice( null, null, -1 ); + } + // Return a new array view: + return slice( x, args2multislice( args ), true, writable ); +} + + +// EXPORTS // + +module.exports = reverseDimensions; diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/package.json b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/package.json new file mode 100644 index 000000000000..dd84f701ecf3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ndarray/base/reverse-dimensions", + "version": "0.0.0", + "description": "Return a view of an input ndarray in which the order of elements along specified dimensions is reversed.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "slice", + "view", + "reverse", + "flip", + "numpy.flip" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/test/test.js b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/test/test.js new file mode 100644 index 000000000000..c894a5691847 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reverse-dimensions/test/test.js @@ -0,0 +1,593 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var typedarray = require( '@stdlib/array/typed' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var baseCtor = require( '@stdlib/ndarray/base/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var ctor = require( '@stdlib/ndarray/ctor' ); +var ndims = require( '@stdlib/ndarray/base/ndims' ); +var numel = require( '@stdlib/ndarray/base/numel' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var reverseDimensions = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reverseDimensions, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a zero-dimensional array', function test( t ) { + var values; + var i; + + values = [ + zeros( [] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error' ); + } + t.end(); + + function badValue( x ) { + return function badValue() { + reverseDimensions( x, [ 0 ], false ); + }; + } +}); + +tape( 'the function throws an error if the dimension index exceeds the number of dimensions', function test( t ) { + var values; + var i; + + values = [ + zeros( [ 1 ] ), + zeros( [ 1, 1 ] ), + zeros( [ 1, 1, 1 ] ), + zeros( [ 1, 1, 1, 1 ] ) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ], [ 10 ] ), RangeError, 'throws an error when provided ' + values[ i ].shape.join( 'x' ) ); + t.throws( badValue( values[ i ], [ -10 ] ), RangeError, 'throws an error when provided ' + values[ i ].shape.join( 'x' ) ); + } + t.end(); + + function badValue( x, dims ) { + return function badValue() { + reverseDimensions( x, dims, false ); + }; + } +}); + +tape( 'the function returns a view of a provided input array (ndims=1)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + var i; + + buf = typedarray( zeroTo( 30 ), 'float64' ); + sh = [ 6 ]; + st = [ 2 ]; + o = 4; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimensions( x, [ 0 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.strictEqual( numel( getShape( x ) ), 6, 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ 14, 12, 10, 8, 6, 4 ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' ); + } + + actual = reverseDimensions( x, [ -1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.strictEqual( numel( getShape( x ) ), 6, 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ 14, 12, 10, 8, 6, 4 ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function returns a view of a provided input array (ndims=2)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 30 ), 'float64' ); + sh = [ 4, 3 ]; + st = [ 6, 2 ]; + o = 4; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimensions( x, [ 0 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ 22, 24, 26 ], + [ 16, 18, 20 ], + [ 10, 12, 14 ], + [ 4, 6, 8 ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ -2 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ 22, 24, 26 ], + [ 16, 18, 20 ], + [ 10, 12, 14 ], + [ 4, 6, 8 ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ 1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ 8, 6, 4 ], + [ 14, 12, 10 ], + [ 20, 18, 16 ], + [ 26, 24, 22 ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ -1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ 8, 6, 4 ], + [ 14, 12, 10 ], + [ 20, 18, 16 ], + [ 26, 24, 22 ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ 0, 1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ 26, 24, 22 ], + [ 20, 18, 16 ], + [ 14, 12, 10 ], + [ 8, 6, 4 ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ -2, -1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 2, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ 26, 24, 22 ], + [ 20, 18, 16 ], + [ 14, 12, 10 ], + [ 8, 6, 4 ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a view of a provided input array (ndims=3)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = typedarray( zeroTo( 100 ), 'float64' ); + sh = [ 2, 4, 3 ]; + st = [ 24, 6, 2 ]; + o = 10; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimensions( x, [ 0 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 34, 36, 38 ], + [ 40, 42, 44 ], + [ 46, 48, 50 ], + [ 52, 54, 56 ] + ], + [ + [ 10, 12, 14 ], + [ 16, 18, 20 ], + [ 22, 24, 26 ], + [ 28, 30, 32 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ -3 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 34, 36, 38 ], + [ 40, 42, 44 ], + [ 46, 48, 50 ], + [ 52, 54, 56 ] + ], + [ + [ 10, 12, 14 ], + [ 16, 18, 20 ], + [ 22, 24, 26 ], + [ 28, 30, 32 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ 1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 28, 30, 32 ], + [ 22, 24, 26 ], + [ 16, 18, 20 ], + [ 10, 12, 14 ] + ], + [ + [ 52, 54, 56 ], + [ 46, 48, 50 ], + [ 40, 42, 44 ], + [ 34, 36, 38 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ -2 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 28, 30, 32 ], + [ 22, 24, 26 ], + [ 16, 18, 20 ], + [ 10, 12, 14 ] + ], + [ + [ 52, 54, 56 ], + [ 46, 48, 50 ], + [ 40, 42, 44 ], + [ 34, 36, 38 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ 2 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 14, 12, 10 ], + [ 20, 18, 16 ], + [ 26, 24, 22 ], + [ 32, 30, 28 ] + ], + [ + [ 38, 36, 34 ], + [ 44, 42, 40 ], + [ 50, 48, 46 ], + [ 56, 54, 52 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ -1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 14, 12, 10 ], + [ 20, 18, 16 ], + [ 26, 24, 22 ], + [ 32, 30, 28 ] + ], + [ + [ 38, 36, 34 ], + [ 44, 42, 40 ], + [ 50, 48, 46 ], + [ 56, 54, 52 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ 0, 1, 2 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 56, 54, 52 ], + [ 50, 48, 46 ], + [ 44, 42, 40 ], + [ 38, 36, 34 ] + ], + [ + [ 32, 30, 28 ], + [ 26, 24, 22 ], + [ 20, 18, 16 ], + [ 14, 12, 10 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + actual = reverseDimensions( x, [ -3, -2, -1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 3, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + + expected = [ + [ + [ 56, 54, 52 ], + [ 50, 48, 46 ], + [ 44, 42, 40 ], + [ 38, 36, 34 ] + ], + [ + [ 32, 30, 28 ], + [ 26, 24, 22 ], + [ 20, 18, 16 ], + [ 14, 12, 10 ] + ] + ]; + actual = ndarray2array( actual ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an ndarray having a constructor supporting read-only instances, the function supports returning a writable view (non-base, ndims=1)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + var i; + + buf = typedarray( zeroTo( 30 ), 'float64' ); + sh = [ 6 ]; + st = [ 2 ]; + o = 4; + ord = 'row-major'; + + x = new ctor( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimensions( x, [ 0 ], true ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.strictEqual( numel( getShape( x ) ), 6, 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), false, 'returns expected value' ); + + expected = [ 14, 12, 10, 8, 6, 4 ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' ); + } + + actual = reverseDimensions( x, [ -1 ], true ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.strictEqual( numel( getShape( x ) ), 6, 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), false, 'returns expected value' ); + + expected = [ 14, 12, 10, 8, 6, 4 ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'if provided an ndarray having a constructor which does not support read-only instances, the function is not guaranteed to return either a read-only or writable view (base, ndims=1)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + var i; + + buf = typedarray( zeroTo( 30 ), 'float64' ); + sh = [ 6 ]; + st = [ 2 ]; + o = 4; + ord = 'row-major'; + + x = new baseCtor( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimensions( x, [ 0 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.strictEqual( numel( getShape( x ) ), 6, 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), false, 'returns expected value' ); + + expected = [ 14, 12, 10, 8, 6, 4 ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' ); + } + + actual = reverseDimensions( x, [ -1 ], false ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( ndims( actual ), 1, 'returns expected value' ); + t.strictEqual( numel( getShape( x ) ), 6, 'returns expected value' ); + t.strictEqual( getDType( actual ), getDType( x ), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), false, 'returns expected value' ); + + expected = [ 14, 12, 10, 8, 6, 4 ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual.iget( i ), expected[ i ], 'returns expected value' ); + } + t.end(); +});