Skip to content

Commit 1080085

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/to-flippedlr
PR-URL: #8848 Closes: stdlib-js/metr-issue-tracker#110 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 3a07ca1 commit 1080085

File tree

10 files changed

+1092
-0
lines changed

10 files changed

+1092
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
# toFlippedlr
22+
23+
> Return a new ndarray where the order of elements along the last dimension of an input ndarray 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 toFlippedlr = require( '@stdlib/ndarray/base/to-flippedlr' );
41+
```
42+
43+
#### toFlippedlr( x )
44+
45+
Returns a new ndarray where the order of elements along the last dimension of an input ndarray 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 sh = x.shape;
60+
// returns [ 3, 2 ]
61+
62+
var arr = ndarray2array( x );
63+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
64+
65+
var y = toFlippedlr( x );
66+
// returns <ndarray>
67+
68+
sh = y.shape;
69+
// returns [ 3, 2 ]
70+
71+
arr = ndarray2array( y );
72+
// returns [ [ 2.0, 1.0 ], [ 4.0, 3.0 ], [ 6.0, 5.0 ] ]
73+
```
74+
75+
The function accepts the following arguments:
76+
77+
- **x**: input ndarray.
78+
79+
</section>
80+
81+
<!-- /.usage -->
82+
83+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
84+
85+
<section class="notes">
86+
87+
## Notes
88+
89+
- If provided a zero-dimensional ndarray, as the ndarray has no dimensions to reverse, the function simply returns a copy of the input ndarray.
90+
91+
</section>
92+
93+
<!-- /.notes -->
94+
95+
<!-- Package usage examples. -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint no-undef: "error" -->
102+
103+
```javascript
104+
var array = require( '@stdlib/ndarray/array' );
105+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
106+
var zeroTo = require( '@stdlib/array/base/zero-to' );
107+
var toFlippedlr = require( '@stdlib/ndarray/base/to-flippedlr' );
108+
109+
// Create a linear ndarray buffer:
110+
var buf = zeroTo( 16 );
111+
112+
// Create a three-dimensional ndarray:
113+
var x = array( buf, {
114+
'shape': [ 2, 4, 2 ]
115+
});
116+
117+
// Reverse the order of last dimension:
118+
var y = toFlippedlr( x );
119+
// returns <ndarray>
120+
121+
var a = ndarray2array( y );
122+
console.log( a );
123+
// => [ [ [ 1, 0 ], [ 3, 2 ], [ 5, 4 ], [ 7, 6 ] ], [ [ 9, 8 ], [ 11, 10 ], [ 13, 12 ], [ 15, 14 ] ] ]
124+
```
125+
126+
</section>
127+
128+
<!-- /.examples -->
129+
130+
<!-- 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. -->
131+
132+
<section class="references">
133+
134+
</section>
135+
136+
<!-- /.references -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
</section>
151+
152+
<!-- /.links -->

0 commit comments

Comments
 (0)