spline.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1989,
  2. 1990, 1991, 1995, 1996, 1997, 1998, 1999, 2000, 2005, 2008, 2009, Free
  3. Software Foundation, Inc.
  4. The GNU plotutils package is free software. You may redistribute it
  5. and/or modify it under the terms of the GNU General Public License as
  6. published by the Free Software foundation; either version 2, or (at your
  7. option) any later version.
  8. The GNU plotutils package is distributed in the hope that it will be
  9. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with the GNU plotutils package; see the file COPYING. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /* This program, spline, interpolates scalar or vector-valued input data
  17. using splines with tension, including piecewise cubic (zero-tension)
  18. splines. When acting as a real-time filter, it uses cubic Bessel
  19. interpolation instead. Written by Robert S. Maier
  20. <rsm@math.arizona.edu>, based on earlier work by Rich Murphey.
  21. References:
  22. D. Kincaid and [E.] W. Cheney, Numerical Analysis, Brooks/Cole,
  23. 2nd. ed., 1996, Section 6.4.
  24. C. de Boor, A Practical Guide to Splines, Springer-Verlag, 1978,
  25. Chapter 4.
  26. A. K. Cline, "Scalar and Planar-Valued Curve Fitting Using Splines under
  27. Tension", Communications of the ACM 17 (1974), 218-223.
  28. The tension in a spline is set with the -T (i.e., --tension) option. By
  29. definition, a one-dimensional spline with tension satisfies the
  30. differential equation y''''=sgn(tension)*(tension**2)y''. The default
  31. value for the tension is zero. If tension=0 then a spline with tension
  32. reduces to a conventional piecewise cubic spline. In the limits
  33. tension->+infinity and tension->-infinity, a spline with tension reduces
  34. to a piecewise linear (`broken line') interpolation.
  35. To oversimplify a bit, 1.0/tension is the maximum abscissa range over
  36. which the spline likes to curve, at least when tension>0. So increasing
  37. the tension far above zero tends to make the spline contain short curved
  38. sections, separated by sections that are almost straight. The curved
  39. sections will be centered on the user-specified data points. The
  40. behavior of the spline when tension<0 is altogether different: it will
  41. tend to oscillate, though as tension->-infinity the oscillations are
  42. damped out.
  43. Tension is a `dimensionful' quantity. If tension=0 (the cubic spline
  44. case), then the computation of the spline is scale-invariant. But if
  45. the tension is nonzero, then when the abscissa values are multiplied by
  46. some common positive factor, the tension should be divided by the same
  47. factor to obtain a scaled version of the original spline.
  48. The algorithms of Kincaid and Cheney have been extended to include
  49. support for periodicity. To obtain a periodic spline, with or without
  50. tension, the user uses the -p (i.e., --periodic) option and supplies
  51. input data satisfying y[n]=y[0]. Also, in the non-periodic case the
  52. algorithms have been extended to include support for a parameter k,
  53. which appears in the two boundary conditions y''[0]=ky''[1] and
  54. y''[n]=ky''[n-1]. The default value of k is 1.0. The parameter k,
  55. which is specified with the -k (i.e. --boundary-condition) option, is
  56. ignored for periodic splines (using the -k option with the -p option
  57. will elicit a warning).
  58. If the -f option is specified, then an altogether different (real-time)
  59. algorithm for generating interpolating points will be used, so that this
  60. program can be used as a real-time filter. If -f is specified then the
  61. -t option, otherwise optional, must also be used. (I.e., the minimum
  62. and maximum abscissa values for the interpolating points must be
  63. specified, and optionally the spacing between them as well. If the
  64. spacing is not specified on the command line, then the interval
  65. [tmin,tmax] will be subdivided into a default number of intervals [100],
  66. unless the default number of intervals is overridden with the -n option.
  67. The real-time algorithm that is used when the -f option is specified is
  68. cubic Bessel interpolation. (The -T, -p, and -k options are ignored
  69. when -f is specified; using them will elicit a warning.) Interpolation
  70. in this case is piecewise cubic, and the slopes at either end of each
  71. sub-interval are found by fitting a parabola through each successive
  72. triple of points. That is, the slope at t=t_n is found by fitting a
  73. parabola through the points at t_(n-1), t_n, and t_(n+1). This
  74. interpolation scheme yields a spline that is only once, rather than
  75. twice, continuously differentiable. However, it has the feature that
  76. all computations are local rather than global, so it is suitable for
  77. real-time work.
  78. Since the above was written, the -d option has been added, to permit the
  79. splining of multidimensional data. All components of a d-dimensional
  80. data set (a d-dimensional vector y is specified at each t) are splined
  81. in the same way, as if they were one-dimensional functions of t. All
  82. options that apply to 1-dimensional datasets, such as -T, -p, -k, -f,
  83. etc., apply to d-dimensional ones also. */
  84. #include "sys-defines.h"
  85. #include "libcommon.h"
  86. #include "getopt.h"
  87. /* states for cubic Bessel DFA; occupancy of data point queue */
  88. enum { STATE_ZERO, STATE_ONE, STATE_TWO, STATE_THREE };
  89. /* types of auto-abscissa */
  90. enum { AUTO_NONE, AUTO_INCREMENT, AUTO_BY_DISTANCE };
  91. #define FUZZ 0.0000001 /* potential roundoff error */
  92. /* Minimum value for magnitude of x, for such functions as x-sinh(x),
  93. x-tanh(x), x-sin(x), and x-tan(x) to have acceptable accuracy. If the
  94. magnitude of x is smaller than this value, these functions of x will be
  95. computed via power series to accuracy O(x**6). */
  96. #define TRIG_ARG_MIN 0.001
  97. /* Maximum value for magnitude of x, beyond which we approximate
  98. x/sinh(x) and x/tanh(x) by |x|exp(-|x|). */
  99. #define TRIG_ARG_MAX 50.0
  100. /* options */
  101. #define ARG_NONE 0
  102. #define ARG_REQUIRED 1
  103. #define ARG_OPTIONAL 2
  104. const char *optstring = "fpsAd:I:O:P:k:n:t:x:T:a::";
  105. struct option long_options[] =
  106. {
  107. {"no-of-intervals", ARG_REQUIRED, NULL, 'n'},
  108. {"periodic", ARG_NONE, NULL, 'p'},
  109. {"y-dimension", ARG_REQUIRED, NULL, 'd'},
  110. {"t-limits", ARG_REQUIRED, NULL, 't'}, /* 1 or 2 or 3 */
  111. {"t-limits", ARG_REQUIRED, NULL, 'x'}, /* obsolescent; hidden */
  112. {"tension", ARG_REQUIRED, NULL, 'T'},
  113. {"boundary-condition",ARG_REQUIRED, NULL, 'k'},
  114. {"auto-abscissa", ARG_OPTIONAL, NULL, 'a'}, /* 0 or 1 or 2 */
  115. {"auto-dist-abscissa",ARG_NONE, NULL, 'A'},
  116. {"filter", ARG_NONE, NULL, 'f'},
  117. {"precision", ARG_REQUIRED, NULL, 'P'},
  118. {"suppress-abscissa", ARG_NONE, NULL, 's'},
  119. /* ascii or double */
  120. {"input-type", ARG_REQUIRED, NULL, 'I'},
  121. {"output-type", ARG_REQUIRED, NULL, 'O'},
  122. /* Long options with no equivalent short option alias */
  123. {"version", ARG_NONE, NULL, 'V' << 8},
  124. {"help", ARG_NONE, NULL, 'h' << 8},
  125. {NULL, 0, 0, 0}
  126. };
  127. /* null-terminated list of options that we don't show to the user */
  128. const int hidden_options[] = { (int)'x', 0 };
  129. /* type of data in input and output streams */
  130. typedef enum
  131. {
  132. T_ASCII, T_SINGLE, T_DOUBLE, T_INTEGER
  133. }
  134. data_type;
  135. data_type input_type = T_ASCII;
  136. data_type output_type = T_ASCII;
  137. const char *progname = "spline"; /* name of this program */
  138. const char *written = "Written by Robert S. Maier and Rich Murphey.";
  139. const char *copyright = "Copyright (C) 2009 Free Software Foundation, Inc.";
  140. const char *usage_appendage = " [FILE]...\n\
  141. With no FILE, or when FILE is -, read standard input.\n";
  142. /* forward references */
  143. bool do_bessel (FILE *input, int ydimension, int auto_abscissa, double auto_t, double auto_delta, double first_t, double last_t, double spacing_t, int precision, bool suppress_abscissa);
  144. bool is_monotonic (int n, double *t);
  145. bool read_data (FILE *input, int *len, int *used, int auto_abscissa, double auto_t, double auto_delta, double **t, int ydimension, double **y, double **z);
  146. bool read_float (FILE *input, double *dptr);
  147. bool skip_whitespace (FILE *stream);
  148. bool write_point (double t, double *y, int ydimension, int precision, bool suppress_abscissa);
  149. double interpolate (int n, double *t, double *y, double *z, double x, double tension, bool periodic);
  150. double quotient_sin_func (double x, double y);
  151. double quotient_sinh_func (double x, double y);
  152. double sin_func (double x);
  153. double sinh_func (double x);
  154. double tan_func (double x);
  155. double tanh_func (double x);
  156. int read_point (FILE *input, double *t, double *y, int ydimension, bool *first_point, int auto_abscissa, double *auto_t, double auto_delta, double *stored);
  157. void do_bessel_range (double abscissa0, double abscissa1, double *value0, double *value1, double *slope0, double *slope1, double first_t, double last_t, double spacing_t, int ydimension, int precision, bool endit, bool suppress_abscissa);
  158. void do_spline (int used, int len, double **t, int ydimension, double **y, double **z, double tension, bool periodic, bool spec_boundary_condition, double boundary_condition, int precision, double first_t, double last_t, double spacing_t, int no_of_intervals, bool spec_first_t, bool spec_last_t, bool spec_spacing_t, bool spec_no_of_intervals, bool suppress_abscissa);
  159. void fit (int n, double *t, double *y, double *z, double k, double tension, bool periodic);
  160. void maybe_emit_oob_warning (void);
  161. void non_monotonic_error (void);
  162. void output_dataset_separator (void);
  163. void set_format_type (char *s, data_type *typep);
  164. int
  165. main (int argc, char *argv[])
  166. {
  167. int option;
  168. int opt_index;
  169. int errcnt = 0; /* errors encountered */
  170. bool show_version = false; /* remember to show version message */
  171. bool show_usage = false; /* remember to output usage message */
  172. bool dataset_follows;
  173. /* parameters controlled by command line options: */
  174. bool filter = false; /* act as a filter (cubic Bessel)? */
  175. bool periodic = false; /* spline should be periodic? */
  176. bool spec_boundary_condition = false; /* user-specified boundary cond'n? */
  177. bool spec_first_t = false, spec_last_t = false, spec_spacing_t = false;
  178. bool spec_no_of_intervals = false; /* user-specified number of intervals? */
  179. bool suppress_abscissa = false; /* for each point, print ordinate only? */
  180. double boundary_condition = 1.0; /* force y''_1 = k * y''_0, etc. */
  181. double delta_t = 1.0; /* increment of auto abscissa */
  182. double first_t = 0.0, last_t = 0.0, spacing_t = 0.0; /* values of limits */
  183. double tension = 0.0; /* `tension' parameter */
  184. double t_start = 0.0; /* start of auto abscissa */
  185. int auto_abscissa = AUTO_NONE; /* automatic generation of abscissa? */
  186. int no_of_intervals = 100; /* no. of intervals to divide abs. range */
  187. int precision = 6; /* default no. of significant digits printed */
  188. int ydimension = 1; /* dimension of each point's ordinate */
  189. /* used in argument parsing */
  190. double local_first_t, local_last_t, local_spacing_t;
  191. double local_t_start, local_delta_t;
  192. int local_precision;
  193. for ( ; ; )
  194. {
  195. option = getopt_long (argc, argv, optstring, long_options, &opt_index);
  196. if (option == 0)
  197. option = long_options[opt_index].val;
  198. switch (option)
  199. {
  200. /* ----------- options with no argument --------------*/
  201. case 'p': /* construct periodic, i.e., closed spline */
  202. periodic = true;
  203. break;
  204. case 'f': /* act as filter */
  205. filter = true;
  206. break;
  207. case 's': /* don't output t values */
  208. suppress_abscissa = true;
  209. break;
  210. case 'A': /* delta t = inter-y distance */
  211. auto_abscissa = AUTO_BY_DISTANCE;
  212. t_start = 0.0;
  213. break;
  214. case 'V' << 8: /* Version */
  215. show_version = true;
  216. break;
  217. case 'h' << 8: /* Help */
  218. show_usage = true;
  219. break;
  220. /*--------------options with a single argument--------*/
  221. case 'I':
  222. set_format_type (optarg, &input_type);
  223. break;
  224. case 'O':
  225. set_format_type (optarg, &output_type);
  226. break;
  227. case 'd': /* dimensionality of ordinate variable */
  228. if (sscanf (optarg, "%d", &ydimension) <= 0 || ydimension < 1)
  229. {
  230. fprintf (stderr,
  231. "%s: error: the ordinate dimension `%s' is bad (it should be a positive integer)\n",
  232. progname, optarg);
  233. errcnt++;
  234. }
  235. break;
  236. case 'k':
  237. if (sscanf (optarg, "%lf", &boundary_condition) <= 0)
  238. {
  239. fprintf (stderr,
  240. "%s: error: the boundary condition argument `%s' is bad\n",
  241. progname, optarg);
  242. errcnt++;
  243. }
  244. else
  245. spec_boundary_condition = true;
  246. break;
  247. case 'T':
  248. if (sscanf (optarg, "%lf", &tension) <= 0)
  249. {
  250. fprintf (stderr,
  251. "%s: error: the tension argument `%s' is bad\n",
  252. progname, optarg);
  253. errcnt++;
  254. }
  255. break;
  256. case 'n': /* number of intervals */
  257. if (sscanf (optarg, "%d", &no_of_intervals) <= 0)
  258. {
  259. fprintf (stderr,
  260. "%s: error: the requested number of intervals `%s' is bad\n",
  261. progname, optarg);
  262. errcnt++;
  263. }
  264. else
  265. spec_no_of_intervals = true;
  266. break;
  267. case 'P': /* precision */
  268. if (sscanf (optarg, "%d", &local_precision) <= 0)
  269. {
  270. fprintf (stderr, "%s: error: the requested precision `%s' is bad (it should be a positive integer)\n",
  271. progname, optarg);
  272. errcnt++;
  273. }
  274. else
  275. {
  276. if (local_precision <= 0)
  277. fprintf (stderr,
  278. "%s: the precision value `%s' is disregarded (it should be a positive integer)\n",
  279. progname, optarg);
  280. else
  281. precision = local_precision;
  282. }
  283. break;
  284. /*------------options with 0 or more args ----------*/
  285. case 'a': /* Auto-abscissa, ARG OPTIONAL [0,1,2] */
  286. auto_abscissa = AUTO_INCREMENT;
  287. if (optind >= argc)
  288. break;
  289. if (sscanf (argv[optind], "%lf", &local_delta_t) <= 0)
  290. break;
  291. delta_t = local_delta_t;
  292. optind++; /* tell getopt we recognized delta_t */
  293. if (optind >= argc)
  294. break;
  295. if (sscanf (argv [optind], "%lf", &local_t_start) <= 0)
  296. break;
  297. t_start = local_t_start;
  298. optind++; /* tell getopt we recognized t_start */
  299. break;
  300. /*--------------options with 1 or more arguments------*/
  301. case 't': /* t axis limits, ARG REQUIRED [1,2,3] */
  302. case 'x': /* obsolescent variant */
  303. if (sscanf (optarg, "%lf", &local_first_t) <= 0)
  304. break;
  305. first_t = local_first_t;
  306. spec_first_t = true;
  307. if (optind >= argc)
  308. break;
  309. if (sscanf (argv [optind], "%lf", &local_last_t) <= 0)
  310. break;
  311. last_t = local_last_t;
  312. spec_last_t = true;
  313. optind++; /* tell getopt we recognized last_t */
  314. if (optind >= argc)
  315. break;
  316. if (sscanf (argv [optind], "%lf", &local_spacing_t) <= 0)
  317. break;
  318. spacing_t = local_spacing_t;
  319. spec_spacing_t = true;
  320. optind++; /* tell getopt we recognized spacing_t */
  321. break;
  322. /*---------------- End of options ----------------*/
  323. default: /* Default, unknown option */
  324. errcnt++;
  325. break;
  326. } /* endswitch */
  327. if ((option == EOF))
  328. {
  329. errcnt--;
  330. break; /* break out of option processing */
  331. }
  332. }
  333. /* endwhile */
  334. if (errcnt > 0)
  335. {
  336. fprintf (stderr, "Try `%s --help' for more information\n", progname);
  337. return EXIT_FAILURE;
  338. }
  339. if (show_version)
  340. {
  341. display_version (progname, written, copyright);
  342. return EXIT_SUCCESS;
  343. }
  344. if (show_usage)
  345. {
  346. display_usage (progname, hidden_options, usage_appendage, 0);
  347. return EXIT_SUCCESS;
  348. }
  349. /* Some sanity checks on user-supplied options. */
  350. if (no_of_intervals < 1)
  351. {
  352. fprintf (stderr,
  353. "%s: error: the abscissa range cannot be subdivided into %d intervals\n",
  354. progname, no_of_intervals);
  355. return EXIT_FAILURE;
  356. }
  357. if (periodic)
  358. {
  359. if (spec_boundary_condition)
  360. fprintf (stderr,
  361. "%s: the setting of a boundary condition is not supported for a periodic spline\n",
  362. progname);
  363. boundary_condition = 0.0;
  364. }
  365. if (filter)
  366. /* acting as a filter, so use cubic Bessel interpolation */
  367. {
  368. if (!spec_first_t || !spec_last_t)
  369. {
  370. fprintf (stderr,
  371. "%s: error: acting as a filter, so the abscissa range should be specified with the -t option\n",
  372. progname);
  373. return EXIT_FAILURE;
  374. }
  375. if (!spec_spacing_t)
  376. spacing_t = (last_t - first_t) / no_of_intervals;
  377. else /* user specified spacing */
  378. {
  379. if (spec_no_of_intervals)
  380. fprintf (stderr, "%s: the requested number of intervals is disregarded\n",
  381. progname);
  382. if ((last_t - first_t) * spacing_t < 0.0)
  383. {
  384. fprintf (stderr, "%s: the requested spacing was of the wrong sign, so it has been corrected\n",
  385. progname);
  386. spacing_t = -spacing_t;
  387. }
  388. /* N.B. if spacing specified, should optionally contract first_t and
  389. last_t to make them integer multiples of spacing; cf. graph */
  390. }
  391. if (spec_boundary_condition)
  392. fprintf (stderr,
  393. "%s: acting as a filter, so the setting of a boundary condition is not supported\n",
  394. progname);
  395. if (tension != 0.0)
  396. fprintf (stderr,
  397. "%s: acting as a filter, so nonzero tension is not supported\n",
  398. progname);
  399. if (periodic)
  400. fprintf (stderr,
  401. "%s: acting as a filter, so periodicity is not supported\n",
  402. progname);
  403. if (optind < argc)
  404. {
  405. /* call do_bessel() on each file specified on the command line,
  406. generating a spline from each dataset in the file */
  407. for (; optind < argc; optind++)
  408. {
  409. FILE *data_file;
  410. /* open file, treating "-" as stdin */
  411. if (strcmp (argv[optind], "-") == 0)
  412. data_file = stdin;
  413. else
  414. {
  415. data_file = fopen (argv[optind], "r");
  416. if (data_file == NULL)
  417. {
  418. fprintf (stderr, "%s: %s: %s\n", progname, argv[optind], strerror(errno));
  419. return EXIT_FAILURE;
  420. }
  421. }
  422. /* loop through datasets in file (may be more than one) */
  423. do
  424. {
  425. dataset_follows = do_bessel (data_file, ydimension,
  426. auto_abscissa, t_start, delta_t,
  427. first_t, last_t, spacing_t,
  428. precision, suppress_abscissa);
  429. /* output a separator between successive datasets */
  430. if (dataset_follows || (optind + 1 != argc))
  431. output_dataset_separator();
  432. } while (dataset_follows);
  433. /* close file */
  434. if (data_file != stdin) /* don't close stdin */
  435. {
  436. if (fclose (data_file) < 0)
  437. {
  438. fprintf (stderr,
  439. "%s: error: the input file `%s' could not be closed\n",
  440. progname, argv[optind]);
  441. return EXIT_FAILURE;
  442. }
  443. }
  444. }
  445. }
  446. else /* no files spec'd, read stdin instead */
  447. /* loop through datasets read from stdin (may be more than one) */
  448. do
  449. {
  450. dataset_follows = do_bessel (stdin, ydimension,
  451. auto_abscissa, t_start, delta_t,
  452. first_t, last_t, spacing_t,
  453. precision, suppress_abscissa);
  454. /* output a separator between successive datasets */
  455. if (dataset_follows)
  456. output_dataset_separator();
  457. }
  458. while (dataset_follows); /* keep going if no EOF yet */
  459. }
  460. else
  461. /* not acting as filter, so use spline interpolation (w/ tension) */
  462. {
  463. double *t, **y, **z; /* ordinate, abscissa, 2nd derivative arrays */
  464. int i, len, used;
  465. if (optind < argc) /* files spec'd on command line */
  466. {
  467. /* call do_spline() on each file specified on the command line,
  468. generating a spline from each dataset contained in the file */
  469. for (; optind < argc; optind++)
  470. {
  471. FILE *data_file;
  472. /* open file, treat "-" as meaning stdin */
  473. if (strcmp (argv[optind], "-") == 0)
  474. data_file = stdin;
  475. else
  476. {
  477. data_file = fopen (argv[optind], "r");
  478. if (data_file == NULL)
  479. {
  480. fprintf (stderr, "%s: error: the file `%s' could not be opened\n",
  481. progname, argv[optind]);
  482. return EXIT_FAILURE;
  483. }
  484. }
  485. /* loop through datasets in file (may be more than one) */
  486. do
  487. {
  488. len = 16; /* initial value of storage length */
  489. used = -1; /* initial value of array size, minus 1 */
  490. t = (double *)xmalloc (sizeof(double) * len);
  491. y = (double **)xmalloc (sizeof(double *) * ydimension);
  492. z = (double **)xmalloc (sizeof(double *) * ydimension);
  493. for (i = 0; i < ydimension; i++)
  494. {
  495. y[i] = (double *)xmalloc (sizeof(double) * len);
  496. z[i] = (double *)xmalloc (sizeof(double) * len);
  497. }
  498. dataset_follows = read_data (data_file, &len, &used,
  499. auto_abscissa, t_start, delta_t,
  500. &t, ydimension, y, z);
  501. /* read_data() may reallocate t,y[*],z[*], and update
  502. len, used; on exit, used + 1 is number of data points */
  503. /* spline the dataset and output interpolating points */
  504. do_spline (used, len,
  505. &t, ydimension, y, z, tension, periodic,
  506. spec_boundary_condition, boundary_condition,
  507. precision,
  508. first_t, last_t, spacing_t, no_of_intervals,
  509. spec_first_t, spec_last_t, spec_spacing_t,
  510. spec_no_of_intervals, suppress_abscissa);
  511. /* output a separator between successive datasets */
  512. if (dataset_follows || (optind + 1 != argc))
  513. output_dataset_separator();
  514. free (z);
  515. free (y);
  516. free (t);
  517. }
  518. while (dataset_follows); /* keep going if no EOF yet */
  519. /* close file */
  520. if (data_file != stdin) /* don't close stdin */
  521. {
  522. if (fclose (data_file) < 0)
  523. {
  524. fprintf (stderr,
  525. "%s: error: the input file `%s' could not be closed\n",
  526. progname, argv[optind]);
  527. return EXIT_FAILURE;
  528. }
  529. }
  530. }
  531. }
  532. else /* no files spec'd, read stdin instead */
  533. /* loop through datasets read from stdin (may be more than one) */
  534. do
  535. {
  536. len = 16; /* initial value for array size */
  537. used = -1; /* initial number of stored points, minus 1 */
  538. t = (double *)xmalloc (sizeof(double) * len);
  539. y = (double **)xmalloc (sizeof(double *) * ydimension);
  540. z = (double **)xmalloc (sizeof(double *) * ydimension);
  541. for (i = 0; i < ydimension; i++)
  542. {
  543. y[i] = (double *)xmalloc (sizeof(double) * len);
  544. z[i] = (double *)xmalloc (sizeof(double) * len);
  545. }
  546. dataset_follows = read_data (stdin, &len, &used,
  547. auto_abscissa, t_start, delta_t,
  548. &t, ydimension, y, z);
  549. /* read_data() may reallocate t,y[*],z[*], and update len,
  550. used; on exit, used + 1 is number of data points */
  551. /* spline the dataset and output interpolating points */
  552. do_spline (used, len,
  553. &t, ydimension, y, z, tension, periodic,
  554. spec_boundary_condition, boundary_condition, precision,
  555. first_t, last_t, spacing_t, no_of_intervals,
  556. spec_first_t, spec_last_t, spec_spacing_t,
  557. spec_no_of_intervals, suppress_abscissa);
  558. /* output a separator between successive datasets */
  559. if (dataset_follows)
  560. output_dataset_separator();
  561. for (i = 0; i < ydimension; i++)
  562. {
  563. free (z[i]);
  564. free (y[i]);
  565. }
  566. free (z);
  567. free (y);
  568. free (t);
  569. }
  570. while (dataset_follows); /* keep going if no EOF yet */
  571. }
  572. return EXIT_SUCCESS;
  573. }
  574. void
  575. set_format_type (char *s, data_type *typep)
  576. {
  577. switch (s[0])
  578. {
  579. case 'a':
  580. case 'A':
  581. *typep = T_ASCII;
  582. break;
  583. case 'f':
  584. case 'F':
  585. *typep = T_SINGLE;
  586. break;
  587. case 'd':
  588. case 'D':
  589. *typep = T_DOUBLE;
  590. break;
  591. case 'i':
  592. case 'I':
  593. *typep = T_INTEGER;
  594. break;
  595. default:
  596. {
  597. fprintf (stderr, "%s: error: the data format type `%s' is invalid\n",
  598. progname, s);
  599. exit (EXIT_FAILURE);
  600. }
  601. break;
  602. }
  603. }
  604. /* fit() computes the array z[] of second derivatives at the knots, i.e.,
  605. internal data points. The abscissa array t[] and the ordinate array y[]
  606. are specified. On entry, have n+1 >= 2 points in the t, y, z arrays,
  607. numbered 0..n. The knots are numbered 1..n-1 as in Kincaid and Cheney.
  608. In the periodic case, the final knot, i.e., (t[n-1],y[n-1]), has the
  609. property that y[n-1]=y[0]; moreover, y[n]=y[1]. The number of points
  610. supplied by the user was n+1 in the non-periodic case, and n in the
  611. periodic case. When this function is called, n>=1 in the non-periodic
  612. case, and n>=2 in the periodic case. */
  613. /* Algorithm: the n-1 by n-1 tridiagonal matrix equation for the vector of
  614. 2nd derivatives at the knots is reduced to upper diagonal form. At that
  615. point the diagonal entries (pivots) of the upper diagonal matrix are in
  616. the vector u[], and the vector on the right-hand side is v[]. That is,
  617. the equation is of the form Ay'' = v, where a_(ii) = u[i], and a_(i,i+1)
  618. = alpha[i]. Here i=1..n-1 indexes the set of knots. The matrix
  619. equation is solved by back-substitution for y''[], i.e., for z[]. */
  620. /* ARGS: k = coeff in bdy condition y''_1 = k y''_0, etc. */
  621. void
  622. fit (int n, double *t, double *y, double *z, double k, double tension,
  623. bool periodic)
  624. {
  625. double *h, *b, *u, *v, *alpha, *beta;
  626. double *uu = NULL, *vv = NULL, *s = NULL;
  627. int i;
  628. if (n == 1) /* exactly 2 points, use straight line */
  629. {
  630. z[0] = z[1] = 0.0;
  631. return;
  632. }
  633. h = (double *)xmalloc (sizeof(double) * n);
  634. b = (double *)xmalloc (sizeof(double) * n);
  635. u = (double *)xmalloc (sizeof(double) * n);
  636. v = (double *)xmalloc (sizeof(double) * n);
  637. alpha = (double *)xmalloc (sizeof(double) * n);
  638. beta = (double *)xmalloc (sizeof(double) * n);
  639. if (periodic)
  640. {
  641. s = (double *)xmalloc (sizeof(double) * n);
  642. uu = (double *)xmalloc (sizeof(double) * n);
  643. vv = (double *)xmalloc (sizeof(double) * n);
  644. }
  645. for (i = 0; i <= n - 1 ; ++i)
  646. {
  647. h[i] = t[i + 1] - t[i];
  648. b[i] = 6.0 * (y[i + 1] - y[i]) / h[i]; /* for computing RHS */
  649. }
  650. if (tension < 0.0) /* must rule out sin(tension * h[i]) = 0 */
  651. {
  652. for (i = 0; i <= n - 1 ; ++i)
  653. if (sin (tension * h[i]) == 0.0)
  654. {
  655. fprintf (stderr, "%s: error: the specified negative tension value is singular\n", progname);
  656. exit (EXIT_FAILURE);
  657. }
  658. }
  659. if (tension == 0.0)
  660. {
  661. for (i = 0; i <= n - 1 ; ++i)
  662. {
  663. alpha[i] = h[i]; /* off-diagonal = alpha[i] to right */
  664. beta[i] = 2.0 * h[i]; /* diagonal = beta[i-1] + beta[i] */
  665. }
  666. }
  667. else
  668. if (tension > 0.0)
  669. /* `positive' (really real) tension, use hyperbolic trig funcs */
  670. {
  671. for (i = 0; i <= n - 1 ; ++i)
  672. {
  673. double x = tension * h[i];
  674. double xabs = (x < 0.0 ? -x : x);
  675. if (xabs < TRIG_ARG_MIN)
  676. /* hand-compute (6/x^2)(1-x/sinh(x)) and (3/x^2)(x/tanh(x)-1)
  677. to improve accuracy; here `x' is tension * h[i] */
  678. {
  679. alpha[i] = h[i] * sinh_func(x);
  680. beta[i] = 2.0 * h[i] * tanh_func(x);
  681. }
  682. else if (xabs > TRIG_ARG_MAX)
  683. /* in (6/x^2)(1-x/sinh(x)) and (3/x^2)(x/tanh(x)-1),
  684. approximate x/sinh(x) and x/tanh(x) by 2|x|exp(-|x|)
  685. and |x|, respectively */
  686. {
  687. int sign = (x < 0.0 ? -1 : 1);
  688. alpha[i] = ((6.0 / (tension * tension))
  689. * ((1.0 / h[i]) - tension * 2 * sign * exp(-xabs)));
  690. beta[i] = ((6.0 / (tension * tension))
  691. * (tension - (1.0 / h[i])));
  692. }
  693. else
  694. {
  695. alpha[i] = ((6.0 / (tension * tension))
  696. * ((1.0 / h[i]) - tension / sinh(x)));
  697. beta[i] = ((6.0 / (tension * tension))
  698. * (tension / tanh(x) - (1.0 / h[i])));
  699. }
  700. }
  701. }
  702. else /* tension < 0 */
  703. /* `negative' (really imaginary) tension, use circular trig funcs */
  704. {
  705. for (i = 0; i <= n - 1 ; ++i)
  706. {
  707. double x = tension * h[i];
  708. double xabs = (x < 0.0 ? -x : x);
  709. if (xabs < TRIG_ARG_MIN)
  710. /* hand-compute (6/x^2)(1-x/sin(x)) and (3/x^2)(x/tan(x)-1)
  711. to improve accuracy; here `x' is tension * h[i] */
  712. {
  713. alpha[i] = h[i] * sin_func(x);
  714. beta[i] = 2.0 * h[i] * tan_func(x);
  715. }
  716. else
  717. {
  718. alpha[i] = ((6.0 / (tension * tension))
  719. * ((1.0 / h[i]) - tension / sin(x)));
  720. beta[i] = ((6.0 / (tension * tension))
  721. * (tension / tan(x) - (1.0 / h[i])));
  722. }
  723. }
  724. }
  725. if (!periodic && n == 2)
  726. u[1] = beta[0] + beta[1] + 2 * k * alpha[0];
  727. else
  728. u[1] = beta[0] + beta[1] + k * alpha[0];
  729. v[1] = b[1] - b[0];
  730. if (u[1] == 0.0)
  731. {
  732. fprintf (stderr,
  733. "%s: error: as posed, the problem of computing a spline is singular\n",
  734. progname);
  735. exit (EXIT_FAILURE);
  736. }
  737. if (periodic)
  738. {
  739. s[1] = alpha[0];
  740. uu[1] = 0.0;
  741. vv[1] = 0.0;
  742. }
  743. for (i = 2; i <= n - 1 ; ++i)
  744. {
  745. u[i] = (beta[i] + beta[i - 1]
  746. - alpha[i - 1] * alpha[i - 1] / u[i - 1]
  747. + (i == n - 1 ? k * alpha[n - 1] : 0.0));
  748. if (u[i] == 0.0)
  749. {
  750. fprintf (stderr,
  751. "%s: error: as posed, the problem of computing a spline is singular\n",
  752. progname);
  753. exit (EXIT_FAILURE);
  754. }
  755. v[i] = b[i] - b[i - 1] - alpha[i - 1] * v[i - 1] / u[i - 1];
  756. if (periodic)
  757. {
  758. s[i] = - s[i-1] * alpha[i-1] / u[i-1];
  759. uu[i] = uu[i-1] - s[i-1] * s[i-1] / u[i-1];
  760. vv[i] = vv[i-1] - v[i-1] * s[i-1] / u[i-1];
  761. }
  762. }
  763. if (!periodic)
  764. {
  765. /* fill in 2nd derivative array */
  766. z[n] = 0.0;
  767. for (i = n - 1; i >= 1; --i)
  768. z[i] = (v[i] - alpha[i] * z[i + 1]) / u[i];
  769. z[0] = 0.0;
  770. /* modify to include boundary condition */
  771. z[0] = k * z[1];
  772. z[n] = k * z[n - 1];
  773. }
  774. else /* periodic */
  775. {
  776. z[n-1] = (v[n-1] + vv[n-1]) / (u[n-1] + uu[n-1] + 2 * s[n-1]);
  777. for (i = n - 2; i >= 1; --i)
  778. z[i] = ((v[i] - alpha[i] * z[i + 1]) - s[i] * z[n-1]) / u[i];
  779. z[0] = z[n-1];
  780. z[n] = z[1];
  781. }
  782. if (periodic)
  783. {
  784. free (vv);
  785. free (uu);
  786. free (s);
  787. }
  788. free (beta);
  789. free (alpha);
  790. free (v);
  791. free (u);
  792. free (b);
  793. free (h);
  794. }
  795. /* interpolate() computes an approximate ordinate value for a given
  796. abscissa value, given an array of data points (stored in t[] and y[],
  797. containing abscissa and ordinate values respectively), and z[], the
  798. array of 2nd derivatives at the knots (i.e. internal data points).
  799. On entry, have n+1 >= 2 points in the t, y, z arrays, numbered 0..n.
  800. The number of knots (i.e. internal data points) is n-1; they are
  801. numbered 1..n-1 as in Kincaid and Cheney. In the periodic case, the
  802. final knot, i.e., (t[n-1],y[n-1]), has the property that y[n-1]=y[0];
  803. also, y[n]=y[1]. The number of data points supplied by the user was n+1
  804. in the non-periodic case, and n in the periodic case. When this
  805. function is called, n>=1 in the non-periodic case, and n>=2 in the
  806. periodic case. */
  807. double
  808. interpolate (int n, double *t, double *y, double *z, double x,
  809. double tension, bool periodic)
  810. {
  811. double diff, updiff, reldiff, relupdiff, h;
  812. double value;
  813. int is_ascending = (t[n-1] < t[n]);
  814. int i = 0, k;
  815. /* in periodic case, map x to t[0] <= x < t[n] */
  816. if (periodic && (x - t[0]) * (x - t[n]) > 0.0)
  817. x -= ((int)(floor( (x - t[0]) / (t[n] - t[0]) )) * (t[n] - t[0]));
  818. /* do binary search to find interval */
  819. for (k = n - i; k > 1;)
  820. {
  821. if (is_ascending ? x >= t[i + (k>>1)] : x <= t[i + (k>>1)])
  822. {
  823. i = i + (k>>1);
  824. k = k - (k>>1);
  825. }
  826. else
  827. k = k>>1;
  828. }
  829. /* at this point, x is between t[i] and t[i+1] */
  830. h = t[i + 1] - t[i];
  831. diff = x - t[i];
  832. updiff = t[i+1] - x;
  833. reldiff = diff / h;
  834. relupdiff = updiff / h;
  835. if (tension == 0.0)
  836. /* evaluate cubic polynomial in nested form */
  837. value = y[i]
  838. + diff
  839. * ((y[i + 1] - y[i]) / h - h * (z[i + 1] + z[i] * 2.0) / 6.0
  840. + diff * (0.5 * z[i] + diff * (z[i + 1] - z[i]) / (6.0 * h)));
  841. else if (tension > 0.0)
  842. /* `positive' (really real) tension, use sinh's */
  843. {
  844. if (fabs(tension * h) < TRIG_ARG_MIN)
  845. /* hand-compute (6/y^2)(sinh(xy)/sinh(y) - x) to improve accuracy;
  846. here `x' means reldiff or relupdiff and `y' means tension*h */
  847. value = (y[i] * relupdiff + y[i+1] * reldiff
  848. + ((z[i] * h * h / 6.0)
  849. * quotient_sinh_func (relupdiff, tension * h))
  850. + ((z[i+1] * h * h / 6.0)
  851. * quotient_sinh_func (reldiff, tension * h)));
  852. else if (fabs(tension * h) > TRIG_ARG_MAX)
  853. /* approximate 1/sinh(y) by 2 sgn(y) exp(-|y|) */
  854. {
  855. int sign = (h < 0.0 ? -1 : 1);
  856. value = (((z[i] * (exp (tension * updiff - sign * tension * h)
  857. + exp (-tension * updiff - sign * tension * h))
  858. + z[i + 1] * (exp (tension * diff - sign * tension * h)
  859. + exp (-tension * diff - sign * tension*h)))
  860. * (sign / (tension * tension)))
  861. + (y[i] - z[i] / (tension * tension)) * (updiff / h)
  862. + (y[i + 1] - z[i + 1] / (tension * tension)) * (diff / h));
  863. }
  864. else
  865. value = (((z[i] * sinh (tension * updiff)
  866. + z[i + 1] * sinh (tension * diff))
  867. / (tension * tension * sinh (tension * h)))
  868. + (y[i] - z[i] / (tension * tension)) * (updiff / h)
  869. + (y[i + 1] - z[i + 1] / (tension * tension)) * (diff / h));
  870. }
  871. else
  872. /* `negative' (really imaginary) tension, use sin's */
  873. {
  874. if (fabs(tension * h) < TRIG_ARG_MIN)
  875. /* hand-compute (6/y^2)(sin(xy)/sin(y) - x) to improve accuracy;
  876. here `x' means reldiff or relupdiff and `y' means tension*h */
  877. value = (y[i] * relupdiff + y[i+1] * reldiff
  878. + ((z[i] * h * h / 6.0)
  879. * quotient_sin_func (relupdiff, tension * h))
  880. + ((z[i+1] * h * h / 6.0)
  881. * quotient_sin_func (reldiff, tension * h)));
  882. else
  883. value = (((z[i] * sin (tension * updiff)
  884. + z[i + 1] * sin (tension * diff))
  885. / (tension * tension * sin (tension * h)))
  886. + (y[i] - z[i] / (tension * tension)) * (updiff / h)
  887. + (y[i + 1] - z[i + 1] / (tension * tension)) * (diff / h));
  888. }
  889. return value;
  890. }
  891. /* is_monotonic() check whether an array of data points, read in by
  892. read_data(), has monotonic abscissa values. */
  893. bool
  894. is_monotonic (int n, double *t)
  895. {
  896. bool is_ascending;
  897. if (t[n-1] < t[n])
  898. is_ascending = true;
  899. else if (t[n-1] > t[n])
  900. is_ascending = false;
  901. else /* equality */
  902. return false;
  903. while (n>0)
  904. {
  905. n--;
  906. if (is_ascending == true ? t[n] >= t[n+1] : t[n] <= t[n+1])
  907. return false;
  908. };
  909. return true;
  910. }
  911. /* read_float reads a single floating point quantity from an input file
  912. (in either ascii or double format). Return value indicates whether it
  913. was read successfully. */
  914. bool
  915. read_float (FILE *input, double *dptr)
  916. {
  917. int num_read;
  918. double dval;
  919. float fval;
  920. int ival;
  921. switch (input_type)
  922. {
  923. case T_ASCII:
  924. default:
  925. num_read = fscanf (input, "%lf", &dval);
  926. break;
  927. case T_SINGLE:
  928. num_read = fread ((void *) &fval, sizeof (fval), 1, input);
  929. dval = fval;
  930. break;
  931. case T_DOUBLE:
  932. num_read = fread ((void *) &dval, sizeof (dval), 1, input);
  933. break;
  934. case T_INTEGER:
  935. num_read = fread ((void *) &ival, sizeof (ival), 1, input);
  936. dval = ival;
  937. break;
  938. }
  939. if (num_read <= 0)
  940. return false;
  941. if (dval != dval)
  942. {
  943. fprintf (stderr, "%s: a NaN (not-a-number) was encountered in a binary input file (it is treated as EOF)\n",
  944. progname);
  945. return false; /* effectively eof */
  946. }
  947. else
  948. {
  949. *dptr = dval;
  950. return true;
  951. }
  952. }
  953. /* Emit a pair of doubles, in specified output representation. Inform user
  954. if any of the emitted values was out-of-bounds for single-precision or
  955. integer format. */
  956. bool
  957. write_point (double t, double *y, int ydimension, int precision, bool suppress_abscissa)
  958. {
  959. int i, num_written = 0;
  960. float ft, fy;
  961. int it, iy;
  962. switch (output_type)
  963. {
  964. case T_ASCII:
  965. default:
  966. if (suppress_abscissa == false)
  967. num_written += printf ("%.*g ", precision, t);
  968. for (i = 0; i < ydimension - 1; i++)
  969. num_written += printf ("%.*g ", precision, y[i]);
  970. num_written += printf ("%.*g\n", precision, y[ydimension - 1]);
  971. break;
  972. case T_SINGLE:
  973. if (suppress_abscissa == false)
  974. {
  975. ft = FROUND(t);
  976. if (ft == FLT_MAX || ft == -(FLT_MAX))
  977. {
  978. maybe_emit_oob_warning();
  979. if (ft == FLT_MAX)
  980. ft *= 0.99999; /* kludge */
  981. }
  982. num_written += fwrite ((void *) &ft, sizeof (ft), 1, stdout);
  983. }
  984. for (i = 0; i < ydimension; i++)
  985. {
  986. fy = y[i];
  987. if (fy == FLT_MAX || fy == -(FLT_MAX))
  988. {
  989. maybe_emit_oob_warning();
  990. if (fy == FLT_MAX)
  991. fy *= 0.99999; /* kludge */
  992. }
  993. num_written += fwrite ((void *) &fy, sizeof (fy), 1, stdout);
  994. }
  995. break;
  996. case T_DOUBLE:
  997. if (suppress_abscissa == false)
  998. num_written += fwrite ((void *) &t, sizeof (t), 1, stdout);
  999. for (i = 0; i < ydimension; i++)
  1000. num_written += fwrite ((void *) &(y[i]), sizeof (double), 1, stdout);
  1001. break;
  1002. case T_INTEGER:
  1003. if (suppress_abscissa == false)
  1004. {
  1005. it = IROUND(t);
  1006. if (it == INT_MAX || it == -(INT_MAX))
  1007. {
  1008. maybe_emit_oob_warning();
  1009. if (it == INT_MAX)
  1010. it--;
  1011. }
  1012. num_written += fwrite ((void *) &it, sizeof (it), 1, stdout);
  1013. }
  1014. for (i = 0; i < ydimension; i++)
  1015. {
  1016. iy = IROUND(y[i]);
  1017. if (iy == INT_MAX || iy == -(INT_MAX))
  1018. {
  1019. maybe_emit_oob_warning();
  1020. if (iy == INT_MAX)
  1021. iy--;
  1022. }
  1023. num_written += fwrite ((void *) &iy, sizeof (iy), 1, stdout);
  1024. }
  1025. break;
  1026. }
  1027. return (num_written > 0 ? true : false); /* i.e. return successp */
  1028. }
  1029. /* read_point() attempts to read a data point from an input file
  1030. (auto-abscissa is supported, as are both ascii and double formats).
  1031. Return value is 0 if a data point was read, 1 if no data point could be
  1032. read (i.e. EOF or garbage in file). A return value of 2 is special: it
  1033. indicates that an explicit end-of-dataset indicator was seen in the input
  1034. stream. For an ascii stream this is two newlines in succession; for a
  1035. double stream this is a DBL_MAX, etc. */
  1036. int
  1037. read_point (FILE *input, double *t, double *y, int ydimension,
  1038. bool *first_point,
  1039. int auto_abscissa, double *auto_t, double auto_delta,
  1040. double *stored)
  1041. {
  1042. bool success;
  1043. int i, items_read, lookahead;
  1044. head:
  1045. if (input_type == T_ASCII)
  1046. {
  1047. bool two_newlines;
  1048. /* skip whitespace, up to but not including 2nd newline */
  1049. two_newlines = skip_whitespace (input);
  1050. if (two_newlines)
  1051. /* end-of-dataset indicator */
  1052. return 2;
  1053. }
  1054. if (feof (input))
  1055. return 1;
  1056. if (input_type == T_ASCII)
  1057. {
  1058. lookahead = getc (input);
  1059. ungetc (lookahead, input);
  1060. if (lookahead == (int)'#') /* comment line */
  1061. {
  1062. char c;
  1063. do
  1064. {
  1065. items_read = fread (&c, sizeof (c), 1, input);
  1066. if (items_read <= 0)
  1067. return 1; /* EOF */
  1068. }
  1069. while (c != '\n');
  1070. ungetc ((int)'\n', input); /* push back \n at the end of # line */
  1071. goto head;
  1072. }
  1073. }
  1074. if (auto_abscissa != AUTO_NONE) /* i.e. AUTO_INCREMENT or AUTO_BY_DISTANCE */
  1075. {
  1076. /* read 1st component of y */
  1077. success = read_float (input, &(y[0]));
  1078. if (!success) /* e.g., EOF */
  1079. return 1;
  1080. if ((input_type == T_DOUBLE && y[0] == DBL_MAX)
  1081. || (input_type == T_SINGLE && y[0] == (double)FLT_MAX)
  1082. || (input_type == T_INTEGER && y[0] == (double)INT_MAX))
  1083. /* end-of-dataset indicator */
  1084. return 2;
  1085. /* read other components of y */
  1086. for (i = 1; i < ydimension; i++)
  1087. {
  1088. success = read_float (input, &(y[i]));
  1089. if (!success) /* effectively EOF (could be garbage) */
  1090. {
  1091. fprintf (stderr, "%s: an input file terminated prematurely\n",
  1092. progname);
  1093. return 1;
  1094. }
  1095. }
  1096. /* t is kept track of, not read from file; two different methods */
  1097. if (auto_abscissa == AUTO_INCREMENT)
  1098. {
  1099. *t = *auto_t;
  1100. *auto_t += auto_delta; /* update */
  1101. }
  1102. else /* AUTO_BY_DISTANCE */
  1103. {
  1104. if (*first_point == true)
  1105. {
  1106. *t = *auto_t;
  1107. *first_point = false;
  1108. }
  1109. else /* compute distance to previous point */
  1110. {
  1111. double distsq = 0.0;
  1112. for (i = 0; i < ydimension; i++)
  1113. distsq += (y[i] - stored[i])*(y[i] - stored[i]);
  1114. *auto_t += sqrt (distsq);
  1115. *t = *auto_t;
  1116. }
  1117. for (i = 0; i < ydimension; i++)
  1118. stored[i] = y[i]; /* store current point */
  1119. }
  1120. /* successfully read all components of y */
  1121. return 0;
  1122. }
  1123. else
  1124. {
  1125. /* read t */
  1126. success = read_float (input, t);
  1127. if (!success) /* e.g., EOF */
  1128. return 1;
  1129. if ((input_type == T_DOUBLE && *t == DBL_MAX)
  1130. || (input_type == T_SINGLE && *t == (double)FLT_MAX)
  1131. || (input_type == T_INTEGER && *t == (double)INT_MAX))
  1132. /* end-of-dataset indicator */
  1133. return 2;
  1134. /* read components of y */
  1135. for (i = 0; i < ydimension; i++)
  1136. {
  1137. success = read_float (input, &(y[i]));
  1138. if (!success) /* effectively EOF (could be garbage) */
  1139. {
  1140. fprintf (stderr, "%s: an input file terminated prematurely\n",
  1141. progname);
  1142. return 1;
  1143. }
  1144. }
  1145. /* successfully read both t and all components of y */
  1146. return 0;
  1147. }
  1148. }
  1149. /* read_data() reads a single dataset from an input file, and stores it.
  1150. If the stream is in ascii format, end-of-dataset is signalled by two
  1151. newlines in succession. If the stream is in double format,
  1152. end-of-dataset is signalled by the occurrence of a DBL_MAX, etc.
  1153. Return value is true if the dataset is ended by an explicit
  1154. end-of-dataset, and false if the dataset is terminated by EOF. That is,
  1155. return value indicates whether another dataset is expected to follow. */
  1156. bool
  1157. read_data (FILE *input, int *len, int *used, int auto_abscissa,
  1158. double auto_t, double auto_delta,
  1159. double **t, int ydimension, double **y, double **z)
  1160. {
  1161. bool first = true;
  1162. int i, success;
  1163. double tt, *yy, *stored;
  1164. yy = (double *)xmalloc (sizeof(double) * ydimension);
  1165. stored = (double *)xmalloc (sizeof(double) * ydimension);
  1166. for ( ; ; )
  1167. {
  1168. if ((++ *used) >= *len)
  1169. {
  1170. *len *= 2;
  1171. *t = (double *)xrealloc (*t, sizeof(double) * *len);
  1172. for (i = 0; i < ydimension; i++)
  1173. {
  1174. y[i] = (double *)xrealloc (y[i], sizeof(double) * *len);
  1175. z[i] = (double *)xrealloc (z[i], sizeof(double) * *len);
  1176. }
  1177. }
  1178. success = read_point (input, &tt, yy, ydimension, &first,
  1179. auto_abscissa, &auto_t, auto_delta, stored);
  1180. switch (success)
  1181. {
  1182. case 0: /* good data point */
  1183. (*t)[*used] = tt;
  1184. for (i = 0; i < ydimension; i++)
  1185. y[i][*used] = yy[i];
  1186. break;
  1187. case 1: /* end of dataset, EOF seen */
  1188. (*used)--;
  1189. free (stored);
  1190. free (yy);
  1191. return false;
  1192. case 2: /* end of dataset, but input continues */
  1193. (*used)--;
  1194. free (stored);
  1195. free (yy);
  1196. return true;
  1197. }
  1198. }
  1199. }
  1200. /* do_spline() is the main routine for piecewise cubic spline
  1201. interpolation, supporting both periodicity and a user-specified boundary
  1202. condition parameter. Nonzero tension may be specified, in which case
  1203. the interpolate() routine, which this calls, will use not cubic
  1204. polynomials but rather expressions involving hyperbolic sines.
  1205. t[] and y[] are the arrays in which the abscissa and ordinate values of
  1206. the user-specified data points are stored, and z[] is the array in which
  1207. the 2nd derivatives at the knots (data points in the interior of the
  1208. interval) will be stored. used+1 is the effective size of each of these
  1209. arrays. The number of points supplied by the user was used+1 in the
  1210. non-periodic case. It was used+0 in the periodic case.
  1211. The reason that the number of elements is greater by one in the periodic
  1212. case is that the first user-supplied data point occurs also at the end.
  1213. In fact, in the periodic case this function will increment the size of
  1214. the array once more, since the periodic interpolation algorithm requires
  1215. the first two data points, not just the first, to appear at the end. */
  1216. /* ARGS: used = indicator that used+1 elements stored in (*t)[] etc.
  1217. len = length of each array
  1218. y,z are ptrs-to-ptrs because we may need to realloc
  1219. k = coeff in bdy condition y''_1 = k y''_0, etc. */
  1220. void
  1221. do_spline (int used, int len, double **t, int ydimension, double **y, double **z,
  1222. double tension, bool periodic, bool spec_boundary_condition,
  1223. double k, int precision, double first_t, double last_t,
  1224. double spacing_t, int no_of_intervals, bool spec_first_t,
  1225. bool spec_last_t, bool spec_spacing_t,
  1226. bool spec_no_of_intervals, bool suppress_abscissa)
  1227. {
  1228. int range_count = 0; /* number of req'd datapoints out of range */
  1229. int lastval = 0; /* last req'd point = 1st/last data point? */
  1230. int i;
  1231. if (used + 1 == 0) /* zero data points in array */
  1232. /* don't output anything (i.e. effectively output a null dataset) */
  1233. return;
  1234. if (used+1 == 1) /* a single data point in array */
  1235. {
  1236. fprintf (stderr,
  1237. "%s: a spline cannot be constructed from a single data point\n",
  1238. progname);
  1239. /* don't output anything (i.e. effectively output a null dataset) */
  1240. return;
  1241. }
  1242. if (!periodic && used+1 <= 2)
  1243. {
  1244. if (spec_boundary_condition)
  1245. fprintf (stderr,
  1246. "%s: the specified boundary condition is ignored, as there are only 2 data points\n",
  1247. progname);
  1248. k = 0.0;
  1249. }
  1250. if (!is_monotonic (used, *t))
  1251. non_monotonic_error(); /* self-explanatory */
  1252. if (periodic)
  1253. {
  1254. bool print_warning = false;
  1255. for (i = 0; i < ydimension; i++)
  1256. {
  1257. if (y[i][used] != y[i][0])
  1258. print_warning = true;
  1259. y[i][used] = y[i][0];
  1260. }
  1261. if (print_warning == true)
  1262. fprintf (stderr, "%s: the final y value is set equal to the initial value, to ensure periodicity\n",
  1263. progname);
  1264. /* add pseudo-point at end (to accord with periodicity) */
  1265. if (used + 1 >= len)
  1266. {
  1267. len++;
  1268. *t = (double *)xrealloc (*t, sizeof(double) * len);
  1269. for (i = 0; i < ydimension; i++)
  1270. {
  1271. y[i] = (double *)xrealloc (y[i], sizeof(double) * len);
  1272. z[i] = (double *)xrealloc (z[i], sizeof(double) * len);
  1273. }
  1274. }
  1275. (*t)[used + 1] = (*t)[used] + ((*t)[1] - (*t)[0]);
  1276. for (i = 0; i < ydimension; i++)
  1277. y[i][used + 1] = y[i][1];
  1278. }
  1279. /* compute z[], array of 2nd derivatives at each knot */
  1280. for (i = 0; i < ydimension; i++)
  1281. fit (used + (periodic ? 1 : 0), /* include pseudo-point if any */
  1282. *t, y[i], z[i], k, tension, periodic);
  1283. if (!spec_first_t)
  1284. first_t = (*t)[0];
  1285. if (!spec_last_t)
  1286. last_t = (*t)[used]; /* used+1 data points in all */
  1287. if (!spec_spacing_t)
  1288. {
  1289. if (no_of_intervals > 0)
  1290. spacing_t = (last_t - first_t) / no_of_intervals;
  1291. else
  1292. spacing_t = 0; /* won't happen */
  1293. }
  1294. else /* user specified spacing */
  1295. {
  1296. if ((last_t - first_t) * spacing_t < 0.0)
  1297. {
  1298. fprintf (stderr, "%s: the requested spacing is of the wrong sign, so it has been corrected\n",
  1299. progname);
  1300. spacing_t = -spacing_t;
  1301. }
  1302. if (spec_no_of_intervals)
  1303. fprintf (stderr, "%s: the requested number of intervals is disregarded\n",
  1304. progname);
  1305. no_of_intervals = (int)(fabs((last_t - first_t) / spacing_t) + FUZZ);
  1306. }
  1307. if (last_t == (*t)[0])
  1308. lastval = 1;
  1309. else if (last_t == (*t)[used])
  1310. lastval = 2;
  1311. for (i = 0; i <= no_of_intervals; ++i)
  1312. {
  1313. double x;
  1314. x = first_t + spacing_t * i;
  1315. if (i == no_of_intervals)
  1316. {
  1317. /* avoid numerical fuzz */
  1318. if (lastval == 1) /* left end of input */
  1319. x = (*t)[0];
  1320. else if (lastval == 2) /* right end of input */
  1321. x = (*t)[used];
  1322. }
  1323. if (periodic || (x - (*t)[0]) * (x - (*t)[used]) <= 0)
  1324. {
  1325. int j;
  1326. double *yy;
  1327. yy = (double *)xmalloc (sizeof(double) * ydimension);
  1328. for (j = 0; j < ydimension; j++)
  1329. yy[j] = interpolate (used, *t, y[j], z[j], x,
  1330. tension, periodic);
  1331. write_point (x, yy, ydimension, precision, suppress_abscissa);
  1332. free (yy);
  1333. }
  1334. else
  1335. range_count++;
  1336. }
  1337. switch (range_count)
  1338. {
  1339. case 0:
  1340. break;
  1341. case 1:
  1342. fprintf (stderr,
  1343. "%s: one requested point could not be computed (as it was out of the data range)\n",
  1344. progname);
  1345. break;
  1346. default:
  1347. fprintf (stderr,
  1348. "%s: %d requested points could not be computed (as they were out of the data range)\n",
  1349. progname, range_count);
  1350. break;
  1351. }
  1352. }
  1353. /* do_bessel() is the main routine for doing real-time cubic Bessel
  1354. interpolation of a dataset. If the input stream is in ascii format,
  1355. end-of-dataset is signalled by two newlines in succession. If the
  1356. stream is in double format, end-of-dataset is signalled by the
  1357. occurrence of a DBL_MAX, etc.
  1358. Return value is true if the dataset is ended by an explicit
  1359. end-of-dataset, and false if the dataset is terminated by EOF. That is,
  1360. return value indicates whether another dataset is expected to follow. */
  1361. bool
  1362. do_bessel (FILE *input, int ydimension, int auto_abscissa, double auto_t,
  1363. double auto_delta, double first_t, double last_t,
  1364. double spacing_t, int precision, bool suppress_abscissa)
  1365. {
  1366. bool first = true;
  1367. double t, *y, *s0, *s1, *s2, *stored;
  1368. double tt[4], **yy;
  1369. int direction = (last_t > first_t ? 1 : -1);
  1370. int state = STATE_ZERO;
  1371. int i, success;
  1372. y = (double *)xmalloc (sizeof(double) * ydimension);
  1373. s0 = (double *)xmalloc (sizeof(double) * ydimension);
  1374. s1 = (double *)xmalloc (sizeof(double) * ydimension);
  1375. s2 = (double *)xmalloc (sizeof(double) * ydimension);
  1376. yy = (double **)xmalloc (4 * sizeof(double *));
  1377. stored = (double *)xmalloc (sizeof(double) * ydimension);
  1378. for (i = 0; i < 4; i++)
  1379. yy[i] = (double *)xmalloc (ydimension * sizeof(double));
  1380. for ( ; ; )
  1381. {
  1382. success = read_point (input, &t, y, ydimension, &first,
  1383. auto_abscissa, &auto_t, auto_delta, stored);
  1384. if (success == 0) /* got a new data point */
  1385. {
  1386. /* use our DFA to process the new data point */
  1387. switch (state)
  1388. {
  1389. case STATE_ZERO: /* just store point */
  1390. tt[0] = t;
  1391. for (i = 0; i < ydimension; i++)
  1392. yy[0][i] = y[i];
  1393. state = STATE_ONE;
  1394. break;
  1395. case STATE_ONE: /* just store point */
  1396. tt[1] = t;
  1397. if (direction * (tt[1] - tt[0]) <= 0)
  1398. non_monotonic_error();
  1399. for (i = 0; i < ydimension; i++)
  1400. yy[1][i] = y[i];
  1401. state = STATE_TWO;
  1402. break;
  1403. case STATE_TWO: /* store point, and process */
  1404. tt[2] = t;
  1405. if (direction * (tt[2] - tt[1]) <= 0)
  1406. non_monotonic_error();
  1407. for (i = 0; i < ydimension; i++)
  1408. {
  1409. yy[2][i] = y[i];
  1410. /* fit parabola through 0,1,2 to compute slopes at 0,1*/
  1411. s0[i] = (((tt[1]-tt[0]) * ((yy[0][i]-yy[2][i]) / (tt[0]-tt[2]))
  1412. + (tt[0]-tt[2]) * ((yy[1][i]-yy[0][i]) / (tt[1]-tt[0])))
  1413. / (tt[1]-tt[2]));
  1414. s1[i] = (((tt[2]-tt[1]) * ((yy[1][i]-yy[0][i]) / (tt[1]-tt[0]))
  1415. + (tt[1]-tt[0]) * ((yy[2][i]-yy[1][i]) / (tt[2]-tt[1])))
  1416. / (tt[2]-tt[0]));
  1417. }
  1418. /* output spline points in range between points 0, 1 */
  1419. do_bessel_range (tt[0], tt[1], yy[0], yy[1], s0, s1,
  1420. first_t, last_t, spacing_t,
  1421. ydimension, precision, false,
  1422. suppress_abscissa);
  1423. state = STATE_THREE;
  1424. break;
  1425. case STATE_THREE: /* store point, and process */
  1426. tt[3] = t;
  1427. if (direction * (tt[3] - tt[2]) <= 0)
  1428. non_monotonic_error();
  1429. for (i = 0; i < ydimension; i++)
  1430. {
  1431. yy[3][i] = y[i];
  1432. /* fit parabola through points 1,2,3 to compute slope at 2 */
  1433. s2[i] = (((tt[3]-tt[2]) * ((yy[2][i]-yy[1][i]) / (tt[2]-tt[1]))
  1434. + (tt[2]-tt[1]) * ((yy[3][i]-yy[2][i]) / (tt[3]-tt[2])))
  1435. / (tt[3]-tt[1]));
  1436. }
  1437. /* output spline points in range between points 1, 2 */
  1438. do_bessel_range (tt[1], tt[2], yy[1], yy[2], s1, s2,
  1439. first_t, last_t, spacing_t,
  1440. ydimension, precision, false,
  1441. suppress_abscissa);
  1442. /* shift points down */
  1443. tt[0] = tt[1];
  1444. tt[1] = tt[2];
  1445. tt[2] = tt[3];
  1446. for (i = 0; i < ydimension; i++)
  1447. {
  1448. yy[0][i] = yy[1][i];
  1449. yy[1][i] = yy[2][i];
  1450. yy[2][i] = yy[3][i];
  1451. /* shift down the only knot slope worth keeping */
  1452. s1[i] = s2[i];
  1453. }
  1454. break;
  1455. }
  1456. }
  1457. else /* didn't get a point, so wind things up */
  1458. {
  1459. switch (state)
  1460. {
  1461. case STATE_ZERO:
  1462. /* silently output a null dataset (i.e., don't output anything) */
  1463. break;
  1464. case STATE_ONE:
  1465. fprintf (stderr, "%s: a spline cannot be constructed from a single data point\n",
  1466. progname);
  1467. /* output a null dataset (i.e., don't output anything) */
  1468. break;
  1469. case STATE_TWO:
  1470. /* have two points: do linear interp between points 0, 1 */
  1471. for (i = 0; i < ydimension; i++)
  1472. s0[i] = s1[i] = (yy[1][i] - yy[0][i])/(tt[1]-tt[0]);
  1473. do_bessel_range (tt[0], tt[1], yy[0], yy[1], s0, s1,
  1474. first_t, last_t, spacing_t,
  1475. ydimension, precision, true,
  1476. suppress_abscissa);
  1477. break;
  1478. case STATE_THREE:
  1479. /* already did 1st of 2 intervals, so do 2nd one too */
  1480. /* fit parabola through points 0,1,2 to compute slope at 2 */
  1481. for (i = 0; i < ydimension; i++)
  1482. s2[i] = (((tt[0]-tt[2]) * ((yy[2][i]-yy[1][i]) / (tt[2]-tt[1]))
  1483. + (tt[2]-tt[1]) * ((yy[0][i]-yy[2][i]) / (tt[0]-tt[2])))
  1484. / (tt[0]-tt[1]));
  1485. /* output spline points in range between points 1, 2 */
  1486. do_bessel_range (tt[1], tt[2], yy[1], yy[2], s1, s2,
  1487. first_t, last_t, spacing_t,
  1488. ydimension, precision, true,
  1489. suppress_abscissa);
  1490. break;
  1491. }
  1492. /* free storage before return */
  1493. for (i = 0; i < 4; i++)
  1494. free (yy[i]);
  1495. free (stored);
  1496. free (yy);
  1497. free (s2);
  1498. free (s1);
  1499. free (s0);
  1500. free (y);
  1501. /* return indication of whether end-of-dataset was seen in stream */
  1502. return (success == 2 ? true : false);
  1503. }
  1504. }
  1505. }
  1506. void
  1507. non_monotonic_error (void)
  1508. {
  1509. fprintf (stderr, "%s: error: the abscissa values are not monotonic\n",
  1510. progname);
  1511. exit (EXIT_FAILURE);
  1512. }
  1513. /* do_bessel_range() computes spline points separated by spacing_t, within
  1514. the abscissa interval abscissa0 <= t < abscissa1, that happen to lie in
  1515. the desired range first_t <= t <= last_t. It writes them to standard
  1516. output. The ordinate values value0 and value1, and endpoint slopes
  1517. slope0 and slope1, are specified. If `endit' is set, then the intervals
  1518. stretch slightly farther than abscissa1 and last_t, to compensate for
  1519. roundoff error. */
  1520. /* ARGS: endit = last interval to be treated? */
  1521. void
  1522. do_bessel_range (double abscissa0, double abscissa1, double *value0,
  1523. double *value1, double *slope0, double *slope1,
  1524. double first_t, double last_t, double spacing_t,
  1525. int ydimension, int precision, bool endit,
  1526. bool suppress_abscissa)
  1527. {
  1528. int direction = ((last_t > first_t) ? 1 : -1); /* sign of spacing_t */
  1529. int i, j;
  1530. int imin1 = (int)((abscissa0 - first_t) / spacing_t - 1);
  1531. int imax1 = (int)((abscissa1 - first_t) / spacing_t + 1);
  1532. int imin2 = 0;
  1533. int imax2 = (int)((last_t - first_t) / spacing_t + 1);
  1534. int imin, imax;
  1535. /* compute maximum interval over which i must range */
  1536. imin = IMAX (imin1, imin2);
  1537. imax = IMIN (imax1, imax2);
  1538. for (i = imin; i <= imax; i++)
  1539. {
  1540. double t;
  1541. t = first_t + i * spacing_t;
  1542. if ((direction * t >= direction * abscissa0)
  1543. && (direction * t >= direction * first_t)
  1544. /* stretch slightly if `endit' is set */
  1545. && ((direction * t < (direction
  1546. * (abscissa1
  1547. + (endit ?
  1548. FUZZ * (abscissa1 - abscissa0) : 0.)))))
  1549. && (direction * t <= (direction
  1550. * (last_t
  1551. + (endit ? FUZZ * (last_t - first_t) : 0.)))))
  1552. {
  1553. double diff = t - abscissa0;
  1554. double updiff = abscissa1 - t;
  1555. double h = abscissa1 - abscissa0;
  1556. double *y;
  1557. bool success;
  1558. y = (double *)xmalloc (sizeof(double) * ydimension);
  1559. for (j = 0; j < ydimension; j++)
  1560. {
  1561. /* should use a nested form */
  1562. y[j] = (value1[j] * (-2.0 * diff * diff * diff / (h * h * h)
  1563. + 3.0 * diff * diff / (h * h))
  1564. + value0[j] * (-2.0 * updiff * updiff * updiff / (h * h * h)
  1565. + 3.0 * updiff * updiff / (h * h)))
  1566. + ((slope1[j] * (diff * diff * diff / (h * h)
  1567. - diff * diff / h)
  1568. - (slope0[j] * (updiff * updiff * updiff / (h * h)
  1569. - updiff * updiff / h))));
  1570. }
  1571. success = write_point (t, y,
  1572. ydimension, precision, suppress_abscissa);
  1573. if (!success)
  1574. {
  1575. fprintf (stderr,
  1576. "%s: error: standard output cannot be written to\n",
  1577. progname);
  1578. exit (EXIT_FAILURE);
  1579. }
  1580. free (y);
  1581. }
  1582. }
  1583. }
  1584. /* Output a separator between datasets. For ascii-format output streams
  1585. this is an extra newline (after the one that the spline ended with,
  1586. yielding two newlines in succession). For double-format output streams
  1587. this is a DBL_MAX, etc. */
  1588. void
  1589. output_dataset_separator (void)
  1590. {
  1591. double ddummy;
  1592. float fdummy;
  1593. int idummy;
  1594. switch (output_type)
  1595. {
  1596. case T_ASCII:
  1597. default:
  1598. printf ("\n");
  1599. break;
  1600. case T_DOUBLE:
  1601. ddummy = DBL_MAX;
  1602. fwrite ((void *) &ddummy, sizeof(ddummy), 1, stdout);
  1603. break;
  1604. case T_SINGLE:
  1605. fdummy = FLT_MAX;
  1606. fwrite ((void *) &fdummy, sizeof(fdummy), 1, stdout);
  1607. break;
  1608. case T_INTEGER:
  1609. idummy = INT_MAX;
  1610. fwrite ((void *) &idummy, sizeof(idummy), 1, stdout);
  1611. break;
  1612. }
  1613. }
  1614. /* skip_whitespace() skips whitespace in an ascii-format input file,
  1615. up to but not including a second newline. Return value indicates
  1616. whether or not two newlines were in fact seen. (For ascii-format
  1617. input files, two newlines signals an end-of-dataset.) */
  1618. bool
  1619. skip_whitespace (FILE *stream)
  1620. {
  1621. int lookahead;
  1622. int nlcount = 0;
  1623. do
  1624. {
  1625. lookahead = getc (stream);
  1626. if (lookahead == (int)'\n')
  1627. nlcount++;
  1628. }
  1629. while (lookahead != EOF
  1630. && isspace((unsigned char)lookahead)
  1631. && nlcount < 2);
  1632. if (lookahead == EOF)
  1633. return false;
  1634. ungetc (lookahead, stream);
  1635. return (nlcount == 2 ? true : false);
  1636. }
  1637. void
  1638. maybe_emit_oob_warning (void)
  1639. {
  1640. static bool warning_written = false;
  1641. if (!warning_written)
  1642. {
  1643. fprintf (stderr, "%s: one or more out-of-bounds output values are approximated\n", progname);
  1644. warning_written = true;
  1645. }
  1646. }
  1647. /* Following four functions compute (6/x^2)(1-x/sinh(x)),
  1648. (3/x^2)(x/tanh(x)-1), (6/x^2)(1-x/sin(x)), and (3/x^2)(x/tan(x)-1) via
  1649. the first three terms of the appropriate power series. They are used
  1650. when |x|<TRIG_ARG_MIN, to avoid loss of significance. Errors are
  1651. O(x**6). */
  1652. double
  1653. sinh_func (double x)
  1654. {
  1655. /* use 1-(7/60)x**2+(31/2520)x**4 */
  1656. return 1.0 - (7.0/60.0)*x*x + (31.0/2520.0)*x*x*x*x;
  1657. }
  1658. double
  1659. tanh_func (double x)
  1660. {
  1661. /* use 1-(1/15)x**2+(2/315)x**4 */
  1662. return 1.0 - (1.0/15.0)*x*x + (2.0/315.0)*x*x*x*x;
  1663. }
  1664. double
  1665. sin_func (double x)
  1666. {
  1667. /* use -1-(7/60)x**2-(31/2520)x**4 */
  1668. return -1.0 - (7.0/60.0)*x*x - (31.0/2520.0)*x*x*x*x;
  1669. }
  1670. double
  1671. tan_func (double x)
  1672. {
  1673. /* use -1-(1/15)x**2-(2/315)x**4 */
  1674. return -1.0 - (1.0/15.0)*x*x - (2.0/315.0)*x*x*x*x;
  1675. }
  1676. /* Following two functions compute (6/y^2)(sinh(xy)/sinh(y)-x) and
  1677. (6/y^2)(sin(xy)/sin(y)-x), via the first three terms of the appropriate
  1678. power series in y. They are used when |y|<TRIG_ARG_MIN, to avoid loss
  1679. of significance. Errors are O(y**6). */
  1680. double
  1681. quotient_sinh_func (double x, double y)
  1682. {
  1683. return ((x*x*x-x) + (x*x*x*x*x/20.0 - x*x*x/6.0 + 7.0*x/60.0)*(y*y)
  1684. + (x*x*x*x*x*x*x/840.0 - x*x*x*x*x/120.0 + 7.0*x*x*x/360.0
  1685. -31.0*x/2520.0)*(y*y*y*y));
  1686. }
  1687. double
  1688. quotient_sin_func (double x, double y)
  1689. {
  1690. return (- (x*x*x-x) + (x*x*x*x*x/20.0 - x*x*x/6.0 + 7.0*x/60.0)*(y*y)
  1691. - (x*x*x*x*x*x*x/840.0 - x*x*x*x*x/120.0 + 7.0*x*x*x/360.0
  1692. -31.0*x/2520.0)*(y*y*y*y));
  1693. }