Skip to content

Commit fdae0a5

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/rot180
PR-URL: #11704 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#302
1 parent af83cc1 commit fdae0a5

10 files changed

Lines changed: 1204 additions & 0 deletions

File tree

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
# rot180
22+
23+
> Rotate an ndarray 180 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 rot180 = require( '@stdlib/ndarray/base/rot180' );
41+
```
42+
43+
#### rot180( x, dims, writable )
44+
45+
Rotates an ndarray 180 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 = rot180( x, [ 0, 1 ], false );
54+
// returns <ndarray>[ [ 4, 3 ], [ 2, 1 ] ]
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+
- **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+
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
74+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
75+
- The returned ndarray is a **view** of the input ndarray. Accordingly, writing to the original ndarray will **mutate** the returned ndarray and vice versa.
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 rot180 = require( '@stdlib/ndarray/base/rot180' );
93+
94+
// Create a 2x3 matrix:
95+
var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
96+
97+
// Rotate 180 degrees in the (0,1) plane:
98+
var y = rot180( x, [ 0, 1 ], false );
99+
var arr = ndarray2array( y );
100+
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]
101+
102+
// Supports negative dimension indices:
103+
y = rot180( x, [ -2, -1 ], false );
104+
arr = ndarray2array( y );
105+
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]
106+
107+
// Rotating twice returns the original arrangement:
108+
y = rot180( rot180( x, [ 0, 1 ], false ), [ 0, 1 ], false );
109+
arr = ndarray2array( y );
110+
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<!-- 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. -->
118+
119+
<section class="references">
120+
121+
</section>
122+
123+
<!-- /.references -->
124+
125+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
126+
127+
<section class="related">
128+
129+
</section>
130+
131+
<!-- /.related -->
132+
133+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
134+
135+
<section class="links">
136+
137+
<!-- <related-links> -->
138+
139+
<!-- </related-links> -->
140+
141+
</section>
142+
143+
<!-- /.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 rot180 = 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 = rot180( values[ i%values.length ], [ 0, 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 = rot180( values[ i%values.length ], [ 0, 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 = rot180( values[ i%values.length ], [ 1, 2 ], 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 = rot180( values[ i%values.length ], [ 0, 2 ], 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 = rot180( values[ i%values.length ], [ 2, 3 ], 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 = rot180( values[ i%values.length ], [ 3, 4 ], 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+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
{{alias}}( x, dims, writable )
3+
Rotates an ndarray 180 degrees in a specified plane.
4+
5+
Each provided dimension index must reside on the interval [-ndims, ndims-1].
6+
7+
The returned ndarray is always a *view* of the input ndarray. Accordingly,
8+
writing to the original ndarray will mutate the returned ndarray and vice
9+
versa.
10+
11+
Parameters
12+
----------
13+
x: ndarray
14+
Input array.
15+
16+
dims: ArrayLikeObject<integer>
17+
Dimension indices defining the plane of rotation. Must contain exactly
18+
two unique dimension indices. If less than zero, an index is resolved
19+
relative to the last dimension, with the last dimension corresponding to
20+
the value `-1`.
21+
22+
writable: boolean
23+
Boolean indicating whether a returned ndarray should be writable. This
24+
parameter only applies to ndarray constructors which support read-only
25+
instances.
26+
27+
Returns
28+
-------
29+
out: ndarray
30+
Output array view.
31+
32+
Examples
33+
--------
34+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
35+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
36+
> var y = {{alias}}( x, [ 0, 1 ], false )
37+
<ndarray>[ [ 4, 3 ], [ 2, 1 ] ]
38+
39+
See Also
40+
--------

0 commit comments

Comments
 (0)