Im triyng to perform a numerical integral using Boost Library quadrature. I did it using trapezoidal like this
double integrand(double xp, double yp, double x, double y, double a, double b) {
double eps;
if(x - xp == 0 && y - yp == 0)
eps = 1e-6;
else
eps = 0.0;
return std::log(std::hypot(x - xp + eps, y - yp + eps)) * w(xp, yp);
}
double integrate(double x, double y, double a, double b) {
auto inner_integral = [m, n, a, b](double yp) {
auto f = [z, m, n, a, b](double xp) { return integrand(xp, yp, x, y, a, b); };
return boost::math::quadrature::trapezoidal(f, 0.0, a, 1e-8);
};
return boost::math::quadrature::trapezoidal(inner_integral, 0.0, b, 1e-8);
}
This works fine but because we have an indetermination close to log(0) it takes a lot of time, so I searched for an alternative. In Boost, they have some integration methods for this kind of situation, so I decided to try tanh_sinh method. I implemented it like this following the given example.
double Integrate(double x, double y, double a, double b) {
boost::math::quadrature::tanh_sinh<double> integrator;
auto inner_integral = [x, y, a, b](double yp) {
auto f = [yp, x, y, a, b](double xp) { return integrand(xp, yp, x, y, a, b); };
return integrator.integrate(f, 0.0, a);
};
return integrator.integrate(inner_integral, 0.0, b);
}
The problem is that when I compile it says error: ‘integrator’ is not captured
followed with note: the lambda has no capture-default
and a huge error.
What am I doing wrong? Why doesn't work?
The problem is exactly what it says. Your lambda isn't capturing integrator
. Change [x,y,a,b]
to [x,y,a,b,integrator]
to capture by value or [x,y,a,b,&integrator]
to capture by reference.
Alternatively you can capture everything by value with [=]
or everything by reference with [&]
.
See lambda expressions for syntax.