cumin.js (2023B)
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 isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); 25 26 27 // MAIN // 28 29 /** 30 * Computes the cumulative minimum of a strided array. 31 * 32 * @param {PositiveInteger} N - number of indexed elements 33 * @param {NumericArray} x - input array 34 * @param {integer} strideX - `x` stride length 35 * @param {NumericArray} y - output array 36 * @param {integer} strideY - `y` stride length 37 * @returns {NumericArray} output array 38 * 39 * @example 40 * var x = [ 1.0, -2.0, 2.0 ]; 41 * var y = [ 0.0, 0.0, 0.0 ]; 42 * var N = x.length; 43 * 44 * var v = cumin( N, x, 1, y, 1 ); 45 * // returns [ 1.0, -2.0, -2.0 ] 46 */ 47 function cumin( N, x, strideX, y, strideY ) { 48 var min; 49 var ix; 50 var iy; 51 var v; 52 var i; 53 54 if ( N <= 0 ) { 55 return y; 56 } 57 if ( strideX < 0 ) { 58 ix = (1-N) * strideX; 59 } else { 60 ix = 0; 61 } 62 if ( strideY < 0 ) { 63 iy = (1-N) * strideY; 64 } else { 65 iy = 0; 66 } 67 min = x[ ix ]; 68 y[ iy ] = min; 69 70 iy += strideY; 71 i = 1; 72 if ( isnan( min ) === false ) { 73 for ( i; i < N; i++ ) { 74 ix += strideX; 75 v = x[ ix ]; 76 if ( isnan( v ) ) { 77 min = v; 78 break; 79 } 80 if ( v < min || ( v === min && isNegativeZero( v ) ) ) { 81 min = v; 82 } 83 y[ iy ] = min; 84 iy += strideY; 85 } 86 } 87 if ( isnan( min ) ) { 88 for ( i; i < N; i++ ) { 89 y[ iy ] = min; 90 iy += strideY; 91 } 92 } 93 return y; 94 } 95 96 97 // EXPORTS // 98 99 module.exports = cumin;