33 #include "boost/format.hpp"
34 #include "boost/shared_ptr.hpp"
35 #include "gsl/gsl_errno.h"
36 #include "gsl/gsl_interp.h"
37 #include "gsl/gsl_spline.h"
48 std::pair<std::vector<double>, std::vector<double> >
49 recenter(std::vector<double>
const &
x,
50 std::vector<double>
const &
y)
52 if (x.size() != y.size()) {
53 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
54 str(
boost::format(
"Dimensions of x and y must match; %ul != %ul")
55 % x.size() % y.size()));
57 std::size_t
const len = x.size();
59 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
60 "You must provide at least 1 point");
61 }
else if (len == 1) {
62 return std::make_pair(x, y);
65 std::vector<double> recentered_x(len + 1);
66 std::vector<double> recentered_y(len + 1);
68 recentered_x[0] = 0.5*(3*x[0] - x[1]);
69 recentered_y[0] = y[0];
71 for (std::size_t i = 0, j = 1; i < len - 1; ++i, ++j) {
72 recentered_x[j] = 0.5*(x[i] + x[i + 1]);
73 recentered_y[j] = 0.5*(y[i] + y[i + 1]);
75 recentered_x[len] = 0.5*(3*x[len - 1] - x[len - 2]);
76 recentered_y[len] = y[len - 1];
78 return std::make_pair(recentered_x, recentered_y);
90 std::vector<double>
const &y,
94 mutable std::vector<double>::const_iterator
_old;
109 if (xInterp < *
_old) {
115 if (
_old <
_x.end() - 1 and xInterp < *(
_old + 1)) {
120 std::vector<double>::const_iterator low = std::upper_bound(
_old,
_x.end(), xInterp);
125 low = std::upper_bound(
_x.begin(), low + 1, xInterp);
130 if (low ==
_x.end()) {
131 return _y[
_y.size() - 1];
132 }
else if (low ==
_x.begin()) {
137 return _y[low -
_x.begin()];
146 ::gsl_interp_type
const *
151 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"CONSTANT interpolation not supported.");
153 return ::gsl_interp_linear;
155 return ::gsl_interp_cspline;
157 return ::gsl_interp_cspline;
159 return ::gsl_interp_cspline_periodic;
161 return ::gsl_interp_akima;
163 return ::gsl_interp_akima_periodic;
165 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
166 "I am unable to make an interpolator of type UNKNOWN");
169 str(
boost::format(
"You can't get here: style == %") % style));
189 std::vector<double>
const &y,
192 Interpolate(x, y), _interpType(styleToGslInterpType(style))
194 _acc = ::gsl_interp_accel_alloc();
196 throw LSST_EXCEPT(pex::exceptions::MemoryError,
"gsl_interp_accel_alloc failed");
201 throw LSST_EXCEPT(pex::exceptions::OutOfRangeError,
202 str(
boost::format(
"Failed to initialise spline for type %s, length %d")
210 int const status = ::gsl_interp_init(
_interp, &x[0], &y[0],
_y.size());
214 % ::gsl_strerror(status) % status));
220 ::gsl_interp_accel_free(
_acc);
228 if ((xInterp <
_x.front() || (xInterp >
_x.back()))) {
239 if (xInterp <
_x.front()) {
249 double d2 = ::gsl_interp_eval_deriv2(
_interp, &
_x[0], &
_y[0], x0,
_acc);
250 return y0 + (xInterp -
x0)*d + 0.5*(xInterp - x0)*(xInterp -
x0)*d2;
252 assert(xInterp >=
_x.front());
253 assert(xInterp <=
_x.back());
265 static std::map<std::string, Interpolate::Style> gslInterpTypeStrings;
266 if (gslInterpTypeStrings.empty()) {
276 if ( gslInterpTypeStrings.find(style) == gslInterpTypeStrings.end()) {
277 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"Interp style not found: "+style);
279 return gslInterpTypeStrings[style];
288 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"n must be greater than 0");
292 static std::vector<Interpolate::Style> styles;
293 if (styles.empty()) {
311 static std::vector<int> minPoints;
312 if (minPoints.empty()) {
324 return minPoints[style];
326 throw LSST_EXCEPT(pex::exceptions::OutOfRangeError,
342 std::pair<std::vector<double>, std::vector<double> >
const xy,
345 ) : _x(xy.first), _y(xy.second), _style(style)
354 std::vector<
double> const &y,
Interpolate::Style stringToInterpStyle(std::string const &style)
Conversion function to switch a string to an Interpolate::Style.
Interpolate values for a set of x,y vector<>s.
std::vector< double >::const_iterator _old
virtual double interpolate(double const x) const
Interpolate a constant to the point xInterp.
friend boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style)
Include files required for standard LSST Exception handling.
boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=Interpolate::AKIMA_SPLINE)
virtual double interpolate(double const x) const
::gsl_interp_type const * _interpType
virtual ~InterpolateGsl()
::gsl_interp_accel * _acc
std::vector< double > const _x
InterpolateConstant(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style)
InterpolateGsl(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style)
Interpolate::Style lookupMaxInterpStyle(int const n)
Get the highest order Interpolation::Style available for 'n' points.
int lookupMinInterpPoints(Interpolate::Style const style)
Get the minimum number of points needed to use the requested interpolation style. ...
#define LSST_EXCEPT(type,...)
Interpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=UNKNOWN)
friend boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style)
std::vector< double > const _y
virtual ~InterpolateConstant()