nansumpw.js (4070B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2020 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var floor = require( '@stdlib/math/base/special/floor' ); 24 25 26 // VARIABLES // 27 28 // Blocksize for pairwise summation (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.): 29 var BLOCKSIZE = 128; 30 31 32 // MAIN // 33 34 /** 35 * Computes the sum of a strided array elements, ignoring `NaN` values and using pairwise summation. 36 * 37 * ## Method 38 * 39 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 40 * 41 * ## References 42 * 43 * - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050). 44 * 45 * @private 46 * @param {PositiveInteger} N - number of indexed elements 47 * @param {NumericArray} out - two-element output array whose first element is the accumulated sum and whose second element is the accumulated number of summed values 48 * @param {NumericArray} x - input array 49 * @param {integer} stride - stride length 50 * @param {NonNegativeInteger} offset - starting index 51 * @returns {NumericArray} output array 52 * 53 * @example 54 * var floor = require( '@stdlib/math/base/special/floor' ); 55 * 56 * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ]; 57 * var N = floor( x.length / 2 ); 58 * 59 * var out = [ 0.0, 0 ]; 60 * var v = nansumpw( N, out, x, 2, 1 ); 61 * // returns [ 5.0, 4 ] 62 */ 63 function nansumpw( N, out, x, stride, offset ) { 64 var ix; 65 var s0; 66 var s1; 67 var s2; 68 var s3; 69 var s4; 70 var s5; 71 var s6; 72 var s7; 73 var M; 74 var s; 75 var n; 76 var v; 77 var i; 78 79 ix = offset; 80 if ( N < 8 ) { 81 // Use simple summation... 82 s = 0.0; 83 n = 0; 84 for ( i = 0; i < N; i++ ) { 85 v = x[ ix ]; 86 if ( v === v ) { 87 s += v; 88 n += 1; 89 } 90 ix += stride; 91 } 92 out[ 0 ] += s; 93 out[ 1 ] += n; 94 return out; 95 } 96 if ( N <= BLOCKSIZE ) { 97 // Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)... 98 s0 = 0.0; 99 s1 = 0.0; 100 s2 = 0.0; 101 s3 = 0.0; 102 s4 = 0.0; 103 s5 = 0.0; 104 s6 = 0.0; 105 s7 = 0.0; 106 n = 0; 107 108 M = N % 8; 109 for ( i = 0; i < N-M; i += 8 ) { 110 v = x[ ix ]; 111 if ( v === v ) { 112 s0 += v; 113 n += 1; 114 } 115 ix += stride; 116 v = x[ ix ]; 117 if ( v === v ) { 118 s1 += v; 119 n += 1; 120 } 121 ix += stride; 122 v = x[ ix ]; 123 if ( v === v ) { 124 s2 += v; 125 n += 1; 126 } 127 ix += stride; 128 v = x[ ix ]; 129 if ( v === v ) { 130 s3 += v; 131 n += 1; 132 } 133 ix += stride; 134 v = x[ ix ]; 135 if ( v === v ) { 136 s4 += v; 137 n += 1; 138 } 139 ix += stride; 140 v = x[ ix ]; 141 if ( v === v ) { 142 s5 += v; 143 n += 1; 144 } 145 ix += stride; 146 v = x[ ix ]; 147 if ( v === v ) { 148 s6 += v; 149 n += 1; 150 } 151 ix += stride; 152 v = x[ ix ]; 153 if ( v === v ) { 154 s7 += v; 155 n += 1; 156 } 157 ix += stride; 158 } 159 // Pairwise sum the accumulators: 160 s = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7)); 161 162 // Clean-up loop... 163 for ( i; i < N; i++ ) { 164 v = x[ ix ]; 165 if ( v === v ) { 166 s += v; 167 n += 1; 168 } 169 ix += stride; 170 } 171 out[ 0 ] += s; 172 out[ 1 ] += n; 173 return out; 174 } 175 // Recurse by dividing by two, but avoiding non-multiples of unroll factor... 176 n = floor( N/2 ); 177 n -= n % 8; 178 return nansumpw( n, out, x, stride, ix ) + nansumpw( N-n, out, x, stride, ix+(n*stride) ); // eslint-disable-line max-len 179 } 180 181 182 // EXPORTS // 183 184 module.exports = nansumpw;