time-to-botec

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

ndarray.js (2183B)


      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.
     30 *
     31 * @param {PositiveInteger} N - number of indexed elements
     32 * @param {NumericArray} x - input array
     33 * @param {integer} strideX - `x` stride length
     34 * @param {NonNegativeInteger} offsetX - `x` starting index
     35 * @param {NumericArray} 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 floor = require( '@stdlib/math/base/special/floor' );
     42 *
     43 * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
     44 * var mask = [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 ];
     45 * var N = floor( x.length / 2 );
     46 *
     47 * var v = mskrange( N, x, 2, 1, mask, 2, 1 );
     48 * // returns 6.0
     49 */
     50 function mskrange( N, x, strideX, offsetX, mask, strideMask, offsetMask ) {
     51 	var max;
     52 	var min;
     53 	var ix;
     54 	var im;
     55 	var v;
     56 	var i;
     57 
     58 	if ( N <= 0 ) {
     59 		return NaN;
     60 	}
     61 	ix = offsetX;
     62 	im = offsetMask;
     63 	for ( i = 0; i < N; i++ ) {
     64 		if ( mask[ im ] === 0 ) {
     65 			break;
     66 		}
     67 		ix += strideX;
     68 		im += strideMask;
     69 	}
     70 	if ( i === N ) {
     71 		return NaN;
     72 	}
     73 	min = x[ ix ];
     74 	if ( isnan( min ) ) {
     75 		return min;
     76 	}
     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 			return v;
     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 = mskrange;