dmskmax.js (2250B)
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 double-precision floating-point strided array according to a mask. 31 * 32 * @param {PositiveInteger} N - number of indexed elements 33 * @param {Float64Array} x - input array 34 * @param {integer} strideX - `x` stride length 35 * @param {Uint8Array} mask - mask array 36 * @param {integer} strideMask - `mask` stride length 37 * @returns {number} maximum value 38 * 39 * @example 40 * var Float64Array = require( '@stdlib/array/float64' ); 41 * var Uint8Array = require( '@stdlib/array/uint8' ); 42 * 43 * var x = new Float64Array( [ 1.0, -2.0, 4.0, 2.0 ] ); 44 * var mask = new Uint8Array( [ 0, 0, 1, 0 ] ); 45 * 46 * var v = dmskmax( x.length, x, 1, mask, 1 ); 47 * // returns 2.0 48 */ 49 function dmskmax( N, x, strideX, mask, strideMask ) { 50 var max; 51 var ix; 52 var im; 53 var v; 54 var i; 55 56 if ( N <= 0 ) { 57 return NaN; 58 } 59 if ( strideX < 0 ) { 60 ix = (1-N) * strideX; 61 } else { 62 ix = 0; 63 } 64 if ( strideMask < 0 ) { 65 im = (1-N) * strideMask; 66 } else { 67 im = 0; 68 } 69 for ( i = 0; i < N; i++ ) { 70 if ( mask[ im ] === 0 ) { 71 break; 72 } 73 ix += strideX; 74 im += strideMask; 75 } 76 if ( i === N ) { 77 return NaN; 78 } 79 max = x[ ix ]; 80 if ( isnan( max ) ) { 81 return max; 82 } 83 i += 1; 84 for ( i; i < N; i++ ) { 85 ix += strideX; 86 im += strideMask; 87 if ( mask[ im ] ) { 88 continue; 89 } 90 v = x[ ix ]; 91 if ( isnan( v ) ) { 92 return v; 93 } 94 if ( v > max || ( v === max && isPositiveZero( v ) ) ) { 95 max = v; 96 } 97 } 98 return max; 99 } 100 101 102 // EXPORTS // 103 104 module.exports = dmskmax;