Skip to content

Commit 37197d9

Browse files
authored
feat: add ndarray/base/to-rot180
PR-URL: #11778 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#306
1 parent e1c8c4d commit 37197d9

10 files changed

Lines changed: 1202 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
# toRot180
22+
23+
> Return a new ndarray where an ndarray is rotated 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 toRot180 = require( '@stdlib/ndarray/base/to-rot180' );
41+
```
42+
43+
#### toRot180( x, dims )
44+
45+
Returns a new ndarray where an ndarray is rotated 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 = toRot180( x, [ 0, 1 ] );
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+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<!-- Package usage examples. -->
79+
80+
<section class="examples">
81+
82+
## Examples
83+
84+
<!-- eslint no-undef: "error" -->
85+
86+
```javascript
87+
var array = require( '@stdlib/ndarray/array' );
88+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
89+
var toRot180 = require( '@stdlib/ndarray/base/to-rot180' );
90+
91+
// Create a 2x3 matrix:
92+
var x = array( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] );
93+
94+
// Rotate 180 degrees in the (0,1) plane:
95+
var y = toRot180( x, [ 0, 1 ] );
96+
var arr = ndarray2array( y );
97+
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]
98+
99+
// Supports negative dimension indices:
100+
y = toRot180( x, [ -2, -1 ] );
101+
arr = ndarray2array( y );
102+
// returns [ [ 6, 5, 4 ], [ 3, 2, 1 ] ]
103+
104+
// Rotating twice returns the original arrangement:
105+
y = toRot180( toRot180( x, [ 0, 1 ] ), [ 0, 1 ] );
106+
arr = ndarray2array( y );
107+
// returns [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- 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. -->
115+
116+
<section class="references">
117+
118+
</section>
119+
120+
<!-- /.references -->
121+
122+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
123+
124+
<section class="related">
125+
126+
</section>
127+
128+
<!-- /.related -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
<!-- <related-links> -->
135+
136+
<!-- </related-links> -->
137+
138+
</section>
139+
140+
<!-- /.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 toRot180 = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( format( '%s::base:ndims=2', pkg ), function benchmark( b ) {
35+
var values;
36+
var out;
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+
out = toRot180( values[ i%values.length ], [ 0, 1 ] );
50+
if ( typeof out !== 'object' ) {
51+
b.fail( 'should return an ndarray' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isndarrayLike( out ) ) {
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 out;
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+
out = toRot180( values[ i%values.length ], [ 0, 1 ] );
82+
if ( typeof out !== 'object' ) {
83+
b.fail( 'should return an ndarray' );
84+
}
85+
}
86+
b.toc();
87+
if ( !isndarrayLike( out ) ) {
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 out;
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+
out = toRot180( values[ i%values.length ], [ 1, 2 ] );
110+
if ( typeof out !== 'object' ) {
111+
b.fail( 'should return an ndarray' );
112+
}
113+
}
114+
b.toc();
115+
if ( !isndarrayLike( out ) ) {
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 out;
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+
out = toRot180( values[ i%values.length ], [ 0, 2 ] );
138+
if ( typeof out !== 'object' ) {
139+
b.fail( 'should return an ndarray' );
140+
}
141+
}
142+
b.toc();
143+
if ( !isndarrayLike( out ) ) {
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 out;
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+
out = toRot180( values[ i%values.length ], [ 2, 3 ] );
166+
if ( typeof out !== 'object' ) {
167+
b.fail( 'should return an ndarray' );
168+
}
169+
}
170+
b.toc();
171+
if ( !isndarrayLike( out ) ) {
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 out;
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+
out = toRot180( values[ i%values.length ], [ 3, 4 ] );
194+
if ( typeof out !== 'object' ) {
195+
b.fail( 'should return an ndarray' );
196+
}
197+
}
198+
b.toc();
199+
if ( !isndarrayLike( out ) ) {
200+
b.fail( 'should return an ndarray' );
201+
}
202+
b.pass( 'benchmark finished' );
203+
b.end();
204+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( x, dims )
3+
Returns a new ndarray where an ndarray is rotated 180 degrees in a specified
4+
plane.
5+
6+
Each provided dimension index must reside on the interval [-ndims, ndims-1].
7+
8+
Parameters
9+
----------
10+
x: ndarray
11+
Input array.
12+
13+
dims: ArrayLikeObject<integer>
14+
Dimension indices defining the plane of rotation. Must contain exactly
15+
two unique dimension indices. If less than zero, an index is resolved
16+
relative to the last dimension, with the last dimension corresponding to
17+
the value `-1`.
18+
19+
Returns
20+
-------
21+
out: ndarray
22+
Output array.
23+
24+
Examples
25+
--------
26+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
27+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
28+
> var y = {{alias}}( x, [ 0, 1 ] )
29+
<ndarray>[ [ 4, 3 ], [ 2, 1 ] ]
30+
31+
See Also
32+
--------

0 commit comments

Comments
 (0)