Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
10 #include "JLang/JException.hh"
11 #include "JLang/JAssert.hh"
12 #include "JMath/JMathSupportkit.hh"
13 #include "JMath/JLegendre.hh"
14 #include "JMath/JZero.hh"
15 #include "JTools/JFunctional.hh"
16 #include "JTools/JDistance.hh"
17 
18 
19 /**
20  * \author mdejong
21  */
22 
23 namespace JTOOLS {}
24 namespace JPP { using namespace JTOOLS; }
25 
26 namespace JTOOLS {
27 
28  using JLANG::JException;
32 
33 
34  /**
35  * Functional collection with Legendre polynomial interpolation.
36  */
37  template<unsigned int N, // number of points to interpolate
38  unsigned int M, // degree of polynomial
39  class JElement_t,
40  template<class, class> class JCollection_t,
41  class JResult_t,
42  class JDistance_t>
44  public JCollection_t<JElement_t, JDistance_t>,
45  public JFunction<typename JElement_t::abscissa_type,
46  typename JResultType<typename JElement_t::ordinate_type>::result_type>
47  {
48  public:
49 
50  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
51 
52  typedef typename collection_type::abscissa_type abscissa_type;
53  typedef typename collection_type::ordinate_type ordinate_type;
54  typedef typename collection_type::value_type value_type;
55  typedef typename collection_type::distance_type distance_type;
56 
57  typedef typename collection_type::const_iterator const_iterator;
58  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
59  typedef typename collection_type::iterator iterator;
60  typedef typename collection_type::reverse_iterator reverse_iterator;
61 
64 
68 
69 
70  /**
71  * Default constructor.
72  */
74  {
75  STATIC_CHECK((N > M));
76  }
77 
78 
79  /**
80  * Recursive interpolation method implementation.
81  *
82  * \param pX pointer to abscissa values
83  * \return function value
84  */
85  virtual result_type evaluate(const argument_type* pX) const override
86  {
87  using namespace std;
88  using namespace JPP;
89 
90  if (this->size() <= 1u) {
91  return this->getExceptionHandler().action(JFunctionalException("JPolfitFunction<>::evaluate() not enough data."));
92  }
93 
94  const argument_type x = *pX;
95 
96  const_iterator p = this->lower_bound(x);
97 
98  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
99  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
100  return this->getExceptionHandler().action(JValueOutOfRange("JPolfitFunction::evaluate() x out of range."));
101  }
102 
103  ++pX; // next argument value
104 
105 
106  int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
107 
108  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
109  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
110 
111  buffer.clear();
112 
113  for (const_iterator q = p; n != 0; ++q, --n) {
114  buffer.push_back(make_pair(this->getDistance(p->getX(), q->getX()), function_type::getValue(q->getY(), pX)));
115  }
116 
117  const JLegendre<result_type, M> g1(buffer.begin(), buffer.end());
118 
119  return g1(this->getDistance(p->getX(), x));
120  }
121 
122  private:
123  /**
124  * Function compilation.
125  */
126  virtual void do_compile() override
127  {}
128 
130  };
131 
132 
133  /**
134  * Template class for polynomial interpolation in 1D
135  *
136  * This class implements the JFunction1D interface.
137  */
138  template<unsigned int N,
139  unsigned int M,
140  class JElement_t,
141  template<class, class> class JCollection_t,
142  class JResult_t = typename JElement_t::ordinate_type,
145  public JPolfitFunction<N, M, JElement_t, JCollection_t, JResult_t, JDistance_t>,
146  public JFunction1D<typename JElement_t::abscissa_type, JResult_t>
147  {
148  public:
149 
150  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
151 
152  typedef typename collection_type::abscissa_type abscissa_type;
153  typedef typename collection_type::ordinate_type ordinate_type;
154  typedef typename collection_type::value_type value_type;
155  typedef typename collection_type::distance_type distance_type;
156 
157  typedef typename collection_type::const_iterator const_iterator;
158  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
159  typedef typename collection_type::iterator iterator;
160  typedef typename collection_type::reverse_iterator reverse_iterator;
161 
163 
167 
168 
169  /**
170  * Default contructor.
171  */
173  {}
174  };
175 }
176 
177 #endif
General exception.
Definition: JException.hh:23
collection_type::distance_type distance_type
Definition: JPolfit.hh:155
Exceptions.
collection_type::distance_type distance_type
Definition: JPolfit.hh:55
JPolfitFunction1D()
Default contructor.
Definition: JPolfit.hh:172
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
Auxiliary methods for mathematics.
Exception for a functional operation.
Definition: JException.hh:126
collection_type::iterator iterator
Definition: JPolfit.hh:59
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolfit.hh:50
collection_type::const_iterator const_iterator
Definition: JPolfit.hh:57
Template class for distance evaluation.
Definition: JDistance.hh:24
JPolfitFunction()
Default constructor.
Definition: JPolfit.hh:73
collection_type::ordinate_type ordinate_type
Definition: JPolfit.hh:53
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolfit.hh:166
collection_type::const_iterator const_iterator
Definition: JPolfit.hh:157
function_type::result_type result_type
Definition: JPolfit.hh:66
collection_type::value_type value_type
Definition: JPolfit.hh:54
function_type::argument_type argument_type
Definition: JPolfit.hh:164
Definition of zero value for any class.
collection_type::ordinate_type ordinate_type
Definition: JPolfit.hh:153
Template definition of function object interface in one dimension.
Definition: JFunctional.hh:317
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
collection_type::reverse_iterator reverse_iterator
Definition: JPolfit.hh:160
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:107
collection_type::reverse_iterator reverse_iterator
Definition: JPolfit.hh:60
collection_type::iterator iterator
Definition: JPolfit.hh:159
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:377
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolfit.hh:150
virtual void do_compile() override
Function compilation.
Definition: JPolfit.hh:126
collection_type::const_reverse_iterator const_reverse_iterator
Definition: JPolfit.hh:158
JResultType< ordinate_type >::result_type data_type
Definition: JPolfit.hh:62
Exception handler for functional object.
Definition: JFunctional.hh:131
Template definition of function object interface in multidimensions.
Definition: JFunctional.hh:303
Exception for numerical precision error.
Definition: JException.hh:252
functional_type::result_type result_type
Definition: JFunctional.hh:308
collection_type::abscissa_type abscissa_type
Definition: JPolfit.hh:152
std::vector< std::pair< abscissa_type, result_type > > buffer
Definition: JPolfit.hh:129
#define STATIC_CHECK(expr)
Definition: JAssert.hh:29
alias put_queue eval echo n
Definition: qlib.csh:19
function_type::argument_type argument_type
Definition: JPolfit.hh:65
Functional collection with Legendre polynomial interpolation.
Definition: JPolfit.hh:43
Template class for polynomial interpolation in 1D.
Definition: JPolfit.hh:144
function_type::result_type result_type
Definition: JPolfit.hh:165
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
functional_type::argument_type argument_type
Definition: JFunctional.hh:307
collection_type::value_type value_type
Definition: JPolfit.hh:154
double u[N+1]
Definition: JPolint.hh:739
collection_type::abscissa_type abscissa_type
Definition: JPolfit.hh:52
JFunction1D< abscissa_type, JResult_t > function_type
Definition: JPolfit.hh:162
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolfit.hh:67
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolfit.hh:85
collection_type::const_reverse_iterator const_reverse_iterator
Definition: JPolfit.hh:58
JFunction< abscissa_type, data_type > function_type
Definition: JPolfit.hh:63
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25