Skip to content

Commit 34ae5c9

Browse files
feat: add array/base/mskbinary5d
PR-URL: #3198 Closes: #3180 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cb27ec4 commit 34ae5c9

File tree

10 files changed

+1162
-0
lines changed

10 files changed

+1162
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
# mskbinary5d
22+
23+
> Apply a binary callback to elements in two five-dimensional nested input arrays according to elements in a five-dimensional nested mask array and assign results to elements in a 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 mskbinary5d = require( '@stdlib/array/base/mskbinary5d' );
37+
```
38+
39+
#### mskbinary5d( arrays, shape, fcn )
40+
41+
Applies a binary callback to elements in two five-dimensional nested input arrays according to elements in a five-dimensional nested mask array and assigns results to elements in a five-dimensional nested output array.
42+
43+
```javascript
44+
var add = require( '@stdlib/math/base/ops/add' );
45+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
46+
47+
var x = [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ];
48+
var z = zeros5d( [ 1, 1, 1, 2, 2 ] );
49+
50+
var mask = [ [ [ [ [ 0, 1 ], [ 0, 0 ] ] ] ] ];
51+
52+
var shape = [ 1, 1, 1, 2, 2 ];
53+
54+
mskbinary5d( [ x, x, mask, z ], shape, add );
55+
// z => [ [ [ [ [ 2.0, 0.0 ], [ 6.0, 8.0 ] ] ] ] ]
56+
```
57+
58+
The function accepts the following arguments:
59+
60+
- **arrays**: array-like object containing two input nested arrays, an input nested mask array, and one output nested array.
61+
- **shape**: array shape.
62+
- **fcn**: binary function to apply.
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<section class="notes">
69+
70+
## Notes
71+
72+
- The function assumes that the input and output arrays have the same shape.
73+
74+
</section>
75+
76+
<!-- /.notes -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
86+
var bernoulli = require( '@stdlib/random/base/bernoulli' ).factory;
87+
var filled5dBy = require( '@stdlib/array/base/filled5d-by' );
88+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
89+
var add = require( '@stdlib/math/base/ops/add' );
90+
var mskbinary5d = require( '@stdlib/array/base/mskbinary5d' );
91+
92+
var shape = [ 1, 2, 1, 3, 3 ];
93+
94+
var x = filled5dBy( shape, discreteUniform( -100, 100 ) );
95+
console.log( x );
96+
97+
var y = filled5dBy( shape, discreteUniform( -100, 100 ) );
98+
console.log( y );
99+
100+
var mask = filled5dBy( shape, bernoulli( 0.5 ) );
101+
console.log( mask );
102+
103+
var z = zeros5d( shape );
104+
console.log( z );
105+
106+
mskbinary5d( [ x, y, mask, z ], shape, add );
107+
console.log( z );
108+
```
109+
110+
</section>
111+
112+
<!-- /.examples -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
</section>
127+
128+
<!-- /.links -->
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 bernoulli = require( '@stdlib/random/base/bernoulli' ).factory;
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var floor = require( '@stdlib/math/base/special/floor' );
29+
var add = require( '@stdlib/math/base/ops/add' );
30+
var filled5dBy = require( '@stdlib/array/base/filled5d-by' );
31+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
32+
var numel = require( '@stdlib/ndarray/base/numel' );
33+
var pkg = require( './../package.json' ).name;
34+
var mskbinary5d = require( './../lib' );
35+
36+
37+
// FUNCTIONS //
38+
39+
/**
40+
* Creates a benchmark function.
41+
*
42+
* @private
43+
* @param {PositiveIntegerArray} shape - array shape
44+
* @returns {Function} benchmark function
45+
*/
46+
function createBenchmark( shape ) {
47+
var arrays;
48+
var x;
49+
var y;
50+
var z;
51+
var m;
52+
53+
x = filled5dBy( shape, uniform( -100.0, 100.0 ) );
54+
y = filled5dBy( shape, uniform( -100.0, 100.0 ) );
55+
m = filled5dBy( shape, bernoulli( 0.5 ) );
56+
z = zeros5d( shape );
57+
58+
arrays = [ x, y, m, z ];
59+
60+
return benchmark;
61+
62+
/**
63+
* Benchmark function.
64+
*
65+
* @private
66+
* @param {Benchmark} b - benchmark instance
67+
*/
68+
function benchmark( b ) {
69+
var i0;
70+
var i1;
71+
var i2;
72+
var i3;
73+
var i4;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
mskbinary5d( arrays, shape, add );
79+
i4 = i % shape[ 0 ];
80+
i3 = i % shape[ 1 ];
81+
i2 = i % shape[ 2 ];
82+
i1 = i % shape[ 3 ];
83+
i0 = i % shape[ 4 ];
84+
if ( isnan( arrays[ 3 ][ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
85+
b.fail( 'should not return NaN' );
86+
}
87+
}
88+
b.toc();
89+
90+
i4 = i % shape[ 0 ];
91+
i3 = i % shape[ 1 ];
92+
i2 = i % shape[ 2 ];
93+
i1 = i % shape[ 3 ];
94+
i0 = i % shape[ 4 ];
95+
if ( isnan( arrays[ 3 ][ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
96+
b.fail( 'should not return NaN' );
97+
}
98+
b.pass( 'benchmark finished' );
99+
b.end();
100+
}
101+
}
102+
103+
104+
// MAIN //
105+
106+
/**
107+
* Main execution sequence.
108+
*
109+
* @private
110+
*/
111+
function main() {
112+
var min;
113+
var max;
114+
var sh;
115+
var N;
116+
var f;
117+
var i;
118+
119+
min = 1; // 10^min
120+
max = 6; // 10^max
121+
122+
for ( i = min; i <= max; i++ ) {
123+
N = floor( pow( pow( 10, i ), 1.0/5.0 ) );
124+
sh = [ N, N, N, N, N ];
125+
f = createBenchmark( sh );
126+
bench( pkg+'::equidimensional:size='+numel( sh ), f );
127+
}
128+
}
129+
130+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( arrays, shape, fcn )
3+
Applies a binary callback to elements in two five-dimensional nested input
4+
arrays according to elements in a five-dimensional nested mask array and
5+
assigns results to elements in a five-dimensional nested output array.
6+
7+
An element in an input array is "masked" if the corresponding element in the
8+
mask array is non-zero.
9+
10+
Parameters
11+
----------
12+
arrays: ArrayLikeObject
13+
Array-like object containing two input nested arrays, an input nested
14+
mask array, and one output nested array.
15+
16+
shape: Array<integer>
17+
Array shape.
18+
19+
fcn: Function
20+
Binary callback.
21+
22+
Examples
23+
--------
24+
> var x = [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ];
25+
> var y = [ [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ] ];
26+
> var m = [ [ [ [ [ 0, 1 ], [ 0, 0 ] ] ] ] ];
27+
> var z = [ [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ] ];
28+
> var shape = [ 1, 1, 1, 2, 2 ];
29+
> {{alias}}( [ x, y, m, z ], shape, {{alias:@stdlib/math/base/ops/add}} );
30+
> z
31+
[ [ [ [ [ 2.0, 0.0 ], [ 6.0, 8.0 ] ] ] ] ]
32+
33+
See Also
34+
--------
35+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Array5D } from '@stdlib/types/array';
24+
import { Shape5D } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Binary callback.
28+
*
29+
* @param v1 - element from first input array
30+
* @param v2 - element from second input array
31+
* @returns result
32+
*/
33+
type Binary<T, U, V> = ( v1: T, v2: U ) => V;
34+
35+
/**
36+
* Applies a binary callback to elements in two five-dimensional nested input arrays according to elements in a five-dimensional nested mask array and assigns results to elements in a five-dimensional nested output array.
37+
*
38+
* ## Notes
39+
*
40+
* - The function assumes that the input and output arrays have the same shape.
41+
* - An element in an input array is "masked" if the corresponding element in the mask array is non-zero.
42+
*
43+
* @param arrays - array containing two input nested arrays, an input nested mask array, and one output nested array
44+
* @param shape - array shape
45+
* @param fcn - binary callback
46+
*
47+
* @example
48+
* var ones5d = require( '@stdlib/array/base/ones5d' );
49+
* var zeros5d = require( '@stdlib/array/base/zeros5d' );
50+
* var add = require( '@stdlib/math/base/ops/add' );
51+
*
52+
* var shape = [ 1, 1, 1, 2, 2 ];
53+
*
54+
* var x = ones5d( shape );
55+
* var y = ones5d( shape );
56+
* var z = zeros5d( shape );
57+
*
58+
* var mask = [ [ [ [ [ 0, 1 ], [ 0, 0 ] ] ] ] ];
59+
*
60+
* mskbinary5d( [ x, y, mask, z ], shape, add );
61+
*
62+
* console.log( z );
63+
* // => [ [ [ [ [ 2.0, 0.0 ], [ 2.0, 2.0 ] ] ] ] ]
64+
*/
65+
declare function mskbinary5d<T = unknown, U = unknown, V = unknown>( arrays: [ Array5D<T>, Array5D<U>, Array5D<number>, Array5D<V> ], shape: Shape5D, fcn: Binary<T, U, V> ): void;
66+
67+
68+
// EXPORTS //
69+
70+
export = mskbinary5d;

0 commit comments

Comments
 (0)