33 #include "boost/format.hpp"
35 #include "gsl/gsl_errno.h"
36 #include "gsl/gsl_interp.h"
37 #include "gsl/gsl_spline.h"
49 std::pair<std::vector<double>, std::vector<double> >
50 recenter(std::vector<double>
const &
x,
51 std::vector<double>
const &
y)
53 if (x.size() != y.size()) {
54 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
55 str(
boost::format(
"Dimensions of x and y must match; %ul != %ul")
56 % x.size() % y.size()));
58 std::size_t
const len = x.size();
61 "You must provide at least 1 point");
62 }
else if (len == 1) {
63 return std::make_pair(x, y);
66 std::vector<double> recentered_x(len + 1);
67 std::vector<double> recentered_y(len + 1);
69 recentered_x[0] = 0.5*(3*x[0] - x[1]);
70 recentered_y[0] = y[0];
72 for (std::size_t i = 0, j = 1; i < len - 1; ++i, ++j) {
73 recentered_x[j] = 0.5*(x[i] + x[i + 1]);
74 recentered_y[j] = 0.5*(y[i] + y[i + 1]);
76 recentered_x[len] = 0.5*(3*x[len - 1] - x[len - 2]);
77 recentered_y[len] = y[len - 1];
79 return std::make_pair(recentered_x, recentered_y);
91 std::vector<double>
const &y,
95 mutable std::vector<double>::const_iterator
_old;
110 if (xInterp < *
_old) {
116 if (
_old <
_x.end() - 1 and xInterp < *(
_old + 1)) {
121 std::vector<double>::const_iterator low = std::upper_bound(
_old,
_x.end(), xInterp);
126 low = std::upper_bound(
_x.begin(), low + 1, xInterp);
131 if (low ==
_x.end()) {
132 return _y[
_y.size() - 1];
133 }
else if (low ==
_x.begin()) {
138 return _y[low -
_x.begin()];
147 ::gsl_interp_type
const *
152 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"CONSTANT interpolation not supported.");
154 return ::gsl_interp_linear;
156 return ::gsl_interp_cspline;
158 return ::gsl_interp_cspline;
160 return ::gsl_interp_cspline_periodic;
162 return ::gsl_interp_akima;
164 return ::gsl_interp_akima_periodic;
166 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
167 "I am unable to make an interpolator of type UNKNOWN");
170 str(
boost::format(
"You can't get here: style == %") % style));
190 std::vector<double>
const &y,
193 Interpolate(x, y), _interpType(styleToGslInterpType(style))
195 _acc = ::gsl_interp_accel_alloc();
197 throw LSST_EXCEPT(pex::exceptions::MemoryError,
"gsl_interp_accel_alloc failed");
202 throw LSST_EXCEPT(pex::exceptions::OutOfRangeError,
203 str(
boost::format(
"Failed to initialise spline for type %s, length %d")
211 int const status = ::gsl_interp_init(
_interp, &x[0], &y[0],
_y.size());
215 % ::gsl_strerror(status) % status));
221 ::gsl_interp_accel_free(
_acc);
229 if ((xInterp <
_x.front() || (xInterp >
_x.back()))) {
240 if (xInterp <
_x.front()) {
250 double d2 = ::gsl_interp_eval_deriv2(
_interp, &
_x[0], &
_y[0], x0,
_acc);
251 return y0 + (xInterp -
x0)*d + 0.5*(xInterp - x0)*(xInterp -
x0)*d2;
253 assert(xInterp >=
_x.front());
254 assert(xInterp <=
_x.back());
266 static std::map<std::string, Interpolate::Style> gslInterpTypeStrings;
267 if (gslInterpTypeStrings.empty()) {
277 if ( gslInterpTypeStrings.find(style) == gslInterpTypeStrings.end()) {
278 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"Interp style not found: "+style);
280 return gslInterpTypeStrings[style];
289 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"n must be greater than 0");
293 static std::vector<Interpolate::Style> styles;
294 if (styles.empty()) {
309 size_t const num = x.size();
310 std::vector<double> out(num);
311 for (
size_t i = 0; i < num; ++i) {
319 int const num = x.getShape()[0];
320 ndarray::Array<double, 1> out = ndarray::allocate(ndarray::makeVector(num));
321 for (
size_t i = 0; i < num; ++i) {
322 std::cout <<
"Interpolating " << x[i] << std::endl;
333 static std::vector<int> minPoints;
334 if (minPoints.empty()) {
346 return minPoints[style];
348 throw LSST_EXCEPT(pex::exceptions::OutOfRangeError,
364 std::pair<std::vector<double>, std::vector<double> >
const xy,
367 ) : _x(xy.first), _y(xy.second), _style(style)
376 std::vector<
double> const &y,
389 ndarray::Array<
double const, 1> const &y,
392 return makeInterpolate(std::vector<double>(x.begin(), x.end()), std::vector<double>(y.begin(), y.end()),
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)
A factory function to make Interpolate objects.
virtual double interpolate(double const x) const =0
Include files required for standard LSST Exception handling.
virtual double interpolate(double const x) const
boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style)
A factory function to make Interpolate objects.
::gsl_interp_type const * _interpType
virtual ~InterpolateGsl()
metadata import lsst afw display as afwDisplay n
::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,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Interpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=UNKNOWN)
Base class ctor.
friend boost::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style)
A factory function to make Interpolate objects.
std::vector< double > const _y
virtual ~InterpolateConstant()