GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
dmaxe.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_exp(double *x, int n)
8 {
9  static double y[2];
10  double mean = 0.0, zmax, tmax, *xcopy, t, z, fx;
11  int i;
12 
13  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
14  fprintf(stderr, "Memory error in Cdhc_dmax_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  z = (double)(i + 1) / (double)n - fx;
29  t = fx - (double)i / (double)n;
30  if (i == 0 || z > zmax)
31  zmax = z;
32 
33  if (i == 0 || t > tmax)
34  tmax = t;
35  }
36 
37  y[0] = zmax;
38  y[1] = tmax;
39 
40  free(xcopy);
41 
42  return y;
43 }
double * Cdhc_dmax_exp(double *x, int n)
Definition: dmaxe.c:7
void free(void *)
#define NULL
Definition: ccmath.h:32
#define x
void * malloc(YYSIZE_T)
double t
Definition: r_raster.c:39
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.