GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 *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 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), 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 }
int dcmp(const void *i, const void *j)
Definition: dcmp.c:1
int y
Definition: plot.c:34
void * malloc(YYSIZE_T)
return NULL
Definition: dbfopen.c:1394
void free(void *)
double * dmax_exp(double *x, int n)
Definition: dmaxe.c:7
int n
Definition: dataquad.c:291