diff --git a/lib/node_modules/@stdlib/ndarray/rot180/README.md b/lib/node_modules/@stdlib/ndarray/rot180/README.md new file mode 100644 index 000000000000..78702e37a334 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/README.md @@ -0,0 +1,134 @@ + + +# rot180 + +> Return a **read-only** view of an input ndarray rotated `180` degrees in a specified plane. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var rot180 = require( '@stdlib/ndarray/rot180' ); +``` + +#### rot180( x\[, options] ) + +Returns a read-only view of an input [`ndarray`][@stdlib/ndarray/ctor] rotated `180` degrees in a specified plane. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] + +var y = rot180( x ); +// returns [ [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **options**: function options. + +The function accepts the following options: + +- **dims**: dimension indices defining the plane of rotation. Must contain exactly two unique dimension indices. If a dimension index is provided as an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. Default: `[-2, -1]`. + +
+ + + + + +
+ +## Notes + +- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var rot180 = require( '@stdlib/ndarray/rot180' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = rot180( x ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/rot180/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/rot180/benchmark/benchmark.js new file mode 100644 index 000000000000..07164df9a1a7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/benchmark/benchmark.js @@ -0,0 +1,196 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var rot180 = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ndims=2', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2 ], { '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 = rot180( 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( format( '%s:ndims=3', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ), + empty( [ 2, 2, 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = rot180( 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( format( '%s:ndims=3,dims=[0,2]', pkg ), function benchmark( b ) { + var values; + var opts; + 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 */ + + opts = { + 'dims': [ 0, 2 ] + }; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = rot180( values[ i%values.length ], opts ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ndims=4', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2, 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, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = rot180( 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( format( '%s:ndims=5', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2, 2, 2, 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, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = rot180( 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/rot180/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/rot180/docs/repl.txt new file mode 100644 index 000000000000..1353318c214f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/docs/repl.txt @@ -0,0 +1,35 @@ + +{{alias}}( x[, options] ) + Returns a read-only view of an input ndarray rotated 180 degrees in a + specified plane. + + Each provided dimension index must reside on the interval [-ndims, ndims-1]. + + Parameters + ---------- + x: ndarray + Input array. + + options: Object (optional) + Function options. + + options.dims: ArrayLikeObject (optional) + Dimension indices defining the plane of rotation. Must contain exactly + two unique dimension indices. If less than zero, an index is resolved + relative to the last dimension, with the last dimension corresponding to + the value `-1`. Default: [ -2, -1 ]. + + Returns + ------- + out: ndarray + Output array view. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x ) + [ [ 4, 3 ], [ 2, 1 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/rot180/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/rot180/docs/types/index.d.ts new file mode 100644 index 000000000000..274fc2263a26 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/docs/types/index.d.ts @@ -0,0 +1,62 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ndarray } from '@stdlib/types/ndarray'; +import { Collection } from '@stdlib/types/array'; + +/** +* Interface defining function options. +*/ +interface Options { + /** + * Dimension indices defining the plane of rotation (default: [-2, -1]). + */ + dims?: Collection; +} + +/** +* Returns a read-only view of an input ndarray rotated 180 degrees in a specified plane. +* +* ## Notes +* +* - Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. +* +* @param x - input array +* @param options - function options +* @param options.dims - dimension indices defining the plane of rotation (default: [-2, -1]) +* @returns output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = rot180( x ); +* // returns [ [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +declare function rot180( x: T, options?: Options ): T; + + +// EXPORTS // + +export = rot180; diff --git a/lib/node_modules/@stdlib/ndarray/rot180/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/rot180/docs/types/test.ts new file mode 100644 index 000000000000..a3ebe50bfa6c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/docs/types/test.ts @@ -0,0 +1,78 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import rot180 = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + rot180( zeros( [ 2, 2 ] ) ); // $ExpectType float64ndarray + rot180( zeros( [ 2, 2 ] ), {} ); // $ExpectType float64ndarray + rot180( zeros( [ 2, 2 ] ), { 'dims': [ 0, 1 ] } ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + rot180( '10' ); // $ExpectError + rot180( 10 ); // $ExpectError + rot180( false ); // $ExpectError + rot180( true ); // $ExpectError + rot180( null ); // $ExpectError + rot180( void 0 ); // $ExpectError + rot180( [] ); // $ExpectError + rot180( {} ); // $ExpectError + rot180( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an object... +{ + const x = zeros( [ 2, 2 ] ); + + rot180( x, '10' ); // $ExpectError + rot180( x, 10 ); // $ExpectError + rot180( x, false ); // $ExpectError + rot180( x, true ); // $ExpectError + rot180( x, null ); // $ExpectError + rot180( x, [] ); // $ExpectError + rot180( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dims` option which is not a collection of numbers... +{ + const x = zeros( [ 2, 2 ] ); + + rot180( x, { 'dims': '10' } ); // $ExpectError + rot180( x, { 'dims': 10 } ); // $ExpectError + rot180( x, { 'dims': false } ); // $ExpectError + rot180( x, { 'dims': true } ); // $ExpectError + rot180( x, { 'dims': null } ); // $ExpectError + rot180( x, { 'dims': {} } ); // $ExpectError + rot180( x, { 'dims': [ '0', '1' ] } ); // $ExpectError + rot180( x, { 'dims': ( x: number ): number => x } ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + rot180(); // $ExpectError + rot180( zeros( [ 2, 2 ] ), {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/rot180/examples/index.js b/lib/node_modules/@stdlib/ndarray/rot180/examples/index.js new file mode 100644 index 000000000000..bdd31c5b59cb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/examples/index.js @@ -0,0 +1,29 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var rot180 = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = rot180( x ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/rot180/lib/index.js b/lib/node_modules/@stdlib/ndarray/rot180/lib/index.js new file mode 100644 index 000000000000..c5384357b3f4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Return a read-only view of an input ndarray rotated 180 degrees in a specified plane. +* +* @module @stdlib/ndarray/rot180 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var rot180 = require( '@stdlib/ndarray/rot180' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = rot180( x ); +* // returns [ [ 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/rot180/lib/main.js b/lib/node_modules/@stdlib/ndarray/rot180/lib/main.js new file mode 100644 index 000000000000..80e6b56bd97a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/lib/main.js @@ -0,0 +1,85 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var isIntegerArray = require( '@stdlib/assert/is-integer-array' ).primitives; +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var base = require( '@stdlib/ndarray/base/rot180' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of an input ndarray rotated 180 degrees in a specified plane. +* +* ## Notes +* +* - Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. +* +* @param {ndarray} x - input array +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims=[-2,-1]] - dimension indices defining the plane of rotation +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} options argument must be an object +* @throws {TypeError} `dims` option must be an array of integers +* @throws {RangeError} must provide exactly two dimension indices +* @throws {RangeError} input ndarray must have at least two dimensions +* @throws {RangeError} must provide valid dimension indices +* @throws {Error} must provide unique dimension indices +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); +* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] +* +* var y = rot180( x ); +* // returns [ [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] +*/ +function rot180( x, options ) { + var dims; + + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + dims = [ -2, -1 ]; + if ( arguments.length > 1 ) { + if ( !isPlainObject( options ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); + } + if ( hasOwnProp( options, 'dims' ) ) { + if ( !isIntegerArray( options.dims ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be an array of integers. Option: `%s`.', 'dims', options.dims ) ); + } + dims = options.dims; + } + } + return base( x, dims, false ); +} + + +// EXPORTS // + +module.exports = rot180; diff --git a/lib/node_modules/@stdlib/ndarray/rot180/package.json b/lib/node_modules/@stdlib/ndarray/rot180/package.json new file mode 100644 index 000000000000..9b1e2204e68c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/rot180", + "version": "0.0.0", + "description": "Return a read-only view of an input ndarray rotated 180 degrees in a specified plane.", + "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", + "ndarray", + "matrix", + "view", + "rotate", + "rotation", + "rot180", + "plane", + "axis" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/rot180/test/test.js b/lib/node_modules/@stdlib/ndarray/rot180/test/test.js new file mode 100644 index 000000000000..7e15adb0d3bc --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rot180/test/test.js @@ -0,0 +1,536 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isReadOnly = require( '@stdlib/ndarray/base/assert/is-read-only' ); +var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' ); +var zeroTo = require( '@stdlib/array/zero-to' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var rot180 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof rot180, '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() { + rot180( value ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an object', 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', + 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() { + rot180( x, value ); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array-like object', 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() { + rot180( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which is not an array of integers', 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 = [ + [ '0', '1' ], + [ 0.5, 1.5 ], + [ NaN, NaN ], + [ null, null ], + [ true, false ], + [ {}, {} ] + ]; + + 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() { + rot180( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option which does not contain exactly two dimension indices', 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 = [ + [ 0 ], + [ 0, 1, 0 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided dimension indices [' + values[ i ] + ']' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + rot180( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option having out-of-bounds dimension indices', 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 = [ + [ 0, 2 ], + [ 2, 0 ], + [ -3, 0 ], + [ 0, -3 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided dimension indices [' + values[ i ] + ']' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + rot180( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided a `dims` option having duplicate dimension indices', 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 = [ + [ 0, 0 ], + [ 1, 1 ], + [ 0, -2 ], + [ -1, 1 ] + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), Error, 'throws an error when provided dimension indices [' + values[ i ] + ']' ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + rot180( x, { + 'dims': value + }); + }; + } +}); + +tape( 'the function throws an error if provided an input ndarray having fewer than two dimensions', function test( t ) { + var values; + var i; + + values = [ + scalar2ndarray( 5.0 ), + array( [ 1.0, 2.0, 3.0 ] ) + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided an ndarray having ' + values[ i ].ndims + ' dimensions' ); + } + t.end(); + + function badValue( x ) { + return function badValue() { + rot180( x ); + }; + } +}); + +tape( 'the function returns a read-only view of a matrix rotated 180 degrees (ndims=2)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 6, 'float64' ); + sh = [ 2, 3 ]; + st = [ 3, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = rot180( x ); + expected = [ + [ 5, 4, 3 ], + [ 2, 1, 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( actual ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns the original arrangement when applied twice (involution)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 6, 'float64' ); + sh = [ 2, 3 ]; + st = [ 3, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = rot180( rot180( x ) ); + expected = [ + [ 0, 1, 2 ], + [ 3, 4, 5 ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a `dims` option', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 6, 'float64' ); + sh = [ 2, 3 ]; + st = [ 3, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + // dims=[-2,-1] (same as default): + actual = rot180( x, { + 'dims': [ -2, -1 ] + }); + expected = [ + [ 5, 4, 3 ], + [ 2, 1, 0 ] + ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // dims=[1,0] (swapping the order of axes does not change rot180): + actual = rot180( x, { + 'dims': [ 1, 0 ] + }); + expected = [ + [ 5, 4, 3 ], + [ 2, 1, 0 ] + ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a stack of matrices (ndims=3)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 12, 'float64' ); + sh = [ 2, 2, 3 ]; + st = [ 6, 3, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = rot180( x ); + expected = [ + [ + [ 5, 4, 3 ], + [ 2, 1, 0 ] + ], + [ + [ 11, 10, 9 ], + [ 8, 7, 6 ] + ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports rotation in arbitrary planes (ndims=3)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 12, 'float64' ); + sh = [ 2, 2, 3 ]; + st = [ 6, 3, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + // dims=[0,1]: + actual = rot180( x, { + 'dims': [ 0, 1 ] + }); + expected = [ + [ + [ 9, 10, 11 ], + [ 6, 7, 8 ] + ], + [ + [ 3, 4, 5 ], + [ 0, 1, 2 ] + ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // dims=[0,2]: + actual = rot180( x, { + 'dims': [ 0, 2 ] + }); + expected = [ + [ + [ 8, 7, 6 ], + [ 11, 10, 9 ] + ], + [ + [ 2, 1, 0 ], + [ 5, 4, 3 ] + ] + ]; + t.deepEqual( getShape( actual ), [ 2, 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports column-major input arrays', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 6, 'float64' ); + sh = [ 2, 3 ]; + st = [ 1, 2 ]; + o = 0; + ord = 'column-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + // In column-major with strides [ 1, 2 ], x = [ [ 0, 2, 4 ], [ 1, 3, 5 ] ]: + actual = rot180( x ); + expected = [ + [ 5, 3, 1 ], + [ 4, 2, 0 ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function preserves the input data type (dtype=int32)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 6, 'int32' ); + sh = [ 2, 3 ]; + st = [ 3, 1 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'int32', buf, sh, st, o, ord ); + + actual = rot180( x ); + expected = [ + [ 5, 4, 3 ], + [ 2, 1, 0 ] + ]; + + 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( actual ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +});