Skip to content

Commit 81f2bfb

Browse files
committed
feat: add ndarray/base/reverse-dimensions
--- 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 b665247 commit 81f2bfb

10 files changed

Lines changed: 1552 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
# reverseDimensions
22+
23+
> Return a view of an input ndarray in which the order of elements along specified dimensions 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 reverseDimensions = require( '@stdlib/ndarray/base/reverse-dimensions' );
41+
```
42+
43+
#### reverseDimensions( x, dims, writable )
44+
45+
Returns a view of an input ndarray in which the order of elements 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 = reverseDimensions( x, [ 0, 1 ], false );
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.
60+
- **dims**: dimension indices 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`.
61+
- **writable**: boolean indicating whether a returned ndarray should be writable.
62+
63+
</section>
64+
65+
<!-- /.usage -->
66+
67+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
68+
69+
<section class="notes">
70+
71+
## Notes
72+
73+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<!-- Package usage examples. -->
80+
81+
<section class="examples">
82+
83+
## Examples
84+
85+
<!-- eslint no-undef: "error" -->
86+
87+
```javascript
88+
var array = require( '@stdlib/ndarray/array' );
89+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
90+
var zeroTo = require( '@stdlib/array/base/zero-to' );
91+
var reverseDimensions = require( '@stdlib/ndarray/base/reverse-dimensions' );
92+
93+
// Create a linear ndarray buffer:
94+
var buf = zeroTo( 16 );
95+
96+
// Create a three-dimensional ndarray:
97+
var x = array( buf, {
98+
'shape': [ 2, 4, 2 ]
99+
});
100+
// returns <ndarray>[ [ [ 0.0, 1.0 ], [ 2.0, 3.0 ], [ 4.0, 5.0 ], [ 6.0, 7.0 ] ], [ [ 8.0, 9.0 ], [ 10.0, 11.0 ], [ 12.0, 13.0 ], [ 14.0, 15.0 ] ] ]
101+
102+
// Reverse elements across all dimensions:
103+
var y = reverseDimensions( x, [ 0, 1, 2 ], false );
104+
// returns <ndarray>[ [ [ 15.0, 14.0 ], [ 13.0, 12.0 ], [ 11.0, 10.0 ], [ 9.0, 8.0 ] ], [ [ 7.0, 6.0 ], [ 5.0, 4.0 ], [ 3.0, 2.0 ], [ 1.0, 0.0 ] ] ]
105+
106+
console.log( ndarray2array( y ) );
107+
```
108+
109+
</section>
110+
111+
<!-- /.examples -->
112+
113+
<!-- 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. -->
114+
115+
<section class="references">
116+
117+
</section>
118+
119+
<!-- /.references -->
120+
121+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
122+
123+
<section class="related">
124+
125+
</section>
126+
127+
<!-- /.related -->
128+
129+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
130+
131+
<section class="links">
132+
133+
</section>
134+
135+
<!-- /.links -->

0 commit comments

Comments
 (0)