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