diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/README.md b/lib/node_modules/@stdlib/ndarray/rotr90/README.md new file mode 100644 index 000000000000..b4909c9013ab --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/README.md @@ -0,0 +1,132 @@ + + +# rotr90 + +> Return a **read-only** view of a matrix (or a stack of matrices) rotated `90` degrees clockwise. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var rotr90 = require( '@stdlib/ndarray/rotr90' ); +``` + +#### rotr90( x, k ) + +Returns a read-only view of a matrix (or a stack of matrices) rotated `90` degrees clockwise. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] ); +// returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] + +var y = rotr90( x, 1 ); +// returns [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ] +``` + +The function accepts the following arguments: + +- **x**: input [`ndarray`][@stdlib/ndarray/ctor]. +- **k**: number of times to rotate by `90` degrees. Positive values rotate clockwise. Negative values rotate counterclockwise. + +
+ + + + + +
+ +## Notes + +- If `k > 0`, the function rotates the matrix clockwise. +- If `k < 0`, the function rotates the matrix counterclockwise. +- If provided an [`ndarray`][@stdlib/ndarray/ctor] with fewer than two dimensions, the function does not perform rotation and simply returns a new view of the input [`ndarray`][@stdlib/ndarray/ctor]. + +
+ + + + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var rotr90 = require( '@stdlib/ndarray/rotr90' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = rotr90( x, 1 ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/rotr90/benchmark/benchmark.js new file mode 100644 index 000000000000..868b19067d73 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/benchmark/benchmark.js @@ -0,0 +1,223 @@ +/** +* @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 rotr90 = require( './../lib' ); + + +// MAIN // + +bench( format( '%s:ndims=0', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [], { 'dtype': 'float64' } ), + empty( [], { 'dtype': 'float32' } ), + empty( [], { 'dtype': 'int32' } ), + empty( [], { 'dtype': 'complex128' } ), + empty( [], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = rotr90( values[ i%values.length ], 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s:ndims=1', pkg ), function benchmark( b ) { + var values; + var v; + var i; + + /* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + values = [ + empty( [ 2 ], { 'dtype': 'float64' } ), + empty( [ 2 ], { 'dtype': 'float32' } ), + empty( [ 2 ], { 'dtype': 'int32' } ), + empty( [ 2 ], { 'dtype': 'complex128' } ), + empty( [ 2 ], { 'dtype': 'generic' } ) + ]; + + /* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */ + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = rotr90( values[ i%values.length ], 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( 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 = rotr90( values[ i%values.length ], 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( 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 = rotr90( values[ i%values.length ], 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( 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 = rotr90( values[ i%values.length ], 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( 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 = rotr90( values[ i%values.length ], 1 ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( v ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/rotr90/docs/repl.txt new file mode 100644 index 000000000000..c87818e5f17e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( x, k ) + Returns a read-only view of a matrix (or a stack of matrices) rotated 90 + degrees clockwise. + + If `k > 0`, the function rotates the matrix clockwise. If `k < 0`, the + function rotates the matrix counterclockwise. + + If provided an ndarray with fewer than two dimensions, the function does not + perform rotation and simply returns a new view of the input ndarray. + + Parameters + ---------- + x: ndarray + Input array. + + k: integer + Number of times to rotate by 90 degrees. + + Returns + ------- + out: ndarray + A read-only view of a matrix (or a stack of matrices) rotated 90 degrees + clockwise. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ) + [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + > var y = {{alias}}( x, 1 ) + [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ] + + See Also + -------- diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/rotr90/docs/types/index.d.ts new file mode 100644 index 000000000000..3b1e802a1333 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/docs/types/index.d.ts @@ -0,0 +1,46 @@ +/* +* @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'; + +/** +* Returns a read-only view of a matrix (or a stack of matrices) rotated 90 degrees clockwise. +* +* @param x - input array +* @param k - number of times to rotate by 90 degrees +* @returns output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +* +* var y = rotr90( x, 1 ); +* // returns [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ] +*/ +declare function rotr90( x: T, k: number ): T; + + +// EXPORTS // + +export = rotr90; diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/rotr90/docs/types/test.ts new file mode 100644 index 000000000000..2920b0171986 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/docs/types/test.ts @@ -0,0 +1,67 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import empty = require( '@stdlib/ndarray/base/empty' ); +import rotr90 = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const order = 'row-major'; + const sh = [ 2, 2 ]; + + rotr90( empty( 'float64', sh, order ), 1 ); // $ExpectType float64ndarray + rotr90( empty( 'complex64', sh, order ), 1 ); // $ExpectType complex64ndarray + rotr90( empty( 'int32', sh, order ), -1 ); // $ExpectType int32ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + rotr90( '10', 1 ); // $ExpectError + rotr90( 10, 1 ); // $ExpectError + rotr90( false, 1 ); // $ExpectError + rotr90( true, 1 ); // $ExpectError + rotr90( null, 1 ); // $ExpectError + rotr90( [], 1 ); // $ExpectError + rotr90( {}, 1 ); // $ExpectError + rotr90( ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + rotr90( x, '10' ); // $ExpectError + rotr90( x, false ); // $ExpectError + rotr90( x, true ); // $ExpectError + rotr90( x, null ); // $ExpectError + rotr90( x, [] ); // $ExpectError + rotr90( x, {} ); // $ExpectError + rotr90( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = empty( 'float64', [ 2, 2 ], 'row-major' ); + + rotr90(); // $ExpectError + rotr90( x ); // $ExpectError + rotr90( x, 1, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/examples/index.js b/lib/node_modules/@stdlib/ndarray/rotr90/examples/index.js new file mode 100644 index 000000000000..ee3fc43a6e3a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/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 rotr90 = require( './../lib' ); + +var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 ); +console.log( ndarray2array( x ) ); + +var y = rotr90( x, 1 ); +console.log( ndarray2array( y ) ); diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/lib/index.js b/lib/node_modules/@stdlib/ndarray/rotr90/lib/index.js new file mode 100644 index 000000000000..daa24dd77086 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/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 a matrix (or a stack of matrices) rotated 90 degrees clockwise. +* +* @module @stdlib/ndarray/rotr90 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var rotr90 = require( '@stdlib/ndarray/rotr90' ); +* +* var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +* +* var y = rotr90( x, 1 ); +* // returns [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/lib/main.js b/lib/node_modules/@stdlib/ndarray/rotr90/lib/main.js new file mode 100644 index 000000000000..e9cbd19a03ac --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/lib/main.js @@ -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. +*/ + +'use strict'; + +// MODULES // + +var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var base = require( '@stdlib/ndarray/base/rotr90' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Returns a read-only view of a matrix (or a stack of matrices) rotated 90 degrees clockwise. +* +* @param {ndarray} x - input array +* @param {integer} k - number of times to rotate by 90 degrees +* @throws {TypeError} first argument must be an ndarray +* @throws {TypeError} second argument must be an integer +* @returns {ndarray} ndarray view +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] ); +* // returns [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ] +* +* var y = rotr90( x, 1 ); +* // returns [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ] +*/ +function rotr90( x, k ) { + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray. Value: `%s`.', x ) ); + } + if ( !isInteger( k ) ) { + throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', k ) ); + } + return base( x, k, false ); +} + + +// EXPORTS // + +module.exports = rotr90; diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/package.json b/lib/node_modules/@stdlib/ndarray/rotr90/package.json new file mode 100644 index 000000000000..7fd1951df2cd --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/rotr90", + "version": "0.0.0", + "description": "Rotate a matrix (or a stack of matrices) 90 degrees clockwise.", + "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", + "rotr90", + "clockwise" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/rotr90/test/test.js b/lib/node_modules/@stdlib/ndarray/rotr90/test/test.js new file mode 100644 index 000000000000..b850337c99a9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/rotr90/test/test.js @@ -0,0 +1,526 @@ +/** +* @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 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 rotr90 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof rotr90, '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() { + rotr90( value, 1 ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an integer', function test( t ) { + var values; + var x; + var i; + + x = new ndarray( 'float64', [ 1, 2, 3, 4 ], [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + rotr90( x, value ); + }; + } +}); + +tape( 'the function returns a read-only view of a zero-dimensional input ndarray', function test( t ) { + var actual; + var x; + + x = scalar2ndarray( 10.0, { + 'dtype': 'float64', + 'order': 'row-major' + }); + + actual = rotr90( x, 1 ); + + 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( actual.get(), x.get(), 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of a one-dimensional input ndarray (no rotation)', function test( t ) { + var expected; + var actual; + var buf; + var ord; + var sh; + var st; + var o; + var x; + + buf = zeroTo( 10, 'float64' ); + sh = [ 5 ]; + st = [ 2 ]; + o = 0; + ord = 'row-major'; + + x = new ndarray( 'float64', buf, sh, st, o, ord ); + + actual = rotr90( x, 1 ); + expected = [ 0, 2, 4, 6, 8 ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( x ), getShape( actual ), 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of a matrix rotated 90 degrees clockwise (k=1, 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 = rotr90( x, 1 ); + expected = [ + [ 3, 0 ], + [ 4, 1 ], + [ 5, 2 ] + ]; + + t.notEqual( actual, x, 'returns expected value' ); + t.strictEqual( isndarrayLike( actual ), true, 'returns expected value' ); + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.strictEqual( isEqualDataType( getDType( actual ), getDType( x ) ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of a matrix rotated 180 degrees (k=2, 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 = rotr90( x, 2 ); + 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.deepEqual( getShape( actual ), [ 2, 3 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of a matrix rotated 270 degrees clockwise (k=3, 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 = rotr90( x, 3 ); + expected = [ + [ 2, 5 ], + [ 1, 4 ], + [ 0, 3 ] + ]; + + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a read-only view of a matrix without rotation for k=0', 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 = rotr90( x, 0 ); + expected = [ + [ 0, 1, 2 ], + [ 3, 4, 5 ] + ]; + + 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 supports negative values of k (counterclockwise rotation)', 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 ); + + // k=-1 (90 deg counterclockwise, equivalent to k=3): + actual = rotr90( x, -1 ); + expected = [ + [ 2, 5 ], + [ 1, 4 ], + [ 0, 3 ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // k=-2 (equivalent to k=2): + actual = rotr90( x, -2 ); + expected = [ + [ 5, 4, 3 ], + [ 2, 1, 0 ] + ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // k=-3 (equivalent to k=1): + actual = rotr90( x, -3 ); + expected = [ + [ 3, 0 ], + [ 4, 1 ], + [ 5, 2 ] + ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function normalizes large values of k', 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 ); + + // k=5 (same as k=1): + actual = rotr90( x, 5 ); + expected = [ + [ 3, 0 ], + [ 4, 1 ], + [ 5, 2 ] + ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // k=8 (same as k=0): + actual = rotr90( x, 8 ); + expected = [ + [ 0, 1, 2 ], + [ 3, 4, 5 ] + ]; + 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 ); + + // k=1 (90 deg clockwise): + actual = rotr90( x, 1 ); + expected = [ + [ + [ 3, 0 ], + [ 4, 1 ], + [ 5, 2 ] + ], + [ + [ 9, 6 ], + [ 10, 7 ], + [ 11, 8 ] + ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 3, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + // k=2 (180 deg): + actual = rotr90( x, 2 ); + expected = [ + [ + [ 5, 4, 3 ], + [ 2, 1, 0 ] + ], + [ + [ 11, 10, 9 ], + [ 8, 7, 6 ] + ] + ]; + 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' ); + + // k=-1 (90 deg counterclockwise): + actual = rotr90( x, -1 ); + expected = [ + [ + [ 2, 5 ], + [ 1, 4 ], + [ 0, 3 ] + ], + [ + [ 8, 11 ], + [ 7, 10 ], + [ 6, 9 ] + ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 2, 3, 2 ], '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 = rotr90( x, 1 ); + expected = [ + [ 1, 0 ], + [ 3, 2 ], + [ 5, 4 ] + ]; + t.strictEqual( isReadOnly( actual ), true, 'returns expected value' ); + t.strictEqual( getData( actual ), getData( x ), 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + actual = rotr90( x, 2 ); + expected = [ + [ 5, 3, 1 ], + [ 4, 2, 0 ] + ]; + 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 = rotr90( x, 1 ); + expected = [ + [ 3, 0 ], + [ 4, 1 ], + [ 5, 2 ] + ]; + + 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 ), [ 3, 2 ], 'returns expected value' ); + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +});