BEM++  2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
hermite_interpolator.hpp
1 #ifndef hermite_interpolator_hpp
2 #define hermite_interpolator_hpp
3 
4 #include "../common/common.hpp"
5 #include "scalar_traits.hpp"
6 
7 #include <cassert>
8 #include <stdexcept>
9 #include <vector>
10 
11 namespace Fiber
12 {
13 
14 template <typename ValueType>
16 {
17 public:
18  typedef typename ScalarTraits<ValueType>::RealType CoordinateType;
19 
20  HermiteInterpolator() : m_start(0.), m_end(0.), m_n(0), m_interval(0.)
21  {}
22 
23  CoordinateType rangeStart() { return m_start; }
24  CoordinateType rangeEnd() { return m_end; }
25 
26  void initialize(CoordinateType start, CoordinateType end,
27  const std::vector<ValueType>& values,
28  const std::vector<ValueType>& derivatives) {
29  if (values.size() != derivatives.size())
30  throw std::invalid_argument("HermiteInterpolator::setData(): "
31  "'values' and 'derivatives' must "
32  "have the same length");
33  if (values.size() < 2)
34  throw std::invalid_argument("HermiteInterpolator::setData(): "
35  "at least two points are required");
36  if (end <= start)
37  throw std::invalid_argument("HermiteInterpolator::setData(): "
38  "'start' must be smaller than 'end'");
39  m_start = start;
40  m_end = end;
41  m_n = values.size();
42  m_interval = (end - start) / (m_n - 1);
43  m_values = values;
44  m_derivatives = derivatives;
45  }
46 
47  ValueType evaluate(CoordinateType x) const {
48  assert(x >= m_start && x <= m_end);
49  CoordinateType fpn;
50  const CoordinateType t = std::modf((x - m_start) / m_interval, &fpn);
51  const int n = int(fpn);
52  assert(n >= 0 && n < m_n);
53  assert(t >= 0 && t <= 1);
54  // Adapted from the chfev routine from SLATEC
55  const ValueType f_1 = m_values[n];
56  const ValueType f_2 = m_values[n+1];
57  const ValueType d_1 = m_derivatives[n] * m_interval;
58  const ValueType d_2 = m_derivatives[n+1] * m_interval;
59  const ValueType Delta = f_2 - f_1;
60  const ValueType Delta_1 = d_1 - Delta;
61  const ValueType Delta_2 = d_2 - Delta;
62  const ValueType c_2 = -(Delta_1 + Delta_1 + Delta_2);
63  const ValueType c_3 = Delta_1 + Delta_2;
64  return f_1 + t * (d_1 + t * (c_2 + t * c_3));
65  }
66 
67 private:
69  CoordinateType m_start, m_end;
70  int m_n;
71  CoordinateType m_interval;
72  std::vector<ValueType> m_values;
73  std::vector<ValueType> m_derivatives;
75 };
76 
77 } // namespace Fiber
78 
79 #endif
Traits of scalar types.
Definition: scalar_traits.hpp:40
Definition: hermite_interpolator.hpp:15