ndarray.js (2256B)
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 isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); 25 26 27 // MAIN // 28 29 /** 30 * Computes the maximum 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 {NonNegativeInteger} offsetX - `x` starting index 36 * @param {NumericArray} mask - mask array 37 * @param {integer} strideMask - `mask` stride length 38 * @param {NonNegativeInteger} offsetMask - `mask` starting index 39 * @returns {number} maximum value 40 * 41 * @example 42 * var floor = require( '@stdlib/math/base/special/floor' ); 43 * 44 * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; 45 * var mask = [ 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 ]; 46 * var N = floor( x.length / 2 ); 47 * 48 * var v = nanmskmax( N, x, 2, 1, mask, 2, 1 ); 49 * // returns 4.0 50 */ 51 function nanmskmax( N, x, strideX, offsetX, mask, strideMask, offsetMask ) { 52 var max; 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 v = x[ ix ]; 65 if ( v === v && mask[ im ] === 0 ) { 66 break; 67 } 68 ix += strideX; 69 im += strideMask; 70 } 71 if ( i === N ) { 72 return NaN; 73 } 74 max = v; 75 i += 1; 76 for ( i; i < N; i++ ) { 77 ix += strideX; 78 im += strideMask; 79 if ( mask[ im ] ) { 80 continue; 81 } 82 v = x[ ix ]; 83 if ( isnan( v ) ) { 84 continue; 85 } 86 if ( v > max || ( v === max && isPositiveZero( v ) ) ) { 87 max = v; 88 } 89 } 90 return max; 91 } 92 93 94 // EXPORTS // 95 96 module.exports = nanmskmax;