GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-57a646b4a4
cvmw2e.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_cramer_von_mises_exp(double *x, int n)
7 {
8  static double y[2];
9  double *xcopy, mean = 0.0, fx, fn2, sum4 = 0.0;
10  int i;
11 
12  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
13  fprintf(stderr, "Memory error in Cdhc_cramer_von_mises_exp\n");
14  exit(EXIT_FAILURE);
15  }
16 
17  for (i = 0; i < n; ++i) {
18  xcopy[i] = x[i];
19  mean += x[i];
20  }
21  mean /= n;
22 
23  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
24 
25  for (i = 0; i < n; ++i) {
26 
27  /*-
28  a = (2 * i + 1) * log (fx);
29  b = (2 * i + 1) * (xcopy[n-i-1] * (-1.0 / mean));
30  sum3 += a + b;
31  */
32  fx = 1 - exp(xcopy[i] * (-1.0 / mean));
33  fn2 = (double)(2.0 * i + 1) / (2 * n);
34  sum4 += (fx - fn2) * (fx - fn2);
35  }
36 
37  /*-
38  cvm = 1.0 / (n * 12) + sum4;
39  cvmod = cvm * (0.16 / n + 1.0);
40  */
41  y[0] = (1.0 / (n * 12) + sum4) * (0.16 / n + 1.0);
42 
43 #ifdef NOISY
44  fprintf(stdout, " TEST16 CVM(E) =%10.4f\n", y[0]);
45 #endif /* NOISY */
46 
47  free(xcopy);
48 
49  return y;
50 }
#define NULL
Definition: ccmath.h:32
double * Cdhc_cramer_von_mises_exp(double *x, int n)
Definition: cvmw2e.c:6
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.
void * malloc(YYSIZE_T)
void free(void *)
#define x