Jpp  18.2.0
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 #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  std::ostringstream os;
97 
98  os << __FILE__ << ':' << __LINE__ << " not enough data " << STREAM("?") << x;
99 
100  return this->getExceptionHandler().action(JFunctionalException(os.str()));
101  }
102 
103  const_iterator p = this->lower_bound(x);
104 
105  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
106  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
107 
108  std::ostringstream os;
109 
110  os << __FILE__ << ':' << __LINE__ << " abscissa out of range "
111  << STREAM("?") << x << " <> "
112  << STREAM("?") << this->begin() ->getX() << ' '
113  << STREAM("?") << this->rbegin()->getX();
114 
115  return this->getExceptionHandler().action(JValueOutOfRange(os.str()));
116  }
117 
118  ++pX; // next argument value
119 
120 
121  int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
122 
123  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
124  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
125 
126  buffer.clear();
127 
128  for (const_iterator q = p; n != 0; ++q, --n) {
129  buffer.push_back(make_pair(this->getDistance(p->getX(), q->getX()), function_type::getValue(q->getY(), pX)));
130  }
131 
132  const JLegendre<result_type, M> g1(buffer.begin(), buffer.end());
133 
134  return g1(this->getDistance(p->getX(), x));
135  }
136 
137  private:
138  /**
139  * Function compilation.
140  */
141  virtual void do_compile() override
142  {}
143 
145  };
146 
147 
148  /**
149  * Template class for polynomial interpolation in 1D
150  *
151  * This class implements the JFunction1D interface.
152  */
153  template<unsigned int N,
154  unsigned int M,
155  class JElement_t,
156  template<class, class> class JCollection_t,
157  class JResult_t = typename JElement_t::ordinate_type,
160  public JPolfitFunction<N, M, JElement_t, JCollection_t, JResult_t, JDistance_t>,
161  public JFunction1D<typename JElement_t::abscissa_type, JResult_t>
162  {
163  public:
164 
165  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
166 
167  typedef typename collection_type::abscissa_type abscissa_type;
168  typedef typename collection_type::ordinate_type ordinate_type;
169  typedef typename collection_type::value_type value_type;
170  typedef typename collection_type::distance_type distance_type;
171 
172  typedef typename collection_type::const_iterator const_iterator;
173  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
174  typedef typename collection_type::iterator iterator;
175  typedef typename collection_type::reverse_iterator reverse_iterator;
176 
178 
182 
183 
184  /**
185  * Default contructor.
186  */
188  {}
189  };
190 }
191 
192 #endif
General exception.
Definition: JException.hh:24
collection_type::distance_type distance_type
Definition: JPolfit.hh:170
Exceptions.
collection_type::distance_type distance_type
Definition: JPolfit.hh:57
JPolfitFunction1D()
Default contructor.
Definition: JPolfit.hh:187
Auxiliary methods for mathematics.
Exception for a functional operation.
Definition: JException.hh:142
collection_type::iterator iterator
Definition: JPolfit.hh:61
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolfit.hh:52
collection_type::const_iterator const_iterator
Definition: JPolfit.hh:59
Template class for distance evaluation.
Definition: JDistance.hh:24
JPolfitFunction()
Default constructor.
Definition: JPolfit.hh:75
collection_type::ordinate_type ordinate_type
Definition: JPolfit.hh:55
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolfit.hh:181
collection_type::const_iterator const_iterator
Definition: JPolfit.hh:172
function_type::result_type result_type
Definition: JPolfit.hh:68
collection_type::value_type value_type
Definition: JPolfit.hh:56
function_type::argument_type argument_type
Definition: JPolfit.hh:179
Definition of zero value for any class.
collection_type::ordinate_type ordinate_type
Definition: JPolfit.hh:168
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:175
const int n
Definition: JPolint.hh:742
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:62
Auxiliary data structure for handling std::ostream.
collection_type::iterator iterator
Definition: JPolfit.hh:174
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:377
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolfit.hh:165
virtual void do_compile() override
Function compilation.
Definition: JPolfit.hh:141
collection_type::const_reverse_iterator const_reverse_iterator
Definition: JPolfit.hh:173
JResultType< ordinate_type >::result_type data_type
Definition: JPolfit.hh:64
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:268
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:167
std::vector< std::pair< abscissa_type, result_type > > buffer
Definition: JPolfit.hh:144
#define STATIC_CHECK(expr)
Definition: JAssert.hh:31
function_type::argument_type argument_type
Definition: JPolfit.hh:67
Functional collection with Legendre polynomial interpolation.
Definition: JPolfit.hh:45
Template class for polynomial interpolation in 1D.
Definition: JPolfit.hh:159
function_type::result_type result_type
Definition: JPolfit.hh:180
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:178
functional_type::argument_type argument_type
Definition: JFunctional.hh:307
collection_type::value_type value_type
Definition: JPolfit.hh:169
double u[N+1]
Definition: JPolint.hh:821
collection_type::abscissa_type abscissa_type
Definition: JPolfit.hh:54
JFunction1D< abscissa_type, JResult_t > function_type
Definition: JPolfit.hh:177
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolfit.hh:69
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolfit.hh:87
collection_type::const_reverse_iterator const_reverse_iterator
Definition: JPolfit.hh:60
JFunction< abscissa_type, data_type > function_type
Definition: JPolfit.hh:65
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25