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));
165 throw LSST_EXCEPT(pex::exceptions::LogicError,
166 str(boost::format(
"You can't get here: style == %") % style));
171 friend std::shared_ptr<Interpolate>
makeInterpolate(std::vector<double>
const &x,
172 std::vector<double>
const &y,
180 InterpolateGsl(std::vector<double>
const &x, std::vector<double>
const &y,
183 ::gsl_interp_type
const *_interpType;
184 ::gsl_interp_accel *_acc;
185 ::gsl_interp *_interp;
188 InterpolateGsl::InterpolateGsl(std::vector<double>
const &x,
189 std::vector<double>
const &y,
192 :
Interpolate(x, y), _interpType(styleToGslInterpType(style)) {
194 ::gsl_set_error_handler_off();
196 _acc = ::gsl_interp_accel_alloc();
198 throw LSST_EXCEPT(pex::exceptions::MemoryError,
"gsl_interp_accel_alloc failed");
201 _interp = ::gsl_interp_alloc(_interpType,
_y.size());
203 throw LSST_EXCEPT(pex::exceptions::OutOfRangeError,
204 str(boost::format(
"Failed to initialise spline for type %s, length %d") %
205 _interpType->name %
_y.size()));
211 int const status = ::gsl_interp_init(_interp, &x[0], &y[0],
_y.size());
214 pex::exceptions::RuntimeError,
215 str(boost::format(
"gsl_interp_init failed: %s [%d]") % ::gsl_strerror(status) % status));
220 ::gsl_interp_free(_interp);
221 ::gsl_interp_accel_free(_acc);
228 if ((xInterp <
_x.front() || (xInterp >
_x.back()))) {
239 if (xInterp <
_x.front()) {
247 double d = ::gsl_interp_eval_deriv(_interp, &
_x[0], &
_y[0], x0, _acc);
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());
254 return ::gsl_interp_eval(_interp, &
_x[0], &
_y[0], xInterp, _acc);
258 static std::map<std::string, Interpolate::Style> gslInterpTypeStrings;
259 if (gslInterpTypeStrings.empty()) {
269 if (gslInterpTypeStrings.find(style) == gslInterpTypeStrings.end()) {
270 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"Interp style not found: " + style);
272 return gslInterpTypeStrings[style];
277 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
"n must be greater than 0");
281 static std::vector<Interpolate::Style> styles;
282 if (styles.empty()) {
296 size_t const num = x.size();
297 std::vector<double> out(num);
298 for (
size_t i = 0; i < num; ++i) {
305 int const num = x.getShape()[0];
306 ndarray::Array<double, 1> out = ndarray::allocate(ndarray::makeVector(num));
307 for (
int i = 0; i < num; ++i) {
308 std::cout <<
"Interpolating " << x[i] << std::endl;
315 static std::vector<int> minPoints;
316 if (minPoints.empty()) {
328 return minPoints[style];
331 pex::exceptions::OutOfRangeError,
343 std::shared_ptr<Interpolate>
makeInterpolate(std::vector<double>
const &x, std::vector<double>
const &y,
349 return std::shared_ptr<Interpolate>(
new InterpolateGsl(x, y, style));
353 std::shared_ptr<Interpolate>
makeInterpolate(ndarray::Array<double const, 1>
const &x,
354 ndarray::Array<double const, 1>
const &y,
356 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()