time-to-botec

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

nanmskmin.js (2089B)


      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 var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Computes the minimum value of a strided array according to a mask, ignoring `NaN` values.
     31 *
     32 * @param {PositiveInteger} N - number of indexed elements
     33 * @param {NumericArray} x - input array
     34 * @param {integer} strideX - `x` stride length
     35 * @param {NumericArray} mask - mask array
     36 * @param {integer} strideMask - `mask` stride length
     37 * @returns {number} minimum value
     38 *
     39 * @example
     40 * var x = [ 1.0, -2.0, -4.0, 2.0, NaN ];
     41 * var mask = [ 0, 0, 1, 0, 0 ];
     42 *
     43 * var v = nanmskmin( x.length, x, 1, mask, 1 );
     44 * // returns -2.0
     45 */
     46 function nanmskmin( N, x, strideX, mask, strideMask ) {
     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 	i += 1;
     79 	for ( i; i < N; i++ ) {
     80 		ix += strideX;
     81 		im += strideMask;
     82 		if ( mask[ im ] ) {
     83 			continue;
     84 		}
     85 		v = x[ ix ];
     86 		if ( isnan( v ) ) {
     87 			continue;
     88 		}
     89 		if ( v < min || ( v === min && isNegativeZero( v ) ) ) {
     90 			min = v;
     91 		}
     92 	}
     93 	return min;
     94 }
     95 
     96 
     97 // EXPORTS //
     98 
     99 module.exports = nanmskmin;