main.js (4099B)
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 isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); 26 var PINF = require( '@stdlib/constants/float64/pinf' ); 27 var Float64Array = require( '@stdlib/array/float64' ); 28 29 30 // MAIN // 31 32 /** 33 * Returns an accumulator function which incrementally computes a moving minimum value. 34 * 35 * @param {PositiveInteger} W - window size 36 * @throws {TypeError} must provide a positive integer 37 * @returns {Function} accumulator function 38 * 39 * @example 40 * var accumulator = incrmmin( 3 ); 41 * 42 * var m = accumulator(); 43 * // returns null 44 * 45 * m = accumulator( 2.0 ); 46 * // returns 2.0 47 * 48 * m = accumulator( -5.0 ); 49 * // returns -5.0 50 * 51 * m = accumulator( 3.0 ); 52 * // returns -5.0 53 * 54 * m = accumulator( 5.0 ); 55 * // returns -5.0 56 * 57 * m = accumulator(); 58 * // returns -5.0 59 */ 60 function incrmmin( W ) { 61 var buf; 62 var min; 63 var N; 64 var i; 65 if ( !isPositiveInteger( W ) ) { 66 throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' ); 67 } 68 buf = new Float64Array( W ); 69 min = PINF; 70 i = -1; 71 N = 0; 72 73 return accumulator; 74 75 /** 76 * If provided a value, the accumulator function returns an updated minimum. If not provided a value, the accumulator function returns the current minimum. 77 * 78 * @private 79 * @param {number} [x] - input value 80 * @returns {(number|null)} minimum value or null 81 */ 82 function accumulator( x ) { 83 var v; 84 var k; 85 if ( arguments.length === 0 ) { 86 if ( N === 0 ) { 87 return null; 88 } 89 return min; 90 } 91 // Update the index for managing the circular buffer: 92 i = (i+1) % W; 93 94 // Case: update initial window... 95 if ( N < W ) { 96 N += 1; 97 if ( 98 isnan( x ) || 99 x < min || 100 ( x === min && isNegativeZero( x ) ) 101 ) { 102 min = x; 103 } 104 } 105 // Case: incoming value is NaN or less than current minimum value... 106 else if ( isnan( x ) || x < min ) { 107 min = x; 108 } 109 // Case: outgoing value is the current minimum and the new value is greater than the minimum, and, thus, we need to find a new minimum among the current values... 110 else if ( ( buf[ i ] === min && x > min ) || isnan( buf[ i ] ) ) { 111 min = x; 112 for ( k = 0; k < W; k++ ) { 113 if ( k !== i ) { 114 v = buf[ k ]; 115 if ( isnan( v ) ) { 116 min = v; 117 break; // no need to continue searching 118 } 119 if ( v < min || ( v === min && isNegativeZero( v ) ) ) { 120 min = v; 121 } 122 } 123 } 124 } 125 // Case: outgoing value is the current minimum, which is zero, and the new value is also zero, and, thus, we need to correctly handle signed zeros... 126 else if ( buf[ i ] === min && x === min && x === 0.0 ) { 127 if ( isNegativeZero( x ) ) { 128 min = x; 129 } else if ( isNegativeZero( buf[ i ] ) ) { 130 // Because the outgoing and incoming are different signs (-,+), we need to search the buffer to see if it contains a negative zero. If so, the minimum value remains negative zero; otherwise, the minimum value is incoming value... 131 min = x; 132 for ( k = 0; k < W; k++ ) { 133 if ( k !== i && isNegativeZero( buf[ k ] ) ) { 134 min = buf[ k ]; 135 break; 136 } 137 } 138 } 139 // Case: the outgoing and incoming values are both positive zero, so nothing changes 140 } 141 // Case: updating existing window; however, the minimum value does not change so nothing to do but update our buffer... 142 143 buf[ i ] = x; 144 return min; 145 } 146 } 147 148 149 // EXPORTS // 150 151 module.exports = incrmmin;