dcumaxabs.c (1817B)
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 #include "stdlib/stats/base/dcumaxabs.h" 20 #include "stdlib/math/base/assert/is_nan.h" 21 #include <stdint.h> 22 #include <math.h> 23 24 /** 25 * Computes the cumulative maximum absolute value of double-precision floating-point strided array elements. 26 * 27 * @param N number of indexed elements 28 * @param X input array 29 * @param strideX X stride length 30 * @param Y output array 31 * @param strideY Y stride length 32 */ 33 void stdlib_strided_dcumaxabs( const int64_t N, const double *X, const int64_t strideX, double *Y, const int64_t strideY ) { 34 double max; 35 int64_t ix; 36 int64_t iy; 37 int64_t i; 38 double v; 39 40 if ( N <= 0 ) { 41 return; 42 } 43 if ( strideX < 0 ) { 44 ix = (1-N) * strideX; 45 } else { 46 ix = 0; 47 } 48 if ( strideY < 0 ) { 49 iy = (1-N) * strideY; 50 } else { 51 iy = 0; 52 } 53 max = fabs( X[ ix ] ); 54 Y[ iy ] = max; 55 56 iy += strideY; 57 i = 1; 58 if ( !stdlib_base_is_nan( max ) ) { 59 for (; i < N; i++ ) { 60 ix += strideX; 61 v = fabs( X[ ix ] ); 62 if ( stdlib_base_is_nan( v ) ) { 63 max = v; 64 break; 65 } 66 if ( v > max ) { 67 max = v; 68 } 69 Y[ iy ] = max; 70 iy += strideY; 71 } 72 } 73 if ( stdlib_base_is_nan( max ) ) { 74 for (; i < N; i++ ) { 75 Y[ iy ] = max; 76 iy += strideY; 77 } 78 } 79 return; 80 }