|
| 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 --> |
0 commit comments