Skip to content

Commit 8bcb738

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/to-reversed
PR-URL: #2861 Ref: #2656 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 1233cc4 commit 8bcb738

File tree

12 files changed

+1227
-434
lines changed

12 files changed

+1227
-434
lines changed

lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/index.d.ts

Lines changed: 2 additions & 434 deletions
Large diffs are not rendered by default.

lib/node_modules/@stdlib/ndarray/base/reverse/docs/types/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import reverse = require( './index' );
3838
reverse( empty( 'uint16', sh, order ), false ); // $ExpectType uint16ndarray
3939
reverse( empty( 'uint8', sh, order ), false ); // $ExpectType uint8ndarray
4040
reverse( empty( 'uint8c', sh, order ), false ); // $ExpectType uint8cndarray
41+
reverse( empty( 'generic', sh, order ), false ); // $ExpectType genericndarray<number>
4142

4243
reverse( empty( 'float64', sh, order ), true ); // $ExpectType float64ndarray
4344
reverse( empty( 'float32', sh, order ), true ); // $ExpectType float32ndarray
@@ -50,6 +51,7 @@ import reverse = require( './index' );
5051
reverse( empty( 'uint16', sh, order ), true ); // $ExpectType uint16ndarray
5152
reverse( empty( 'uint8', sh, order ), true ); // $ExpectType uint8ndarray
5253
reverse( empty( 'uint8c', sh, order ), true ); // $ExpectType uint8cndarray
54+
reverse( empty( 'generic', sh, order ), false ); // $ExpectType genericndarray<number>
5355
}
5456

5557
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# toReversed
22+
23+
> Return a new ndarray where the order of elements of an input ndarray is reversed along each dimension.
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 toReversed = require( '@stdlib/ndarray/base/to-reversed' );
41+
```
42+
43+
#### toReversed( x )
44+
45+
Returns a new ndarray where the order of elements of an input ndarray is reversed along each dimension.
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 = toReversed( x );
66+
// returns <ndarray>
67+
68+
sh = y.shape;
69+
// returns [ 3, 2 ]
70+
71+
arr = ndarray2array( y );
72+
// returns [ [ 6.0, 5.0 ], [ 4.0, 3.0 ], [ 2.0, 1.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+
</section>
88+
89+
<!-- /.notes -->
90+
91+
<!-- Package usage examples. -->
92+
93+
<section class="examples">
94+
95+
## Examples
96+
97+
<!-- eslint no-undef: "error" -->
98+
99+
```javascript
100+
var array = require( '@stdlib/ndarray/array' );
101+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
102+
var zeroTo = require( '@stdlib/array/base/zero-to' );
103+
var toReversed = require( '@stdlib/ndarray/base/to-reversed' );
104+
105+
// Create a linear ndarray buffer:
106+
var buf = zeroTo( 16 );
107+
108+
// Create a one-dimensional ndarray:
109+
var x1 = array( buf, {
110+
'shape': [ 16 ]
111+
});
112+
113+
// Reverse element order:
114+
var y1 = toReversed( x1, false );
115+
// returns <ndarray>
116+
117+
var a1 = ndarray2array( y1 );
118+
// returns [ 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ]
119+
120+
// Create a two-dimensional ndarray:
121+
var x2 = array( buf, {
122+
'shape': [ 4, 4 ]
123+
});
124+
125+
// Reverse element order:
126+
var y2 = toReversed( x2, false );
127+
// returns <ndarray>
128+
129+
var a2 = ndarray2array( y2 );
130+
// returns [ [ 15, 14, 13, 12 ], [ 11, 10, 9, 8 ], [ 7, 6, 5, 4 ], [ 3, 2, 1, 0 ] ]
131+
132+
// Create a three-dimensional ndarray:
133+
var x3 = array( buf, {
134+
'shape': [ 2, 4, 2 ]
135+
});
136+
137+
// Reverse element order:
138+
var y3 = toReversed( x3, false );
139+
// returns <ndarray>
140+
141+
var a3 = ndarray2array( y3 );
142+
// returns [ [ [ 15, 14 ], [ 13, 12 ], [ 11, 10 ], [ 9, 8 ] ], [ [ 7, 6 ], [ 5, 4 ], [ 3, 2 ], [ 1, 0 ] ] ]
143+
```
144+
145+
</section>
146+
147+
<!-- /.examples -->
148+
149+
<!-- 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. -->
150+
151+
<section class="references">
152+
153+
</section>
154+
155+
<!-- /.references -->
156+
157+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
158+
159+
<section class="related">
160+
161+
</section>
162+
163+
<!-- /.related -->
164+
165+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
166+
167+
<section class="links">
168+
169+
</section>
170+
171+
<!-- /.links -->

0 commit comments

Comments
 (0)