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