Skip to content

Commit e74cfa7

Browse files
Neerajpathak07kgrytestdlib-bot
authored
feat: add array/base/broadcasted-quinary4d
PR-URL: #3267 Closes: #3171 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <82920195+stdlib-bot@users.noreply.github.com>
1 parent 552c38e commit e74cfa7

File tree

10 files changed

+1576
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)