Jpp  19.1.0
the software that should make you happy
JPolfit.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JPOLFIT__
2 #define __JTOOLS__JPOLFIT__
3 
4 #include <cmath>
5 #include <iterator>
6 #include <algorithm>
7 #include <vector>
8 #include <utility>
9 #include <sstream>
10 
11 #include "JLang/JException.hh"
12 #include "JLang/JAssert.hh"
14 #include "JMath/JMathSupportkit.hh"
15 #include "JMath/JLegendre.hh"
16 #include "JMath/JZero.hh"
17 #include "JTools/JFunctional.hh"
18 #include "JTools/JDistance.hh"
19 
20 
21 /**
22  * \author mdejong
23  */
24 
25 namespace JTOOLS {}
26 namespace JPP { using namespace JTOOLS; }
27 
28 namespace JTOOLS {
29 
30  using JLANG::JException;
34 
35 
36  /**
37  * Functional collection with Legendre polynomial interpolation.
38  */
39  template<unsigned int N, // number of points to interpolate
40  unsigned int M, // degree of polynomial
41  class JElement_t,
42  template<class, class> class JCollection_t,
43  class JResult_t,
44  class JDistance_t>
46  public JCollection_t<JElement_t, JDistance_t>,
47  public JFunction<typename JElement_t::abscissa_type,
48  typename JResultType<typename JElement_t::ordinate_type>::result_type>
49  {
50  public:
51 
52  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
53 
54  typedef typename collection_type::abscissa_type abscissa_type;
55  typedef typename collection_type::ordinate_type ordinate_type;
56  typedef typename collection_type::value_type value_type;
57  typedef typename collection_type::distance_type distance_type;
58 
59  typedef typename collection_type::const_iterator const_iterator;
60  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
61  typedef typename collection_type::iterator iterator;
62  typedef typename collection_type::reverse_iterator reverse_iterator;
63 
66 
70 
71 
72  /**
73  * Default constructor.
74  */
76  {
77  STATIC_CHECK((N > M));
78  }
79 
80 
81  /**
82  * Recursive interpolation method implementation.
83  *
84  * \param pX pointer to abscissa values
85  * \return function value
86  */
87  virtual result_type evaluate(const argument_type* pX) const override
88  {
89  using namespace std;
90  using namespace JPP;
91 
92  const argument_type x = *pX;
93 
94  if (this->size() <= 1u) {
95 
96  try {
97  return this->getExceptionHandler().action();
98  }
99  catch (const JException& error) {
100 
101  std::ostringstream os;
102 
103  os << __FILE__ << ':' << __LINE__ << " not enough data " << STREAM("?") << x;
104 
105  throw JFunctionalException(os.str());
106  }
107  }
108 
109  const_iterator p = this->lower_bound(x);
110 
111  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
112  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
113 
114  try {
115  return this->getExceptionHandler().action();
116  }
117  catch (const JException& error) {
118 
119  std::ostringstream os;
120 
121  os << __FILE__ << ':' << __LINE__ << " abscissa out of range "
122  << STREAM("?") << x << " <> "
123  << STREAM("?") << this->begin() ->getX() << ' '
124  << STREAM("?") << this->rbegin()->getX();
125 
126  throw JValueOutOfRange(os.str());
127  }
128  }
129 
130  ++pX; // next argument value
131 
132 
133  int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
134 
135  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
136  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
137 
138  buffer.clear();
139 
140  for (const_iterator q = p; n != 0; ++q, --n) {
141  buffer.push_back(make_pair(this->getDistance(p->getX(), q->getX()), function_type::getValue(q->getY(), pX)));
142  }
143 
144  const JLegendre<result_type, M> g1(buffer.begin(), buffer.end());
145 
146  return g1(this->getDistance(p->getX(), x));
147  }
148 
149  private:
150  /**
151  * Function compilation.
152  */
153  virtual void do_compile() override
154  {}
155 
157  };
158 
159 
160  /**
161  * Template class for polynomial interpolation in 1D
162  *
163  * This class implements the JFunction1D interface.
164  */
165  template<unsigned int N,
166  unsigned int M,
167  class JElement_t,
168  template<class, class> class JCollection_t,
169  class JResult_t = typename JElement_t::ordinate_type,
172  public JPolfitFunction<N, M, JElement_t, JCollection_t, JResult_t, JDistance_t>,
173  public JFunction1D<typename JElement_t::abscissa_type, JResult_t>
174  {
175  public:
176 
177  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
178 
179  typedef typename collection_type::abscissa_type abscissa_type;
180  typedef typename collection_type::ordinate_type ordinate_type;
181  typedef typename collection_type::value_type value_type;
182  typedef typename collection_type::distance_type distance_type;
183 
184  typedef typename collection_type::const_iterator const_iterator;
185  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
186  typedef typename collection_type::iterator iterator;
187  typedef typename collection_type::reverse_iterator reverse_iterator;
188 
190 
194 
195 
196  /**
197  * Default contructor.
198  */
200  {}
201  };
202 }
203 
204 #endif
#define STATIC_CHECK(expr)
Definition: JAssert.hh:31
Exceptions.
Auxiliary methods for mathematics.
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
Definition of zero value for any class.
General exception.
Definition: JException.hh:24
Exception for a functional operation.
Definition: JException.hh:144
Exception for numerical precision error.
Definition: JException.hh:270
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
JArgument_t argument_type
Definition: JFunctional.hh:84
const JExceptionHandler & getExceptionHandler() const
Get exception handler.
Definition: JFunctional.hh:292
JResult_t result_type
Definition: JFunctional.hh:87
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:107
Template class for polynomial interpolation in 1D.
Definition: JPolfit.hh:174
collection_type::const_reverse_iterator const_reverse_iterator
Definition: JPolfit.hh:185
collection_type::reverse_iterator reverse_iterator
Definition: JPolfit.hh:187
function_type::result_type result_type
Definition: JPolfit.hh:192
collection_type::ordinate_type ordinate_type
Definition: JPolfit.hh:180
collection_type::value_type value_type
Definition: JPolfit.hh:181
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolfit.hh:193
JPolfitFunction1D()
Default contructor.
Definition: JPolfit.hh:199
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolfit.hh:177
JFunction1D< abscissa_type, JResult_t > function_type
Definition: JPolfit.hh:189
collection_type::abscissa_type abscissa_type
Definition: JPolfit.hh:179
collection_type::distance_type distance_type
Definition: JPolfit.hh:182
function_type::argument_type argument_type
Definition: JPolfit.hh:191
collection_type::iterator iterator
Definition: JPolfit.hh:186
collection_type::const_iterator const_iterator
Definition: JPolfit.hh:184
Functional collection with Legendre polynomial interpolation.
Definition: JPolfit.hh:49
collection_type::value_type value_type
Definition: JPolfit.hh:56
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolfit.hh:87
collection_type::const_iterator const_iterator
Definition: JPolfit.hh:59
function_type::result_type result_type
Definition: JPolfit.hh:68
collection_type::const_reverse_iterator const_reverse_iterator
Definition: JPolfit.hh:60
function_type::argument_type argument_type
Definition: JPolfit.hh:67
virtual void do_compile() override
Function compilation.
Definition: JPolfit.hh:153
collection_type::abscissa_type abscissa_type
Definition: JPolfit.hh:54
JResultType< ordinate_type >::result_type data_type
Definition: JPolfit.hh:64
std::vector< std::pair< abscissa_type, result_type > > buffer
Definition: JPolfit.hh:156
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolfit.hh:69
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolfit.hh:52
collection_type::ordinate_type ordinate_type
Definition: JPolfit.hh:55
collection_type::reverse_iterator reverse_iterator
Definition: JPolfit.hh:62
JPolfitFunction()
Default constructor.
Definition: JPolfit.hh:75
collection_type::iterator iterator
Definition: JPolfit.hh:61
JFunction< abscissa_type, data_type > function_type
Definition: JPolfit.hh:65
collection_type::distance_type distance_type
Definition: JPolfit.hh:57
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
const int n
Definition: JPolint.hh:786
Definition: JSTDTypes.hh:14
Template definition for function evaluation of Legendre polynome.
Definition: JLegendre.hh:269
Template definition of function object interface in one dimension.
Definition: JFunctional.hh:334
functional_type::result_type result_type
Definition: JFunctional.hh:339
functional_type::argument_type argument_type
Definition: JFunctional.hh:338
Template definition of function object interface in multidimensions.
Definition: JFunctional.hh:320
functional_type::argument_type argument_type
Definition: JFunctional.hh:322
functional_type::result_type result_type
Definition: JFunctional.hh:323
Exception handler for functional object.
Definition: JFunctional.hh:133
result_type action() const
Implementation of exception handler.
Definition: JFunctional.hh:155
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:392
Auxiliary data structure for handling std::ostream.