All files main.js

100% Statements 138/138
100% Branches 18/18
100% Functions 1/1
100% Lines 138/138

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 1391x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 4x 4x 70x 74x 2x 2x 68x 68x 68x 68x 68x 74x 6x 6x 74x 6x 6x 56x 56x 56x 56x 56x 74x 3x 3x 56x 56x 56x 56x 56x 56x 56x 74x 23x 23x 23x 23x 23x 23x 33x 33x 13x 13x 13x 13x 20x 20x 11x 11x 11x 11x 11x 11x 56x 56x 56x 74x 1x 1x 1x 1x 1x  
/**
* @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 toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
var getDType = require( '@stdlib/ndarray/base/dtype' );
var getShape = require( '@stdlib/ndarray/base/shape' );
var getStrides = require( '@stdlib/ndarray/base/strides' );
var getOffset = require( '@stdlib/ndarray/base/offset' );
var getOrder = require( '@stdlib/ndarray/base/order' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var join = require( '@stdlib/array/base/join' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Rotates an ndarray 90 degrees in a specified plane.
*
* ## Notes
*
* -   If `k > 0`, the function rotates the plane from the first specified dimension toward the second specified dimension. This means that, for a two-dimensional ndarray and `dims = [0, 1]`, the function rotates the plane counterclockwise.
* -   If `k < 0`, the function rotates the plane from the second specified dimension toward the first specified dimension. This means that, for a two-dimensional ndarray and `dims = [1, 0]`, the function rotates the plane clockwise.
* -   Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
*
* @param {ndarray} x - input array
* @param {IntegerArray} dims - dimension indices defining the plane of rotation
* @param {integer} k - number of times to rotate by 90 degrees
* @param {boolean} writable - boolean indicating whether the returned ndarray should be writable
* @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, 2 ], [ 3, 4 ] ] );
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
*
* var y = rot90( x, [ 0, 1 ], 1, false );
* // returns <ndarray>[ [ 2, 4 ], [ 1, 3 ] ]
*/
function rot90( x, dims, k, writable ) {
	var offset;
	var sd0;
	var sd1;
	var sh;
	var st;
	var d0;
	var d1;
	var M;
	var N;
	var d;
 
	if ( dims.length !== 2 ) {
		throw new RangeError( format( 'invalid argument. Must provide exactly two dimension indices. Value: `[%s]`.', join( dims, ', ' ) ) );
	}
	sh = getShape( x, true );
	if ( sh.length < 2 ) {
		throw new RangeError( format( 'invalid argument. Input ndarray must have at least two dimensions. Number of dimensions: %d.', sh.length ) );
	}
	st = getStrides( x, true );
	offset = getOffset( x );
 
	// Resolve dimension indices...
	d = toUniqueNormalizedIndices( dims, sh.length-1 );
	if ( d === null ) {
		throw new RangeError( format( 'invalid argument. Specified dimension index is out-of-bounds. Must be on the interval: [-%u, %u]. Value: `[%s]`.', sh.length, sh.length-1, join( dims, ', ' ) ) );
	}
	if ( d.length !== dims.length ) {
		throw new Error( format( 'invalid argument. Must provide unique dimension indices. Value: `[%s]`.', join( dims, ', ' ) ) );
	}
	d0 = d[ 0 ];
	d1 = d[ 1 ];
 
	// Normalize `k` to the interval [0, 3]:
	k %= 4;
	if ( k < 0 ) {
		k += 4;
	}
	// Cache the original shape and stride values for the rotation plane:
	M = sh[ d0 ];
	N = sh[ d1 ];
	sd0 = st[ d0 ];
	sd1 = st[ d1 ];
 
	// Case: rotate 90 deg counterclockwise
	if ( k === 1 ) {
		sh[ d0 ] = N;
		sh[ d1 ] = M;
		st[ d0 ] = -sd1;
		st[ d1 ] = sd0;
		offset += ( N - 1 ) * sd1;
	}
	// Case: rotate 180 deg (i.e., reverse both dimensions in the rotation plane)
	else if ( k === 2 ) {
		st[ d0 ] = -sd0;
		st[ d1 ] = -sd1;
		offset += ( ( M - 1 ) * sd0 ) + ( ( N - 1 ) * sd1 );
	}
	// Case: rotate 270 deg counterclockwise (i.e., 90 deg clockwise)
	else if ( k === 3 ) {
		sh[ d0 ] = N;
		sh[ d1 ] = M;
		st[ d0 ] = sd1;
		st[ d1 ] = -sd0;
		offset += ( M - 1 ) * sd0;
	}
	return new x.constructor( getDType( x ), getData( x ), sh, st, offset, getOrder( x ), { // eslint-disable-line max-len
		'readonly': !writable
	});
}
 
 
// EXPORTS //
 
module.exports = rot90;