Skip to content

Commit 11c92e5

Browse files
committed
feat: add array/base/fliplr3d
1 parent 12232dc commit 11c92e5

File tree

10 files changed

+695
-0
lines changed

10 files changed

+695
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# fliplr3d
22+
23+
> Reverse the order of elements along the last dimension of a three-dimensional nested input array.
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 fliplr3d = require( '@stdlib/array/base/fliplr3d' );
41+
```
42+
43+
#### fliplr3d( x )
44+
45+
Reverses the order of elements along the last dimension of a three-dimensional nested input array.
46+
47+
```javascript
48+
var out = fliplr3d( [ [ [ 1, 2 ], [ 3, 4 ] ] ] );
49+
// returns [ [ [ 2, 1 ], [ 4, 3 ] ] ]
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
</section>
61+
62+
<!-- /.notes -->
63+
64+
<!-- Package usage examples. -->
65+
66+
<section class="examples">
67+
68+
## Examples
69+
70+
<!-- eslint no-undef: "error" -->
71+
72+
```javascript
73+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
74+
var filled3dBy = require( '@stdlib/array/base/filled3d-by' );
75+
var fliplr3d = require( '@stdlib/array/base/fliplr3d' );
76+
77+
var x = filled3dBy( [ 3, 3, 3 ], discreteUniform( -50, 50 ) );
78+
console.log( x );
79+
80+
var y = fliplr3d( x );
81+
console.log( y );
82+
```
83+
84+
</section>
85+
86+
<!-- /.examples -->
87+
88+
<!-- 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. -->
89+
90+
<section class="references">
91+
92+
</section>
93+
94+
<!-- /.references -->
95+
96+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
97+
98+
<section class="related">
99+
100+
</section>
101+
102+
<!-- /.related -->
103+
104+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
105+
106+
<section class="links">
107+
108+
</section>
109+
110+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 uniform = require( '@stdlib/random/base/uniform' ).factory;
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var filled3dBy = require( '@stdlib/array/base/filled3d-by' );
29+
var numel = require( '@stdlib/ndarray/base/numel' );
30+
var pkg = require( './../package.json' ).name;
31+
var fliplr3d = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveIntegerArray} shape - array shape
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( shape ) {
44+
var x = filled3dBy( shape, uniform( -100.0, 100.0 ) );
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var out;
55+
var i0;
56+
var i1;
57+
var i2;
58+
var i;
59+
60+
b.tic();
61+
for ( i = 0; i < b.iterations; i++ ) {
62+
out = fliplr3d( x );
63+
i2 = i % shape[ 0 ];
64+
i1 = i % shape[ 1 ];
65+
i0 = i % shape[ 2 ];
66+
if ( isnan( out[ i2 ][ i1 ][ i0 ] ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
72+
i2 = i % shape[ 0 ];
73+
i1 = i % shape[ 1 ];
74+
i0 = i % shape[ 2 ];
75+
if ( isnan( out[ i2 ][ i1 ][ i0 ] ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var min;
93+
var max;
94+
var sh;
95+
var N;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
N = floor( pow( pow( 10, i ), 1.0/3.0 ) );
104+
sh = [ N, N, N ];
105+
f = createBenchmark( sh );
106+
bench( pkg+'::equidimensional:size='+numel( sh ), f );
107+
}
108+
}
109+
110+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( x )
3+
Reverses the order of elements along the last dimension of a three-
4+
dimensional nested input array.
5+
6+
The function does *not* perform a deep copy of nested array elements.
7+
8+
Parameters
9+
----------
10+
x: ArrayLikeObject
11+
Input nested array.
12+
13+
Returns
14+
-------
15+
out: Array
16+
Output array.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( [ [ [ 1, 2 ], [ 3, 4 ] ] ] )
21+
[ [ [ 2, 1 ], [ 4, 3 ] ] ]
22+
23+
See Also
24+
--------
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 { Array3D } from '@stdlib/types/array';
24+
25+
/**
26+
* Reverses the order of elements along the last dimension of a three-dimensional nested input array.
27+
*
28+
* ## Notes
29+
*
30+
* - The function does **not** perform a deep copy of nested array elements.
31+
*
32+
* @param x - input nested array
33+
* @returns output array
34+
*
35+
* @example
36+
* var x = [ [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ];
37+
*
38+
* var out = fliplr3d( x );
39+
* // returns [ [ [ 2, 1 ], [ 4, 3 ], [ 6, 5 ] ] ]
40+
*/
41+
declare function fliplr3d<T = unknown>( x: Array3D<T> ): Array3D<T>;
42+
43+
44+
// EXPORTS //
45+
46+
export = fliplr3d;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
import fliplr3d = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a nested array...
25+
{
26+
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
27+
28+
fliplr3d( x ); // $ExpectType Array3D<number>
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not a nested array...
32+
{
33+
fliplr3d( 'abc' ); // $ExpectError
34+
fliplr3d( 1 ); // $ExpectError
35+
fliplr3d( true ); // $ExpectError
36+
fliplr3d( false ); // $ExpectError
37+
fliplr3d( null ); // $ExpectError
38+
fliplr3d( {} ); // $ExpectError
39+
fliplr3d( ( x: number ): number => x ); // $ExpectError
40+
}
41+
42+
// The compiler throws an error if the function is provided an unsupported number of arguments...
43+
{
44+
const x = [ [ [ 1, 2 ], [ 3, 4 ] ] ];
45+
46+
fliplr3d(); // $ExpectError
47+
fliplr3d( x, 2 ); // $ExpectError
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
22+
var filled3dBy = require( '@stdlib/array/base/filled3d-by' );
23+
var fliplr3d = require( './../lib' );
24+
25+
var x = filled3dBy( [ 3, 3, 3 ], discreteUniform( -50, 50 ) );
26+
console.log( x );
27+
28+
var y = fliplr3d( x );
29+
console.log( y );

0 commit comments

Comments
 (0)