integration.h
Go to the documentation of this file.
#ifndef INTEGRATION_H
#define INTEGRATION_H
#include <limits>
#include <itpp/base/math/elem_math.h>
#include <itpp/base/help_functions.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/specmat.h>
#include <itpp/itexports.h>
namespace itpp
{
namespace details
{
template<typename Ftn>
double quadstep(Ftn f, double a, double b,
double fa, double fm, double fb, double is)
{
double Q, m, h, fml, fmr, i1, i2;
m = (a + b) / 2;
h = (b - a) / 4;
fml = f(a + h);
fmr = f(b - h);
i1 = h / 1.5 * (fa + 4 * fm + fb);
i2 = h / 3 * (fa + 4 * (fml + fmr) + 2 * fm + fb);
i1 = (16 * i2 - i1) / 15;
if((is + (i1 - i2) == is) || (m <= a) || (b <= m)) {
if((m <= a) || (b <= m)) {
it_warning("Interval contains no more machine number. Required tolerance may not be met");
}
Q = i1;
return Q;
}
else {
Q = quadstep(f, a, m, fa, fml, fm, is) + quadstep(f, m, b, fm, fmr, fb, is);
}
return Q;
}
template<typename Ftn>
double quadlstep(Ftn f, double a, double b,
double fa, double fb, double is)
{
static const double alpha = std::sqrt(2.0 / 3);
static const double beta = 1.0 / std::sqrt(5.0);
double Q, h, m, mll, ml, mr, mrr, fmll, fml, fm, fmr, fmrr,
i1, i2;
h = (b - a) / 2;
m = (a + b) / 2;
mll = m - alpha * h;
ml = m - beta * h;
mr = m + beta * h;
mrr = m + alpha * h;
fmll = f(mll);
fml = f(ml);
fm = f(m);
fmr = f(mr);
fmrr = f(mrr);
i2 = (h / 6) * (fa + fb + 5 * (fml + fmr));
i1 = (h / 1470) * (77 * (fa + fb) + 432 * (fmll + fmrr) + 625 * (fml + fmr) + 672 * fm);
if((is + (i1 - i2) == is) || (mll <= a) || (b <= mrr)) {
if((m <= a) || (b <= m)) {
it_warning("Interval contains no more machine number. Required tolerance may not be met");
}
Q = i1;
return Q;
}
else {
Q = quadlstep(f, a, mll, fa, fmll, is) + quadlstep(f, mll, ml, fmll, fml, is) + quadlstep(f, ml, m, fml, fm, is) +
quadlstep(f, m, mr, fm, fmr, is) + quadlstep(f, mr, mrr, fmr, fmrr, is) + quadlstep(f, mrr, b, fmrr, fb, is);
}
return Q;
}
}
template <typename Ftn>
double quad(Ftn f, double a, double b,
double tol = std::numeric_limits<double>::epsilon())
{
vec x(3), y(3), yy(5);
double Q, fa, fm, fb, is;
x = vec_3(a, (a + b) / 2, b);
y = apply_functor<double, Ftn>(f, x);
fa = y(0);
fm = y(1);
fb = y(2);
yy = apply_functor<double, Ftn>(f, a + vec(".9501 .2311 .6068 .4860 .8913")
* (b - a));
is = (b - a) / 8 * (sum(y) + sum(yy));
if(is == 0.0)
is = b - a;
is = is * tol / std::numeric_limits<double>::epsilon();
Q = details::quadstep(f, a, b, fa, fm, fb, is);
return Q;
}
ITPP_EXPORT double quad(double(*f)(double), double a, double b,
double tol = std::numeric_limits<double>::epsilon());
template<typename Ftn>
double quadl(Ftn f, double a, double b,
double tol = std::numeric_limits<double>::epsilon())
{
static const double alpha = std::sqrt(2.0 / 3);
static const double beta = 1.0 / std::sqrt(5.0);
double Q, m, h, x1, x2, x3, fa, fb, i1, i2, is, s, erri1, erri2, R;
vec x(13), y(13);
double tol2 = tol;
m = (a + b) / 2;
h = (b - a) / 2;
x1 = .942882415695480;
x2 = .641853342345781;
x3 = .236383199662150;
x(0) = a;
x(1) = m - x1 * h;
x(2) = m - alpha * h;
x(3) = m - x2 * h;
x(4) = m - beta * h;
x(5) = m - x3 * h;
x(6) = m;
x(7) = m + x3 * h;
x(8) = m + beta * h;
x(9) = m + x2 * h;
x(10) = m + alpha * h;
x(11) = m + x1 * h;
x(12) = b;
y = apply_functor<double, Ftn>(f, x);
fa = y(0);
fb = y(12);
i2 = (h / 6) * (y(0) + y(12) + 5 * (y(4) + y(8)));
i1 = (h / 1470) * (77 * (y(0) + y(12)) + 432 * (y(2) + y(10)) + 625 * (y(4) + y(8)) + 672 * y(6));
is = h * (.0158271919734802 * (y(0) + y(12)) + .0942738402188500 * (y(1) + y(11)) + .155071987336585 * (y(2) + y(10)) +
.188821573960182 * (y(3) + y(9)) + .199773405226859 * (y(4) + y(8)) + .224926465333340 * (y(5) + y(7)) + .242611071901408 * y(6));
s = sign(is);
if(s == 0.0)
s = 1;
erri1 = std::abs(i1 - is);
erri2 = std::abs(i2 - is);
R = 1;
if(erri2 != 0.0)
R = erri1 / erri2;
if(R > 0 && R < 1)
tol2 = tol2 / R;
is = s * std::abs(is) * tol2 / std::numeric_limits<double>::epsilon();
if(is == 0.0)
is = b - a;
Q = details::quadlstep(f, a, b, fa, fb, is);
return Q;
}
ITPP_EXPORT double quadl(double(*f)(double), double a, double b,
double tol = std::numeric_limits<double>::epsilon());
} // namespace itpp
#endif // #ifndef INTEGRATION_H
itpp::Array
General array class.
Definition array.h:105
elem_math.h
Elementary mathematical functions - header file.
it_warning
#define it_warning(s)
Display a warning message.
Definition itassert.h:173
itpp::sum
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition matfunc.h:59
itpp::sign
double sign(double x)
Signum function.
Definition elem_math.h:81
help_functions.h
Help functions to make functions with vec and mat as arguments.
itpp::details::quadlstep
double quadlstep(Ftn f, double a, double b, double fa, double fb, double is)
Adaptive Lobatto quadrature integration recursion step.
Definition integration.h:74
itpp::details::quadstep
double quadstep(Ftn f, double a, double b, double fa, double fm, double fb, double is)
Simpson quadrature integration recursion step.
Definition integration.h:45
matfunc.h
Various functions on vectors and matrices - header file.
itpp
itpp namespace
Definition itmex.h:37
itpp::quadl
double quadl(Ftn f, double a, double b, double tol=std::numeric_limits< double >::epsilon())
Definition integration.h:264
itpp::quad
double quad(Ftn f, double a, double b, double tol=std::numeric_limits< double >::epsilon())
Definition integration.h:162
specmat.h
Definitions of special vectors and matrices.

Generated on Mon Jun 10 2024 11:49:18 for IT++ by Doxygen 1.9.8