README.md (6132B)
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 # dnannsumpw 22 23 > Calculate the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var dnannsumpw = require( '@stdlib/blas/ext/base/dnannsumpw' ); 37 ``` 38 39 #### dnannsumpw( N, x, strideX, out, strideOut ) 40 41 Computes the sum of double-precision floating-point strided array elements, ignoring `NaN` values and using pairwise summation. 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 = dnannsumpw( 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 = dnannsumpw( 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 = dnannsumpw( N, x1, 2, out1, 1 ); 92 // returns <Float64Array>[ 5.0, 4 ] 93 ``` 94 95 #### dnannsumpw.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 pairwise summation 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 = dnannsumpw.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 = dnannsumpw.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 - In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques. 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 randu = require( '@stdlib/random/base/randu' ); 151 var round = require( '@stdlib/math/base/special/round' ); 152 var Float64Array = require( '@stdlib/array/float64' ); 153 var dnannsumpw = require( '@stdlib/blas/ext/base/dnannsumpw' ); 154 155 var x; 156 var i; 157 158 x = new Float64Array( 10 ); 159 for ( i = 0; i < x.length; i++ ) { 160 if ( randu() < 0.2 ) { 161 x[ i ] = NaN; 162 } else { 163 x[ i ] = round( randu()*100.0 ); 164 } 165 } 166 console.log( x ); 167 168 var out = new Float64Array( 2 ); 169 dnannsumpw( x.length, x, 1, out, 1 ); 170 console.log( out ); 171 ``` 172 173 </section> 174 175 <!-- /.examples --> 176 177 * * * 178 179 <section class="references"> 180 181 ## References 182 183 - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050][@higham:1993a]. 184 185 </section> 186 187 <!-- /.references --> 188 189 <section class="links"> 190 191 [@stdlib/array/float64]: https://www.npmjs.com/package/@stdlib/array-float64 192 193 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 194 195 [@higham:1993a]: https://doi.org/10.1137/0914050 196 197 </section> 198 199 <!-- /.links -->