Skip to content

Commit 820598d

Browse files
committed
feat: add array/base/broadcasted-ternary2d
1 parent 7214b1b commit 820598d

File tree

10 files changed

+1018
-0
lines changed

10 files changed

+1018
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
# bternary2d
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 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 bternary2d = require( '@stdlib/array/base/broadcasted-ternary2d' );
37+
```
38+
39+
#### bternary2d( 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 two-dimensional nested output array.
42+
43+
```javascript
44+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
45+
46+
function add( x, y, z ) {
47+
return x + y + z;
48+
}
49+
50+
var x = [ [ 1.0, 2.0 ] ];
51+
var y = [ [ 3.0 ], [ 4.0 ] ];
52+
var z = [ [ 5.0 ] ];
53+
var out = zeros2d( [ 2, 2 ] );
54+
55+
var shapes = [
56+
[ 1, 2 ],
57+
[ 2, 1 ],
58+
[ 1, 1 ],
59+
[ 2, 2 ]
60+
];
61+
62+
bternary2d( [ x, y, z, out ], shapes, add );
63+
// out => [ [ 9.0, 10.0 ], [ 10.0, 11.0 ] ]
64+
```
65+
66+
The function accepts the following arguments:
67+
68+
- **arrays**: array-like object containing three input nested arrays and one output nested array.
69+
- **shapes**: array shapes.
70+
- **fcn**: ternary function to apply.
71+
72+
</section>
73+
74+
<!-- /.usage -->
75+
76+
<section class="notes">
77+
78+
## Notes
79+
80+
- The input and output array shapes must be broadcast [compatible][@stdlib/ndarray/base/broadcast-shapes].
81+
82+
</section>
83+
84+
<!-- /.notes -->
85+
86+
<section class="examples">
87+
88+
## Examples
89+
90+
<!-- eslint no-undef: "error" -->
91+
92+
```javascript
93+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
94+
var filled2dBy = require( '@stdlib/array/base/filled2d-by' );
95+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
96+
var bternary2d = require( '@stdlib/array/base/broadcasted-ternary2d' );
97+
98+
function add( x, y, z ) {
99+
return x + y + z;
100+
}
101+
102+
var shapes = [
103+
[ 1, 3 ],
104+
[ 3, 1 ],
105+
[ 1, 1 ],
106+
[ 3, 3 ]
107+
];
108+
109+
var x = filled2dBy( shapes[ 0 ], discreteUniform( -100, 100 ) );
110+
console.log( x );
111+
112+
var y = filled2dBy( shapes[ 1 ], discreteUniform( -100, 100 ) );
113+
console.log( y );
114+
115+
var z = filled2dBy( shapes[ 2 ], discreteUniform( -100, 100 ) );
116+
console.log( z );
117+
118+
var out = zeros2d( shapes[ 3 ] );
119+
console.log( out );
120+
121+
bternary2d( [ x, y, z, out ], shapes, add );
122+
console.log( out );
123+
```
124+
125+
</section>
126+
127+
<!-- /.examples -->
128+
129+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
130+
131+
<section class="related">
132+
133+
</section>
134+
135+
<!-- /.related -->
136+
137+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="links">
140+
141+
[@stdlib/array/base/broadcast-array]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/broadcast-array
142+
143+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
144+
145+
</section>
146+
147+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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 bternary2d = 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+
* @returns {number} sum
45+
*/
46+
function add( x, y, z ) {
47+
return x + y + z;
48+
}
49+
50+
/**
51+
* Creates a benchmark function.
52+
*
53+
* @private
54+
* @param {PositiveIntegerArray} shape - output array shape
55+
* @returns {Function} benchmark function
56+
*/
57+
function createBenchmark( shape ) {
58+
var arrays;
59+
var shapes;
60+
var out;
61+
var x;
62+
var y;
63+
var z;
64+
65+
shapes = [
66+
[ 1, shape[ 1 ] ],
67+
[ shape[ 0 ], 1 ],
68+
[ 1, 1 ],
69+
shape
70+
];
71+
x = filled2dBy( shapes[ 0 ], uniform( -100.0, 100.0 ) );
72+
y = filled2dBy( shapes[ 1 ], uniform( -100.0, 100.0 ) );
73+
z = filled2dBy( shapes[ 2 ], uniform( -100.0, 100.0 ) );
74+
out = zeros2d( shapes[ 3 ] );
75+
76+
arrays = [ x, y, z, out ];
77+
78+
return benchmark;
79+
80+
/**
81+
* Benchmark function.
82+
*
83+
* @private
84+
* @param {Benchmark} b - benchmark instance
85+
*/
86+
function benchmark( b ) {
87+
var i0;
88+
var i1;
89+
var i;
90+
91+
b.tic();
92+
for ( i = 0; i < b.iterations; i++ ) {
93+
bternary2d( arrays, shapes, add );
94+
i1 = i % shapes[ 1 ][ 0 ];
95+
i0 = i % shapes[ 1 ][ 1 ];
96+
if ( isnan( arrays[ 3 ][ i1 ][ i0 ] ) ) {
97+
b.fail( 'should not return NaN' );
98+
}
99+
}
100+
b.toc();
101+
102+
i1 = i % shapes[ 1 ][ 0 ];
103+
i0 = i % shapes[ 1 ][ 1 ];
104+
if ( isnan( arrays[ 3 ][ i1 ][ i0 ] ) ) {
105+
b.fail( 'should not return NaN' );
106+
}
107+
b.pass( 'benchmark finished' );
108+
b.end();
109+
}
110+
}
111+
112+
113+
// MAIN //
114+
115+
/**
116+
* Main execution sequence.
117+
*
118+
* @private
119+
*/
120+
function main() {
121+
var min;
122+
var max;
123+
var sh;
124+
var N;
125+
var f;
126+
var i;
127+
128+
min = 1; // 10^min
129+
max = 6; // 10^max
130+
131+
for ( i = min; i <= max; i++ ) {
132+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
133+
sh = [ N, N ];
134+
f = createBenchmark( sh );
135+
bench( pkg+'::square_matrix:size='+numel( sh ), f );
136+
}
137+
}
138+
139+
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 two-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, 2.0 ];
22+
> var y = [ [ 3.0 ], [ 4.0 ] ];
23+
> var z = [ [ 1.0 ] ];
24+
> var out = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ];
25+
> var shapes = [ [ 2 ], [ 2, 1 ], [ 1, 1 ], [ 2, 2 ] ];
26+
> {{alias}}( [ x, y, z, out ], shapes, fcn );
27+
> out
28+
[ [ 5.0, 6.0 ], [ 6.0, 7.0 ] ]
29+
30+
See Also
31+
--------
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Array1D, Array2D } from '@stdlib/types/array';
24+
import { Shape1D, Shape2D } from '@stdlib/types/ndarray';
25+
26+
/**
27+
* Ternary callback.
28+
*
29+
* @param value - input value
30+
* @returns result
31+
*/
32+
type Ternary<T, U, V, W> = ( v1: T, v2: U, v3: V ) => W;
33+
34+
/**
35+
* Applies a ternary callback to elements in three broadcasted input arrays and assigns results to elements in a two-dimensional nested output array.
36+
*
37+
* ## Notes
38+
*
39+
* - The input array shapes must be broadcast compatible with the output array shape.
40+
*
41+
* @param arrays - array containing three input nested arrays and one output nested array
42+
* @param shapes - array shapes
43+
* @param fcn - ternary callback
44+
*
45+
* @example
46+
* var ones2d = require( `@stdlib/array/base/ones2d` );
47+
* var zeros2d = require( `@stdlib/array/base/zeros2d` );
48+
*
49+
* function add( x, y, z ) {
50+
* return x + y + z;
51+
* }
52+
*
53+
* var shapes = [
54+
* [ 1, 2 ],
55+
* [ 2, 1 ],
56+
* [ 1, 1 ],
57+
* [ 2, 2 ]
58+
* ];
59+
*
60+
* var x = ones2d( shapes[ 0 ] );
61+
* var y = ones2d( shapes[ 1 ] );
62+
* var z = ones2d( shapes[ 2 ] );
63+
* var out = zeros2d( shapes[ 3 ] );
64+
*
65+
* bternary2d( [ x, y, z, out ], shapes, add );
66+
*
67+
* console.log( out );
68+
* // => [ [ 3.0, 6.0 ], [ 9.0, 12.0 ] ]
69+
*/
70+
declare function bternary2d<T = unknown, U = unknown, V = unknown, W = unknown>( arrays: [ Array1D<T> | Array2D<T>, Array1D<U> | Array2D<U>, Array1D<V> | Array2D<V>, Array2D<W> ], shapes: [ Shape1D | Shape2D, Shape1D | Shape2D, Shape1D | Shape2D, Shape2D ], fcn: Ternary<T, U, V, W> ): void;
71+
72+
73+
// EXPORTS //
74+
75+
export = bternary2d;

0 commit comments

Comments
 (0)