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