time-to-botec

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

ndarray.js (2446B)


      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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
     24 var isNegativeZerof = require( '@stdlib/math/base/assert/is-negative-zerof' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Computes the minimum value of a single-precision floating-point strided array according to a mask, ignoring `NaN` values.
     31 *
     32 * @param {PositiveInteger} N - number of indexed elements
     33 * @param {Float32Array} x - input array
     34 * @param {integer} strideX - `x` stride length
     35 * @param {NonNegativeInteger} offsetX - `x` starting index
     36 * @param {Uint8Array} mask - mask array
     37 * @param {integer} strideMask - `mask` stride length
     38 * @param {NonNegativeInteger} offsetMask - `mask` starting index
     39 * @returns {number} minimum value
     40 *
     41 * @example
     42 * var Float32Array = require( '@stdlib/array/float32' );
     43 * var Uint8Array = require( '@stdlib/array/uint8' );
     44 * var floor = require( '@stdlib/math/base/special/floor' );
     45 *
     46 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, -5.0, -6.0 ] );
     47 * var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 ] );
     48 * var N = floor( x.length / 2 );
     49 *
     50 * var v = snanmskmin( N, x, 2, 1, mask, 2, 1 );
     51 * // returns -2.0
     52 */
     53 function snanmskmin( N, x, strideX, offsetX, mask, strideMask, offsetMask ) {
     54 	var min;
     55 	var ix;
     56 	var im;
     57 	var v;
     58 	var i;
     59 
     60 	if ( N <= 0 ) {
     61 		return NaN;
     62 	}
     63 	ix = offsetX;
     64 	im = offsetMask;
     65 	for ( i = 0; i < N; i++ ) {
     66 		v = x[ ix ];
     67 		if ( v === v && mask[ im ] === 0 ) {
     68 			break;
     69 		}
     70 		ix += strideX;
     71 		im += strideMask;
     72 	}
     73 	if ( i === N ) {
     74 		return NaN;
     75 	}
     76 	min = v;
     77 	i += 1;
     78 	for ( i; i < N; i++ ) {
     79 		ix += strideX;
     80 		im += strideMask;
     81 		if ( mask[ im ] ) {
     82 			continue;
     83 		}
     84 		v = x[ ix ];
     85 		if ( isnanf( v ) ) {
     86 			continue;
     87 		}
     88 		if ( v < min || ( v === min && isNegativeZerof( v ) ) ) {
     89 			min = v;
     90 		}
     91 	}
     92 	return min;
     93 }
     94 
     95 
     96 // EXPORTS //
     97 
     98 module.exports = snanmskmin;