From 76c2e90c443711173fdb2aeedf2774a4801b5718 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 2 Oct 2025 12:24:48 +0500 Subject: [PATCH 1/3] feat: add ndarray/reverse-dimension --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../ndarray/reverse-dimension/README.md | 130 +++++++ .../reverse-dimension/benchmark/benchmark.js | 222 ++++++++++++ .../ndarray/reverse-dimension/docs/repl.txt | 32 ++ .../reverse-dimension/docs/types/index.d.ts | 58 +++ .../reverse-dimension/docs/types/test.ts | 67 ++++ .../reverse-dimension/examples/index.js | 29 ++ .../ndarray/reverse-dimension/lib/index.js | 56 +++ .../ndarray/reverse-dimension/lib/main.js | 75 ++++ .../ndarray/reverse-dimension/package.json | 65 ++++ .../ndarray/reverse-dimension/test/test.js | 336 ++++++++++++++++++ 10 files changed, 1070 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md b/lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md new file mode 100644 index 000000000000..e7bc4908b565 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md @@ -0,0 +1,130 @@ + + +# reverseDimension + +> Return a **read-only** view of an input [`ndarray`][@stdlib/ndarray/ctor] in which the order of elements along a specified dimension is reversed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var reverseDimension = require( '@stdlib/ndarray/reverse-dimension' ); +``` + +#### reverseDimension( x, dim ) + +Returns a read-only view of an input [`ndarray`][@stdlib/ndarray/ctor] in which the order of elements along a specified dimension is reversed. + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], { + 'shape': [ 3, 2 ] +}); +// returns + +var arr = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +var y = reverseDimension( x, 0 ); +// returns + +arr = ndarray2array( y ); +// returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ] +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var reverseDimension = require( '@stdlib/ndarray/reverse-dimension' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = reverseDimension( x, 0 ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/reverse-dimension/benchmark/benchmark.js new file mode 100644 index 000000000000..7465a744b8f8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/benchmark/benchmark.js @@ -0,0 +1,222 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 pkg = require( './../package.json' ).name; +var reverseDimension = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d', 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 = reverseDimension( 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( pkg+'::2d,dim=0', 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 = reverseDimension( 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( pkg+'::2d,dim=1', 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 = reverseDimension( values[ i%values.length ], 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( pkg+'::3d,dim=0', 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 = reverseDimension( 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( pkg+'::3d,dim=1', 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 = reverseDimension( values[ i%values.length ], 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( pkg+'::3d,dim=2', 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 = reverseDimension( values[ i%values.length ], 2 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt new file mode 100644 index 000000000000..4c208c44b7e8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( x, dim ) + Returns a read-only view of an input ndarray in which the order of elements + along a specified dimension is reversed. + + Parameters + ---------- + x: ndarray + Input array. + + dim: integer + Index of dimension to reverse. + + Returns + ------- + out: ndarray + A read-only view of an input ndarray in which the order of elements + along a specified dimension is reversed. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + + > {{alias:@stdlib/ndarray/to-array}}( x ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x, 0 ) + + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ 3, 4 ], [ 1, 2 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/index.d.ts new file mode 100644 index 000000000000..694c198bf8c2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/index.d.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +/** +* Returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed. +* +* @param x - input array +* @param dim - index of dimension to reverse +* @returns output array +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimension( x, 0 ); +* // returns +* +* arr = ndarray2array( y ); +* // returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ] +*/ +declare function reverseDimension( x: T, dim: number ): T; + + +// EXPORTS // + +export = reverseDimension; diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/test.ts new file mode 100644 index 000000000000..ce4c6c95a971 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/types/test.ts @@ -0,0 +1,67 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 reverseDimension = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + reverseDimension( empty( 'float64', sh, order ), 0 ); // $ExpectType float64ndarray + reverseDimension( empty( 'complex64', sh, order ), 1 ); // $ExpectType complex64ndarray + reverseDimension( 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... +{ + reverseDimension( '10', 0 ); // $ExpectError + reverseDimension( 10, 0 ); // $ExpectError + reverseDimension( false, 0 ); // $ExpectError + reverseDimension( true, 0 ); // $ExpectError + reverseDimension( null, 0 ); // $ExpectError + reverseDimension( [], 0 ); // $ExpectError + reverseDimension( {}, 0 ); // $ExpectError + reverseDimension( ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + reverseDimension( x, '1' ); // $ExpectError + reverseDimension( x, false ); // $ExpectError + reverseDimension( x, true ); // $ExpectError + reverseDimension( x, null ); // $ExpectError + reverseDimension( x, [] ); // $ExpectError + reverseDimension( x, {} ); // $ExpectError + reverseDimension( 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' ); + + reverseDimension(); // $ExpectError + reverseDimension( x ); // $ExpectError + reverseDimension( x, 0, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/examples/index.js b/lib/node_modules/@stdlib/ndarray/reverse-dimension/examples/index.js new file mode 100644 index 000000000000..e22957f570bc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/examples/index.js @@ -0,0 +1,29 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 reverseDimension = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = reverseDimension( x, 0 ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/index.js b/lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/index.js new file mode 100644 index 000000000000..92768cd87d7a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/index.js @@ -0,0 +1,56 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed. +* +* @module @stdlib/ndarray/reverse-dimension +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var reverseDimension = require( '@stdlib/ndarray/reverse-dimension' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimension( x, 0 ); +* // returns +* +* arr = ndarray2array( y ); +* // returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/main.js b/lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/main.js new file mode 100644 index 000000000000..cea9b0491770 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/lib/main.js @@ -0,0 +1,75 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var base = require( '@stdlib/ndarray/base/reverse-dimension' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed. +* +* @param {ndarray} x - input array +* @param {integer} dim - index of dimension to reverse +* @throws {TypeError} first argument must be an ndarray having one or more dimensions +* @throws {TypeError} second argument must be an integer +* @throws {RangeError} dimension index exceeds the number of dimensions +* @returns {ndarray} ndarray view +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = reverseDimension( x, 0 ); +* // returns +* +* arr = ndarray2array( y ); +* // returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ] +*/ +function reverseDimension( x, dim ) { + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isInteger( dim ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', dim ) ); + } + return base( x, dim, false ); +} + + +// EXPORTS // + +module.exports = reverseDimension; diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/package.json b/lib/node_modules/@stdlib/ndarray/reverse-dimension/package.json new file mode 100644 index 000000000000..8d27581e999a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/reverse-dimension", + "version": "0.0.0", + "description": "Return a read-only view of an input ndarray in which the order of elements 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", + "view", + "reverse", + "flip", + "dimension" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js b/lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js new file mode 100644 index 000000000000..0dae54acc80d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js @@ -0,0 +1,336 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var zeroTo = require( '@stdlib/array/zero-to' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +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 reverseDimension = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reverseDimension, '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() { + reverseDimension( value, 0 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + 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() { + reverseDimension( x, value ); + }; + } +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed (ndims=1)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 10, 'float64' ); + sh = [ 5 ]; + st = [ 2 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimension( x, 0 ); + expected = [ 8, 6, 4, 2, 0 ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed (ndims=2, dim=0)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 12, 'float64' ); + sh = [ 3, 2 ]; + st = [ 4, 2 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimension( x, 0 ); + expected = [ + [ 8, 10 ], + [ 4, 6 ], + [ 0, 2 ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed (ndims=2, dim=1)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 12, 'float64' ); + sh = [ 3, 2 ]; + st = [ 4, 2 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimension( x, 1 ); + expected = [ + [ 2, 0 ], + [ 6, 4 ], + [ 10, 8 ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed (ndims=3, dim=0)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 24, 'float64' ); + sh = [ 2, 3, 2 ]; + st = [ 12, 4, 2 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimension( x, 0 ); + expected = [ + [ + [ 12, 14 ], + [ 16, 18 ], + [ 20, 22 ] + ], + [ + [ 0, 2 ], + [ 4, 6 ], + [ 8, 10 ] + ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed (ndims=3, dim=1)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 24, 'float64' ); + sh = [ 2, 3, 2 ]; + st = [ 12, 4, 2 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimension( x, 1 ); + expected = [ + [ + [ 8, 10 ], + [ 4, 6 ], + [ 0, 2 ] + ], + [ + [ 20, 22 ], + [ 16, 18 ], + [ 12, 14 ] + ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed (ndims=3, dim=2)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 24, 'float64' ); + sh = [ 2, 3, 2 ]; + st = [ 12, 4, 2 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = reverseDimension( x, 2 ); + expected = [ + [ + [ 2, 0 ], + [ 6, 4 ], + [ 10, 8 ] + ], + [ + [ 14, 12 ], + [ 18, 16 ], + [ 22, 20 ] + ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); From 99c822d18ae69f231c76cd4326f94fee52794596 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 2 Oct 2025 12:35:10 +0500 Subject: [PATCH 2/3] docs: add missing docs --- 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: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - 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: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md b/lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md index e7bc4908b565..b1443f4b5932 100644 --- a/lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/README.md @@ -63,6 +63,11 @@ arr = ndarray2array( y ); // returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ] ``` +The function accepts the following arguments: + +- **x**: input ndarray. +- **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`. + From eafa29f24fa09589e5cf2d1f21447bd1cf6f70c6 Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 2 Oct 2025 20:16:27 +0100 Subject: [PATCH 3/3] test: add tests and update description --- 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: na - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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: na - task: lint_license_headers status: passed --- --- .../ndarray/reverse-dimension/docs/repl.txt | 4 +- .../ndarray/reverse-dimension/test/test.js | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt index 4c208c44b7e8..cd6f14934c13 100644 --- a/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/docs/repl.txt @@ -9,7 +9,9 @@ Input array. dim: integer - Index of dimension to reverse. + Index of dimension to reverse. 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`. Returns ------- diff --git a/lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js b/lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js index 0dae54acc80d..469ee1d6f78f 100644 --- a/lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js +++ b/lib/node_modules/@stdlib/ndarray/reverse-dimension/test/test.js @@ -30,6 +30,7 @@ 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 reverseDimension = require( './../lib' ); @@ -102,6 +103,48 @@ tape( 'the function throws an error if provided a second argument which is not a } }); +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, dim ) { + return function badValue() { + reverseDimension( x, dim ); + }; + } +}); + +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() { + reverseDimension( x, 0 ); + }; + } +}); + tape( 'the function returns a read-only view of an input ndarray in which the order of elements along a specified dimension is reversed (ndims=1)', function test( t ) { var expected; var actual;