npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
LinInterpolatedTable1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_LININTERPOLATEDTABLE1D_HH_
2 #define NPSTAT_LININTERPOLATEDTABLE1D_HH_
3 
4 /*!
5 // \file LinInterpolatedTable1D.hh
6 //
7 // \brief One-dimensional linear interpolation/extrapolation table
8 //
9 // LinInterpolatedTableND template provides a more flexible tool
10 // in multiple dimensions (including one). It can use arbitrary
11 // internal data representation, non-uniform point spacing, etc.
12 //
13 // For tabular interpolation of statistical densities, see classes
14 // "Tabulated1D" and "BinnedDensity1D" in the "Distributions1D.hh" header.
15 //
16 // If linear interpolation beyond the grid limits is not required,
17 // the facilities built into the ArrayND class can do the job as well.
18 //
19 // Author: I. Volobouev
20 //
21 // April 2011
22 */
23 
24 #include <utility>
25 #include <vector>
26 
27 #include "geners/CPP11_auto_ptr.hh"
28 #include "geners/ClassId.hh"
29 
30 namespace npstat {
31  /**
32  // This class can be used to interpolate a table of values linearly
33  // in one dimension. Regular coordinate spacing only, double precision
34  // numbers are used to represent the data internally. Extrapolation
35  // beyond the leftmost or rightmost data points could be either
36  // linear or constant.
37  */
39  {
40  public:
41  /**
42  // Constructor from a regularly spaced data. Extrapolation
43  // from the edge to infinity can be either linear or constant,
44  // as defined by the parameters "leftExtrapolationLinear" and
45  // "rightExtrapolationLinear". "npoints" (size of the data array)
46  // must be larger than 1.
47  */
48  template <typename Real>
49  LinInterpolatedTable1D(const Real* data, unsigned npoints,
50  double x_min, double x_max,
51  bool leftExtrapolationLinear,
52  bool rightExtrapolationLinear);
53 
54  /**
55  // Constructor from a list of points, not necessarily regularly
56  // spaced (but must be sorted in the increasing order). The first
57  // member of the pair is the x coordinate, the second is the
58  // tabulated function value. The input list will be interpolated
59  // to "npoints" internal points linearly.
60  */
61  template <typename Real>
62  LinInterpolatedTable1D(const std::vector<std::pair<Real,Real> >& v,
63  unsigned npoints,
64  bool leftExtrapolationLinear,
65  bool rightExtrapolationLinear);
66 
67  /**
68  // Constructor which builds a function returning the given constant
69  // for every argument value
70  */
71  explicit LinInterpolatedTable1D(double c);
72 
73  /** Default constructor builds a functor which always returns 0.0 */
75 
77 
78  /** Interpolate to the given coordinate */
79  double operator()(const double& x) const;
80 
81  //@{
82  /** Comparison for equality (useful for I/O testing) */
83  bool operator==(const LinInterpolatedTable1D& r) const;
84  inline bool operator!=(const LinInterpolatedTable1D& r) const
85  {return !(*this == r);}
86  //@}
87 
88  //@{
89  /** A simple inspector */
90  inline double xmin() const {return xmin_;}
91  inline double xmax() const {return xmax_;}
92  inline unsigned npoints() const {return npoints_;}
93  inline bool leftExtrapolationLinear() const
94  {return leftExtrapolationLinear_;}
95  inline bool rightExtrapolationLinear() const
96  {return rightExtrapolationLinear_;}
97  inline const double* data() const {return &data_[0];}
98  //@}
99 
100  /**
101  // This method checks whether the table is monotonous
102  // (and, therefore, can be inverted). Possible flat regions
103  // at the edges are not taken into account.
104  */
105  bool isMonotonous() const;
106 
107  /**
108  // Generate the inverse lookup table. Note that it is only
109  // possible if the original table is monotonous (not taking
110  // into account possible flat regions at the edges). If the
111  // inversion is not possible, NULL pointer will be returned.
112  //
113  // The new table will have "npoints" points. The parameters
114  // "leftExtrapolationLinear" and "rightExtrapolationLinear"
115  // refer to the inverted table. Note that left and right will
116  // exchange places if the original table is decreasing.
117  */
118  CPP11_auto_ptr<LinInterpolatedTable1D> inverse(
119  unsigned npoints, bool leftExtrapolationLinear,
120  bool rightExtrapolationLinear) const;
121 
122  //@{
123  /** Method related to "geners" I/O */
124  inline gs::ClassId classId() const {return gs::ClassId(*this);}
125  bool write(std::ostream& of) const;
126  //@}
127 
128  static inline const char* classname()
129  {return "npstat::LinInterpolatedTable1D";}
130  static inline unsigned version() {return 1;}
131  static LinInterpolatedTable1D* read(const gs::ClassId& id, std::istream&);
132 
133  private:
134  static inline double interpolateSimple(
135  const double x0, const double x1,
136  const double y0, const double y1,
137  const double x)
138  {
139  return y0 + (y1 - y0)*((x - x0)/(x1 - x0));
140  }
141 
142  std::vector<double> data_;
143  double xmin_;
144  double xmax_;
145  double binwidth_;
146  unsigned npoints_;
147  bool leftExtrapolationLinear_;
148  bool rightExtrapolationLinear_;
149  mutable bool monotonous_;
150  mutable bool monotonicityKnown_;
151 
152 #ifdef SWIG
153  public:
154  inline LinInterpolatedTable1D* inverse2(
155  unsigned npoints, bool leftExtrapolationLinear,
156  bool rightExtrapolationLinear) const
157  {
158  CPP11_auto_ptr<LinInterpolatedTable1D> ptr = inverse(
159  npoints, leftExtrapolationLinear, rightExtrapolationLinear);
160  return ptr.release();
161  }
162 #endif // SWIG
163  };
164 }
165 
166 #include "npstat/nm/LinInterpolatedTable1D.icc"
167 
168 #endif // NPSTAT_LININTERPOLATEDTABLE1D_HH_
Definition: LinInterpolatedTable1D.hh:39
LinInterpolatedTable1D(const std::vector< std::pair< Real, Real > > &v, unsigned npoints, bool leftExtrapolationLinear, bool rightExtrapolationLinear)
double operator()(const double &x) const
CPP11_auto_ptr< LinInterpolatedTable1D > inverse(unsigned npoints, bool leftExtrapolationLinear, bool rightExtrapolationLinear) const
gs::ClassId classId() const
Definition: LinInterpolatedTable1D.hh:124
LinInterpolatedTable1D(const Real *data, unsigned npoints, double x_min, double x_max, bool leftExtrapolationLinear, bool rightExtrapolationLinear)
bool operator==(const LinInterpolatedTable1D &r) const
double xmin() const
Definition: LinInterpolatedTable1D.hh:90
Definition: AbsArrayProjector.hh:14