GRASS Programmer's Manual  6.4.2(2012)
dmaxe.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <math.h>
00004 #include "local_proto.h"
00005 
00006 
00007 double *dmax_exp(double *x, int n)
00008 {
00009     static double y[2];
00010     double mean = 0.0, zmax, tmax, *xcopy, t, z, fx;
00011     int i;
00012 
00013     if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
00014         fprintf(stderr, "Memory error in dmax_exp\n");
00015         exit(EXIT_FAILURE);
00016     }
00017 
00018     for (i = 0; i < n; ++i) {
00019         xcopy[i] = x[i];
00020         mean += x[i];
00021     }
00022     mean /= n;
00023 
00024     qsort(xcopy, n, sizeof(double), dcmp);
00025 
00026     for (i = 0; i < n; ++i) {
00027         fx = 1 - exp(-xcopy[i] / mean);
00028         z = (double)(i + 1) / (double)n - fx;
00029         t = fx - (double)i / (double)n;
00030         if (i == 0 || z > zmax)
00031             zmax = z;
00032 
00033         if (i == 0 || t > tmax)
00034             tmax = t;
00035     }
00036 
00037     y[0] = zmax;
00038     y[1] = tmax;
00039 
00040     free(xcopy);
00041 
00042     return y;
00043 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines