From 983eafd3bbfca27598f72beb7bfdfc5944765ee7 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Sat, 25 Apr 2026 18:30:13 +0500 Subject: [PATCH] feat: add ndarray/base/to-rotl90 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/ndarray/base/to-rotl90/README.md | 152 +++++ .../base/to-rotl90/benchmark/benchmark.js | 272 +++++++++ .../ndarray/base/to-rotl90/docs/repl.txt | 38 ++ .../base/to-rotl90/docs/types/index.d.ts | 46 ++ .../ndarray/base/to-rotl90/docs/types/test.ts | 76 +++ .../ndarray/base/to-rotl90/examples/index.js | 51 ++ .../ndarray/base/to-rotl90/lib/index.js | 44 ++ .../ndarray/base/to-rotl90/lib/main.js | 65 +++ .../ndarray/base/to-rotl90/package.json | 66 +++ .../ndarray/base/to-rotl90/test/test.js | 538 ++++++++++++++++++ 10 files changed, 1348 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/to-rotl90/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/README.md b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/README.md new file mode 100644 index 000000000000..c5236cf3ef5f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/README.md @@ -0,0 +1,152 @@ + + +# toRotl90 + +> Return a new ndarray where a matrix (or a stack of matrices) is rotated 90 degrees counterclockwise. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var toRotl90 = require( '@stdlib/ndarray/base/to-rotl90' ); +``` + +#### toRotl90( x, k ) + +Returns a new ndarray where a matrix (or a stack of matrices) is rotated 90 degrees counterclockwise. + +```javascript +var array = require( '@stdlib/ndarray/array' ); + +var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +// returns [ [ 1, 2 ], [ 3, 4 ] ] + +var y = toRotl90( x, 1 ); +// returns [ [ 2, 4 ], [ 1, 3 ] ] +``` + +The function accepts the following arguments: + +- **x**: input ndarray. +- **k**: number of times to rotate by 90 degrees. Positive values rotate counterclockwise. Negative values rotate clockwise. + +
+ + + + + +
+ +## Notes + +- If `k > 0`, the function rotates the matrix counterclockwise. +- If `k < 0`, the function rotates the matrix clockwise. +- If provided an ndarray with fewer than two dimensions, the function does not perform a rotation and simply returns a copy of the input ndarray. + +
+ + + + + +
+ +## Examples + + + +```javascript +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toRotl90 = require( '@stdlib/ndarray/base/to-rotl90' ); + +// Create a 2x3 matrix: +var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); + +// Rotate 90 degrees counterclockwise: +var y = toRotl90( x, 1 ); +var arr = ndarray2array( y ); +// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ] + +// Rotate 180 degrees: +y = toRotl90( x, 2 ); +arr = ndarray2array( y ); +// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ] + +// Rotate 270 degrees counterclockwise (equivalent to 90 degrees clockwise): +y = toRotl90( x, 3 ); +arr = ndarray2array( y ); +// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ] + +// Rotate 360 degrees (equivalent to no rotation): +y = toRotl90( x, 4 ); +arr = ndarray2array( y ); +// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + +// Rotate 90 degrees clockwise (equivalent to k=3): +y = toRotl90( x, -1 ); +arr = ndarray2array( y ); +// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ] +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/benchmark/benchmark.js new file mode 100644 index 000000000000..b71a1a7b1b72 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/benchmark/benchmark.js @@ -0,0 +1,272 @@ +/** +* @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 baseEmpty = require( '@stdlib/ndarray/base/empty' ); +var empty = require( '@stdlib/ndarray/empty' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var toRotl90 = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::2d,base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::2d,non-base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::3d,non-base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::4d,base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::4d,non-base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::5d,base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::5d,non-base', pkg ), function benchmark( b ) { + var values; + var out; + 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++ ) { + out = toRotl90( values[ i%values.length ], 1 ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/repl.txt new file mode 100644 index 000000000000..d9eadc3c933b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/repl.txt @@ -0,0 +1,38 @@ + +{{alias}}( x, k ) + Returns a new ndarray where a matrix (or a stack of matrices) is rotated 90 + degrees counterclockwise. + + If `k > 0`, the function rotates the matrix counterclockwise. If `k < 0`, + the function rotates the matrix clockwise. + + If provided an ndarray with fewer than two dimensions, the function does not + perform a rotation and simply returns a copy of the input ndarray. + + Parameters + ---------- + x: ndarray + Input array. + + k: integer + Number of times to rotate by 90 degrees. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] ) + [ [ 1, 2 ], [ 3, 4 ] ] + > var y = {{alias}}( x, 1 ) + [ [ 2, 4 ], [ 1, 3 ] ] + > y = {{alias}}( x, 2 ) + [ [ 4, 3 ], [ 2, 1 ] ] + > y = {{alias}}( x, 3 ) + [ [ 3, 1 ], [ 4, 2 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/types/index.d.ts new file mode 100644 index 000000000000..b41653631731 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/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 { typedndarray } from '@stdlib/types/ndarray'; + +/** +* Returns a new ndarray where a matrix (or a stack of matrices) is rotated 90 degrees counterclockwise. +* +* @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, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = toRotl90( x, 1 ); +* // returns [ [ 2, 4 ], [ 1, 3 ] ] +*/ +declare function toRotl90 = typedndarray>( x: U, k: number ): U; + + +// EXPORTS // + +export = toRotl90; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/types/test.ts new file mode 100644 index 000000000000..ba518b9e5c42 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/docs/types/test.ts @@ -0,0 +1,76 @@ +/* +* @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 zeros = require( '@stdlib/ndarray/base/zeros' ); +import toRotl90 = require( './index' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + toRotl90( zeros( 'float64', sh, ord ), 1 ); // $ExpectType float64ndarray + toRotl90( zeros( 'float32', sh, ord ), 1 ); // $ExpectType float32ndarray + toRotl90( zeros( 'complex128', sh, ord ), 1 ); // $ExpectType complex128ndarray + toRotl90( zeros( 'complex64', sh, ord ), 1 ); // $ExpectType complex64ndarray + toRotl90( zeros( 'int32', sh, ord ), 1 ); // $ExpectType int32ndarray + toRotl90( zeros( 'int16', sh, ord ), 1 ); // $ExpectType int16ndarray + toRotl90( zeros( 'int8', sh, ord ), 1 ); // $ExpectType int8ndarray + toRotl90( zeros( 'uint32', sh, ord ), 1 ); // $ExpectType uint32ndarray + toRotl90( zeros( 'uint16', sh, ord ), 1 ); // $ExpectType uint16ndarray + toRotl90( zeros( 'uint8', sh, ord ), 1 ); // $ExpectType uint8ndarray + toRotl90( zeros( 'uint8c', sh, ord ), 1 ); // $ExpectType uint8cndarray + toRotl90( zeros( 'generic', sh, ord ), 1 ); // $ExpectType genericndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + toRotl90( '10', 1 ); // $ExpectError + toRotl90( 10, 1 ); // $ExpectError + toRotl90( false, 1 ); // $ExpectError + toRotl90( true, 1 ); // $ExpectError + toRotl90( null, 1 ); // $ExpectError + toRotl90( void 0, 1 ); // $ExpectError + toRotl90( [], 1 ); // $ExpectError + toRotl90( {}, 1 ); // $ExpectError + toRotl90( ( 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 = zeros( 'float64', [ 2, 2 ], 'row-major' ); + + toRotl90( x, '10' ); // $ExpectError + toRotl90( x, false ); // $ExpectError + toRotl90( x, true ); // $ExpectError + toRotl90( x, null ); // $ExpectError + toRotl90( x, void 0 ); // $ExpectError + toRotl90( x, [] ); // $ExpectError + toRotl90( x, {} ); // $ExpectError + toRotl90( x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + toRotl90(); // $ExpectError + toRotl90( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + toRotl90( zeros( 'float64', [ 2, 2 ], 'row-major' ), 1, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/examples/index.js new file mode 100644 index 000000000000..56d85fc1dd32 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/examples/index.js @@ -0,0 +1,51 @@ +/** +* @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 array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var toRotl90 = require( './../lib' ); + +// Create a 2x3 matrix: +var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ); + +// Rotate 90 degrees counterclockwise: +var y = toRotl90( x, 1 ); +console.log( ndarray2array( y ) ); +// => [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ] + +// Rotate 180 degrees: +y = toRotl90( x, 2 ); +console.log( ndarray2array( y ) ); +// => [ [ 6, 5, 4 ], [ 3, 2, 1 ] ] + +// Rotate 270 degrees counterclockwise: +y = toRotl90( x, 3 ); +console.log( ndarray2array( y ) ); +// => [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ] + +// Rotate 360 degrees: +y = toRotl90( x, 4 ); +console.log( ndarray2array( y ) ); +// => [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] + +// Rotate 90 degrees clockwise (equivalent to k=3): +y = toRotl90( x, -1 ); +console.log( ndarray2array( y ) ); +// => [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ] diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/lib/index.js new file mode 100644 index 000000000000..831e322b3b48 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/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 new ndarray where a matrix (or a stack of matrices) is rotated 90 degrees counterclockwise. +* +* @module @stdlib/ndarray/base/to-rotl90 +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* var toRotl90 = require( '@stdlib/ndarray/base/to-rotl90' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = toRotl90( x, 1 ); +* // returns [ [ 2, 4 ], [ 1, 3 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/lib/main.js new file mode 100644 index 000000000000..0cb34d183380 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/lib/main.js @@ -0,0 +1,65 @@ +/** +* @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 emptyLike = require( '@stdlib/ndarray/base/empty-like' ); +var rotl90 = require( '@stdlib/ndarray/base/rotl90' ); +var assign = require( '@stdlib/ndarray/base/assign' ); + + +// MAIN // + +/** +* Returns a new ndarray where a matrix (or a stack of matrices) is rotated 90 degrees counterclockwise. +* +* @param {ndarray} x - input array +* @param {integer} k - number of times to rotate by 90 degrees +* @returns {ndarray} output array +* +* @example +* var array = require( '@stdlib/ndarray/array' ); +* +* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); +* // returns [ [ 1, 2 ], [ 3, 4 ] ] +* +* var y = toRotl90( x, 1 ); +* // returns [ [ 2, 4 ], [ 1, 3 ] ] +*/ +function toRotl90( x, k ) { + var out; + var xr; + + // Create a rotated view of the input ndarray: + xr = rotl90( x, k, false ); + + // Create an output ndarray having the same shape and data type as the rotated view: + out = emptyLike( xr ); + + // Assign the elements of the rotated view to the output ndarray: + assign( [ xr, out ] ); + + return out; +} + + +// EXPORTS // + +module.exports = toRotl90; diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/package.json b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/package.json new file mode 100644 index 000000000000..8e6440d42d9f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/package.json @@ -0,0 +1,66 @@ +{ + "name": "@stdlib/ndarray/base/to-rotl90", + "version": "0.0.0", + "description": "Return a new ndarray where a matrix (or a stack of matrices) is rotated 90 degrees counterclockwise.", + "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", + "ndarray", + "matrix", + "rotate", + "rotation", + "rotl90", + "counterclockwise", + "numpy.rot90" + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/to-rotl90/test/test.js b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/test/test.js new file mode 100644 index 000000000000..6d8e67d45df5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/to-rotl90/test/test.js @@ -0,0 +1,538 @@ +/** +* @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 Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var Complex128Array = require( '@stdlib/array/complex128' ); +var Complex64Array = require( '@stdlib/array/complex64' ); +var real = require( '@stdlib/complex/float64/real' ); +var imag = require( '@stdlib/complex/float64/imag' ); +var realf = require( '@stdlib/complex/float32/real' ); +var imagf = require( '@stdlib/complex/float32/imag' ); +var instanceOf = require( '@stdlib/assert/instance-of' ); +var base = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var array = require( '@stdlib/ndarray/array' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var toRotl90 = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof toRotl90, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'when provided a zero-dimensional input array, the function returns a new zero-dimensional array', function test( t ) { + var actual; + var buf; + var x; + + buf = new Float64Array( [ 3.14 ] ); + x = new base( 'float64', buf, [], [ 0 ], 0, 'row-major' ); + + actual = toRotl90( x, 1 ); + t.strictEqual( instanceOf( actual, base ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.strictEqual( actual.get(), x.get(), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + + t.end(); +}); + +tape( 'when provided a one-dimensional input array, the function returns a new one-dimensional array', function test( t ) { + var expected; + var actual; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 6 ], [ 1 ], 0, 'row-major' ); + + actual = toRotl90( x, 1 ); + t.strictEqual( instanceOf( actual, base ), true, 'returns expected value' ); + t.deepEqual( getShape( actual ), [ 6 ], 'returns expected value' ); + t.strictEqual( String( getDType( actual ) ), String( getDType( x ) ), 'returns expected value' ); + t.notEqual( getData( actual ), getData( x ), 'returns expected value' ); + expected = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; + t.deepEqual( ndarray2array( actual ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where a matrix is rotated counterclockwise for all values of k (dtype=float64, base, row-major)', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + // k=0 (no rotation): + arr = toRotl90( x, 0 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 3 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + t.strictEqual( getOrder( arr ), getOrder( x ), 'returns expected value' ); + t.notEqual( arr, x, 'returns expected value' ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=1 (90 degrees counterclockwise): + arr = toRotl90( x, 1 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 3.0, 6.0 ], [ 2.0, 5.0 ], [ 1.0, 4.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=2 (180 degrees): + arr = toRotl90( x, 2 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 3 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 6.0, 5.0, 4.0 ], [ 3.0, 2.0, 1.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=3 (270 degrees counterclockwise = 90 degrees clockwise): + arr = toRotl90( x, 3 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where a matrix is rotated counterclockwise for all values of k (dtype=float64, base, column-major)', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 1, 2 ], 0, 'column-major' ); + + // In column-major with strides [1,2], x = [ [ 1.0, 3.0, 5.0 ], [ 2.0, 4.0, 6.0 ] ] + + // k=1: + arr = toRotl90( x, 1 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=2: + arr = toRotl90( x, 2 ); + t.deepEqual( getShape( arr ), [ 2, 3 ], 'returns expected value' ); + expected = [ [ 6.0, 4.0, 2.0 ], [ 5.0, 3.0, 1.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=3: + arr = toRotl90( x, 3 ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + expected = [ [ 2.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 5.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where a matrix is rotated counterclockwise for all values of k (dtype=float64, non-base)', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = array( buf, { + 'shape': [ 2, 3 ], + 'dtype': 'float64' + }); + + // k=0: + arr = toRotl90( x, 0 ); + t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=1: + arr = toRotl90( x, 1 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 3.0, 6.0 ], [ 2.0, 5.0 ], [ 1.0, 4.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=2: + arr = toRotl90( x, 2 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 6.0, 5.0, 4.0 ], [ 3.0, 2.0, 1.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=3: + arr = toRotl90( x, 3 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where a matrix is rotated counterclockwise for all values of k (dtype=float32)', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float32', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + // k=1: + arr = toRotl90( x, 1 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 3.0, 6.0 ], [ 2.0, 5.0 ], [ 1.0, 4.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=2: + arr = toRotl90( x, 2 ); + t.deepEqual( getShape( arr ), [ 2, 3 ], 'returns expected value' ); + expected = [ [ 6.0, 5.0, 4.0 ], [ 3.0, 2.0, 1.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=3: + arr = toRotl90( x, 3 ); + t.deepEqual( getShape( arr ), [ 3, 2 ], 'returns expected value' ); + expected = [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // Non-base, k=1: + x = array( buf, { + 'shape': [ 2, 3 ], + 'dtype': 'float32' + }); + arr = toRotl90( x, 1 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 3.0, 6.0 ], [ 2.0, 5.0 ], [ 1.0, 4.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where a matrix is rotated counterclockwise (dtype=complex128, base)', function test( t ) { + var arr; + var buf; + var v; + var x; + + buf = new Complex128Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0 ] ); + x = new base( 'complex128', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + // k=1 (90 degrees counterclockwise): + arr = toRotl90( x, 1 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + + v = arr.get( 0, 0 ); + t.strictEqual( real( v ), 2.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + v = arr.get( 0, 1 ); + t.strictEqual( real( v ), 4.0, 'returns expected value' ); + t.strictEqual( imag( v ), 4.0, 'returns expected value' ); + v = arr.get( 1, 0 ); + t.strictEqual( real( v ), 1.0, 'returns expected value' ); + t.strictEqual( imag( v ), 1.0, 'returns expected value' ); + v = arr.get( 1, 1 ); + t.strictEqual( real( v ), 3.0, 'returns expected value' ); + t.strictEqual( imag( v ), 3.0, 'returns expected value' ); + + // k=2: + arr = toRotl90( x, 2 ); + v = arr.get( 0, 0 ); + t.strictEqual( real( v ), 4.0, 'returns expected value' ); + t.strictEqual( imag( v ), 4.0, 'returns expected value' ); + v = arr.get( 0, 1 ); + t.strictEqual( real( v ), 3.0, 'returns expected value' ); + t.strictEqual( imag( v ), 3.0, 'returns expected value' ); + v = arr.get( 1, 0 ); + t.strictEqual( real( v ), 2.0, 'returns expected value' ); + t.strictEqual( imag( v ), 2.0, 'returns expected value' ); + v = arr.get( 1, 1 ); + t.strictEqual( real( v ), 1.0, 'returns expected value' ); + t.strictEqual( imag( v ), 1.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where a matrix is rotated counterclockwise (dtype=complex64, base)', function test( t ) { + var arr; + var buf; + var v; + var x; + + buf = new Complex64Array( [ 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0 ] ); + x = new base( 'complex64', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + // k=1 (90 degrees counterclockwise): + arr = toRotl90( x, 1 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.strictEqual( String( getDType( arr ) ), String( getDType( x ) ), 'returns expected value' ); + t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + + v = arr.get( 0, 0 ); + t.strictEqual( realf( v ), 2.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 2.0, 'returns expected value' ); + v = arr.get( 0, 1 ); + t.strictEqual( realf( v ), 4.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 4.0, 'returns expected value' ); + v = arr.get( 1, 0 ); + t.strictEqual( realf( v ), 1.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 1.0, 'returns expected value' ); + v = arr.get( 1, 1 ); + t.strictEqual( realf( v ), 3.0, 'returns expected value' ); + t.strictEqual( imagf( v ), 3.0, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a new ndarray where a matrix is rotated counterclockwise for all values of k (dtype=generic)', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = [ 1, 2, 3, 4, 5, 6 ]; + x = new base( 'generic', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + // k=0: + arr = toRotl90( x, 0 ); + t.strictEqual( instanceOf( arr, base ), true, 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=1: + arr = toRotl90( x, 1 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=2: + arr = toRotl90( x, 2 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=3: + arr = toRotl90( x, 3 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // Non-base, k=1: + x = array( buf, { + 'shape': [ 2, 3 ], + 'dtype': 'generic' + }); + arr = toRotl90( x, 1 ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative and large values of k', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + // k=-1 (90 degrees clockwise): + arr = toRotl90( x, -1 ); + expected = [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=-2: + arr = toRotl90( x, -2 ); + expected = [ [ 6.0, 5.0, 4.0 ], [ 3.0, 2.0, 1.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=-3: + arr = toRotl90( x, -3 ); + expected = [ [ 3.0, 6.0 ], [ 2.0, 5.0 ], [ 1.0, 4.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=-4: + arr = toRotl90( x, -4 ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=5 (same as k=1): + arr = toRotl90( x, 5 ); + expected = [ [ 3.0, 6.0 ], [ 2.0, 5.0 ], [ 1.0, 4.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=8 (same as k=0): + arr = toRotl90( x, 8 ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=-5 (same as k=-1): + arr = toRotl90( x, -5 ); + expected = [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=-8 (same as k=0): + arr = toRotl90( x, -8 ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a stack of matrices for all values of k', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]; + x = new base( 'generic', buf, [ 2, 2, 3 ], [ 6, 3, 1 ], 0, 'row-major' ); + + // k=0: + arr = toRotl90( x, 0 ); + t.deepEqual( getShape( arr ), [ 2, 2, 3 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ + [ [ 1, 2, 3 ], [ 4, 5, 6 ] ], + [ [ 7, 8, 9 ], [ 10, 11, 12 ] ] + ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=1: + arr = toRotl90( x, 1 ); + t.deepEqual( getShape( arr ), [ 2, 3, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ + [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ], + [ [ 9, 12 ], [ 8, 11 ], [ 7, 10 ] ] + ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=2: + arr = toRotl90( x, 2 ); + t.deepEqual( getShape( arr ), [ 2, 2, 3 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ + [ [ 6, 5, 4 ], [ 3, 2, 1 ] ], + [ [ 12, 11, 10 ], [ 9, 8, 7 ] ] + ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=3: + arr = toRotl90( x, 3 ); + t.deepEqual( getShape( arr ), [ 2, 3, 2 ], 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + expected = [ + [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ], + [ [ 10, 7 ], [ 11, 8 ], [ 12, 9 ] ] + ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports a non-zero offset', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = new Float64Array( [ 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 3, 1 ], 2, 'row-major' ); + + // k=0: + arr = toRotl90( x, 0 ); + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=1: + arr = toRotl90( x, 1 ); + expected = [ [ 3.0, 6.0 ], [ 2.0, 5.0 ], [ 1.0, 4.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=2: + arr = toRotl90( x, 2 ); + expected = [ [ 6.0, 5.0, 4.0 ], [ 3.0, 2.0, 1.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + // k=3: + arr = toRotl90( x, 3 ); + expected = [ [ 4.0, 1.0 ], [ 5.0, 2.0 ], [ 6.0, 3.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'rotating four times returns the original arrangement', function test( t ) { + var expected; + var arr; + var buf; + var x; + + buf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); + x = new base( 'float64', buf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' ); + + arr = toRotl90( x, 1 ); + arr = toRotl90( arr, 1 ); + arr = toRotl90( arr, 1 ); + arr = toRotl90( arr, 1 ); + + expected = [ [ 1.0, 2.0, 3.0 ], [ 4.0, 5.0, 6.0 ] ]; + t.deepEqual( ndarray2array( arr ), expected, 'returns expected value' ); + t.notEqual( getData( arr ), getData( x ), 'returns expected value' ); + + t.end(); +});