|
| 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 | +# dnancusumkbn |
| 22 | + |
| 23 | +> Calculate the cumulative sum of double-precision floating-point strided array elements ignoring `NaN` values and using an improved Kahan–Babuška algorithm. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var dnancusumkbn = require( '@stdlib/blas/ext/base/dnancusumkbn' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### dnancusumkbn( N, sum, x, strideX, y, strideY ) |
| 40 | + |
| 41 | +Computes the cumulative sum of double-precision floating-point strided array elements ignoring `NaN` values and using an improved Kahan–Babuška algorithm. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 45 | + |
| 46 | +var x = new Float64Array( [ 1.0, -2.0, NaN ] ); |
| 47 | +var y = new Float64Array( x.length ); |
| 48 | + |
| 49 | +dnancusumkbn( x.length, 0.0, x, 1, y, 1 ); |
| 50 | +// y => <Float64Array>[ 1.0, -1.0, -1.0 ] |
| 51 | + |
| 52 | +x = new Float64Array( [ 1.0, -2.0, NaN ] ); |
| 53 | +y = new Float64Array( x.length ); |
| 54 | + |
| 55 | +dnancusumkbn( x.length, 10.0, x, 1, y, 1 ); |
| 56 | +// y => <Float64Array>[ 11.0, 9.0, 9.0 ] |
| 57 | +``` |
| 58 | + |
| 59 | +The function has the following parameters: |
| 60 | + |
| 61 | +- **N**: number of indexed elements. |
| 62 | +- **sum**: initial sum. |
| 63 | +- **x**: input [`Float64Array`][@stdlib/array/float64]. |
| 64 | +- **strideX**: stride length for `x`. |
| 65 | +- **y**: output [`Float64Array`][@stdlib/array/float64]. |
| 66 | +- **strideY**: stride length for `y`. |
| 67 | + |
| 68 | +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the cumulative sum of every other element: |
| 69 | + |
| 70 | +```javascript |
| 71 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 72 | + |
| 73 | +var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, NaN, NaN ] ); |
| 74 | +var y = new Float64Array( x.length ); |
| 75 | + |
| 76 | +var v = dnancusumkbn( 4, 0.0, x, 2, y, 1 ); |
| 77 | +// y => <Float64Array>[ 1.0, 3.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 ] |
| 78 | +``` |
| 79 | + |
| 80 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 81 | + |
| 82 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 83 | + |
| 84 | +```javascript |
| 85 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 86 | + |
| 87 | +// Initial arrays... |
| 88 | +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, NaN, NaN ] ); |
| 89 | +var y0 = new Float64Array( x0.length ); |
| 90 | + |
| 91 | +// Create offset views... |
| 92 | +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 93 | +var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element |
| 94 | + |
| 95 | +dnancusumkbn( 4, 0.0, x1, -2, y1, 1 ); |
| 96 | +// y0 => <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 1.0, 0.0 ] |
| 97 | +``` |
| 98 | + |
| 99 | +#### dnancusumkbn.ndarray( N, sum, x, strideX, offsetX, y, strideY, offsetY ) |
| 100 | + |
| 101 | +Computes the cumulative sum of double-precision floating-point strided array elements ignoring `NaN` values and using an improved Kahan–Babuška algorithm and alternative indexing semantics. |
| 102 | + |
| 103 | +```javascript |
| 104 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 105 | + |
| 106 | +var x = new Float64Array( [ 1.0, -2.0, NaN ] ); |
| 107 | +var y = new Float64Array( x.length ); |
| 108 | + |
| 109 | +dnancusumkbn.ndarray( x.length, 0.0, x, 1, 0, y, 1, 0 ); |
| 110 | +// y => <Float64Array>[ 1.0, -1.0, -1.0 ] |
| 111 | +``` |
| 112 | + |
| 113 | +The function has the following additional parameters: |
| 114 | + |
| 115 | +- **offsetX**: starting index for `x`. |
| 116 | +- **offsetY**: starting index for `y`. |
| 117 | + |
| 118 | +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 calculate the cumulative sum of every other element in the strided input array starting from the second element and to store in the last `N` elements of the strided output array starting from the last element: |
| 119 | + |
| 120 | +```javascript |
| 121 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 122 | + |
| 123 | +var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, NaN, NaN ] ); |
| 124 | +var y = new Float64Array( x.length ); |
| 125 | + |
| 126 | +dnancusumkbn.ndarray( 4, 0.0, x, 2, 1, y, -1, y.length-1 ); |
| 127 | +// y => <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, -1.0, 1.0 ] |
| 128 | +``` |
| 129 | + |
| 130 | +</section> |
| 131 | + |
| 132 | +<!-- /.usage --> |
| 133 | + |
| 134 | +<section class="notes"> |
| 135 | + |
| 136 | +## Notes |
| 137 | + |
| 138 | +- If `N <= 0`, both functions return `y` unchanged. |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.notes --> |
| 143 | + |
| 144 | +<section class="examples"> |
| 145 | + |
| 146 | +## Examples |
| 147 | + |
| 148 | +<!-- eslint no-undef: "error" --> |
| 149 | + |
| 150 | +```javascript |
| 151 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 152 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 153 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 154 | +var zeros = require( '@stdlib/array/zeros' ); |
| 155 | +var dnancusumkbn = require( '@stdlib/blas/ext/base/dnancusumkbn' ); |
| 156 | + |
| 157 | +function rand() { |
| 158 | + if ( bernoulli( 0.7 ) > 0 ) { |
| 159 | + return discreteUniform( 0, 100 ); |
| 160 | + } |
| 161 | + return NaN; |
| 162 | +} |
| 163 | + |
| 164 | +var x = filledarrayBy( 10, 'float64', rand ); |
| 165 | +console.log( x ); |
| 166 | + |
| 167 | +var y = zeros( 10, 'float64' ); |
| 168 | +console.log( y ); |
| 169 | + |
| 170 | +dnancusumkbn( x.length, 0.0, x, 1, y, -1 ); |
| 171 | +console.log( y ); |
| 172 | +``` |
| 173 | + |
| 174 | +</section> |
| 175 | + |
| 176 | +<!-- /.examples --> |
| 177 | + |
| 178 | +<!-- C interface documentation. --> |
| 179 | + |
| 180 | +* * * |
| 181 | + |
| 182 | +<section class="c"> |
| 183 | + |
| 184 | +## C APIs |
| 185 | + |
| 186 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 187 | + |
| 188 | +<section class="intro"> |
| 189 | + |
| 190 | +</section> |
| 191 | + |
| 192 | +<!-- /.intro --> |
| 193 | + |
| 194 | +<!-- C usage documentation. --> |
| 195 | + |
| 196 | +<section class="usage"> |
| 197 | + |
| 198 | +### Usage |
| 199 | + |
| 200 | +```c |
| 201 | +#include "stdlib/blas/ext/base/dnancusumkbn.h" |
| 202 | +``` |
| 203 | + |
| 204 | +#### stdlib_strided_dnancusumkbn( N, sum, \*X, strideX, \*Y, strideY ) |
| 205 | + |
| 206 | +Computes the cumulative sum of double-precision floating-point strided array elements ignoring `NaN` values and using an improved Kahan–Babuška algorithm. |
| 207 | + |
| 208 | +```c |
| 209 | +const double x[] = { 1.0, 2.0, 3.0, 4.0 } |
| 210 | +double y[] = { 0.0, 0.0, 0.0, 0.0 } |
| 211 | + |
| 212 | +stdlib_strided_dnancusumkbn( 4, 0.0, x, 1, y, 1 ); |
| 213 | +``` |
| 214 | +
|
| 215 | +The function accepts the following arguments: |
| 216 | +
|
| 217 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 218 | +- **sum**: `[in] double` initial sum. |
| 219 | +- **X**: `[in] double*` input array. |
| 220 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 221 | +- **Y**: `[out] double*` output array. |
| 222 | +- **strideY**: `[in] CBLAS_INT` stride length for `Y`. |
| 223 | +
|
| 224 | +```c |
| 225 | +void stdlib_strided_dnancusumkbn( const CBLAS_INT N, const double sum, const double *X, const CBLAS_INT strideX, double *Y, const CBLAS_INT strideY ); |
| 226 | +``` |
| 227 | + |
| 228 | +<!-- lint disable maximum-heading-length --> |
| 229 | + |
| 230 | +#### stdlib_strided_dnancusumkbn_ndarray( N, sum, \*X, strideX, offsetX, \*Y, strideY, offsetY ) |
| 231 | + |
| 232 | +<!-- lint enable maximum-heading-length --> |
| 233 | + |
| 234 | +Computes the cumulative sum of double-precision floating-point strided array elements ignoring `NaN` values and using an improved Kahan–Babuška algorithm and alternative indexing semantics. |
| 235 | + |
| 236 | +```c |
| 237 | +const double x[] = { 1.0, 2.0, 3.0, 4.0 } |
| 238 | +double y[] = { 0.0, 0.0, 0.0, 0.0 } |
| 239 | + |
| 240 | +stdlib_strided_dnancusumkbn_ndarray( 4, 0.0, x, 1, 0, y, 1, 0 ); |
| 241 | +``` |
| 242 | +
|
| 243 | +The function accepts the following arguments: |
| 244 | +
|
| 245 | +- **N**: `[in] CBLAS_INT` number of indexed elements. |
| 246 | +- **sum**: `[in] double` initial sum. |
| 247 | +- **X**: `[in] double*` input array. |
| 248 | +- **strideX**: `[in] CBLAS_INT` stride length for `X`. |
| 249 | +- **offsetX**: `[in] CBLAS_INT` starting index for `X`. |
| 250 | +- **Y**: `[out] double*` output array. |
| 251 | +- **strideY**: `[in] CBLAS_INT` stride length for `Y`. |
| 252 | +- **offsetY**: `[in] CBLAS_INT` starting index for `Y`. |
| 253 | +
|
| 254 | +```c |
| 255 | +void stdlib_strided_dnancusumkbn_ndarray( const CBLAS_INT N, const double sum, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY ); |
| 256 | +``` |
| 257 | + |
| 258 | +</section> |
| 259 | + |
| 260 | +<!-- /.usage --> |
| 261 | + |
| 262 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 263 | + |
| 264 | +<section class="notes"> |
| 265 | + |
| 266 | +</section> |
| 267 | + |
| 268 | +<!-- /.notes --> |
| 269 | + |
| 270 | +<!-- C API usage examples. --> |
| 271 | + |
| 272 | +<section class="examples"> |
| 273 | + |
| 274 | +### Examples |
| 275 | + |
| 276 | +```c |
| 277 | +#include "stdlib/blas/ext/base/dnancusumkbn.h" |
| 278 | +#include <stdio.h> |
| 279 | + |
| 280 | +int main( void ) { |
| 281 | + // Create strided arrays: |
| 282 | + const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 0.0/0.0, 0.0/0.0 }; |
| 283 | + double y[] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; |
| 284 | + |
| 285 | + // Specify the number of elements: |
| 286 | + const int N = 4; |
| 287 | + |
| 288 | + // Specify stride lengths: |
| 289 | + const int strideX = 2; |
| 290 | + const int strideY = -2; |
| 291 | + |
| 292 | + // Compute the cumulative sum: |
| 293 | + stdlib_strided_dnancusumkbn( N, 0.0, x, strideX, y, strideY ); |
| 294 | + |
| 295 | + // Print the result: |
| 296 | + for ( int i = 0; i < 8; i++ ) { |
| 297 | + printf( "y[ %d ] = %lf\n", i, y[ i ] ); |
| 298 | + } |
| 299 | +} |
| 300 | +``` |
| 301 | +
|
| 302 | +</section> |
| 303 | +
|
| 304 | +<!-- /.examples --> |
| 305 | +
|
| 306 | +</section> |
| 307 | +
|
| 308 | +<!-- /.c --> |
| 309 | +
|
| 310 | +<section class="references"> |
| 311 | +
|
| 312 | +## References |
| 313 | +
|
| 314 | +- Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106][@neumaier:1974a]. |
| 315 | +
|
| 316 | +</section> |
| 317 | +
|
| 318 | +<!-- /.references --> |
| 319 | +
|
| 320 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 321 | +
|
| 322 | +<section class="related"> |
| 323 | +
|
| 324 | +</section> |
| 325 | +
|
| 326 | +<!-- /.related --> |
| 327 | +
|
| 328 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 329 | +
|
| 330 | +<section class="links"> |
| 331 | +
|
| 332 | +[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64 |
| 333 | +
|
| 334 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 335 | +
|
| 336 | +[@neumaier:1974a]: https://doi.org/10.1002/zamm.19740540106 |
| 337 | +
|
| 338 | +</section> |
| 339 | +
|
| 340 | +<!-- /.links --> |
0 commit comments