GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
chisqe.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 
5 
6 double *Cdhc_chi_square_exp(double *x, int n)
7 {
8  static double y[2];
9  double mean = 0.0, sum3 = 0.0, *v;
10  int i, j, k, *f;
11 
12  k = rint(4.0 * pow(0.75 * (n - 1.0) * (n - 1.0), 0.2));
13 
14  while ((double)(n / k) < 5.0)
15  --k;
16 
17  if ((f = (int *)calloc(k, sizeof(int))) == NULL) {
18  fprintf(stderr, "Memory error in Cdhc_chi_square\n");
19  exit(EXIT_FAILURE);
20  }
21  if ((v = (double *)malloc((k + 1) * sizeof(double))) == NULL) {
22  fprintf(stderr, "Memory error in Cdhc_chi_square\n");
23  exit(EXIT_FAILURE);
24  }
25 
26  for (i = 0; i < n; ++i)
27  mean += x[i];
28 
29  mean = n / mean;
30  v[0] = 0.0;
31 
32  for (i = 1; i < k; ++i)
33  v[i] = -log(1.0 - (double)i / k) / mean;
34 
35  v[k] = 1e9;
36 
37  for (i = 0; i < n; ++i) {
38  j = 0;
39  while (j < k) {
40  if (x[i] > v[j] && x[i] <= v[j + 1]) {
41  f[j]++;
42  j = k;
43  }
44  j++;
45  }
46  }
47 
48  for (i = 0; i < k; ++i)
49  sum3 += f[i] * f[i];
50 
51  y[0] = sum3 * k / n - n;
52  y[1] = (double)k - 2.0;
53 
54 #ifdef NOISY
55  fprintf(stdout, " TEST21 CS(E) =%10.4f DOF =%10.4f\n", y[0], y[1]);
56 #endif /* NOISY */
57 
58  free(f);
59  free(v);
60 
61  return y;
62 }
void free(void *)
#define NULL
Definition: ccmath.h:32
#define x
void * malloc(YYSIZE_T)
double * Cdhc_chi_square_exp(double *x, int n)
Definition: chisqe.c:6
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.