Skip to content

Commit ca1d25d

Browse files
committed
feat: add array/base/broadcasted-quinary2d
1 parent 4340e7c commit ca1d25d

File tree

10 files changed

+1173
-0
lines changed

10 files changed

+1173
-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+
# bquinary2d
22+
23+
> Apply a quinary callback to elements in four [broadcasted][@stdlib/array/base/broadcast-array] nested input arrays and assign results to elements in a two-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 bquinary2d = require( '@stdlib/array/base/broadcasted-quinary2d' );
37+
```
38+
39+
#### bquinary2d( arrays, shapes, fcn )
40+
41+
Applies a quinary callback to elements in four [broadcasted][@stdlib/array/base/broadcast-array] nested input arrays and assigns results to elements in a two-dimensional nested output array.
42+
43+
```javascript
44+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
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 = zeros2d( [ 2, 2 ] );
56+
57+
var shapes = [
58+
[ 1, 2 ],
59+
[ 2, 1 ],
60+
[ 1, 1 ],
61+
[ 1, 1 ],
62+
[ 1, 1 ],
63+
[ 2, 2 ]
64+
];
65+
66+
bquinary2d( [ x, y, z, w, v, out ], shapes, add );
67+
// out => [ [ 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 filled2dBy = require( '@stdlib/array/base/filled2d-by' );
99+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
100+
var bquinary2d = require( '@stdlib/array/base/broadcasted-quinary2d' );
101+
102+
function add( x, y, z, w, v ) {
103+
return x + y + z + w + v;
104+
}
105+
106+
var shapes = [
107+
[ 1, 3 ],
108+
[ 3, 1 ],
109+
[ 1, 1 ],
110+
[ 3, 3 ],
111+
[ 1, 3 ],
112+
[ 3, 3 ]
113+
];
114+
115+
var x = filled2dBy( shapes[ 0 ], discreteUniform( -100, 100 ) );
116+
console.log( x );
117+
118+
var y = filled2dBy( shapes[ 1 ], discreteUniform( -100, 100 ) );
119+
console.log( y );
120+
121+
var z = filled2dBy( shapes[ 2 ], discreteUniform( -100, 100 ) );
122+
console.log( z );
123+
124+
var w = filled2dBy( shapes[ 3 ], discreteUniform( -100, 100 ) );
125+
console.log( w );
126+
127+
var v = filled2dBy( shapes[ 4 ], discreteUniform( -100, 100 ) );
128+
console.log( v );
129+
130+
var out = zeros2d( shapes[ 5 ] );
131+
console.log( out );
132+
133+
bquinary2d( [ 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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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 filled2dBy = require( '@stdlib/array/base/filled2d-by' );
29+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
30+
var numel = require( '@stdlib/ndarray/base/numel' );
31+
var pkg = require( './../package.json' ).name;
32+
var bquinary2d = 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, shape[ 1 ] ],
71+
[ shape[ 0 ], 1 ],
72+
[ 1, 1 ],
73+
[ shape[ 0 ], shape[ 1 ] ],
74+
[ 1, 1 ],
75+
shape
76+
];
77+
x = filled2dBy( shapes[ 0 ], uniform( -100.0, 100.0 ) );
78+
y = filled2dBy( shapes[ 1 ], uniform( -100.0, 100.0 ) );
79+
z = filled2dBy( shapes[ 2 ], uniform( -100.0, 100.0 ) );
80+
w = filled2dBy( shapes[ 3 ], uniform( -100.0, 100.0 ) );
81+
v = filled2dBy( shapes[ 4 ], uniform( -100.0, 100.0 ) );
82+
out = zeros2d( 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 i;
98+
99+
b.tic();
100+
for ( i = 0; i < b.iterations; i++ ) {
101+
bquinary2d( arrays, shapes, add );
102+
i1 = i % shapes[ 1 ][ 0 ];
103+
i0 = i % shapes[ 1 ][ 1 ];
104+
if ( isnan( arrays[ 5 ][ i1 ][ i0 ] ) ) {
105+
b.fail( 'should not return NaN' );
106+
}
107+
}
108+
b.toc();
109+
110+
i1 = i % shapes[ 1 ][ 0 ];
111+
i0 = i % shapes[ 1 ][ 1 ];
112+
if ( isnan( arrays[ 5 ][ i1 ][ i0 ] ) ) {
113+
b.fail( 'should not return NaN' );
114+
}
115+
b.pass( 'benchmark finished' );
116+
b.end();
117+
}
118+
}
119+
120+
121+
// MAIN //
122+
123+
/**
124+
* Main execution sequence.
125+
*
126+
* @private
127+
*/
128+
function main() {
129+
var min;
130+
var max;
131+
var sh;
132+
var N;
133+
var f;
134+
var i;
135+
136+
min = 1; // 10^min
137+
max = 6; // 10^max
138+
139+
for ( i = min; i <= max; i++ ) {
140+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
141+
sh = [ N, N ];
142+
f = createBenchmark( sh );
143+
bench( pkg+'::square_matrix:size='+numel( sh ), f );
144+
}
145+
}
146+
147+
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
4+
and assigns results to elements in a two-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 ], [ 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)