Jpp
 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"
11 #include "JTools/JFunctional.hh"
12 #include "JTools/JDistance.hh"
13 #include "JTools/JResult.hh"
14 #include "JTools/JElement.hh"
15 #include "JTools/JMapCollection.hh"
16 #include "JTools/JQuadrature.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  * Template definition for functional collection with polynomial interpolation.
36  */
37  template<unsigned int N,
38  class JElement_t,
39  template<class, class> class JCollection_t,
40  class JResult_t,
41  class JDistance_t>
43 
44 
45  /**
46  * Template specialisation for functional collection with polynomial interpolation.
47  *
48  * Polynomial interpolation code is taken from reference:
49  * Numerical Recipes in C++, W.H. Press, S.A. Teukolsky, W.T. Vetterling and B.P. Flannery,
50  * Cambridge University Press.
51  */
52  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
54  JElement_t,
55  JCollection_t,
56  typename JResultType<typename JElement_t::ordinate_type>::result_type,
57  JDistance_t> :
58  public JCollection_t<JElement_t, JDistance_t>,
59  public JFunction<typename JElement_t::abscissa_type,
60  typename JResultType<typename JElement_t::ordinate_type>::result_type>
61  {
62  public:
63 
64  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
65 
66  typedef typename collection_type::abscissa_type abscissa_type;
67  typedef typename collection_type::ordinate_type ordinate_type;
68  typedef typename collection_type::value_type value_type;
69  typedef typename collection_type::distance_type distance_type;
70 
71  typedef typename collection_type::const_iterator const_iterator;
72  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
73  typedef typename collection_type::iterator iterator;
74  typedef typename collection_type::reverse_iterator reverse_iterator;
75 
78 
82 
83 
84  /**
85  * Default constructor.
86  */
88  {}
89 
90 
91  /**
92  * Recursive interpolation method implementation.
93  *
94  * \param pX pointer to abscissa values
95  * \return function value
96  */
97  virtual result_type evaluate(const argument_type* pX) const
98  {
99  if (this->size() <= 1u) {
100  return this->getExceptionHandler().action(JFunctionalException("JPolintFunction<>::evaluate() not enough data."));
101  }
102 
103  const argument_type x = *pX;
104 
105  const_iterator p = this->lower_bound(x);
106 
107  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
108  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
109  return this->getExceptionHandler().action(JValueOutOfRange("JPolintFunction::evaluate() x out of range."));
110  }
111 
112  ++pX; // next argument value
113 
114 
115  const int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
116 
117  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
118  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
119 
120 
121  int j = 0;
122 
123  for (int i = 0; i != n; ++p, ++i) {
124 
125  u[i] = this->getDistance(x, p->getX());
126  v[i] = function_type::getValue(p->getY(), pX);
127  w[i] = v[i];
128 
129  if (fabs(u[i]) < fabs(u[j])) {
130  j = i;
131  }
132  }
133 
134 
135  result_type y = v[j];
136 
137  --j;
138 
139  for (int m = 1; m != n; ++m) {
140 
141  for (int i = 0; i != n-m; ++i) {
142 
143  const double ho = u[ i ];
144  const double hp = u[i+m];
145  const double dx = ho - hp;
146 
147  v[i] = v[i+1];
148  v[i] -= w[ i ];
149  w[i] = v[ i ];
150 
151  v[i] *= ho/dx;
152  w[i] *= hp/dx;
153  }
154 
155  if (2*(j+1) < n - m)
156  y += v[j+1];
157  else
158  y += w[j--];
159  }
160 
161  return y;
162  }
163 
164  private:
165  /**
166  * Function compilation.
167  */
168  virtual void do_compile()
169  {}
170 
171 
172  mutable double u[N+1];
173  mutable result_type v[N+1];
174  mutable result_type w[N+1];
175  };
176 
177 
178  /**
179  * Template specialisation for zero-degree polynomial interpolation method.\n
180  * The interpolation is based on a simple lookup table.
181  */
182  template<class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
183  class JPolintFunction<0,
184  JElement_t,
185  JCollection_t,
186  typename JResultType<typename JElement_t::ordinate_type>::result_type,
187  JDistance_t> :
188  public JCollection_t<JElement_t, JDistance_t>,
189  public JFunction<typename JElement_t::abscissa_type,
190  typename JResultType<typename JElement_t::ordinate_type>::result_type>
191  {
192  public:
193 
194  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
195 
196  typedef typename collection_type::abscissa_type abscissa_type;
197  typedef typename collection_type::ordinate_type ordinate_type;
198  typedef typename collection_type::value_type value_type;
199  typedef typename collection_type::distance_type distance_type;
200 
201  typedef typename collection_type::const_iterator const_iterator;
202  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
203  typedef typename collection_type::iterator iterator;
204  typedef typename collection_type::reverse_iterator reverse_iterator;
205 
208 
212 
213 
214  /**
215  * Default constructor.
216  */
218  {}
219 
220 
221  /**
222  * Recursive interpolation method implementation.
223  *
224  * \param pX pointer to abscissa values
225  * \return function value
226  */
227  virtual result_type evaluate(const argument_type* pX) const
228  {
229  if (this->size() <= 1u) {
230  return this->getExceptionHandler().action(JFunctionalException("JPolintFunction<>::evaluate() not enough data."));
231  }
232 
233  const argument_type x = *pX;
234 
235  const_iterator p = this->lower_bound(x);
236 
237  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
238  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
239 
240  return this->getExceptionHandler().action(JValueOutOfRange("JPolintFunction::evaluate() x out of range."));
241  }
242 
243  ++pX; // next argument value
244 
245 
246  const_iterator q = p--;
247 
248  if (q == this->begin() || this->getDistance(x, q->getX()) < this->getDistance(p->getX(), x))
249  return function_type::getValue(q->getY(), pX);
250  else
251  return function_type::getValue(p->getY(), pX);
252  }
253 
254  private:
255  /**
256  * Function compilation.
257  */
258  virtual void do_compile()
259  {}
260  };
261 
262 
263  /**
264  * Template specialisation for first-degree polynomial interpolation method.\n
265  * The interpolation is based on a simple linear interpolation.
266  */
267  template<class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
268  class JPolintFunction<1,
269  JElement_t,
270  JCollection_t,
271  typename JResultType<typename JElement_t::ordinate_type>::result_type,
272  JDistance_t> :
273  public JCollection_t<JElement_t, JDistance_t>,
274  public JFunction<typename JElement_t::abscissa_type,
275  typename JResultType<typename JElement_t::ordinate_type>::result_type>
276  {
277  public:
278 
279  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
280 
281  typedef typename collection_type::abscissa_type abscissa_type;
282  typedef typename collection_type::ordinate_type ordinate_type;
283  typedef typename collection_type::value_type value_type;
284  typedef typename collection_type::distance_type distance_type;
285 
286  typedef typename collection_type::const_iterator const_iterator;
287  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
288  typedef typename collection_type::iterator iterator;
289  typedef typename collection_type::reverse_iterator reverse_iterator;
290 
293 
297 
298 
299  /**
300  * Default constructor.
301  */
303  {}
304 
305 
306  /**
307  * Recursive interpolation method implementation.
308  *
309  * \param pX pointer to abscissa values
310  * \return function value
311  */
312  virtual result_type evaluate(const argument_type* pX) const
313  {
314  if (this->size() <= 1u) {
315  return this->getExceptionHandler().action(JFunctionalException("JPolintFunction<>::evaluate() not enough data."));
316  }
317 
318  const argument_type x = *pX;
319 
320  const_iterator p = this->lower_bound(x);
321 
322  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
323  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
324 
325  return this->getExceptionHandler().action(JValueOutOfRange("JPolintFunction::evaluate() x out of range."));
326  }
327 
328  ++pX; // next argument value
329 
330 
331  const_iterator q = p--;
332 
333  const double dx = this->getDistance(p->getX(), q->getX());
334  const double a = this->getDistance(x, q->getX()) / dx;
335  const double b = 1.0 - a;
336 
337  ya = function_type::getValue(p->getY(), pX);
338  yb = function_type::getValue(q->getY(), pX);
339 
340  ya *= a;
341  yb *= b;
342 
343  ya += yb;
344 
345  return ya;
346  }
347 
348  private:
349  /**
350  * Function compilation.
351  */
352  virtual void do_compile()
353  {}
354 
355  mutable result_type ya;
356  mutable result_type yb;
357  };
358 
359 
360  /**
361  * Template specialisation for polynomial interpolation method with returning JResultPDF data structure.
362  *
363  * Note that the data structure of the elements in the collection should have the additional methods:
364  * <pre>
365  * ordinate_type getIntegral() const;
366  * void setIntegral(ordinate_type v);
367  * </pre>
368  * to get and set the integral values, respectively.
369  */
370  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
372  JElement_t,
373  JCollection_t,
374  JResultPDF<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
375  JDistance_t> :
376  public JCollection_t<JElement_t, JDistance_t>,
377  public JFunction<typename JElement_t::abscissa_type,
378  JResultPDF<typename JResultType<typename JElement_t::ordinate_type>::result_type> >
379  {
380  public:
381 
382  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
383 
384  typedef typename collection_type::abscissa_type abscissa_type;
385  typedef typename collection_type::ordinate_type ordinate_type;
386  typedef typename collection_type::value_type value_type;
387  typedef typename collection_type::distance_type distance_type;
388 
389  typedef typename collection_type::const_iterator const_iterator;
390  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
391  typedef typename collection_type::iterator iterator;
392  typedef typename collection_type::reverse_iterator reverse_iterator;
393 
396 
400 
402 
403 
404  /**
405  * Default contructor.
406  */
408  {}
409 
410 
411  /**
412  * Recursive interpolation method implementation.
413  *
414  * \param pX pointer to abscissa values
415  * \return function value
416  */
417  virtual result_type evaluate(const argument_type* pX) const
418  {
419  if (this->size() <= 1u) {
420  return this->getExceptionHandler().action(JFunctionalException("JPolintFunction<>::evaluate() not enough data."));
421  }
422 
423  const argument_type x = *pX;
424 
425  const_iterator p = this->lower_bound(x);
426 
427  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
428  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
429 
430  return this->getExceptionHandler().action(JValueOutOfRange("JPolintFunction::evaluate() x out of range."));
431  }
432 
433  ++pX; // next argument value
434 
435  {
436  const int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
437 
438  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
439  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
440 
441 
442  int j = 0;
443 
444  for (int i = 0; i != n; ++p, ++i) {
445 
446  u[i] = this->getDistance(x, p->getX());
447 
448  w[i][0] = v[i][0] = JFunctional<argument_type, data_type>::getValue(p->getY(), pX);
449  w[i][1] = v[i][1] = JMATH::zero;
450  w[i][2] = v[i][2] = p->getIntegral();
451 
452  if (fabs(u[i]) < fabs(u[j])) {
453  j = i;
454  }
455  }
456 
457 
458  result.f = v[j][0];
459  result.fp = v[j][1];
460  result.v = v[j][2];
461  result.V = this->rbegin()->getIntegral();
462 
463  --j;
464 
465  for (int m = 1; m != n; ++m) {
466 
467  for (int i = 0; i != n-m; ++i) {
468 
469  const double ho = u[ i ];
470  const double hp = u[i+m];
471  const double dx = ho - hp;
472 
473  for (int k = 0; k != 3; ++k) {
474  r[k] = (v[i+1][k] - w[i][k]) / dx;
475  }
476 
477  v[i][0] = ho * r[0];
478  w[i][0] = hp * r[0];
479  v[i][1] = ho * r[1] - r[0];
480  w[i][1] = hp * r[1] - r[0];
481  v[i][2] = ho * r[2];
482  w[i][2] = hp * r[2];
483  }
484 
485  if (2*(j+1) < n - m) {
486 
487  result.f += v[j+1][0];
488  result.fp += v[j+1][1];
489  result.v += v[j+1][2];
490 
491  } else {
492 
493  result.f += w[j][0];
494  result.fp += w[j][1];
495  result.v += w[j][2];
496 
497  --j;
498  }
499  }
500  }
501 
502  return result;
503  }
504 
505  private:
506  /**
507  * Function compilation.
508  */
509  virtual void do_compile()
510  {
512 
513  if (this->getSize() > 1) {
514 
515  const JGaussLegendre engine(N);
516 
517  this->begin()->setIntegral(V);
518 
519  for (iterator j = this->begin(), i = j++; j != this->end(); ++i, ++j) {
520 
521  const abscissa_type xmin = i->getX();
522  const abscissa_type xmax = j->getX();
523 
524  for (JGaussLegendre::const_iterator m = engine.begin(); m != engine.end(); ++m) {
525 
526  const abscissa_type x = 0.5 * (xmax + xmin + m->getX() * (xmax - xmin));
527  const ordinate_type v = 0.5 * (xmax - xmin) * m->getY() * get_value(this->evaluate(&x));
528 
529  V += v;
530  }
531 
532  j->setIntegral(V);
533  }
534  }
535  }
536 
537 
538  mutable double u[N+1];
539  mutable data_type v[N+1][3];
540  mutable data_type w[N+1][3];
541  mutable data_type r[3];
542 
544  };
545 
546 
547  /**
548  * Template definition of base class for polynomial interpolations with polynomial result.
549  */
550  template<unsigned int N,
551  class JElement_t,
552  template<class, class> class JCollection_t,
553  class JResult_t,
554  class JDistance_t>
556 
557 
558  /**
559  * Template base class for polynomial interpolations with polynomial result.
560  *
561  * This class partially implements the JFunctional interface.
562  */
563  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t, unsigned int M>
564  class JPolintCollection<N,
565  JElement_t,
566  JCollection_t,
567  JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
568  JDistance_t> :
569  public JCollection_t<JElement_t, JDistance_t>,
570  public virtual JFunctional<>,
571  private JLANG::JAssert<M <= N>
572  {
573  public:
574 
575  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
576 
577  typedef typename collection_type::abscissa_type abscissa_type;
578  typedef typename collection_type::ordinate_type ordinate_type;
579  typedef typename collection_type::value_type value_type;
580  typedef typename collection_type::distance_type distance_type;
581 
582  typedef typename collection_type::const_iterator const_iterator;
583  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
584  typedef typename collection_type::iterator iterator;
585  typedef typename collection_type::reverse_iterator reverse_iterator;
586 
587  typedef typename JResultType<ordinate_type>::result_type data_type;
588  typedef JFunction<abscissa_type, JResultPolynome<M, data_type> > function_type;
589 
590  typedef typename function_type::argument_type argument_type;
591  typedef typename function_type::result_type result_type;
592 
593  using JFunctional<>::compile;
594 
595 
596  /**
597  * Default contructor.
598  */
599  JPolintCollection()
600  {}
601 
602 
603  /**
604  * Recursive interpolation method implementation.
605  *
606  * \param pX pointer to abscissa values
607  * \return function value
608  */
609  result_type evaluate(const argument_type* pX) const
610  {
611  if (this->size() <= N) {
612  THROW(JFunctionalException, "JPolintFunction<>::evaluate() not enough data.");
613  }
614 
615  const argument_type x = *pX;
616 
617  const_iterator p = this->lower_bound(x);
618 
619  if ((p == this->begin() && this->getDistance(x, (p++)->getX()) > distance_type::precision) ||
620  (p == this->end() && this->getDistance((--p)->getX(), x) > distance_type::precision)) {
621 
622  THROW(JValueOutOfRange, "JPolintFunction::evaluate() x out of range.");
623  }
624 
625  ++pX; // next argument value
626 
627 
628  const int n = std::min((int) (N + 1), (int) this->size()); // number of points to interpolate
629 
630  for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {} // move p to begin of data
631  for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
632 
633 
634  int j = 0;
635 
636  for (int i = 0; i != n; ++p, ++i) {
637 
638  u[i] = this->getDistance(x, p->getX());
639 
640  w[i][0] = v[i][0] = JFunctional<argument_type, data_type>::getValue(p->getY(), pX);
641 
642  for (unsigned int k = 1; k != M+1; ++k) {
643  w[i][k] = v[i][k] = JMATH::zero;
644  }
645 
646  if (fabs(u[i]) < fabs(u[j])) {
647  j = i;
648  }
649  }
650 
651 
652  for (unsigned int k = 0; k != M+1; ++k) {
653  result.y[k] = v[j][k];
654  }
655 
656  --j;
657 
658  for (int m = 1; m != n; ++m) {
659 
660  for (int i = 0; i != n-m; ++i) {
661 
662  const double ho = u[ i ];
663  const double hp = u[i+m];
664  const double dx = ho - hp;
665 
666  for (int k = 0; k != M+1; ++k) {
667  r[k] = (v[i+1][k] - w[i][k]) / dx;
668  }
669 
670  v[i][0] = ho * r[0];
671  w[i][0] = hp * r[0];
672 
673  for (int k = 1; k != M+1; ++k) {
674  v[i][k] = ho * r[k] - k * r[k-1];
675  w[i][k] = hp * r[k] - k * r[k-1];
676  }
677  }
678 
679  if (2*(j+1) < n - m) {
680 
681  for (int k = 0; k != M+1; ++k) {
682  result.y[k] += v[j+1][k];
683  }
684 
685  } else {
686 
687  for (int k = 0; k != M+1; ++k) {
688  result.y[k] += w[j][k];
689  }
690 
691  --j;
692  }
693  }
694 
695  return result;
696  }
697 
698  private:
699  /**
700  * Function compilation.
701  */
702  virtual void do_compile()
703  {}
704 
705 
706  mutable double u[N+1];
707  mutable data_type v[N+1][M+1];
708  mutable data_type w[N+1][M+1];
709  mutable data_type r[M+1];
710 
711  mutable result_type result;
712  };
713 
714 
715  /**
716  * Template specialisation for polynomial interpolation method with returning JResultPolynome data structure.
717  */
718  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t, unsigned int M>
720  JElement_t,
721  JCollection_t,
722  JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
723  JDistance_t> :
724  public JPolintCollection<N,
725  JElement_t,
726  JCollection_t,
727  JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
728  JDistance_t>,
729  public JFunction<typename JElement_t::abscissa_type,
730  JResultPolynome<N, typename JResultType<typename JElement_t::ordinate_type>::result_type> >
731  {
732  public:
733 
734  typedef JPolintCollection<N,
735  JElement_t,
736  JCollection_t,
738  JDistance_t> collection_type;
739 
740  typedef typename collection_type::abscissa_type abscissa_type;
741  typedef typename collection_type::ordinate_type ordinate_type;
742  typedef typename collection_type::value_type value_type;
743  typedef typename collection_type::distance_type distance_type;
744 
745  typedef typename collection_type::const_iterator const_iterator;
746  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
747  typedef typename collection_type::iterator iterator;
748  typedef typename collection_type::reverse_iterator reverse_iterator;
749 
752 
756 
757 
758  /**
759  * Default contructor.
760  */
762  {}
763 
764 
765  /**
766  * Recursive interpolation method implementation.
767  *
768  * \param pX pointer to abscissa values
769  * \return function value
770  */
771  virtual result_type evaluate(const argument_type* pX) const
772  {
773  try {
774  return collection_type::evaluate(pX);
775  }
776  catch(const JException& error) {
777  return this->getExceptionHandler().action(error);
778  }
779  }
780  };
781 
782 
783  /**
784  * Template specialisation for polynomial interpolation method with returning JResultDerivative data structure.
785  */
786  template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
788  JElement_t,
789  JCollection_t,
790  JResultDerivative<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
791  JDistance_t> :
792  public JPolintCollection<N,
793  JElement_t,
794  JCollection_t,
795  JResultPolynome<1, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
796  JDistance_t>,
797  public JFunction<typename JElement_t::abscissa_type,
798  JResultDerivative<typename JResultType<typename JElement_t::ordinate_type>::result_type> >
799  {
800  public:
801 
802  typedef JPolintCollection<N,
803  JElement_t,
804  JCollection_t,
806  JDistance_t> collection_type;
807 
808  typedef typename collection_type::abscissa_type abscissa_type;
809  typedef typename collection_type::ordinate_type ordinate_type;
810  typedef typename collection_type::value_type value_type;
811  typedef typename collection_type::distance_type distance_type;
812 
813  typedef typename collection_type::const_iterator const_iterator;
814  typedef typename collection_type::const_reverse_iterator const_reverse_iterator;
815  typedef typename collection_type::iterator iterator;
816  typedef typename collection_type::reverse_iterator reverse_iterator;
817 
820 
824 
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
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 JResultHesse 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  JResultHesse<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
861  JDistance_t> :
862  public JPolintCollection<N,
863  JElement_t,
864  JCollection_t,
865  JResultPolynome<2, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
866  JDistance_t>,
867  public JFunction<typename JElement_t::abscissa_type,
868  JResultHesse<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
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 class for polynomial interpolation in 1D
925  *
926  * This class implements the JFunction1D interface.
927  */
928  template<unsigned int N,
929  class JElement_t,
930  template<class, class> class JCollection_t,
931  class JResult_t = typename JElement_t::ordinate_type,
934  public JPolintFunction<N, JElement_t, JCollection_t, JResult_t, JDistance_t>,
935  public JFunction1D<typename JElement_t::abscissa_type, JResult_t>
936  {
937  public:
938 
939  typedef JCollection_t<JElement_t, JDistance_t> collection_type;
940 
945 
950 
952 
956 
957 
958  /**
959  * Default contructor.
960  */
962  {}
963  };
964 
965 
966  /**
967  * Functional map with polynomial interpolation.
968  */
969  template<unsigned int N,
970  class JKey_t,
971  class JValue_t,
972  template<class, class, class> class JMap_t,
973  class JResult_t,
974  class JDistance_t = JDistance<JKey_t> >
975  class JPolintMap :
976  public JPolintFunction<N,
977  JElement2D<JKey_t, JValue_t>,
978  JMapCollection<JMap_t>::template collection_type,
979  JResult_t,
980  JDistance_t>
981  {
982  public:
983 
985  typedef JPolintFunction<N,
986  element_type,
987  JMapCollection<JMap_t>::template collection_type,
988  JResult_t,
989  JDistance_t> JPolintFunction_t;
990 
991  typedef typename JPolintFunction_t::abscissa_type abscissa_type;
992  typedef typename JPolintFunction_t::ordinate_type ordinate_type;
993  typedef typename JPolintFunction_t::value_type value_type;
994  typedef typename JPolintFunction_t::distance_type distance_type;
995 
996  typedef typename JPolintFunction_t::const_iterator const_iterator;
997  typedef typename JPolintFunction_t::const_reverse_iterator const_reverse_iterator;
998  typedef typename JPolintFunction_t::iterator iterator;
999  typedef typename JPolintFunction_t::reverse_iterator reverse_iterator;
1000 
1001  typedef typename JPolintFunction_t::argument_type argument_type;
1002  typedef typename JPolintFunction_t::result_type result_type;
1003  typedef typename JPolintFunction_t::JExceptionHandler exceptionhandler_type;
1004 
1005 
1006  /**
1007  * Default constructor.
1008  */
1010  {}
1011  };
1012 
1013 
1014  /**
1015  * Conversion of data points to integral values.
1016  *
1017  * This method transfers the integration to the corresponding specialised function.
1018  *
1019  * \param input collection
1020  * \param output mappable collection
1021  * \return integral
1022  */
1023  template<unsigned int N,
1024  class JElement_t,
1025  template<class, class> class JCollection_t,
1026  class JResult_t,
1027  class JDistance_t>
1028  inline typename JElement_t::ordinate_type
1030  typename JMappable<JElement_t>::map_type& output)
1031  {
1032  return integrate(input, output, JLANG::JBool<N == 0 || N == 1>());
1033  }
1034 
1035 
1036  /**
1037  * Conversion of data points to integral values.
1038  *
1039  * The integration uses the Gauss-Legendre quadratures with the number of points set
1040  * to the degree of the input polynomial interpolating function.
1041  *
1042  * \param input collection
1043  * \param output mappable collection
1044  * \param option false
1045  * \return integral
1046  */
1047  template<unsigned int N,
1048  class JElement_t,
1049  template<class, class> class JCollection_t,
1050  class JResult_t,
1051  class JDistance_t>
1052  inline typename JElement_t::ordinate_type
1054  typename JMappable<JElement_t>::map_type& output,
1055  const JLANG::JBool<false>& option)
1056  {
1057  typedef typename JElement_t::abscissa_type abscissa_type;
1058  typedef typename JElement_t::ordinate_type ordinate_type;
1060 
1061  ordinate_type V(JMATH::zero);
1062 
1063  if (input.getSize() > 1) {
1064 
1065  output.put(input.begin()->getX(), V);
1066 
1067  const JGaussLegendre engine(N);
1068 
1069  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1070 
1071  const abscissa_type xmin = i->getX();
1072  const abscissa_type xmax = j->getX();
1073 
1074  for (JGaussLegendre::const_iterator m = engine.begin(); m != engine.end(); ++m) {
1075 
1076  const abscissa_type x = 0.5 * (xmax + xmin + m->getX() * (xmax - xmin));
1077  const ordinate_type v = 0.5 * (xmax - xmin) * m->getY() * get_value(input(x));
1078 
1079  V += v;
1080  }
1081 
1082  output.put(xmax, V);
1083  }
1084  }
1085 
1086  return V;
1087  }
1088 
1089 
1090  /**
1091  * Conversion of data points to integral values.
1092  *
1093  * The integration is based on the sum of ordinates of the input data points.
1094  *
1095  * \param input collection
1096  * \param output mappable collection
1097  * \param option true
1098  * \return integral
1099  */
1100  template<class JElement_t,
1101  template<class, class> class JCollection_t,
1102  class JResult_t,
1103  class JDistance_t>
1104  inline typename JElement_t::ordinate_type
1106  typename JMappable<JElement_t>::map_type& output,
1107  const JLANG::JBool<true>& option)
1108  {
1109  typedef typename JElement_t::ordinate_type ordinate_type;
1111 
1112  ordinate_type V(JMATH::zero);
1113 
1114  if (input.getSize() > 1) {
1115 
1116  output.put(input.begin()->getX(), V);
1117 
1118  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1119 
1120  V += input.getDistance(i->getX(), j->getX()) * j->getY();
1121 
1122  output.put(j->getX(), V);
1123  }
1124  }
1125 
1126  return V;
1127  }
1128 
1129 
1130  /**
1131  * Conversion of data points to integral values.
1132  *
1133  * The integration is based on the trapezoidal rule applied to the input data points.
1134  *
1135  * \param input collection
1136  * \param output mappable collection
1137  * \param option true
1138  * \return integral
1139  */
1140  template<class JElement_t,
1141  template<class, class> class JCollection_t,
1142  class JResult_t,
1143  class JDistance_t>
1144  inline typename JElement_t::ordinate_type
1146  typename JMappable<JElement_t>::map_type& output,
1147  const JLANG::JBool<true>& option)
1148  {
1149  typedef typename JElement_t::ordinate_type ordinate_type;
1151 
1152  ordinate_type V(JMATH::zero);
1153 
1154  if (input.getSize() > 1) {
1155 
1156  output.put(input.begin()->getX(), V);
1157 
1158  for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1159 
1160  V += 0.5 * input.getDistance(i->getX(), j->getX()) * (i->getY() + j->getY());
1161 
1162  output.put(j->getX(), V);
1163  }
1164  }
1165 
1166  return V;
1167  }
1168 }
1169 
1170 #endif
*fatal Wrong number of arguments esac JCookie sh JRuns D $DETECTOR d sort n k
Definition: JRunrange.sh:16
container_type::reverse_iterator reverse_iterator
Definition: JCollection.hh:93
General exception.
Definition: JException.hh:23
JElement2D< JKey_t, JValue_t > element_type
Definition: JPolint.hh:984
data_type w[N+1][M+1]
Definition: JPolint.hh:708
Exceptions.
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:42
function_type::JExceptionHandler exceptionhandler_type
Definition: JPolint.hh:955
Numerical integrator for W(x) = 1.
Definition: JQuadrature.hh:111
function_type::argument_type argument_type
Definition: JPolint.hh:953
do $JPP JMEstimator M
Definition: JMEstimator.sh:37
JElement_t value_type
Definition: JCollection.hh:83
Exception for a functional operation.
Definition: JException.hh:126
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:943
Template class for distance evaluation.
Definition: JDistance.hh:24
Data structure for result including value and N derivatives of function.
Definition: JResult.hh:531
This include file containes various data structures that can be used as specific return types for the...
JPolintFunction1D()
Default contructor.
Definition: JPolint.hh:961
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:670
Template interface definition for associative collection of elements.
collection_type::const_iterator const_iterator
Definition: JPolint.hh:946
virtual result_type evaluate(const argument_type *pX) const
Recursive interpolation method implementation.
Definition: JPolint.hh:227
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
data_type r[M+1]
Definition: JPolint.hh:709
JPolintFunction_t::reverse_iterator reverse_iterator
Definition: JPolint.hh:999
Generation of compiler error.
Definition: JAssert.hh:17
fi JEventTimesliceWriter a
JPolintCollection< N, JElement_t, JCollection_t, JResultPolynome< M, typename JResultType< typename JElement_t::ordinate_type >::result_type >, JDistance_t > collection_type
Definition: JPolint.hh:738
size_t getSize(T(&array)[N])
Get size of c-array.
Definition: JLangToolkit.hh:32
JElement_t::abscissa_type abscissa_type
Definition: JCollection.hh:81
*fatal Wrong number of arguments esac if[!-d ${OUTPUT_DIR}]
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:1003
collection_type::ordinate_type ordinate_type
Definition: JPolint.hh:942
JPolintMap()
Default constructor.
Definition: JPolint.hh:1009
JPolintFunction_t::iterator iterator
Definition: JPolint.hh:998
JPolintFunction_t::value_type value_type
Definition: JPolint.hh:993
JPolintFunction_t::ordinate_type ordinate_type
Definition: JPolint.hh:992
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:997
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:989
Template definition of base class for polynomial interpolations with polynomial result.
Definition: JPolint.hh:555
container_type::iterator iterator
Definition: JCollection.hh:92
JPolintFunction_t::argument_type argument_type
Definition: JPolint.hh:1001
Data structure for result including value, first derivative and integrals of function.
Definition: JResult.hh:335
Template definition of function object interface.
Definition: JFunctional.hh:32
return result
Definition: JPolint.hh:695
virtual void do_compile()
Function compilation.
Definition: JPolint.hh:702
functional_type::result_type result_type
Definition: JFunctional.hh:324
virtual result_type evaluate(const argument_type *pX) const
Recursive interpolation method implementation.
Definition: JPolint.hh:97
container_type::const_reverse_iterator const_reverse_iterator
Definition: JCollection.hh:91
Auxiliary class to evaluate result type.
Definition: JFunctional.hh:377
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:947
Data structure for result including value and first derivative of function.
Definition: JResult.hh:41
JDistance_t distance_type
Definition: JCollection.hh:84
Exception handler for functional object.
Definition: JFunctional.hh:131
collection_type::abscissa_type abscissa_type
Definition: JPolint.hh:941
Template definition of function object interface in multidimensions.
Definition: JFunctional.hh:303
JCollection_t< JElement_t, JDistance_t > collection_type
Definition: JPolint.hh:939
collection_type::iterator iterator
Definition: JPolint.hh:948
virtual result_type evaluate(const argument_type *pX) const
Recursive interpolation method implementation.
Definition: JPolint.hh:312
Exception for numerical precision error.
Definition: JException.hh:252
functional_type::result_type result_type
Definition: JFunctional.hh:308
Auxiliary classes for numerical integration.
JElement_t::ordinate_type ordinate_type
Definition: JCollection.hh:82
JPolintCollection< N, JElement_t, JCollection_t, JResultPolynome< 1, typename JResultType< typename JElement_t::ordinate_type >::result_type >, JDistance_t > collection_type
Definition: JPolint.hh:806
alias put_queue eval echo n
Definition: qlib.csh:19
Functional map with polynomial interpolation.
Definition: JPolint.hh:975
2D Element.
Definition: JElement.hh:44
JPolintCollection< N, JElement_t, JCollection_t, JResultPolynome< 2, typename JResultType< typename JElement_t::ordinate_type >::result_type >, JDistance_t > collection_type
Definition: JPolint.hh:876
container_type::const_iterator const_iterator
Definition: JCollection.hh:90
JPolintFunction_t::result_type result_type
Definition: JPolint.hh:1002
int j
Definition: JPolint.hh:634
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:991
collection_type::reverse_iterator reverse_iterator
Definition: JPolint.hh:949
collection_type::distance_type distance_type
Definition: JPolint.hh:944
data_type v[N+1][M+1]
Definition: JPolint.hh:707
double u[N+1]
Definition: JPolint.hh:706
Data structure for result including value and first derivative of function.
Definition: JResult.hh:210
JFunction1D< abscissa_type, JResult_t > function_type
Definition: JPolint.hh:951
function_type::result_type result_type
Definition: JPolint.hh:954
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:37
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:936
JPolintFunction_t::distance_type distance_type
Definition: JPolint.hh:994
JPolintFunction_t::const_iterator const_iterator
Definition: JPolint.hh:996
Template class for polynomial interpolation in 1D.
Definition: JPolint.hh:933
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