Skip to content

Commit 2b7cf06

Browse files
committed
feat: add array/base/ternary2d
1 parent 38333d8 commit 2b7cf06

File tree

10 files changed

+876
-0
lines changed

10 files changed

+876
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
# ternary2d
22+
23+
> Apply a ternary callback to elements in three two-dimensional 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 ternary2d = require( '@stdlib/array/base/ternary2d' );
37+
```
38+
39+
#### ternary2d( arrays, shape, fcn )
40+
41+
Applies a ternary callback to elements in three two-dimensional 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 ], [ 3.0, 4.0 ] ];
51+
var out = zeros2d( [ 2, 2 ] );
52+
53+
var shape = [ 2, 2 ];
54+
55+
ternary2d( [ x, x, x, out ], shape, add );
56+
// out => [ [ 3.0, 6.0 ], [ 9.0, 12.0 ] ]
57+
```
58+
59+
The function accepts the following arguments:
60+
61+
- **arrays**: array-like object containing three input nested arrays and one output nested array.
62+
- **shape**: array shape.
63+
- **fcn**: ternary function to apply.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<section class="notes">
70+
71+
## Notes
72+
73+
- The function assumes that the input and output arrays have the same shape.
74+
75+
</section>
76+
77+
<!-- /.notes -->
78+
79+
<section class="examples">
80+
81+
## Examples
82+
83+
<!-- eslint no-undef: "error" -->
84+
85+
```javascript
86+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
87+
var filled2dBy = require( '@stdlib/array/base/filled2d-by' );
88+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
89+
var ternary2d = require( '@stdlib/array/base/ternary2d' );
90+
91+
function add( x, y, z ) {
92+
return x + y + z;
93+
}
94+
95+
var shape = [ 3, 3 ];
96+
97+
var x = filled2dBy( shape, discreteUniform( -100, 100 ) );
98+
console.log( x );
99+
100+
var y = filled2dBy( shape, discreteUniform( -100, 100 ) );
101+
console.log( y );
102+
103+
var z = filled2dBy( shape, discreteUniform( -100, 100 ) );
104+
console.log( z );
105+
106+
var out = zeros2d( shape );
107+
console.log( out );
108+
109+
ternary2d( [ x, y, z, out ], shape, add );
110+
console.log( out );
111+
```
112+
113+
</section>
114+
115+
<!-- /.examples -->
116+
117+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
118+
119+
<section class="related">
120+
121+
</section>
122+
123+
<!-- /.related -->
124+
125+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="links">
128+
129+
</section>
130+
131+
<!-- /.links -->
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 ternary2d = 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 - array shape
55+
* @returns {Function} benchmark function
56+
*/
57+
function createBenchmark( shape ) {
58+
var arrays;
59+
var out;
60+
var x;
61+
var y;
62+
var z;
63+
64+
x = filled2dBy( shape, uniform( -100.0, 100.0 ) );
65+
y = filled2dBy( shape, uniform( -100.0, 100.0 ) );
66+
z = filled2dBy( shape, uniform( -100.0, 100.0 ) );
67+
out = zeros2d( shape );
68+
69+
arrays = [ x, y, z, out ];
70+
71+
return benchmark;
72+
73+
/**
74+
* Benchmark function.
75+
*
76+
* @private
77+
* @param {Benchmark} b - benchmark instance
78+
*/
79+
function benchmark( b ) {
80+
var i0;
81+
var i1;
82+
var i;
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
ternary2d( arrays, shape, add );
87+
i1 = i % shape[ 0 ];
88+
i0 = i % shape[ 1 ];
89+
if ( isnan( arrays[ 3 ][ i1 ][ i0 ] ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
}
93+
b.toc();
94+
95+
i1 = i % shape[ 0 ];
96+
i0 = i % shape[ 1 ];
97+
if ( isnan( arrays[ 3 ][ i1 ][ i0 ] ) ) {
98+
b.fail( 'should not return NaN' );
99+
}
100+
b.pass( 'benchmark finished' );
101+
b.end();
102+
}
103+
}
104+
105+
106+
// MAIN //
107+
108+
/**
109+
* Main execution sequence.
110+
*
111+
* @private
112+
*/
113+
function main() {
114+
var min;
115+
var max;
116+
var sh;
117+
var N;
118+
var f;
119+
var i;
120+
121+
min = 1; // 10^min
122+
max = 6; // 10^max
123+
124+
for ( i = min; i <= max; i++ ) {
125+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
126+
sh = [ N, N ];
127+
f = createBenchmark( sh );
128+
bench( pkg+'::square_matrix:size='+numel( sh ), f );
129+
}
130+
}
131+
132+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( arrays, shape, fcn )
3+
Applies a ternary callback to elements in three two-dimensional nested input
4+
arrays and assigns results to elements in a two-dimensional nested output
5+
array.
6+
7+
Parameters
8+
----------
9+
arrays: ArrayLikeObject
10+
Array-like object containing three input nested arrays and one output
11+
nested array.
12+
13+
shape: Array<integer>
14+
Array shape.
15+
16+
fcn: Function
17+
Ternary callback.
18+
19+
Examples
20+
--------
21+
> function fcn( x, y, z ) { return x + y + z };
22+
> var x = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
23+
> var y = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
24+
> var z = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
25+
> var out = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ];
26+
> var shape = [ 2, 2 ];
27+
> {{alias}}( [ x, y, z, out ], shape, fcn );
28+
> out
29+
[ [ 3.0, 6.0 ], [ 9.0, 12.0 ] ]
30+
31+
See Also
32+
--------
33+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 { Array2D } from '@stdlib/types/array';
24+
import { 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 two-dimensional nested input arrays and assigns results to elements in a two-dimensional nested output array.
36+
*
37+
* ## Notes
38+
*
39+
* - The function assumes that the input and output arrays have the same shape.
40+
*
41+
* @param arrays - array containing three input nested arrays and one output nested array
42+
* @param shape - array shape
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 shape = [ 2, 2 ];
54+
*
55+
* var x = ones2d( shape );
56+
* var y = ones2d( shape );
57+
* var z = ones2d( shape );
58+
* var out = zeros2d( shape );
59+
*
60+
* ternary2d( [ x, y, z, out ], shape, add );
61+
*
62+
* console.log( out );
63+
* // => [ [ 3.0, 3.0 ], [ 3.0, 3.0 ] ]
64+
*/
65+
declare function ternary2d<T = unknown, U = unknown, V = unknown, W = unknown>( arrays: [ Array2D<T>, Array2D<U>, Array2D<V>, Array2D<W> ], shape: Shape2D, fcn: Ternary<T, U, V, W> ): void;
66+
67+
68+
// EXPORTS //
69+
70+
export = ternary2d;

0 commit comments

Comments
 (0)