ndarray.js (2244B)
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. 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 = mskmax( N, x, 2, 1, mask, 2, 1 ); 49 * // returns 4.0 50 */ 51 function mskmax( 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 if ( mask[ im ] === 0 ) { 65 break; 66 } 67 ix += strideX; 68 im += strideMask; 69 } 70 if ( i === N ) { 71 return NaN; 72 } 73 max = x[ ix ]; 74 if ( isnan( max ) ) { 75 return max; 76 } 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 ( isnan( v ) ) { 86 return v; 87 } 88 if ( v > max || ( v === max && isPositiveZero( v ) ) ) { 89 max = v; 90 } 91 } 92 return max; 93 } 94 95 96 // EXPORTS // 97 98 module.exports = mskmax;