我必须在Rcpp中计算这个两倍:
。在此,“ t”和“ m”是变量。参数“ beta”,“ c”和“ p”。为此,我编写了以下代码。第一个代码是我要集成的函子。第二个代码用于集成。第三个代码用于计算两倍总和。我想知道我是否做对了。任何帮助将不胜感激:
// [[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;
}
我做对了吗?