Skip to content

Commit 9e31f2f

Browse files
committed
feat: add ndarray/base/to-flippedud
--- 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 e7fbc24 commit 9e31f2f

File tree

10 files changed

+891
-0
lines changed

10 files changed

+891
-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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 zeroTo = require( '@stdlib/array/base/zero-to' );
26+
var baseCtor = require( '@stdlib/ndarray/base/ctor' );
27+
var pkg = require( './../package.json' ).name;
28+
var toFlippedud = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg+':ndims=1', function benchmark( b ) {
34+
var values;
35+
var out;
36+
var i;
37+
38+
values = [
39+
baseCtor( 'float64', zeroTo( 10 ), [ 10 ], [ 1 ], 0, 'row-major' ),
40+
baseCtor( 'float64', zeroTo( 5 ), [ 5 ], [ 1 ], 0, 'row-major' ),
41+
baseCtor( 'float64', zeroTo( 20 ), [ 20 ], [ 1 ], 0, 'row-major' )
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
out = toFlippedud( values[ i%values.length ] );
47+
if ( typeof out !== 'object' ) {
48+
b.fail( 'should return an ndarray' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isndarrayLike( out ) ) {
53+
b.fail( 'should return an ndarray' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
58+
59+
bench( pkg+':ndims=2,len=100', function benchmark( b ) {
60+
var values;
61+
var out;
62+
var i;
63+
64+
values = [
65+
baseCtor( 'float64', zeroTo( 100 ), [ 10, 10 ], [ 10, 1 ], 0, 'row-major' ),
66+
baseCtor( 'float64', zeroTo( 100 ), [ 5, 20 ], [ 20, 1 ], 0, 'row-major' ),
67+
baseCtor( 'float64', zeroTo( 100 ), [ 20, 5 ], [ 5, 1 ], 0, 'row-major' )
68+
];
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
out = toFlippedud( values[ i%values.length ] );
73+
if ( typeof out !== 'object' ) {
74+
b.fail( 'should return an ndarray' );
75+
}
76+
}
77+
b.toc();
78+
if ( !isndarrayLike( out ) ) {
79+
b.fail( 'should return an ndarray' );
80+
}
81+
b.pass( 'benchmark finished' );
82+
b.end();
83+
});
84+
85+
bench( pkg+':ndims=3,len=1000', function benchmark( b ) {
86+
var values;
87+
var out;
88+
var i;
89+
90+
values = [
91+
baseCtor( 'float64', zeroTo( 1000 ), [ 10, 10, 10 ], [ 100, 10, 1 ], 0, 'row-major' ),
92+
baseCtor( 'float64', zeroTo( 1000 ), [ 5, 20, 10 ], [ 200, 10, 1 ], 0, 'row-major' ),
93+
baseCtor( 'float64', zeroTo( 1000 ), [ 20, 5, 10 ], [ 50, 10, 1 ], 0, 'row-major' )
94+
];
95+
96+
b.tic();
97+
for ( i = 0; i < b.iterations; i++ ) {
98+
out = toFlippedud( values[ i%values.length ] );
99+
if ( typeof out !== 'object' ) {
100+
b.fail( 'should return an ndarray' );
101+
}
102+
}
103+
b.toc();
104+
if ( !isndarrayLike( out ) ) {
105+
b.fail( 'should return an ndarray' );
106+
}
107+
b.pass( 'benchmark finished' );
108+
b.end();
109+
});
110+
111+
bench( pkg+':ndims=2,len=10000', function benchmark( b ) {
112+
var values;
113+
var out;
114+
var i;
115+
116+
values = [
117+
baseCtor( 'float64', zeroTo( 10000 ), [ 100, 100 ], [ 100, 1 ], 0, 'row-major' ),
118+
baseCtor( 'float64', zeroTo( 10000 ), [ 50, 200 ], [ 200, 1 ], 0, 'row-major' ),
119+
baseCtor( 'float64', zeroTo( 10000 ), [ 200, 50 ], [ 50, 1 ], 0, 'row-major' )
120+
];
121+
122+
b.tic();
123+
for ( i = 0; i < b.iterations; i++ ) {
124+
out = toFlippedud( values[ i%values.length ] );
125+
if ( typeof out !== 'object' ) {
126+
b.fail( 'should return an ndarray' );
127+
}
128+
}
129+
b.toc();
130+
if ( !isndarrayLike( out ) ) {
131+
b.fail( 'should return an ndarray' );
132+
}
133+
b.pass( 'benchmark finished' );
134+
b.end();
135+
});
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+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a new ndarray where the order of elements along the second-to-last dimension of an input ndarray is reversed.
27+
*
28+
* @param x - input array
29+
* @returns output array
30+
*
31+
* @example
32+
* var typedarray = require( '@stdlib/array/typed' );
33+
* var ndarray = require( '@stdlib/ndarray/ctor' );
34+
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
35+
*
36+
* var buffer = typedarray( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ], 'float64' );
37+
* var shape = [ 3, 2 ];
38+
* var strides = [ 2, 1 ];
39+
* var offset = 0;
40+
*
41+
* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' );
42+
* // returns <ndarray>
43+
*
44+
* var sh = x.shape;
45+
* // returns [ 3, 2 ]
46+
*
47+
* var arr = ndarray2array( x );
48+
* // returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
49+
*
50+
* var y = toFlippedud( x );
51+
* // returns <ndarray>
52+
*
53+
* sh = y.shape;
54+
* // returns [ 3, 2 ]
55+
*
56+
* arr = ndarray2array( y );
57+
* // returns [ [ 5.0, 6.0 ], [ 3.0, 4.0 ], [ 1.0, 2.0 ] ]
58+
*/
59+
declare function toFlippedud<T extends ndarray>( x: T ): T;
60+
61+
62+
// EXPORTS //
63+
64+
export = toFlippedud;

0 commit comments

Comments
 (0)