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