1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
1.2 |
static const char RCSid[] = "$Id: gcalc.c,v 1.1 2003/02/22 02:07:26 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* gcalc.c - routines to do calculations on graph files. |
6 |
|
|
* |
7 |
|
|
* 7/7/86 |
8 |
|
|
* Greg Ward Larson |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <stdio.h> |
12 |
schorsch |
1.2 |
#include <string.h> |
13 |
greg |
1.1 |
|
14 |
|
|
#include "mgvars.h" |
15 |
|
|
|
16 |
|
|
|
17 |
|
|
static double xsum, xxsum, ysum, yysum, xysum; |
18 |
|
|
static double xmin, xmax, ymin, ymax; |
19 |
|
|
static double lastx, lasty, rsum; |
20 |
|
|
static int npts; |
21 |
|
|
|
22 |
|
|
|
23 |
|
|
gcalc(types) /* do a calculation */ |
24 |
|
|
char *types; |
25 |
|
|
{ |
26 |
|
|
int i, calcpoint(); |
27 |
|
|
|
28 |
schorsch |
1.2 |
if (strchr(types, 'h') == NULL) |
29 |
greg |
1.1 |
gcheader(types); |
30 |
|
|
|
31 |
|
|
xmin = gparam[XMIN].flags & DEFINED ? |
32 |
|
|
varvalue(gparam[XMIN].name) : |
33 |
|
|
-1e10; |
34 |
|
|
xmax = gparam[XMAX].flags & DEFINED ? |
35 |
|
|
varvalue(gparam[XMAX].name) : |
36 |
|
|
1e10; |
37 |
|
|
|
38 |
|
|
for (i = 0; i < MAXCUR; i++) { |
39 |
|
|
xsum = xxsum = ysum = yysum = xysum = 0.0; |
40 |
|
|
rsum = 0.0; |
41 |
|
|
npts = 0; |
42 |
|
|
mgcurve(i, calcpoint); |
43 |
|
|
gcvalue(i, types); |
44 |
|
|
} |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
gcheader(types) /* print header */ |
49 |
|
|
register char *types; |
50 |
|
|
{ |
51 |
|
|
printf("__"); |
52 |
|
|
while (*types) |
53 |
|
|
switch (*types++) { |
54 |
|
|
case 'n': |
55 |
|
|
printf("|_Label___________"); |
56 |
|
|
break; |
57 |
|
|
case 'a': |
58 |
|
|
printf("|____Mean______S.D._"); |
59 |
|
|
break; |
60 |
|
|
case 'm': |
61 |
|
|
printf("|_____Min_______Max_"); |
62 |
|
|
break; |
63 |
|
|
case 'i': |
64 |
|
|
printf("|___Integ_"); |
65 |
|
|
break; |
66 |
|
|
case 'l': |
67 |
|
|
printf("|___Slope_____Intcp______Corr_"); |
68 |
|
|
break; |
69 |
|
|
default: |
70 |
|
|
break; |
71 |
|
|
} |
72 |
|
|
printf("\n"); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
gcvalue(c, types) /* print the values for the curve */ |
77 |
|
|
int c; |
78 |
|
|
register char *types; |
79 |
|
|
{ |
80 |
|
|
double d1, d2, d3, sqrt(); |
81 |
|
|
|
82 |
|
|
if (npts < 1) |
83 |
|
|
return; |
84 |
|
|
|
85 |
|
|
printf("%c:", c+'A'); |
86 |
|
|
while (*types) |
87 |
|
|
switch (*types++) { |
88 |
|
|
case 'n': |
89 |
|
|
printf(" %-16s", cparam[c][CLABEL].flags & DEFINED ? |
90 |
|
|
cparam[c][CLABEL].v.s : ""); |
91 |
|
|
break; |
92 |
|
|
case 'a': |
93 |
|
|
d1 = sqrt((yysum - ysum*ysum/npts)/(npts-1)); |
94 |
|
|
printf(" %9.2f %9.3f", ysum/npts, d1); |
95 |
|
|
break; |
96 |
|
|
case 'm': |
97 |
|
|
printf(" %9.2f %9.2f", ymin, ymax); |
98 |
|
|
break; |
99 |
|
|
case 'i': |
100 |
|
|
printf(" %9.2f", rsum); |
101 |
|
|
break; |
102 |
|
|
case 'l': |
103 |
|
|
d3 = xxsum - xsum*xsum/npts; |
104 |
|
|
d1 = (xysum - xsum*ysum/npts)/d3; |
105 |
|
|
d2 = (ysum - d1*xsum)/npts; |
106 |
|
|
d3 = d1*sqrt(d3/(yysum - ysum*ysum/npts)); |
107 |
|
|
printf(" %9.5f %9.2f %9.5f", d1, d2, d3); |
108 |
|
|
break; |
109 |
|
|
default: |
110 |
|
|
break; |
111 |
|
|
} |
112 |
|
|
printf("\n"); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
calcpoint(c, x, y) /* add a point to our calculation */ |
117 |
|
|
int c; |
118 |
|
|
double x, y; |
119 |
|
|
{ |
120 |
|
|
if (x < xmin || x > xmax) |
121 |
|
|
return; |
122 |
|
|
|
123 |
|
|
xsum += x; |
124 |
|
|
xxsum += x*x; |
125 |
|
|
ysum += y; |
126 |
|
|
yysum += y*y; |
127 |
|
|
xysum += x*y; |
128 |
|
|
|
129 |
|
|
if (npts) { |
130 |
|
|
if (y < ymin) |
131 |
|
|
ymin = y; |
132 |
|
|
else if (y > ymax) |
133 |
|
|
ymax = y; |
134 |
|
|
} else |
135 |
|
|
ymin = ymax = y; |
136 |
|
|
|
137 |
|
|
if (npts) |
138 |
|
|
rsum += ( y + lasty )*( x - lastx )/2.0; |
139 |
|
|
lastx = x; |
140 |
|
|
lasty = y; |
141 |
|
|
|
142 |
|
|
npts++; |
143 |
|
|
} |