1 #ifndef GRIDINTERPOLATION_IMPL
2 #error Please, include "MathUtils/interpolation/GridInterpolation.h"
11 template <
typename T,
typename Enable =
void>
18 struct InterpolationImpl<T, typename std::enable_if<std::is_floating_point<T>::value>::type> {
24 template <
typename... Rest>
29 if ((x < knots.
front() || x > knots.
back()) && !extrapolate) {
33 if (knots.
size() == 1) {
34 return (*interpolators[0])(rest...);
40 }
else if (x2i == knots.
size()) {
45 double y1 = (*interpolators[x1i])(rest...);
46 double y2 = (*interpolators[x2i])(rest...);
62 if (x < knots.
front() || x > knots.
back())
67 template <
typename... Rest>
71 if (x < knots.
front() || x > knots.
back())
73 return (*interpolators[x])(rest...);
77 if (knots.
front() != 0) {
80 for (
auto b = knots.
begin() + 1; b != knots.
end(); ++b) {
81 if (*b - *(b - 1) != 1) {
96 if (i >= knots.
size() || knots[i] != x)
101 template <
typename... Rest>
104 const Rest... rest) {
106 if (i >= knots.
size() || knots[i] != x)
108 return (*interpolators[i])(rest...);
119 template <
typename T>
131 : m_knots(std::get<0>(grid)), m_values(values.begin(), values.end()), m_extrapolate(extrapolate) {
136 if (m_knots.size() != values.
size()) {
138 << m_knots.size() <<
" != " << m_values.size();
171 template <
typename T,
typename... Rest>
183 : m_extrapolate(extrapolate) {
190 m_knots = std::get<0>(grid);
192 if (m_knots.size() != values.
shape().
back()) {
197 m_interpolators.resize(m_knots.size());
198 for (
size_t i = 0; i < m_knots.size(); ++i) {
199 auto subvalues = values.
rslice(i);
200 m_interpolators[i].reset(
new InterpN<Rest...>(subgrid, subvalues, extrapolate));
220 InterpN(
const InterpN& other) : m_knots(other.m_knots), m_extrapolate(other.m_extrapolate) {
221 m_interpolators.resize(m_knots.size());
222 for (
size_t i = 0; i < m_interpolators.size(); ++i) {
InterpN(const std::tuple< std::vector< T >, std::vector< Rest >...> &grid, const NdArray::NdArray< double > &values, bool extrapolate)
static void checkOrder(const std::vector< T > &knots)
double operator()(const T x) const
double operator()(T x, Rest...rest) const
static std::tuple< Tn...> Tail(std::tuple< T0, Tn...> &&tuple)
static void checkOrder(const std::vector< T > &)
static double interpolate(const T x, const std::vector< T > &knots, const std::vector< double > &values, bool)
std::vector< std::unique_ptr< InterpN< Rest...> > > m_interpolators
ELEMENTS_API double simple_interpolation(double x, const std::vector< double > &xp, const std::vector< double > &yp, bool extrapolate=false)
static double interpolate(const T x, const std::vector< T > &knots, const std::vector< double > &values, bool)
const std::vector< size_t > & shape() const
static double interpolate(const T x, const std::vector< T > &knots, const std::vector< std::unique_ptr< InterpN< Rest...>>> &interpolators, bool, const Rest...rest)
static double interpolate(const T x, const std::vector< T > &knots, const std::vector< double > &values, bool extrapolate)
static void checkOrder(const std::vector< T > &knots)
InterpN(const InterpN &other)
Copy constructor.
self_type rslice(size_t i)
std::vector< double > m_values
static double interpolate(const T x, const std::vector< T > &knots, const std::vector< std::unique_ptr< InterpN< Rest...>>> &interpolators, bool, const Rest...rest)
ELEMENTS_API std::unique_ptr< Function > interpolate(const std::vector< double > &x, const std::vector< double > &y, InterpolationType type, bool extrapolate=false)
InterpN(const std::tuple< std::vector< T >> &grid, const NdArray::NdArray< double > &values, bool extrapolate)
InterpN(const std::tuple< std::vector< AxisType >...> &grid, const NdArray::NdArray< double > &values, bool extrapolate)
static double interpolate(const T x, const std::vector< T > &knots, const std::vector< std::unique_ptr< InterpN< Rest...>>> &interpolators, bool extrapolate, const Rest...rest)