Skip to content

Commit 5f9a1c0

Browse files
committed
feat: add array/base/quinary2d
1 parent 0248a5e commit 5f9a1c0

File tree

10 files changed

+952
-0
lines changed

10 files changed

+952
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
# quinary2d
22+
23+
> Apply a quinary callback to elements in five 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 quinary2d = require( '@stdlib/array/base/quinary2d' );
37+
```
38+
39+
#### quinary2d( arrays, shape, fcn )
40+
41+
Applies a quinary callback to elements in five 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, w, v ) {
47+
return x + y + z + w + v;
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+
quinary2d( [ x, x, x, x, x, out ], shape, add );
56+
// out => [ [ 5.0, 10.0 ], [ 15.0, 20.0 ] ]
57+
```
58+
59+
The function accepts the following arguments:
60+
61+
- **arrays**: array-like object containing five input nested arrays and one output nested array.
62+
- **shape**: array shape.
63+
- **fcn**: quinary 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 quinary2d = require( '@stdlib/array/base/quinary2d' );
90+
91+
function add( x, y, z, w, v ) {
92+
return x + y + z + w + v;
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 w = filled2dBy( shape, discreteUniform( -100, 100 ) );
107+
console.log( w );
108+
109+
var v = filled2dBy( shape, discreteUniform( -100, 100 ) );
110+
console.log( v );
111+
112+
var out = zeros2d( shape );
113+
console.log( out );
114+
115+
quinary2d( [ x, y, z, w, v, out ], shape, add );
116+
console.log( out );
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
124+
125+
<section class="related">
126+
127+
</section>
128+
129+
<!-- /.related -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
</section>
136+
137+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 quinary2d = 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 - array shape
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( shape ) {
60+
var arrays;
61+
var out;
62+
var x;
63+
var y;
64+
var z;
65+
var w;
66+
var v;
67+
68+
x = filled2dBy( shape, uniform( -100.0, 100.0 ) );
69+
y = filled2dBy( shape, uniform( -100.0, 100.0 ) );
70+
z = filled2dBy( shape, uniform( -100.0, 100.0 ) );
71+
w = filled2dBy( shape, uniform( -100.0, 100.0 ) );
72+
v = filled2dBy( shape, uniform( -100.0, 100.0 ) );
73+
out = zeros2d( shape );
74+
75+
arrays = [ x, y, z, w, v, out ];
76+
77+
return benchmark;
78+
79+
/**
80+
* Benchmark function.
81+
*
82+
* @private
83+
* @param {Benchmark} b - benchmark instance
84+
*/
85+
function benchmark( b ) {
86+
var i0;
87+
var i1;
88+
var i;
89+
90+
b.tic();
91+
for ( i = 0; i < b.iterations; i++ ) {
92+
quinary2d( arrays, shape, add );
93+
i1 = i % shape[ 0 ];
94+
i0 = i % shape[ 1 ];
95+
if ( isnan( arrays[ 5 ][ i1 ][ i0 ] ) ) {
96+
b.fail( 'should not return NaN' );
97+
}
98+
}
99+
b.toc();
100+
101+
i1 = i % shape[ 0 ];
102+
i0 = i % shape[ 1 ];
103+
if ( isnan( arrays[ 5 ][ i1 ][ i0 ] ) ) {
104+
b.fail( 'should not return NaN' );
105+
}
106+
b.pass( 'benchmark finished' );
107+
b.end();
108+
}
109+
}
110+
111+
112+
// MAIN //
113+
114+
/**
115+
* Main execution sequence.
116+
*
117+
* @private
118+
*/
119+
function main() {
120+
var min;
121+
var max;
122+
var sh;
123+
var N;
124+
var f;
125+
var i;
126+
127+
min = 1; // 10^min
128+
max = 6; // 10^max
129+
130+
for ( i = min; i <= max; i++ ) {
131+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
132+
sh = [ N, N ];
133+
f = createBenchmark( sh );
134+
bench( pkg+'::square_matrix:size='+numel( sh ), f );
135+
}
136+
}
137+
138+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( arrays, shape, fcn )
3+
Applies a quinary callback to elements in five two-dimensional nested
4+
input arrays and assigns results to elements in a two-dimensional nested
5+
output array.
6+
7+
Parameters
8+
----------
9+
arrays: ArrayLikeObject
10+
Array-like object containing five input nested arrays and one output
11+
nested array.
12+
13+
shape: Array<integer>
14+
Array shape.
15+
16+
fcn: Function
17+
Quinary callback.
18+
19+
Examples
20+
--------
21+
> function fcn( x, y, z, w, v ) { return x + y + z + w + v; };
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 w = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
26+
> var v = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
27+
> var out = [ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ];
28+
> var shape = [ 2, 2 ];
29+
> {{alias}}( [ x, y, z, w, v, out ], shape, fcn );
30+
> out
31+
[ [ 5.0, 10.0 ], [ 15.0, 20.0 ] ]
32+
33+
See Also
34+
--------
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
* Quinary callback.
28+
*
29+
* @param value - input value
30+
* @returns result
31+
*/
32+
type Quinary<T, U, V, W, X, Y> = ( v1: T, v2: U, v3: V, v4: W, v5: X ) => Y;
33+
34+
/**
35+
* Applies a quinary callback to elements in five 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 five input nested arrays and one output nested array
42+
* @param shape - array shape
43+
* @param fcn - quinary 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, w, v ) {
50+
* return x + y + z + w + v;
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 w = ones2d( shape );
59+
* var v = ones2d( shape );
60+
* var out = zeros2d( shape );
61+
*
62+
* quinary2d( [ x, y, z, w, v, out ], shape, add );
63+
*
64+
* console.log( out );
65+
* // => [ [ 5.0, 5.0 ], [ 5.0, 5.0 ] ]
66+
*/
67+
declare function quinary2d<T = unknown, U = unknown, V = unknown, W = unknown, X = unknown, Y = unknown>( arrays: [ Array2D<T>, Array2D<U>, Array2D<V>, Array2D<W>, Array2D<X>, Array2D<Y> ], shape: Shape2D, fcn: Quinary<T, U, V, W, X, Y> ): void;
68+
69+
70+
// EXPORTS //
71+
72+
export = quinary2d;

0 commit comments

Comments
 (0)