TabulatedFunction.h
Go to the documentation of this file.
1 #ifndef _utl_TabulatedFunction_h_
2 #define _utl_TabulatedFunction_h_
3 
4 #include <utl/TabulatedFunctionIterators.h>
5 #include <utl/IteratorRange.h>
6 #include <vector>
7 #include <algorithm>
8 #include <numeric>
9 #include <string>
10 #include <functional>
11 
12 
13 namespace utl {
14 
28 
29  public:
30  typedef std::vector<double> Array;
31  typedef Array::iterator ArrayIterator;
32  typedef Array::const_iterator ArrayConstIterator;
33  typedef Array::reverse_iterator ArrayReverseIterator;
34  typedef Array::const_reverse_iterator ArrayConstReverseIterator;
35  typedef Array::const_reference ArrayConstReference;
38 
39  TabulatedFunction(const unsigned int interpolationOrder = 1,
40  const double boundaryTolerance = 1e-3) :
41  fInterpolationOrder(interpolationOrder),
42  fBoundaryTolerance(boundaryTolerance)
43  { }
44 
45  TabulatedFunction(const std::vector<double>& xValues,
46  const std::vector<double>& yValues,
47  const unsigned int interpolationOrder = 1,
48  const double boundaryTolerance = 1e-3) :
49  fInterpolationOrder(interpolationOrder),
50  fBoundaryTolerance(boundaryTolerance)
51  {
52  FillTable(xValues, yValues);
53  }
54 
55  unsigned int GetNPoints() const { return fX.size(); }
56 
58  Pair operator[](const int idx) const { return Pair(fX[idx], fY[idx]); }
59 
60  void Clear() { fX.clear(); fY.clear(); }
61 
62  void PushBack(const double x, const double y);
63 
65  { return Iterator(fX.begin(), fY.begin()); }
66 
68  { return Iterator(fX.end(), fY.end()); }
69 
71  { return ConstIterator(fX.begin(), fY.begin()); }
72 
74  { return ConstIterator(fX.end(), fY.end()); }
75 
77 
78 
79  ArrayIterator XBegin() { return fX.begin(); }
80 
82  ArrayConstIterator XBegin() const { return fX.begin(); }
83 
85  ArrayReverseIterator XRBegin() { return fX.rbegin(); }
86 
88  ArrayConstReverseIterator XRBegin() const { return fX.rbegin(); }
89 
91  ArrayIterator YBegin() { return fY.begin(); }
92 
94  ArrayConstIterator YBegin() const { return fY.begin(); }
95 
97  ArrayReverseIterator YRBegin() { return fY.rbegin(); }
98 
100  ArrayConstReverseIterator YRBegin() const { return fY.rbegin(); }
101 
103  ArrayIterator XEnd() { return fX.end(); }
104 
106  ArrayConstIterator XEnd() const { return fX.end(); }
107 
109  ArrayReverseIterator XREnd() { return fX.rend(); }
110 
111  // end reverse iterator for X
112  ArrayConstReverseIterator XREnd() const { return fX.rend(); }
113 
115  ArrayIterator YEnd() { return fY.end(); }
116 
118  ArrayConstIterator YEnd() const { return fY.end(); }
119 
121  ArrayReverseIterator YREnd() { return fY.rend(); }
122 
124  ArrayConstReverseIterator YREnd() const { return fY.rend(); }
125 
129  OFFLINE_MAKE_BOTH_ITERATOR_RANGES(ArrayReverseIterator, ArrayConstReverseIterator, YR)
130 
132  ArrayConstReference XFront() const { return fX.front(); }
133 
135  ArrayConstReference YFront() const { return fY.front(); }
136 
138  ArrayConstReference XBack() const { return fX.back(); }
139 
141  ArrayConstReference YBack() const { return fY.back(); }
142 
143  Iterator Insert(const double x, const double y);
144 
145  //ConstIterator Find(const double x, const double y) const;
146 
147  ConstIterator FindX(const double x) const;
148 
149  ConstIterator FindY(const double y) const;
150 
152  double Y(const double x) const
153  { return InterpolateY(x, fInterpolationOrder); }
154 
156  double InterpolateY(const double x, const unsigned int polyDegree) const;
157 
158  // Interpolate the Y value with the polynomial degree given in the constructor
159  double operator()(const double x) const { return Y(x); }
160 
161  // get/set the \p idx-th value of the Ys
162  const double& GetY(const unsigned int idx) const { return fY[idx]; }
163 
164  // get/set the \p idx-th value of the Xs
165  const double& GetX(const unsigned int idx) const { return fX[idx]; }
166 
167  double& GetX(const unsigned int idx) { return fX[idx]; }
168  double& GetY(const unsigned int idx) { return fY[idx]; }
169 
171  double SumX() const { return std::accumulate(fX.begin(), fX.end(), 0.); }
172 
174  double SumY() const { return std::accumulate(fY.begin(), fY.end(), 0.); }
175 
176  bool IsInValidRange(const double x) const
177  { return fX.front() <= x && x <= fX.back(); }
178 
179  bool operator==(const TabulatedFunction& t) const
180  { return fX == t.fX && fY == t.fY; }
181 
182  bool operator!=(const TabulatedFunction& t) const
183  { return !operator==(t); }
184 
185  unsigned int GetInterpolationOrder() const { return fInterpolationOrder; }
186 
187  void SetInterpolationOrder(const unsigned int interpolationOrder)
188  { fInterpolationOrder = interpolationOrder; }
189 
190  double GetBoundaryTolerance() const { return fBoundaryTolerance; }
191 
192  void SetBoundaryTolerance(const double tolerance) { fBoundaryTolerance = tolerance; }
193 
194  void
195  ScaleX(const double s)
196  {
197  std::transform(
198  XBegin(), XEnd(), XBegin(),
199  [&s](auto const& elem) { return elem * s; }
200  );
201  }
202 
203  void
204  ScaleY(const double s)
205  {
206  std::transform(
207  YBegin(), YEnd(), YBegin(),
208  [&s](auto const& elem) { return elem * s; }
209  );
210  }
211 
212  void
214  {
215  using std::swap;
216  swap(fX, tf.fX);
217  swap(fY, tf.fY);
220  }
221 
222  protected:
223  void FillTable(const std::vector<double>& xValues,
224  const std::vector<double>& yValues);
225 
228 
229  private:
230  double LinearInterpolateY(const double x) const;
231 
232  unsigned int fInterpolationOrder;
234  };
235 
236 
238  { t1.Swap(t2); }
239 
240 }
241 
242 #endif
Pair operator[](const int idx) const
Note: cannot be used in assignment.
Array::reverse_iterator ArrayReverseIterator
unsigned int GetNPoints() const
ConstIterator FindY(const double y) const
void swap(utl::Trace< T > &t1, utl::Trace< T > &t2)
Definition: Trace.h:363
ArrayConstIterator YBegin() const
begin of array of Y
TabulatedFunction(const unsigned int interpolationOrder=1, const double boundaryTolerance=1e-3)
ArrayConstReference XBack() const
read-only reference to back of array of X
#define OFFLINE_MAKE_BOTH_ITERATOR_RANGES(_Iterator_, _ConstIterator_, _NamePrefix_)
Definition: IteratorRange.h:43
ConstTabulatedFunctionIterator ConstIterator
#define OFFLINE_MAKE_BOTH_FRIEND_RANGES(_Iterator_, _ConstIterator_, _Class_)
Array::const_iterator ArrayConstIterator
ArrayReverseIterator YREnd()
end reverse iterator for Y
ArrayReverseIterator XREnd()
end reverse iterator for X
void swap(utl::TabulatedFunction &t1, utl::TabulatedFunction &t2)
ArrayIterator XEnd()
end of array of X
ConstIterator End() const
Class to hold collection (x,y) points and provide interpolation between them.
ConstIterator FindX(const double x) const
TabulatedFunction(const std::vector< double > &xValues, const std::vector< double > &yValues, const unsigned int interpolationOrder=1, const double boundaryTolerance=1e-3)
bool IsInValidRange(const double x) const
ArrayConstIterator XBegin() const
begin of array of X
ArrayConstReverseIterator XREnd() const
double operator()(const double x) const
void PushBack(const double x, const double y)
unsigned int fInterpolationOrder
double & GetY(const unsigned int idx)
double SumY() const
return the sum of Y values
ArrayConstReverseIterator YRBegin() const
begin reverse iterator for Y
std::vector< double > Array
constexpr double s
Definition: AugerUnits.h:163
ArrayConstReference XFront() const
read-only reference to front of array of X
ConstIterator Begin() const
Array::const_reference ArrayConstReference
bool operator==(const TabulatedFunction &t) const
Iterator Insert(const double x, const double y)
ArrayConstReference YFront() const
read-only reference to front of array of Y
void FillTable(const std::vector< double > &xValues, const std::vector< double > &yValues)
ArrayReverseIterator XRBegin()
begin reverse iterator for X
a second level trigger
Definition: XbT2.h:8
TabulatedFunctionIterator Iterator
ArrayConstReverseIterator XRBegin() const
begin reverse iterator for X
const double & GetY(const unsigned int idx) const
ArrayIterator YBegin()
begin of array of Y
void SetInterpolationOrder(const unsigned int interpolationOrder)
double & GetX(const unsigned int idx)
void SetBoundaryTolerance(const double tolerance)
ArrayConstReference YBack() const
read-only reference to back of array of Y
Array::iterator ArrayIterator
Array::const_reverse_iterator ArrayConstReverseIterator
double InterpolateY(const double x, const unsigned int polyDegree) const
Interpolate the Y value with a polyDegree polynomial.
ArrayIterator XBegin()
begin of array of X
ArrayConstIterator XEnd() const
end of array of X
unsigned int GetInterpolationOrder() const
double LinearInterpolateY(const double x) const
bool operator!=(const TabulatedFunction &t) const
void Swap(TabulatedFunction &tf)
const double & GetX(const unsigned int idx) const
ArrayConstReverseIterator YREnd() const
end reverse iterator for Y
ArrayIterator YEnd()
end of array of Y
ArrayConstIterator YEnd() const
end of array of Y
a pair of graph points (x,y)
Definition: Pair.h:25
double Y(const double x) const
Get or interpolate the Y value that corresponds to parameter x.
double GetBoundaryTolerance() const
void ScaleY(const double s)
void ScaleX(const double s)
ArrayReverseIterator YRBegin()
begin reverse iterator for Y
double SumX() const
return the sum of X values

, generated on Tue Sep 26 2023.