C++ Rcpp中的双和
我必须在Rcpp中计算此双倍总和: 。这里,“t”和“m”是变量。“beta”、“c”和“p”是参数。为此我编写了以下代码。第一个代码是我要积分的函子。第二个代码用于集成。第三个代码用于计算双和。我想知道我做得是否正确。任何帮助都将不胜感激:C++ Rcpp中的双和,c++,r,rcpp,C++,R,Rcpp,我必须在Rcpp中计算此双倍总和: 。这里,“t”和“m”是变量。“beta”、“c”和“p”是参数。为此我编写了以下代码。第一个代码是我要积分的函子。第二个代码用于集成。第三个代码用于计算双和。我想知道我做得是否正确。任何帮助都将不胜感激: // [[Rcpp::depends(RcppEigen)]] // [[Rcpp::depends(RcppNumerical)]] #include <RcppNumerical.h> using namespace Numer; u
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::depends(RcppNumerical)]]
#include <RcppNumerical.h>
using namespace Numer;
using namespace Rcpp;
using namespace std;
// this is the functor we are going to integrate
class hnr: public MFunc
{
private:
double beta;
double p;
double c;
public:
hnr(double beta_, double p_, double c_): beta(beta_), p(p_), c(c_) {};
double operator()(Constvec& x)
{
return R::dexp(x[0],1/beta,0)) * (1/(pow(x[1]+c,p))) ;
}
};
// [[Rcpp::export]]
double integrate_test3( double beta, double p, double c, Eigen::VectorXd lower, Eigen::VectorXd upper)
{
// this is the function to integrate the above function.
// the first observation of "lower" and "upper" vector
// is for m; the second ones are for t
hnr f( beta, p, c);
double err_est;
int err_code;
int maxeval = 5000;
double res = integrate ( f, lower, upper,err_est,err_code,maxeval);
return res;
}
// This is the final double sum
// [[Rcpp::export]]
double partB2(std::vector<double> t, std::vector<double> m, std::vector<double> theta){
// extracting the parameter estimates
double beta=theta[0];
double c=theta[1];
double p=theta[2];
int n= ts.size();
std::vector<double> temp2(n);
for(int i=0; i <n; i++){
std::vector<double> newts();
newts.insert(newts.end(),t.begin(),t.begin()+i);
std::vector<double> newms();
newms.insert(newms.end(),m.begin(),m.begin()+i );
int l= newts.size();
double temp2in=0;
for(int j=0; j <l; j++){
Eigen::VectorXd lower(2);
lower << 0, (t[i]-newts[j]);
Eigen::VectorXd upper(2);
upper << 9, (t[i+1]-newts[j]);
temp2in += integrate_test3( beta, p, c, lower, upper);
}
temp2[i]= temp2in;
}
double partC = std::accumulate(temp2.begin(), temp2.end(), 0.0);
return partC;
}
/[[Rcpp::depends(RcppEigen)]]
//[[Rcpp::dependens(rcppnumeric)]]
#包括
使用名称空间编号;
使用名称空间Rcpp;
使用名称空间std;
//这是我们要积分的函子
hnr类:公共MFunc
{
私人:
双β;
双p;
双c;
公众:
hnr(双β,双p,双c):β(β),p(p),c(c){};
双运算符()
{
返回R::dexp(x[0],1/beta,0))*(1/(pow(x[1]+c,p));
}
};
//[[Rcpp::导出]]
双积分测试3(双β,双p,双c,本征::向量xd下限,本征::向量xd上限)
{
//这是集成上述功能的功能。
//“下”和“上”向量的首次观测
//代表m;第二个代表t
hnrf(β,p,c);
双重错误;
内部错误代码;
int maxeval=5000;
双精度=积分(f、下限、上限、错误码、错误码、最大值);
返回res;
}
//这是最后的双倍和
//[[Rcpp::导出]]
双部分B2(标准::向量t,标准::向量m,标准::向量θ){
//参数估计的提取
双β=θ[0];
双c=θ[1];
双p=θ[2];
int n=ts.size();
std::向量temp2(n);
对于(int i=0;i“我做得对吗?”您的测试用例告诉您什么?@πάνταῥεῖ 因为我是Rcpp新手,我用我的r代码测试了它,他们给了我不同的答案。为什么你不在处理和之前先解积分呢?在我看来,dt
和dm
积分都可以很容易地以封闭形式解算。