From 8bcb738f0fc355eae92b40541cc61550fda1fbef Mon Sep 17 00:00:00 2001 From: Muhammad Haris <101793258+headlessNode@users.noreply.github.com> Date: Sat, 7 Sep 2024 14:09:03 +0500 Subject: [PATCH] feat: add `ndarray/base/to-reversed` PR-URL: https://github.com/stdlib-js/stdlib/pull/2861 Ref: https://github.com/stdlib-js/stdlib/issues/2656 Co-authored-by: Athan Reines Reviewed-by: Athan Reines --- .../base/reverse/docs/types/index.d.ts | 436 +----------------- .../ndarray/base/reverse/docs/types/test.ts | 2 + .../ndarray/base/to-reversed/README.md | 171 +++++++ .../base/to-reversed/benchmarks/benchmark.js | 393 ++++++++++++++++ .../ndarray/base/to-reversed/docs/repl.txt | 31 ++ .../base/to-reversed/docs/types/index.d.ts | 64 +++ .../base/to-reversed/docs/types/test.ts | 63 +++ .../base/to-reversed/examples/index.js | 66 +++ .../ndarray/base/to-reversed/lib/index.js | 62 +++ .../ndarray/base/to-reversed/lib/main.js | 82 ++++ .../ndarray/base/to-reversed/package.json | 68 +++ .../ndarray/base/to-reversed/test/test.js | 223 +++++++++ 12 files changed, 1227 insertions(+), 434 deletions(-) create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/benchmarks/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-reversed/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/index.d.ts index 84473380aa6..55facc3dd5d 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/index.d.ts @@ -20,7 +20,7 @@ /// -import { typedndarray, genericndarray, float64ndarray, float32ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, complex128ndarray, complex64ndarray } from '@stdlib/types/ndarray'; +import { ndarray } from '@stdlib/types/ndarray'; /** * Returns a view of an input ndarray in which the order of elements along each dimension is reversed. @@ -57,439 +57,7 @@ import { typedndarray, genericndarray, float64ndarray, float32ndarray, int32ndar * arr = ndarray2array( y ); * // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] */ -declare function reverse( x: float64ndarray, writable: boolean ): float64ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], 'float32' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'float32', 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 = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] -*/ -declare function reverse( x: float32ndarray, writable: boolean ): float32ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1, 2, 3, 4, 5, 6 ], 'int32' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: int32ndarray, writable: boolean ): int32ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1, 2, 3, 4, 5, 6 ], 'int16' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: int16ndarray, writable: boolean ): int16ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1, 2, 3, 4, 5, 6 ], 'int8' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: int8ndarray, writable: boolean ): int8ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1, 2, 3, 4, 5, 6 ], 'uint32' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: uint32ndarray, writable: boolean ): uint32ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1, 2, 3, 4, 5, 6 ], 'uint16' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: uint16ndarray, writable: boolean ): uint16ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1, 2, 3, 4, 5, 6 ], 'uint8' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: uint8ndarray, writable: boolean ): uint8ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = typedarray( [ 1, 2, 3, 4, 5, 6 ], 'uint8c' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: uint8cndarray, writable: boolean ): uint8cndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = 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 ], 'complex128' ); -* var shape = [ 3, 2 ]; -* var strides = [ 2, 1 ]; -* var offset = 0; -* -* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); -* // returns -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -*/ -declare function reverse( x: complex128ndarray, writable: boolean ): complex128ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 = 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 sh = x.shape; -* // returns [ 3, 2 ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -*/ -declare function reverse( x: complex64ndarray, writable: boolean ): complex64ndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: genericndarray, writable: boolean ): genericndarray; - -/** -* Returns a view of an input ndarray in which the order of elements along each dimension is reversed. -* -* @param x - input array -* @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 -* -* var sh = x.shape; -* // returns [ 3, 2 ] -* -* var arr = ndarray2array( x ); -* // returns [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] -* -* var y = reverse( x, false ); -* // returns -* -* sh = y.shape; -* // returns [ 3, 2 ] -* -* arr = ndarray2array( y ); -* // returns [ [ 6, 5 ], [ 4, 3 ], [ 2, 1 ] ] -*/ -declare function reverse( x: typedndarray, writable: boolean ): typedndarray; +declare function reverse( x: T, writable: boolean ): T; // EXPORTS // diff --git a/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/test.ts index 919526faa03..bbfb9d494db 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/test.ts +++ b/lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/test.ts @@ -38,6 +38,7 @@ import reverse = require( './index' ); reverse( empty( 'uint16', sh, order ), false ); // $ExpectType uint16ndarray reverse( empty( 'uint8', sh, order ), false ); // $ExpectType uint8ndarray reverse( empty( 'uint8c', sh, order ), false ); // $ExpectType uint8cndarray + reverse( empty( 'generic', sh, order ), false ); // $ExpectType genericndarray reverse( empty( 'float64', sh, order ), true ); // $ExpectType float64ndarray reverse( empty( 'float32', sh, order ), true ); // $ExpectType float32ndarray @@ -50,6 +51,7 @@ import reverse = require( './index' ); reverse( empty( 'uint16', sh, order ), true ); // $ExpectType uint16ndarray reverse( empty( 'uint8', sh, order ), true ); // $ExpectType uint8ndarray reverse( empty( 'uint8c', sh, order ), true ); // $ExpectType uint8cndarray + reverse( empty( 'generic', sh, order ), false ); // $ExpectType genericndarray } // The compiler throws an error if the function is provided a first argument which is not an ndarray... diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/README.md b/lib/node_modules/@stdlib/ndarray/base/to-reversed/README.md new file mode 100644 index 00000000000..bac1913676b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/README.md @@ -0,0 +1,171 @@ + + +# toReversed + +> Return a new ndarray where the order of elements of an input ndarray is reversed along each dimension. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toReversed = require( '@stdlib/ndarray/base/to-reversed' ); +``` + +#### toReversed( x ) + +Returns a new ndarray where the order of elements of an input ndarray is reversed along each dimension. + +```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 = toReversed( x ); +// returns + +sh = y.shape; +// returns [ 3, 2 ] + +arr = ndarray2array( y ); +// returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: 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 toReversed = require( '@stdlib/ndarray/base/to-reversed' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 16 ); + +// Create a one-dimensional ndarray: +var x1 = array( buf, { + 'shape': [ 16 ] +}); + +// Reverse element order: +var y1 = toReversed( x1, false ); +// returns + +var a1 = ndarray2array( y1 ); +// returns [ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ] + +// Create a two-dimensional ndarray: +var x2 = array( buf, { + 'shape': [ 4, 4 ] +}); + +// Reverse element order: +var y2 = toReversed( x2, false ); +// returns + +var a2 = ndarray2array( y2 ); +// returns [ [ 15, 14, 13, 12 ], [ 11, 10, 9, 8 ], [ 7, 6, 5, 4 ], [ 3, 2, 1, 0 ] ] + +// Create a three-dimensional ndarray: +var x3 = array( buf, { + 'shape': [ 2, 4, 2 ] +}); + +// Reverse element order: +var y3 = toReversed( x3, false ); +// returns + +var a3 = ndarray2array( y3 ); +// returns [ [ [ 15, 14 ], [ 13, 12 ], [ 11, 10 ], [ 9, 8 ] ], [ [ 7, 6 ], [ 5, 4 ], [ 3, 2 ], [ 1, 0 ] ] ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/benchmarks/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/to-reversed/benchmarks/benchmark.js new file mode 100644 index 00000000000..5b44812e209 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/benchmarks/benchmark.js @@ -0,0 +1,393 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable no-restricted-syntax */ + +'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 toReversed = require( './../lib' ); + + +// MAIN // + +bench( pkg+'::0d,base', function benchmark( b ) { + var values; + var v; + var i; + + values = [ + baseEmpty( 'float64', [], 'row-major' ), + baseEmpty( 'float32', [], 'row-major' ), + baseEmpty( 'int32', [], 'row-major' ), + baseEmpty( 'complex128', [], 'row-major' ), + baseEmpty( 'generic', [], 'row-major' ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = toReversed( 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+'::0d,non-base', function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline */ + + values = [ + empty( [], { 'dtype': 'float64' } ), + empty( [], { 'dtype': 'float32' } ), + empty( [], { 'dtype': 'int32' } ), + empty( [], { 'dtype': 'complex128' } ), + empty( [], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = toReversed( 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,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 = toReversed( 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 = toReversed( 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 = toReversed( 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 = toReversed( 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 = toReversed( 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 = toReversed( 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 = toReversed( 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 = toReversed( 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 = toReversed( 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 = toReversed( 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-reversed/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/repl.txt new file mode 100644 index 00000000000..c45dc101718 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( x ) + Returns a new ndarray where the order of elements 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 ) + [ [ 4, 3 ], [ 2, 1 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/index.d.ts new file mode 100644 index 00000000000..8630acfc231 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/index.d.ts @@ -0,0 +1,64 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 of an input ndarray is reversed along each dimension. +* +* @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 = toReversed( x ); +* // returns +* +* sh = y.shape; +* // returns [ 3, 2 ] +* +* arr = ndarray2array( y ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +declare function toReversed( x: T ): T; + + +// EXPORTS // + +export = toReversed; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/test.ts new file mode 100644 index 00000000000..e4d9a460c5a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/docs/types/test.ts @@ -0,0 +1,63 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 toReversed = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + toReversed( empty( 'float64', sh, order ) ); // $ExpectType float64ndarray + toReversed( empty( 'float32', sh, order ) ); // $ExpectType float32ndarray + toReversed( empty( 'complex128', sh, order ) ); // $ExpectType complex128ndarray + toReversed( empty( 'complex64', sh, order ) ); // $ExpectType complex64ndarray + toReversed( empty( 'int32', sh, order ) ); // $ExpectType int32ndarray + toReversed( empty( 'int16', sh, order ) ); // $ExpectType int16ndarray + toReversed( empty( 'int8', sh, order ) ); // $ExpectType int8ndarray + toReversed( empty( 'uint32', sh, order ) ); // $ExpectType uint32ndarray + toReversed( empty( 'uint16', sh, order ) ); // $ExpectType uint16ndarray + toReversed( empty( 'uint8', sh, order ) ); // $ExpectType uint8ndarray + toReversed( empty( 'uint8c', sh, order ) ); // $ExpectType uint8cndarray + toReversed( empty( 'generic', sh, order ) ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided an argument which is not an ndarray... +{ + toReversed( '10' ); // $ExpectError + toReversed( 10 ); // $ExpectError + toReversed( false ); // $ExpectError + toReversed( true ); // $ExpectError + toReversed( null ); // $ExpectError + toReversed( [] ); // $ExpectError + toReversed( {} ); // $ExpectError + toReversed( ( 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' ); + + toReversed( x, false ); // $ExpectError + toReversed( x, {} ); // $ExpectError + toReversed( x, 1, '10', {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/to-reversed/examples/index.js new file mode 100644 index 00000000000..076aabf41e2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/examples/index.js @@ -0,0 +1,66 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 toReversed = require( './../lib' ); + +// Create a linear ndarray buffer: +var buf = zeroTo( 16 ); + +// Create a one-dimensional ndarray: +var x1 = array( buf, { + 'shape': [ 16 ] +}); + +// Reverse element order: +var y1 = toReversed( x1, false ); +// returns + +var a1 = ndarray2array( y1 ); +console.log( a1 ); +// => [ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ] + +// Create a two-dimensional ndarray: +var x2 = array( buf, { + 'shape': [ 4, 4 ] +}); + +// Reverse element order: +var y2 = toReversed( x2, false ); +// returns + +var a2 = ndarray2array( y2 ); +console.log( a2 ); +// => [ [ 15, 14, 13, 12 ], [ 11, 10, 9, 8 ], [ 7, 6, 5, 4 ], [ 3, 2, 1, 0 ] ] + +// Create a three-dimensional ndarray: +var x3 = array( buf, { + 'shape': [ 2, 4, 2 ] +}); + +// Reverse element order: +var y3 = toReversed( x3, false ); +// returns + +var a3 = ndarray2array( y3 ); +console.log( a3 ); +// => [ [ [ 15, 14 ], [ 13, 12 ], [ 11, 10 ], [ 9, 8 ] ], [ [ 7, 6 ], [ 5, 4 ], [ 3, 2 ], [ 1, 0 ] ] ] diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/to-reversed/lib/index.js new file mode 100644 index 00000000000..e8bc2ac40d5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/lib/index.js @@ -0,0 +1,62 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a new ndarray where the order of elements of an input ndarray is reversed along each dimension. +* +* @module @stdlib/ndarray/base/to-reversed +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var toReversed = require( '@stdlib/ndarray/base/to-reversed' ); +* +* 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 = toReversed( x ); +* // returns +* +* sh = y.shape; +* // returns [ 3, 2 ] +* +* arr = ndarray2array( y ); +* // 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/to-reversed/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/to-reversed/lib/main.js new file mode 100644 index 00000000000..99be2504d93 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/lib/main.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 emptyLike = require( '@stdlib/ndarray/base/empty-like' ); +var reverse = require( '@stdlib/ndarray/base/reverse' ); +var assign = require( '@stdlib/ndarray/base/assign' ); + + +// MAIN // + +/** +* Returns a new ndarray where the order of elements of an input ndarray is reversed along each dimension. +* +* @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 = toReversed( x ); +* // returns +* +* sh = y.shape; +* // returns [ 3, 2 ] +* +* arr = ndarray2array( y ); +* // returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +function toReversed( x ) { + var out; + var xr; + + // Create a reversed view of the input ndarray: + xr = reverse( x, false ); + + // Create an output ndarray with the same shape and data type as the input ndarray: + out = emptyLike( x ); + + // Assign the elements of the reversed input ndarray view to the output ndarray: + assign( [ xr, out ] ); + + return out; +} + + +// EXPORTS // + +module.exports = toReversed; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/package.json b/lib/node_modules/@stdlib/ndarray/base/to-reversed/package.json new file mode 100644 index 00000000000..83039614a28 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/ndarray/base/to-reversed", + "version": "0.0.0", + "description": "Return a new ndarray where the order of elements of an input ndarray is reversed along each dimension.", + "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", + "to-reversed", + "flip", + "numpy.flip" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-reversed/test/test.js b/lib/node_modules/@stdlib/ndarray/base/to-reversed/test/test.js new file mode 100644 index 00000000000..2e53be75ea5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-reversed/test/test.js @@ -0,0 +1,223 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 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 toReversed = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toReversed, '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 = toReversed( 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 = toReversed( 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( 'when provided a zero-dimensional input array, the function returns a new zero-dimensional array (non-base, offset, read-only)', function test( t ) { + var actual; + var x; + + x = new ctor( 'float64', typedarray( zeroTo( 4 ), 'float64' ), [], [ 0 ], 3, 'row-major' ); + + actual = toReversed( 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.strictEqual( isReadOnly( actual ), false, 'returns expected value' ); + + t.end(); +}); + +tape( 'when provided a zero-dimensional input array, the function returns a new zero-dimensional array (non-base, offset, writable)', function test( t ) { + var actual; + var x; + + x = new ctor( 'float64', typedarray( zeroTo( 4 ), 'float64' ), [], [ 0 ], 3, 'row-major' ); + + actual = toReversed( 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.strictEqual( isReadOnly( actual ), false, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where the order of the elements of an input ndarray is reversed along each 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 = toReversed( 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 is reversed along each 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 = toReversed( 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 = [ + [ 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 new ndarray where the order of the elements of an input ndarray is reversed along each 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 = toReversed( 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 = [ + [ + [ 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(); +});