time-to-botec

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

ndarray.js (2376B)


      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 double-precision floating-point strided array according to a mask, ignoring `NaN` values.
     30 *
     31 * @param {PositiveInteger} N - number of indexed elements
     32 * @param {Float64Array} x - input array
     33 * @param {integer} strideX - `x` stride length
     34 * @param {NonNegativeInteger} offsetX - `x` starting index
     35 * @param {Uint8Array} mask - mask array
     36 * @param {integer} strideMask - `mask` stride length
     37 * @param {NonNegativeInteger} offsetMask - `mask` starting index
     38 * @returns {number} range
     39 *
     40 * @example
     41 * var Float64Array = require( '@stdlib/array/float64' );
     42 * var Uint8Array = require( '@stdlib/array/uint8' );
     43 * var floor = require( '@stdlib/math/base/special/floor' );
     44 *
     45 * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
     46 * var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 ] );
     47 * var N = floor( x.length / 2 );
     48 *
     49 * var v = dnanmskrange( N, x, 2, 1, mask, 2, 1 );
     50 * // returns 6.0
     51 */
     52 function dnanmskrange( N, x, strideX, offsetX, mask, strideMask, offsetMask ) {
     53 	var max;
     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 	max = min;
     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 ) {
     90 			min = v;
     91 		} else if ( v > max ) {
     92 			max = v;
     93 		}
     94 	}
     95 	return max - min;
     96 }
     97 
     98 
     99 // EXPORTS //
    100 
    101 module.exports = dnanmskrange;