Skip to content

Commit 9ef9c56

Browse files
authored
feat: add ndarray/base/rotl90
PR-URL: #11633 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#299
1 parent 2672e84 commit 9ef9c56

10 files changed

Lines changed: 1444 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
# rotl90
22+
23+
> Rotate a matrix (or a stack of matrices) 90 degrees counterclockwise.
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 rotl90 = require( '@stdlib/ndarray/base/rotl90' );
41+
```
42+
43+
#### rotl90( x, k, writable )
44+
45+
Rotates a matrix (or a stack of matrices) 90 degrees counterclockwise.
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 = rotl90( x, 1, false );
54+
// returns <ndarray>[ [ 2, 4 ], [ 1, 3 ] ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input ndarray.
60+
- **k**: number of times to rotate by 90 degrees. Positive values rotate counterclockwise. Negative values rotate clockwise.
61+
- **writable**: boolean indicating whether a returned ndarray should be writable.
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+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
74+
- If `k > 0`, the function rotates the matrix counterclockwise.
75+
- If `k < 0`, the function rotates the matrix clockwise.
76+
- The returned ndarray is a **view** of the input ndarray. Accordingly, writing to the original ndarray will **mutate** the returned ndarray and vice versa.
77+
- If provided an ndarray with fewer than two dimensions, the function does not perform rotation and simply returns a new view of the input ndarray.
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<!-- Package usage examples. -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var array = require( '@stdlib/ndarray/array' );
93+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
94+
var rotl90 = require( '@stdlib/ndarray/base/rotl90' );
95+
96+
// Create a 2x3 matrix:
97+
var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
98+
99+
// Rotate 90 degrees counterclockwise:
100+
var y = rotl90( x, 1, false );
101+
var arr = ndarray2array( y );
102+
// returns [ [ 3, 6 ], [ 2, 5 ], [ 1, 4 ] ]
103+
104+
// Rotate 180 degrees:
105+
y = rotl90( x, 2, false );
106+
arr = ndarray2array( y );
107+
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]
108+
109+
// Rotate 270 degrees counterclockwise (equivalent to 90 degrees clockwise):
110+
y = rotl90( x, 3, false );
111+
arr = ndarray2array( y );
112+
// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]
113+
114+
// Rotate 360 degrees (equivalent to no rotation):
115+
y = rotl90( x, 4, false );
116+
arr = ndarray2array( y );
117+
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
118+
119+
// Rotate 90 degrees clockwise (equivalent to k=3):
120+
y = rotl90( x, -1, false );
121+
arr = ndarray2array( y );
122+
// returns [ [ 4, 1 ], [ 5, 2 ], [ 6, 3 ] ]
123+
```
124+
125+
</section>
126+
127+
<!-- /.examples -->
128+
129+
<!-- 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. -->
130+
131+
<section class="references">
132+
133+
</section>
134+
135+
<!-- /.references -->
136+
137+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
138+
139+
<section class="related">
140+
141+
</section>
142+
143+
<!-- /.related -->
144+
145+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="links">
148+
149+
<!-- <related-links> -->
150+
151+
<!-- </related-links> -->
152+
153+
</section>
154+
155+
<!-- /.links -->

0 commit comments

Comments
 (0)