29 #ifndef gslInterpolator_hpp
30 #define gslInterpolator_hpp
33 #include <gsl/gsl_interp.h>
34 #include <gsl/gsl_errno.h>
36 #include "../mxException.hpp"
46 template<
typename _realT>
51 static const gsl_interp_type * interpolator()
53 return ::gsl_interp_linear;
61 template<
typename _realT>
66 static const gsl_interp_type * interpolator()
68 return ::gsl_interp_steffen;
80 template<
typename interpT>
84 typedef typename interpT::realT realT;
86 static_assert( std::is_same<
double,
typename std::remove_cv<realT>::type>::value,
"GSL Interpolation only works with double");
91 gsl_interp_accel *
m_acc {
nullptr};
109 std::vector<realT> & yin
133 void setup( std::vector<realT> & xin,
134 std::vector<realT> & yin
145 template<
typename interpT>
150 template<
typename interpT>
159 template<
typename interpT>
161 std::vector<realT> & yin
164 setup(xin.data(), yin.data(), xin.size());
167 template<
typename interpT>
170 if(m_interp !=
nullptr) gsl_interp_free(m_interp);
171 if(m_acc !=
nullptr) gsl_interp_accel_free (m_acc);
174 template<
typename interpT>
180 if(m_interp !=
nullptr) gsl_interp_free(m_interp);
181 if(m_acc !=
nullptr) gsl_interp_accel_free(m_acc);
183 m_interp = gsl_interp_alloc(interpT::interpolator(), Nin);
184 if(!m_interp) mxThrowException(
err::allocerr,
"gslInterpolation::setup",
"gsl_interp_alloc failed");
186 m_acc = gsl_interp_accel_alloc ();
187 if(!m_acc) mxThrowException(
err::allocerr,
"gslInterpolation::setup",
"gsl_interp_accel_alloc failed");
190 int errv = gsl_interp_init(m_interp, xin, yin, Nin);
194 mxThrowException(
err::liberr,
"gslInterpolation::setup", std::string(
"gsl_interp_init failed: ") + gsl_strerror(errv));
196 errv = gsl_interp_accel_reset(m_acc);
200 mxThrowException(
err::liberr,
"gslInterpolation::setup", std::string(
"gsl_interp_accel_reset failed: ") + gsl_strerror(errv));
206 template<
typename interpT>
208 std::vector<realT> & yin
211 if(xin.size() != yin.size())
213 mxThrowException(
err::sizeerr,
"gslInterpolator<interpT>::setup",
"input vectors must be the same size");
215 setup(xin.data(), yin.data(), xin.size());
218 template<
typename interpT>
222 int errv = gsl_interp_eval_e (m_interp, m_xin, m_yin, x, m_acc, &y);
223 if(errv != 0 && errv != GSL_EDOM)
225 mxThrowException(
err::liberr,
"gslInterpolation::setup",
"gsl_interp_eval_e failed");
mxException for an allocation error
mxException for errors returned by a library call
mxException for a size error
Class to manage interpolation using the GSL interpolation library.
gslInterpolator(std::vector< realT > &xin, std::vector< realT > &yin)
Vector constructor.
gsl_interp_accel * m_acc
the gsl interpolation accelerator structure
void setup(std::vector< realT > &xin, std::vector< realT > &yin)
Setup the interpolator for the supplied data vectors.
~gslInterpolator()
Destructor.
realT * m_xin
the input x data
realT operator()(const realT &x)
Calculate the interpolated function value at a point.
gslInterpolator()
Default constructor.
gslInterpolator(realT *xin, realT *yin, size_t Nin)
Raw pointer constructor
gsl_interp * m_interp
the gsl interpolator structure
void setup(realT *xin, realT *yin, size_t Nin)
Setup the interpolator for the supplied data pointers.
realT * m_yin
the input y data
GSL Linear Interpolation.
GSL Steffen Interpolation.