dmeanlipw.c (1713B)
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/dmeanlipw.h" 20 #include "stdlib/blas/ext/base/dapxsumpw.h" 21 #include <stdint.h> 22 23 /** 24 * Computes the arithmetic mean of a double-precision floating-point strided array using a one-pass trial mean algorithm with pairwise summation. 25 * 26 * ## References 27 * 28 * - Ling, Robert F. 1974. "Comparison of Several Algorithms for Computing Sample Means and Variances." _Journal of the American Statistical Association_ 69 (348). American Statistical Association, Taylor & Francis, Ltd.: 859–66. doi:[10.2307/2286154](https://doi.org/10.2307/2286154). 29 * 30 * @param N number of indexed elements 31 * @param X input array 32 * @param stride stride length 33 * @return output value 34 */ 35 double stdlib_strided_dmeanlipw( const int64_t N, const double *X, const int64_t stride ) { 36 int64_t ix; 37 38 if ( N <= 0 ) { 39 return 0.0 / 0.0; // NaN 40 } 41 if ( N == 1 || stride == 0 ) { 42 return X[ 0 ]; 43 } 44 if ( stride < 0 ) { 45 ix = (1-N) * stride; 46 } else { 47 ix = 0; 48 } 49 return X[ ix ] + ( stdlib_strided_dapxsumpw( N-1, -X[ ix ], X+( (stride > 0) ? stride : 0 ), stride ) / (double)N ); 50 }