All files / mprod/lib main.js

45.64% Statements 89/195
100% Branches 1/1
0% Functions 0/2
45.64% Lines 89/195

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 1961x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive;
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var isInfinite = require( '@stdlib/math/base/assert/is-infinite' );
var frexp = require( '@stdlib/math/base/special/frexp' ).assign;
var ldexp = require( '@stdlib/math/base/special/ldexp' );
var Float64Array = require( '@stdlib/array/float64' );
var format = require( '@stdlib/string/format' );
 
 
// FUNCTIONS //
 
/**
* Computes an updated product.
*
* @private
* @param {Array} workspace - workspace array
* @param {Object} acc - accumulated fractional and exponent parts
* @param {number} x - multiplicative factor
* @returns {number} product
*/
function product( workspace, acc, x ) {
	// Split the incoming value into a normalized fraction and exponent:
	frexp( x, workspace, 1, 0 );

	// Update the accumulated fraction:
	acc.frac *= workspace[ 0 ];

	// Update the accumulated exponent:
	acc.exp += workspace[ 1 ];

	// Ensure fraction remains normalized to avoid overflow/underflow...
	if ( acc.frac > -0.5 && acc.frac < 0.5 ) {
		frexp( acc.frac, workspace, 1, 0 );
		acc.frac = workspace[ 0 ];
		acc.exp += workspace[ 1 ];
	}
	return ldexp( acc.frac, acc.exp );
}
 
 
// MAIN //
 
/**
* Returns an accumulator function which incrementally computes a moving product.
*
* ## Method
*
* To avoid overflow/underflow, we store the fractional and exponent parts of intermediate results separately. By keeping a normalized fraction, we prevent underflow/overflow of the fraction. Underflow of the exponent is impossible, as IEEE 754 floating-point exponents are integer values. Overflow of the exponent is possible, but highly unlikely. In the worst case, an intermediate exponent is greater than the minimum safe integer, and adding the exponent of an incoming value does not change the intermediate result. While incorrect, such behavior does not lead to exponent overflow.
*
* While intermediate results are largely immune to overflow and not subject to underflow, this does not mean that returned results will never be zero or infinite. In fact, zero (underflow) and infinite (overflow) results may be transient (i.e., infinity followed by a finite number).
*
* ## References
*
* -   Ueberhuber, Christoph W. 1997. _Numerical Computation 1: Methods, Software, and Analysis_. Springer-Verlag Berlin Heidelberg. doi:[10.1007/978-3-642-59118-1](https://doi.org/10.1007/978-3-642-59118-1).
*
* @param {PositiveInteger} W - window size
* @throws {TypeError} must provide a positive integer
* @returns {Function} accumulator function
*
* @example
* var accumulator = incrmprod( 3 );
*
* var p = accumulator();
* // returns null
*
* p = accumulator( 2.0 );
* // returns 2.0
*
* p = accumulator( -5.0 );
* // returns -10.0
*
* p = accumulator( 3.0 );
* // returns -30.0
*
* p = accumulator( 5.0 );
* // returns -75.0
*
* p = accumulator();
* // returns -75.0
*/
function incrmprod( W ) {
	var parts;
	var prod;
	var buf;
	var acc;
	var N;
	var i;
	if ( !isPositiveInteger( W ) ) {
		throw new TypeError( format( 'invalid argument. Must provide a positive integer. Value: `%s`.', W ) );
	}
	buf = new Float64Array( W );
	i = -1;
	N = 0;

	// Initialize a workspace for `frexp`:
	parts = [ 0.0, 0 ];

	// Initial product is 1.0, which may be split into its fractional and exponent parts (0.5 x 2.0**1 = 1.0):
	prod = 1.0;
	acc = {};
	acc.frac = 0.5;
	acc.exp = 1.0;

	return accumulator;

	/**
	* If provided a value, the accumulator function returns an updated product. If not provided a value, the accumulator function returns the current product.
	*
	* @private
	* @param {number} [x] - input value
	* @returns {(number|null)} product or null
	*/
	function accumulator( x ) {
		var k;
		var v;
		if ( arguments.length === 0 ) {
			if ( N === 0 ) {
				return null;
			}
			return prod;
		}
		// Update the index for managing the circular buffer:
		i = (i+1) % W;

		// Case: incoming value is NaN, the accumulated value is automatically NaN...
		if ( isnan( x ) ) {
			N = W; // explicitly set to avoid `N < W` branch
			prod = NaN;
		}
		// Case: initial window...
		else if ( N < W ) {
			N += 1;
			prod = product( parts, acc, x );
		}
		// Case: outgoing value is a "special" value, and, thus, we need to compute the accumulated value...
		else if (
			buf[ i ] === 0.0 ||
			isnan( buf[ i ] ) ||
			isInfinite( buf[ i ] )
		) {
			N = 1;
			acc.frac = 0.5;
			acc.exp = 1.0;
			product( parts, acc, x );
			for ( k = 0; k < W; k++ ) {
				if ( k !== i ) {
					v = buf[ k ];
					if ( isnan( v ) ) {
						N = W; // explicitly set to avoid `N < W` branch
						prod = NaN;
						break; // product is automatically NaN, so no need to continue
					}
					N += 1;
					prod = product( parts, acc, v );
				}
			}
		}
		// Case: neither the current accumulated value nor the incoming value are NaN, so we need to update the accumulated value...
		else if ( isnan( prod ) === false ) {
			v = x / buf[ i ];
			prod = product( parts, acc, v );
		}
		// Case: the current accumulated value is NaN, so nothing to do until the buffer no longer contains NaN values...
		buf[ i ] = x;

		return prod;
	}
}
 
 
// EXPORTS //
 
module.exports = incrmprod;