Skip to content

Commit 3a7f770

Browse files
committed
feat: add array/base/strided2array5d
1 parent bbf1876 commit 3a7f770

File tree

10 files changed

+955
-0
lines changed

10 files changed

+955
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
# strided2array5d
22+
23+
> Convert a strided array to a five-dimensional nested array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var strided2array5d = require( '@stdlib/array/base/strided2array5d' );
37+
```
38+
39+
#### strided2array5d( x, shape, strides, offset )
40+
41+
Converts a strided array to a five-dimensional nested array.
42+
43+
```javascript
44+
var x = [ 1, 2, 3, 4, 5, 6 ];
45+
46+
var arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 6, 6, 6, 2, 1 ], 0 );
47+
// returns [ [ [ [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ] ] ]
48+
49+
arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 1, 1, 1, 1, 3 ], 0 );
50+
// returns [ [ [ [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] ] ] ]
51+
```
52+
53+
The function accepts the following arguments:
54+
55+
- **x**: input array.
56+
- **shape**: array shape.
57+
- **strides**: dimension strides.
58+
- **offset**: index of the first indexed value in the input array.
59+
60+
</section>
61+
62+
<!-- /.usage -->
63+
64+
<section class="notes">
65+
66+
## Notes
67+
68+
- The function assumes that the input array is [compatible][@stdlib/ndarray/base/assert/is-buffer-length-compatible] with the specified array shape, dimension strides, and index offset.
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<section class="examples">
75+
76+
## Examples
77+
78+
<!-- eslint no-undef: "error" -->
79+
80+
```javascript
81+
var zeroTo = require( '@stdlib/array/base/zero-to' );
82+
var numel = require( '@stdlib/ndarray/base/numel' );
83+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
84+
var strided2array5d = require( '@stdlib/array/base/strided2array5d' );
85+
86+
var shape = [ 1, 1, 3, 3, 3 ];
87+
88+
var x = zeroTo( numel( shape ) );
89+
console.log( x );
90+
91+
var y = strided2array5d( x, shape, shape2strides( shape, 'row-major' ), 0 );
92+
console.log( y );
93+
94+
y = strided2array5d( x, shape, shape2strides( shape, 'column-major' ), 0 );
95+
console.log( y );
96+
```
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
103+
104+
<section class="related">
105+
106+
</section>
107+
108+
<!-- /.related -->
109+
110+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
111+
112+
<section class="links">
113+
114+
[@stdlib/ndarray/base/assert/is-buffer-length-compatible]: https://github.com/stdlib-js/stdlib
115+
116+
</section>
117+
118+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var floor = require( '@stdlib/math/base/special/floor' );
27+
var zeroTo = require( '@stdlib/array/base/zero-to' );
28+
var numel = require( '@stdlib/ndarray/base/numel' );
29+
var orders = require( '@stdlib/ndarray/orders' );
30+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
31+
var pkg = require( './../package.json' ).name;
32+
var strided2array5d = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var ORDERS = orders();
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveIntegerArray} shape - array shape
47+
* @param {string} order - memory layout order
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( shape, order ) {
51+
var strides = shape2strides( shape, order );
52+
var x = zeroTo( numel( shape ) );
53+
return benchmark;
54+
55+
/**
56+
* Benchmark function.
57+
*
58+
* @private
59+
* @param {Benchmark} b - benchmark instance
60+
*/
61+
function benchmark( b ) {
62+
var out;
63+
var i0;
64+
var i1;
65+
var i2;
66+
var i3;
67+
var i4;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
out = strided2array5d( x, shape, strides, 0 );
73+
i4 = i % shape[ 0 ];
74+
i3 = i % shape[ 1 ];
75+
i2 = i % shape[ 2 ];
76+
i1 = i % shape[ 3 ];
77+
i0 = i % shape[ 4 ];
78+
if ( isnan( out[ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
79+
b.fail( 'should not return NaN' );
80+
}
81+
}
82+
b.toc();
83+
84+
i4 = i % shape[ 0 ];
85+
i3 = i % shape[ 1 ];
86+
i2 = i % shape[ 2 ];
87+
i1 = i % shape[ 3 ];
88+
i0 = i % shape[ 4 ];
89+
if ( isnan( out[ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
}
95+
}
96+
97+
98+
// MAIN //
99+
100+
/**
101+
* Main execution sequence.
102+
*
103+
* @private
104+
*/
105+
function main() {
106+
var min;
107+
var max;
108+
var ord;
109+
var sh;
110+
var N;
111+
var f;
112+
var i;
113+
var j;
114+
115+
min = 1; // 10^min
116+
max = 6; // 10^max
117+
118+
for ( j = 0; j < ORDERS.length; j++ ) {
119+
for ( i = min; i <= max; i++ ) {
120+
N = floor( pow( pow( 10, i ), 1.0/5.0 ) );
121+
sh = [ N, N, N, N, N ];
122+
ord = ORDERS[ j ];
123+
f = createBenchmark( sh, ord );
124+
bench( pkg+'::equidimensional:size='+numel( sh )+',order='+ord, f );
125+
}
126+
}
127+
}
128+
129+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( x, shape, strides, offset )
3+
Converts a strided array to a five-dimensional nested array.
4+
5+
The function assumes that the input array is compatible with the specified
6+
array shape, dimension strides, and index offset.
7+
8+
Parameters
9+
----------
10+
x: ArrayLikeObject
11+
Input array.
12+
13+
shape: Array<integer>
14+
Array shape.
15+
16+
strides: Array<integer>
17+
Dimension strides.
18+
19+
offset: integer
20+
Index of the first indexed value in the input array.
21+
22+
Examples
23+
--------
24+
> var x = [ 1.0, 2.0, 3.0, 4.0 ];
25+
> var y = {{alias}}( x, [ 1, 1, 1, 2, 2 ], [ 4, 4, 4, 2, 1 ], 0 )
26+
[ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ]
27+
28+
See Also
29+
--------
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 { Array5D, ArrayLike } from '@stdlib/types/array';
24+
import { Shape5D, Strides5D } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Converts a strided array to a five-dimensional nested array.
28+
*
29+
* ## Notes
30+
*
31+
* - The function assumes that the input array is compatible with the specified array shape, dimension strides, and index offset.
32+
*
33+
* @param x - input array
34+
* @param shape - array shape
35+
* @param strides - dimension strides
36+
* @param offset - index of the first indexed value in the input array
37+
* @returns five-dimensional nested array
38+
*
39+
* @example
40+
* var x = [ 1, 2, 3, 4, 5, 6 ];
41+
*
42+
* var arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 6, 6, 6, 2, 1 ], 0 );
43+
* // returns [ [ [ [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] ] ] ]
44+
*
45+
* @example
46+
* var x = [ 1, 2, 3, 4, 5, 6 ];
47+
*
48+
* var arr = strided2array5d( x, [ 1, 1, 1, 3, 2 ], [ 1, 1, 1, 1, 3 ], 0 );
49+
* // returns [ [ [ [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] ] ] ]
50+
*/
51+
declare function strided2array5d<T = unknown>( x: ArrayLike<T>, shape: Shape5D, strides: Strides5D, offset: number ): Array5D<T>;
52+
53+
54+
// EXPORTS //
55+
56+
export = strided2array5d;

0 commit comments

Comments
 (0)