Skip to content

Commit e1c8c4d

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/to-rot90
PR-URL: #11775 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#316
1 parent be8badf commit e1c8c4d

10 files changed

Lines changed: 1565 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
# toRot90
22+
23+
> Return a new [`ndarray`][@stdlib/ndarray/ctor] where an input [`ndarray`][@stdlib/ndarray/ctor] is rotated `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. -->
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 toRot90 = require( '@stdlib/ndarray/to-rot90' );
41+
```
42+
43+
#### toRot90( x\[, options] )
44+
45+
Returns a new [`ndarray`][@stdlib/ndarray/ctor] where an input [`ndarray`][@stdlib/ndarray/ctor] is rotated `90` degrees in a specified plane.
46+
47+
```javascript
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
51+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
52+
53+
var y = toRot90( x );
54+
// returns <ndarray>[ [ 2.0, 4.0 ], [ 1.0, 3.0 ] ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input [`ndarray`][@stdlib/ndarray/ctor].
60+
- **options**: function options.
61+
62+
The function accepts the following options:
63+
64+
- **k**: number of times to rotate by `90` degrees. Positive values rotate counterclockwise. Negative values rotate clockwise. Default: `1`.
65+
- **dims**: dimension indices defining the plane of rotation. Must contain exactly two unique dimension indices. If a dimension index is provided as an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`. Default: `[-2, -1]`.
66+
67+
</section>
68+
69+
<!-- /.usage -->
70+
71+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
72+
73+
<section class="notes">
74+
75+
## Notes
76+
77+
- If `options.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 `options.dims = [0, 1]`, the function rotates the plane counterclockwise.
78+
- If `options.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 `options.dims = [0, 1]`, the function rotates the plane clockwise.
79+
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<!-- Package usage examples. -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var uniform = require( '@stdlib/random/uniform' );
95+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
96+
var toRot90 = require( '@stdlib/ndarray/to-rot90' );
97+
98+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
99+
console.log( ndarray2array( x ) );
100+
101+
var y = toRot90( x );
102+
console.log( ndarray2array( y ) );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- 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. -->
110+
111+
<section class="references">
112+
113+
</section>
114+
115+
<!-- /.references -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
130+
131+
<!-- <related-links> -->
132+
133+
<!-- </related-links> -->
134+
135+
</section>
136+
137+
<!-- /.links -->
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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 empty = require( '@stdlib/ndarray/empty' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var toRot90 = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s:ndims=2', pkg ), function benchmark( b ) {
34+
var values;
35+
var out;
36+
var i;
37+
38+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
39+
40+
values = [
41+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
42+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
43+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
44+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
45+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
46+
];
47+
48+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
49+
50+
b.tic();
51+
for ( i = 0; i < b.iterations; i++ ) {
52+
out = toRot90( values[ i%values.length ] );
53+
if ( typeof out !== 'object' ) {
54+
b.fail( 'should return an ndarray' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isndarrayLike( out ) ) {
59+
b.fail( 'should return an ndarray' );
60+
}
61+
b.pass( 'benchmark finished' );
62+
b.end();
63+
});
64+
65+
bench( format( '%s:ndims=3', pkg ), function benchmark( b ) {
66+
var values;
67+
var out;
68+
var i;
69+
70+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
71+
72+
values = [
73+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
74+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
75+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
76+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
77+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
78+
];
79+
80+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
out = toRot90( values[ i%values.length ] );
85+
if ( typeof out !== 'object' ) {
86+
b.fail( 'should return an ndarray' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isndarrayLike( out ) ) {
91+
b.fail( 'should return an ndarray' );
92+
}
93+
b.pass( 'benchmark finished' );
94+
b.end();
95+
});
96+
97+
bench( format( '%s:ndims=3,dims=[0,2]', pkg ), function benchmark( b ) {
98+
var values;
99+
var opts;
100+
var out;
101+
var i;
102+
103+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
104+
105+
values = [
106+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
107+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
108+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
109+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
110+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
111+
];
112+
113+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
114+
115+
opts = {
116+
'dims': [ 0, 2 ]
117+
};
118+
119+
b.tic();
120+
for ( i = 0; i < b.iterations; i++ ) {
121+
out = toRot90( values[ i%values.length ], opts );
122+
if ( typeof out !== 'object' ) {
123+
b.fail( 'should return an ndarray' );
124+
}
125+
}
126+
b.toc();
127+
if ( !isndarrayLike( out ) ) {
128+
b.fail( 'should return an ndarray' );
129+
}
130+
b.pass( 'benchmark finished' );
131+
b.end();
132+
});
133+
134+
bench( format( '%s:ndims=4', pkg ), function benchmark( b ) {
135+
var values;
136+
var out;
137+
var i;
138+
139+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
140+
141+
values = [
142+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
143+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
144+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
145+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
146+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'generic' } )
147+
];
148+
149+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
150+
151+
b.tic();
152+
for ( i = 0; i < b.iterations; i++ ) {
153+
out = toRot90( values[ i%values.length ] );
154+
if ( typeof out !== 'object' ) {
155+
b.fail( 'should return an ndarray' );
156+
}
157+
}
158+
b.toc();
159+
if ( !isndarrayLike( out ) ) {
160+
b.fail( 'should return an ndarray' );
161+
}
162+
b.pass( 'benchmark finished' );
163+
b.end();
164+
});
165+
166+
bench( format( '%s:ndims=5', pkg ), function benchmark( b ) {
167+
var values;
168+
var out;
169+
var i;
170+
171+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
172+
173+
values = [
174+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
175+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
176+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
177+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
178+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'generic' } )
179+
];
180+
181+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
182+
183+
b.tic();
184+
for ( i = 0; i < b.iterations; i++ ) {
185+
out = toRot90( values[ i%values.length ] );
186+
if ( typeof out !== 'object' ) {
187+
b.fail( 'should return an ndarray' );
188+
}
189+
}
190+
b.toc();
191+
if ( !isndarrayLike( out ) ) {
192+
b.fail( 'should return an ndarray' );
193+
}
194+
b.pass( 'benchmark finished' );
195+
b.end();
196+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
{{alias}}( x[, options] )
3+
Returns a new ndarray where an input ndarray is rotated 90 degrees in a
4+
specified plane.
5+
6+
If `options.k > 0`, the function rotates the plane from the first specified
7+
dimension toward the second specified dimension. This means that, for a
8+
two-dimensional ndarray and `options.dims = [0, 1]`, the function rotates
9+
the plane counterclockwise.
10+
11+
If `options.k < 0`, the function rotates the plane from the second specified
12+
dimension toward the first specified dimension. This means that, for a
13+
two-dimensional ndarray and `options.dims = [0, 1]`, the function rotates
14+
the plane clockwise.
15+
16+
Each provided dimension index must reside on the interval [-ndims, ndims-1].
17+
18+
Parameters
19+
----------
20+
x: ndarray
21+
Input array.
22+
23+
options: Object (optional)
24+
Function options.
25+
26+
options.k: integer (optional)
27+
Number of times to rotate by 90 degrees. Default: 1.
28+
29+
options.dims: ArrayLikeObject<integer> (optional)
30+
Dimension indices defining the plane of rotation. Must contain exactly
31+
two unique dimension indices. If less than zero, an index is resolved
32+
relative to the last dimension, with the last dimension corresponding to
33+
the value `-1`. Default: [ -2, -1 ].
34+
35+
Returns
36+
-------
37+
out: ndarray
38+
Output array.
39+
40+
Examples
41+
--------
42+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
43+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
44+
> var y = {{alias}}( x )
45+
<ndarray>[ [ 2, 4 ], [ 1, 3 ] ]
46+
> y = {{alias}}( x, { 'k': 2 } )
47+
<ndarray>[ [ 4, 3 ], [ 2, 1 ] ]
48+
49+
See Also
50+
--------

0 commit comments

Comments
 (0)