【发布时间】:2021-04-30 14:36:02
【问题描述】:
我正在开发一个大型 C++ 框架来进行一些粒子物理计算,如果有一种方法可以对具有 N 个变量的通用函数进行 N 维积分,那就太好了。积分发生在 N 立方 [0:1]^N 上。
不幸的是,被积函数是一个成员函数,所以我将一个 std::bind 对象传递给一个模板函数(lambdas 也可以很好地工作)。当N > 1时,集成方法再次绑定固定一个变量的函数,并将新函数传递给N-1的方法。
我觉得这可以通过将N 作为模板参数递归地完成,但这就是我卡住的地方:有没有办法“模板化”绑定部分,以便添加适量的占位符与函数的数量或N?然后将新的绑定方法传递给integral<N-1>等。我认为问题在于std::bind对象没有签名。也许使用 lambdas 可以利用可变参数包扩展,但模板必须再次以某种方式解析签名。
有没有办法使这项工作?如果可能在 c++11 中并且没有 boost 库。
这是我现在没有递归的非常简化的版本,而是每个维度的积分方法的不同定义(现在最多 3 个)
#include <iostream>
#include <functional>
#include <cmath>
// integrate between [0:1]
template <typename Functor>
double integral1D(Functor fn, size_t steps = 100) {
double sum = 0.;
double step = 1./steps;
for (size_t s = 0; s < steps; ++s)
sum += fn(s * step);
return sum * step;
}
template <typename Functor>
double integral2D(Functor fn, size_t steps = 100) {
auto subfn = [&](double v) ->double {
auto subint = std::bind(fn, v, std::placeholders::_1);
return integral1D(subint, steps);
};
return integral1D(subfn, steps);
}
template <typename Functor>
double integral3D(Functor fn, size_t steps = 100) {
auto subfn = [&](double v) ->double {
auto subint = std::bind(fn, v, std::placeholders::_1, std::placeholders::_2);
return integral2D(subint, steps);
};
return integral1D(subfn, steps);
}
struct A
{
double gaus1(double x, double range) { // computes jacobian on [-range:range]
x = range * (2. * x - 1.);
return 2 * range * std::exp(- x*x);
}
double gaus2(double x, double y, double range) {
return gaus1(x, range) * gaus1(y, range);
}
double gaus3(double x, double y, double z, double range) {
return gaus2(x, y, range) * gaus1(z, range);
}
double gaus1_integrate(double range) {
auto func = std::bind(&A::gaus1, this, std::placeholders::_1, range);
return integral1D(func);
}
double gaus2_integrate(double range) {
auto func = std::bind(&A::gaus2, this, std::placeholders::_1,
std::placeholders::_2, range);
return integral2D(func);
}
double gaus3_integrate(double range) {
auto func = std::bind(&A::gaus3, this, std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3, range);
return integral3D(func);
}
};
int main() {
A a;
std::cout << "1D integral is " << a.gaus1_integrate(50) << "\n";
std::cout << "2D integral is " << a.gaus2_integrate(50) << "\n";
std::cout << "3D integral is " << a.gaus3_integrate(50) << "\n";
return 0;
}
上面的例子有效并给出了预期的结果。我知道不建议对具有更多维度的更复杂的函数进行黎曼和(或等价的),但很高兴看看上面描述的方法是否可以工作。
回答
根据 Guillaume 的有用建议,这里是 c++11 工作示例。需要进行少量修改。
#include <iostream>
#include <functional>
#include <cmath>
template<int n, typename Functor>
struct integralNDsubint {
double v;
const Functor &fn;
template<typename... Args>
double operator()(Args... rest) const {
return fn(v, rest...);
}
};
template <int n, typename Functor, typename std::enable_if<(n > 1), bool>::type = true>
double integralND(Functor fn, size_t steps = 100) {
auto subfn = [&](double v) -> double {
auto subint = integralNDsubint<n, Functor> {v, fn};
// following block can be used in c++14 without
// need of helper struct integralNDsubint
//auto subint = [v, &fn](auto... rest) {
//return fn(v, rest...);
//};
return integralND<n - 1>(subint, steps);
};
return integralND<1>(subfn, steps);
}
template <int n, typename Functor, typename std::enable_if<(n == 1), bool>::type = true>
double integralND(Functor fn, size_t steps = 100) {
double sum = 0.;
double step = 1./steps;
for (size_t s = 0; s < steps; ++s) {
sum += fn(s * step);
}
return sum * step;
}
struct A
{
double gaus1(double x, double range) {
x = range * (2. * x - 1.);
return 2 * range * std::exp(- x*x);
}
double gaus2(double x, double y, double range) {
return gaus1(x, range) * gaus1(y, range);
}
double gaus3(double x, double y, double z, double range) {
return gaus2(x, y, range) * gaus1(z, range);
}
double gaus1_integrate(double range) {
auto func = [=](double x) { return gaus1(x, range); };
return integralND<1>(func);
}
double gaus2_integrate(double range) {
auto func = [=](double x, double y) { return gaus2(x, y, range); } ;
return integralND<2>(func);
}
double gaus3_integrate(double range) {
auto func = [=](double x, double y, double z) { return gaus3(x, y, z, range); } ;
return integralND<3>(func);
}
};
int main() {
A a;
std::cout << "1D integral is " << a.gaus1_integrate(50) << "\n";
std::cout << "2D integral is " << a.gaus2_integrate(50) << "\n";
std::cout << "3D integral is " << a.gaus3_integrate(50) << "\n";
return 0;
}
【问题讨论】:
-
一般建议是使用 lambda(可能是可变参数)而不是
std::bind。 Lambda 通常是可优化的,但std::bind通常是有代价的。 -
@GuillaumeRacicot 是的,虽然我认为可变参数 lambda 在 c++11 中不可用
-
我刚刚注意到了这个标签。确实,它们不可用。您可以通过使用模板化
operator()实现结构来手动编写函数对象。最好的当然是使用 C++14,但是如果没有语法糖,可以考虑手动编写。
标签: c++ c++11 templates lambda stdbind