Skip to content

Commit f2f50f1

Browse files
committed
Add strided interface to perform element-wise addition via a callback function
1 parent a4397e5 commit f2f50f1

File tree

14 files changed

+2546
-0
lines changed

14 files changed

+2546
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
<!-- lint disable maximum-heading-length -->
22+
23+
# addBy
24+
25+
> Element-wise addition of two strided arrays via a callback function.
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var addBy = require( '@stdlib/math/strided/ops/add-by' );
39+
```
40+
41+
#### addBy( N, x, strideX, y, strideY, z, strideZ, clbk\[, thisArg] )
42+
43+
Performs element-wise addition of two strided arrays via a callback function and assigns each result to an element in an output strided array.
44+
45+
```javascript
46+
function accessor( values ) {
47+
return values;
48+
}
49+
50+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
51+
var y = [ 11.0, 12.0, 13.0, 14.0, 15.0 ];
52+
var z = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
53+
54+
addBy( x.length, x, 1, y, 1, z, 1, accessor );
55+
// z => [ 12.0, 14.0, 16.0, 18.0, 20.0 ]
56+
```
57+
58+
The function accepts the following arguments:
59+
60+
- **N**: number of indexed elements.
61+
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
62+
- **strideX**: index increment for `x`.
63+
- **y**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
64+
- **strideY**: index increment for `y`.
65+
- **z**: output [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
66+
- **strideZ**: index increment for `z`.
67+
- **clbk**: callback function.
68+
- **thisArg**: execution context (_optional_).
69+
70+
The invoked callback function is provided four arguments:
71+
72+
- **values**: input array element values `[vx, vy]`.
73+
- **idx**: iteration index (zero-based).
74+
- **indices**: input and output array strided indices `[ix, iy, iz]` (computed according to `offset + idx*stride`).
75+
- **arrays**: input and output arrays/collections `[x, y, z]`.
76+
77+
To set the callback execution context, provide a `thisArg`.
78+
79+
```javascript
80+
function accessor( values ) {
81+
this.count += 1;
82+
return values;
83+
}
84+
85+
var context = {
86+
'count': 0
87+
};
88+
89+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
90+
var y = [ 11.0, 12.0, 13.0, 14.0, 15.0 ];
91+
var z = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
92+
93+
addBy( x.length, x, 1, y, 1, z, 1, accessor, context );
94+
// z => [ 12.0, 14.0, 16.0, 18.0, 20.0 ]
95+
96+
var cnt = context.count;
97+
// returns 5
98+
```
99+
100+
The `N` and `stride` parameters determine which elements in the strided arrays are accessed at runtime. For example, to index every other value in `x` and to index the first `N` elements of `y` in reverse order,
101+
102+
```javascript
103+
function accessor( values ) {
104+
return values;
105+
}
106+
107+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
108+
var y = [ 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ];
109+
var z = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
110+
111+
addBy( 3, x, 2, y, -1, z, 1, accessor );
112+
// z => [ 14.0, 15.0, 16.0, 0.0, 0.0, 0.0 ]
113+
```
114+
115+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
116+
117+
```javascript
118+
var Float64Array = require( '@stdlib/array/float64' );
119+
120+
function accessor( values ) {
121+
return values;
122+
}
123+
124+
// Initial arrays...
125+
var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
126+
var y0 = new Float64Array( [ 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ] );
127+
var z0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
128+
129+
// Create offset views...
130+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
131+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element
132+
var z1 = new Float64Array( z0.buffer, z0.BYTES_PER_ELEMENT*3 ); // start at 4th element
133+
134+
addBy( 3, x1, -2, y1, 1, z1, 1, accessor );
135+
// z0 => <Float64Array>[ 0.0, 0.0, 0.0, 20.0, 19.0, 18.0 ]
136+
```
137+
138+
#### addBy.ndarray( N, x, strideX, offsetX, y, strideY, offsetY, z, strideZ, offsetZ, clbk\[, thisArg] )
139+
140+
Performs element-wise addition of two strided arrays via a callback function and assigns each result to an element in an output strided array using alternative indexing semantics.
141+
142+
```javascript
143+
function accessor( values ) {
144+
return values;
145+
}
146+
147+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
148+
var y = [ 11.0, 12.0, 13.0, 14.0, 15.0 ];
149+
var z = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
150+
151+
addBy.ndarray( x.length, x, 1, 0, y, 1, 0, z, 1, 0, accessor );
152+
// z => [ 12.0, 14.0, 16.0, 18.0, 20.0 ]
153+
```
154+
155+
The function accepts the following additional arguments:
156+
157+
- **offsetX**: starting index for `x`.
158+
- **offsetY**: starting index for `y`.
159+
- **offsetZ**: starting index for `z`.
160+
161+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the offset parameters support indexing semantics based on starting indices. For example, to index every other value in `x` starting from the second value and to index the last `N` elements in `y`,
162+
163+
```javascript
164+
function accessor( values ) {
165+
return values;
166+
}
167+
168+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
169+
var y = [ 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ];
170+
var z = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
171+
172+
addBy.ndarray( 3, x, 2, 1, y, -1, y.length-1, z, 1, 2, accessor );
173+
// z => [ 0.0, 0.0, 0.0, 20.0, 19.0, 18.0 ]
174+
```
175+
176+
</section>
177+
178+
<!-- /.usage -->
179+
180+
<section class="notes">
181+
182+
## Notes
183+
184+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
185+
186+
```javascript
187+
function accessor() {
188+
// No-op...
189+
}
190+
191+
var x = [ 1.0, 2.0, 3.0, 4.0, 5.0 ];
192+
var y = [ 11.0, 12.0, 13.0, 14.0, 15.0 ];
193+
var z = [ 0.0, 0.0, 0.0, 0.0, 0.0 ];
194+
195+
addBy( x.length, x, 1, y, 1, z, 1, accessor );
196+
// z => [ 0.0, 0.0, 0.0, 0.0, 0.0 ]
197+
```
198+
199+
</section>
200+
201+
<!-- /.notes -->
202+
203+
<section class="examples">
204+
205+
## Examples
206+
207+
<!-- eslint no-undef: "error" -->
208+
209+
```javascript
210+
var uniform = require( '@stdlib/random/base/uniform' ).factory;
211+
var filledarray = require( '@stdlib/array/filled' );
212+
var filledarrayBy = require( '@stdlib/array/filled-by' );
213+
var addBy = require( '@stdlib/math/strided/ops/add-by' );
214+
215+
function accessor( values, i ) {
216+
if ( (i%3) === 0 ) {
217+
// Simulate a "missing" value...
218+
return;
219+
}
220+
return values;
221+
}
222+
223+
var x = filledarrayBy( 10, 'generic', uniform( -10.0, 10.0 ) );
224+
console.log( x );
225+
226+
var y = filledarrayBy( x.length, 'generic', uniform( -10.0, 10.0 ) );
227+
console.log( y );
228+
229+
var z = filledarray( null, x.length, 'generic' );
230+
console.log( z );
231+
232+
addBy.ndarray( x.length, x, 1, 0, y, -1, y.length-1, z, 1, 0, accessor );
233+
console.log( z );
234+
```
235+
236+
</section>
237+
238+
<!-- /.examples -->
239+
240+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
241+
242+
<section class="related">
243+
244+
</section>
245+
246+
<!-- /.related -->
247+
248+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
249+
250+
<section class="links">
251+
252+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
253+
254+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
255+
256+
[@stdlib/math/base/ops/add]: https://github.com/stdlib-js/stdlib
257+
258+
</section>
259+
260+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 filledarray = require( '@stdlib/array/filled' );
28+
var filledarrayBy = require( '@stdlib/array/filled-by' );
29+
var pkg = require( './../package.json' ).name;
30+
var addBy = require( './../lib/main.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var rand = uniform( -10.0, 10.0 );
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Accessor function.
42+
*
43+
* @private
44+
* @param {Array<number>} values - array elements
45+
* @returns {Array<number>} accessed values
46+
*/
47+
function accessor( values ) {
48+
return values;
49+
}
50+
51+
/**
52+
* Creates a benchmark function.
53+
*
54+
* @private
55+
* @param {PositiveInteger} len - array length
56+
* @returns {Function} benchmark function
57+
*/
58+
function createBenchmark( len ) {
59+
var x;
60+
var y;
61+
var z;
62+
63+
x = filledarrayBy( len, 'generic', rand );
64+
y = filledarrayBy( len, 'generic', rand );
65+
z = filledarray( 0.0, len, 'generic' );
66+
67+
return benchmark;
68+
69+
/**
70+
* Benchmark function.
71+
*
72+
* @private
73+
* @param {Benchmark} b - benchmark instance
74+
*/
75+
function benchmark( b ) {
76+
var out;
77+
var i;
78+
79+
b.tic();
80+
for ( i = 0; i < b.iterations; i++ ) {
81+
out = addBy( x.length, x, 1, y, 1, z, 1, accessor );
82+
if ( isnan( out[ i%len ] ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
}
86+
b.toc();
87+
if ( isnan( out[ i%len ] ) ) {
88+
b.fail( 'should not return NaN' );
89+
}
90+
b.pass( 'benchmark finished' );
91+
b.end();
92+
}
93+
}
94+
95+
96+
// MAIN //
97+
98+
/**
99+
* Main execution sequence.
100+
*
101+
* @private
102+
*/
103+
function main() {
104+
var len;
105+
var min;
106+
var max;
107+
var f;
108+
var i;
109+
110+
min = 1; // 10^min
111+
max = 6; // 10^max
112+
113+
for ( i = min; i <= max; i++ ) {
114+
len = pow( 10, i );
115+
f = createBenchmark( len );
116+
bench( pkg+':len='+len, f );
117+
}
118+
}
119+
120+
main();

0 commit comments

Comments
 (0)