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