time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (2681B)


      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 isArrayLike = require( '@stdlib/assert/is-array-like-object' );
     25 var incrmminmax = require( './../../../incr/mminmax' );
     26 var abs = require( '@stdlib/math/base/special/abs' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns an accumulator function which incrementally computes moving minimum and maximum absolute values.
     33 *
     34 * @param {Collection} [out] - output array
     35 * @param {PositiveInteger} window - window size
     36 * @throws {TypeError} output argument must be array-like
     37 * @throws {TypeError} window size must be a positive integer
     38 * @returns {Function} accumulator function
     39 *
     40 * @example
     41 * var accumulator = incrmminmaxabs( 3 );
     42 *
     43 * var mm = accumulator();
     44 * // returns null
     45 *
     46 * mm = accumulator( 2.0 );
     47 * // returns [ 2.0, 2.0 ]
     48 *
     49 * mm = accumulator( -5.0 );
     50 * // returns [ 2.0, 5.0 ]
     51 *
     52 * mm = accumulator( 3.0 );
     53 * // returns [ 2.0, 5.0 ]
     54 *
     55 * mm = accumulator( 5.0 );
     56 * // returns [ 3.0, 5.0 ]
     57 *
     58 * mm = accumulator();
     59 * // returns [ 3.0, 5.0 ]
     60 */
     61 function incrmminmaxabs( out, window ) {
     62 	var minmax;
     63 	var o;
     64 	var W;
     65 	if ( arguments.length === 1 ) {
     66 		o = [ 0.0, 0.0 ];
     67 		W = out;
     68 	} else {
     69 		if ( !isArrayLike( out ) ) {
     70 			throw new TypeError( 'invalid argument. Output argument must be an array-like object. Value: `' + out + '`.' );
     71 		}
     72 		o = out;
     73 		W = window;
     74 	}
     75 	if ( !isPositiveInteger( W ) ) {
     76 		throw new TypeError( 'invalid argument. Window size must be a positive integer. Value: `' + W + '`.' );
     77 	}
     78 	minmax = incrmminmax( o, W );
     79 	return accumulator;
     80 
     81 	/**
     82 	* If provided a value, the accumulator function returns updated minimum and maximum absolute values. If not provided a value, the accumulator function returns the current minimum and maximum absolute values.
     83 	*
     84 	* @private
     85 	* @param {number} [x] - input value
     86 	* @returns {(ArrayLikeObject|null)} output array or null
     87 	*/
     88 	function accumulator( x ) {
     89 		if ( arguments.length === 0 ) {
     90 			return minmax();
     91 		}
     92 		return minmax( abs( x ) );
     93 	}
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = incrmminmaxabs;