|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2020 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 | +# snancusumkbn |
| 22 | + |
| 23 | +> Calculate the cumulative sum of single-precision floating-point strided array elements using an improved Kahan–Babuška algorithm, ignoring `NaN` values. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var snancusumkbn = require( '@stdlib/blas/ext/base/snancusumkbn' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### snancusumkbn( N, sum, x, strideX, y, strideY ) |
| 40 | + |
| 41 | +Computes the cumulative sum of single-precision floating-point strided array elements using an improved Kahan–Babuška algorithm, ignoring `NaN` values. |
| 42 | + |
| 43 | +```javascript |
| 44 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 45 | + |
| 46 | +var x = new Float32Array( [ 1.0, NaN, 2.0 ] ); |
| 47 | +var y = new Float32Array( x.length ); |
| 48 | + |
| 49 | +snancusumkbn( x.length, 0.0, x, 1, y, 1 ); |
| 50 | +// y => <Float32Array>[ 1.0, 1.0, 3.0 ] |
| 51 | + |
| 52 | +x = new Float32Array( [ 1.0, -2.0, NaN ] ); |
| 53 | +y = new Float32Array( x.length ); |
| 54 | + |
| 55 | +snancusumkbn( x.length, 10.0, x, 1, y, 1 ); |
| 56 | +// y => <Float32Array>[ 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 [`Float32Array`][@stdlib/array/float32]. |
| 64 | +- **strideX**: stride length for `x`. |
| 65 | +- **y**: output [`Float32Array`][@stdlib/array/float32]. |
| 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 Float32Array = require( '@stdlib/array/float32' ); |
| 72 | +var x = new Float32Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] ); |
| 73 | +var y = new Float32Array( x.length ); |
| 74 | + |
| 75 | +var v = snancusumkbn( 4, 0.0, x, 2, y, 1 ); |
| 76 | +// y => <Float32Array>[ 1.0, 1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ] |
| 77 | +``` |
| 78 | + |
| 79 | +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. |
| 80 | + |
| 81 | +<!-- eslint-disable stdlib/capitalized-comments --> |
| 82 | + |
| 83 | +```javascript |
| 84 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 85 | + |
| 86 | +// Initial arrays... |
| 87 | +var x0 = new Float32Array( [ 2.0, 1.0, 2.0, NaN, -2.0, NaN, 3.0, 4.0 ] ); |
| 88 | +var y0 = new Float32Array( x0.length ); |
| 89 | + |
| 90 | +// Create offset views... |
| 91 | +var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element |
| 92 | +var y1 = new Float32Array( y0.buffer, y0.BYTES_PER_ELEMENT*3 ); // start at 4th element |
| 93 | + |
| 94 | +snancusumkbn( 4, 0.0, x1, -2, y1, 1 ); |
| 95 | +// y0 => <Float32Array>[ 0.0, 0.0, 0.0, 4.0, 4.0, 4.0, 5.0, 0.0 ] |
| 96 | +``` |
| 97 | + |
| 98 | +#### snancusumkbn.ndarray( N, sum, x, strideX, offsetX, y, strideY, offsetY ) |
| 99 | + |
| 100 | +Computes the cumulative sum of single-precision floating-point strided array elements using an improved Kahan–Babuška algorithm, ignoring `NaN` values and alternative indexing semantics. |
| 101 | + |
| 102 | +```javascript |
| 103 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 104 | + |
| 105 | +var x = new Float32Array( [ 1.0, NaN, 2.0 ] ); |
| 106 | +var y = new Float32Array( 3 ); |
| 107 | + |
| 108 | +snancusumkbn.ndarray( 3, 0.0, x, 1, 0, y, 1, 0 ); |
| 109 | +// y => <Float32Array>[ 1.0, 1.0, 3.0 ] |
| 110 | +``` |
| 111 | + |
| 112 | +The function has the following additional parameters: |
| 113 | + |
| 114 | +- **offsetX**: starting index for `x`. |
| 115 | +- **offsetY**: starting index for `y`. |
| 116 | + |
| 117 | +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 starting from the second element and to store in the last `N` elements of `y` starting from the last element: |
| 118 | + |
| 119 | +```javascript |
| 120 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 121 | + |
| 122 | +var x = new Float32Array( [ 2.0, 1.0, NaN, -2.0, NaN, 2.0, 3.0, 4.0 ] ); |
| 123 | +var y = new Float32Array( x.length ); |
| 124 | + |
| 125 | +snancusumkbn.ndarray( 4, 0.0, x, 2, 1, y, -1, y.length-1 ); |
| 126 | +// y => <Float32Array>[ 0.0, 0.0, 0.0, 0.0, 5.0, 1.0, -1.0, 1.0 ] |
| 127 | +``` |
| 128 | + |
| 129 | +</section> |
| 130 | + |
| 131 | +<!-- /.usage --> |
| 132 | + |
| 133 | +<section class="notes"> |
| 134 | + |
| 135 | +## Notes |
| 136 | + |
| 137 | +- If `N <= 0`, both functions return `y` unchanged. |
| 138 | + |
| 139 | +</section> |
| 140 | + |
| 141 | +<!-- /.notes --> |
| 142 | + |
| 143 | +<section class="examples"> |
| 144 | + |
| 145 | +## Examples |
| 146 | + |
| 147 | +<!-- eslint no-undef: "error" --> |
| 148 | + |
| 149 | +```javascript |
| 150 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); |
| 151 | +var bernoulli = require( '@stdlib/random/base/bernoulli' ); |
| 152 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 153 | +var snancusumkbn = require( '@stdlib/blas/ext/base/snancusumkbn' ); |
| 154 | + |
| 155 | +function rand() { |
| 156 | + if ( bernoulli( 0.7 ) > 0 ) { |
| 157 | + return discreteUniform( 0, 100 ); |
| 158 | + } |
| 159 | + return NaN; |
| 160 | +} |
| 161 | + |
| 162 | +function constant() { |
| 163 | + return 0.0; |
| 164 | +} |
| 165 | + |
| 166 | +var x = filledarrayBy( 10, 'float32', rand ); |
| 167 | +console.log( x ); |
| 168 | + |
| 169 | +var y = filledarrayBy( 10, 'float32', constant ); |
| 170 | +console.log( y ); |
| 171 | + |
| 172 | +snancusumkbn( x.length, 0.0, x, 1, y, -1 ); |
| 173 | +console.log( y ); |
| 174 | +``` |
| 175 | + |
| 176 | +</section> |
| 177 | + |
| 178 | +<!-- /.examples --> |
| 179 | + |
| 180 | +<section class="references"> |
| 181 | + |
| 182 | +</section> |
| 183 | + |
| 184 | +<!-- /.references --> |
| 185 | + |
| 186 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 187 | + |
| 188 | +<section class="related"> |
| 189 | + |
| 190 | +* * * |
| 191 | + |
| 192 | +## See Also |
| 193 | + |
| 194 | +- <span class="package-name">[`@stdlib/blas/ext/base/dcusumkbn`][@stdlib/blas/ext/base/dcusumkbn]</span><span class="delimiter">: </span><span class="description">calculate the cumulative sum of double-precision floating-point strided array elements using an improved Kahan–Babuška algorithm.</span> |
| 195 | +- <span class="package-name">[`@stdlib/blas/ext/base/gcusumkbn`][@stdlib/blas/ext/base/gcusumkbn]</span><span class="delimiter">: </span><span class="description">calculate the cumulative sum of strided array elements using an improved Kahan–Babuška algorithm.</span> |
| 196 | +- <span class="package-name">[`@stdlib/blas/ext/base/scusum`][@stdlib/blas/ext/base/scusum]</span><span class="delimiter">: </span><span class="description">calculate the cumulative sum of single-precision floating-point strided array elements.</span> |
| 197 | + |
| 198 | +</section> |
| 199 | + |
| 200 | +<!-- /.related --> |
| 201 | + |
| 202 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 203 | + |
| 204 | +<section class="links"> |
| 205 | + |
| 206 | +[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32 |
| 207 | + |
| 208 | +[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray |
| 209 | + |
| 210 | +<!-- <related-links> --> |
| 211 | + |
| 212 | +[@stdlib/blas/ext/base/dcusumkbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dcusumkbn |
| 213 | + |
| 214 | +[@stdlib/blas/ext/base/gcusumkbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/gcusumkbn |
| 215 | + |
| 216 | +[@stdlib/blas/ext/base/scusum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/scusum |
| 217 | + |
| 218 | +<!-- </related-links> --> |
| 219 | + |
| 220 | +</section> |
| 221 | + |
| 222 | +<!-- /.links --> |
0 commit comments