You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Licensed under the Apache License, Version 2.0 (the "License");
272
+
* you may not use this file except in compliance with the License.
273
+
* You may obtain a copy of the License at
274
+
*
275
+
* http://www.apache.org/licenses/LICENSE-2.0
276
+
*
277
+
* Unless required by applicable law or agreed to in writing, software
278
+
* distributed under the License is distributed on an "AS IS" BASIS,
279
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
280
+
* See the License for the specific language governing permissions and
281
+
* limitations under the License.
282
+
*/
283
+
284
+
'use strict';
285
+
286
+
// MODULES //
287
+
288
+
var abs = require( '@stdlib/math/base/special/abs' );
289
+
290
+
291
+
// MAIN //
292
+
293
+
/**
294
+
* Computes the sum of strided array elements using an improved Kahan–Babuška algorithm.
295
+
*
296
+
* ## Method
297
+
*
298
+
* - This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974).
299
+
*
300
+
* ## References
301
+
*
302
+
* - Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106).
303
+
*
304
+
* @private
305
+
* @param {PositiveInteger} N - number of indexed elements
306
+
* @param {Object} x - input array object
307
+
* @param {Collection} x.data - input array data
308
+
* @param {Array<Function>} x.accessors - array element accessors
309
+
* @param {integer} strideX - stride length
310
+
* @param {NonNegativeInteger} offsetX - starting index
311
+
* @returns {number} sum
312
+
*
313
+
* @example
314
+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
315
+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
316
+
*
317
+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
318
+
*
319
+
* var v = gsumkbn( 4, arraylike2object( x ), 2, 1 );
320
+
* // returns 5.0
321
+
*/
322
+
function gsumkbn( N, x, strideX, offsetX ) {
323
+
var xbuf;
324
+
var get;
325
+
var sum;
326
+
var ix;
327
+
var v;
328
+
var t;
329
+
var c;
330
+
var i;
331
+
332
+
if ( N <= 0 ) <spanclass="branch-0 cbranch-no" title="branch not covered" >{</span>
333
+
<spanclass="cstat-no" title="statement not covered" > return 0.0;</span>
334
+
<spanclass="cstat-no" title="statement not covered" > }</span>
335
+
336
+
// Cache reference to array data:
337
+
xbuf = x.data;
338
+
339
+
// Cache a reference to the element accessor:
340
+
get = x.accessors[ 0 ];
341
+
342
+
ix = offsetX;
343
+
if ( strideX === 0 ) <spanclass="branch-0 cbranch-no" title="branch not covered" >{</span>
344
+
<spanclass="cstat-no" title="statement not covered" > return N * get( xbuf, ix );</span>
345
+
<spanclass="cstat-no" title="statement not covered" > }</span>
346
+
sum = 0.0;
347
+
c = 0.0;
348
+
for ( i = 0; i < N; i++ ) {
349
+
v = get( xbuf, ix );
350
+
t = sum + v;
351
+
if ( abs( sum ) >= abs( v ) ) {
352
+
c += (sum-t) + v;
353
+
} else {
354
+
c += (v-t) + sum;
355
+
}
356
+
sum = t;
357
+
ix += strideX;
358
+
}
359
+
return sum + c;
360
+
}
361
+
362
+
363
+
// EXPORTS //
364
+
365
+
module.exports = gsumkbn;
366
+
</pre></td></tr></table></pre>
367
+
368
+
<divclass='push'></div><!-- for sticky footer -->
369
+
</div><!-- /wrapper -->
370
+
<divclass='footer quiet pad2 space-top1 center small'>
0 commit comments