Skip to content

Commit e32a80c

Browse files
committed
Auto-generated commit
1 parent 964e458 commit e32a80c

File tree

13 files changed

+807
-5
lines changed

13 files changed

+807
-5
lines changed

base/binary2d/README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
# binary2d
22+
23+
> Apply a binary callback to elements in 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 binary2d = require( '@stdlib/array/base/binary2d' );
37+
```
38+
39+
#### binary2d( arrays, shape, fcn )
40+
41+
Applies a binary callback to elements in two-dimensional nested input arrays and assigns results to elements in a two-dimensional nested output array.
42+
43+
```javascript
44+
var add = require( '@stdlib/math/base/ops/add' );
45+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
46+
47+
var x = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
48+
var z = zeros2d( [ 2, 2 ] );
49+
50+
var shape = [ 2, 2 ];
51+
52+
binary2d( [ x, x, z ], shape, add );
53+
// z => [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ]
54+
```
55+
56+
The function accepts the following arguments:
57+
58+
- **arrays**: array-like object containing two input nested arrays and one output nested array.
59+
- **shape**: array shape.
60+
- **fcn**: binary function to apply.
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<section class="notes">
67+
68+
## Notes
69+
70+
- The function assumes that the input and output arrays have the same shape.
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
84+
var filled2dBy = require( '@stdlib/array/base/filled2d-by' );
85+
var zeros2d = require( '@stdlib/array/base/zeros2d' );
86+
var add = require( '@stdlib/math/base/ops/add' );
87+
var binary2d = require( '@stdlib/array/base/binary2d' );
88+
89+
var shape = [ 3, 3 ];
90+
91+
var x = filled2dBy( shape, discreteUniform( -100, 100 ) );
92+
console.log( x );
93+
94+
var y = filled2dBy( shape, discreteUniform( -100, 100 ) );
95+
console.log( y );
96+
97+
var z = zeros2d( shape );
98+
console.log( z );
99+
100+
binary2d( [ x, y, z ], shape, add );
101+
console.log( z );
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
109+
110+
<section class="related">
111+
112+
</section>
113+
114+
<!-- /.related -->
115+
116+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
117+
118+
<section class="links">
119+
120+
</section>
121+
122+
<!-- /.links -->
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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 add = require( '@stdlib/math/base/ops/add' );
29+
var filled2dBy = require( './../../../base/filled2d-by' );
30+
var zeros2d = require( './../../../base/zeros2d' );
31+
var numel = require( '@stdlib/ndarray/base/numel' );
32+
var pkg = require( './../package.json' ).name;
33+
var binary2d = require( './../lib' );
34+
35+
36+
// FUNCTIONS //
37+
38+
/**
39+
* Creates a benchmark function.
40+
*
41+
* @private
42+
* @param {PositiveIntegerArray} shape - array shape
43+
* @returns {Function} benchmark function
44+
*/
45+
function createBenchmark( shape ) {
46+
var arrays;
47+
var x;
48+
var y;
49+
var z;
50+
51+
x = filled2dBy( shape, uniform( -100.0, 100.0 ) );
52+
y = filled2dBy( shape, uniform( -100.0, 100.0 ) );
53+
z = zeros2d( shape );
54+
55+
arrays = [ x, y, z ];
56+
57+
return benchmark;
58+
59+
/**
60+
* Benchmark function.
61+
*
62+
* @private
63+
* @param {Benchmark} b - benchmark instance
64+
*/
65+
function benchmark( b ) {
66+
var i0;
67+
var i1;
68+
var i;
69+
70+
b.tic();
71+
for ( i = 0; i < b.iterations; i++ ) {
72+
binary2d( arrays, shape, add );
73+
i1 = i % shape[ 0 ];
74+
i0 = i % shape[ 1 ];
75+
if ( isnan( arrays[ 2 ][ i1 ][ i0 ] ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
}
79+
b.toc();
80+
81+
i1 = i % shape[ 0 ];
82+
i0 = i % shape[ 1 ];
83+
if ( isnan( arrays[ 2 ][ i1 ][ i0 ] ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
b.pass( 'benchmark finished' );
87+
b.end();
88+
}
89+
}
90+
91+
92+
// MAIN //
93+
94+
/**
95+
* Main execution sequence.
96+
*
97+
* @private
98+
*/
99+
function main() {
100+
var min;
101+
var max;
102+
var sh;
103+
var N;
104+
var f;
105+
var i;
106+
107+
min = 1; // 10^min
108+
max = 6; // 10^max
109+
110+
for ( i = min; i <= max; i++ ) {
111+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
112+
sh = [ N, N ];
113+
f = createBenchmark( sh );
114+
bench( pkg+'::square_matrix:size='+numel( sh ), f );
115+
}
116+
}
117+
118+
main();

base/binary2d/docs/repl.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( arrays, shape, fcn )
3+
Applies a binary callback to elements in two-dimensional nested 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 two input nested arrays and one output
10+
nested array.
11+
12+
shape: Array<integer>
13+
Array shape.
14+
15+
fcn: Function
16+
Binary callback.
17+
18+
Examples
19+
--------
20+
> var x = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
21+
> var y = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
22+
> var z = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ];
23+
> var shape = [ 2, 2 ];
24+
> {{alias}}( [ x, y, z ], shape, {{alias:@stdlib/math/base/ops/add}} );
25+
> z
26+
[ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ]
27+
28+
See Also
29+
--------
30+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
* Binary callback.
28+
*
29+
* @param value - input value
30+
* @returns result
31+
*/
32+
type Binary<T, U, V> = ( v1: T, v2: U ) => V;
33+
34+
/**
35+
* Applies a binary callback to elements in 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 two input nested arrays and one output nested array
42+
* @param shape - array shape
43+
* @param fcn - binary callback
44+
*
45+
* @example
46+
* var ones2d = require( `@stdlib/array/base/ones2d` );
47+
* var zeros2d = require( `@stdlib/array/base/zeros2d` );
48+
* var add = require( `@stdlib/math/base/ops/add` );
49+
*
50+
* var shape = [ 2, 2 ];
51+
*
52+
* var x = ones2d( shape );
53+
* var y = ones2d( shape );
54+
* var z = zeros2d( shape );
55+
*
56+
* binary2d( [ x, y, z ], shape, add );
57+
*
58+
* console.log( z );
59+
* // => [ [ 2.0, 2.0 ], [ 2.0, 2.0 ] ]
60+
*/
61+
declare function binary2d<T = unknown, U = unknown, V = unknown>( arrays: [ Array2D<T>, Array2D<U>, Array2D<V> ], shape: Shape2D, fcn: Binary<T, U, V> ): void;
62+
63+
64+
// EXPORTS //
65+
66+
export = binary2d;

0 commit comments

Comments
 (0)