2#include "../../Matrix.h"
3#include "../../sorting.h"
61 const std::function<
double(
double)>& fun,
66 std::vector<double>& nodes,
71 std::cerr <<
"H too small! h = " << h << std::endl;
76 if(values.
rows() == 0) {
77 values = x.
Apply(fun);
78 }
else if(values.
rows() < 3) {
79 values = { { values(0, 0) }, { fun(x(1, 0)) }, { values(1, 0) } };
82 auto qSimpson = (h / 6.0) * (values(2, 0) + 4.0 * values(1, 0) + values(0, 0));
83 auto qTrapez = (h / 2.0) * (values(2, 0) + values(0, 0));
85 auto error = fabs(qSimpson - qTrapez);
87 nodes.push_back(x(1, 0));
89 if(error <= tol) {
return { qSimpson, nodes }; }
91 auto hTol = tol / 2.0;
92 auto leftValues = values.
GetSlice(0, 1, 0, 0);
94 auto rightValues = values.
GetSlice(1, 2, 0, 0);
95 auto resRight =
quad_adaptive_rec(fun, x(1, 0), b, hTol, hMin, nodes, rightValues);
97 return { resLeft.first + resRight.first, nodes };
118const std::function<
double(
double)>& fun,
123 std::vector<double> nodes = { lower, upper };
126 res.second =
sort(res.second);
std::pair< double, std::vector< double > > quadrature(const std::function< double(double)> &fun, const double &lower, const double &upper, const double &tol, const double &hMin)
Definition: Integration.h:117
size_t rows() const
Definition: Matrix.h:193
Matrix GetSlice(size_t rowStart) const
Definition: Matrix.h:609
Matrix< T > Apply(const std::function< T(T)> &fun) const
Definition: Matrix.h:375
Definition: Integration.h:7
std::pair< double, std::vector< double > > quad_adaptive_rec(const std::function< double(double)> &fun, const double &a, const double &b, const double &tol, const double &hMin, std::vector< double > &nodes, Matrix< double > &values)
Definition: Integration.h:60
std::vector< T > sort(const std::vector< T > &in)
Definition: sorting.h:33