Skip to content

Commit 0333160

Browse files
committed
Auto-generated commit
1 parent ea9779a commit 0333160

13 files changed

Lines changed: 982 additions & 5 deletions

File tree

base/binary4d/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+
# binary4d
22+
23+
> Apply a binary callback to elements in four-dimensional nested input arrays and assign results to elements in a four-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 binary4d = require( '@stdlib/array/base/binary4d' );
37+
```
38+
39+
#### binary4d( arrays, shape, fcn )
40+
41+
Applies a binary callback to elements in four-dimensional nested input arrays and assigns results to elements in a four-dimensional nested output array.
42+
43+
```javascript
44+
var add = require( '@stdlib/math/base/ops/add' );
45+
var zeros4d = require( '@stdlib/array/base/zeros4d' );
46+
47+
var x = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ];
48+
var z = zeros4d( [ 1, 1, 2, 2 ] );
49+
50+
var shape = [ 1, 1, 2, 2 ];
51+
52+
binary4d( [ 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 filled4dBy = require( '@stdlib/array/base/filled4d-by' );
85+
var zeros4d = require( '@stdlib/array/base/zeros4d' );
86+
var add = require( '@stdlib/math/base/ops/add' );
87+
var binary4d = require( '@stdlib/array/base/binary4d' );
88+
89+
var shape = [ 2, 2, 3, 3 ];
90+
91+
var x = filled4dBy( shape, discreteUniform( -100, 100 ) );
92+
console.log( x );
93+
94+
var y = filled4dBy( shape, discreteUniform( -100, 100 ) );
95+
console.log( y );
96+
97+
var z = zeros4d( shape );
98+
console.log( z );
99+
100+
binary4d( [ 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: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 filled4dBy = require( './../../../base/filled4d-by' );
30+
var zeros4d = require( './../../../base/zeros4d' );
31+
var numel = require( '@stdlib/ndarray/base/numel' );
32+
var pkg = require( './../package.json' ).name;
33+
var binary4d = 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 = filled4dBy( shape, uniform( -100.0, 100.0 ) );
52+
y = filled4dBy( shape, uniform( -100.0, 100.0 ) );
53+
z = zeros4d( 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 i2;
69+
var i3;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
binary4d( arrays, shape, add );
75+
i3 = i % shape[ 0 ];
76+
i2 = i % shape[ 1 ];
77+
i1 = i % shape[ 2 ];
78+
i0 = i % shape[ 3 ];
79+
if ( isnan( arrays[ 2 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
}
83+
b.toc();
84+
85+
i3 = i % shape[ 0 ];
86+
i2 = i % shape[ 1 ];
87+
i1 = i % shape[ 2 ];
88+
i0 = i % shape[ 3 ];
89+
if ( isnan( arrays[ 2 ][ i3 ][ i2 ][ i1 ][ i0 ] ) ) {
90+
b.fail( 'should not return NaN' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
}
95+
}
96+
97+
98+
// MAIN //
99+
100+
/**
101+
* Main execution sequence.
102+
*
103+
* @private
104+
*/
105+
function main() {
106+
var min;
107+
var max;
108+
var sh;
109+
var N;
110+
var f;
111+
var i;
112+
113+
min = 1; // 10^min
114+
max = 6; // 10^max
115+
116+
for ( i = min; i <= max; i++ ) {
117+
N = floor( pow( pow( 10, i ), 1.0/4.0 ) );
118+
sh = [ N, N, N, N ];
119+
f = createBenchmark( sh );
120+
bench( pkg+'::equidimensional:size='+numel( sh ), f );
121+
}
122+
}
123+
124+
main();

base/binary4d/docs/repl.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
{{alias}}( arrays, shape, fcn )
3+
Applies a binary callback to elements in four-dimensional nested input
4+
arrays and assigns results to elements in a four-dimensional nested output
5+
array.
6+
7+
Parameters
8+
----------
9+
arrays: ArrayLikeObject
10+
Array-like object containing two input nested arrays and one output
11+
nested array.
12+
13+
shape: Array<integer>
14+
Array shape.
15+
16+
fcn: Function
17+
Binary callback.
18+
19+
Examples
20+
--------
21+
> var x = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ];
22+
> var y = [ [ [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ] ];
23+
> var z = [ [ [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ] ] ];
24+
> var shape = [ 1, 1, 2, 2 ];
25+
> {{alias}}( [ x, y, z ], shape, {{alias:@stdlib/math/base/ops/add}} );
26+
> z
27+
[ [ [ [ 2.0, 4.0 ], [ 6.0, 8.0 ] ] ] ]
28+
29+
See Also
30+
--------
31+
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 { Array4D } from '@stdlib/types/array';
24+
import { Shape4D } 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 four-dimensional nested input arrays and assigns results to elements in a four-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 ones4d = require( `@stdlib/array/base/ones4d` );
47+
* var zeros4d = require( `@stdlib/array/base/zeros4d` );
48+
* var add = require( `@stdlib/math/base/ops/add` );
49+
*
50+
* var shape = [ 1, 2, 2, 2 ];
51+
*
52+
* var x = ones4d( shape );
53+
* var y = ones4d( shape );
54+
* var z = zeros4d( shape );
55+
*
56+
* binary4d( [ x, y, z ], shape, add );
57+
*
58+
* console.log( z );
59+
* // => [ [ [ [ 2.0, 2.0 ], [ 2.0, 2.0 ] ], [ [ 2.0, 2.0 ], [ 2.0, 2.0 ] ] ] ]
60+
*/
61+
declare function binary4d<T = unknown, U = unknown, V = unknown>( arrays: [ Array4D<T>, Array4D<U>, Array4D<V> ], shape: Shape4D, fcn: Binary<T, U, V> ): void;
62+
63+
64+
// EXPORTS //
65+
66+
export = binary4d;

0 commit comments

Comments
 (0)