1 #ifndef __JTOOLS__JPOLINT__
2 #define __JTOOLS__JPOLINT__
26 namespace JPP {
using namespace JTOOLS; }
39 template<
unsigned int N,
41 template<
class,
class>
class JCollection_t,
54 template<
unsigned int N,
class JElement_t,
template<
class,
class>
class JCollection_t,
class 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>
75 typedef typename collection_type::iterator
iterator;
103 if (this->size() > 1
u) {
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)) {
110 std::ostringstream os;
112 os << __FILE__ <<
':' << __LINE__ <<
" abscissa out of range "
113 <<
STREAM(
"?") << x <<
" <> "
114 <<
STREAM(
"?") << this->begin() ->getX() <<
' '
115 <<
STREAM(
"?") << this->rbegin()->getX();
123 const int n = std::min((
int) (
N + 1), (
int) this->size());
125 for (
int i = n/2;
i != 0 && p != this->end(); --
i, ++p) {}
126 for (
int i = n ;
i != 0 && p != this->begin(); --
i, --p) {}
131 for (
int i = 0;
i !=
n; ++p, ++
i) {
137 if (fabs(
u[
i]) < fabs(
u[j])) {
147 for (
int m = 1; m !=
n; ++m) {
149 for (
int i = 0;
i != n-m; ++
i) {
151 const double ho =
u[
i ];
152 const double hp =
u[
i+m];
153 const double dx = ho - hp;
171 }
else if (this->size() == 1
u && this->
getDistance(x, this->begin()->getX()) <= distance_type::precision) {
177 std::ostringstream os;
179 os << __FILE__ <<
':' << __LINE__ <<
" not enough data " <<
STREAM(
"?") <<
x;
194 mutable double u[
N+1];
204 template<
class JElement_t,
template<
class,
class>
class JCollection_t,
class 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>
225 typedef typename collection_type::iterator
iterator;
253 if (this->size() > 1
u) {
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)) {
260 std::ostringstream os;
262 os << __FILE__ <<
':' << __LINE__ <<
" abscissa out of range "
263 <<
STREAM(
"?") << x <<
" <> "
264 <<
STREAM(
"?") << this->begin() ->getX() <<
' '
265 <<
STREAM(
"?") << this->rbegin()->getX();
280 }
else if (this->size() == 1
u && this->
getDistance(x, this->begin()->getX()) <= distance_type::precision) {
286 std::ostringstream os;
288 os << __FILE__ <<
':' << __LINE__ <<
" not enough data " <<
STREAM(
"?") <<
x;
307 template<
class JElement_t,
template<
class,
class>
class JCollection_t,
class 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>
328 typedef typename collection_type::iterator
iterator;
356 if (this->size() > 1
u) {
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)) {
363 std::ostringstream os;
365 os << __FILE__ <<
':' << __LINE__ <<
" abscissa out of range "
366 <<
STREAM(
"?") << x <<
" <> "
367 <<
STREAM(
"?") << this->begin() ->getX() <<
' '
368 <<
STREAM(
"?") << this->rbegin()->getX();
378 const double dx = this->
getDistance(p->getX(), q->getX());
380 const double b = 1.0 -
a;
392 }
else if (this->size() == 1
u && this->
getDistance(x, this->begin()->getX()) <= distance_type::precision) {
398 std::ostringstream os;
400 os << __FILE__ <<
':' << __LINE__ <<
" not enough data " <<
STREAM(
"?") <<
x;
430 template<
unsigned int N,
class JElement_t,
template<
class,
class>
class JCollection_t,
class 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> >
451 typedef typename collection_type::iterator
iterator;
481 if (this->size() <= 1
u) {
483 std::ostringstream os;
485 os << __FILE__ <<
':' << __LINE__ <<
" not enough data " <<
STREAM(
"?") <<
x;
492 if (p == this->begin() && this->
getDistance(x, (p++)->getX()) > distance_type::precision) {
496 std::ostringstream os;
498 os << __FILE__ <<
':' << __LINE__ <<
" abscissa out of range " <<
STREAM(
"?") << x <<
" < " <<
STREAM(
"?") << this->begin() ->getX();
505 result.V = this->rbegin()->getIntegral();
513 }
else if (p == this->end() && this->
getDistance((--p)->getX(), x) > distance_type::precision) {
517 std::ostringstream os;
519 os << __FILE__ <<
':' << __LINE__ <<
" abscissa out of range " <<
STREAM(
"?") << x <<
" > " <<
STREAM(
"?") << this->rbegin()->getX();
525 result.v = this->rbegin()->getIntegral();
526 result.V = this->rbegin()->getIntegral();
538 const int n = std::min((
int) (
N + 1), (
int) this->size());
540 for (
int i = n/2;
i != 0 && p != this->end(); --
i, ++p) {}
541 for (
int i = n ;
i != 0 && p != this->begin(); --
i, --p) {}
546 for (
int i = 0;
i !=
n; ++p, ++
i) {
552 w[
i][2] =
v[
i][2] = p->getIntegral();
554 if (fabs(
u[
i]) < fabs(
u[j])) {
563 result.V = this->rbegin()->getIntegral();
567 for (
int m = 1; m !=
n; ++m) {
569 for (
int i = 0;
i != n-m; ++
i) {
571 const double ho =
u[
i ];
572 const double hp =
u[
i+m];
573 const double dx = ho - hp;
575 for (
int k = 0;
k != 3; ++
k) {
581 v[
i][1] = ho * r[1] - r[0];
582 w[
i][1] = hp * r[1] - r[0];
587 if (2*(j+1) < n - m) {
619 this->begin()->setIntegral(V);
621 for (
iterator j = this->begin(),
i =
j++;
j != this->end(); ++
i, ++
j) {
640 mutable double u[
N+1];
652 template<
unsigned int N,
654 template<
class,
class>
class JCollection_t,
665 template<
unsigned int N,
class JElement_t,
template<
class,
class>
class JCollection_t,
class JDistance_t,
unsigned int M>
671 public JCollection_t<JElement_t, JDistance_t>,
677 typedef JCollection_t<JElement_t, JDistance_t> collection_type;
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;
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;
689 typedef typename JResultType<ordinate_type>::result_type data_type;
690 typedef JFunction<abscissa_type, JResultPolynome<M, data_type> > function_type;
692 typedef typename function_type::argument_type argument_type;
693 typedef typename function_type::result_type result_type;
695 using JFunctional<>::compile;
711 result_type evaluate(const argument_type* pX) const
713 const argument_type x = *pX;
715 if (this->size() <= N) {
717 std::ostringstream os;
719 os << __FILE__ << ':' << __LINE__ << " not enough data " << STREAM("?") << x;
721 throw JFunctionalException(os.str());
724 const_iterator p = this->lower_bound(x);
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)) {
729 std::ostringstream os;
731 os << __FILE__ << ':' << __LINE__ << " abscissa out of range "
732 << STREAM("?") << x << " <> "
733 << STREAM("?") << this->begin() ->getX() << ' '
734 << STREAM("?") << this->rbegin()->getX();
736 throw JValueOutOfRange(os.str());
742 const int n = std::min((int) (N + 1), (int) this->size());
744 for (int i = n/2; i != 0 && p != this->end(); --i, ++p) {}
745 for (int i = n ; i != 0 && p != this->begin(); --i, --p) {}
750 for (int i = 0; i != n; ++p, ++i) {
752 u[i] = this->getDistance(x, p->getX());
754 w[i][0] = v[i][0] = JFunctional<argument_type, data_type>::getValue(p->getY(), pX);
756 for (unsigned int k = 1; k != M+1; ++k) {
757 w[i][k] = v[i][k] = JMATH::zero;
760 if (fabs(u[i]) < fabs(u[j])) {
766 for (unsigned int k = 0; k != M+1; ++k) {
767 result.y[k] = v[j][k];
772 for (int m = 1; m != n; ++m) {
774 for (int i = 0; i != n-m; ++i) {
776 const double ho = u[ i ];
777 const double hp = u[i+m];
778 const double dx = ho - hp;
780 for (int k = 0; k != M+1; ++k) {
781 r[k] = (v[i+1][k] - w[i][k]) / dx;
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];
793 if (2*(j+1) < n - m) {
795 for (int k = 0; k != M+1; ++k) {
796 result.y[k] += v[j+1][k];
801 for (int k = 0; k != M+1; ++k) {
802 result.y[k] += w[j][k];
816 virtual void do_compile() override
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];
826 mutable result_type result;
833 template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t, unsigned int M>
834 class JPolintFunction<N,
837 JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
839 public JPolintCollection<N,
842 JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
844 public virtual JFunction<typename JElement_t::abscissa_type,
845 JResultPolynome<N, typename JResultType<typename JElement_t::ordinate_type>::result_type> >
849 typedef JPolintCollection<N,
852 JResultPolynome<M, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
853 JDistance_t> collection_type;
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;
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;
865 typedef typename JResultType<ordinate_type>::result_type data_type;
866 typedef JFunction<abscissa_type, JResultPolynome<M, data_type> > function_type;
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;
886 virtual result_type evaluate(const argument_type* pX) const override
889 return collection_type::evaluate(pX);
891 catch(const JException& error) {
892 return this->getExceptionHandler().action(error);
901 template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
902 class JPolintFunction<N,
905 JResultDerivative<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
907 public JPolintCollection<N,
910 JResultPolynome<1, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
912 public virtual JFunction<typename JElement_t::abscissa_type,
913 JResultDerivative<typename JResultType<typename JElement_t::ordinate_type>::result_type> >
917 typedef JPolintCollection<N,
920 JResultPolynome<1, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
921 JDistance_t> collection_type;
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;
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;
933 typedef typename JResultType<ordinate_type>::result_type data_type;
934 typedef JFunction<abscissa_type, JResultDerivative<data_type> > function_type;
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;
940 using JFunctional<>::compile;
956 virtual result_type evaluate(const argument_type* pX) const override
959 return collection_type::evaluate(pX);
961 catch(const JException& error) {
962 return this->getExceptionHandler().action(error);
971 template<unsigned int N, class JElement_t, template<class, class> class JCollection_t, class JDistance_t>
972 class JPolintFunction<N,
975 JResultHesse<typename JResultType<typename JElement_t::ordinate_type>::result_type>,
977 public JPolintCollection<N,
980 JResultPolynome<2, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
982 public virtual JFunction<typename JElement_t::abscissa_type,
983 JResultHesse<typename JResultType<typename JElement_t::ordinate_type>::result_type> >
987 typedef JPolintCollection<N,
990 JResultPolynome<2, typename JResultType<typename JElement_t::ordinate_type>::result_type>,
991 JDistance_t> collection_type;
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;
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;
1003 typedef typename JResultType<ordinate_type>::result_type data_type;
1004 typedef JFunction<abscissa_type, JResultHesse<data_type> > function_type;
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;
1010 using JFunctional<>::compile;
1026 virtual result_type evaluate(const argument_type* pX) const override
1029 return collection_type::evaluate(pX);
1031 catch(const JException& error) {
1032 return this->getExceptionHandler().action(error);
1043 template<unsigned int N,
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>
1054 typedef JCollection_t<JElement_t, JDistance_t> collection_type;
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;
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;
1066 typedef JFunction1D<abscissa_type, JResult_t> function_type;
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;
1086 template<class JAbscissa_t, class JOrdinate_t>
1089 template<template<class, class, class> class JMap_t>
1090 struct JMapCollection;
1096 template<unsigned int N,
1099 template<class, class, class> class JMap_t,
1101 class JDistance_t = JDistance<JKey_t> >
1103 public JPolintFunction<N,
1104 JElement2D<JKey_t, JValue_t>,
1105 JMapCollection<JMap_t>::template collection_type,
1111 typedef JElement2D<JKey_t, JValue_t> element_type;
1112 typedef JPolintFunction<N,
1114 JMapCollection<JMap_t>::template collection_type,
1116 JDistance_t> JPolintFunction_t;
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;
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;
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;
1150 template<unsigned int N,
1152 template<class, class> class JCollection_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)
1159 return integrate(input, output, JLANG::JBool<N == 0 || N == 1>());
1174 template<unsigned int N,
1176 template<class, class> class JCollection_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)
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;
1188 ordinate_type V(JMATH::zero);
1190 if (input.getSize() > 1) {
1192 output.put(input.begin()->getX(), V);
1194 const JGaussLegendre engine(N);
1196 for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1198 const abscissa_type xmin = i->getX();
1199 const abscissa_type xmax = j->getX();
1201 for (JGaussLegendre::const_iterator m = engine.begin(); m != engine.end(); ++m) {
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));
1209 output.put(xmax, V);
1227 template<class JElement_t,
1228 template<class, class> class JCollection_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)
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;
1239 ordinate_type V(JMATH::zero);
1241 if (input.getSize() > 1) {
1243 output.put(input.begin()->getX(), V);
1245 for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1247 V += input.getDistance(i->getX(), j->getX()) * j->getY();
1249 output.put(j->getX(), V);
1267 template<class JElement_t,
1268 template<class, class> class JCollection_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)
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;
1279 ordinate_type V(JMATH::zero);
1281 if (input.getSize() > 1) {
1283 output.put(input.begin()->getX(), V);
1285 for (const_iterator j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
1287 V += 0.5 * input.getDistance(i->getX(), j->getX()) * (i->getY() + j->getY());
1289 output.put(j->getX(),
V);
then fatal No hydrophone data file $HYDROPHONE_TXT fi sort gr k
double getValue(const JScale_t scale)
Get numerical value corresponding to scale.
Exception for a functional operation.
The elements in a collection are sorted according to their abscissa values and a given distance opera...
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.
Generation of compiler error.
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
size_t getSize(T(&array)[N])
Get size of c-array.
double getDistance(const JFirst_t &first, const JSecond_t &second)
Get distance between objects.
Auxiliary data structure for handling std::ostream.
Exception for numerical precision error.
Auxiliary classes for numerical integration.
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Exception for accessing a value in a collection that is outside of its range.