Skip to content

Commit f375981

Browse files
headlessNodekgryte
andauthored
feat: add ndarray/base/to-flippedud
PR-URL: #8849 Closes: stdlib-js/metr-issue-tracker#111 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent 9df0e8d commit f375981

File tree

10 files changed

+971
-0
lines changed

10 files changed

+971
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
# toFlippedud
22+
23+
> Return a new ndarray where the order of elements along the second-to-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 toFlippedud = require( '@stdlib/ndarray/base/to-flippedud' );
41+
```
42+
43+
#### toFlippedud( x )
44+
45+
Returns a new ndarray where the order of elements along the second-to-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 = toFlippedud( x );
66+
// returns <ndarray>
67+
68+
sh = y.shape;
69+
// returns [ 3, 2 ]
70+
71+
arr = ndarray2array( y );
72+
// returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.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 the input ndarray. Similarly, if provided a one-dimensional ndarray, as the ndarray has only one dimension, 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+
```javascript
102+
var array = require( '@stdlib/ndarray/array' );
103+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
104+
var zeroTo = require( '@stdlib/array/base/zero-to' );
105+
var toFlippedud = require( '@stdlib/ndarray/base/to-flippedud' );
106+
107+
// Create a linear ndarray buffer:
108+
var buf = zeroTo( 16 );
109+
110+
// Create a three-dimensional ndarray:
111+
var x = array( buf, {
112+
'shape': [ 2, 4, 2 ]
113+
});
114+
115+
// Reverse the order of second-to-last dimension:
116+
var y = toFlippedud( x );
117+
// returns <ndarray>
118+
119+
var a = ndarray2array( y );
120+
console.log( a );
121+
// => [ [ [ 6, 7 ], [ 4, 5 ], [ 2, 3 ], [ 0, 1 ] ], [ [ 14, 15 ], [ 12, 13 ], [ 10, 11 ], [ 8, 9 ] ] ]
122+
```
123+
124+
</section>
125+
126+
<!-- /.examples -->
127+
128+
<!-- 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. -->
129+
130+
<section class="references">
131+
132+
</section>
133+
134+
<!-- /.references -->
135+
136+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
137+
138+
<section class="related">
139+
140+
</section>
141+
142+
<!-- /.related -->
143+
144+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
145+
146+
<section class="links">
147+
148+
</section>
149+
150+
<!-- /.links -->
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
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 baseEmpty = require( '@stdlib/ndarray/base/empty' );
26+
var empty = require( '@stdlib/ndarray/empty' );
27+
var pkg = require( './../package.json' ).name;
28+
var toFlippedud = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+'::1d,base', function benchmark( b ) {
34+
var values;
35+
var out;
36+
var i;
37+
38+
values = [
39+
baseEmpty( 'float64', [ 2 ], 'row-major' ),
40+
baseEmpty( 'float32', [ 2 ], 'row-major' ),
41+
baseEmpty( 'int32', [ 2 ], 'row-major' ),
42+
baseEmpty( 'complex128', [ 2 ], 'row-major' ),
43+
baseEmpty( 'generic', [ 2 ], 'row-major' )
44+
];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
out = toFlippedud( values[ i%values.length ] );
49+
if ( typeof out !== 'object' ) {
50+
b.fail( 'should return an ndarray' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isndarrayLike( out ) ) {
55+
b.fail( 'should return an ndarray' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( pkg+'::1d,non-base', function benchmark( b ) {
62+
var values;
63+
var out;
64+
var i;
65+
66+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
67+
68+
values = [
69+
empty( [ 2 ], { 'dtype': 'float64' } ),
70+
empty( [ 2 ], { 'dtype': 'float32' } ),
71+
empty( [ 2 ], { 'dtype': 'int32' } ),
72+
empty( [ 2 ], { 'dtype': 'complex128' } ),
73+
empty( [ 2 ], { 'dtype': 'generic' } )
74+
];
75+
76+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
out = toFlippedud( values[ i%values.length ] );
81+
if ( typeof out !== 'object' ) {
82+
b.fail( 'should return an ndarray' );
83+
}
84+
}
85+
b.toc();
86+
if ( !isndarrayLike( out ) ) {
87+
b.fail( 'should return an ndarray' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
});
92+
93+
bench( pkg+'::2d,base', function benchmark( b ) {
94+
var values;
95+
var out;
96+
var i;
97+
98+
values = [
99+
baseEmpty( 'float64', [ 2, 2 ], 'row-major' ),
100+
baseEmpty( 'float32', [ 2, 2 ], 'row-major' ),
101+
baseEmpty( 'int32', [ 2, 2 ], 'row-major' ),
102+
baseEmpty( 'complex128', [ 2, 2 ], 'row-major' ),
103+
baseEmpty( 'generic', [ 2, 2 ], 'row-major' )
104+
];
105+
106+
b.tic();
107+
for ( i = 0; i < b.iterations; i++ ) {
108+
out = toFlippedud( values[ i%values.length ] );
109+
if ( typeof out !== 'object' ) {
110+
b.fail( 'should return an ndarray' );
111+
}
112+
}
113+
b.toc();
114+
if ( !isndarrayLike( out ) ) {
115+
b.fail( 'should return an ndarray' );
116+
}
117+
b.pass( 'benchmark finished' );
118+
b.end();
119+
});
120+
121+
bench( pkg+'::2d,non-base', function benchmark( b ) {
122+
var values;
123+
var out;
124+
var i;
125+
126+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
127+
128+
values = [
129+
empty( [ 2, 2 ], { 'dtype': 'float64' } ),
130+
empty( [ 2, 2 ], { 'dtype': 'float32' } ),
131+
empty( [ 2, 2 ], { 'dtype': 'int32' } ),
132+
empty( [ 2, 2 ], { 'dtype': 'complex128' } ),
133+
empty( [ 2, 2 ], { 'dtype': 'generic' } )
134+
];
135+
136+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
137+
138+
b.tic();
139+
for ( i = 0; i < b.iterations; i++ ) {
140+
out = toFlippedud( values[ i%values.length ] );
141+
if ( typeof out !== 'object' ) {
142+
b.fail( 'should return an ndarray' );
143+
}
144+
}
145+
b.toc();
146+
if ( !isndarrayLike( out ) ) {
147+
b.fail( 'should return an ndarray' );
148+
}
149+
b.pass( 'benchmark finished' );
150+
b.end();
151+
});
152+
153+
bench( pkg+'::3d,base', function benchmark( b ) {
154+
var values;
155+
var out;
156+
var i;
157+
158+
values = [
159+
baseEmpty( 'float64', [ 2, 2, 2 ], 'row-major' ),
160+
baseEmpty( 'float32', [ 2, 2, 2 ], 'row-major' ),
161+
baseEmpty( 'int32', [ 2, 2, 2 ], 'row-major' ),
162+
baseEmpty( 'complex128', [ 2, 2, 2 ], 'row-major' ),
163+
baseEmpty( 'generic', [ 2, 2, 2 ], 'row-major' )
164+
];
165+
166+
b.tic();
167+
for ( i = 0; i < b.iterations; i++ ) {
168+
out = toFlippedud( values[ i%values.length ] );
169+
if ( typeof out !== 'object' ) {
170+
b.fail( 'should return an ndarray' );
171+
}
172+
}
173+
b.toc();
174+
if ( !isndarrayLike( out ) ) {
175+
b.fail( 'should return an ndarray' );
176+
}
177+
b.pass( 'benchmark finished' );
178+
b.end();
179+
});
180+
181+
bench( pkg+'::3d,non-base', function benchmark( b ) {
182+
var values;
183+
var out;
184+
var i;
185+
186+
/* eslint-disable object-curly-newline, stdlib/line-closing-bracket-spacing */
187+
188+
values = [
189+
empty( [ 2, 2, 2 ], { 'dtype': 'float64' } ),
190+
empty( [ 2, 2, 2 ], { 'dtype': 'float32' } ),
191+
empty( [ 2, 2, 2 ], { 'dtype': 'int32' } ),
192+
empty( [ 2, 2, 2 ], { 'dtype': 'complex128' } ),
193+
empty( [ 2, 2, 2 ], { 'dtype': 'generic' } )
194+
];
195+
196+
/* eslint-enable object-curly-newline, stdlib/line-closing-bracket-spacing */
197+
198+
b.tic();
199+
for ( i = 0; i < b.iterations; i++ ) {
200+
out = toFlippedud( values[ i%values.length ] );
201+
if ( typeof out !== 'object' ) {
202+
b.fail( 'should return an ndarray' );
203+
}
204+
}
205+
b.toc();
206+
if ( !isndarrayLike( out ) ) {
207+
b.fail( 'should return an ndarray' );
208+
}
209+
b.pass( 'benchmark finished' );
210+
b.end();
211+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( x )
3+
Returns a new ndarray where the order of elements along the second-to-last
4+
dimension of an input ndarray is reversed.
5+
6+
Parameters
7+
----------
8+
x: ndarray
9+
Input array.
10+
11+
Returns
12+
-------
13+
out: ndarray
14+
Output array.
15+
16+
Examples
17+
--------
18+
> var x = {{alias:@stdlib/ndarray/array}}( [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] )
19+
<ndarray>
20+
> x.shape
21+
[ 3, 2 ]
22+
> var y = {{alias}}( x )
23+
<ndarray>
24+
> y.shape
25+
[ 3, 2 ]
26+
> {{alias:@stdlib/ndarray/to-array}}( y )
27+
[ [ 5, 6 ], [ 3, 4 ], [ 1, 2 ] ]
28+
29+
See Also
30+
--------
31+

0 commit comments

Comments
 (0)