Skip to content

Commit bf5274a

Browse files
committed
feat(array/base/*): add array/base/broadcasted-quinary5d
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 07f3085 commit bf5274a

File tree

10 files changed

+1548
-0
lines changed

10 files changed

+1548
-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) 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 five [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+
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 = zeros5d( [ 1, 1, 2, 2, 2 ] );
56+
57+
var shapes = [
58+
[ 1, 2 ],
59+
[ 2, 1 ],
60+
[ 1, 1 ],
61+
[ 1, 1 ],
62+
[ 1, 1 ],
63+
[ 1, 1, 2, 2, 2 ]
64+
];
65+
66+
bquinary5d( [ 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 ] ] ] ] ]
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 filled5dBy = require( '@stdlib/array/base/filled5d-by' );
99+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
100+
var bquinary5d = require( '@stdlib/array/base/broadcasted-quinary5d' );
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, 1, 3 ],
108+
[ 1, 1, 3, 1, 1 ],
109+
[ 1, 1, 1, 1, 1 ],
110+
[ 1, 1, 1, 3, 1 ],
111+
[ 1, 1, 1, 1, 3 ],
112+
[ 1, 1, 3, 3, 3 ]
113+
];
114+
115+
var x = filled5dBy( shapes[ 0 ], discreteUniform( -100, 100 ) );
116+
console.log( x );
117+
118+
var y = filled5dBy( shapes[ 1 ], discreteUniform( -100, 100 ) );
119+
console.log( y );
120+
121+
var z = filled5dBy( shapes[ 2 ], discreteUniform( -100, 100 ) );
122+
console.log( z );
123+
124+
var w = filled5dBy( shapes[ 3 ], discreteUniform( -100, 100 ) );
125+
console.log( w );
126+
127+
var v = filled5dBy( shapes[ 4 ], discreteUniform( -100, 100 ) );
128+
console.log( v );
129+
130+
var out = zeros5d( shapes[ 5 ] );
131+
console.log( out );
132+
133+
bquinary5d( [ 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: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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 filled5dBy = require( '@stdlib/array/base/filled5d-by' );
29+
var zeros5d = require( '@stdlib/array/base/zeros5d' );
30+
var numel = require( '@stdlib/ndarray/base/numel' );
31+
var pkg = require( './../package.json' ).name;
32+
var bquinary5d = 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, 1, shape[ 4 ] ],
71+
[ shape[ 0 ], 1, 1, 1, 1 ],
72+
[ 1, 1, 1, 1, 1 ],
73+
[ 1, shape[ 1 ], 1, 1, 1 ],
74+
[ 1, 1, shape[ 2 ], 1, 1 ],
75+
shape
76+
];
77+
x = filled5dBy( shapes[ 0 ], uniform( -100.0, 100.0 ) );
78+
y = filled5dBy( shapes[ 1 ], uniform( -100.0, 100.0 ) );
79+
z = filled5dBy( shapes[ 2 ], uniform( -100.0, 100.0 ) );
80+
w = filled5dBy( shapes[ 3 ], uniform( -100.0, 100.0 ) );
81+
v = filled5dBy( shapes[ 4 ], uniform( -100.0, 100.0 ) );
82+
out = zeros5d( 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 i4;
100+
var i;
101+
102+
b.tic();
103+
for ( i = 0; i < b.iterations; i++ ) {
104+
bquinary5d( arrays, shapes, add );
105+
i4 = i % shapes[ 1 ][ 0 ];
106+
i3 = i % shapes[ 3 ][ 1 ];
107+
i2 = i % shapes[ 4 ][ 2 ];
108+
i1 = i % shapes[ 0 ][ 3 ];
109+
i0 = i % shapes[ 0 ][ 4 ];
110+
if ( isnan( arrays[ 5 ][ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
111+
b.fail( 'should not return NaN' );
112+
}
113+
}
114+
b.toc();
115+
116+
i4 = i % shapes[ 1 ][ 0 ];
117+
i3 = i % shapes[ 3 ][ 1 ];
118+
i2 = i % shapes[ 4 ][ 2 ];
119+
i1 = i % shapes[ 0 ][ 3 ];
120+
i0 = i % shapes[ 0 ][ 4 ];
121+
if ( isnan( arrays[ 5 ][ i4 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
122+
b.fail( 'should not return NaN' );
123+
}
124+
b.pass( 'benchmark finished' );
125+
b.end();
126+
}
127+
}
128+
129+
130+
// MAIN //
131+
132+
/**
133+
* Main execution sequence.
134+
*
135+
* @private
136+
*/
137+
function main() {
138+
var min;
139+
var max;
140+
var sh;
141+
var N;
142+
var f;
143+
var i;
144+
145+
min = 1; // 10^min
146+
max = 5; // 10^max
147+
148+
for ( i = min; i <= max; i++ ) {
149+
N = floor( pow( pow( 10, i ), 1.0/5.0 ) );
150+
sh = [ N, N, N, N, N ];
151+
f = createBenchmark( sh );
152+
bench( pkg+'::equidimensional:size='+numel( sh ), f );
153+
}
154+
}
155+
156+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 = [
27+
... [ 2 ], [ 2, 1 ], [ 1, 1 ], [ 1 ], [ 1 ], [ 1, 1, 1, 2, 2 ]
28+
... ];
29+
> {{alias}}( [ x, y, z, w, v, out ], shapes, fcn );
30+
> out
31+
[ [ [ [ [ 8.0, 9.0 ], [ 9.0, 10.0 ] ] ] ] ]
32+
33+
See Also
34+
--------

0 commit comments

Comments
 (0)