| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
schorsch |
1.5 |
static const char RCSid[] = "$Id: tabfunc.c,v 1.4 2003/07/14 20:02:29 schorsch Exp $"; |
| 3 |
greg |
1.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Put tabular data into functions suitable for cal programs. |
| 6 |
|
|
* |
| 7 |
|
|
* 2/2/95 Greg Ward |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include <stdio.h> |
| 11 |
|
|
#include <stdlib.h> |
| 12 |
schorsch |
1.2 |
#include <string.h> |
| 13 |
greg |
1.1 |
#include <math.h> |
| 14 |
|
|
#include <ctype.h> |
| 15 |
schorsch |
1.4 |
#include <sys/types.h> |
| 16 |
schorsch |
1.2 |
|
| 17 |
schorsch |
1.4 |
#include "rtprocess.h" /* getpid() */ |
| 18 |
|
|
#include "rtmath.h" |
| 19 |
|
|
#include "rtio.h" |
| 20 |
greg |
1.1 |
|
| 21 |
|
|
#define isdelim(c) (isspace(c) || (c)==',') |
| 22 |
|
|
|
| 23 |
|
|
#define MAXTAB 1024 /* maximum number of data rows */ |
| 24 |
|
|
#define MAXLINE 4096 /* maximum line width (characters) */ |
| 25 |
|
|
#define OUTFMT "%.7g" /* output format conversion string */ |
| 26 |
|
|
|
| 27 |
|
|
int interpolate = 0; |
| 28 |
|
|
char *progname; |
| 29 |
|
|
char **func; |
| 30 |
|
|
int nfuncs; |
| 31 |
|
|
|
| 32 |
schorsch |
1.3 |
RREAL abscissa[MAXTAB]; /* independent values (first column) */ |
| 33 |
|
|
RREAL (*ordinate)[MAXTAB]; /* dependent values (other columns) */ |
| 34 |
greg |
1.1 |
int tabsize = 0; /* final table size (number of rows) */ |
| 35 |
|
|
char locID[16]; /* local identifier (for uniqueness) */ |
| 36 |
|
|
|
| 37 |
schorsch |
1.2 |
/*extern char *fgets(), *fskip(), *absc_exp();*/ |
| 38 |
greg |
1.1 |
|
| 39 |
schorsch |
1.2 |
static void load_data(FILE *fp); |
| 40 |
|
|
static void print_funcs(char *xe); |
| 41 |
schorsch |
1.3 |
static void putlist(register RREAL *av, int al, register int pos); |
| 42 |
schorsch |
1.2 |
static char * absc_exp(void); |
| 43 |
|
|
|
| 44 |
|
|
int |
| 45 |
|
|
main( |
| 46 |
|
|
int argc, |
| 47 |
|
|
char **argv |
| 48 |
|
|
) |
| 49 |
greg |
1.1 |
{ |
| 50 |
|
|
progname = argv[0]; |
| 51 |
|
|
argv++; |
| 52 |
|
|
argc--; |
| 53 |
|
|
if (argc && !strcmp(argv[0], "-i")) { |
| 54 |
|
|
interpolate++; |
| 55 |
|
|
puts("interp_arr2`(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1);"); |
| 56 |
|
|
puts("interp_arr`(x,f):if(x-1,if(f(0)-x,interp_arr2`(floor(x),x,f),f(f(0))),f(1));"); |
| 57 |
|
|
argv++; |
| 58 |
|
|
argc--; |
| 59 |
|
|
} |
| 60 |
|
|
if (!argc || argv[0][0] == '-') { |
| 61 |
|
|
fprintf(stderr, "Usage: %s [-i] func1 [func2 ..]\n", progname); |
| 62 |
|
|
exit(1); |
| 63 |
|
|
} |
| 64 |
|
|
func = argv; |
| 65 |
|
|
nfuncs = argc; |
| 66 |
schorsch |
1.3 |
ordinate = (RREAL (*)[MAXTAB])malloc(nfuncs*MAXTAB*sizeof(RREAL)); |
| 67 |
greg |
1.1 |
if (ordinate == NULL) { |
| 68 |
|
|
fprintf(stderr, "%s: not enough memory\n", progname); |
| 69 |
|
|
exit(1); |
| 70 |
|
|
} |
| 71 |
|
|
sprintf(locID, "p%d", getpid()); |
| 72 |
|
|
load_data(stdin); |
| 73 |
|
|
print_funcs(absc_exp()); |
| 74 |
|
|
exit(0); |
| 75 |
|
|
} |
| 76 |
|
|
|
| 77 |
|
|
|
| 78 |
schorsch |
1.2 |
static void |
| 79 |
|
|
load_data( /* load tabular data from fp */ |
| 80 |
|
|
FILE *fp |
| 81 |
|
|
) |
| 82 |
greg |
1.1 |
{ |
| 83 |
|
|
int lineno; |
| 84 |
|
|
char *err; |
| 85 |
|
|
char inpbuf[MAXLINE]; |
| 86 |
|
|
register char *cp; |
| 87 |
|
|
register int i; |
| 88 |
|
|
|
| 89 |
|
|
tabsize = lineno = 0; |
| 90 |
|
|
inpbuf[MAXLINE-2] = '\n'; |
| 91 |
|
|
while (fgets(inpbuf, MAXLINE, fp) != NULL) { |
| 92 |
|
|
lineno++; |
| 93 |
|
|
if (inpbuf[MAXLINE-2] != '\n') { |
| 94 |
|
|
err = "line too long"; |
| 95 |
|
|
goto fatal; |
| 96 |
|
|
} |
| 97 |
|
|
if (tabsize >= MAXTAB-1) { |
| 98 |
|
|
err = "too many rows"; |
| 99 |
|
|
goto fatal; |
| 100 |
|
|
} |
| 101 |
|
|
if ((cp = fskip(inpbuf)) == NULL) |
| 102 |
|
|
continue; /* skip non-data lines */ |
| 103 |
|
|
abscissa[tabsize] = atof(inpbuf); |
| 104 |
|
|
for (i = 0; i < nfuncs; i++) { |
| 105 |
|
|
while (isdelim(*cp)) |
| 106 |
|
|
cp++; |
| 107 |
|
|
if (!*cp) { |
| 108 |
|
|
err = "too few columns"; |
| 109 |
|
|
goto fatal; |
| 110 |
|
|
} |
| 111 |
|
|
ordinate[i][tabsize] = atof(cp); |
| 112 |
|
|
if ((cp = fskip(cp)) == NULL) { |
| 113 |
|
|
err = "bad floating-point format"; |
| 114 |
|
|
goto fatal; |
| 115 |
|
|
} |
| 116 |
|
|
} |
| 117 |
|
|
tabsize++; |
| 118 |
|
|
} |
| 119 |
|
|
return; |
| 120 |
|
|
fatal: |
| 121 |
|
|
fprintf(stderr, "%s: input line %d: %s\n", progname, lineno, err); |
| 122 |
|
|
exit(1); |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
|
| 126 |
schorsch |
1.2 |
static char * |
| 127 |
|
|
absc_exp(void) /* produce expression for abscissa */ |
| 128 |
greg |
1.1 |
{ |
| 129 |
|
|
static char ourexp[64]; |
| 130 |
|
|
double step, eps; |
| 131 |
|
|
int uniform, increasing; |
| 132 |
|
|
register int i; |
| 133 |
|
|
|
| 134 |
|
|
if (tabsize < 2) |
| 135 |
|
|
return("1"); |
| 136 |
|
|
step = abscissa[1] - abscissa[0]; |
| 137 |
|
|
eps = ((increasing = (step > 0)) ? 1e-3 : -1e-3) * step; |
| 138 |
|
|
uniform = 1; |
| 139 |
|
|
for (i = 2; i < tabsize; i++) { |
| 140 |
|
|
if (uniform && fabs((abscissa[i]-abscissa[i-1]) - step) > eps) |
| 141 |
|
|
uniform = 0; |
| 142 |
|
|
if (!uniform && (abscissa[i-1] < abscissa[i]) != increasing) { |
| 143 |
|
|
fprintf(stderr, "%s: input not a function\n", |
| 144 |
|
|
progname); |
| 145 |
|
|
exit(1); |
| 146 |
|
|
} |
| 147 |
|
|
} |
| 148 |
|
|
if (uniform) { |
| 149 |
|
|
if (increasing && fabs(step - 1) < eps) { |
| 150 |
|
|
if (fabs(abscissa[0] - 1) < eps) |
| 151 |
|
|
strcpy(ourexp, "x"); |
| 152 |
|
|
else |
| 153 |
|
|
sprintf(ourexp, "x-%g", abscissa[0]-1); |
| 154 |
|
|
} else |
| 155 |
|
|
sprintf(ourexp, "(x-%g)/%g+1", abscissa[0], step); |
| 156 |
|
|
} else { |
| 157 |
|
|
printf("X`%s(i):select(i,", locID); |
| 158 |
|
|
putlist(abscissa, tabsize, 20); |
| 159 |
|
|
puts(");"); |
| 160 |
|
|
if (increasing) { |
| 161 |
|
|
printf("fx`%s(x):if(x-%g,if(%g-x,fx2`%s(x,%d),%d),1);\n", |
| 162 |
|
|
locID, abscissa[0], abscissa[tabsize-1], |
| 163 |
|
|
locID, tabsize, tabsize); |
| 164 |
|
|
printf("fx2`%s(x,i):if(x-X`%s(i),\n", locID, locID); |
| 165 |
|
|
} else { |
| 166 |
|
|
printf("fx`%s(x):if(%g-x,if(x-%g,fx2`%s(x,%d),%d),1);\n", |
| 167 |
|
|
locID, abscissa[0], abscissa[tabsize-1], |
| 168 |
|
|
locID, tabsize, tabsize); |
| 169 |
|
|
printf("fx2`%s(x,i):if(X`%s(i)-x,\n", locID, locID); |
| 170 |
|
|
} |
| 171 |
|
|
printf("\ti+(x-X`%s(i))/(X`%s(i+1)-X`%s(i)),\n", |
| 172 |
|
|
locID, locID, locID); |
| 173 |
|
|
printf("\tfx2`%s(x,i-1));\n", locID); |
| 174 |
|
|
sprintf(ourexp, "fx`%s(x)", locID); |
| 175 |
|
|
} |
| 176 |
|
|
return(ourexp); |
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
|
| 180 |
schorsch |
1.2 |
static void |
| 181 |
|
|
print_funcs( /* print functions */ |
| 182 |
|
|
char *xe |
| 183 |
|
|
) |
| 184 |
greg |
1.1 |
{ |
| 185 |
|
|
int xelen; |
| 186 |
|
|
register int i; |
| 187 |
|
|
|
| 188 |
|
|
xelen = strlen(xe); |
| 189 |
|
|
for (i = 0; i < nfuncs; i++) { |
| 190 |
schorsch |
1.5 |
if ((func[i][0] == '\0') | (func[i][0] == '0')) |
| 191 |
greg |
1.1 |
continue; |
| 192 |
|
|
if (interpolate) { |
| 193 |
|
|
printf("%s`%s(i):select(i,", func[i], locID); |
| 194 |
|
|
putlist(ordinate[i], tabsize, |
| 195 |
|
|
27+strlen(func[i])); |
| 196 |
|
|
puts(");"); |
| 197 |
|
|
printf("%s(x):interp_arr`(%s,%s`%s);\n", |
| 198 |
|
|
func[i], xe, func[i], locID); |
| 199 |
|
|
} else { |
| 200 |
|
|
printf("%s(x):select(%s,", func[i], xe); |
| 201 |
|
|
putlist(ordinate[i], tabsize, strlen(func[i])+xelen+12); |
| 202 |
|
|
puts(");"); |
| 203 |
|
|
} |
| 204 |
|
|
} |
| 205 |
|
|
} |
| 206 |
|
|
|
| 207 |
|
|
|
| 208 |
schorsch |
1.2 |
static void |
| 209 |
|
|
putlist( /* put out array of values */ |
| 210 |
schorsch |
1.3 |
register RREAL *av, |
| 211 |
schorsch |
1.2 |
int al, |
| 212 |
|
|
register int pos |
| 213 |
|
|
) |
| 214 |
greg |
1.1 |
{ |
| 215 |
|
|
char obuf[32]; |
| 216 |
|
|
int len; |
| 217 |
|
|
|
| 218 |
|
|
while (al--) { |
| 219 |
|
|
sprintf(obuf, OUTFMT, *av++); |
| 220 |
|
|
pos += (len = strlen(obuf)+1); |
| 221 |
|
|
if (pos >= 64) { |
| 222 |
|
|
putchar('\n'); |
| 223 |
|
|
pos = len; |
| 224 |
|
|
} |
| 225 |
|
|
fputs(obuf, stdout); |
| 226 |
|
|
if (al) |
| 227 |
|
|
putchar(','); |
| 228 |
|
|
} |
| 229 |
|
|
} |