GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-55b2a2bccc
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 double *Cdhc_dmax_exp(double *x, int n)
7 {
8  static double y[2];
9  double mean = 0.0, zmax, tmax, *xcopy, t, z, fx;
10  int i;
11 
12  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
13  fprintf(stderr, "Memory error in Cdhc_dmax_exp\n");
14  exit(EXIT_FAILURE);
15  }
16 
17  for (i = 0; i < n; ++i) {
18  xcopy[i] = x[i];
19  mean += x[i];
20  }
21  mean /= n;
22 
23  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
24 
25  for (i = 0; i < n; ++i) {
26  fx = 1 - exp(-xcopy[i] / mean);
27  z = (double)(i + 1) / (double)n - fx;
28  t = fx - (double)i / (double)n;
29  if (i == 0 || z > zmax)
30  zmax = z;
31 
32  if (i == 0 || t > tmax)
33  tmax = t;
34  }
35 
36  y[0] = zmax;
37  y[1] = tmax;
38 
39  free(xcopy);
40 
41  return y;
42 }
#define NULL
Definition: ccmath.h:32
int Cdhc_dcmp(const void *i, const void *j)
Definition: dcmp.c:1
double * Cdhc_dmax_exp(double *x, int n)
Definition: dmaxe.c:6
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
double t
Definition: r_raster.c:39
void * malloc(YYSIZE_T)
void free(void *)
#define x