Skip to content

Commit 7368bff

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/rot90
PR-URL: #11660 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#313
1 parent 82f105c commit 7368bff

10 files changed

Lines changed: 1556 additions & 0 deletions

File tree

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# rot90
22+
23+
> Rotate an ndarray 90 degrees in a specified plane.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var rot90 = require( '@stdlib/ndarray/base/rot90' );
41+
```
42+
43+
#### rot90( x, dims, k, writable )
44+
45+
Rotates an ndarray 90 degrees in the plane specified by two dimension indices.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
51+
// returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
52+
53+
var y = rot90( x, [ 0, 1 ], 1, false );
54+
// returns <ndarray>[ [ 2, 4 ], [ 1, 3 ] ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input ndarray.
60+
- **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`.
61+
- **k**: number of times to rotate by 90 degrees. Positive values rotate counterclockwise. Negative values rotate clockwise.
62+
- **writable**: boolean indicating whether a returned ndarray should be writable.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
## Notes
73+
74+
- 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.
75+
- 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.
76+
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
77+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
78+
- The returned ndarray is a **view** of the input ndarray. Accordingly, writing to the original ndarray will **mutate** the returned ndarray and vice versa.
79+
80+
</section>
81+
82+
<!-- /.notes -->
83+
84+
<!-- Package usage examples. -->
85+
86+
<section class="examples">
87+
88+
## Examples
89+
90+
<!-- eslint no-undef: "error" -->
91+
92+
```javascript
93+
var array = require( '@stdlib/ndarray/array' );
94+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
95+
var rot90 = require( '@stdlib/ndarray/base/rot90' );
96+
97+
// Create a 2x3 matrix:
98+
var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
99+
100+
// Rotate 90 degrees counterclockwise in the (0,1) plane:
101+
var y = rot90( x, [ 0, 1 ], 1, false );
102+
var arr = ndarray2array( y );
103+
// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]
104+
105+
// Rotate 180 degrees:
106+
y = rot90( x, [ 0, 1 ], 2, false );
107+
arr = ndarray2array( y );
108+
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]
109+
110+
// Rotate 270 degrees counterclockwise (equivalent to k=-1):
111+
y = rot90( x, [ 0, 1 ], 3, false );
112+
arr = ndarray2array( y );
113+
// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]
114+
115+
// Rotate 360 degrees (equivalent to no rotation):
116+
y = rot90( x, [ 0, 1 ], 4, false );
117+
arr = ndarray2array( y );
118+
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
119+
120+
// Rotate 90 degrees clockwise (equivalent to k=3):
121+
y = rot90( x, [ 0, 1 ], -1, false );
122+
arr = ndarray2array( y );
123+
// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]
124+
125+
// Supports negative dimension indices:
126+
y = rot90( x, [ -2, -1 ], 1, false );
127+
arr = ndarray2array( y );
128+
// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
136+
137+
<section class="references">
138+
139+
</section>
140+
141+
<!-- /.references -->
142+
143+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
144+
145+
<section class="related">
146+
147+
</section>
148+
149+
<!-- /.related -->
150+
151+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
152+
153+
<section class="links">
154+
155+
<!-- <related-links> -->
156+
157+
<!-- </related-links> -->
158+
159+
</section>
160+
161+
<!-- /.links -->
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
25+
var baseEmpty = require( '@stdlib/ndarray/base/empty' );
26+
var empty = require( '@stdlib/ndarray/empty' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var rot90 = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( format( '%s::base:ndims=2', pkg ), function benchmark( b ) {
35+
var values;
36+
var v;
37+
var i;
38+
39+
values = [
40+
baseEmpty( 'float64', [ 2, 2 ], 'row-major' ),
41+
baseEmpty( 'float32', [ 2, 2 ], 'row-major' ),
42+
baseEmpty( 'int32', [ 2, 2 ], 'row-major' ),
43+
baseEmpty( 'complex128', [ 2, 2 ], 'row-major' ),
44+
baseEmpty( 'generic', [ 2, 2 ], 'row-major' )
45+
];
46+
47+
b.tic();
48+
for ( i = 0; i < b.iterations; i++ ) {
49+
v = rot90( values[ i%values.length ], [ 0, 1 ], 1, false );
50+
if ( typeof v !== 'object' ) {
51+
b.fail( 'should return an ndarray' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isndarrayLike( v ) ) {
56+
b.fail( 'should return an ndarray' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
61+
62+
bench( format( '%s::non-base:ndims=2', pkg ), function benchmark( b ) {
63+
var values;
64+
var v;
65+
var i;
66+
67+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
68+
69+
values = [
70+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
71+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
72+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
73+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
74+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
75+
];
76+
77+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
78+
79+
b.tic();
80+
for ( i = 0; i < b.iterations; i++ ) {
81+
v = rot90( values[ i%values.length ], [ 0, 1 ], 1, false );
82+
if ( typeof v !== 'object' ) {
83+
b.fail( 'should return an ndarray' );
84+
}
85+
}
86+
b.toc();
87+
if ( !isndarrayLike( v ) ) {
88+
b.fail( 'should return an ndarray' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
});
93+
94+
bench( format( '%s::base:ndims=3,dims=[1,2]', pkg ), function benchmark( b ) {
95+
var values;
96+
var v;
97+
var i;
98+
99+
values = [
100+
baseEmpty( 'float64', [ 2, 2, 2 ], 'row-major' ),
101+
baseEmpty( 'float32', [ 2, 2, 2 ], 'row-major' ),
102+
baseEmpty( 'int32', [ 2, 2, 2 ], 'row-major' ),
103+
baseEmpty( 'complex128', [ 2, 2, 2 ], 'row-major' ),
104+
baseEmpty( 'generic', [ 2, 2, 2 ], 'row-major' )
105+
];
106+
107+
b.tic();
108+
for ( i = 0; i < b.iterations; i++ ) {
109+
v = rot90( values[ i%values.length ], [ 1, 2 ], 1, false );
110+
if ( typeof v !== 'object' ) {
111+
b.fail( 'should return an ndarray' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isndarrayLike( v ) ) {
116+
b.fail( 'should return an ndarray' );
117+
}
118+
b.pass( 'benchmark finished' );
119+
b.end();
120+
});
121+
122+
bench( format( '%s::base:ndims=3,dims=[0,2]', pkg ), function benchmark( b ) {
123+
var values;
124+
var v;
125+
var i;
126+
127+
values = [
128+
baseEmpty( 'float64', [ 2, 2, 2 ], 'row-major' ),
129+
baseEmpty( 'float32', [ 2, 2, 2 ], 'row-major' ),
130+
baseEmpty( 'int32', [ 2, 2, 2 ], 'row-major' ),
131+
baseEmpty( 'complex128', [ 2, 2, 2 ], 'row-major' ),
132+
baseEmpty( 'generic', [ 2, 2, 2 ], 'row-major' )
133+
];
134+
135+
b.tic();
136+
for ( i = 0; i < b.iterations; i++ ) {
137+
v = rot90( values[ i%values.length ], [ 0, 2 ], 1, false );
138+
if ( typeof v !== 'object' ) {
139+
b.fail( 'should return an ndarray' );
140+
}
141+
}
142+
b.toc();
143+
if ( !isndarrayLike( v ) ) {
144+
b.fail( 'should return an ndarray' );
145+
}
146+
b.pass( 'benchmark finished' );
147+
b.end();
148+
});
149+
150+
bench( format( '%s::base:ndims=4', pkg ), function benchmark( b ) {
151+
var values;
152+
var v;
153+
var i;
154+
155+
values = [
156+
baseEmpty( 'float64', [ 2, 2, 2, 2 ], 'row-major' ),
157+
baseEmpty( 'float32', [ 2, 2, 2, 2 ], 'row-major' ),
158+
baseEmpty( 'int32', [ 2, 2, 2, 2 ], 'row-major' ),
159+
baseEmpty( 'complex128', [ 2, 2, 2, 2 ], 'row-major' ),
160+
baseEmpty( 'generic', [ 2, 2, 2, 2 ], 'row-major' )
161+
];
162+
163+
b.tic();
164+
for ( i = 0; i < b.iterations; i++ ) {
165+
v = rot90( values[ i%values.length ], [ 2, 3 ], 1, false );
166+
if ( typeof v !== 'object' ) {
167+
b.fail( 'should return an ndarray' );
168+
}
169+
}
170+
b.toc();
171+
if ( !isndarrayLike( v ) ) {
172+
b.fail( 'should return an ndarray' );
173+
}
174+
b.pass( 'benchmark finished' );
175+
b.end();
176+
});
177+
178+
bench( format( '%s::base:ndims=5', pkg ), function benchmark( b ) {
179+
var values;
180+
var v;
181+
var i;
182+
183+
values = [
184+
baseEmpty( 'float64', [ 2, 2, 2, 2, 2 ], 'row-major' ),
185+
baseEmpty( 'float32', [ 2, 2, 2, 2, 2 ], 'row-major' ),
186+
baseEmpty( 'int32', [ 2, 2, 2, 2, 2 ], 'row-major' ),
187+
baseEmpty( 'complex128', [ 2, 2, 2, 2, 2 ], 'row-major' ),
188+
baseEmpty( 'generic', [ 2, 2, 2, 2, 2 ], 'row-major' )
189+
];
190+
191+
b.tic();
192+
for ( i = 0; i < b.iterations; i++ ) {
193+
v = rot90( values[ i%values.length ], [ 3, 4 ], 1, false );
194+
if ( typeof v !== 'object' ) {
195+
b.fail( 'should return an ndarray' );
196+
}
197+
}
198+
b.toc();
199+
if ( !isndarrayLike( v ) ) {
200+
b.fail( 'should return an ndarray' );
201+
}
202+
b.pass( 'benchmark finished' );
203+
b.end();
204+
});

0 commit comments

Comments
 (0)