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