lsst.afw  13.0-102-g6486c126f
Spline.cc
Go to the documentation of this file.
1 /*
2  * A number of non-trivial splines
3  *
4  * @note These should be merged into lsst::afw::math::Interpolate, but its current implementation
5  * (and to some degree interface) uses gsl explicitly
6  */
7 #include <limits>
8 
9 #include "boost/format.hpp"
10 #include "lsst/pex/exceptions/Runtime.h"
12 #include "lsst/afw/geom/Angle.h"
13 
14 namespace afwGeom = lsst::afw::geom;
15 
16 namespace lsst {
17 namespace afw {
18 namespace math {
19 namespace detail {
20 
21 static int search_array(double z, double const *x, int n, int i);
22 
23 void Spline::_allocateSpline(int const nknot) {
24  _knots.resize(nknot);
25  _coeffs.resize(4);
26  for (unsigned int i = 0; i != _coeffs.size(); ++i) {
27  _coeffs[i].reserve(nknot);
28  }
29 }
30 
31 void Spline::interpolate(std::vector<double> const &x, std::vector<double> &y) const {
32  int const nknot = _knots.size();
33  int const n = x.size();
34 
35  y.resize(n); // may default-construct elements which is a little inefficient
36  /*
37  * For _knots[i] <= x <= _knots[i+1], the interpolant
38  * has the form
39  * val = _coeff[0][i] +dx*(_coeff[1][i] + dx*(_coeff[2][i]/2 + dx*_coeff[3][i]/6))
40  * with
41  * dx = x - knots[i]
42  */
43  int ind = -1; // no idea initially
44  for (int i = 0; i != n; ++i) {
45  ind = search_array(x[i], &_knots[0], nknot, ind);
46 
47  if (ind < 0) { // off bottom
48  ind = 0;
49  } else if (ind >= nknot) { // off top
50  ind = nknot - 1;
51  }
52 
53  double const dx = x[i] - _knots[ind];
54  y[i] = _coeffs[0][ind] +
55  dx * (_coeffs[1][ind] + dx * (_coeffs[2][ind] / 2 + dx * _coeffs[3][ind] / 6));
56  }
57 }
58 
59 void Spline::derivative(std::vector<double> const &x, std::vector<double> &dydx) const {
60  int const nknot = _knots.size();
61  int const n = x.size();
62 
63  dydx.resize(n); // may default-construct elements which is a little inefficient
64  /*
65  * For _knots[i] <= x <= _knots[i+1], the * interpolant has the form
66  * val = _coeff[0][i] +dx*(_coeff[1][i] + dx*(_coeff[2][i]/2 + dx*_coeff[3][i]/6))
67  * with
68  * dx = x - knots[i]
69  * so the derivative is
70  * val = _coeff[1][i] + dx*(_coeff[2][i] + dx*_coeff[3][i]/2))
71  */
72 
73  int ind = -1; // no idea initially
74  for (int i = 0; i != n; ++i) {
75  ind = search_array(x[i], &_knots[0], nknot, ind);
76 
77  if (ind < 0) { // off bottom
78  ind = 0;
79  } else if (ind >= nknot) { // off top
80  ind = nknot - 1;
81  }
82 
83  double const dx = x[i] - _knots[ind];
84  dydx[i] = _coeffs[1][ind] + dx * (_coeffs[2][ind] + dx * _coeffs[3][ind] / 2);
85  }
86 }
87 
88 TautSpline::TautSpline(std::vector<double> const &x, std::vector<double> const &y, double const gamma0,
89  Symmetry type) {
90  if (x.size() != y.size()) {
91  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
92  (boost::format("TautSpline: x and y must have the same size; saw %d %d\n") %
93  x.size() % y.size())
94  .str());
95  }
96 
97  int const ntau = x.size(); /* size of tau and gtau, must be >= 2*/
98  if (ntau < 2) {
99  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
100  (boost::format("TautSpline: ntau = %d, should be >= 2\n") % ntau).str());
101  }
102 
103  switch (type) {
104  case Unknown:
105  calculateTautSpline(x, y, gamma0);
106  break;
107  case Even:
108  case Odd:
109  calculateTautSplineEvenOdd(x, y, gamma0, type == Even);
110  break;
111  }
112 }
113 
114 void TautSpline::calculateTautSpline(std::vector<double> const &x, std::vector<double> const &y,
115  double const gamma0) {
116  const double *tau = &x[0];
117  const double *gtau = &y[0];
118  int const ntau = x.size(); // size of tau and gtau, must be >= 2
119 
120  if (ntau < 4) { // use a single quadratic
121  int const nknot = ntau;
122 
123  _allocateSpline(nknot);
124 
125  _knots[0] = tau[0];
126  for (int i = 1; i < nknot; i++) {
127  _knots[i] = tau[i];
128  if (tau[i - 1] >= tau[i]) {
129  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
130  (boost::format("point %d and the next, %f %f, are out of order") % (i - 1) %
131  tau[i - 1] %
132  tau[i]).str());
133  }
134  }
135 
136  if (ntau == 2) {
137  _coeffs[0][0] = gtau[0];
138  _coeffs[1][0] = (gtau[1] - gtau[0]) / (tau[1] - tau[0]);
139  _coeffs[2][0] = _coeffs[3][0] = 0;
140 
141  _coeffs[0][1] = gtau[1];
142  _coeffs[1][1] = (gtau[1] - gtau[0]) / (tau[1] - tau[0]);
143  _coeffs[2][1] = _coeffs[3][1] = 0;
144  } else { /* must be 3 */
145  double tmp = (tau[2] - tau[0]) * (tau[2] - tau[1]) * (tau[1] - tau[0]);
146  _coeffs[0][0] = gtau[0];
147  _coeffs[1][0] = ((gtau[1] - gtau[0]) * pow(tau[2] - tau[0], 2) -
148  (gtau[2] - gtau[0]) * pow(tau[1] - tau[0], 2)) /
149  tmp;
150  _coeffs[2][0] =
151  -2 * ((gtau[1] - gtau[0]) * (tau[2] - tau[0]) - (gtau[2] - gtau[0]) * (tau[1] - tau[0])) /
152  tmp;
153  _coeffs[3][0] = 0;
154 
155  _coeffs[0][1] = gtau[1];
156  _coeffs[1][1] = _coeffs[1][0] + (tau[1] - tau[0]) * _coeffs[2][0];
157  _coeffs[2][1] = _coeffs[2][0];
158  _coeffs[3][1] = 0;
159 
160  _coeffs[0][2] = gtau[2];
161  _coeffs[1][2] = _coeffs[1][0] + (tau[2] - tau[0]) * _coeffs[2][0];
162  _coeffs[2][2] = _coeffs[2][0];
163  _coeffs[3][2] = 0;
164  }
165 
166  return;
167  }
168  /*
169  * Allocate scratch space
170  * s[0][...] = dtau = tau(.+1) - tau
171  * s[1][...] = diag = diagonal in linear system
172  * s[2][...] = u = upper diagonal in linear system
173  * s[3][...] = r = right side for linear system (initially)
174  * = fsecnd = solution of linear system , namely the second
175  * derivatives of interpolant at tau
176  * s[4][...] = z = indicator of additional knots
177  * s[5][...] = 1/hsecnd(1,x) with x = z or = 1-z. see below.
178  */
179  std::vector<std::vector<double> > s(6); // scratch space
180 
181  for (int i = 0; i != 6; i++) {
182  s[i].resize(ntau);
183  }
184  /*
185  * Construct delta tau and first and second (divided) differences of data
186  */
187 
188  for (int i = 0; i < ntau - 1; i++) {
189  s[0][i] = tau[i + 1] - tau[i];
190  if (s[0][i] <= 0.) {
191  throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
192  (boost::format("point %d and the next, %f %f, are out of order") % i % tau[i] %
193  tau[i + 1])
194  .str());
195  }
196  s[3][i + 1] = (gtau[i + 1] - gtau[i]) / s[0][i];
197  }
198  for (int i = 1; i < ntau - 1; ++i) {
199  s[3][i] = s[3][i + 1] - s[3][i];
200  }
201  /*
202  * Construct system of equations for second derivatives at tau. At each
203  * interior data point, there is one continuity equation, at the first
204  * and the last interior data point there is an additional one for a
205  * total of ntau equations in ntau unknowns.
206  */
207  s[1][1] = s[0][0] / 3;
208 
209  int method;
210  double gamma = gamma0; // control the smoothing
211  if (gamma <= 0) {
212  method = 1;
213  } else if (gamma > 3) {
214  gamma -= 3;
215  method = 3;
216  } else {
217  method = 2;
218  }
219  double const onemg3 = 1 - gamma / 3;
220 
221  int nknot = ntau; // count number of knots
222  /*
223  * Some compilers don't realise that the flow of control always initialises
224  * these variables; so initialise them to placate e.g. gcc
225  */
226  double zeta = 0.0;
227  double alpha = 0.0;
228  double ratio = 0.0;
229 
230  // double c, d;
231  // double z, denom, factr2;
232  // double onemzt, zt2, del;
233 
234  double entry3 = 0.0;
235  double factor = 0.0;
236  double onemzt = 0;
237  double zt2 = 0;
238  double z_half = 0;
239 
240  for (int i = 1; i < ntau - 2; ++i) {
241  /*
242  * construct z[i] and zeta[i]
243  */
244  double z = .5;
245  if ((method == 2 && s[3][i] * s[3][i + 1] >= 0) || method == 3) {
246  double const temp = fabs(s[3][i + 1]);
247  double const denom = fabs(s[3][i]) + temp;
248  if (denom != 0) {
249  z = temp / denom;
250  if (fabs(z - 0.5) <= 1.0 / 6.0) {
251  z = 0.5;
252  }
253  }
254  }
255 
256  s[4][i] = z;
257  /*
258  set up part of the i-th equation which depends on the i-th interval
259  */
260  z_half = z - 0.5;
261  if (z_half < 0) {
262  zeta = gamma * z;
263  onemzt = 1 - zeta;
264  zt2 = zeta * zeta;
265  double temp = onemg3 / onemzt;
266  alpha = (temp < 1 ? temp : 1);
267  factor = zeta / (alpha * (zt2 - 1) + 1);
268  s[5][i] = zeta * factor / 6;
269  s[1][i] += s[0][i] * ((1 - alpha * onemzt) * factor / 2 - s[5][i]);
270  /*
271  * if z = 0 and the previous z = 1, then d[i] = 0. Since then
272  * also u[i-1] = l[i+1] = 0, its value does not matter. Reset
273  * d[i] = 1 to insure nonzero pivot in elimination.
274  */
275  if (s[1][i] <= 0.) {
276  s[1][i] = 1;
277  }
278  s[2][i] = s[0][i] / 6;
279 
280  if (z != 0) { /* we'll get a new knot */
281  nknot++;
282  }
283  } else if (z_half == 0) {
284  s[1][i] += s[0][i] / 3;
285  s[2][i] = s[0][i] / 6;
286  } else {
287  onemzt = gamma * (1 - z);
288  zeta = 1 - onemzt;
289  double const temp = onemg3 / zeta;
290  alpha = (temp < 1 ? temp : 1);
291  factor = onemzt / (1 - alpha * zeta * (onemzt + 1));
292  s[5][i] = onemzt * factor / 6;
293  s[1][i] += s[0][i] / 3;
294  s[2][i] = s[5][i] * s[0][i];
295 
296  if (onemzt != 0) { /* we'll get a new knot */
297  nknot++;
298  }
299  }
300  if (i == 1) {
301  s[4][0] = 0.5;
302  /*
303  * the first two equations enforce continuity of the first and of
304  * the third derivative across tau[1].
305  */
306  s[1][0] = s[0][0] / 6;
307  s[2][0] = s[1][1];
308  entry3 = s[2][1];
309  if (z_half < 0) {
310  const double factr2 = zeta * (alpha * (zt2 - 1.) + 1.) / (alpha * (zeta * zt2 - 1.) + 1.);
311  ratio = factr2 * s[0][1] / s[1][0];
312  s[1][1] = factr2 * s[0][1] + s[0][0];
313  s[2][1] = -factr2 * s[0][0];
314  } else if (z_half == 0) {
315  ratio = s[0][1] / s[1][0];
316  s[1][1] = s[0][1] + s[0][0];
317  s[2][1] = -s[0][0];
318  } else {
319  ratio = s[0][1] / s[1][0];
320  s[1][1] = s[0][1] + s[0][0];
321  s[2][1] = -s[0][0] * 6 * alpha * s[5][1];
322  }
323  /*
324  * at this point, the first two equations read
325  * diag[0]*x0 + u[0]*x1 + entry3*x2 = r[1]
326  * -ratio*diag[0]*x0 + diag[1]*x1 + u[1]*x2 = 0
327  * set r[0] = r[1] and eliminate x1 from the second equation
328  */
329  s[3][0] = s[3][1];
330 
331  s[1][1] += ratio * s[2][0];
332  s[2][1] += ratio * entry3;
333  s[3][1] = ratio * s[3][1];
334  } else {
335  /*
336  * the i-th equation enforces continuity of the first derivative
337  * across tau[i]; it reads
338  * -ratio*diag[i-1]*x_{i-1} + diag[i]*x_i + u[i]*x_{i+1} = r[i]
339  * eliminate x_{i-1} from this equation
340  */
341  s[1][i] += ratio * s[2][i - 1];
342  s[3][i] += ratio * s[3][i - 1];
343  }
344  /*
345  * Set up the part of the next equation which depends on the i-th interval.
346  */
347  if (z_half < 0) {
348  ratio = -s[5][i] * s[0][i] / s[1][i];
349  s[1][i + 1] = s[0][i] / 3;
350  } else if (z_half == 0) {
351  ratio = -(s[0][i] / 6) / s[1][i];
352  s[1][i + 1] = s[0][i] / 3;
353  } else {
354  ratio = -(s[0][i] / 6) / s[1][i];
355  s[1][i + 1] = s[0][i] * ((1 - zeta * alpha) * factor / 2 - s[5][i]);
356  }
357  }
358 
359  s[4][ntau - 2] = 0.5;
360  /*
361  * last two equations, which enforce continuity of third derivative and
362  * of first derivative across tau[ntau - 2]
363  */
364  double const entry_ = ratio * s[2][ntau - 3] + s[1][ntau - 2] + s[0][ntau - 2] / 3;
365  s[1][ntau - 1] = s[0][ntau - 2] / 6;
366  s[3][ntau - 1] = ratio * s[3][ntau - 3] + s[3][ntau - 2];
367  if (z_half < 0) {
368  ratio = s[0][ntau - 2] * 6 * s[5][ntau - 3] * alpha / s[1][ntau - 3];
369  s[1][ntau - 2] = ratio * s[2][ntau - 3] + s[0][ntau - 2] + s[0][ntau - 3];
370  s[2][ntau - 2] = -s[0][ntau - 3];
371  } else if (z_half == 0) {
372  ratio = s[0][ntau - 2] / s[1][ntau - 3];
373  s[1][ntau - 2] = ratio * s[2][ntau - 3] + s[0][ntau - 2] + s[0][ntau - 3];
374  s[2][ntau - 2] = -s[0][ntau - 3];
375  } else {
376  const double factr2 =
377  onemzt * (alpha * (onemzt * onemzt - 1) + 1) / (alpha * (onemzt * onemzt * onemzt - 1) + 1);
378  ratio = factr2 * s[0][ntau - 2] / s[1][ntau - 3];
379  s[1][ntau - 2] = ratio * s[2][ntau - 3] + factr2 * s[0][ntau - 3] + s[0][ntau - 2];
380  s[2][ntau - 2] = -factr2 * s[0][ntau - 3];
381  }
382  /*
383  * at this point, the last two equations read
384  * diag[i]*x_i + u[i]*x_{i+1} = r[i]
385  * -ratio*diag[i]*x_i + diag[i+1]*x_{i+1} = r[i+1]
386  * eliminate x_i from last equation
387  */
388  s[3][ntau - 2] = ratio * s[3][ntau - 3];
389  ratio = -entry_ / s[1][ntau - 2];
390  s[1][ntau - 1] += ratio * s[2][ntau - 2];
391  s[3][ntau - 1] += ratio * s[3][ntau - 2];
392 
393  /*
394  * back substitution
395  */
396  s[3][ntau - 1] /= s[1][ntau - 1];
397  for (int i = ntau - 2; i > 0; --i) {
398  s[3][i] = (s[3][i] - s[2][i] * s[3][i + 1]) / s[1][i];
399  }
400 
401  s[3][0] = (s[3][0] - s[2][0] * s[3][1] - entry3 * s[3][2]) / s[1][0];
402 /*
403  * construct polynomial pieces; first allocate space for the coefficients
404  */
405 #if 1
406  /*
407  * Start by counting the knots
408  */
409  {
410  int const nknot0 = nknot;
411  int nknot = ntau;
412 
413  for (int i = 0; i < ntau - 1; ++i) {
414  double const z = s[4][i];
415  if ((z < 0.5 && z != 0.0) || (z > 0.5 && (1 - z) != 0.0)) {
416  nknot++;
417  }
418  }
419  assert(nknot == nknot0);
420  }
421 #endif
422  _allocateSpline(nknot);
423 
424  _knots[0] = tau[0];
425  int j = 0;
426  for (int i = 0; i < ntau - 1; ++i) {
427  _coeffs[0][j] = gtau[i];
428  _coeffs[2][j] = s[3][i];
429  double const divdif = (gtau[i + 1] - gtau[i]) / s[0][i];
430  double z = s[4][i];
431  double const z_half = z - 0.5;
432  if (z_half < 0) {
433  if (z == 0) {
434  _coeffs[1][j] = divdif;
435  _coeffs[2][j] = 0;
436  _coeffs[3][j] = 0;
437  } else {
438  zeta = gamma * z;
439  onemzt = 1 - zeta;
440  double const c = s[3][i + 1] / 6;
441  double const d = s[3][i] * s[5][i];
442  j++;
443 
444  double const del = zeta * s[0][i];
445  _knots[j] = tau[i] + del;
446  zt2 = zeta * zeta;
447  double temp = onemg3 / onemzt;
448  alpha = (temp < 1 ? temp : 1);
449  factor = onemzt * onemzt * alpha;
450  temp = s[0][i];
451  _coeffs[0][j] = gtau[i] + divdif * del +
452  temp * temp * (d * onemzt * (factor - 1) + c * zeta * (zt2 - 1));
453  _coeffs[1][j] = divdif + s[0][i] * (d * (1 - 3 * factor) + c * (3 * zt2 - 1));
454  _coeffs[2][j] = (d * alpha * onemzt + c * zeta) * 6;
455  _coeffs[3][j] = (c - d * alpha) * 6 / s[0][i];
456  if (del * zt2 == 0) {
457  _coeffs[1][j - 1] = 0; /* would be NaN in an */
458  _coeffs[3][j - 1] = 0; /* 0-length interval */
459  } else {
460  _coeffs[3][j - 1] = _coeffs[3][j] - d * 6 * (1 - alpha) / (del * zt2);
461  _coeffs[1][j - 1] = _coeffs[1][j] - del * (_coeffs[2][j] - del / 2 * _coeffs[3][j - 1]);
462  }
463  }
464  } else if (z_half == 0) {
465  _coeffs[1][j] = divdif - s[0][i] * (s[3][i] * 2 + s[3][i + 1]) / 6;
466  _coeffs[3][j] = (s[3][i + 1] - s[3][i]) / s[0][i];
467  } else {
468  onemzt = gamma * (1 - z);
469  if (onemzt == 0) {
470  _coeffs[1][j] = divdif;
471  _coeffs[2][j] = 0;
472  _coeffs[3][j] = 0;
473  } else {
474  zeta = 1 - onemzt;
475  double const temp = onemg3 / zeta;
476  alpha = (temp < 1 ? temp : 1);
477  double const c = s[3][i + 1] * s[5][i];
478  double const d = s[3][i] / 6;
479  double const del = zeta * s[0][i];
480  _knots[j + 1] = tau[i] + del;
481  _coeffs[1][j] = divdif - s[0][i] * (2 * d + c);
482  _coeffs[3][j] = (c * alpha - d) * 6 / s[0][i];
483  j++;
484 
485  _coeffs[3][j] =
486  _coeffs[3][j - 1] + (1 - alpha) * 6 * c / (s[0][i] * (onemzt * onemzt * onemzt));
487  _coeffs[2][j] = _coeffs[2][j - 1] + del * _coeffs[3][j - 1];
488  _coeffs[1][j] = _coeffs[1][j - 1] + del * (_coeffs[2][j - 1] + del / 2 * _coeffs[3][j - 1]);
489  _coeffs[0][j] = _coeffs[0][j - 1] +
490  del * (_coeffs[1][j - 1] +
491  del / 2 * (_coeffs[2][j - 1] + del / 3 * _coeffs[3][j - 1]));
492  }
493  }
494 
495  j++;
496  _knots[j] = tau[i + 1];
497  }
498  /*
499  * If there are discontinuities some of the knots may be at the same
500  * position; in this case we generated some NaNs above. As they only
501  * occur for 0-length segments, it's safe to replace them by 0s
502  *
503  * Due to the not-a-knot condition, the last set of coefficients isn't
504  * needed (the last-but-one is equivalent), but it makes the book-keeping
505  * easier if we _do_ generate them
506  */
507  double const del = tau[ntau - 1] - _knots[nknot - 2];
508 
509  _coeffs[0][nknot - 1] = _coeffs[0][nknot - 2] +
510  del * (_coeffs[1][nknot - 2] +
511  del * (_coeffs[2][nknot - 2] / 2 + del * _coeffs[3][nknot - 2] / 6));
512  _coeffs[1][nknot - 1] =
513  _coeffs[1][nknot - 2] + del * (_coeffs[2][nknot - 2] + del * _coeffs[3][nknot - 2] / 2);
514  _coeffs[2][nknot - 1] = _coeffs[2][nknot - 2] + del * _coeffs[3][nknot - 2];
515  _coeffs[3][nknot - 1] = _coeffs[3][nknot - 2];
516 
517  assert(j + 1 == nknot);
518 }
519 
520 /*
521  * Here's the code to fit smoothing splines through data points
522  */
523 static void spcof1(const double x[], double avh, const double y[], const double dy[], int n, double p,
524  double q, double a[], double *c[3], double u[], const double v[]);
525 
526 static void sperr1(const double x[], double avh, const double dy[], int n, double *r[3], double p, double var,
527  std::vector<double> *se);
528 
529 static double spfit1(const double x[], double avh, const double dy[], int n, double rho, double *p, double *q,
530  double var, double stat[], const double a[], double *c[3], double *r[3], double *t[2],
531  double u[], double v[]);
532 
533 static double spint1(const double x[], double *avh, const double y[], double dy[], int n, double a[],
534  double *c[3], double *r[3], double *t[2]);
535 
536 SmoothedSpline::SmoothedSpline(std::vector<double> const &x, std::vector<double> const &f,
537  std::vector<double> const &df, double s, double *chisq,
538  std::vector<double> *errs) {
539  float var = 1; // i.e. df is the absolute s.d. N.B. ADD GCV Variant with var=-1
540  int const n = x.size();
541  double const ratio = 2.0;
542  double const tau = 1.618033989; /* golden ratio */
543  double avdf, avar, stat[6];
544  double p, q, delta, r1, r2, r3, r4;
545  double gf1, gf2, gf3, gf4, avh, err;
546  /*
547  * allocate scratch space
548  */
549  _allocateSpline(n);
550 
551  double *y = &_coeffs[0][0];
552  double *c[3];
553  c[0] = &_coeffs[1][0];
554  c[1] = &_coeffs[2][0];
555  c[2] = &_coeffs[3][0];
556 
557  std::vector<double> scratch(7 * (n + 2)); // scratch space
558 
559  double *r[3];
560  r[0] = &scratch[0] + 1; // we want indices -1..n
561  r[1] = r[0] + (n + 2);
562  r[2] = r[1] + (n + 2);
563  double *t[2];
564  t[0] = r[2] + (n + 2);
565  t[1] = t[0] + (n + 2);
566  double *u = t[1] + (n + 2);
567  double *v = u + (n + 2);
568  /*
569  * and so to work.
570  */
571  std::vector<double> sdf = df; // scaled values of df
572 
573  avdf = spint1(&x[0], &avh, &f[0], &sdf[0], n, y, c, r, t);
574  avar = var;
575  if (var > 0) {
576  avar *= avdf * avdf;
577  }
578 
579  if (var == 0) { /* simply find a natural cubic spline*/
580  r1 = 0;
581 
582  gf1 = spfit1(&x[0], avh, &sdf[0], n, r1, &p, &q, avar, stat, y, c, r, t, u, v);
583  } else { /* Find local minimum of gcv or the
584  expected mean square error */
585  r1 = 1;
586  r2 = ratio * r1;
587  gf2 = spfit1(&x[0], avh, &sdf[0], n, r2, &p, &q, avar, stat, y, c, r, t, u, v);
588  bool set_r3 = false; // was r3 set?
589  for (;;) {
590  gf1 = spfit1(&x[0], avh, &sdf[0], n, r1, &p, &q, avar, stat, y, c, r, t, u, v);
591  if (gf1 > gf2) {
592  break;
593  }
594 
595  if (p <= 0) {
596  break;
597  }
598  r2 = r1;
599  gf2 = gf1;
600  r1 /= ratio;
601  }
602 
603  if (p <= 0) {
604  set_r3 = false;
605  r3 = 0; /* placate compiler */
606  } else {
607  r3 = ratio * r2;
608  set_r3 = true;
609 
610  for (;;) {
611  gf3 = spfit1(&x[0], avh, &sdf[0], n, r3, &p, &q, avar, stat, y, c, r, t, u, v);
612  if (gf3 >= gf2) {
613  break;
614  }
615 
616  if (q <= 0) {
617  break;
618  }
619  r2 = r3;
620  gf2 = gf3;
621  r3 = ratio * r3;
622  }
623  }
624 
625  if (p > 0 && q > 0) {
626  assert(set_r3);
627  r2 = r3;
628  gf2 = gf3;
629  delta = (r2 - r1) / tau;
630  r4 = r1 + delta;
631  r3 = r2 - delta;
632  gf3 = spfit1(&x[0], avh, &sdf[0], n, r3, &p, &q, avar, stat, y, c, r, t, u, v);
633  gf4 = spfit1(&x[0], avh, &sdf[0], n, r4, &p, &q, avar, stat, y, c, r, t, u, v);
634  /*
635  * Golden section search for local minimum
636  */
637  do {
638  if (gf3 <= gf4) {
639  r2 = r4;
640  gf2 = gf4;
641  r4 = r3;
642  gf4 = gf3;
643  delta /= tau;
644  r3 = r2 - delta;
645  gf3 = spfit1(&x[0], avh, &sdf[0], n, r3, &p, &q, avar, stat, y, c, r, t, u, v);
646  } else {
647  r1 = r3;
648  gf1 = gf3;
649  r3 = r4;
650  gf3 = gf4;
651  delta /= tau;
652  r4 = r1 + delta;
653  gf4 = spfit1(&x[0], avh, &sdf[0], n, r4, &p, &q, avar, stat, y, c, r, t, u, v);
654  }
655 
656  err = (r2 - r1) / (r1 + r2);
657  } while (err * err + 1 > 1 && err > 1e-6);
658 
659  r1 = (r1 + r2) * .5;
660  gf1 = spfit1(&x[0], avh, &sdf[0], n, r1, &p, &q, avar, stat, y, c, r, t, u, v);
661  }
662  }
663  /*
664  * Calculate spline coefficients
665  */
666  spcof1(&x[0], avh, &f[0], &sdf[0], n, p, q, y, c, u, v);
667 
668  stat[2] /= avdf * avdf; /* undo scaling */
669  stat[3] /= avdf * avdf;
670  stat[4] /= avdf * avdf;
671  /*
672  * Optionally calculate standard error estimates
673  */
674  if (errs != NULL) {
675  sperr1(&x[0], avh, &sdf[0], n, r, p, avar, errs);
676  }
677  /*
678  * clean up
679  */
680  if (chisq != NULL) {
681  *chisq = n * stat[4];
682  }
683 }
684 
694 static double spint1(const double x[], double *avh, const double f[], double df[], int n, double a[],
695  double *c[3], double *r[3], double *t[2]) {
696  double avdf;
697  double e, ff, g, h;
698  int i;
699 
700  assert(n >= 3);
701  /*
702  * Get average x spacing in avh
703  */
704  g = 0;
705  for (i = 0; i < n - 1; ++i) {
706  h = x[i + 1] - x[i];
707  assert(h > 0);
708 
709  g += h;
710  }
711  *avh = g / (n - 1);
712  /*
713  * Scale relative weights
714  */
715  g = 0;
716  for (int i = 0; i < n; ++i) {
717  assert(df[i] > 0);
718 
719  g += df[i] * df[i];
720  }
721  avdf = sqrt(g / n);
722 
723  for (i = 0; i < n; ++i) {
724  df[i] /= avdf;
725  }
726  /*
727  * Initialize h,f
728  */
729  h = (x[1] - x[0]) / (*avh);
730  ff = (f[1] - f[0]) / h;
731  /*
732  * Calculate a,t,r
733  */
734  for (i = 1; i < n - 1; ++i) {
735  g = h;
736  h = (x[i + 1] - x[i]) / (*avh);
737  e = ff;
738  ff = (f[i + 1] - f[i]) / h;
739  a[i] = ff - e;
740  t[0][i] = (g + h) * 2. / 3.;
741  t[1][i] = h / 3.;
742  r[2][i] = df[i - 1] / g;
743  r[0][i] = df[i + 1] / h;
744  r[1][i] = -df[i] / g - df[i] / h;
745  }
746  /*
747  * Calculate c = r'*r
748  */
749  r[1][n - 1] = 0;
750  r[2][n - 1] = 0;
751  r[2][n] = 0;
752 
753  for (i = 1; i < n - 1; i++) {
754  c[0][i] = r[0][i] * r[0][i] + r[1][i] * r[1][i] + r[2][i] * r[2][i];
755  c[1][i] = r[0][i] * r[1][i + 1] + r[1][i] * r[2][i + 1];
756  c[2][i] = r[0][i] * r[2][i + 2];
757  }
758 
759  return (avdf);
760 }
761 
787 static double spfit1(const double x[], double avh, const double dy[], int n, double rho, double *pp,
788  double *pq, double var, double stat[], const double a[], double *c[3], double *r[3],
789  double *t[2], double u[], double v[]) {
790  double const eps = std::numeric_limits<double>::epsilon();
791  double e, f, g, h;
792  double fun;
793  int i;
794  double p, q; /* == *pp, *pq */
795  /*
796  * Use p and q instead of rho to prevent overflow or underflow
797  */
798  if (fabs(rho) < eps) {
799  p = 0;
800  q = 1;
801  } else if (fabs(1 / rho) < eps) {
802  p = 1;
803  q = 0;
804  } else {
805  p = rho / (1 + rho);
806  q = 1 / (1 + rho);
807  }
808  /*
809  * Rational Cholesky decomposition of p*c + q*t
810  */
811  f = 0;
812  g = 0;
813  h = 0;
814  r[0][-1] = r[0][0] = 0;
815 
816  for (int i = 1; i < n - 1; ++i) {
817  r[2][i - 2] = g * r[0][i - 2];
818  r[1][i - 1] = f * r[0][i - 1];
819  {
820  double tmp = p * c[0][i] + q * t[0][i] - f * r[1][i - 1] - g * r[2][i - 2];
821  if (tmp == 0.0) {
822  r[0][i] = 1e30;
823  } else {
824  r[0][i] = 1 / tmp;
825  }
826  }
827  f = p * c[1][i] + q * t[1][i] - h * r[1][i - 1];
828  g = h;
829  h = p * c[2][i];
830  }
831  /*
832  * Solve for u
833  */
834  u[-1] = u[0] = 0;
835  for (int i = 1; i < n - 1; i++) {
836  u[i] = a[i] - r[1][i - 1] * u[i - 1] - r[2][i - 2] * u[i - 2];
837  }
838  u[n - 1] = u[n] = 0;
839  for (int i = n - 2; i > 0; i--) {
840  u[i] = r[0][i] * u[i] - r[1][i] * u[i + 1] - r[2][i] * u[i + 2];
841  }
842  /*
843  * Calculate residual vector v
844  */
845  e = h = 0;
846  for (int i = 0; i < n - 1; i++) {
847  g = h;
848  h = (u[i + 1] - u[i]) / ((x[i + 1] - x[i]) / avh);
849  v[i] = dy[i] * (h - g);
850  e += v[i] * v[i];
851  }
852  v[n - 1] = -h * dy[n - 1];
853  e += v[n - 1] * v[n - 1];
854  /*
855  * Calculate upper three bands of inverse matrix
856  */
857  r[0][n - 1] = r[1][n - 1] = r[0][n] = 0;
858  for (i = n - 2; i > 0; i--) {
859  g = r[1][i];
860  h = r[2][i];
861  r[1][i] = -g * r[0][i + 1] - h * r[1][i + 1];
862  r[2][i] = -g * r[1][i + 1] - h * r[0][i + 2];
863  r[0][i] = r[0][i] - g * r[1][i] - h * r[2][i];
864  }
865  /*
866  * Calculate trace
867  */
868  f = g = h = 0;
869  for (i = 1; i < n - 1; ++i) {
870  f += r[0][i] * c[0][i];
871  g += r[1][i] * c[1][i];
872  h += r[2][i] * c[2][i];
873  }
874  f += 2 * (g + h);
875  /*
876  * Calculate statistics
877  */
878  stat[0] = p;
879  stat[1] = f * p;
880  stat[2] = n * e / (f * f + 1e-20);
881  stat[3] = e * p * p / n;
882  stat[5] = e * p / ((f < 0) ? f - 1e-10 : f + 1e-10);
883  if (var >= 0) {
884  fun = stat[3] - 2 * var * stat[1] / n + var;
885 
886  stat[4] = fun;
887  } else {
888  stat[4] = stat[5] - stat[3];
889  fun = stat[2];
890  }
891 
892  *pp = p;
893  *pq = q;
894 
895  return (fun);
896 }
897 
902 static void spcof1(const double x[], double avh, const double y[], const double dy[], int n, double p,
903  double q, double a[], double *c[3], double u[], const double v[]) {
904  double h;
905  int i;
906  double qh;
907 
908  qh = q / (avh * avh);
909 
910  for (i = 0; i < n; ++i) {
911  a[i] = y[i] - p * dy[i] * v[i];
912  u[i] = qh * u[i];
913  }
914  /*
915  * calculate c
916  */
917  for (i = 0; i < n - 1; ++i) {
918  h = x[i + 1] - x[i];
919  c[2][i] = (u[i + 1] - u[i]) / (3 * h);
920  c[0][i] = (a[i + 1] - a[i]) / h - (h * c[2][i] + u[i]) * h;
921  c[1][i] = u[i];
922  }
923 }
924 
930 static void sperr1(const double x[], double avh, const double dy[], int n, double *r[3], double p, double var,
931  std::vector<double> *se) {
932  double f, g, h;
933  int i;
934  double f1, g1, h1;
935  /*
936  * Initialize
937  */
938  h = avh / (x[1] - x[0]);
939  (*se)[0] = 1 - p * dy[0] * dy[0] * h * h * r[0][1];
940  r[0][0] = r[1][0] = r[2][0] = 0;
941  /*
942  * Calculate diagonal elements
943  */
944  for (i = 1; i < n - 1; ++i) {
945  f = h;
946  h = avh / (x[i + 1] - x[i]);
947  g = -(f + h);
948  f1 = f * r[0][i - 1] + g * r[1][i - 1] + h * r[2][i - 1];
949  g1 = f * r[1][i - 1] + g * r[0][i] + h * r[1][i];
950  h1 = f * r[2][i - 1] + g * r[1][i] + h * r[0][i + 1];
951  (*se)[i] = 1 - p * dy[i] * dy[i] * (f * f1 + g * g1 + h * h1);
952  }
953  (*se)[n - 1] = 1 - p * dy[n - 1] * dy[n - 1] * h * h * r[0][n - 2];
954  /*
955  * Calculate standard error estimates
956  */
957  for (int i = 0; i < n; ++i) {
958  double const tmp = (*se)[i] * var;
959  (*se)[i] = (tmp >= 0) ? sqrt(tmp) * dy[i] : 0;
960  }
961 }
962 
963 void TautSpline::calculateTautSplineEvenOdd(std::vector<double> const &_tau, std::vector<double> const &_gtau,
964  double const gamma,
965  bool const even // ensure Even symmetry
966  ) {
967  const double *tau = &_tau[0];
968  const double *gtau = &_gtau[0];
969  int const ntau = _tau.size(); // size of tau and gtau, must be >= 2
970  std::vector<double> x, y; // tau and gtau, extended to -ve tau
971 
972  if (tau[0] == 0.0f) {
973  int const np = 2 * ntau - 1;
974  x.resize(np);
975  y.resize(np);
976 
977  x[ntau - 1] = tau[0];
978  y[ntau - 1] = gtau[0];
979  for (int i = 1; i != ntau; ++i) {
980  if (even) {
981  x[ntau - 1 + i] = tau[i];
982  y[ntau - 1 + i] = gtau[i];
983  x[ntau - 1 - i] = -tau[i];
984  y[ntau - 1 - i] = gtau[i];
985  } else {
986  x[ntau - 1 + i] = tau[i];
987  y[ntau - 1 + i] = gtau[i];
988  x[ntau - 1 - i] = -tau[i];
989  y[ntau - 1 - i] = -gtau[i];
990  }
991  }
992  } else {
993  int const np = 2 * ntau;
994  x.resize(np);
995  y.resize(np);
996 
997  for (int i = 0; i != ntau; ++i) {
998  if (even) {
999  x[ntau + i] = tau[i];
1000  y[ntau + i] = gtau[i];
1001  x[ntau - 1 - i] = -tau[i];
1002  y[ntau - 1 - i] = gtau[i];
1003  } else {
1004  x[ntau + i] = tau[i];
1005  y[ntau + i] = gtau[i];
1006  x[ntau - 1 - i] = -tau[i];
1007  y[ntau - 1 - i] = -gtau[i];
1008  }
1009  }
1010  }
1011 
1012  TautSpline sp(x, y, gamma); // fit a taut spline to x, y
1013  /*
1014  * Now repackage that spline to reflect the original points
1015  */
1016  int ii;
1017  for (ii = sp._knots.size() - 1; ii >= 0; --ii) {
1018  if (sp._knots[ii] < 0.0f) {
1019  break;
1020  }
1021  }
1022  int const i0 = ii + 1;
1023  int const nknot = sp._knots.size() - i0;
1024 
1025  _allocateSpline(nknot);
1026 
1027  for (int i = i0; i != static_cast<int>(sp._knots.size()); ++i) {
1028  _knots[i - i0] = sp._knots[i];
1029  for (int j = 0; j != 4; ++j) {
1030  _coeffs[j][i - i0] = sp._coeffs[j][i];
1031  }
1032  }
1033 }
1034 
1040 static int search_array(double z, double const *x, int n, int i) {
1041  int lo, hi, mid;
1042  double xm;
1043 
1044  if (i < 0 || i >= n) { /* initial guess is useless */
1045  lo = -1;
1046  hi = n;
1047  } else {
1048  unsigned int step = 1; /* how much to step up/down */
1049 
1050  if (z > x[i]) { /* expand search upwards */
1051  if (i == n - 1) { /* off top of array */
1052  return (n - 1);
1053  }
1054 
1055  lo = i;
1056  hi = lo + 1;
1057  while (z >= x[hi]) {
1058  lo = hi;
1059  step += step; /* double step size */
1060  hi = lo + step;
1061  if (hi >= n) { /* reached top of array */
1062  hi = n - 1;
1063  break;
1064  }
1065  }
1066  } else { /* expand it downwards */
1067  if (i == 0) { /* off bottom of array */
1068  return (-1);
1069  }
1070 
1071  hi = i;
1072  lo = i - 1;
1073  while (z < x[lo]) {
1074  hi = lo;
1075  step += step; /* double step size */
1076  lo = hi - step;
1077  if (lo < 0) { /* off bottom of array */
1078  lo = -1;
1079  break;
1080  }
1081  }
1082  }
1083  }
1084  /*
1085  * perform bisection
1086  */
1087  while (hi - lo > 1) {
1088  mid = (lo + hi) / 2;
1089  xm = x[mid];
1090  if (z <= xm) {
1091  hi = mid;
1092  } else {
1093  lo = mid;
1094  }
1095  }
1096 
1097  if (lo == -1) { /* off the bottom */
1098  return (lo);
1099  }
1100  /*
1101  * If there's a discontinuity (many knots at same x-value), choose the
1102  * largest
1103  */
1104  xm = x[lo];
1105  while (lo < n - 1 && x[lo + 1] == xm) lo++;
1106 
1107  return (lo);
1108 }
1109 
1113 static void keep_valid_roots(std::vector<double> &roots, std::vector<double> &newRoots, double x0,
1114  double x1) {
1115  for (unsigned int i = 0; i != newRoots.size(); ++i) {
1116  if (newRoots[i] >= x0 && newRoots[i] < x1) { // keep this root
1117  roots.push_back(newRoots[i]);
1118  }
1119  }
1120 
1121  newRoots.clear();
1122 }
1123 
1127 namespace {
1128 void do_quadratic(double a, double b, double c, std::vector<double> &roots) {
1129  if (::fabs(a) < std::numeric_limits<double>::epsilon()) {
1130  if (::fabs(b) >= std::numeric_limits<double>::epsilon()) {
1131  roots.push_back(-c / b);
1132  }
1133  } else {
1134  double const tmp = b * b - 4 * a * c;
1135 
1136  if (tmp >= 0) {
1137  if (b >= 0) {
1138  roots.push_back((-b - sqrt(tmp)) / (2 * a));
1139  } else {
1140  roots.push_back((-b + sqrt(tmp)) / (2 * a));
1141  }
1142  roots.push_back(c / (a * roots[0]));
1143  /*
1144  * sort roots
1145  */
1146  if (roots[0] > roots[1]) {
1147  double const tmp2 = roots[0];
1148  roots[0] = roots[1];
1149  roots[1] = tmp2;
1150  }
1151  }
1152  }
1153 }
1154 
1158 void do_cubic(double a, double b, double c, double d, std::vector<double> &roots) {
1159  if (::fabs(a) < std::numeric_limits<double>::epsilon()) {
1160  do_quadratic(b, c, d, roots);
1161  return;
1162  }
1163  b /= a;
1164  c /= a;
1165  d /= a;
1166 
1167  double const q = (b * b - 3 * c) / 9;
1168  double const r = (2 * b * b * b - 9 * b * c + 27 * d) / 54;
1169  /*
1170  * n.b. note that the test for the number of roots is carried out on the
1171  * same variables as are used in (e.g.) the acos, as it is possible for
1172  * r*r < q*q*q && r > sq*sq*sq due to rounding.
1173  */
1174  double const sq = (q >= 0) ? sqrt(q) : -sqrt(-q);
1175  double const sq3 = sq * sq * sq;
1176  if (::fabs(r) < sq3) { // three real roots
1177  double const theta = ::acos(r / sq3); // sq3 cannot be zero
1178 
1179  roots.push_back(-2 * sq * cos(theta / 3) - b / 3);
1180  roots.push_back(-2 * sq * cos((theta + afwGeom::TWOPI) / 3) - b / 3);
1181  roots.push_back(-2 * sq * cos((theta - afwGeom::TWOPI) / 3) - b / 3);
1182  /*
1183  * sort roots
1184  */
1185  if (roots[0] > roots[1]) {
1186  std::swap(roots[0], roots[1]);
1187  }
1188  if (roots[1] > roots[2]) {
1189  std::swap(roots[1], roots[2]);
1190  }
1191  if (roots[0] > roots[1]) {
1192  std::swap(roots[0], roots[1]);
1193  }
1194 
1195  return;
1196  } else if (::fabs(r) == sq3) { /* no more than two real roots */
1197  double const aa = -((r < 0) ? -::pow(-r, 1.0 / 3.0) : ::pow(r, 1.0 / 3.0));
1198 
1199  if (::fabs(aa) < std::numeric_limits<double>::epsilon()) { /* degenerate case; one real root */
1200  roots.push_back(-b / 3);
1201  return;
1202  } else {
1203  roots.push_back(2 * aa - b / 3);
1204  roots.push_back(-aa - b / 3);
1205 
1206  if (roots[0] > roots[1]) {
1207  std::swap(roots[0], roots[1]);
1208  }
1209 
1210  return;
1211  }
1212  } else { /* only one real root */
1213  double tmp = ::sqrt(r * r - (q > 0 ? sq3 * sq3 : -sq3 * sq3));
1214  tmp = r + (r < 0 ? -tmp : tmp);
1215  double const aa = -((tmp < 0) ? -::pow(-tmp, 1.0 / 3.0) : ::pow(tmp, 1.0 / 3.0));
1216  double const bb = (fabs(aa) < std::numeric_limits<double>::epsilon()) ? 0 : q / aa;
1217 
1218  roots.push_back((aa + bb) - b / 3);
1219 #if 0
1220  roots.push_back(-(aa + bb)/2 - b/3); // the real
1221  roots.push_back(::sqrt(3)/2*(aa - bb)); // and imaginary parts of the complex roots
1222 #endif
1223  return;
1224  }
1225 }
1226 }
1227 
1228 std::vector<double> Spline::roots(double const value, double a, double const b) const {
1229  /*
1230  * Strategy: we know that the interpolant has the form
1231  * val = coef[0][i] +dx*(coef[1][i] + dx*(coef[2][i]/2 + dx*coef[3][i]/6))
1232  * so we can use the usual analytic solution for a cubic. Note that the
1233  * cubic quoted above returns dx, the distance from the previous knot,
1234  * rather than x itself
1235  */
1236  std::vector<double> roots; /* the roots found */
1237  double x0 = a; // lower end of current range
1238  double const x1 = b;
1239  int const nknot = _knots.size();
1240 
1241  int i0 = search_array(x0, &_knots[0], nknot, -1);
1242  int const i1 = search_array(x1, &_knots[0], nknot, i0);
1243  assert(i1 >= i0 && i1 <= nknot - 1);
1244 
1245  std::vector<double> newRoots; // the roots we find in some interval
1246  /*
1247  * Deal with special case that x0 may be off one end or the other of
1248  * the array of knots.
1249  */
1250  if (i0 < 0) { /* off bottom */
1251  i0 = 0;
1252  do_cubic(_coeffs[3][i0] / 6, _coeffs[2][i0] / 2, _coeffs[1][i0], _coeffs[0][i0] - value, newRoots);
1253  //
1254  // Could use
1255  // std::transform(newRoots.begin(), newRoots.end(), newRoots.begin(),
1256  // std::tr1::bind(std::plus<double>(), _1, _knots[i0]));
1257  // but let's not
1258  //
1259  for (unsigned int j = 0; j != newRoots.size(); ++j) {
1260  newRoots[j] += _knots[i0];
1261  }
1262  keep_valid_roots(roots, newRoots, x0, _knots[i0]);
1263 
1264  x0 = _knots[i0];
1265  } else if (i0 >= nknot) { /* off top */
1266  i0 = nknot - 1;
1267  assert(i0 >= 0);
1268  do_cubic(_coeffs[3][i0] / 6, _coeffs[2][i0] / 2, _coeffs[1][i0], _coeffs[0][i0] - value, newRoots);
1269 
1270  for (unsigned int j = 0; j != newRoots.size(); ++j) {
1271  newRoots[j] += _knots[i0];
1272  }
1273  keep_valid_roots(roots, newRoots, x0, x1);
1274 
1275  return roots;
1276  }
1277  /*
1278  * OK, now search in main body of spline. Note that i1 may be nknot - 1, and
1279  * in any case the right hand limit of the last segment is at x1, not a knot
1280  */
1281  for (int i = i0; i <= i1; i++) {
1282  do_cubic(_coeffs[3][i] / 6, _coeffs[2][i] / 2, _coeffs[1][i], _coeffs[0][i] - value, newRoots);
1283 
1284  for (unsigned int j = 0; j != newRoots.size(); ++j) {
1285  newRoots[j] += _knots[i];
1286  }
1287  keep_valid_roots(roots, newRoots, ((i == i0) ? x0 : _knots[i]), ((i == i1) ? x1 : _knots[i + 1]));
1288  }
1289 
1290  return roots;
1291 }
1292 }
1293 }
1294 }
1295 }
std::vector< std::vector< double > > _coeffs
Definition: Spline.h:51
std::vector< double > _knots
Definition: Spline.h:50
std::vector< double > roots(double const value, double const x0, double const x1) const
Find the roots of Spline - val = 0 in the range [x0, x1).
Definition: Spline.cc:1228
SmoothedSpline(std::vector< double > const &x, std::vector< double > const &y, std::vector< double > const &dy, double s, double *chisq=NULL, std::vector< double > *errs=NULL)
Cubic spline data smoother.
Definition: Spline.cc:536
int const step
Definition: BoundedField.cc:97
void derivative(std::vector< double > const &x, std::vector< double > &dydx) const
Find the derivative of a Spline.
Definition: Spline.cc:59
A base class for image defects.
Definition: cameraGeom.dox:3
void interpolate(std::vector< double > const &x, std::vector< double > &y) const
Interpolate a Spline.
Definition: Spline.cc:31
double x
void _allocateSpline(int const nknot)
Allocate the storage a Spline needs.
Definition: Spline.cc:23
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:442
TautSpline(std::vector< double > const &x, std::vector< double > const &y, double const gamma=0, Symmetry type=Unknown)
Construct cubic spline interpolant to given data.
Definition: Spline.cc:88
double constexpr TWOPI
Definition: Angle.h:22