Skip to content

Commit 287c085

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/to-reversed-dimension
PR-URL: #10058 Closes: stdlib-js/metr-issue-tracker#169 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 841ab2e commit 287c085

File tree

10 files changed

+1077
-0
lines changed

10 files changed

+1077
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
# toReversedDimension
22+
23+
> Return a new [ndarray][@stdlib/ndarray/ctor] where the order of elements of an input [ndarray][@stdlib/ndarray/ctor] along a specified dimension is reversed.
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 toReversedDimension = require( '@stdlib/ndarray/to-reversed-dimension' );
41+
```
42+
43+
#### toReversedDimension( x\[, options] )
44+
45+
Returns a new [ndarray][@stdlib/ndarray/ctor] where the order of elements of an input [ndarray][@stdlib/ndarray/ctor] along a specified dimension is reversed.
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 = toReversedDimension( x );
54+
// returns <ndarray>[ [ 2.0, 1.0 ], [ 4.0, 3.0 ] ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
60+
- **options**: function options (_optional_).
61+
62+
The function supports the following options:
63+
64+
- **dim**: index of dimension along which to reverse elements. If provided 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: `-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+
</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 uniform = require( '@stdlib/random/uniform' );
88+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
89+
var toReversedDimension = require( '@stdlib/ndarray/to-reversed-dimension' );
90+
91+
var x = uniform( [ 3, 3, 3 ], -10.0, 10.0 );
92+
console.log( ndarray2array( x ) );
93+
94+
var y = toReversedDimension( x );
95+
console.log( ndarray2array( y ) );
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- 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. -->
103+
104+
<section class="references">
105+
106+
</section>
107+
108+
<!-- /.references -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
123+
124+
<!-- <related-links> -->
125+
126+
<!-- </related-links> -->
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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 toReversedDimension = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( format( '%s::1d', 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 ], { 'dtype': 'float64' } ),
42+
empty( [ 2 ], { 'dtype': 'float32' } ),
43+
empty( [ 2 ], { 'dtype': 'int32' } ),
44+
empty( [ 2 ], { 'dtype': 'complex128' } ),
45+
empty( [ 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 = toReversedDimension( values[ i%values.length ], {
53+
'dim': 0
54+
});
55+
if ( typeof v !== 'object' ) {
56+
b.fail( 'should return an ndarray' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isndarrayLike( v ) ) {
61+
b.fail( 'should return an ndarray' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});
66+
67+
bench( format( '%s::2d', pkg ), function benchmark( b ) {
68+
var values;
69+
var v;
70+
var i;
71+
72+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
73+
74+
values = [
75+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
76+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
77+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
78+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
79+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
80+
];
81+
82+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
v = toReversedDimension( values[ i%values.length ], {
87+
'dim': 0
88+
});
89+
if ( typeof v !== 'object' ) {
90+
b.fail( 'should return an ndarray' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isndarrayLike( v ) ) {
95+
b.fail( 'should return an ndarray' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( format( '%s::3d', pkg ), function benchmark( b ) {
102+
var values;
103+
var v;
104+
var i;
105+
106+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
107+
108+
values = [
109+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
110+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
111+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
112+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
113+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
114+
];
115+
116+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
117+
118+
b.tic();
119+
for ( i = 0; i < b.iterations; i++ ) {
120+
v = toReversedDimension( values[ i%values.length ], {
121+
'dim': 0
122+
});
123+
if ( typeof v !== 'object' ) {
124+
b.fail( 'should return an ndarray' );
125+
}
126+
}
127+
b.toc();
128+
if ( !isndarrayLike( v ) ) {
129+
b.fail( 'should return an ndarray' );
130+
}
131+
b.pass( 'benchmark finished' );
132+
b.end();
133+
});
134+
135+
bench( format( '%s::4d', pkg ), function benchmark( b ) {
136+
var values;
137+
var v;
138+
var i;
139+
140+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
141+
142+
values = [
143+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
144+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
145+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
146+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
147+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'generic' } )
148+
];
149+
150+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
151+
152+
b.tic();
153+
for ( i = 0; i < b.iterations; i++ ) {
154+
v = toReversedDimension( values[ i%values.length ], {
155+
'dim': 0
156+
});
157+
if ( typeof v !== 'object' ) {
158+
b.fail( 'should return an ndarray' );
159+
}
160+
}
161+
b.toc();
162+
if ( !isndarrayLike( v ) ) {
163+
b.fail( 'should return an ndarray' );
164+
}
165+
b.pass( 'benchmark finished' );
166+
b.end();
167+
});
168+
169+
bench( format( '%s::5d', pkg ), function benchmark( b ) {
170+
var values;
171+
var v;
172+
var i;
173+
174+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
175+
176+
values = [
177+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
178+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
179+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
180+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
181+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'generic' } )
182+
];
183+
184+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
185+
186+
b.tic();
187+
for ( i = 0; i < b.iterations; i++ ) {
188+
v = toReversedDimension( values[ i%values.length ], {
189+
'dim': 0
190+
});
191+
if ( typeof v !== 'object' ) {
192+
b.fail( 'should return an ndarray' );
193+
}
194+
}
195+
b.toc();
196+
if ( !isndarrayLike( v ) ) {
197+
b.fail( 'should return an ndarray' );
198+
}
199+
b.pass( 'benchmark finished' );
200+
b.end();
201+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( x[, options] )
3+
Returns a new ndarray where the order of elements of an input ndarray along
4+
a specified dimension is reversed.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input array.
10+
11+
options: object (optional)
12+
Function options.
13+
14+
options.dim: integer (optional)
15+
Index of dimension along which to reverse elements. If provided an
16+
integer less than zero, the dimension index is resolved relative to the
17+
last dimension, with the last dimension corresponding to the value `-1`.
18+
Default: `-1`.
19+
20+
Returns
21+
-------
22+
out: ndarray
23+
Output array.
24+
25+
Examples
26+
--------
27+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
28+
<ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
29+
> var y = {{alias}}( x )
30+
<ndarray>[ [ 2, 1 ], [ 4, 3 ] ]
31+
32+
See Also
33+
--------
34+

0 commit comments

Comments
 (0)