From 7e1062b08ff2f342c1c6c29cc739f21554ad5edc Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 6 Dec 2025 14:06:38 +0500 Subject: [PATCH] feat: add ndarray/base/to-flippedlr --- 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/to-flippedlr/README.md | 152 ++++++++ .../base/to-flippedlr/benchmark/benchmark.js | 331 ++++++++++++++++++ .../ndarray/base/to-flippedlr/docs/repl.txt | 31 ++ .../base/to-flippedlr/docs/types/index.d.ts | 64 ++++ .../base/to-flippedlr/docs/types/test.ts | 63 ++++ .../base/to-flippedlr/examples/index.js | 40 +++ .../ndarray/base/to-flippedlr/lib/index.js | 62 ++++ .../ndarray/base/to-flippedlr/lib/main.js | 89 +++++ .../ndarray/base/to-flippedlr/package.json | 67 ++++ .../ndarray/base/to-flippedlr/test/test.js | 188 ++++++++++ 10 files changed, 1087 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-flippedlr/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/README.md b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/README.md new file mode 100644 index 000000000000..07476009c9c9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/README.md @@ -0,0 +1,152 @@ + + +# toFlippedlr + +> Return a new ndarray where the order of elements along the last dimension of an input ndarray is reversed. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toFlippedlr = require( '@stdlib/ndarray/base/to-flippedlr' ); +``` + +#### toFlippedlr( x ) + +Returns a new ndarray where the order of elements along the last dimension of an input ndarray is reversed. + +```javascript +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 sh = x.shape; +// returns [ 3, 2 ] + +var arr = ndarray2array( x ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] + +var y = toFlippedlr( x ); +// returns + +sh = y.shape; +// returns [ 3, 2 ] + +arr = ndarray2array( y ); +// returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 5.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. + +
+ + + + + +
+ +## Notes + +- If provided a zero-dimensional ndarray, as the ndarray has no dimensions to reverse, the function simply returns a copy of the input ndarray. + +
+ + + + + +
+ +## Examples + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var toFlippedlr = require( '@stdlib/ndarray/base/to-flippedlr' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 16 ); + +// Create a three-dimensional ndarray: +var x = array( buf, { + 'shape': [ 2, 4, 2 ] +}); + +// Reverse the order of last dimension: +var y = toFlippedlr( x ); +// returns + +var a = ndarray2array( y ); +console.log( a ); +// => [ [ [ 1, 0 ], [ 3, 2 ], [ 5, 4 ], [ 7, 6 ] ], [ [ 9, 8 ], [ 11, 10 ], [ 13, 12 ], [ 15, 14 ] ] ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/benchmark/benchmark.js new file mode 100644 index 000000000000..520c27f6db83 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/benchmark/benchmark.js @@ -0,0 +1,331 @@ +/** +* @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 baseEmpty = require( '@stdlib/ndarray/base/empty' ); +var empty = require( '@stdlib/ndarray/empty' ); +var pkg = require( './../package.json' ).name; +var toFlippedlr = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::1d,base', 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 = toFlippedlr( values[ i%values.length ] ); + 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+'::1d,non-base', 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 = toFlippedlr( values[ i%values.length ] ); + 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,base', 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 = toFlippedlr( values[ i%values.length ] ); + 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,non-base', 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 = toFlippedlr( values[ i%values.length ] ); + 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,base', 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 = toFlippedlr( values[ i%values.length ] ); + 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,non-base', 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 = toFlippedlr( values[ i%values.length ] ); + 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+'::4d,base', 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 = toFlippedlr( values[ i%values.length ] ); + 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+'::4d,non-base', 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 = toFlippedlr( values[ i%values.length ] ); + 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+'::5d,base', 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 = toFlippedlr( values[ i%values.length ] ); + 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+'::5d,non-base', 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 = toFlippedlr( values[ i%values.length ] ); + 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/to-flippedlr/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/repl.txt new file mode 100644 index 000000000000..f5ea4bbd2cb3 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( x ) + Returns a new ndarray where the order of elements along the last dimension + of an input ndarray is reversed along each dimension. + + Parameters + ---------- + x: ndarray + Input array. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + + > x.shape + [ 2, 2 ] + > var y = {{alias}}( x ) + + > y.shape + [ 2, 2 ] + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ [ 2, 1 ], [ 4, 3 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/index.d.ts new file mode 100644 index 000000000000..d1343f93536b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @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 new ndarray where the order of elements along the last dimension of an input ndarray is reversed. +* +* @param x - input array +* @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 = typedarray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], 'float64' ); +* var shape = [ 3, 2 ]; +* var strides = [ 2, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var sh = x.shape; +* // returns [ 3, 2 ] +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = toFlippedlr( x ); +* // returns +* +* sh = y.shape; +* // returns [ 3, 2 ] +* +* arr = ndarray2array( y ); +* // returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 5.0 ] ] +*/ +declare function toFlippedlr( x: T ): T; + + +// EXPORTS // + +export = toFlippedlr; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/test.ts new file mode 100644 index 000000000000..e59e38f34344 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/docs/types/test.ts @@ -0,0 +1,63 @@ +/* +* @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 toFlippedlr = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + toFlippedlr( empty( 'float64', sh, order ) ); // $ExpectType float64ndarray + toFlippedlr( empty( 'float32', sh, order ) ); // $ExpectType float32ndarray + toFlippedlr( empty( 'complex128', sh, order ) ); // $ExpectType complex128ndarray + toFlippedlr( empty( 'complex64', sh, order ) ); // $ExpectType complex64ndarray + toFlippedlr( empty( 'int32', sh, order ) ); // $ExpectType int32ndarray + toFlippedlr( empty( 'int16', sh, order ) ); // $ExpectType int16ndarray + toFlippedlr( empty( 'int8', sh, order ) ); // $ExpectType int8ndarray + toFlippedlr( empty( 'uint32', sh, order ) ); // $ExpectType uint32ndarray + toFlippedlr( empty( 'uint16', sh, order ) ); // $ExpectType uint16ndarray + toFlippedlr( empty( 'uint8', sh, order ) ); // $ExpectType uint8ndarray + toFlippedlr( empty( 'uint8c', sh, order ) ); // $ExpectType uint8cndarray + toFlippedlr( empty( 'generic', sh, order ) ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided an argument which is not an ndarray... +{ + toFlippedlr( '10' ); // $ExpectError + toFlippedlr( 10 ); // $ExpectError + toFlippedlr( false ); // $ExpectError + toFlippedlr( true ); // $ExpectError + toFlippedlr( null ); // $ExpectError + toFlippedlr( [] ); // $ExpectError + toFlippedlr( {} ); // $ExpectError + toFlippedlr( ( 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' ); + + toFlippedlr( x, false ); // $ExpectError + toFlippedlr( x, {} ); // $ExpectError + toFlippedlr( x, 1, '10', {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/examples/index.js new file mode 100644 index 000000000000..540cefcbc140 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/examples/index.js @@ -0,0 +1,40 @@ +/** +* @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 array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var toFlippedlr = require( './../lib' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 16 ); + +// Create a three-dimensional ndarray: +var x = array( buf, { + 'shape': [ 2, 4, 2 ] +}); + +// Reverse the order of last dimension: +var y = toFlippedlr( x ); +// returns + +var a = ndarray2array( y ); +console.log( a ); +// => [ [ [ 1, 0 ], [ 3, 2 ], [ 5, 4 ], [ 7, 6 ] ], [ [ 9, 8 ], [ 11, 10 ], [ 13, 12 ], [ 15, 14 ] ] ] diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/index.js new file mode 100644 index 000000000000..46db27a1a016 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/index.js @@ -0,0 +1,62 @@ +/** +* @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 new ndarray where the order of elements along the last dimension of an input ndarray is reversed. +* +* @module @stdlib/ndarray/base/to-flippedlr +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var toFlippedlr = require( '@stdlib/ndarray/base/to-flippedlr' ); +* +* 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 sh = x.shape; +* // returns [ 3, 2 ] +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = toFlippedlr( x ); +* // returns +* +* sh = y.shape; +* // returns [ 3, 2 ] +* +* arr = ndarray2array( y ); +* // returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 5.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/main.js new file mode 100644 index 000000000000..37889bc98ac4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/lib/main.js @@ -0,0 +1,89 @@ +/** +* @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 ndims = require( '@stdlib/ndarray/base/ndims' ); +var emptyLike = require( '@stdlib/ndarray/base/empty-like' ); +var reverseDimension = require( '@stdlib/ndarray/base/reverse-dimension' ); +var assign = require( '@stdlib/ndarray/base/assign' ); + + +// MAIN // + +/** +* Returns a new ndarray where the order of elements along the last dimension of an input ndarray is reversed. +* +* @param {ndarray} x - input array +* @returns {ndarray} 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 sh = x.shape; +* // returns [ 3, 2 ] +* +* var arr = ndarray2array( x ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] +* +* var y = toFlippedlr( x ); +* // returns +* +* sh = y.shape; +* // returns [ 3, 2 ] +* +* arr = ndarray2array( y ); +* // returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 5.0 ] ] +*/ +function toFlippedlr( x ) { + var out; + var xr; + + // Create an output ndarray with the same shape and data type as the input ndarray: + out = emptyLike( x ); + + // Check whether we were provided a zero-dimensional array... + if ( ndims( x ) === 0 ) { + // Nothing to reverse so just return a copy: + assign( [ x, out ] ); + return out; + } + // Create a reversed view of the input ndarray: + xr = reverseDimension( x, -1, false ); + + // Assign the elements of the reversed input ndarray view to the output ndarray: + assign( [ xr, out ] ); + + return out; +} + + +// EXPORTS // + +module.exports = toFlippedlr; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/package.json b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/package.json new file mode 100644 index 000000000000..4162b629daba --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/ndarray/base/to-flippedlr", + "version": "0.0.0", + "description": "Return a new ndarray where the order of elements along the last dimension of an input ndarray is reversed.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "data", + "structure", + "vector", + "ndarray", + "matrix", + "slice", + "view", + "reverse", + "flip", + "numpy.fliplr" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/test/test.js b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/test/test.js new file mode 100644 index 000000000000..a15057b4690a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-flippedlr/test/test.js @@ -0,0 +1,188 @@ +/** +* @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 zeroTo = require( '@stdlib/array/base/zero-to' ); +var typedarray = require( '@stdlib/array/typed' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var baseCtor = require( '@stdlib/ndarray/base/ctor' ); +var ctor = require( '@stdlib/ndarray/ctor' ); +var toFlippedlr = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toFlippedlr, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'when provided a zero-dimensional input array, the function returns a new zero-dimensional array (base)', function test( t ) { + var actual; + var x; + + x = scalar2ndarray( 3.14, 'float64', 'row-major' ); + + actual = toFlippedlr( x ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.ndims, 0, 'returns expected value' ); + t.strictEqual( actual.dtype, x.dtype, 'returns expected value' ); + t.strictEqual( actual.get(), x.get(), 'returns expected value' ); + t.notEqual( actual.data, x.data, 'returns expected value' ); + + t.end(); +}); + +tape( 'when provided a zero-dimensional input array, the function returns a new zero-dimensional array (base, offset)', function test( t ) { + var actual; + var x; + + x = new baseCtor( 'float64', typedarray( zeroTo( 4 ), 'float64' ), [], [ 0 ], 3, 'row-major' ); + + actual = toFlippedlr( x ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.ndims, 0, 'returns expected value' ); + t.strictEqual( actual.dtype, x.dtype, 'returns expected value' ); + t.strictEqual( actual.get(), 3, 'returns expected value' ); + t.notEqual( actual.data, x.data, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where the order of the elements of an input ndarray are reversed along the last dimension (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 = toFlippedlr( x ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.ndims, 1, 'returns expected value' ); + t.strictEqual( actual.length, 6, 'returns expected value' ); + t.strictEqual( actual.dtype, x.dtype, 'returns expected value' ); + t.notEqual( actual.data, x.data, '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 new ndarray where the order of the elements of an input ndarray are reversed along the last dimension (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 = toFlippedlr( x ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.ndims, 2, 'returns expected value' ); + t.deepEqual( actual.shape, [ 4, 3 ], 'returns expected value' ); + t.strictEqual( actual.dtype, x.dtype, 'returns expected value' ); + t.notEqual( actual.data, x.data, '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' ); + t.end(); +}); + +tape( 'the function returns a new ndarray where the order of the elements of an input ndarray are reversed along the last dimension (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 = toFlippedlr( x ); + + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( actual.ndims, 3, 'returns expected value' ); + t.deepEqual( actual.shape, [ 2, 4, 3 ], 'returns expected value' ); + t.strictEqual( actual.dtype, x.dtype, 'returns expected value' ); + t.notEqual( actual.data, x.data, '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' ); + t.end(); +});