@stdlib/stats-strided-dvariancetk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/main.c ADDED
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 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/strided/dvariancetk.h"
20
+ #include "stdlib/blas/base/shared.h"
21
+ #include "stdlib/strided/base/stride2offset.h"
22
+
23
+ /**
24
+ * Computes the variance of a double-precision floating-point strided array using a one-pass textbook algorithm.
25
+ *
26
+ * @param N number of indexed elements
27
+ * @param correction degrees of freedom adjustment
28
+ * @param X input array
29
+ * @param strideX stride length
30
+ * @return output value
31
+ */
32
+ double API_SUFFIX(stdlib_strided_dvariancetk)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX ) {
33
+ const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
34
+ return API_SUFFIX(stdlib_strided_dvariancetk_ndarray)( N, correction, X, strideX, ox );
35
+ }
36
+
37
+ /**
38
+ * Computes the variance of a double-precision floating-point strided array using a one-pass textbook algorithm and alternative indexing semantics.
39
+ *
40
+ * @param N number of indexed elements
41
+ * @param correction degrees of freedom adjustment
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_dvariancetk_ndarray)( const CBLAS_INT N, const double correction, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
48
+ CBLAS_INT ix;
49
+ CBLAS_INT i;
50
+ double dN;
51
+ double S2;
52
+ double S;
53
+ double n;
54
+ double v;
55
+
56
+ dN = (double)N;
57
+ n = dN - correction;
58
+ if ( N <= 0 || n <= 0.0 ) {
59
+ return 0.0 / 0.0; // NaN
60
+ }
61
+ if ( N == 1 || strideX == 0 ) {
62
+ return 0.0;
63
+ }
64
+ ix = offsetX;
65
+ S2 = 0.0;
66
+ S = 0.0;
67
+ for ( i = 0; i < N; i++ ) {
68
+ v = X[ ix ];
69
+ S2 += v * v;
70
+ S += v;
71
+ ix += strideX;
72
+ }
73
+ return (S2 - ((S/dN)*S)) / n;
74
+ }