GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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 double *Cdhc_chi_square(double *x, int n)
7 {
8  static double y[2];
9  double mean = 0.0, sdx = 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  sdx += x[i] * x[i];
29  }
30  sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
31  mean /= n;
32 
33  v[0] = -1e9;
34  for (i = 1; i < k; ++i)
35  v[i] = mean + Cdhc_xinormal((double)i / k) * sdx;
36 
37  v[k] = 1e9;
38 
39  for (i = 0; i < n; ++i) {
40  j = 0;
41  while (j < k) {
42  if (x[i] > v[j] && x[i] <= v[j + 1]) {
43  f[j]++;
44  j = k;
45  }
46  j++;
47  }
48  }
49 
50  for (i = 0; i < k; ++i)
51  sum3 += f[i] * f[i];
52 
53  y[0] = sum3 * k / n - n;
54  y[1] = (double)k - 3.0;
55 
56 #ifdef NOISY
57  fprintf(stdout, " TEST12 CS(N) =%10.4f DOF =%10.4f\n", y[0], y[1]);
58 #endif /* NOISY */
59 
60  free(f);
61  free(v);
62 
63  return y;
64 }
#define NULL
Definition: ccmath.h:32
double * Cdhc_chi_square(double *x, int n)
Definition: chisqn.c:6
double Cdhc_xinormal(double)
Definition: xinormal.c:4
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
void * malloc(YYSIZE_T)
void free(void *)
#define x