time-to-botec

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

main.js (2767B)


      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 isArrayLike = require( '@stdlib/assert/is-array-like-object' );
     24 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     25 var PINF = require( '@stdlib/constants/float64/pinf' );
     26 var abs = require( '@stdlib/math/base/special/abs' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns an accumulator function which incrementally computes minimum and maximum absolute values.
     33 *
     34 * @param {Collection} [out] - output array
     35 * @throws {TypeError} output argument must be array-like
     36 * @returns {Function} accumulator function
     37 *
     38 * @example
     39 * var accumulator = incrminmaxabs();
     40 *
     41 * var mm = accumulator();
     42 * // returns null
     43 *
     44 * mm = accumulator( 2.0 );
     45 * // returns [ 2.0, 2.0 ]
     46 *
     47 * mm = accumulator( -5.0 );
     48 * // returns [ 2.0, 5.0 ]
     49 *
     50 * mm = accumulator( 3.0 );
     51 * // returns [ 2.0, 5.0 ]
     52 *
     53 * mm = accumulator( 5.0 );
     54 * // returns [ 2.0, 5.0 ]
     55 *
     56 * mm = accumulator();
     57 * // returns [ 2.0, 5.0 ]
     58 */
     59 function incrminmaxabs( out ) {
     60 	var minmax;
     61 	var min;
     62 	var max;
     63 	var FLG;
     64 	if ( arguments.length === 0 ) {
     65 		minmax = [ 0.0, 0.0 ];
     66 	} else {
     67 		if ( !isArrayLike( out ) ) {
     68 			throw new TypeError( 'invalid argument. Output argument must be an array-like object. Value: `' + out + '`.' );
     69 		}
     70 		minmax = out;
     71 	}
     72 	min = PINF;
     73 	max = 0.0;
     74 	FLG = false;
     75 	return accumulator;
     76 
     77 	/**
     78 	* 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.
     79 	*
     80 	* @private
     81 	* @param {number} [x] - input value
     82 	* @returns {(ArrayLikeObject|null)} output array or null
     83 	*/
     84 	function accumulator( x ) {
     85 		var ax;
     86 		if ( arguments.length === 0 ) {
     87 			if ( FLG === false ) {
     88 				return null;
     89 			}
     90 			minmax[ 0 ] = min; // Why? Because we cannot guarantee someone hasn't mutated the output array
     91 			minmax[ 1 ] = max;
     92 			return minmax;
     93 		}
     94 		FLG = true;
     95 		if ( isnan( x ) ) {
     96 			min = x;
     97 			max = x;
     98 		} else {
     99 			ax = abs( x );
    100 			if ( ax < min ) {
    101 				min = ax;
    102 			}
    103 			if ( ax > max ) {
    104 				max = ax;
    105 			}
    106 		}
    107 		minmax[ 0 ] = min;
    108 		minmax[ 1 ] = max;
    109 		return minmax;
    110 	}
    111 }
    112 
    113 
    114 // EXPORTS //
    115 
    116 module.exports = incrminmaxabs;