Skip to content

Commit 807dbee

Browse files
committed
feat: add array/base/map5d
1 parent 755cb43 commit 807dbee

File tree

14 files changed

+1942
-0
lines changed

14 files changed

+1942
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
# map5d
22+
23+
> Apply a function to elements in a five-dimensional nested input array and assign results to elements in a new five-dimensional nested output 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 map5d = require( '@stdlib/array/base/map5d' );
37+
```
38+
39+
#### map5d( x, shape, fcn\[, thisArg] )
40+
41+
Applies a function to elements in a five-dimensional nested input array and assigns results to elements in a new five-dimensional nested output array.
42+
43+
```javascript
44+
var naryFunction = require( '@stdlib/utils/nary-function' );
45+
var abs = require( '@stdlib/math/base/special/abs' );
46+
47+
var x = [ [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ] ];
48+
var shape = [ 1, 1, 1, 2, 2 ];
49+
50+
var y = map5d( x, shape, naryFunction( abs, 1 ) );
51+
// returns [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ]
52+
```
53+
54+
The function accepts the following arguments:
55+
56+
- **x**: input nested array.
57+
- **shape**: array shape.
58+
- **fcn**: function to apply.
59+
- **thisArg**: applied function execution context (_optional_).
60+
61+
To set the applied function's execution context, provide a `thisArg`.
62+
63+
<!-- eslint-disable no-invalid-this -->
64+
65+
```javascript
66+
function fcn( x ) {
67+
this.count += 1;
68+
return x;
69+
}
70+
71+
var x = [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ];
72+
var shape = [ 1, 1, 1, 2, 2 ];
73+
74+
var ctx = {
75+
'count': 0
76+
};
77+
78+
var y = map5d( x, shape, fcn, ctx );
79+
// returns [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ]
80+
81+
var v = ctx.count;
82+
// returns 4
83+
```
84+
85+
#### map5d.assign( x, y, shape, fcn\[, thisArg] )
86+
87+
Applies a function to elements in a five-dimensional nested input array and assigns results to elements in a five-dimensional nested output array.
88+
89+
```javascript
90+
var naryFunction = require( '@stdlib/utils/nary-function' );
91+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
92+
var abs = require( '@stdlib/math/base/special/abs' );
93+
94+
var x = [ [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ] ];
95+
var shape = [ 1, 1, 1, 2, 2 ];
96+
97+
var y = zeros5d( shape );
98+
99+
var out = map5d.assign( x, y, shape, naryFunction( abs, 1 ) );
100+
// returns [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ]
101+
102+
var bool = ( out === y );
103+
// returns true
104+
```
105+
106+
The function accepts the following arguments:
107+
108+
- **x**: input nested array.
109+
- **y**: output nested array.
110+
- **shape**: array shape.
111+
- **fcn**: function to apply.
112+
- **thisArg**: applied function execution context (_optional_).
113+
114+
The function assumes that the input and output arrays have the same shape.
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<section class="notes">
121+
122+
</section>
123+
124+
<!-- /.notes -->
125+
126+
<section class="examples">
127+
128+
## Examples
129+
130+
<!-- eslint no-undef: "error" -->
131+
132+
```javascript
133+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
134+
var filled5dBy = require( '@stdlib/array/base/filled5d-by' );
135+
var naryFunction = require( '@stdlib/utils/nary-function' );
136+
var abs = require( '@stdlib/math/base/special/abs' );
137+
var map5d = require( '@stdlib/array/base/map5d' );
138+
139+
var shape = [ 1, 1, 3, 3, 3 ];
140+
141+
var x = filled5dBy( shape, discreteUniform( -100, 100 ) );
142+
console.log( x );
143+
144+
var y = map5d( x, shape, naryFunction( abs, 1 ) );
145+
console.log( y );
146+
```
147+
148+
</section>
149+
150+
<!-- /.examples -->
151+
152+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
153+
154+
<section class="related">
155+
156+
</section>
157+
158+
<!-- /.related -->
159+
160+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
161+
162+
<section class="links">
163+
164+
</section>
165+
166+
<!-- /.links -->
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 identity = require( '@stdlib/math/base/special/identity' );
29+
var filled5dBy = require( '@stdlib/array/base/filled5d-by' );
30+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
31+
var numel = require( '@stdlib/ndarray/base/numel' );
32+
var pkg = require( './../package.json' ).name;
33+
var map5d = require( './../lib' ).assign;
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveIntegerArray} shape - array shape
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( shape ) {
46+
var x;
47+
var y;
48+
49+
x = filled5dBy( shape, uniform( -100.0, 100.0 ) );
50+
y = zeros5d( shape );
51+
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var out;
62+
var i0;
63+
var i1;
64+
var i2;
65+
var i3;
66+
var i4;
67+
var i;
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
out = map5d( x, y, shape, identity );
72+
i4 = i % shape[ 0 ];
73+
i3 = i % shape[ 1 ];
74+
i2 = i % shape[ 2 ];
75+
i1 = i % shape[ 3 ];
76+
i0 = i % shape[ 4 ];
77+
if ( isnan( out[ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
83+
i4 = i % shape[ 0 ];
84+
i3 = i % shape[ 1 ];
85+
i2 = i % shape[ 2 ];
86+
i1 = i % shape[ 3 ];
87+
i0 = i % shape[ 4 ];
88+
if ( isnan( out[ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
b.pass( 'benchmark finished' );
92+
b.end();
93+
}
94+
}
95+
96+
97+
// MAIN //
98+
99+
/**
100+
* Main execution sequence.
101+
*
102+
* @private
103+
*/
104+
function main() {
105+
var min;
106+
var max;
107+
var sh;
108+
var N;
109+
var f;
110+
var i;
111+
112+
min = 1; // 10^min
113+
max = 6; // 10^max
114+
115+
for ( i = min; i <= max; i++ ) {
116+
N = floor( pow( pow( 10, i ), 1.0/5.0 ) );
117+
sh = [ N, N, N, N, N ];
118+
f = createBenchmark( sh );
119+
bench( pkg+'::equidimensional:assign:size='+numel( sh ), f );
120+
}
121+
}
122+
123+
main();

0 commit comments

Comments
 (0)