Skip to content

Commit 982980c

Browse files
Vinit-Panditkgryte
andauthored
feat: add array/base/broadcasted-ternary5d
PR-URL: #3251 Closes: #3166 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Vinit Pandit <[email protected]>
1 parent e7e0f46 commit 982980c

File tree

10 files changed

+1257
-0
lines changed

10 files changed

+1257
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
# bternary5d
22+
23+
> Apply a ternary callback to elements in three [broadcasted][@stdlib/array/base/broadcast-array] nested input arrays 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 bternary5d = require( '@stdlib/array/base/broadcasted-ternary5d' );
37+
```
38+
39+
#### bternary5d( arrays, shapes, fcn )
40+
41+
Applies a ternary callback to elements in three [broadcasted][@stdlib/array/base/broadcast-array] nested input arrays and assigns results to elements in a five-dimensional nested output array.
42+
43+
```javascript
44+
var add = require( '@stdlib/math/base/ops/add3' );
45+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
46+
47+
var x = [ [ [ 1.0, 2.0 ] ] ];
48+
var y = [ [ [ 3.0 ], [ 4.0 ] ] ];
49+
var z = [ [ [ [ 4.0, 5.0 ] ], [ [ 4.0, 5.0 ] ] ] ];
50+
var out = zeros5d( [ 1, 1, 2, 2 ] );
51+
52+
var shapes = [
53+
[ 1, 1, 2 ],
54+
[ 1, 2, 1 ],
55+
[ 1, 2, 1, 2 ],
56+
[ 1, 1, 2, 2, 2 ]
57+
];
58+
59+
bternary5d( [ x, y, z, out ], shapes, add );
60+
// out => [ [ [ [ [ 8.0, 10.0 ], [ 9.0, 11.0 ] ], [ [ 8.0, 10.0 ], [ 9.0, 11.0 ] ] ] ] ]
61+
```
62+
63+
The function accepts the following arguments:
64+
65+
- **arrays**: array-like object containing three input nested arrays and one output nested array.
66+
- **shapes**: array shapes.
67+
- **fcn**: ternary function to apply.
68+
69+
</section>
70+
71+
<!-- /.usage -->
72+
73+
<section class="notes">
74+
75+
## Notes
76+
77+
- The input and output array shapes must be broadcast [compatible][@stdlib/ndarray/base/broadcast-shapes].
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
91+
var filled5dBy = require( '@stdlib/array/base/filled5d-by' );
92+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
93+
var add = require( '@stdlib/math/base/ops/add3' );
94+
var bternary5d = require( '@stdlib/array/base/broadcasted-ternary5d' );
95+
96+
var shapes = [
97+
[ 1, 1, 1, 1, 3 ],
98+
[ 1, 1, 3, 1, 1 ],
99+
[ 1, 1, 1, 3, 1 ],
100+
[ 1, 1, 3, 3, 3 ]
101+
];
102+
103+
var x = filled5dBy( shapes[ 0 ], discreteUniform( -100, 100 ) );
104+
console.log( x );
105+
106+
var y = filled5dBy( shapes[ 1 ], discreteUniform( -100, 100 ) );
107+
console.log( y );
108+
109+
var z = filled5dBy( shapes[ 2 ], discreteUniform( -100, 100 ) );
110+
console.log( z );
111+
112+
var out = zeros5d( shapes[ 3 ] );
113+
console.log( out );
114+
115+
bternary5d( [ x, y, z, out ], shapes, add );
116+
console.log( out );
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
124+
125+
<section class="related">
126+
127+
</section>
128+
129+
<!-- /.related -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
[@stdlib/array/base/broadcast-array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/broadcast-array
136+
137+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
138+
139+
</section>
140+
141+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 add = require( '@stdlib/math/base/ops/add3' );
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 bternary5d = require( './../lib' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveIntegerArray} shape - output array shape
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( shape ) {
46+
var arrays;
47+
var shapes;
48+
var x;
49+
var y;
50+
var z;
51+
var w;
52+
53+
shapes = [
54+
[ shape[ 0 ], 1, 1, 1, 1 ],
55+
[ 1, 1, 1, 1, shape[ 1 ] ],
56+
[ 1, 1, shape[ 2 ], 1, 1 ],
57+
shape
58+
];
59+
x = filled5dBy( shapes[ 0 ], uniform( -100.0, 100.0 ) );
60+
y = filled5dBy( shapes[ 1 ], uniform( -100.0, 100.0 ) );
61+
z = filled5dBy( shapes[ 2 ], uniform( -100.0, 100.0 ) );
62+
w = zeros5d( shapes[ 3 ] );
63+
64+
arrays = [ x, y, z, w ];
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 i4;
80+
var i;
81+
82+
b.tic();
83+
for ( i = 0; i < b.iterations; i++ ) {
84+
bternary5d( arrays, shapes, add );
85+
i4 = i % shapes[ 1 ][ 0 ];
86+
i3 = i % shapes[ 1 ][ 1 ];
87+
i2 = i % shapes[ 1 ][ 2 ];
88+
i1 = i % shapes[ 1 ][ 3 ];
89+
i0 = i % shapes[ 1 ][ 4 ];
90+
if ( isnan( arrays[ 3 ][ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
91+
b.fail( 'should not return NaN' );
92+
}
93+
}
94+
b.toc();
95+
96+
i4 = i % shapes[ 1 ][ 0 ];
97+
i3 = i % shapes[ 1 ][ 1 ];
98+
i2 = i % shapes[ 1 ][ 2 ];
99+
i1 = i % shapes[ 1 ][ 3 ];
100+
i0 = i % shapes[ 1 ][ 4 ];
101+
if ( isnan(arrays[ 3 ][ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
102+
b.fail( 'should not return NaN' );
103+
}
104+
b.pass( 'benchmark finished' );
105+
b.end();
106+
}
107+
}
108+
109+
110+
// MAIN //
111+
112+
/**
113+
* Main execution sequence.
114+
*
115+
* @private
116+
*/
117+
function main() {
118+
var min;
119+
var max;
120+
var sh;
121+
var N;
122+
var f;
123+
var i;
124+
125+
min = 1; // 10^min
126+
max = 6; // 10^max
127+
128+
for ( i = min; i <= max; i++ ) {
129+
N = floor( pow( pow( 10, i ), 1.0 / 5.0 ) );
130+
sh = [ N, N, N, N, N ];
131+
f = createBenchmark( sh );
132+
bench( pkg + '::equidimensional:size=' + numel( sh ), f );
133+
}
134+
}
135+
136+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( arrays, shapes, fcn )
3+
Applies a ternary callback to elements in three broadcasted input arrays and
4+
assigns results to elements in a five-dimensional nested output array.
5+
6+
Parameters
7+
----------
8+
arrays: ArrayLikeObject
9+
Array-like object containing three input nested arrays and one output
10+
nested array.
11+
12+
shapes: Array<Array<integer>>
13+
Array shapes.
14+
15+
fcn: Function
16+
Ternary callback.
17+
18+
Examples
19+
--------
20+
> function fcn( x, y, z ) { return x + y + z };
21+
> var x = [ 1.0 ];
22+
> var y = [ [ 2.0, 2.0 ], [ 3.0, 3.0 ] ];
23+
> var z = [ [ [ [ [ 2.0 ], [ 3.0 ] ] ] ] ];
24+
> var out = [ [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ] ];
25+
> var shapes = [ [ 1 ], [ 2, 2 ], [ 1, 1, 1, 2, 1 ], [ 1, 1, 1, 2, 2 ] ];
26+
> {{alias}}( [ x, y, z, out ], shapes, fcn );
27+
> out
28+
[ [ [ [ [ 5.0, 5.0 ], [ 7.0, 7.0 ] ] ] ] ]
29+
30+
See Also
31+
--------
32+

0 commit comments

Comments
 (0)