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