README.md (5468B)
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 # gnansumpw 22 23 > Calculate the sum of 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 gnansumpw = require( '@stdlib/blas/ext/base/gnansumpw' ); 37 ``` 38 39 #### gnansumpw( N, x, stride ) 40 41 Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation. 42 43 ```javascript 44 var x = [ 1.0, -2.0, NaN, 2.0 ]; 45 var N = x.length; 46 47 var v = gnansumpw( N, x, 1 ); 48 // returns 1.0 49 ``` 50 51 The function has the following parameters: 52 53 - **N**: number of indexed elements. 54 - **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. 55 - **stride**: index increment for `x`. 56 57 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the sum of every other element in `x`, 58 59 ```javascript 60 var floor = require( '@stdlib/math/base/special/floor' ); 61 62 var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ]; 63 var N = floor( x.length / 2 ); 64 65 var v = gnansumpw( N, x, 2 ); 66 // returns 5.0 67 ``` 68 69 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 70 71 <!-- eslint-disable stdlib/capitalized-comments --> 72 73 ```javascript 74 var Float64Array = require( '@stdlib/array/float64' ); 75 var floor = require( '@stdlib/math/base/special/floor' ); 76 77 var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 78 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 79 80 var N = floor( x0.length / 2 ); 81 82 var v = gnansumpw( N, x1, 2 ); 83 // returns 5.0 84 ``` 85 86 #### gnansumpw.ndarray( N, x, stride, offset ) 87 88 Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics. 89 90 ```javascript 91 var x = [ 1.0, -2.0, NaN, 2.0 ]; 92 var N = x.length; 93 94 var v = gnansumpw.ndarray( N, x, 1, 0 ); 95 // returns 1.0 96 ``` 97 98 The function has the following additional parameters: 99 100 - **offset**: starting index for `x`. 101 102 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 103 104 ```javascript 105 var floor = require( '@stdlib/math/base/special/floor' ); 106 107 var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ]; 108 var N = floor( x.length / 2 ); 109 110 var v = gnansumpw.ndarray( N, x, 2, 1 ); 111 // returns 5.0 112 ``` 113 114 </section> 115 116 <!-- /.usage --> 117 118 <section class="notes"> 119 120 ## Notes 121 122 - If `N <= 0`, both functions return `0.0`. 123 - 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. 124 - Depending on the environment, the typed versions ([`dnansumpw`][@stdlib/blas/ext/base/dnansumpw], [`snansumpw`][@stdlib/blas/ext/base/snansumpw], etc.) are likely to be significantly more performant. 125 126 </section> 127 128 <!-- /.notes --> 129 130 <section class="examples"> 131 132 ## Examples 133 134 <!-- eslint no-undef: "error" --> 135 136 ```javascript 137 var randu = require( '@stdlib/random/base/randu' ); 138 var round = require( '@stdlib/math/base/special/round' ); 139 var Float64Array = require( '@stdlib/array/float64' ); 140 var gnansumpw = require( '@stdlib/blas/ext/base/gnansumpw' ); 141 142 var x; 143 var i; 144 145 x = new Float64Array( 10 ); 146 for ( i = 0; i < x.length; i++ ) { 147 if ( randu() < 0.2 ) { 148 x[ i ] = NaN; 149 } else { 150 x[ i ] = round( randu()*100.0 ); 151 } 152 } 153 console.log( x ); 154 155 var v = gnansumpw( x.length, x, 1 ); 156 console.log( v ); 157 ``` 158 159 </section> 160 161 <!-- /.examples --> 162 163 * * * 164 165 <section class="references"> 166 167 ## References 168 169 - 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]. 170 171 </section> 172 173 <!-- /.references --> 174 175 <section class="links"> 176 177 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 178 179 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 180 181 [@stdlib/blas/ext/base/dnansumpw]: https://www.npmjs.com/package/@stdlib/blas/tree/main/ext/base/dnansumpw 182 183 [@stdlib/blas/ext/base/snansumpw]: https://www.npmjs.com/package/@stdlib/blas/tree/main/ext/base/snansumpw 184 185 [@higham:1993a]: https://doi.org/10.1137/0914050 186 187 </section> 188 189 <!-- /.links -->