Skip to content

Commit 33d0690

Browse files
committed
feat: add ndarray/base/to-rot90
--- 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: skipped - task: lint_license_headers status: passed ---
1 parent 78898df commit 33d0690

10 files changed

Lines changed: 1454 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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 where an ndarray 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 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 toRot90 = require( '@stdlib/ndarray/base/to-rot90' );
41+
```
42+
43+
#### toRot90( x, dims, k )
44+
45+
Returns a new ndarray where an ndarray is rotated `90` degrees in a specified plane.
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 = toRot90( x, [ 0, 1 ], 1 );
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+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
68+
69+
<section class="notes">
70+
71+
## Notes
72+
73+
- 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.
74+
- 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.
75+
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
76+
77+
</section>
78+
79+
<!-- /.notes -->
80+
81+
<!-- Package usage examples. -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var array = require( '@stdlib/ndarray/array' );
91+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
92+
var toRot90 = require( '@stdlib/ndarray/base/to-rot90' );
93+
94+
// Create a 2x3 matrix:
95+
var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
96+
97+
// Rotate 90 degrees counterclockwise in the (0,1) plane:
98+
var y = toRot90( x, [ 0, 1 ], 1 );
99+
var arr = ndarray2array( y );
100+
// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]
101+
102+
// Rotate 180 degrees:
103+
y = toRot90( x, [ 0, 1 ], 2 );
104+
arr = ndarray2array( y );
105+
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]
106+
107+
// Rotate 270 degrees counterclockwise (equivalent to k=-1):
108+
y = toRot90( x, [ 0, 1 ], 3 );
109+
arr = ndarray2array( y );
110+
// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]
111+
112+
// Rotate 360 degrees (equivalent to no rotation):
113+
y = toRot90( x, [ 0, 1 ], 4 );
114+
arr = ndarray2array( y );
115+
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
116+
117+
// Rotate 90 degrees clockwise (equivalent to k=3):
118+
y = toRot90( x, [ 0, 1 ], -1 );
119+
arr = ndarray2array( y );
120+
// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]
121+
122+
// Supports negative dimension indices:
123+
y = toRot90( x, [ -2, -1 ], 1 );
124+
arr = ndarray2array( y );
125+
// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]
126+
```
127+
128+
</section>
129+
130+
<!-- /.examples -->
131+
132+
<!-- 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. -->
133+
134+
<section class="references">
135+
136+
</section>
137+
138+
<!-- /.references -->
139+
140+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141+
142+
<section class="related">
143+
144+
</section>
145+
146+
<!-- /.related -->
147+
148+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
149+
150+
<section class="links">
151+
152+
<!-- <related-links> -->
153+
154+
<!-- </related-links> -->
155+
156+
</section>
157+
158+
<!-- /.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 toRot90 = 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 = toRot90( values[ i%values.length ], [ 0, 1 ], 1 );
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 = toRot90( values[ i%values.length ], [ 0, 1 ], 1 );
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 = toRot90( values[ i%values.length ], [ 1, 2 ], 1 );
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 = toRot90( values[ i%values.length ], [ 0, 2 ], 1 );
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 = toRot90( values[ i%values.length ], [ 2, 3 ], 1 );
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 = toRot90( values[ i%values.length ], [ 3, 4 ], 1 );
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)