Im triyng使用Boost Library求积执行数值积分。我用类似的梯形
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);
}
这很好,但因为我们有一个接近log(0(的不确定性,这需要很多时间,所以我寻找了一个替代方案。在Boost中,他们有一些针对这种情况的集成方法,所以我决定尝试tanh_sinh方法。我按照给定的例子这样实现它。
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);
}
问题是,当我编译时,它说error: ‘integrator’ is not captured
后面跟着note: the lambda has no capture-default
,这是一个巨大的错误。
我做错了什么?为什么不起作用?
问题正是它所说的。您的lambda没有捕获integrator
。将[x,y,a,b]
更改为[x,y,a,b,integrator]
以通过值捕获,或将[x,y,a,b,&integrator]
更改为通过引用捕获。
或者,您可以使用[=]
按值捕获所有内容,或者使用[&]
按引用捕获所有内容。
有关语法,请参见lambda表达式。