Skip to content

Commit 9162dd9

Browse files
committed
feat: add ndarray/to-reversed-dimensions
1 parent c5fad82 commit 9162dd9

10 files changed

Lines changed: 1007 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
# toReversedDimensions
22+
23+
> Return a new [`ndarray`][@stdlib/ndarray/ctor] where the order of elements of an input [`ndarray`][@stdlib/ndarray/ctor] along specified dimensions is reversed.
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 toReversedDimensions = require( '@stdlib/ndarray/to-reversed-dimensions' );
41+
```
42+
43+
#### toReversedDimensions( x, dims )
44+
45+
Returns a new [`ndarray`][@stdlib/ndarray/ctor] where the order of elements of an input [`ndarray`][@stdlib/ndarray/ctor] along specified dimensions 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 ], [ 5.0, 6.0 ] ] );
51+
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
52+
53+
var y = toReversedDimensions( x, [ 0, 1 ] );
54+
// returns <ndarray>[ [ 6.0, 5.0 ], [ 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+
- **dims**: indices of dimensions along which to reverse elements. 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+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<!-- Package usage examples. -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var uniform = require( '@stdlib/random/discrete-uniform' );
84+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
85+
var toReversedDimensions = require( '@stdlib/ndarray/to-reversed-dimensions' );
86+
87+
var x = uniform( [ 3, 3, 3 ], -100, 100 );
88+
console.log( ndarray2array( x ) );
89+
90+
var y = toReversedDimensions( x, [ 0, 2 ] );
91+
console.log( ndarray2array( y ) );
92+
```
93+
94+
</section>
95+
96+
<!-- /.examples -->
97+
98+
<!-- 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. -->
99+
100+
<section class="references">
101+
102+
</section>
103+
104+
<!-- /.references -->
105+
106+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
107+
108+
<section class="related">
109+
110+
</section>
111+
112+
<!-- /.related -->
113+
114+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
115+
116+
<section class="links">
117+
118+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
119+
120+
<!-- <related-links> -->
121+
122+
<!-- </related-links> -->
123+
124+
</section>
125+
126+
<!-- /.links -->
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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 Float64Array = require( '@stdlib/array/float64' );
25+
var ndarrayBase = require( '@stdlib/ndarray/base/ctor' );
26+
var ndarray = require( '@stdlib/ndarray/ctor' );
27+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var toReversedDimensions = require( './../lib' );
31+
32+
33+
// MAIN //
34+
35+
bench( format( '%s:ctor=base,ndims=1', pkg ), function benchmark( b ) {
36+
var strides;
37+
var buffer;
38+
var offset;
39+
var dtype;
40+
var order;
41+
var shape;
42+
var dims;
43+
var out;
44+
var i;
45+
var x;
46+
47+
dtype = 'float64';
48+
buffer = new Float64Array( 24 );
49+
shape = [ 24 ];
50+
strides = [ 1 ];
51+
offset = 0;
52+
order = 'row-major';
53+
dims = [ 0 ];
54+
55+
x = ndarrayBase( dtype, buffer, shape, strides, offset, order );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = toReversedDimensions( x, dims );
60+
if ( typeof out !== 'object' ) {
61+
b.fail( 'should return an object' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isndarrayLike( out ) ) {
66+
b.fail( 'should return an ndarray' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
});
71+
72+
bench( format( '%s:ctor=ndarray,ndims=1', pkg ), function benchmark( b ) {
73+
var strides;
74+
var buffer;
75+
var offset;
76+
var dtype;
77+
var order;
78+
var shape;
79+
var dims;
80+
var out;
81+
var i;
82+
var x;
83+
84+
dtype = 'float64';
85+
buffer = new Float64Array( 24 );
86+
shape = [ 24 ];
87+
strides = [ 1 ];
88+
offset = 0;
89+
order = 'row-major';
90+
dims = [ 0 ];
91+
92+
x = ndarray( dtype, buffer, shape, strides, offset, order );
93+
94+
b.tic();
95+
for ( i = 0; i < b.iterations; i++ ) {
96+
out = toReversedDimensions( x, dims );
97+
if ( typeof out !== 'object' ) {
98+
b.fail( 'should return an object' );
99+
}
100+
}
101+
b.toc();
102+
if ( !isndarrayLike( out ) ) {
103+
b.fail( 'should return an ndarray' );
104+
}
105+
b.pass( 'benchmark finished' );
106+
b.end();
107+
});
108+
109+
bench( format( '%s:ctor=base,ndims=2', pkg ), function benchmark( b ) {
110+
var strides;
111+
var buffer;
112+
var offset;
113+
var dtype;
114+
var order;
115+
var shape;
116+
var dims;
117+
var out;
118+
var i;
119+
var x;
120+
121+
dtype = 'float64';
122+
buffer = new Float64Array( 24 );
123+
shape = [ 6, 4 ];
124+
strides = [ 4, 1 ];
125+
offset = 0;
126+
order = 'row-major';
127+
dims = [ 0, 1 ];
128+
129+
x = ndarrayBase( dtype, buffer, shape, strides, offset, order );
130+
131+
b.tic();
132+
for ( i = 0; i < b.iterations; i++ ) {
133+
out = toReversedDimensions( x, dims );
134+
if ( typeof out !== 'object' ) {
135+
b.fail( 'should return an object' );
136+
}
137+
}
138+
b.toc();
139+
if ( !isndarrayLike( out ) ) {
140+
b.fail( 'should return an ndarray' );
141+
}
142+
b.pass( 'benchmark finished' );
143+
b.end();
144+
});
145+
146+
bench( format( '%s:ctor=ndarray,ndims=2', pkg ), function benchmark( b ) {
147+
var strides;
148+
var buffer;
149+
var offset;
150+
var dtype;
151+
var order;
152+
var shape;
153+
var dims;
154+
var out;
155+
var i;
156+
var x;
157+
158+
dtype = 'float64';
159+
buffer = new Float64Array( 24 );
160+
shape = [ 6, 4 ];
161+
strides = [ 4, 1 ];
162+
offset = 0;
163+
order = 'row-major';
164+
dims = [ 0, 1 ];
165+
166+
x = ndarray( dtype, buffer, shape, strides, offset, order );
167+
168+
b.tic();
169+
for ( i = 0; i < b.iterations; i++ ) {
170+
out = toReversedDimensions( x, dims );
171+
if ( typeof out !== 'object' ) {
172+
b.fail( 'should return an object' );
173+
}
174+
}
175+
b.toc();
176+
if ( !isndarrayLike( out ) ) {
177+
b.fail( 'should return an ndarray' );
178+
}
179+
b.pass( 'benchmark finished' );
180+
b.end();
181+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( x, dims )
3+
Returns a new ndarray where the order of elements of an input ndarray along
4+
specified dimensions is reversed.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input array.
10+
11+
dims: ArrayLike<integer>
12+
Indices of dimensions along which to reverse elements. If a dimension
13+
index is provided as an integer less than zero, the dimension index is
14+
resolved relative to the last dimension, with the last dimension
15+
corresponding to the value `-1`.
16+
17+
Returns
18+
-------
19+
out: ndarray
20+
Output array.
21+
22+
Examples
23+
--------
24+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] )
25+
<ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
26+
> var y = {{alias}}( x, [ 0, 1 ] )
27+
<ndarray>[ [ 4.0, 3.0 ], [ 2.0, 1.0 ] ]
28+
29+
See Also
30+
--------
31+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
import { Collection } from '@stdlib/types/array';
25+
26+
/**
27+
* Returns a new ndarray where the order of elements of an input ndarray along specified dimensions is reversed.
28+
*
29+
* @param x - input array
30+
* @param dims - indices of dimensions to reverse
31+
* @returns output array
32+
*
33+
* @example
34+
* var array = require( '@stdlib/ndarray/array' );
35+
*
36+
* var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] );
37+
* // returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
38+
*
39+
* var y = toReversedDimensions( x, [ 0, 1 ] );
40+
* // returns <ndarray>[ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ]
41+
*/
42+
declare function toReversedDimensions<T extends ndarray = ndarray>( x: T, dims: Collection<number> ): T;
43+
44+
45+
// EXPORTS //
46+
47+
export = toReversedDimensions;

0 commit comments

Comments
 (0)