GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
7 double *dmax(double *x, int n)
8 {
9  static double y[2];
10  double *xcopy, sqrt2, sqrtn, mean = 0.0, sdx = 0.0, fx;
11  double dp, dp_max, dm, dm_max;
12  int i;
13 
14  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
15  fprintf(stderr, "Memory error in dmax\n");
16  exit(EXIT_FAILURE);
17  }
18 
19  sqrt2 = sqrt((double)2.0);
20  sqrtn = sqrt((double)n);
21 
22  for (i = 0; i < n; ++i) {
23  xcopy[i] = x[i];
24  mean += x[i];
25  sdx += x[i] * x[i];
26  }
27  sdx = sqrt((n * sdx - mean * mean) / (n * (n - 1.0)));
28  mean /= n;
29 
30  qsort(xcopy, n, sizeof(double), dcmp);
31 
32  for (i = 0; i < n; ++i) {
33  xcopy[i] = (xcopy[i] - mean) / sdx;
34  fx = 0.5 + normp(xcopy[i] / sqrt2) / 2.0;
35  if (fx <= 1e-5)
36  fx = 1e-5;
37 
38  if (fx >= 0.99999)
39  fx = 0.99999;
40 
41  dp = (double)(i + 1) / (double)n - fx;
42  dm = fx - i / (double)n;
43  if (i == 0 || dp > dp_max)
44  dp_max = dp;
45 
46  if (i == 0 || dm > dm_max)
47  dm_max = dm;
48  }
49 
50  y[0] = dp_max;
51  y[1] = dm_max;
52 
53  free(xcopy);
54 
55  return y;
56 }
int dcmp(const void *i, const void *j)
Definition: dcmp.c:1
int y
Definition: plot.c:34
void * malloc(YYSIZE_T)
double normp(double)
Definition: normp.c:23
return NULL
Definition: dbfopen.c:1394
void free(void *)
DCELL dmax
Definition: g3dcolor.c:53
int n
Definition: dataquad.c:291