Skip to content

Commit 95ee555

Browse files
committed
feat: add ndarray/reverse
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 0562a32 commit 95ee555

File tree

10 files changed

+897
-0
lines changed

10 files changed

+897
-0
lines changed
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) 2025 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+
# reverse
22+
23+
> Return a **read-only** view of an input ndarray in which the order of elements along each dimension 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 reverse = require( '@stdlib/ndarray/reverse' );
41+
```
42+
43+
#### reverse( x )
44+
45+
Returns a read-only view of an input ndarray in which the order of elements along each dimension is reversed.
46+
47+
```javascript
48+
var ndarray = require( '@stdlib/ndarray/ctor' );
49+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
50+
51+
var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
52+
var shape = [ 3, 2 ];
53+
var strides = [ 2, 1 ];
54+
var offset = 0;
55+
56+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
57+
// returns <ndarray>
58+
59+
var arr = ndarray2array( x );
60+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
61+
62+
var y = reverse( x );
63+
// returns <ndarray>
64+
65+
arr = ndarray2array( y );
66+
// returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.0 ] ]
67+
```
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
74+
75+
<section class="notes">
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 ndarray = require( '@stdlib/ndarray/ctor' );
91+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
92+
var reverse = require( '@stdlib/ndarray/reverse' );
93+
94+
var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ];
95+
var shape = [ 2, 2, 2 ];
96+
var strides = [ 4, 2, 1 ];
97+
var offset = 0;
98+
99+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
100+
// returns <ndarray>
101+
102+
console.log( ndarray2array( x ) );
103+
104+
var y = reverse( x );
105+
// returns <ndarray>
106+
107+
console.log( ndarray2array( y ) );
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: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 pkg = require( './../package.json' ).name;
27+
var reverse = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+'::0d', function benchmark( b ) {
33+
var values;
34+
var v;
35+
var i;
36+
37+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
38+
39+
values = [
40+
41+
empty( [], { 'dtype': 'float64' } ),
42+
empty( [], { 'dtype': 'float32' } ),
43+
empty( [], { 'dtype': 'int32' } ),
44+
empty( [], { 'dtype': 'complex128' } ),
45+
empty( [], { '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 = reverse( 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( pkg+'::1d', 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 ], { 'dtype': 'float64' } ),
74+
empty( [ 2 ], { 'dtype': 'float32' } ),
75+
empty( [ 2 ], { 'dtype': 'int32' } ),
76+
empty( [ 2 ], { 'dtype': 'complex128' } ),
77+
empty( [ 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 = reverse( 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( pkg+'::2d', function benchmark( b ) {
98+
var values;
99+
var v;
100+
var i;
101+
102+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
103+
104+
values = [
105+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
106+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
107+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
108+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
109+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
110+
];
111+
112+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
113+
114+
b.tic();
115+
for ( i = 0; i < b.iterations; i++ ) {
116+
v = reverse( values[ i%values.length ] );
117+
if ( typeof v !== 'object' ) {
118+
b.fail( 'should return an ndarray' );
119+
}
120+
}
121+
b.toc();
122+
if ( !isndarrayLike( v ) ) {
123+
b.fail( 'should return an ndarray' );
124+
}
125+
b.pass( 'benchmark finished' );
126+
b.end();
127+
});
128+
129+
bench( pkg+'::3d', function benchmark( b ) {
130+
var values;
131+
var v;
132+
var i;
133+
134+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
135+
136+
values = [
137+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
138+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
139+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
140+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
141+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
142+
];
143+
144+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
145+
146+
b.tic();
147+
for ( i = 0; i < b.iterations; i++ ) {
148+
v = reverse( values[ i%values.length ] );
149+
if ( typeof v !== 'object' ) {
150+
b.fail( 'should return an ndarray' );
151+
}
152+
}
153+
b.toc();
154+
if ( !isndarrayLike( v ) ) {
155+
b.fail( 'should return an ndarray' );
156+
}
157+
b.pass( 'benchmark finished' );
158+
b.end();
159+
});
160+
161+
bench( pkg+'::4d', function benchmark( b ) {
162+
var values;
163+
var v;
164+
var i;
165+
166+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
167+
168+
values = [
169+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
170+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
171+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
172+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
173+
empty( [ 2, 2, 2, 2 ], { 'dtype': 'generic' } )
174+
];
175+
176+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
177+
178+
b.tic();
179+
for ( i = 0; i < b.iterations; i++ ) {
180+
v = reverse( values[ i%values.length ] );
181+
if ( typeof v !== 'object' ) {
182+
b.fail( 'should return an ndarray' );
183+
}
184+
}
185+
b.toc();
186+
if ( !isndarrayLike( v ) ) {
187+
b.fail( 'should return an ndarray' );
188+
}
189+
b.pass( 'benchmark finished' );
190+
b.end();
191+
});
192+
193+
bench( pkg+'::5d', function benchmark( b ) {
194+
var values;
195+
var v;
196+
var i;
197+
198+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
199+
200+
values = [
201+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float64' } ),
202+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'float32' } ),
203+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'int32' } ),
204+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'complex128' } ),
205+
empty( [ 2, 2, 2, 2, 2 ], { 'dtype': 'generic' } )
206+
];
207+
208+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
209+
210+
b.tic();
211+
for ( i = 0; i < b.iterations; i++ ) {
212+
v = reverse( values[ i%values.length ] );
213+
if ( typeof v !== 'object' ) {
214+
b.fail( 'should return an ndarray' );
215+
}
216+
}
217+
b.toc();
218+
if ( !isndarrayLike( v ) ) {
219+
b.fail( 'should return an ndarray' );
220+
}
221+
b.pass( 'benchmark finished' );
222+
b.end();
223+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{{alias}}( x )
2+
Returns a read-only view of an input ndarray in which the order of
3+
elements along each dimension is reversed.
4+
5+
Parameters
6+
----------
7+
x: ndarray
8+
Input array.
9+
10+
Returns
11+
-------
12+
out: ndarray
13+
A read-only view of an input ndarray in which the order of elements
14+
along each dimension is reversed.
15+
16+
Examples
17+
--------
18+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ] ] )
19+
<ndarray>
20+
> {{alias:@stdlib/ndarray/to-array}}( x )
21+
[ [ 1, 2 ], [ 3, 4 ] ]
22+
> var y = {{alias}}( x )
23+
<ndarray>
24+
> {{alias:@stdlib/ndarray/to-array}}( y )
25+
[ [ 4, 3 ], [ 2, 1 ] ]
26+
27+
See Also
28+
--------

0 commit comments

Comments
 (0)