spline.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /* spline.c -- Do spline interpolation. */
  2. /******************************************************************************
  3. David L. Fox
  4. 2140 Braun Dr.
  5. Golden, CO 80401
  6. This program has been placed in the public domain by its author.
  7. Version Date Change
  8. 1.1 17 Dec 1985 Modify getdata() to realloc() more memory if needed.
  9. 1.0 14 May 1985
  10. spline [options] [file]
  11. Spline reads pairs of numbers from file (or the standard input, if file is missing).
  12. The pairs are interpreted as abscissas and ordinates of a function. The output of
  13. spline consists of similar pairs generated from the input by interpolation with
  14. cubic splines. (See R. W. Hamming, Numerical Methods for Scientists and Engineers,
  15. 2nd ed., pp. 349ff.) There are sufficiently many points in the output to appear
  16. smooth when plotted (e.g., by graph(1)). The output points are approximately evenly
  17. spaced and include the input points.
  18. There may be one or more options of the form: -o [argument [arg2] ].
  19. The available options are:
  20. -a Generate abscissa values automatically. The input consists of a list of
  21. ordinates. The generated abscissas are evenly spaced with spacing given by
  22. the argument, or 1 if the next argument is not a number.
  23. -f Set the first derivative of the spline function at the left and right end
  24. points to the first and second arguments following -f. If only one numerical
  25. argument follows -f then that value is used for both left and right end points.
  26. -k The argument of the k option is the value of the constant used to calculate
  27. the boundary values by y''[0] = ky''[1] and y''[n] = ky''[n-1]. The default
  28. value is k = 0.
  29. -m The argument gives the maximum number of input data points. The default is 1000.
  30. If the input contains more than this many points additional memory will be
  31. allocated. This option may be used to slightly increase efficiency for large
  32. data sets or reduce the amount of memory required for small data sets.
  33. -n The number of output points is given by the argument. The default value is 100.
  34. -p The splines used for interpolation are forced to be periodic, i.e. y'[0] = y'[n].
  35. The first and last ordinates should be equal.
  36. -s Set the second derivative of the spline function at the left and right end
  37. points to the first and second arguments following -s. If only one numerical
  38. argument follows -s then that value is used for both left and right end points.
  39. -x The argument (and arg2, if present) are the lower (and upper) limits on the
  40. abscissa values in the output. If the x option is not specified these limits
  41. are calculated from the data. Automatic abscissas start at the lower limit
  42. (default 0).
  43. The data need not be monotonic in x but all the x values must be distinct.
  44. Non-numeric data in the input is ignored.
  45. ******************************************************************************/
  46. #include <a:stdio.h>
  47. #include <ctype.h>
  48. /* The constant DOUBLE is a compile time switch.
  49. If #define DOUBLE appears here double pecision variables are
  50. used to store all data and parameters.
  51. Otherwise, float variables are used for the data.
  52. For most smoothing and and interpolation applications single
  53. precision is more than adequate.
  54. Double precision is used to solve the system of linear equations
  55. in either case.
  56. */
  57. /* #define DOUBLE */
  58. #ifdef DOUBLE
  59. #define double real; /* Type used for data storage. */
  60. #define IFMT "%F" /* Input format. */
  61. #define OFMT "%18.14g %18.14g\n" /* Output format. */
  62. #else
  63. #define float real; /* Type used for data storage. */
  64. #define IFMT "%f" /* Input format. */
  65. #define OFMT "%8.5g %8.5g\n" /* Output format. */
  66. #endif
  67. /* Numerical constants: These may be machine and/or precision dependent. */
  68. #define HUGE (1.e38) /* Largest floating point number. */
  69. #define EPS 1.e-5 /* Test for zero when solving linear equations. */
  70. /* Default parameters */
  71. #define NPTS 1000
  72. #define NINT 100
  73. #define DEFSTEP 1. /* Default step size for automatic abcissas. */
  74. #define DEFBVK 0. /* Default boundary value constant. */
  75. /* Boundary condition types. */
  76. #define EXTRAP 0 /* Extrapolate second derivative:
  77. y''(0) = k * y''(1), y''(n) = k * y''(n-1) */
  78. #define FDERIV 1 /* Fixed first derivatives y'(0) and y'(n). */
  79. #define SDERIV 2 /* Fixed second derivatives y''(0) and y''(n). */
  80. #define PERIOD 3 /* Periodic: derivatives equal at end points. */
  81. /* Token types for command line processing. */
  82. #define OPTION 1
  83. #define NUMBER 2
  84. #define OTHER 3
  85. /* Define error numbers. */
  86. #define MEMERR 1
  87. #define NODATA 2
  88. #define BADOPT 4
  89. #define BADFILE 5
  90. #define XTRAARG 6
  91. #define XTRAPTS 7
  92. #define SINGERR 8
  93. #define DUPX 9
  94. #define BADBC 10
  95. #define RANGERR 11
  96. /* Constants and flags are global. */
  97. int aflag = FALSE; /* Automatic abscissa flag. */
  98. real step = DEFSTEP; /* Spacing for automatic abscissas. */
  99. int bdcode = EXTRAP; /* Type of boundary conditions:
  100. 0 extrapolate f'' with coeficient k.
  101. 1 first derivatives given
  102. 2 second derivatives given
  103. 3 periodic */
  104. real leftd = 0., /* Boundary values of derivatives. */
  105. rightd = 0.;
  106. real k = DEFBVK; /* Boundary value constant. */
  107. int rflag = 0; /* 0: take range from data, 1: min. given, 2: min. & max. given. */
  108. real xmin = HUGE, xmax; /* Output range. */
  109. unsigned nint = NINT; /* Number of intervals in output. */
  110. unsigned maxpts = NPTS; /* Maximun number of points. */
  111. unsigned nknots = 0; /* Number of input points. */
  112. int sflag = FALSE; /* If TRUE data must be sorted. */
  113. char *datafile; /* Name of data file */
  114. int xcompare(); /* Function to compare x values of two data points. */
  115. struct pair {
  116. real x, y;
  117. } *data; /* Points to list of data points. */
  118. struct bandm {
  119. double diag, upper, rhs;
  120. } *eqns; /* Points to elements of band matrix used to calculate
  121. coefficients of the splines. */
  122. main(argc, argv)
  123. int argc;
  124. char **argv;
  125. {
  126. setup(argc, argv);
  127. if (nknots == 1) { /* Cannot interpolate with one data point. */
  128. printf(OFMT,data->x, data->y);
  129. exit(0);
  130. }
  131. if (sflag) /* Sort data if needed. */
  132. qsort(data, nknots, sizeof(struct pair), xcompare);
  133. calcspline();
  134. interpolate();
  135. }
  136. /* xcompare -- Compare abcissas of two data points (for qsort). */
  137. xcompare(arg1, arg2)
  138. struct pair *arg1, *arg2;
  139. {
  140. if (arg1->x > arg2->x)
  141. return(1);
  142. else if (arg1->x < arg2->x)
  143. return(-1);
  144. else
  145. return(0);
  146. }
  147. /* error -- Print error message and abort fatal errors. */
  148. error(errno, auxmsg)
  149. int errno;
  150. char *auxmsg;
  151. { char *msg;
  152. int fatal, usemsg;
  153. static char *usage =
  154. "usage: spline [options] [file]\noptions:\n",
  155. *options = "-a spacing\n-k const\n-n intervals\n-m points\n-p\n-x xmin xmax\n";
  156. fatal = TRUE; /* Default is fatal error. */
  157. usemsg = FALSE; /* Default no usage message. */
  158. fprintf(stderr, "spline: ");
  159. switch(errno) {
  160. case MEMERR:
  161. msg = "not enough memory for %u data points\n";
  162. break;
  163. case NODATA:
  164. msg = "data file %s empty\n";
  165. break;
  166. case BADOPT:
  167. msg = "unknown option: %c\n";
  168. usemsg = TRUE;
  169. break;
  170. case BADFILE:
  171. msg = "cannot open file: %s\n";
  172. break;
  173. case XTRAARG:
  174. msg = "extra argument ignored: %s\n";
  175. fatal = FALSE;
  176. usemsg = TRUE;
  177. break;
  178. case XTRAPTS:
  179. fatal = FALSE;
  180. msg = "%s";
  181. break;
  182. case DUPX:
  183. msg = "duplicate abcissa value: %s\n";
  184. break;
  185. case RANGERR:
  186. msg = "xmax < xmin not allowed %s\n";
  187. break;
  188. /* The following errors "can't happen." */
  189. /* If they occur some sort of bug is indicated. */
  190. case SINGERR:
  191. msg = "singular matrix encountered %s\n";
  192. break;
  193. case BADBC:
  194. msg = "internal error: bad boundary value code %d\n";
  195. break;
  196. default:
  197. fprintf(stderr, "unknown error number: %d\n", errno);
  198. exit(1);
  199. }
  200. fprintf(stderr, msg, auxmsg);
  201. if (usemsg)
  202. fprintf(stderr,"%s%s", usage, options);
  203. if (fatal)
  204. exit(1);
  205. }
  206. /* setup -- Initalize all constants and read data. */
  207. setup(argc, argv)
  208. int argc;
  209. char **argv;
  210. { char *malloc();
  211. FILE fdinp, /* Source of input. */
  212. doarg();
  213. fdinp = doarg(argc, argv); /* Process command line arguments. */
  214. /* Allocate memory for data and band matrix of coefficients. */
  215. if ((data = malloc(maxpts*sizeof(struct pair))) == NULL) {
  216. error(MEMERR, (char *)maxpts);
  217. }
  218. getdata(fdinp); /* Read data from fdinp. */
  219. if (fdinp != stdin)
  220. fclose(fdinp); /* Close input data file. */
  221. if (nknots == 0) {
  222. error(NODATA, datafile);
  223. }
  224. /* Allocate memory for calculation of spline coefficients. */
  225. if ((eqns = malloc((nknots+1)*sizeof(struct bandm))) == NULL) {
  226. error(MEMERR, (char *)nknots);
  227. }
  228. }
  229. /* doarg -- Process arguments. */
  230. FILE
  231. doarg(argc, argv)
  232. int argc;
  233. char **argv;
  234. { int i, type;
  235. double atof();
  236. char *s, str[15];
  237. FILE fdinp;
  238. s = argv[i=1];
  239. type = gettok(&i, &s, argv, str);
  240. do {
  241. if (type == OPTION) {
  242. switch(*str) {
  243. case 'a': /* Automatic abscissa. */
  244. aflag = TRUE;
  245. rflag = rflag < 2 ? 1 : rflag;
  246. if (xmin == HUGE) /* Initialize xmin, if needed. */
  247. xmin = 0.;
  248. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  249. if ((step = atof(str)) <= 0.)
  250. error(RANGERR,"");
  251. type = gettok(&i, &s, argv, str);
  252. }
  253. break;
  254. case 'f': /* Fix first derivative at boundaries. */
  255. bdcode = FDERIV;
  256. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  257. leftd = atof(str);
  258. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  259. rightd = atof(str);
  260. type = gettok(&i, &s, argv, str);
  261. }
  262. else {
  263. rightd = leftd;
  264. }
  265. }
  266. break;
  267. case 'k': /* Set boundary value constant. */
  268. bdcode = EXTRAP;
  269. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  270. k = atof(str);
  271. type = gettok(&i, &s, argv, str);
  272. }
  273. break;
  274. case 'm': /* Set number of intervals in output. */
  275. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  276. maxpts = (unsigned)atoi(str);
  277. type = gettok(&i, &s, argv, str);
  278. }
  279. break;
  280. case 'n': /* Set number of intervals in output. */
  281. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  282. nint = (unsigned)atoi(str);
  283. type = gettok(&i, &s, argv, str);
  284. }
  285. break;
  286. case 'p': /* Require periodic interpolation function. */
  287. bdcode = PERIOD;
  288. type = gettok(&i, &s, argv, str);
  289. break;
  290. case 's': /* Fix second derivative at boundaries. */
  291. bdcode = SDERIV;
  292. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  293. leftd = atof(str);
  294. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  295. rightd = atof(str);
  296. type = gettok(&i, &s, argv, str);
  297. }
  298. else {
  299. rightd = leftd;
  300. }
  301. }
  302. break;
  303. case 'x': /* Set range of x values. */
  304. rflag = 1;
  305. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  306. xmin = atof(str);
  307. if ((type = gettok(&i, &s, argv, str)) == NUMBER) {
  308. xmax = atof(str);
  309. rflag = 2;
  310. type = gettok(&i, &s, argv, str);
  311. if (xmax <= xmin)
  312. error(RANGERR, "");
  313. }
  314. }
  315. break;
  316. default:
  317. error(BADOPT, (char *)argv[i][1]);
  318. }
  319. }
  320. else {
  321. if (argc > i) {
  322. datafile = argv[i];
  323. if ((fdinp = fopen(argv[i++], "r")) == NULL) {
  324. error(BADFILE, argv[i-1]);
  325. }
  326. if (argc > i)
  327. error(XTRAARG, argv[i]);
  328. }
  329. else
  330. fdinp = stdin;
  331. break;
  332. }
  333. } while (i < argc);
  334. return fdinp;
  335. }
  336. /* gettok -- Get one token from command line, return type. */
  337. gettok(indexp, locp, argv, str)
  338. int *indexp; /* Pointer to index in argv array. */
  339. char **locp; /* Pointer to current location in argv[*indexp]. */
  340. char **argv;
  341. char *str;
  342. { char *s;
  343. char *strcpy(), *strchr();
  344. int type;
  345. s = *locp;
  346. while (isspace(*s) || *s == ',')
  347. ++s;
  348. if (*s == '\0') /* Look at next element in argv. */
  349. s = argv[++*indexp];
  350. if (*s == '-' && isalpha(s[1])) {
  351. /* Found an option. */
  352. *str = *++s;
  353. str[1] = '\0';
  354. ++s;
  355. type = OPTION;
  356. }
  357. else if (isnumber(s)) {
  358. while (isnumber(s))
  359. *str++ = *s++;
  360. *str = '\0';
  361. type = NUMBER;
  362. }
  363. else {
  364. strcpy(str, s);
  365. s = strchr(s, '\0');
  366. type = OTHER;
  367. }
  368. *locp = s;
  369. return(type);
  370. }
  371. /* isnumber -- Return TRUE if argument is the ASCII representation of a number. */
  372. isnumber(string)
  373. char *string;
  374. {
  375. if (isdigit(*string) ||
  376. *string == '.' ||
  377. *string == '+' ||
  378. (*string == '-' && (isdigit(string[1]) || string[1] == '.')))
  379. return(TRUE);
  380. else
  381. return(FALSE);
  382. }
  383. /* getdata -- Read data points from fdinp. */
  384. getdata(fdinp)
  385. FILE fdinp;
  386. { int n, i;
  387. real lastx, min, max;
  388. struct pair *dp;
  389. char msg[60], *realloc();
  390. nknots = 0;
  391. lastx = -HUGE;
  392. dp = data; /* Point to head of list of data. */
  393. min = HUGE;
  394. max = -HUGE;
  395. do {
  396. if (aflag) { /* Generate abcissa. */
  397. dp->x = xmin + nknots*step;
  398. n = 0;
  399. }
  400. else { /* Read abcissa. */
  401. while ((n = (fscanf(fdinp,IFMT,&dp->x))) == 0)
  402. ; /* Skip non-numeric input. */
  403. }
  404. if (n == 1) {
  405. if (min > dp->x)
  406. min = dp->x;
  407. if (max < dp->x)
  408. max = dp->x;
  409. if (lastx > dp->x) { /* Check for monotonicity. */
  410. sflag = TRUE; /* Data must be sorted. */
  411. }
  412. lastx = dp->x;
  413. }
  414. /* Read ordinate. */
  415. while ((n = (fscanf(fdinp, IFMT, &dp->y))) == 0)
  416. ; /* Skip non-numeric input. */
  417. if (++nknots >= maxpts) { /* Too many points, allocate more memory. */
  418. if ((data = realloc(data, (maxpts *= 2)*sizeof(struct pair))) == NULL) {
  419. error(MEMERR, (char *)maxpts);
  420. }
  421. dp = data + nknots;
  422. sprintf(msg, "more than %d input points, more memory allocated\n",
  423. maxpts/2);
  424. error(XTRAPTS,msg);
  425. }
  426. else
  427. ++dp;
  428. } while (n == 1);
  429. --nknots;
  430. if (aflag) { /* Compute maximum ordinate. */
  431. max = xmin + (nknots-1)*step;
  432. }
  433. if (rflag < 2) {
  434. xmax = max;
  435. if (rflag < 1)
  436. xmin = min;
  437. }
  438. }
  439. /* calcspline -- Calculate coeficients of spline functions. */
  440. calcspline()
  441. {
  442. calccoef();
  443. solvband();
  444. }
  445. /* calccoef -- Calculate coefficients of linear equations determining spline functions. */
  446. calccoef()
  447. { int i, j;
  448. struct bandm *ptr, tmp1, tmp2;
  449. double h, h1;
  450. char str[10];
  451. ptr = eqns;
  452. /* Initialize first row of matrix. */
  453. if ((h1 = data[1].x - data[0].x) == 0.) {
  454. sprintf(str, "%8.5g", data[0].x);
  455. error(DUPX, str);
  456. }
  457. switch(bdcode) { /* First equation depends on boundary conditions. */
  458. case EXTRAP:
  459. ptr->upper = -k;
  460. ptr->diag = 1.;
  461. ptr->rhs = 0.;
  462. break;
  463. case FDERIV: /* Fixed first derivatives at ends. */
  464. ptr->upper = 1.;
  465. ptr->diag = 2.;
  466. h = data[1].x - data[0].x;
  467. ptr->rhs = 6.*((data[1].y - data[0].y)/(h*h) - leftd/h);
  468. break;
  469. case SDERIV:
  470. ptr->upper = 0.;
  471. ptr->diag = 1.;
  472. ptr->rhs = leftd;
  473. break;
  474. case PERIOD: /* Periodic splines. */
  475. ptr->upper = 1.;
  476. ptr->diag = 2.;
  477. break;
  478. default:
  479. error(BADBC, (char *) bdcode);
  480. }
  481. ++ptr;
  482. /* Initialize rows 1 to n-1, sub-diagonal band is assumed to be 1. */
  483. for (i=1; i < nknots-1; ++i, ++ptr) {
  484. h = h1;
  485. if ((h1 = data[i+1].x - data[i].x) == 0.) {
  486. sprintf(str, "%8.5g", data[i].x);
  487. error(DUPX, str);
  488. }
  489. ptr->diag = 2.*(h + h1)/h;
  490. ptr->upper = h1/h;
  491. ptr->rhs = 6.*((data[i+1].y-data[i].y)/(h*h1) -
  492. (data[i].y - data[i-1].y)/(h*h));
  493. }
  494. /* Initialize last row. */
  495. switch(bdcode) {
  496. case EXTRAP: /* Standard case. */
  497. ptr->diag = 1.;
  498. ptr->upper = -k; /* Upper holds actual sub-diagonal value. */
  499. ptr->rhs = 0.;
  500. break;
  501. case FDERIV: /* Fixed first derivatives at ends. */
  502. ptr->upper = 1.;
  503. ptr->diag = 2.;
  504. h = data[nknots-1].x - data[nknots-2].x;
  505. ptr->rhs = 6.*((data[nknots-2].y - data[nknots-1].y)/(h*h) + rightd/h);
  506. break;
  507. case SDERIV:
  508. ptr->upper = 0.;
  509. ptr->diag = 1.;
  510. ptr->rhs = rightd;
  511. break;
  512. case PERIOD: /* Use periodic boundary conditions. */
  513. /* First and last row are not in tridiagonal form. */
  514. h = data[1].x - data[0].x;
  515. h1 = data[nknots-1].x - data[nknots-2].x;
  516. ptr->diag = 1.;
  517. ptr->upper = 0.;
  518. tmp1.diag = -1.;
  519. tmp1.upper = 0;
  520. tmp1.rhs = 0.;
  521. tmp2.diag = 2.*h1/h;
  522. tmp2.upper = h1/h;
  523. tmp2.rhs = 6.*((data[1].y - data[0].y)/(h*h) -
  524. (data[nknots-1].y - data[nknots-2].y)/(h1*h));
  525. /* Transform periodic boundary equations to tri-diagonal form. */
  526. for (i = 1; i < nknots - 1; ++i) {
  527. tmp1.upper /= tmp1.diag;
  528. tmp1.rhs /= tmp1.diag;
  529. ptr->diag /= tmp1.diag;
  530. ptr->upper /= tmp1.diag;
  531. tmp1.diag = tmp1.upper - eqns[i].diag;
  532. tmp1.upper = -eqns[i].upper;
  533. tmp1.rhs -= eqns[i].rhs;
  534. tmp2.upper /= tmp2.diag;
  535. tmp2.rhs /= tmp2.diag;
  536. eqns->diag /= tmp2.diag;
  537. eqns->upper /= tmp2.diag;
  538. tmp2.diag = tmp2.upper - eqns[nknots-1-i].diag/eqns[nknots-1-i].upper;
  539. tmp2.upper = -1./eqns[nknots-1-i].upper;
  540. tmp2.rhs -= eqns[nknots-1-i].rhs/eqns[nknots-1-i].upper;
  541. }
  542. /* Add in remaining terms of boundary condition equation. */
  543. ptr->upper += tmp1.diag;
  544. ptr->diag += tmp1.upper;
  545. ptr->rhs = tmp1.rhs;
  546. eqns->diag += tmp2.upper;
  547. eqns->upper += tmp2.diag;
  548. eqns->rhs = tmp2.rhs;
  549. break;
  550. default:
  551. error(BADBC, (char *) bdcode);
  552. }
  553. }
  554. /* solvband -- Solve band matrix for spline functions. */
  555. solvband()
  556. { int i, flag;
  557. struct bandm *ptr;
  558. double k1;
  559. double fabs();
  560. int fcompare();
  561. ptr = eqns;
  562. flag = FALSE;
  563. /* Make a pass to triangularize matrix. */
  564. for (i=1; i < nknots - 1; ++i, ++ptr) {
  565. if (fabs(ptr->diag) < EPS) {
  566. /* Near zero on diagonal, pivot. */
  567. if (fabs(ptr->upper) < EPS)
  568. error(SINGERR, "");
  569. flag = TRUE;
  570. ptr->diag = i; /* Keep row index in diag.
  571. Actual value of diag is always 1. */
  572. if (i == nknots - 2) {
  573. flag = 2;
  574. /* Exchange next to last and last rows. */
  575. k1 = ptr->rhs/ptr->upper;
  576. if (fabs((ptr+1)->upper) < EPS)
  577. error(SINGERR, "");
  578. ptr->rhs = (ptr+1)->rhs/(ptr+1)->upper;
  579. ptr->upper = (ptr+1)->diag/(ptr+1)->upper;
  580. (ptr+1)->upper = 0.;
  581. (ptr+1)->rhs = k1;
  582. }
  583. else {
  584. ptr->rhs = (ptr+1)->rhs - (k1 = ptr->rhs/ptr->upper)*(ptr+1)->diag;
  585. ptr->upper = (ptr+1)->upper; /* This isn't super-diagonal element
  586. but rather one to its right.
  587. Must watch for this when
  588. back substituting. */
  589. (++ptr)->diag = i-1;
  590. ++i;
  591. ptr->upper = 0;
  592. ptr->rhs = k1;
  593. (ptr+1)->rhs -= ptr->rhs;
  594. }
  595. }
  596. else {
  597. ptr->upper /= ptr->diag;
  598. ptr->rhs /= ptr->diag;
  599. ptr->diag = i-1; /* Used to reorder solution if needed. */
  600. (ptr+1)->diag -= ptr->upper;
  601. (ptr+1)->rhs -= ptr->rhs;
  602. }
  603. }
  604. /* Last row is a special case. */
  605. if (flag != 2) {
  606. /* If flag == 2 last row is already computed. */
  607. ptr->upper /= ptr->diag;
  608. ptr->rhs /= ptr->diag;
  609. ptr->diag = ptr - eqns;
  610. ++ptr;
  611. ptr->diag -= (ptr-1)->upper * ptr->upper;
  612. ptr->rhs -= (ptr-1)->rhs * ptr->upper;
  613. ptr->rhs /= ptr->diag;
  614. ptr->diag = ptr - eqns;
  615. }
  616. /* Now make a pass back substituting for solution. */
  617. --ptr;
  618. for ( ; ptr >= eqns; --ptr) {
  619. if ((int)ptr->diag != ptr - eqns) {
  620. /* This row and one above have been exchanged in a pivot. */
  621. --ptr;
  622. ptr->rhs -= ptr->upper * (ptr+2)->rhs;
  623. }
  624. else
  625. ptr->rhs -= ptr->upper * (ptr+1)->rhs;
  626. }
  627. if (flag) { /* Undo reordering done by pivoting. */
  628. qsort(eqns, nknots, sizeof(struct bandm), fcompare);
  629. }
  630. }
  631. /* fcompare -- Compare two floating point numbers. */
  632. fcompare(arg1, arg2)
  633. real *arg1, *arg2;
  634. {
  635. if (arg1 > arg2)
  636. return(1);
  637. else if (arg1 < arg2)
  638. return(-1);
  639. else
  640. return(0);
  641. }
  642. /* interpolate -- Do spline interpolation. */
  643. interpolate()
  644. { int i;
  645. struct pair *dp;
  646. struct bandm *ep;
  647. real h, xi, yi, hi, xu, xl, limit;
  648. h = (xmax - xmin)/nint;
  649. ep = eqns;
  650. dp = data + 1;
  651. for (xi = xmin; xi < xmax + 0.25*h; xi += h) {
  652. while (dp->x < xi && dp < data + nknots - 1) {
  653. ++dp; /* Skip to correct interval. */
  654. ++ep;
  655. }
  656. if (dp < data + nknots - 1 && dp->x < xmax)
  657. limit = dp->x;
  658. else
  659. limit = xmax;
  660. for ( ; xi < limit - 0.25*h; xi += h) {
  661. /* Do interpolation. */
  662. hi = dp->x - (dp-1)->x;
  663. xu = dp->x - xi;
  664. xl = xi - (dp-1)->x;
  665. yi = ((ep+1)->rhs*xl*xl/(6.*hi) + dp->y/hi - (ep+1)->rhs*hi/6.)*xl +
  666. (ep->rhs*xu*xu/(6.*hi) + (dp-1)->y/hi - ep->rhs*hi/6.)*xu;
  667. printf(OFMT, xi, yi);
  668. }
  669. if (limit != dp->x) { /* Interpolate. */
  670. hi = dp->x - (dp-1)->x;
  671. xu = dp->x - xmax;
  672. xl = xmax - (dp-1)->x;
  673. yi = ((ep+1)->rhs*xl*xl/(6.*hi) + dp->y/hi - (ep+1)->rhs*hi/6.)*xl +
  674. (ep->rhs*xu*xu/(6.*hi) + (dp-1)->y/hi - ep->rhs*hi/6.)*xu;
  675. printf(OFMT, xmax, yi);
  676. }
  677. else { /* Print knot. */
  678. printf(OFMT, dp->x, dp->y);
  679. xi = dp->x;
  680. }
  681. }
  682. }