|
| 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/dnanmaxabs.h" |
| 20 | +#include "stdlib/math/base/assert/is_nan.h" |
| 21 | +#include "stdlib/blas/base/shared.h" |
| 22 | +#include "stdlib/math/base/special/abs.h" |
| 23 | +#include "stdlib/strided/base/stride2offset.h" |
| 24 | + |
| 25 | +/** |
| 26 | +* Computes the maximum absolute value of a double-precision floating-point strided array, ignoring `NaN` values. |
| 27 | +* |
| 28 | +* @param N number of indexed elements |
| 29 | +* @param X input array |
| 30 | +* @param strideX stride length |
| 31 | +* @return output value |
| 32 | +*/ |
| 33 | +double API_SUFFIX(stdlib_strided_dnanmaxabs)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX ) { |
| 34 | + const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX ); |
| 35 | + return API_SUFFIX(stdlib_strided_dnanmaxabs_ndarray)( N, X, strideX, ox ); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | +* Computes the maximum absolute value of a double-precision floating-point strided array, ignoring `NaN` values and using alternative indexing semantics. |
| 40 | +* |
| 41 | +* @param N number of indexed elements |
| 42 | +* @param X input array |
| 43 | +* @param strideX stride length |
| 44 | +* @param offsetX starting index for X |
| 45 | +* @return output value |
| 46 | +*/ |
| 47 | +double API_SUFFIX(stdlib_strided_dnanmaxabs_ndarray)( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) { |
| 48 | + double max; |
| 49 | + CBLAS_INT ix; |
| 50 | + CBLAS_INT i; |
| 51 | + double v; |
| 52 | + |
| 53 | + if ( N <= 0 ) { |
| 54 | + return 0.0 / 0.0; // NaN |
| 55 | + } |
| 56 | + if ( N == 1 || strideX == 0 ) { |
| 57 | + return stdlib_base_abs( X[ offsetX ] ); |
| 58 | + } |
| 59 | + ix = offsetX; |
| 60 | + for ( i = 0; i < N; i++ ) { |
| 61 | + v = X[ ix ]; |
| 62 | + if ( v == v ) { |
| 63 | + break; |
| 64 | + } |
| 65 | + ix += strideX; |
| 66 | + } |
| 67 | + if ( i == N ) { |
| 68 | + return 0.0 / 0.0; // NaN |
| 69 | + } |
| 70 | + max = stdlib_base_abs( v ); |
| 71 | + i += 1; |
| 72 | + for (; i < N; i++ ) { |
| 73 | + ix += strideX; |
| 74 | + v = stdlib_base_abs( X[ ix ] ); |
| 75 | + if ( stdlib_base_is_nan( v ) ) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + if ( v > max ) { |
| 79 | + max = v; |
| 80 | + } |
| 81 | + } |
| 82 | + return max; |
| 83 | +} |
0 commit comments