time-to-botec

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

nanmskrange.js (2026B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Computes the range of a strided array according to a mask, ignoring `NaN` values.
     30 *
     31 * @param {PositiveInteger} N - number of indexed elements
     32 * @param {NumericArray} x - input array
     33 * @param {integer} strideX - `x` stride length
     34 * @param {NumericArray} mask - mask array
     35 * @param {integer} strideMask - `mask` stride length
     36 * @returns {number} range
     37 *
     38 * @example
     39 * var x = [ 1.0, -2.0, 4.0, 2.0, NaN ];
     40 * var mask = [ 0, 0, 1, 0, 0 ];
     41 *
     42 * var v = nanmskrange( x.length, x, 1, mask, 1 );
     43 * // returns 4.0
     44 */
     45 function nanmskrange( N, x, strideX, mask, strideMask ) {
     46 	var max;
     47 	var min;
     48 	var ix;
     49 	var im;
     50 	var v;
     51 	var i;
     52 
     53 	if ( N <= 0 ) {
     54 		return NaN;
     55 	}
     56 	if ( strideX < 0 ) {
     57 		ix = (1-N) * strideX;
     58 	} else {
     59 		ix = 0;
     60 	}
     61 	if ( strideMask < 0 ) {
     62 		im = (1-N) * strideMask;
     63 	} else {
     64 		im = 0;
     65 	}
     66 	for ( i = 0; i < N; i++ ) {
     67 		v = x[ ix ];
     68 		if ( v === v && mask[ im ] === 0 ) {
     69 			break;
     70 		}
     71 		ix += strideX;
     72 		im += strideMask;
     73 	}
     74 	if ( i === N ) {
     75 		return NaN;
     76 	}
     77 	min = v;
     78 	max = min;
     79 	i += 1;
     80 	for ( i; i < N; i++ ) {
     81 		ix += strideX;
     82 		im += strideMask;
     83 		if ( mask[ im ] ) {
     84 			continue;
     85 		}
     86 		v = x[ ix ];
     87 		if ( isnan( v ) ) {
     88 			continue;
     89 		}
     90 		if ( v < min ) {
     91 			min = v;
     92 		} else if ( v > max ) {
     93 			max = v;
     94 		}
     95 	}
     96 	return max - min;
     97 }
     98 
     99 
    100 // EXPORTS //
    101 
    102 module.exports = nanmskrange;