ndarray.js (2852B)
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 float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' ); 24 var floor = require( '@stdlib/math/base/special/floor' ); 25 26 27 // VARIABLES // 28 29 // Blocksize for pairwise summation: 30 var BLOCKSIZE = 128; 31 32 33 // MAIN // 34 35 /** 36 * Computes the cumulative sum of single-precision floating-point strided array elements using pairwise summation. 37 * 38 * ## Method 39 * 40 * - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`. 41 * 42 * ## References 43 * 44 * - 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). 45 * 46 * @param {PositiveInteger} N - number of indexed elements 47 * @param {number} sum - initial sum 48 * @param {Float32Array} x - input array 49 * @param {integer} strideX - `x` stride length 50 * @param {NonNegativeInteger} offsetX - starting index for `x` 51 * @param {Float32Array} y - output array 52 * @param {integer} strideY - `y` stride length 53 * @param {NonNegativeInteger} offsetY - starting index for `y` 54 * @returns {Float32Array} output array 55 * 56 * @example 57 * var Float32Array = require( '@stdlib/array/float32' ); 58 * var floor = require( '@stdlib/math/base/special/floor' ); 59 * 60 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); 61 * var y = new Float32Array( x.length ); 62 * var N = floor( x.length / 2 ); 63 * 64 * var v = scusumpw( N, 0.0, x, 2, 1, y, 1, 0 ); 65 * // returns <Float32Array>[ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ] 66 */ 67 function scusumpw( N, sum, x, strideX, offsetX, y, strideY, offsetY ) { 68 var ix; 69 var iy; 70 var s; 71 var n; 72 var i; 73 74 if ( N <= 0 ) { 75 return y; 76 } 77 ix = offsetX; 78 iy = offsetY; 79 if ( N <= BLOCKSIZE ) { 80 s = 0.0; 81 for ( i = 0; i < N; i++ ) { 82 s = float64ToFloat32( s + x[ ix ] ); 83 y[ iy ] = float64ToFloat32( sum + s ); 84 ix += strideX; 85 iy += strideY; 86 } 87 return y; 88 } 89 n = floor( N/2 ); 90 scusumpw( n, sum, x, strideX, ix, y, strideY, iy ); 91 iy += (n-1) * strideY; 92 scusumpw( N-n, y[ iy ], x, strideX, ix+(n*strideX), y, strideY, iy+strideY ); // eslint-disable-line max-len 93 return y; 94 } 95 96 97 // EXPORTS // 98 99 module.exports = scusumpw;