LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
lsst::afw::math::detail::Spline Class Reference

#include <Spline.h>

Inheritance diagram for lsst::afw::math::detail::Spline:
lsst::afw::math::detail::SmoothedSpline lsst::afw::math::detail::TautSpline

Public Member Functions

virtual ~Spline ()
 
void interpolate (std::vector< double > const &x, std::vector< double > &y) const
 
void derivative (std::vector< double > const &x, std::vector< double > &dydx) const
 
std::vector< double > roots (double const value, double const x0, double const x1) const
 

Protected Member Functions

 Spline ()
 
void _allocateSpline (int const nknot)
 

Protected Attributes

std::vector< double > _knots
 
std::vector< std::vector
< double > > 
_coeffs
 

Detailed Description

Definition at line 12 of file Spline.h.

Constructor & Destructor Documentation

virtual lsst::afw::math::detail::Spline::~Spline ( )
inlinevirtual

Definition at line 14 of file Spline.h.

14 {}
lsst::afw::math::detail::Spline::Spline ( )
inlineprotected

Definition at line 29 of file Spline.h.

29 {}

Member Function Documentation

void lsst::afw::math::detail::Spline::_allocateSpline ( int const  nknot)
protected

Definition at line 25 of file Spline.cc.

26 {
27  _knots.resize(nknot);
28  _coeffs.resize(4);
29  for (unsigned int i = 0; i != _coeffs.size(); ++i) {
30  _coeffs[i].reserve(nknot);
31  }
32 }
std::vector< std::vector< double > > _coeffs
Definition: Spline.h:33
std::vector< double > _knots
Definition: Spline.h:32
void lsst::afw::math::detail::Spline::derivative ( std::vector< double > const &  x,
std::vector< double > &  dydx 
) const

Find the derivative of a Spline.

Parameters
xpoints to evaluate derivative at
dydxderivatives at x

Definition at line 73 of file Spline.cc.

76 {
77  int const nknot = _knots.size();
78  int const n = x.size();
79 
80  dydx.resize(n); // may default-construct elements which is a little inefficient
81  /*
82  * For _knots[i] <= x <= _knots[i+1], the * interpolant has the form
83  * val = _coeff[0][i] +dx*(_coeff[1][i] + dx*(_coeff[2][i]/2 + dx*_coeff[3][i]/6))
84  * with
85  * dx = x - knots[i]
86  * so the derivative is
87  * val = _coeff[1][i] + dx*(_coeff[2][i] + dx*_coeff[3][i]/2))
88  */
89 
90  int ind = -1; // no idea initially
91  for (int i = 0; i != n; ++i) {
92  ind = search_array(x[i], &_knots[0], nknot, ind);
93 
94  if(ind < 0) { // off bottom
95  ind = 0;
96  } else if(ind >= nknot) { // off top
97  ind = nknot - 1;
98  }
99 
100  double const dx = x[i] - _knots[ind];
101  dydx[i] = _coeffs[1][ind] + dx*(_coeffs[2][ind] + dx*_coeffs[3][ind]/2);
102  }
103 }
std::vector< std::vector< double > > _coeffs
Definition: Spline.h:33
std::vector< double > _knots
Definition: Spline.h:32
void lsst::afw::math::detail::Spline::interpolate ( std::vector< double > const &  x,
std::vector< double > &  y 
) const

Interpolate a Spline.

Parameters
xpoints to interpolate at
yinterpolated values at x

Definition at line 38 of file Spline.cc.

41 {
42  int const nknot = _knots.size();
43  int const n = x.size();
44 
45  y.resize(n); // may default-construct elements which is a little inefficient
46  /*
47  * For _knots[i] <= x <= _knots[i+1], the interpolant
48  * has the form
49  * val = _coeff[0][i] +dx*(_coeff[1][i] + dx*(_coeff[2][i]/2 + dx*_coeff[3][i]/6))
50  * with
51  * dx = x - knots[i]
52  */
53  int ind = -1; // no idea initially
54  for (int i = 0; i != n; ++i) {
55  ind = search_array(x[i], &_knots[0], nknot, ind);
56 
57  if(ind < 0) { // off bottom
58  ind = 0;
59  } else if(ind >= nknot) { // off top
60  ind = nknot - 1;
61  }
62 
63  double const dx = x[i] - _knots[ind];
64  y[i] = _coeffs[0][ind] + dx*(_coeffs[1][ind] + dx*(_coeffs[2][ind]/2 + dx*_coeffs[3][ind]/6));
65  }
66 }
std::vector< std::vector< double > > _coeffs
Definition: Spline.h:33
std::vector< double > _knots
Definition: Spline.h:32
std::vector< double > lsst::afw::math::detail::Spline::roots ( double const  value,
double const  x0,
double const  x1 
) const
Parameters
valuedesired value
x0specify desired range is [x0,x1)
x1specify desired range is [x0,x1)

Definition at line 1472 of file Spline.cc.

1476 {
1477  /*
1478  * Strategy: we know that the interpolant has the form
1479  * val = coef[0][i] +dx*(coef[1][i] + dx*(coef[2][i]/2 + dx*coef[3][i]/6))
1480  * so we can use the usual analytic solution for a cubic. Note that the
1481  * cubic quoted above returns dx, the distance from the previous knot,
1482  * rather than x itself
1483  */
1484  std::vector<double> roots; /* the roots found */
1485  double x0 = a; // lower end of current range
1486  double const x1 = b;
1487  int const nknot = _knots.size();
1488 
1489  int i0 = search_array(x0, &_knots[0], nknot, -1);
1490  int const i1 = search_array(x1, &_knots[0], nknot, i0);
1491  assert (i1 >= i0 && i1 <= nknot - 1);
1492 
1493  std::vector<double> newRoots; // the roots we find in some interval
1494 /*
1495  * Deal with special case that x0 may be off one end or the other of
1496  * the array of knots.
1497  */
1498  if(i0 < 0) { /* off bottom */
1499  i0 = 0;
1500  do_cubic(_coeffs[3][i0]/6, _coeffs[2][i0]/2, _coeffs[1][i0], _coeffs[0][i0] - value, newRoots);
1501  //
1502  // Could use
1503  // std::transform(newRoots.begin(), newRoots.end(), newRoots.begin(),
1504  // std::tr1::bind(std::plus<double>(), _1, _knots[i0]));
1505  // but let's not
1506  //
1507  for (unsigned int j = 0; j != newRoots.size(); ++j) {
1508  newRoots[j] += _knots[i0];
1509  }
1510  keep_valid_roots(roots, newRoots, x0, _knots[i0]);
1511 
1512  x0 = _knots[i0];
1513  } else if(i0 >= nknot) { /* off top */
1514  i0 = nknot - 1;
1515  assert (i0 >= 0);
1516  do_cubic(_coeffs[3][i0]/6, _coeffs[2][i0]/2, _coeffs[1][i0], _coeffs[0][i0] - value, newRoots);
1517 
1518  for (unsigned int j = 0; j != newRoots.size(); ++j) {
1519  newRoots[j] += _knots[i0];
1520  }
1521  keep_valid_roots(roots, newRoots, x0, x1);
1522 
1523  return roots;
1524  }
1525 /*
1526  * OK, now search in main body of spline. Note that i1 may be nknot - 1, and
1527  * in any case the right hand limit of the last segment is at x1, not a knot
1528  */
1529  for (int i = i0;i <= i1;i++) {
1530  do_cubic(_coeffs[3][i]/6, _coeffs[2][i]/2, _coeffs[1][i], _coeffs[0][i] - value, newRoots);
1531 
1532  for (unsigned int j = 0; j != newRoots.size(); ++j) {
1533  newRoots[j] += _knots[i];
1534  }
1535  keep_valid_roots(roots, newRoots, ((i == i0) ? x0 : _knots[i]), ((i == i1) ? x1 : _knots[i + 1]));
1536  }
1537 
1538  return roots;
1539 }
std::vector< std::vector< double > > _coeffs
Definition: Spline.h:33
std::vector< double > _knots
Definition: Spline.h:32
int const x0
Definition: saturated.cc:45
std::vector< double > roots(double const value, double const x0, double const x1) const
Definition: Spline.cc:1472
afw::table::Key< double > b

Member Data Documentation

std::vector<std::vector<double> > lsst::afw::math::detail::Spline::_coeffs
protected

Definition at line 33 of file Spline.h.

std::vector<double> lsst::afw::math::detail::Spline::_knots
protected

Definition at line 32 of file Spline.h.


The documentation for this class was generated from the following files: