我正在寻找用于二维数值积分的确定性线程安全 Rcpp 算法。RcppNumerical 为 Cubase 提供了一个用于多维集成的部分接口,但从我的试验来看,它在 RcppParallel 中似乎不是线程安全的,它可能使用了 Monte Carlo 方法。这让我回到了重复的一维积分。我已经成功地将它与(不是线程安全的)R 函数 Rdqags 一起使用,但是我对 RcppNumerical 的(可能是幼稚的)编码无法编译,因为嵌套类是抽象的。也许是由于 operator() 虚函数。
任何人都可以在 RcppNumerical 或其他替代方案中提出解决此问题的方法吗?
下面是我模拟来自https://github.com/yixuan/RcppNumerical的二维示例的测试代码。它给出了类似的错误
无法将变量“f2”声明为抽象类型“Normal2”
无法将变量“f1”声明为抽象类型“Normal1”
默里
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppNumerical)]]
#include <RcppNumerical.h>
using namespace Numer;
// P(a1 < X1 < b1, a2 < X2 < b2), (X1, X2) ~ N([0], [1 rho])
// ([0], [rho 1])
class Normal2: public Func
{
private:
const double rho;
const double x;
double const1; // 2 * (1 - rho^2)
double const2; // 1 / (2 * PI) / sqrt(1 - rho^2)
public:
Normal2(const double& rho_, const double& x_) : rho(rho_), x(x_)
{
const1 = 2.0 * (1.0 - rho * rho);
const2 = 1.0 / (2 * M_PI) / std::sqrt(1.0 - rho * rho);
}
// PDF of bivariate normal
double operator()(const double& y)
{
double z = x * x - 2 * rho * x * y + y * y;
return const2 * std::exp(-z / const1);
}
};
class Normal1: public Func
{
private:
const double rho;
double a2, b2;
public:
Normal1(const double& rho_, const double& a2_, const double& b2_) : rho(rho_), a2(a2_), b2(b2_) {}
// integral in y dimension for given x
double operator()(const double& x)
{
Normal2 f2(rho, x);
double err_est;
int err_code;
const double res = integrate(f2, a2, b2, err_est, err_code);
return res;
}
};
// [[Rcpp::export]]
Rcpp::List integrate_test3()
{
double a1 = -1.0;
double b1 = 1.0;
double a2 = -1.0;
double b2 = 1.0;
Normal1 f1(0.5, a2, b2); // rho = 0.5
double err_est;
int err_code;
const double res = integrate(f1, a1, b1, err_est, err_code);
return Rcpp::List::create(
Rcpp::Named("approximate") = res,
Rcpp::Named("error_estimate") = err_est,
Rcpp::Named("error_code") = err_code
);
}