GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
watsonue.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_watson_u2_exp(double *x, int n)
8 {
9  double *xcopy, mean = 0.0, zbar = 0.0, fn2, fx, sum4 = 0.0;
10  static double y[2];
11  int i;
12 
13  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
14  fprintf(stderr, "Memory error in Cdhc_watson_u2_exp\n");
15  exit(EXIT_FAILURE);
16  }
17 
18  for (i = 0; i < n; ++i) {
19  xcopy[i] = x[i];
20  mean += x[i];
21  }
22  mean /= n;
23 
24  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
25 
26  for (i = 0; i < n; ++i) {
27  fx = 1 - exp(-xcopy[i] / mean);
28  if (fx <= 1e-5)
29  fx = 1e-5;
30 
31  if (fx >= 0.99999)
32  fx = 0.99999;
33 
34  /* sum3 += 2 * (i + 1) * (log (fx) + log (1.0 - fx[n - i - 1])); */
35  fn2 = (2.0 * i + 1.0) / (2.0 * n);
36  sum4 += (fx - fn2) * (fx - fn2);
37  fn2 = (2.0 * (i + 1) - 1.0) / (2.0 * n);
38  zbar += fx;
39  }
40 
41  zbar /= n;
42  y[0] = (1.0 / (n * 12) + sum4) - n * (zbar - .5) * (zbar - .5);
43  y[0] *= 1.0 + 0.16 / n;
44 
45 #ifdef NOISY
46  fprintf(stdout, " TEST19 WU2(E) =%10.4f\n", y[0]);
47 #endif /* NOISY */
48 
49  free(xcopy);
50 
51  return y;
52 }
void free(void *)
#define NULL
Definition: ccmath.h:32
#define x
void * malloc(YYSIZE_T)
double * Cdhc_watson_u2_exp(double *x, int n)
Definition: watsonue.c:7
int Cdhc_dcmp(const void *i, const void *j)
Definition: dcmp.c:1
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.