|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# dnansumpw |
| 22 | + |
| 23 | +> Calculate the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var dnansumpw = require( '@stdlib/blas/ext/base/wasm/dnansumpw' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### dnansumpw.main( N, x, strideX ) |
| 34 | + |
| 35 | +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 39 | + |
| 40 | +var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); |
| 41 | + |
| 42 | +var v = dnansumpw.main( x.length, x, 1 ); |
| 43 | +// returns 1.0 |
| 44 | +``` |
| 45 | + |
| 46 | +The function has the following parameters: |
| 47 | + |
| 48 | +- **N**: number of indexed elements. |
| 49 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 50 | +- **strideX**: stride length for `x`. |
| 51 | + |
| 52 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the sum of every other element in `x`, |
| 53 | + |
| 54 | +```javascript |
| 55 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 56 | + |
| 57 | +var x = new Float64Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] ); |
| 58 | + |
| 59 | +var sum = dnansumpw.main( 4, x, 2 ); |
| 60 | +// returns 5.0 |
| 61 | +``` |
| 62 | + |
| 63 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 64 | + |
| 65 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 66 | + |
| 67 | +```javascript |
| 68 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 69 | + |
| 70 | +var x0 = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 71 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 72 | + |
| 73 | +var v = dnansumpw.main( 4, x1, 2 ); |
| 74 | +// returns 5.0 |
| 75 | +``` |
| 76 | + |
| 77 | +#### dnansumpw.ndarray( N, x, strideX, offsetX ) |
| 78 | + |
| 79 | +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics. |
| 80 | + |
| 81 | +```javascript |
| 82 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 83 | + |
| 84 | +var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); |
| 85 | + |
| 86 | +var v = dnansumpw.ndarray( x.length, x, 1, 0 ); |
| 87 | +// returns 1.0 |
| 88 | +``` |
| 89 | + |
| 90 | +The function has the following additional parameters: |
| 91 | + |
| 92 | +- **offsetX**: starting index for `x`. |
| 93 | + |
| 94 | +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other element starting from the second element: |
| 95 | + |
| 96 | +```javascript |
| 97 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 98 | + |
| 99 | +var x = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); |
| 100 | + |
| 101 | +var v = dnansumpw.ndarray( 4, x, 2, 1 ); |
| 102 | +// returns 5.0 |
| 103 | +``` |
| 104 | + |
| 105 | +* * * |
| 106 | + |
| 107 | +### Module |
| 108 | + |
| 109 | +#### dnansumpw.Module( memory ) |
| 110 | + |
| 111 | +Returns a new WebAssembly [module wrapper][@stdlib/wasm/module-wrapper] instance which uses the provided WebAssembly [memory][@stdlib/wasm/memory] instance as its underlying memory. |
| 112 | + |
| 113 | +<!-- eslint-disable node/no-sync --> |
| 114 | + |
| 115 | +```javascript |
| 116 | +var Memory = require( '@stdlib/wasm/memory' ); |
| 117 | + |
| 118 | +// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB): |
| 119 | +var mem = new Memory({ |
| 120 | + 'initial': 10, |
| 121 | + 'maximum': 100 |
| 122 | +}); |
| 123 | + |
| 124 | +// Create a BLAS routine: |
| 125 | +var mod = new dnansumpw.Module( mem ); |
| 126 | +// returns <Module> |
| 127 | + |
| 128 | +// Initialize the routine: |
| 129 | +mod.initializeSync(); |
| 130 | +``` |
| 131 | + |
| 132 | +#### dnansumpw.Module.prototype.main( N, xp, sx ) |
| 133 | + |
| 134 | +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. |
| 135 | + |
| 136 | +<!-- eslint-disable node/no-sync --> |
| 137 | + |
| 138 | +```javascript |
| 139 | +var Memory = require( '@stdlib/wasm/memory' ); |
| 140 | +var oneTo = require( '@stdlib/array/one-to' ); |
| 141 | +var zeros = require( '@stdlib/array/zeros' ); |
| 142 | + |
| 143 | +// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB): |
| 144 | +var mem = new Memory({ |
| 145 | + 'initial': 10, |
| 146 | + 'maximum': 100 |
| 147 | +}); |
| 148 | + |
| 149 | +// Create a BLAS routine: |
| 150 | +var mod = new dnansumpw.Module( mem ); |
| 151 | +// returns <Module> |
| 152 | + |
| 153 | +// Initialize the routine: |
| 154 | +mod.initializeSync(); |
| 155 | + |
| 156 | +// Define a vector data type: |
| 157 | +var dtype = 'float64'; |
| 158 | + |
| 159 | +// Specify a vector length: |
| 160 | +var N = 3; |
| 161 | + |
| 162 | +// Define a pointer (i.e., byte offset) for storing the input vector: |
| 163 | +var xptr = 0; |
| 164 | + |
| 165 | +// Write vector values to module memory: |
| 166 | +mod.write( xptr, oneTo( N, dtype ) ); |
| 167 | + |
| 168 | +// Perform computation: |
| 169 | +var v = mod.main( N, xptr, 1 ); |
| 170 | +// returns 6.0 |
| 171 | +``` |
| 172 | + |
| 173 | +The function has the following parameters: |
| 174 | + |
| 175 | +- **N**: number of indexed elements. |
| 176 | +- **xp**: input [`Float64Array`][@stdlib/array/float64] pointer (i.e., byte offset). |
| 177 | +- **sx**: stride length for `x`. |
| 178 | + |
| 179 | +#### dnansumpw.Module.prototype.ndarray( N, xp, sx, ox ) |
| 180 | + |
| 181 | +Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics. |
| 182 | + |
| 183 | +<!-- eslint-disable node/no-sync --> |
| 184 | + |
| 185 | +```javascript |
| 186 | +var Memory = require( '@stdlib/wasm/memory' ); |
| 187 | +var oneTo = require( '@stdlib/array/one-to' ); |
| 188 | +var zeros = require( '@stdlib/array/zeros' ); |
| 189 | + |
| 190 | +// Create a new memory instance with an initial size of 10 pages (640KiB) and a maximum size of 100 pages (6.4MiB): |
| 191 | +var mem = new Memory({ |
| 192 | + 'initial': 10, |
| 193 | + 'maximum': 100 |
| 194 | +}); |
| 195 | + |
| 196 | +// Create a BLAS routine: |
| 197 | +var mod = new dnansumpw.Module( mem ); |
| 198 | +// returns <Module> |
| 199 | + |
| 200 | +// Initialize the routine: |
| 201 | +mod.initializeSync(); |
| 202 | + |
| 203 | +// Define a vector data type: |
| 204 | +var dtype = 'float64'; |
| 205 | + |
| 206 | +// Specify a vector length: |
| 207 | +var N = 3; |
| 208 | + |
| 209 | +// Define a pointer (i.e., byte offset) for storing the input vector: |
| 210 | +var xptr = 0; |
| 211 | + |
| 212 | +// Write vector values to module memory: |
| 213 | +mod.write( xptr, oneTo( N, dtype ) ); |
| 214 | + |
| 215 | +// Perform computation: |
| 216 | +var v = mod.ndarray( N, xptr, 1, 0 ); |
| 217 | +// returns 6.0 |
| 218 | +``` |
| 219 | + |
| 220 | +The function has the following additional parameters: |
| 221 | + |
| 222 | +- **ox**: starting index for `x`. |
| 223 | + |
| 224 | +</section> |
| 225 | + |
| 226 | +<!-- /.usage --> |
| 227 | + |
| 228 | +<section class="notes"> |
| 229 | + |
| 230 | +* * * |
| 231 | + |
| 232 | +## Notes |
| 233 | + |
| 234 | +- If `N <= 0`, both `main` and `ndarray` methods return `0.0`. |
| 235 | +- This package implements routines using WebAssembly. When provided arrays which are not allocated on a `dnansumpw` module memory instance, data must be explicitly copied to module memory prior to computation. Data movement may entail a performance cost, and, thus, if you are using arrays external to module memory, you should prefer using [`@stdlib/blas/ext/base/dnansumpw`][@stdlib/blas/ext/base/dnansumpw]. However, if working with arrays which are allocated and explicitly managed on module memory, you can achieve better performance when compared to the pure JavaScript implementations found in [`@stdlib/blas/ext/base/dnansumpw`][@stdlib/blas/ext/base/dnansumpw]. Beware that such performance gains may come at the cost of additional complexity when having to perform manual memory management. Choosing between implementations depends heavily on the particular needs and constraints of your application, with no one choice universally better than the other. |
| 236 | + |
| 237 | +</section> |
| 238 | + |
| 239 | +<!-- /.notes --> |
| 240 | + |
| 241 | +<section class="examples"> |
| 242 | + |
| 243 | +* * * |
| 244 | + |
| 245 | +## Examples |
| 246 | + |
| 247 | +<!-- eslint no-undef: "error" --> |
| 248 | + |
| 249 | +```javascript |
| 250 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 251 | +var dnansumpw = require( '@stdlib/blas/ext/base/wasm/dnansumpw' ); |
| 252 | + |
| 253 | +var opts = { |
| 254 | + 'dtype': 'float64' |
| 255 | +}; |
| 256 | +var x = discreteUniform( 10, 0, 100, opts ); |
| 257 | +console.log( x ); |
| 258 | + |
| 259 | +var v = dnansumpw.ndarray( x.length, x, 1, 0 ); |
| 260 | +console.log( v ); |
| 261 | +``` |
| 262 | + |
| 263 | +</section> |
| 264 | + |
| 265 | +<!-- /.examples --> |
| 266 | + |
| 267 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 268 | + |
| 269 | +<section class="related"> |
| 270 | + |
| 271 | +</section> |
| 272 | + |
| 273 | +<!-- /.related --> |
| 274 | + |
| 275 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 276 | + |
| 277 | +<section class="links"> |
| 278 | + |
| 279 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 280 | + |
| 281 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 282 | + |
| 283 | +[@stdlib/wasm/memory]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/memory |
| 284 | + |
| 285 | +[@stdlib/wasm/module-wrapper]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/wasm/module-wrapper |
| 286 | + |
| 287 | +[@stdlib/blas/ext/base/dnansumpw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dnansumpw |
| 288 | + |
| 289 | +</section> |
| 290 | + |
| 291 | +<!-- /.links --> |
0 commit comments