main.js (3212B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; 24 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 25 var Float64Array = require( '@stdlib/array/float64' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an accumulator function which incrementally computes a moving sum. 32 * 33 * @param {PositiveInteger} W - window size 34 * @throws {TypeError} must provide a positive integer 35 * @returns {Function} accumulator function 36 * 37 * @example 38 * var accumulator = incrmsum( 3 ); 39 * 40 * var sum = accumulator(); 41 * // returns null 42 * 43 * sum = accumulator( 2.0 ); 44 * // returns 2.0 45 * 46 * sum = accumulator( -5.0 ); 47 * // returns -3.0 48 * 49 * sum = accumulator( 3.0 ); 50 * // returns 0.0 51 * 52 * sum = accumulator( 5.0 ); 53 * // returns 3.0 54 * 55 * sum = accumulator(); 56 * // returns 3.0 57 */ 58 function incrmsum( W ) { 59 var buf; 60 var sum; 61 var N; 62 var i; 63 if ( !isPositiveInteger( W ) ) { 64 throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' ); 65 } 66 buf = new Float64Array( W ); 67 sum = 0.0; 68 i = -1; 69 N = 0; 70 71 return accumulator; 72 73 /** 74 * If provided a value, the accumulator function returns an updated sum. If not provided a value, the accumulator function returns the current sum. 75 * 76 * @private 77 * @param {number} [x] - input value 78 * @returns {(number|null)} sum or null 79 */ 80 function accumulator( x ) { 81 var k; 82 if ( arguments.length === 0 ) { 83 if ( N === 0 ) { 84 return null; 85 } 86 return sum; 87 } 88 // Update the index for managing the circular buffer: 89 i = (i+1) % W; 90 91 // Case: incoming value is NaN, the accumulated value is automatically NaN... 92 if ( isnan( x ) ) { 93 N = W; // explicitly set to avoid `N < W` branch 94 sum = NaN; 95 } 96 // Case: initial window... 97 else if ( N < W ) { 98 N += 1; 99 sum += x; 100 } 101 // Case: outgoing value is NaN, and, thus, we need to compute the accumulated value... 102 else if ( isnan( buf[ i ] ) ) { 103 N = 1; 104 sum = x; 105 for ( k = 0; k < W; k++ ) { 106 if ( k !== i ) { 107 if ( isnan( buf[ k ] ) ) { 108 N = W; // explicitly set to avoid `N < W` branch 109 sum = NaN; 110 break; // sum is automatically NaN, so no need to continue 111 } 112 N += 1; 113 sum += buf[ k ]; 114 } 115 } 116 } 117 // Case: neither the current accumulated value nor the incoming value are NaN, so we need to update the accumulated value... 118 else if ( isnan( sum ) === false ) { 119 sum += x - buf[ i ]; 120 } 121 // Case: the current accumulated value is NaN, so nothing to do until the buffer no longer contains NaN values... 122 123 buf[ i ] = x; 124 return sum; 125 } 126 } 127 128 129 // EXPORTS // 130 131 module.exports = incrmsum;