README.md (5691B)
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 # dnannsumkbn2 22 23 > Calculate the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative 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 dnannsumkbn2 = require( '@stdlib/blas/ext/base/dnannsumkbn2' ); 37 ``` 38 39 #### dnannsumkbn2( N, x, strideX, out, strideOut ) 40 41 Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative 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, 2.0 ] ); 47 var out = new Float64Array( 2 ); 48 49 var v = dnannsumkbn2( x.length, x, 1, out, 1 ); 50 // returns <Float64Array>[ 1.0, 3 ] 51 ``` 52 53 The function has the following parameters: 54 55 - **N**: number of indexed elements. 56 - **x**: input [`Float64Array`][@stdlib/array/float64]. 57 - **strideX**: index increment for `x`. 58 - **out**: output [`Float64Array`][@stdlib/array/float64] whose first element is the sum and whose second element is the number of non-NaN elements. 59 - **strideOut**: index increment for `out`. 60 61 The `N` and `stride` parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element in `x`, 62 63 ```javascript 64 var Float64Array = require( '@stdlib/array/float64' ); 65 var floor = require( '@stdlib/math/base/special/floor' ); 66 67 var x = new Float64Array( [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ] ); 68 var out = new Float64Array( 2 ); 69 var N = floor( x.length / 2 ); 70 71 var v = dnannsumkbn2( N, x, 2, out, 1 ); 72 // returns <Float64Array>[ 5.0, 2 ] 73 ``` 74 75 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 76 77 <!-- eslint-disable stdlib/capitalized-comments --> 78 79 ```javascript 80 var Float64Array = require( '@stdlib/array/float64' ); 81 var floor = require( '@stdlib/math/base/special/floor' ); 82 83 var x0 = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 84 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 85 86 var out0 = new Float64Array( 4 ); 87 var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element 88 89 var N = floor( x0.length / 2 ); 90 91 var v = dnannsumkbn2( N, x1, 2, out1, 1 ); 92 // returns <Float64Array>[ 5.0, 4 ] 93 ``` 94 95 #### dnannsumkbn2.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut ) 96 97 Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics. 98 99 ```javascript 100 var Float64Array = require( '@stdlib/array/float64' ); 101 102 var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] ); 103 var out = new Float64Array( 2 ); 104 105 var v = dnannsumkbn2.ndarray( x.length, x, 1, 0, out, 1, 0 ); 106 // returns <Float64Array>[ 1.0, 3 ] 107 ``` 108 109 The function has the following additional parameters: 110 111 - **offsetX**: starting index for `x`. 112 - **offsetOut**: starting index for `out`. 113 114 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 value in `x` starting from the second value 115 116 ```javascript 117 var Float64Array = require( '@stdlib/array/float64' ); 118 var floor = require( '@stdlib/math/base/special/floor' ); 119 120 var x = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 121 var out = new Float64Array( 4 ); 122 var N = floor( x.length / 2 ); 123 124 var v = dnannsumkbn2.ndarray( N, x, 2, 1, out, 2, 1 ); 125 // returns <Float64Array>[ 0.0, 5.0, 0.0, 4 ] 126 ``` 127 128 </section> 129 130 <!-- /.usage --> 131 132 <section class="notes"> 133 134 ## Notes 135 136 - If `N <= 0`, both functions return a sum equal to `0.0`. 137 138 </section> 139 140 <!-- /.notes --> 141 142 <section class="examples"> 143 144 ## Examples 145 146 <!-- eslint no-undef: "error" --> 147 148 ```javascript 149 var randu = require( '@stdlib/random/base/randu' ); 150 var round = require( '@stdlib/math/base/special/round' ); 151 var Float64Array = require( '@stdlib/array/float64' ); 152 var dnannsumkbn2 = require( '@stdlib/blas/ext/base/dnannsumkbn2' ); 153 154 var x; 155 var i; 156 157 x = new Float64Array( 10 ); 158 for ( i = 0; i < x.length; i++ ) { 159 if ( randu() < 0.2 ) { 160 x[ i ] = NaN; 161 } else { 162 x[ i ] = round( randu()*100.0 ); 163 } 164 } 165 console.log( x ); 166 167 var out = new Float64Array( 2 ); 168 dnannsumkbn2( x.length, x, 1, out, 1 ); 169 console.log( out ); 170 ``` 171 172 </section> 173 174 <!-- /.examples --> 175 176 * * * 177 178 <section class="references"> 179 180 ## References 181 182 - Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x][@klein:2005a]. 183 184 </section> 185 186 <!-- /.references --> 187 188 <section class="links"> 189 190 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64 191 192 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 193 194 [@klein:2005a]: https://doi.org/10.1007/s00607-005-0139-x 195 196 </section> 197 198 <!-- /.links -->