npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
interpolate.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_INTERPOLATE_HH_
2 #define NPSTAT_INTERPOLATE_HH_
3 
4 /*!
5 // \file interpolate.hh
6 //
7 // \brief Low-order polynomial interpolation (up to cubic)
8 // for equidistant coordinates
9 //
10 // Author: I. Volobouev
11 //
12 // October 2009
13 */
14 
15 #include <vector>
16 #include <cassert>
17 
19 
20 namespace npstat {
21  /**
22  // Linear interpolation. Assumes that
23  // the function values are given at 0 and 1.
24  */
25  template<typename T>
26  inline T interpolate_linear(const double x, const T& f0, const T& f1)
27  {
28  const typename ProperDblFromCmpl<T>::type dx = 1.0 - x;
29  return f0*dx + f1*static_cast<typename ProperDblFromCmpl<T>::type>(x);
30  }
31 
32  /**
33  // Quadratic interpolation. Assume that
34  // the function values are given at 0, 1, and 2.
35  */
36  template<typename T>
37  inline T interpolate_quadratic(const double x, const T& f0,
38  const T& f1, const T& f2)
39  {
40  static const typename ProperDblFromCmpl<T>::type two = 2.0;
41  const typename ProperDblFromCmpl<T>::type dx = x - 1.0;
42  return f1 + ((f2 - f0)/two + ((f2 - f1) + (f0 - f1))*(dx/two))*dx;
43  }
44 
45  /**
46  // Cubic interpolation. Assume that
47  // the function values are given at 0, 1, 2, and 3.
48  */
49  template<typename T>
50  inline T interpolate_cubic(const double x, const T& f0, const T& f1,
51  const T& f2, const T& f3)
52  {
53  return interpolate_linear(x*(3.0 - x)/2.0,
54  interpolate_linear(x/3.0, f0, f3),
55  interpolate_linear(x - 1.0, f1, f2));
56  }
57 
58  //@{
59  /**
60  // Get the coefficients of the interpolating polynomial.
61  // The interpolated function values are provided at 0, 1, ...
62  // The return value of the function is the number of
63  // coefficients (i.e., the polynomial degree plus one).
64  // On exit, the coefficients are placed into the "buffer"
65  // array in the order of increasing monomial degree.
66  // The length of the provided buffer must be sufficient
67  // to hold all these coefficients.
68  */
69  template<typename T>
70  unsigned interpolation_coefficients(T* buffer, unsigned bufLen,
71  const T& f0, const T& f1);
72  template<typename T>
73  unsigned interpolation_coefficients(T* buffer, unsigned bufLen,
74  const T& f0, const T& f1, const T& f2);
75  template<typename T>
76  unsigned interpolation_coefficients(T* buffer, unsigned bufLen,
77  const T& f0, const T& f1,
78  const T& f2, const T& f3);
79  //@}
80 #ifdef SWIG
81  inline std::vector<double> interpolation_coefficients_2(
82  const double f0, const double f1)
83  {
84  const unsigned nCoeffs = 2;
85  std::vector<double> buf(nCoeffs);
86  const unsigned n = interpolation_coefficients(
87  &buf[0], nCoeffs, f0, f1);
88  assert(n == nCoeffs);
89  return buf;
90  }
91 
92  inline std::vector<double> interpolation_coefficients_2(
93  const double f0, const double f1, const double f2)
94  {
95  const unsigned nCoeffs = 3;
96  std::vector<double> buf(nCoeffs);
97  const unsigned n = interpolation_coefficients(
98  &buf[0], nCoeffs, f0, f1, f2);
99  assert(n == nCoeffs);
100  return buf;
101  }
102 
103  inline std::vector<double> interpolation_coefficients_2(
104  const double f0, const double f1,
105  const double f2, const double f3)
106  {
107  const unsigned nCoeffs = 4;
108  std::vector<double> buf(nCoeffs);
109  const unsigned n = interpolation_coefficients(
110  &buf[0], nCoeffs, f0, f1, f2, f3);
111  assert(n == nCoeffs);
112  return buf;
113  }
114 #endif // SWIG
115 }
116 
117 #include "npstat/nm/interpolate.icc"
118 
119 #endif // NPSTAT_INTERPOLATE_HH_
Compile-time deduction of the underlying floating point type from the given complex type.
Definition: AbsArrayProjector.hh:14
T interpolate_quadratic(const double x, const T &f0, const T &f1, const T &f2)
Definition: interpolate.hh:37
T interpolate_linear(const double x, const T &f0, const T &f1)
Definition: interpolate.hh:26
T interpolate_cubic(const double x, const T &f0, const T &f1, const T &f2, const T &f3)
Definition: interpolate.hh:50
unsigned interpolation_coefficients(T *buffer, unsigned bufLen, const T &f0, const T &f1)