GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
cvmw2n.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(double *x, int n)
7 {
8  int i;
9  static double y[2];
10  double mean = 0.0, sdx = 0.0, fx, sqrt2, *xcopy;
11 
12  sqrt2 = sqrt((double)2.0);
13  y[1] = 0.0;
14 
15  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
16  fprintf(stderr, "Memory error in Cdhc_cramer_von_mises\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.0)));
26  mean /= n;
27 
28  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
29 
30  for (i = 0; i < n; ++i) {
31  fx = 0.5 + Cdhc_normp((xcopy[i] - mean) / sdx / sqrt2) / 2.0;
32  if (fx <= 1e-5)
33  fx = 1e-5;
34 
35  if (fx >= 0.99999)
36  fx = 0.99999;
37 
38  fx -= (2.0 * i + 1.0) / (2.0 * n);
39  y[1] += fx * fx;
40  }
41  y[1] += 1.0 / (double)(n * 12);
42  y[0] = y[1] * (0.5 / n + 1.0);
43 
44 #ifdef NOISY
45  fprintf(stdout, " TEST9 CVM(N) =%10.4f\n", y[0]);
46 #endif /* NOISY */
47 
48  free(xcopy);
49 
50  return y;
51 }
#define NULL
Definition: ccmath.h:32
double * Cdhc_cramer_von_mises(double *x, int n)
Definition: cvmw2n.c:6
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 *)
#define x