Jpp  18.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPolint.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JPOLINT__
2 #define __JTOOLS__JPOLINT__
3 
4 #include <cmath>
5 #include <iterator>
6 #include <algorithm>
7 
8 #include "JLang/JException.hh"
9 #include "JLang/JAssert.hh"
10 #include "JLang/JBool.hh"
12 #include "JTools/JFunctional.hh"
13 #include "JTools/JDistance.hh"
14 #include "JTools/JResult.hh"
15 #include "JTools/JElement.hh"
16 #include "JTools/JMapCollection.hh"
17 #include "JTools/JQuadrature.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  * Template definition for functional collection with polynomial interpolation.
37  */
38  template<unsigned int N,
39  class JElement_t,
40  template<class, class> class JCollection_t,
41  class JResult_t,
42  class JDistance_t>
44 
45 
46  /**
47  * Template specialisation for functional collection with polynomial interpolation.
48  *
49  * Polynomial interpolation code is taken from reference:
50  * Numerical Recipes in C++, W.H. Press, S.A. Teukolsky, W.T. Vetterling and B.P. Flannery,
51  * Cambridge University Press.
52  */
53  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
55  JElement_t,
56  JCollection_t,
57  typename JResultType<typename JElement_t::ordinate_type>::result_type,
58  JDistance_t> :
59  public JCollection_t<JElement_t, JDistance_t>,
60  public JFunction<typename JElement_t::abscissa_type,
61  typename JResultType<typename JElement_t::ordinate_type>::result_type>
62  {
63  public:
64 
65  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
66 
67  typedef typename collection_type::abscissa_type abscissa_type;
68  typedef typename collection_type::ordinate_type ordinate_type;
69  typedef typename collection_type::value_type value_type;
70  typedef typename collection_type::distance_type distance_type;
71 
72  typedef typename collection_type::const_iterator const_iterator;
73  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
74  typedef typename collection_type::iterator iterator;
75  typedef typename collection_type::reverse_iterator reverse_iterator;
76 
79 
83 
84 
85  /**
86  * Default constructor.
87  */
89  {}
90 
91 
92  /**
93  * Recursive interpolation method implementation.
94  *
95  * \param pX pointer to abscissa values
96  * \return function value
97  */
98  virtual result_type evaluate(const argument_type* pX) const override
99  {
100  const argument_type x = *pX;
101 
102  if (this->size() > 1u) {
103 
104  const_iterator p = this->lower_bound(x);
105 
106  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
107  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
108 
109  return this->getExceptionHandler().action(MAKE_EXCEPTION(JValueOutOfRange, "abscissa out of range "
110  << STREAM("?") << x << " <> "
111  << STREAM("?") << this->begin() ->getX() << ' '
112  << STREAM("?") << this->rbegin()->getX()));
113  }
114 
115  ++pX; // next argument value
116 
117 
118  const int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
119 
120  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
121  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
122 
123 
124  int j = 0;
125 
126  for (int i = 0; i != n; ++p, ++i) {
127 
128  u[i] = this->getDistance(x, p->getX());
129  v[i] = function_type::getValue(p->getY(), pX);
130  w[i] = v[i];
131 
132  if (fabs(u[i]) < fabs(u[j])) {
133  j = i;
134  }
135  }
136 
137 
138  result_type y = v[j];
139 
140  --j;
141 
142  for (int m = 1; m != n; ++m) {
143 
144  for (int i = 0; i != n-m; ++i) {
145 
146  const double ho = u[ i ];
147  const double hp = u[i+m];
148  const double dx = ho - hp;
149 
150  v[i] = v[i+1];
151  v[i] -= w[ i ];
152  w[i] = v[ i ];
153 
154  v[i] *= ho/dx;
155  w[i] *= hp/dx;
156  }
157 
158  if (2*(j+1) < n - m)
159  y += v[j+1];
160  else
161  y += w[j--];
162  }
163 
164  return y;
165 
166  } else if (this->size() == 1u && this->getDistance(x, this->begin()->getX()) <= distance_type::precision) {
167 
168  return function_type::getValue(this->begin()->getY(), ++pX);
169 
170  } else {
171 
172  return this->getExceptionHandler().action(MAKE_EXCEPTION(JFunctionalException, "not enough data " << STREAM("?") << x));
173  }
174  }
175 
176  protected:
177  /**
178  * Function compilation.
179  */
180  virtual void do_compile() override
181  {}
182 
183 
184  private:
185  mutable double u[N+1];
186  mutable result_type v[N+1];
187  mutable result_type w[N+1];
188  };
189 
190 
191  /**
192  * Template specialisation for zero-degree polynomial interpolation method.\n
193  * The interpolation is based on a simple lookup table.
194  */
195  template<class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
196  class JPolintFunction<0,
197  JElement_t,
198  JCollection_t,
199  typename JResultType<typename JElement_t::ordinate_type>::result_type,
200  JDistance_t> :
201  public JCollection_t<JElement_t, JDistance_t>,
202  public JFunction<typename JElement_t::abscissa_type,
203  typename JResultType<typename JElement_t::ordinate_type>::result_type>
204  {
205  public:
206 
207  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
208 
209  typedef typename collection_type::abscissa_type abscissa_type;
210  typedef typename collection_type::ordinate_type ordinate_type;
211  typedef typename collection_type::value_type value_type;
212  typedef typename collection_type::distance_type distance_type;
213 
214  typedef typename collection_type::const_iterator const_iterator;
215  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
216  typedef typename collection_type::iterator iterator;
217  typedef typename collection_type::reverse_iterator reverse_iterator;
218 
221 
225 
226 
227  /**
228  * Default constructor.
229  */
231  {}
232 
233 
234  /**
235  * Recursive interpolation method implementation.
236  *
237  * \param pX pointer to abscissa values
238  * \return function value
239  */
240  virtual result_type evaluate(const argument_type* pX) const override
241  {
242  const argument_type x = *pX;
243 
244  if (this->size() > 1u) {
245 
246  const_iterator p = this->lower_bound(x);
247 
248  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
249  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
250 
251  return this->getExceptionHandler().action(MAKE_EXCEPTION(JValueOutOfRange, "abscissa out of range "
252  << STREAM("?") << x << " <> "
253  << STREAM("?") << this->begin() ->getX() << ' '
254  << STREAM("?") << this->rbegin()->getX()));
255  }
256 
257  ++pX; // next argument value
258 
259 
260  const_iterator q = p--;
261 
262  if (q == this->begin() || this->getDistance(x, q->getX()) < this->getDistance(p->getX(), x))
263  return function_type::getValue(q->getY(), pX);
264  else
265  return function_type::getValue(p->getY(), pX);
266 
267  } else if (this->size() == 1u && this->getDistance(x, this->begin()->getX()) <= distance_type::precision) {
268 
269  return function_type::getValue(this->begin()->getY(), ++pX);
270 
271  } else {
272 
273  return this->getExceptionHandler().action(MAKE_EXCEPTION(JFunctionalException, "not enough data " << STREAM("?") << x));
274  }
275  }
276 
277  protected:
278  /**
279  * Function compilation.
280  */
281  virtual void do_compile() override
282  {}
283  };
284 
285 
286  /**
287  * Template specialisation for first-degree polynomial interpolation method.\n
288  * The interpolation is based on a simple linear interpolation.
289  */
290  template<class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
291  class JPolintFunction<1,
292  JElement_t,
293  JCollection_t,
294  typename JResultType<typename JElement_t::ordinate_type>::result_type,
295  JDistance_t> :
296  public JCollection_t<JElement_t, JDistance_t>,
297  public JFunction<typename JElement_t::abscissa_type,
298  typename JResultType<typename JElement_t::ordinate_type>::result_type>
299  {
300  public:
301 
302  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
303 
304  typedef typename collection_type::abscissa_type abscissa_type;
305  typedef typename collection_type::ordinate_type ordinate_type;
306  typedef typename collection_type::value_type value_type;
307  typedef typename collection_type::distance_type distance_type;
308 
309  typedef typename collection_type::const_iterator const_iterator;
310  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
311  typedef typename collection_type::iterator iterator;
312  typedef typename collection_type::reverse_iterator reverse_iterator;
313 
316 
320 
321 
322  /**
323  * Default constructor.
324  */
326  {}
327 
328 
329  /**
330  * Recursive interpolation method implementation.
331  *
332  * \param pX pointer to abscissa values
333  * \return function value
334  */
335  virtual result_type evaluate(const argument_type* pX) const override
336  {
337  const argument_type x = *pX;
338 
339  if (this->size() > 1u) {
340 
341  const_iterator p = this->lower_bound(x);
342 
343  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
344  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
345 
346  return this->getExceptionHandler().action(MAKE_EXCEPTION(JValueOutOfRange, "abscissa out of range "
347  << STREAM("?") << x << " <> "
348  << STREAM("?") << this->begin() ->getX() << ' '
349  << STREAM("?") << this->rbegin()->getX()));
350  }
351 
352  ++pX; // next argument value
353 
354 
355  const_iterator q = p--;
356 
357  const double dx = this->getDistance(p->getX(), q->getX());
358  const double a = this->getDistance(x, q->getX()) / dx;
359  const double b = 1.0 - a;
360 
361  ya = function_type::getValue(p->getY(), pX);
362  yb = function_type::getValue(q->getY(), pX);
363 
364  ya *= a;
365  yb *= b;
366 
367  ya += yb;
368 
369  return ya;
370 
371  } else if (this->size() == 1u && this->getDistance(x, this->begin()->getX()) <= distance_type::precision) {
372 
373  return function_type::getValue(this->begin()->getY(), ++pX);
374 
375  } else {
376 
377  return this->getExceptionHandler().action(MAKE_EXCEPTION(JFunctionalException, "not enough data " << STREAM("?") << x));
378  }
379  }
380 
381  protected:
382  /**
383  * Function compilation.
384  */
385  virtual void do_compile() override
386  {}
387 
388 
389  private:
390  mutable result_type ya;
391  mutable result_type yb;
392  };
393 
394 
395  /**
396  * Template specialisation for polynomial interpolation method with returning JResultPDF data structure.
397  *
398  * Note that the data structure of the elements in the collection should have the additional methods:
399  * <pre>
400  * ordinate_type getIntegral() const;
401  * void setIntegral(ordinate_type v);
402  * </pre>
403  * to get and set the integral values, respectively.
404  */
405  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
407  JElement_t,
408  JCollection_t,
409  JResultPDF<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
410  JDistance_t> :
411  public JCollection_t<JElement_t, JDistance_t>,
412  public JFunction<typename JElement_t::abscissa_type,
413  JResultPDF<typename JResultType<typename JElement_t::ordinate_type>::result_type> >
414  {
415  public:
416 
417  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
418 
419  typedef typename collection_type::abscissa_type abscissa_type;
420  typedef typename collection_type::ordinate_type ordinate_type;
421  typedef typename collection_type::value_type value_type;
422  typedef typename collection_type::distance_type distance_type;
423 
424  typedef typename collection_type::const_iterator const_iterator;
425  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
426  typedef typename collection_type::iterator iterator;
427  typedef typename collection_type::reverse_iterator reverse_iterator;
428 
431 
435 
437 
438 
439  /**
440  * Default contructor.
441  */
443  {}
444 
445 
446  /**
447  * Recursive interpolation method implementation.
448  *
449  * \param pX pointer to abscissa values
450  * \return function value
451  */
452  virtual result_type evaluate(const argument_type* pX) const override
453  {
454  const argument_type x = *pX;
455 
456  if (this->size() <= 1u) {
457  return this->getExceptionHandler().action(MAKE_EXCEPTION(JFunctionalException, "not enough data " << STREAM("?") << x));
458  }
459 
460  const_iterator p = this->lower_bound(x);
461 
462  if (p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) {
463 
464  try {
465 
466  result = this->getExceptionHandler().action(MAKE_EXCEPTION(JValueOutOfRange, "abscissa out of range "
467  << STREAM("?") << x << " < " << STREAM("?") << this->begin() ->getX()));
468 
469  // overwrite integral values
470 
471  result.v = 0;
472  result.V = this->rbegin()->getIntegral();
473 
474  } catch(const JValueOutOfRange& exception) {
475  throw exception;
476  }
477 
478  return result;
479 
480  } else if (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision) {
481 
482  try {
483 
484  result = this->getExceptionHandler().action(MAKE_EXCEPTION(JValueOutOfRange, "abscissa out of range "
485  << STREAM("?") << x << " > " << STREAM("?") << this->rbegin() ->getX()));
486 
487  // overwrite integral values
488 
489  result.v = this->rbegin()->getIntegral();
490  result.V = this->rbegin()->getIntegral();
491 
492  } catch(const JValueOutOfRange& exception) {
493  throw exception;
494  }
495 
496  return result;
497  }
498 
499  ++pX; // next argument value
500 
501  {
502  const int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
503 
504  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
505  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
506 
507 
508  int j = 0;
509 
510  for (int i = 0; i != n; ++p, ++i) {
511 
512  u[i] = this->getDistance(x, p->getX());
513 
514  w[i][0] = v[i][0] = JFunctional<argument_type, data_type>::getValue(p->getY(), pX);
515  w[i][1] = v[i][1] = JMATH::zero;
516  w[i][2] = v[i][2] = p->getIntegral();
517 
518  if (fabs(u[i]) < fabs(u[j])) {
519  j = i;
520  }
521  }
522 
523 
524  result.f = v[j][0];
525  result.fp = v[j][1];
526  result.v = v[j][2];
527  result.V = this->rbegin()->getIntegral();
528 
529  --j;
530 
531  for (int m = 1; m != n; ++m) {
532 
533  for (int i = 0; i != n-m; ++i) {
534 
535  const double ho = u[ i ];
536  const double hp = u[i+m];
537  const double dx = ho - hp;
538 
539  for (int k = 0; k != 3; ++k) {
540  r[k] = (v[i+1][k] - w[i][k]) / dx;
541  }
542 
543  v[i][0] = ho * r[0];
544  w[i][0] = hp * r[0];
545  v[i][1] = ho * r[1] - r[0];
546  w[i][1] = hp * r[1] - r[0];
547  v[i][2] = ho * r[2];
548  w[i][2] = hp * r[2];
549  }
550 
551  if (2*(j+1) < n - m) {
552 
553  result.f += v[j+1][0];
554  result.fp += v[j+1][1];
555  result.v += v[j+1][2];
556 
557  } else {
558 
559  result.f += w[j][0];
560  result.fp += w[j][1];
561  result.v += w[j][2];
562 
563  --j;
564  }
565  }
566  }
567 
568  return result;
569  }
570 
571  protected:
572  /**
573  * Function compilation.
574  */
575  virtual void do_compile() override
576  {
578 
579  if (this->getSize() > 1) {
580 
581  const JGaussLegendre engine(N);
582 
583  this->begin()->setIntegral(V);
584 
585  for (iterator j = this->begin(), i = j++; j != this->end(); ++i, ++j) {
586 
587  const abscissa_type xmin = i->getX();
588  const abscissa_type xmax = j->getX();
589 
590  for (JGaussLegendre::const_iterator m = engine.begin(); m != engine.end(); ++m) {
591 
592  const abscissa_type x = 0.5 * (xmax + xmin + m->getX() * (xmax - xmin));
593  const ordinate_type v = 0.5 * (xmax - xmin) * m->getY() * get_value(this->evaluate(&x));
594 
595  V += v;
596  }
597 
598  j->setIntegral(V);
599  }
600  }
601  }
602 
603 
604  mutable double u[N+1];
605  mutable data_type v[N+1][3];
606  mutable data_type w[N+1][3];
607  mutable data_type r[3];
608 
610  };
611 
612 
613  /**
614  * Template definition of base class for polynomial interpolations with polynomial result.
615  */
616  template<unsigned int N,
617  class JElement_t,
618  template<class, class> class JCollection_t,
619  class JResult_t,
620  class JDistance_t>
622 
623 
624  /**
625  * Template base class for polynomial interpolations with polynomial result.
626  *
627  * This class partially implements the JFunctional interface.
628  */
629  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t, unsigned int M>
630  class JPolintCollection<N,
631  JElement_t,
632  JCollection_t,
633  JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
634  JDistance_t> :
635  public JCollection_t<JElement_t, JDistance_t>,
636  public virtual JFunctional<>,
637  private JLANG::JAssert<M <= N>
638  {
639  public:
640 
641  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
642 
643  typedef typename collection_type::abscissa_type abscissa_type;
644  typedef typename collection_type::ordinate_type ordinate_type;
645  typedef typename collection_type::value_type value_type;
646  typedef typename collection_type::distance_type distance_type;
647 
648  typedef typename collection_type::const_iterator const_iterator;
649  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
650  typedef typename collection_type::iterator iterator;
651  typedef typename collection_type::reverse_iterator reverse_iterator;
652 
653  typedef typename JResultType<ordinate_type>::result_type data_type;
654  typedef JFunction<abscissa_type, JResultPolynome<M, data_type> > function_type;
655 
656  typedef typename function_type::argument_type argument_type;
657  typedef typename function_type::result_type result_type;
658 
659  using JFunctional<>::compile;
660 
661 
662  /**
663  * Default contructor.
664  */
665  JPolintCollection()
666  {}
667 
668 
669  /**
670  * Recursive interpolation method implementation.
671  *
672  * \param pX pointer to abscissa values
673  * \return function value
674  */
675  result_type evaluate(const argument_type* pX) const
676  {
677  const argument_type x = *pX;
678 
679  if (this->size() <= N) {
680  THROW(JFunctionalException, "not enough data " << STREAM("?") << x);
681  }
682 
683  const_iterator p = this->lower_bound(x);
684 
685  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
686  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
687 
688  THROW(JValueOutOfRange, "abscissa out of range "
689  << STREAM("?") << x << " <> "
690  << STREAM("?") << this->begin() ->getX() << ' '
691  << STREAM("?") << this->rbegin()->getX());
692  }
693 
694  ++pX; // next argument value
695 
696 
697  const int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
698 
699  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
700  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
701 
702 
703  int j = 0;
704 
705  for (int i = 0; i != n; ++p, ++i) {
706 
707  u[i] = this->getDistance(x, p->getX());
708 
709  w[i][0] = v[i][0] = JFunctional<argument_type, data_type>::getValue(p->getY(), pX);
710 
711  for (unsigned int k = 1; k != M+1; ++k) {
712  w[i][k] = v[i][k] = JMATH::zero;
713  }
714 
715  if (fabs(u[i]) < fabs(u[j])) {
716  j = i;
717  }
718  }
719 
720 
721  for (unsigned int k = 0; k != M+1; ++k) {
722  result.y[k] = v[j][k];
723  }
724 
725  --j;
726 
727  for (int m = 1; m != n; ++m) {
728 
729  for (int i = 0; i != n-m; ++i) {
730 
731  const double ho = u[ i ];
732  const double hp = u[i+m];
733  const double dx = ho - hp;
734 
735  for (int k = 0; k != M+1; ++k) {
736  r[k] = (v[i+1][k] - w[i][k]) / dx;
737  }
738 
739  v[i][0] = ho * r[0];
740  w[i][0] = hp * r[0];
741 
742  for (int k = 1; k != M+1; ++k) {
743  v[i][k] = ho * r[k] - k * r[k-1];
744  w[i][k] = hp * r[k] - k * r[k-1];
745  }
746  }
747 
748  if (2*(j+1) < n - m) {
749 
750  for (int k = 0; k != M+1; ++k) {
751  result.y[k] += v[j+1][k];
752  }
753 
754  } else {
755 
756  for (int k = 0; k != M+1; ++k) {
757  result.y[k] += w[j][k];
758  }
759 
760  --j;
761  }
762  }
763 
764  return result;
765  }
766 
767  protected:
768  /**
769  * Function compilation.
770  */
771  virtual void do_compile() override
772  {}
773 
774 
775  private:
776  mutable double u[N+1];
777  mutable data_type v[N+1][M+1];
778  mutable data_type w[N+1][M+1];
779  mutable data_type r[M+1];
780 
781  mutable result_type result;
782  };
783 
784 
785  /**
786  * Template specialisation for polynomial interpolation method with returning JResultPolynome data structure.
787  */
788  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t, unsigned int M>
790  JElement_t,
791  JCollection_t,
792  JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
793  JDistance_t> :
794  public JPolintCollection<N,
795  JElement_t,
796  JCollection_t,
797  JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
798  JDistance_t>,
799  public JFunction<typename JElement_t::abscissa_type,
800  JResultPolynome<N, typename JResultType<typename JElement_t::ordinate_type>::result_type> >
801  {
802  public:
803 
804  typedef JPolintCollection<N,
805  JElement_t,
806  JCollection_t,
808  JDistance_t> collection_type;
809 
810  typedef typename collection_type::abscissa_type abscissa_type;
811  typedef typename collection_type::ordinate_type ordinate_type;
812  typedef typename collection_type::value_type value_type;
813  typedef typename collection_type::distance_type distance_type;
814 
815  typedef typename collection_type::const_iterator const_iterator;
816  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
817  typedef typename collection_type::iterator iterator;
818  typedef typename collection_type::reverse_iterator reverse_iterator;
819 
822 
826 
827 
828  /**
829  * Default contructor.
830  */
832  {}
833 
834 
835  /**
836  * Recursive interpolation method implementation.
837  *
838  * \param pX pointer to abscissa values
839  * \return function value
840  */
841  virtual result_type evaluate(const argument_type* pX) const override
842  {
843  try {
844  return collection_type::evaluate(pX);
845  }
846  catch(const JException& error) {
847  return this->getExceptionHandler().action(error);
848  }
849  }
850  };
851 
852 
853  /**
854  * Template specialisation for polynomial interpolation method with returning JResultDerivative data structure.
855  */
856  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
858  JElement_t,
859  JCollection_t,
860  JResultDerivative<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
861  JDistance_t> :
862  public JPolintCollection<N,
863  JElement_t,
864  JCollection_t,
865  JResultPolynome<1, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
866  JDistance_t>,
867  public JFunction<typename JElement_t::abscissa_type,
868  JResultDerivative<typename JResultType<typename JElement_t::ordinate_type>::result_type> >
869  {
870  public:
871 
872  typedef JPolintCollection<N,
873  JElement_t,
874  JCollection_t,
876  JDistance_t> collection_type;
877 
878  typedef typename collection_type::abscissa_type abscissa_type;
879  typedef typename collection_type::ordinate_type ordinate_type;
880  typedef typename collection_type::value_type value_type;
881  typedef typename collection_type::distance_type distance_type;
882 
883  typedef typename collection_type::const_iterator const_iterator;
884  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
885  typedef typename collection_type::iterator iterator;
886  typedef typename collection_type::reverse_iterator reverse_iterator;
887 
890 
894 
896 
897 
898  /**
899  * Default contructor.
900  */
902  {}
903 
904 
905  /**
906  * Recursive interpolation method implementation.
907  *
908  * \param pX pointer to abscissa values
909  * \return function value
910  */
911  virtual result_type evaluate(const argument_type* pX) const override
912  {
913  try {
914  return collection_type::evaluate(pX);
915  }
916  catch(const JException& error) {
917  return this->getExceptionHandler().action(error);
918  }
919  }
920  };
921 
922 
923  /**
924  * Template specialisation for polynomial interpolation method with returning JResultHesse data structure.
925  */
926  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
928  JElement_t,
929  JCollection_t,
930  JResultHesse<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
931  JDistance_t> :
932  public JPolintCollection<N,
933  JElement_t,
934  JCollection_t,
935  JResultPolynome<2, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
936  JDistance_t>,
937  public JFunction<typename JElement_t::abscissa_type,
938  JResultHesse<typename JResultType<typename JElement_t::ordinate_type>::result_type> >
939  {
940  public:
941 
942  typedef JPolintCollection<N,
943  JElement_t,
944  JCollection_t,
946  JDistance_t> collection_type;
947 
948  typedef typename collection_type::abscissa_type abscissa_type;
949  typedef typename collection_type::ordinate_type ordinate_type;
950  typedef typename collection_type::value_type value_type;
951  typedef typename collection_type::distance_type distance_type;
952 
953  typedef typename collection_type::const_iterator const_iterator;
954  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
955  typedef typename collection_type::iterator iterator;
956  typedef typename collection_type::reverse_iterator reverse_iterator;
957 
960 
964 
966 
967 
968  /**
969  * Default contructor.
970  */
972  {}
973 
974 
975  /**
976  * Recursive interpolation method implementation.
977  *
978  * \param pX pointer to abscissa values
979  * \return function value
980  */
981  virtual result_type evaluate(const argument_type* pX) const override
982  {
983  try {
984  return collection_type::evaluate(pX);
985  }
986  catch(const JException& error) {
987  return this->getExceptionHandler().action(error);
988  }
989  }
990  };
991 
992 
993  /**
994  * Template class for polynomial interpolation in 1D
995  *
996  * This class implements the JFunction1D interface.
997  */
998  template<unsigned int N,
999  class JElement_t,
1000  template<class, class> class JCollection_t,
1001  class JResult_t = typename JElement_t::ordinate_type,
1002  class JDistance_t = JDistance<typename JElement_t::abscissa_type> >
1004  public JPolintFunction<N, JElement_t, JCollection_t, JResult_t, JDistance_t>,
1005  public JFunction1D<typename JElement_t::abscissa_type, JResult_t>
1006  {
1007  public:
1008 
1009  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
1010 
1015 
1020 
1022 
1026 
1027 
1028  /**
1029  * Default contructor.
1030  */
1032  {}
1033  };
1034 
1035 
1036  /**
1037  * \cond NEVER
1038  * Forward declarations.
1039  * \endcond
1040  */
1041  template<class JAbscissa_t, class JOrdinate_t>
1042  struct JElement2D;
1043 
1044  template<template<class, class, class> class JMap_t>
1045  struct JMapCollection;
1046 
1047 
1048  /**
1049  * Functional map with polynomial interpolation.
1050  */
1051  template<unsigned int N,
1052  class JKey_t,
1053  class JValue_t,
1054  template<class, class, class> class JMap_t,
1055  class JResult_t,
1056  class JDistance_t = JDistance<JKey_t> >
1057  class JPolintMap :
1058  public JPolintFunction<N,
1059  JElement2D<JKey_t, JValue_t>,
1060  JMapCollection<JMap_t>::template collection_type,
1061  JResult_t,
1062  JDistance_t>
1063  {
1064  public:
1065 
1067  typedef JPolintFunction<N,
1068  element_type,
1069  JMapCollection<JMap_t>::template collection_type,
1070  JResult_t,
1071  JDistance_t> JPolintFunction_t;
1072 
1073  typedef typename JPolintFunction_t::abscissa_type abscissa_type;
1074  typedef typename JPolintFunction_t::ordinate_type ordinate_type;
1075  typedef typename JPolintFunction_t::value_type value_type;
1076  typedef typename JPolintFunction_t::distance_type distance_type;
1077 
1078  typedef typename JPolintFunction_t::const_iterator const_iterator;
1079  typedef typename JPolintFunction_t::const_reverse_iterator const_reverse_iterator;
1080  typedef typename JPolintFunction_t::iterator iterator;
1081  typedef typename JPolintFunction_t::reverse_iterator reverse_iterator;
1082 
1083  typedef typename JPolintFunction_t::argument_type argument_type;
1084  typedef typename JPolintFunction_t::result_type result_type;
1085  typedef typename JPolintFunction_t::JExceptionHandler exceptionhandler_type;
1086 
1087 
1088  /**
1089  * Default constructor.
1090  */
1092  {}
1093  };
1094 
1095 
1096  /**
1097  * Conversion of data points to integral values.
1098  *
1099  * This method transfers the integration to the corresponding specialised function.
1100  *
1101  * \param input collection
1102  * \param output mappable collection
1103  * \return integral
1104  */
1105  template<unsigned int N,
1106  class JElement_t,
1107  template<class, class> class JCollection_t,
1108  class JResult_t,
1109  class JDistance_t>
1110  inline typename JElement_t::ordinate_type
1112  typename JMappable<JElement_t>::map_type& output)
1113  {
1114  return integrate(input, output, JLANG::JBool<N == 0 || N == 1>());
1115  }
1116 
1117 
1118  /**
1119  * Conversion of data points to integral values.
1120  *
1121  * The integration uses the Gauss-Legendre quadratures with the number of points set
1122  * to the degree of the input polynomial interpolating function.
1123  *
1124  * \param input collection
1125  * \param output mappable collection
1126  * \param option false
1127  * \return integral
1128  */
1129  template<unsigned int N,
1130  class JElement_t,
1131  template<class, class> class JCollection_t,
1132  class JResult_t,
1133  class JDistance_t>
1134  inline typename JElement_t::ordinate_type
1136  typename JMappable<JElement_t>::map_type& output,
1137  const JLANG::JBool<false>& option)
1138  {
1139  typedef typename JElement_t::abscissa_type abscissa_type;
1140  typedef typename JElement_t::ordinate_type ordinate_type;
1142 
1143  ordinate_type V(JMATH::zero);
1144 
1145  if (input.getSize() > 1) {
1146 
1147  output.put(input.begin()->getX(), V);
1148 
1149  const JGaussLegendre engine(N);
1150 
1151  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1152 
1153  const abscissa_type xmin = i->getX();
1154  const abscissa_type xmax = j->getX();
1155 
1156  for (JGaussLegendre::const_iterator m = engine.begin(); m != engine.end(); ++m) {
1157 
1158  const abscissa_type x = 0.5 * (xmax + xmin + m->getX() * (xmax - xmin));
1159  const ordinate_type v = 0.5 * (xmax - xmin) * m->getY() * get_value(input(x));
1160 
1161  V += v;
1162  }
1163 
1164  output.put(xmax, V);
1165  }
1166  }
1167 
1168  return V;
1169  }
1170 
1171 
1172  /**
1173  * Conversion of data points to integral values.
1174  *
1175  * The integration is based on the sum of ordinates of the input data points.
1176  *
1177  * \param input collection
1178  * \param output mappable collection
1179  * \param option true
1180  * \return integral
1181  */
1182  template<class JElement_t,
1183  template<class, class> class JCollection_t,
1184  class JResult_t,
1185  class JDistance_t>
1186  inline typename JElement_t::ordinate_type
1188  typename JMappable<JElement_t>::map_type& output,
1189  const JLANG::JBool<true>& option)
1190  {
1191  typedef typename JElement_t::ordinate_type ordinate_type;
1193 
1194  ordinate_type V(JMATH::zero);
1195 
1196  if (input.getSize() > 1) {
1197 
1198  output.put(input.begin()->getX(), V);
1199 
1200  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1201 
1202  V += input.getDistance(i->getX(), j->getX()) * j->getY();
1203 
1204  output.put(j->getX(), V);
1205  }
1206  }
1207 
1208  return V;
1209  }
1210 
1211 
1212  /**
1213  * Conversion of data points to integral values.
1214  *
1215  * The integration is based on the trapezoidal rule applied to the input data points.
1216  *
1217  * \param input collection
1218  * \param output mappable collection
1219  * \param option true
1220  * \return integral
1221  */
1222  template<class JElement_t,
1223  template<class, class> class JCollection_t,
1224  class JResult_t,
1225  class JDistance_t>
1226  inline typename JElement_t::ordinate_type
1228  typename JMappable<JElement_t>::map_type& output,
1229  const JLANG::JBool<true>& option)
1230  {
1231  typedef typename JElement_t::ordinate_type ordinate_type;
1233 
1234  ordinate_type V(JMATH::zero);
1235 
1236  if (input.getSize() > 1) {
1237 
1238  output.put(input.begin()->getX(), V);
1239 
1240  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1241 
1242  V += 0.5 * input.getDistance(i->getX(), j->getX()) * (i->getY() + j->getY());
1243 
1244  output.put(j->getX(), V);
1245  }
1246  }
1247 
1248  return V;
1249  }
1250 }
1251 
1252 #endif
const double xmax
Definition: JQuadrature.cc:24
container_type::reverse_iterator reverse_iterator
Definition: JCollection.hh:94
General exception.
Definition: JException.hh:23
JElement2D< JKey_t, JValue_t > element_type
Definition: JPolint.hh:1066
data_type w[N+1][M+1]
Definition: JPolint.hh:778
Exceptions.
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Definition: JScale.hh:47
Template definition for functional collection with polynomial interpolation.
Definition: JPolint.hh:43
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolint.hh:1025
Numerical integrator for .
Definition: JQuadrature.hh:111
function_type::argument_type argument_type
Definition: JPolint.hh:1023
JElement_t value_type
Definition: JCollection.hh:84
Exception for a functional operation.
Definition: JException.hh:126
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolint.hh:335
The elements in a collection are sorted according to their abscissa values and a given distance opera...
collection_type::value_type value_type
Definition: JPolint.hh:1013
Template class for distance evaluation.
Definition: JDistance.hh:24
Data structure for result including value and N derivatives of function.
Definition: JResult.hh:533
This include file containes various data structures that can be used as specific return types for the...
JPolintFunction1D()
Default contructor.
Definition: JPolint.hh:1031
functional_type::argument_type argument_type
Definition: JFunctional.hh:323
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolint.hh:841
Template interface definition for associative collection of elements.
collection_type::const_iterator const_iterator
Definition: JPolint.hh:1016
virtual void do_compile() override
Function compilation.
Definition: JPolint.hh:771
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
data_type r[M+1]
Definition: JPolint.hh:779
JPolintFunction_t::reverse_iterator reverse_iterator
Definition: JPolint.hh:1081
Generation of compiler error.
Definition: JAssert.hh:17
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolint.hh:240
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
JPolintCollection< N, JElement_t, JCollection_t, JResultPolynome< M, typename JResultType< typename JElement_t::ordinate_type >::result_type >, JDistance_t > collection_type
Definition: JPolint.hh:808
size_t getSize(T(&array)[N])
Get size of c-array.
Definition: JLangToolkit.hh:32
JElement_t::abscissa_type abscissa_type
Definition: JCollection.hh:82
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.
JPolintFunction_t::JExceptionHandler exceptionhandler_type
Definition: JPolint.hh:1085
collection_type::ordinate_type ordinate_type
Definition: JPolint.hh:1012
JPolintMap()
Default constructor.
Definition: JPolint.hh:1091
JPolintFunction_t::iterator iterator
Definition: JPolint.hh:1080
const int n
Definition: JPolint.hh:697
JPolintFunction_t::value_type value_type
Definition: JPolint.hh:1075
JPolintFunction_t::ordinate_type ordinate_type
Definition: JPolint.hh:1074
static result_type getValue(const JFunctional &function, const argument_type *pX)
Recursive function value evaluation.
Definition: JFunctional.hh:107
JPolintFunction_t::const_reverse_iterator const_reverse_iterator
Definition: JPolint.hh:1079
Auxiliary template class for type bool.
Definition: JBool.hh:20
JPolintFunction< N, element_type, JMapCollection< JMap_t >::template collection_type, JResult_t, JDistance_t > JPolintFunction_t
Definition: JPolint.hh:1071
Template definition of base class for polynomial interpolations with polynomial result.
Definition: JPolint.hh:621
container_type::iterator iterator
Definition: JCollection.hh:93
JPolintFunction_t::argument_type argument_type
Definition: JPolint.hh:1083
then JCalibrateToT a
Definition: JTuneHV.sh:116
Data structure for result including value, first derivative and integrals of function.
Definition: JResult.hh:337
Auxiliary data structure for handling std::ostream.
Template definition of function object interface.
Definition: JFunctional.hh:32
return result
Definition: JPolint.hh:764
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolint.hh:452
functional_type::result_type result_type
Definition: JFunctional.hh:324
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolint.hh:911
container_type::const_reverse_iterator const_reverse_iterator
Definition: JCollection.hh:92
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:377
#define MAKE_EXCEPTION(JException_t, A)
Make exception.
Definition: JException.hh:687
void put(typename JClass< key_type >::argument_type key, typename JClass< mapped_type >::argument_type value)
Put pair-wise element (key,value) into collection.
Template class to define the corresponding JCollection for a given template JMap. ...
collection_type::const_reverse_iterator const_reverse_iterator
Definition: JPolint.hh:1017
Data structure for result including value and first derivative of function.
Definition: JResult.hh:43
JDistance_t distance_type
Definition: JCollection.hh:85
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolint.hh:981
Exception handler for functional object.
Definition: JFunctional.hh:131
collection_type::abscissa_type abscissa_type
Definition: JPolint.hh:1011
Template definition of function object interface in multidimensions.
Definition: JFunctional.hh:303
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolint.hh:1009
collection_type::iterator iterator
Definition: JPolint.hh:1018
const double xmin
Definition: JQuadrature.cc:23
Exception for numerical precision error.
Definition: JException.hh:252
functional_type::result_type result_type
Definition: JFunctional.hh:308
Auxiliary classes for numerical integration.
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:36
JElement_t::ordinate_type ordinate_type
Definition: JCollection.hh:83
JPolintCollection< N, JElement_t, JCollection_t, JResultPolynome< 1, typename JResultType< typename JElement_t::ordinate_type >::result_type >, JDistance_t > collection_type
Definition: JPolint.hh:876
virtual result_type evaluate(const argument_type *pX) const override
Recursive interpolation method implementation.
Definition: JPolint.hh:98
Functional map with polynomial interpolation.
Definition: JPolint.hh:1057
2D Element.
Definition: JElement.hh:46
JPolintCollection< N, JElement_t, JCollection_t, JResultPolynome< 2, typename JResultType< typename JElement_t::ordinate_type >::result_type >, JDistance_t > collection_type
Definition: JPolint.hh:946
container_type::const_iterator const_iterator
Definition: JCollection.hh:91
JPolintFunction_t::result_type result_type
Definition: JPolint.hh:1084
int j
Definition: JPolint.hh:703
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
JPolintFunction_t::abscissa_type abscissa_type
Definition: JPolint.hh:1073
collection_type::reverse_iterator reverse_iterator
Definition: JPolint.hh:1019
collection_type::distance_type distance_type
Definition: JPolint.hh:1014
data_type v[N+1][M+1]
Definition: JPolint.hh:777
double u[N+1]
Definition: JPolint.hh:776
Data structure for result including value and first derivative of function.
Definition: JResult.hh:212
JFunction1D< abscissa_type, JResult_t > function_type
Definition: JPolint.hh:1021
function_type::result_type result_type
Definition: JPolint.hh:1024
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:998
JPolintFunction_t::distance_type distance_type
Definition: JPolint.hh:1076
JPolintFunction_t::const_iterator const_iterator
Definition: JPolint.hh:1078
Template class for polynomial interpolation in 1D.
Definition: JPolint.hh:1003
JElement_t::ordinate_type integrate(const JCollection< JElement_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of data points to integral values.
Definition: JCollection.hh:812