Skip to content

Commit 781bf08

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/rot180
PR-URL: #11772 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#303
1 parent 7c667ac commit 781bf08

10 files changed

Lines changed: 1265 additions & 0 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
> Return a **read-only** view of an input ndarray 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. -->
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/rot180' );
41+
```
42+
43+
#### rot180( x\[, options] )
44+
45+
Returns a read-only view of an input [`ndarray`][@stdlib/ndarray/ctor] rotated `180` 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 = rot180( x );
54+
// returns <ndarray>[ [ 4.0, 3.0 ], [ 2.0, 1.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+
- **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]`.
65+
66+
</section>
67+
68+
<!-- /.usage -->
69+
70+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
71+
72+
<section class="notes">
73+
74+
## Notes
75+
76+
- Each provided dimension index must reside on the interval `[-ndims, ndims-1]`.
77+
78+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<!-- Package usage examples. -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var uniform = require( '@stdlib/random/uniform' );
92+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
93+
var rot180 = require( '@stdlib/ndarray/rot180' );
94+
95+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
96+
console.log( ndarray2array( x ) );
97+
98+
var y = rot180( x );
99+
console.log( ndarray2array( y ) );
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- 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. -->
107+
108+
<section class="references">
109+
110+
</section>
111+
112+
<!-- /.references -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
127+
128+
<!-- <related-links> -->
129+
130+
<!-- </related-links> -->
131+
132+
</section>
133+
134+
<!-- /.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 rot180 = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s:ndims=2', pkg ), function benchmark( b ) {
34+
var values;
35+
var v;
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+
v = rot180( values[ i%values.length ] );
53+
if ( typeof v !== 'object' ) {
54+
b.fail( 'should return an ndarray' );
55+
}
56+
}
57+
b.toc();
58+
if ( !isndarrayLike( v ) ) {
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 v;
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+
v = rot180( values[ i%values.length ] );
85+
if ( typeof v !== 'object' ) {
86+
b.fail( 'should return an ndarray' );
87+
}
88+
}
89+
b.toc();
90+
if ( !isndarrayLike( v ) ) {
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 v;
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+
v = rot180( values[ i%values.length ], opts );
122+
if ( typeof v !== 'object' ) {
123+
b.fail( 'should return an ndarray' );
124+
}
125+
}
126+
b.toc();
127+
if ( !isndarrayLike( v ) ) {
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 v;
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+
v = rot180( values[ i%values.length ] );
154+
if ( typeof v !== 'object' ) {
155+
b.fail( 'should return an ndarray' );
156+
}
157+
}
158+
b.toc();
159+
if ( !isndarrayLike( v ) ) {
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 v;
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+
v = rot180( values[ i%values.length ] );
186+
if ( typeof v !== 'object' ) {
187+
b.fail( 'should return an ndarray' );
188+
}
189+
}
190+
b.toc();
191+
if ( !isndarrayLike( v ) ) {
192+
b.fail( 'should return an ndarray' );
193+
}
194+
b.pass( 'benchmark finished' );
195+
b.end();
196+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( x[, options] )
3+
Returns a read-only view of an input ndarray rotated 180 degrees in a
4+
specified 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+
options: Object (optional)
14+
Function options.
15+
16+
options.dims: ArrayLikeObject<integer> (optional)
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`. Default: [ -2, -1 ].
21+
22+
Returns
23+
-------
24+
out: ndarray
25+
Output array view.
26+
27+
Examples
28+
--------
29+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
30+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
31+
> var y = {{alias}}( x )
32+
<ndarray>[ [ 4, 3 ], [ 2, 1 ] ]
33+
34+
See Also
35+
--------

0 commit comments

Comments
 (0)