Interpolate.h
Go to the documentation of this file.
1 /**
2  Interpolationn helpers.
3 
4  See test_interpolate.cxx.
5  */
6 
7 #ifndef WIRECELLUTIL_INTERPOLATE
8 #define WIRECELLUTIL_INTERPOLATE
9 
10 #include <vector>
11 
12 namespace WireCell {
13 
14 
15 
16  /**
17  Use like:
18 
19  linterp<double> lin(f.begin(), f.end(), x0, xstep);
20  ...
21  double y = lin(42.0);
22 
23  where "f" is some kind of collection of doubles.
24 
25  */
26  template<class Real>
27  class linterp {
28  public:
29  template<class BidiIterator>
30  linterp(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step)
31  : m_dat(f,end_p), m_le(left_endpoint), m_step(step) {
32  m_re = m_le + m_step * (m_dat.size()-1);
33  }
34 
35  Real operator()(Real x) const {
36  if (x <= m_le) return m_dat.front();
37  if (x >= m_re) return m_dat.back();
38 
39  int ind = int((x-m_le)/m_step);
40  Real y0 = m_dat[ind];
41  Real y1 = m_dat[ind+1];
42  Real x0 = m_le + ind * m_step;
43 
44  return y0 + (x-x0) * (y1-y0) / m_step;
45  }
46 
47  private:
48  std::vector<Real> m_dat;
49  Real m_le, m_re, m_step;
50  };
51 
52 
53 
54  /** You may also want to use Boost for fancier interpolation.
55  * They have similar calling interface:
56  #include <boost/math/interpolators/cubic_b_spline.hpp>
57  #include <iostream>
58  ...
59  boost::math::cubic_b_spline<double> spline(f.begin(), f.end(), x0, xstep);
60  spline(42);
61  *
62  * More info here:
63  https://www.boost.org/doc/libs/1_65_0/libs/math/doc/html/math_toolkit/interpolate/cubic_b.html
64  */
65 
66 
67 }
68 
69 #endif
Real operator()(Real x) const
Definition: Interpolate.h:35
linterp(BidiIterator f, BidiIterator end_p, Real left_endpoint, Real step)
Definition: Interpolate.h:30
std::vector< Real > m_dat
Definition: Interpolate.h:48
Definition: Main.h:22
list x
Definition: train.py:276