main.js (2099B)
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 abs = require( '@stdlib/math/base/special/abs' ); 25 var incrmmax = require( './../../../incr/mmax' ); 26 27 28 // MAIN // 29 30 /** 31 * Returns an accumulator function which incrementally computes a moving maximum absolute value. 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 = incrmmaxabs( 3 ); 39 * 40 * var m = accumulator(); 41 * // returns null 42 * 43 * m = accumulator( 2.0 ); 44 * // returns 2.0 45 * 46 * m = accumulator( -5.0 ); 47 * // returns 5.0 48 * 49 * m = accumulator( 3.0 ); 50 * // returns 5.0 51 * 52 * m = accumulator( 5.0 ); 53 * // returns 5.0 54 * 55 * m = accumulator(); 56 * // returns 5.0 57 */ 58 function incrmmaxabs( W ) { 59 var max; 60 if ( !isPositiveInteger( W ) ) { 61 throw new TypeError( 'invalid argument. Must provide a positive integer. Value: `' + W + '`.' ); 62 } 63 max = incrmmax( W ); 64 return accumulator; 65 66 /** 67 * If provided a value, the accumulator function returns an updated maximum absolute value. If not provided a value, the accumulator function returns the current maximum absolute value. 68 * 69 * @private 70 * @param {number} [x] - input value 71 * @returns {(number|null)} maximum absolute value or null 72 */ 73 function accumulator( x ) { 74 if ( arguments.length === 0 ) { 75 return max(); 76 } 77 return max( abs( x ) ); 78 } 79 } 80 81 82 // EXPORTS // 83 84 module.exports = incrmmaxabs;