31 #include "boost/format.hpp"
33 #include "gsl/gsl_errno.h"
34 #include "gsl/gsl_interp.h"
35 #include "gsl/gsl_spline.h"
37 #include "lsst/pex/exceptions.h"
45 std::pair<std::vector<double>, std::vector<double> > recenter(std::vector<double>
const &
x,
46 std::vector<double>
const &y) {
47 if (x.size() != y.size()) {
49 pex::exceptions::InvalidParameterError,
50 str(boost::format(
"Dimensions of x and y must match; %ul != %ul") % x.size() % y.size()));
52 std::size_t
const len = x.size();
54 throw LSST_EXCEPT(pex::exceptions::OutOfRangeError,
"You must provide at least 1 point");
55 }
else if (len == 1) {
56 return std::make_pair(x, y);
59 std::vector<double> recentered_x(len + 1);
60 std::vector<double> recentered_y(len + 1);
62 recentered_x[0] = 0.5 * (3 * x[0] - x[1]);
63 recentered_y[0] = y[0];
65 for (std::size_t i = 0, j = 1; i < len - 1; ++i, ++j) {
66 recentered_x[j] = 0.5 * (x[i] + x[i + 1]);
67 recentered_y[j] = 0.5 * (y[i] + y[i + 1]);
69 recentered_x[len] = 0.5 * (3 * x[len - 1] - x[len - 2]);
70 recentered_y[len] = y[len - 1];
72 return std::make_pair(recentered_x, recentered_y);
77 friend std::shared_ptr<Interpolate>
makeInterpolate(std::vector<double>
const &x,
78 std::vector<double>
const &y,
87 std::vector<double>
const &y,
91 mutable std::vector<double>::const_iterator _old;
104 if (xInterp < *_old) {
105 if (_old ==
_x.begin()) {
110 if (_old <
_x.end() - 1 and xInterp < *(_old + 1)) {
111 return _y[_old -
_x.begin()];
115 std::vector<double>::const_iterator low = std::upper_bound(_old,
_x.end(), xInterp);
118 if (low == _old && _old !=
_x.begin()) {
120 low = std::upper_bound(
_x.begin(), low + 1, xInterp);
125 if (low ==
_x.end()) {
126 return _y[
_y.size() - 1];
127 }
else if (low ==
_x.begin()) {
132 return _y[low -
_x.begin()];
143 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
144 "CONSTANT interpolation not supported.");
146 return ::gsl_interp_linear;
148 return ::gsl_interp_cspline;
150 return ::gsl_interp_cspline;
152 return ::gsl_interp_cspline_periodic;
154 return ::gsl_interp_akima;
156 return ::gsl_interp_akima_periodic;
158 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
159 "I am unable to make an interpolator of type UNKNOWN");
161 throw LSST_EXCEPT(pex::exceptions::LogicError,
162 str(boost::format(
"You can't get here: style == %") % style));
168 friend std::shared_ptr<Interpolate>
makeInterpolate(std::vector<double>
const &x,
169 std::vector<double>
const &y,
177 InterpolateGsl(std::vector<double>
const &x, std::vector<double>
const &y,
180 ::gsl_interp_type
const *_interpType;
181 ::gsl_interp_accel *_acc;
182 ::gsl_interp *_interp;
185 InterpolateGsl::InterpolateGsl(std::vector<double>
const &x,
186 std::vector<double>
const &y,
189 :
Interpolate(x, y), _interpType(styleToGslInterpType(style)) {
191 ::gsl_set_error_handler_off();
193 _acc = ::gsl_interp_accel_alloc();
195 throw LSST_EXCEPT(pex::exceptions::MemoryError,
"gsl_interp_accel_alloc failed");
198 _interp = ::gsl_interp_alloc(_interpType,
_y.size());
200 throw LSST_EXCEPT(pex::exceptions::OutOfRangeError,
201 str(boost::format(
"Failed to initialise spline for type %s, length %d") %
202 _interpType->name %
_y.size()));
208 int const status = ::gsl_interp_init(_interp, &x[0], &y[0],
_y.size());
211 pex::exceptions::RuntimeError,
212 str(boost::format(
"gsl_interp_init failed: %s [%d]") % ::gsl_strerror(status) % status));
217 ::gsl_interp_free(_interp);
218 ::gsl_interp_accel_free(_acc);
225 if ((xInterp <
_x.front() || (xInterp >
_x.back()))) {
236 if (xInterp <
_x.front()) {
244 double d = ::gsl_interp_eval_deriv(_interp, &
_x[0], &
_y[0], x0, _acc);
246 double d2 = ::gsl_interp_eval_deriv2(_interp, &
_x[0], &
_y[0], x0, _acc);
247 return y0 + (xInterp - x0) * d + 0.5 * (xInterp - x0) * (xInterp - x0) * d2;
249 assert(xInterp >=
_x.front());
250 assert(xInterp <=
_x.back());
251 return ::gsl_interp_eval(_interp, &
_x[0], &
_y[0], xInterp, _acc);
255 static std::map<std::string, Interpolate::Style> gslInterpTypeStrings;
256 if (gslInterpTypeStrings.empty()) {
266 if (gslInterpTypeStrings.find(style) == gslInterpTypeStrings.end()) {
267 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"Interp style not found: " + style);
269 return gslInterpTypeStrings[style];
274 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"n must be greater than 0");
278 static std::vector<Interpolate::Style> styles;
279 if (styles.empty()) {
293 size_t const num = x.size();
294 std::vector<double> out(num);
295 for (
size_t i = 0; i < num; ++i) {
302 int const num = x.getShape()[0];
303 ndarray::Array<double, 1> out = ndarray::allocate(ndarray::makeVector(num));
304 for (
size_t i = 0; i < num; ++i) {
305 std::cout <<
"Interpolating " << x[i] << std::endl;
312 static std::vector<int> minPoints;
313 if (minPoints.empty()) {
325 return minPoints[style];
328 pex::exceptions::OutOfRangeError,
340 std::shared_ptr<Interpolate>
makeInterpolate(std::vector<double>
const &x, std::vector<double>
const &y,
346 return std::shared_ptr<Interpolate>(
new InterpolateGsl(x, y, style));
350 std::shared_ptr<Interpolate>
makeInterpolate(ndarray::Array<double const, 1>
const &x,
351 ndarray::Array<double const, 1>
const &y,
353 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.
virtual double interpolate(double const x) const
virtual double interpolate(double const x) const =0
std::shared_ptr< Interpolate > makeInterpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=Interpolate::AKIMA_SPLINE)
A factory function to make Interpolate objects.
virtual double interpolate(double const x) const
virtual ~InterpolateGsl()
std::vector< double > const _x
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. ...
friend std::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.
Interpolate(std::vector< double > const &x, std::vector< double > const &y, Interpolate::Style const style=UNKNOWN)
Base class ctor.
std::vector< double > const _y
friend std::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 ~InterpolateConstant()