ultimatepp/bazaar/STEM4U/Integral.cpp
koldo 66957ff519 STEM4U: Added numerical integration and DAE solving
git-svn-id: svn://ultimatepp.org/upp/trunk@14562 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2020-06-07 10:18:34 +00:00

41 lines
910 B
C++

#include <Core/Core.h>
#include <Functions4U/Functions4U.h>
#include <plugin/Eigen/Eigen.h>
#include "Integral.h"
namespace Upp {
using namespace Eigen;
double Integral(const VectorXd &y, const VectorXd &x) {
ASSERT(x.size() == y.size());
double ret = 0;
Eigen::Index n = x.size();
for (int i = 1; i < n; ++i)
ret += Avg(y(i), y(i-1))*(x(i) - x(i-1));
return ret;
}
double Integral(VectorXd &y, double dx, IntegralType type) {
Eigen::Index n = y.size();
if (n <= 1)
return 0;
else if (n == 2)
return Avg(y(0), y(1))*dx;
else if (type == TRAPEZOIDAL)
return (y.segment(1, n-2).sum() + y(0)/2 + y(n-1)/2)*dx;
else {
double ret = 0;
if ((n-1)%2) {
ret = Avg(y(n-1), y(n-2))*dx;
--n;
}
return ret + dx/3*(y(0) + 2*(Map<VectorXd, 0, InnerStride<2>>(y.data()+1, n/2).sum() +
y.block(1, 0, n-2, 1).sum()) + y(n-1));
}
}
}