Skip to content

Commit eb1f7f2

Browse files
committed
feat: add array/base/broadcasted-quinary5d
1 parent 8785e54 commit eb1f7f2

File tree

10 files changed

+1529
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)