GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
dmax.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_dmax(double *x, int n)
8 {
9  static double y[2];
10  double *xcopy, sqrt2, sqrtn, mean = 0.0, sdx = 0.0, fx;
11  double dp, dp_max, dm, dm_max;
12  int i;
13 
14  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
15  fprintf(stderr, "Memory error in Cdhc_dmax\n");
16  exit(EXIT_FAILURE);
17  }
18 
19  sqrt2 = sqrt((double)2.0);
20  sqrtn = sqrt((double)n);
21 
22  for (i = 0; i < n; ++i) {
23  xcopy[i] = x[i];
24  mean += x[i];
25  sdx += x[i] * x[i];
26  }
27  sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
28  mean /= n;
29 
30  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
31 
32  for (i = 0; i < n; ++i) {
33  xcopy[i] = (xcopy[i] - mean) / sdx;
34  fx = 0.5 + Cdhc_normp(xcopy[i] / sqrt2) / 2.0;
35  if (fx <= 1e-5)
36  fx = 1e-5;
37 
38  if (fx >= 0.99999)
39  fx = 0.99999;
40 
41  dp = (double)(i + 1) / (double)n - fx;
42  dm = fx - i / (double)n;
43  if (i == 0 || dp > dp_max)
44  dp_max = dp;
45 
46  if (i == 0 || dm > dm_max)
47  dm_max = dm;
48  }
49 
50  y[0] = dp_max;
51  y[1] = dm_max;
52 
53  free(xcopy);
54 
55  return y;
56 }
double * Cdhc_dmax(double *x, int n)
Definition: dmax.c:7
void free(void *)
#define NULL
Definition: ccmath.h:32
#define x
void * malloc(YYSIZE_T)
double Cdhc_normp(double)
Definition: normp.c:23
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.