Skip to content

Commit 143b370

Browse files
feat: add array/base/unary4d-by
PR-URL: #3220 Closes: #3174 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 70aaa8c commit 143b370

File tree

10 files changed

+1210
-0
lines changed

10 files changed

+1210
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# unary4dBy
22+
23+
> Apply a unary function to each element retrieved from a four-dimensional nested input array according to a callback function and assign results to elements in a four-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 unary4dBy = require( '@stdlib/array/base/unary4d-by' );
37+
```
38+
39+
#### unary4dBy( arrays, shape, fcn, clbk\[, thisArg] )
40+
41+
Applies a unary function to each element retrieved from a four-dimensional nested input array according to a callback function and assigns results to elements in a four-dimensional nested output array.
42+
43+
```javascript
44+
var abs = require( '@stdlib/math/base/special/abs' );
45+
46+
function accessor( v ) {
47+
return v * 2.0;
48+
}
49+
50+
var x = [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ];
51+
var shape = [ 1, 1, 2, 2 ];
52+
53+
unary4dBy( [ x, x ], shape, abs, accessor );
54+
// x => [ [ [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ] ] ]
55+
```
56+
57+
The function accepts the following arguments:
58+
59+
- **arrays**: array-like object containing one input nested array and one output nested array.
60+
- **shape**: array shape.
61+
- **fcn**: unary function to apply to callback return values.
62+
- **clbk**: callback function.
63+
- **thisArg**: callback function execution context (optional).
64+
65+
The invoked callback function is provided the following arguments:
66+
67+
- **value**: array element.
68+
- **indices**: current array element indices.
69+
- **arrays**: input and output arrays.
70+
71+
To set the callback execution context, provide a `thisArg`.
72+
73+
<!-- eslint-disable no-invalid-this -->
74+
75+
```javascript
76+
var abs = require( '@stdlib/math/base/special/abs' );
77+
78+
function accessor( v ) {
79+
this.count += 1;
80+
return v * 2.0;
81+
}
82+
83+
var context = {
84+
'count': 0
85+
};
86+
87+
var x = [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ];
88+
var y = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ];
89+
90+
var shape = [ 1, 1, 2, 2 ];
91+
92+
unary4dBy( [ x, y ], shape, abs, accessor, context );
93+
// y => [ [ [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ] ] ]
94+
95+
var cnt = context.count;
96+
// returns 4
97+
```
98+
99+
</section>
100+
101+
<!-- /.usage -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
108+
109+
```javascript
110+
var abs = require( '@stdlib/math/base/special/abs' );
111+
112+
function accessor() {
113+
// No-op...
114+
}
115+
116+
var x = [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ];
117+
var y = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ];
118+
119+
var shape = [ 1, 1, 2, 2 ];
120+
121+
unary4dBy( [ x, y ], shape, abs, accessor );
122+
// y => [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ]
123+
```
124+
125+
- The function assumes that the input and output arrays have the same shape.
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<section class="examples">
132+
133+
## Examples
134+
135+
<!-- eslint no-undef: "error" -->
136+
137+
```javascript
138+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
139+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
140+
var filled4dBy = require( '@stdlib/array/base/filled4d-by' );
141+
var zeros4d = require( '@stdlib/array/base/zeros4d' );
142+
var abs = require( '@stdlib/math/base/special/abs' );
143+
var unary4dBy = require( '@stdlib/array/base/unary4d-by' );
144+
145+
function accessor( v ) {
146+
// Randomly determine whether a value should be considered "missing":
147+
return ( bernoulli( 0.5 ) > 0 ) ? v : void 0;
148+
}
149+
150+
var shape = [ 2, 2, 3, 3 ];
151+
152+
var x = filled4dBy( shape, discreteUniform( -100, 100 ) );
153+
console.log( x );
154+
155+
var y = zeros4d( shape );
156+
console.log( y );
157+
158+
unary4dBy( [ x, y ], shape, abs, accessor );
159+
console.log( y );
160+
```
161+
162+
</section>
163+
164+
<!-- /.examples -->
165+
166+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
167+
168+
<section class="related">
169+
170+
</section>
171+
172+
<!-- /.related -->
173+
174+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
175+
176+
<section class="links">
177+
178+
</section>
179+
180+
<!-- /.links -->
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 filled4dBy = require( '@stdlib/array/base/filled4d-by' );
30+
var zeros4d = require( '@stdlib/array/base/zeros4d' );
31+
var numel = require( '@stdlib/ndarray/base/numel' );
32+
var pkg = require( './../package.json' ).name;
33+
var unary4dBy = require( './../lib' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Accessor function.
40+
*
41+
* @private
42+
* @param {number} value - array element
43+
* @returns {number} accessed value
44+
*/
45+
function accessor( value ) {
46+
return value;
47+
}
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveIntegerArray} shape - array shape
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( shape ) {
57+
var arrays;
58+
var x;
59+
var y;
60+
61+
x = filled4dBy( shape, uniform( -100.0, 100.0 ) );
62+
y = zeros4d( shape );
63+
64+
arrays = [ x, y ];
65+
66+
return benchmark;
67+
68+
/**
69+
* Benchmark function.
70+
*
71+
* @private
72+
* @param {Benchmark} b - benchmark instance
73+
*/
74+
function benchmark( b ) {
75+
var i0;
76+
var i1;
77+
var i2;
78+
var i3;
79+
var i;
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
unary4dBy( arrays, shape, identity, accessor );
84+
i3 = i % shape[ 0 ];
85+
i2 = i % shape[ 1 ];
86+
i1 = i % shape[ 2 ];
87+
i0 = i % shape[ 3 ];
88+
if ( isnan( arrays[ 1 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
}
92+
b.toc();
93+
94+
i3 = i % shape[ 0 ];
95+
i2 = i % shape[ 1 ];
96+
i1 = i % shape[ 2 ];
97+
i0 = i % shape[ 3 ];
98+
if ( isnan( arrays[ 1 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
99+
b.fail( 'should not return NaN' );
100+
}
101+
b.pass( 'benchmark finished' );
102+
b.end();
103+
}
104+
}
105+
106+
107+
// MAIN //
108+
109+
/**
110+
* Main execution sequence.
111+
*
112+
* @private
113+
*/
114+
function main() {
115+
var min;
116+
var max;
117+
var sh;
118+
var N;
119+
var f;
120+
var i;
121+
122+
min = 1; // 10^min
123+
max = 6; // 10^max
124+
125+
for ( i = min; i <= max; i++ ) {
126+
N = floor( pow( pow( 10, i ), 1.0/4.0 ) );
127+
sh = [ N, N, N, N ];
128+
f = createBenchmark( sh );
129+
bench( pkg+'::equidimensional:size='+numel( sh ), f );
130+
}
131+
}
132+
133+
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( arrays, shape, fcn, clbk[, thisArg] )
3+
Applies a unary function to each element retrieved from a four-dimensional
4+
nested input array according to a callback function and assigns results to
5+
elements in a four-dimensional nested output array.
6+
7+
The callback function is provided the following arguments:
8+
9+
- value: array element.
10+
- indices: current array element indices.
11+
- arrays: input and output arrays.
12+
13+
If the callback function does not return any value (or equivalently,
14+
explicitly returns `undefined`), the value is ignored.
15+
16+
Parameters
17+
----------
18+
arrays: ArrayLikeObject
19+
Array-like object containing one input nested array and one output
20+
nested array.
21+
22+
shape: Array<integer>
23+
Array shape.
24+
25+
fcn: Function
26+
Unary function to apply to callback return values.
27+
28+
clbk: Function
29+
Callback function.
30+
31+
thisArg: any (optional)
32+
Callback execution context.
33+
34+
Examples
35+
--------
36+
> var x = [ [ [ [ -1.0, -2.0 ], [ -3.0, -4.0 ] ] ] ];
37+
> var y = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ];
38+
> var shape = [ 1, 1, 2, 2 ];
39+
> function clbk( v ) { return v * 2.0; };
40+
> {{alias}}( [ x, y ], shape, {{alias:@stdlib/math/base/special/abs}}, clbk );
41+
> y
42+
[ [ [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ] ] ]
43+
44+
See Also
45+
--------
46+

0 commit comments

Comments
 (0)