3#include <gsl/gsl_errno.h>
4#include <gsl/gsl_spline.h>
5#include <gsl/gsl_version.h>
11#ifdef GSL_MAJOR_VERSION
12#if GSL_MAJOR_VERSION == 1
51static const std::map<Method, const gsl_interp_type *> interp_method{
83 gsl_interp_accel *acc;
94 Interp(
const std::vector<double> &x,
const std::vector<double> &y,
96 : acc(gsl_interp_accel_alloc()),
97 spline(gsl_spline_alloc(interp_method.at(method), x.size())),
100 assert(x.size() == y.size() &&
101 "In Interp, input vectors x and y must have same size");
102 assert(x.size() >= gsl_interp_type_min_size(interp_method.at(method)) &&
103 "In Interp, certain interpolation methods require a minimum number "
105 gsl_spline_init(spline, x.data(), y.data(), x.size());
109 gsl_spline_free(spline);
110 gsl_interp_accel_free(acc);
120 if (x < x0 || x > xmax)
123 return gsl_spline_eval(spline, x, acc);
127 std::vector<double>
interp(
const std::vector<double> &x)
const {
128 std::vector<double> y;
139 std::vector<double>
operator()(
const std::vector<double> &x)
const {
154inline std::vector<double>
interpolate(
const std::vector<double> &x_in,
155 const std::vector<double> &y_in,
156 const std::vector<double> &x_out,
158 Interp i_func(x_in, y_in, method);
159 return i_func(x_out);
165static constexpr bool has_steffen_method() {
Stateful 1D interpolation object using GSL.
Definition Interpolator.hpp:80
double interp(double x) const
Returns interpolated y(x). Returns 0.0 if x is outside [x0, xmax].
Definition Interpolator.hpp:119
double operator()(double x) const
Returns interpolated y(x). Returns 0.0 if x is outside [x0, xmax].
Definition Interpolator.hpp:136
Interp(const Interpolator::Interp &)=delete
Copy construction deleted.
Interp(const std::vector< double > &x, const std::vector< double > &y, Method method=Method::cspline)
Construct interpolation object from data points.
Definition Interpolator.hpp:94
std::vector< double > operator()(const std::vector< double > &x) const
Returns interpolated y(x) for each point in x. Returns 0.0 outside range.
Definition Interpolator.hpp:139
std::vector< double > interp(const std::vector< double > &x) const
Returns interpolated y(x) for each point in x. Returns 0.0 outside range.
Definition Interpolator.hpp:127
Interp & operator=(const Interpolator::Interp &)=delete
Copy assignment deleted.
1D interpolation using GSL splines.
Definition Interpolator.hpp:26
std::vector< double > interpolate(const std::vector< double > &x_in, const std::vector< double > &y_in, const std::vector< double > &x_out, Method method=Method::cspline)
Convenience wrapper: interpolates y_in(x_in) and evaluates at x_out.
Definition Interpolator.hpp:154
Method
Interpolation method.
Definition Interpolator.hpp:33
@ polynomial
Polynomial interpolation; only suitable for small numbers of points.
@ akima_periodic
Akima spline with periodic boundary conditions.
@ cspline_periodic
Cubic spline with periodic boundary conditions; first and last y-values must match.
@ linear
Linear interpolation; no additional memory required.
@ cspline
Cubic spline with natural boundary conditions (zero second derivative at endpoints)
@ steffen
Steffen's monotone spline: no spurious oscillations between data points. GSL v2+ only.
@ akima
Akima spline with natural boundary conditions (Wodicka non-rounded corner algorithm)