GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-77aab223bc
watsonun.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "local_proto.h"
5 
6 double *Cdhc_watson_u2(double *x, int n)
7 {
8  double *xcopy, mean = 0.0, sdx = 0.0, sqrt2, zbar = 0.0;
9  double fn2, fx, sum4 = 0.0;
10  static double y[2];
11  int i;
12 
13  sqrt2 = sqrt((double)2.0);
14 
15  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
16  fprintf(stderr, "Memory error in Cdhc_anderson_darling\n");
17  exit(EXIT_FAILURE);
18  }
19 
20  for (i = 0; i < n; ++i) {
21  xcopy[i] = x[i];
22  mean += x[i];
23  sdx += x[i] * x[i];
24  }
25  sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1)));
26  mean /= n;
27 
28  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
29 
30  for (i = 0; i < n; ++i) {
31  xcopy[i] = (xcopy[i] - mean) / sdx;
32  fn2 = (2.0 * (i + 1) - 1.0) / (2.0 * n);
33  fx = 0.5 + Cdhc_normp(xcopy[i] / sqrt2) / 2.0;
34 
35  if (fx <= 0.0)
36  fx = 1e-5;
37 
38  if (fx >= 1.0)
39  fx = 0.99999;
40 
41  zbar += fx;
42  sum4 += (fx - fn2) * (fx - fn2);
43  }
44 
45  zbar /= n;
46  y[0] = (1.0 / (n * 12) + sum4) - n * (zbar - .5) * (zbar - .5);
47  y[0] *= 0.5 / n + 1.0;
48 
49 #ifdef NOISY
50  fprintf(stdout, " TEST6 WU2(N) =%10.4f\n", y[0]);
51 #endif /* NOISY */
52 
53  free(xcopy);
54 
55  return y;
56 }
#define NULL
Definition: ccmath.h:32
int Cdhc_dcmp(const void *i, const void *j)
Definition: dcmp.c:1
double Cdhc_normp(double)
Definition: normp.c:22
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
void * malloc(YYSIZE_T)
void free(void *)
double * Cdhc_watson_u2(double *x, int n)
Definition: watsonun.c:6
#define x