我在c++中定义了两个函数,我使用Rcpp包在R中调用这两个函数。我将f1和f2函数放在一个cppFunction((中。运行后,f1函数在R中正常工作,但我在调用f2时得到了这个错误:";找不到函数f2";。这是代码:
cppFunction('
std::vector<double> f1( double lambda, double gamma) {
double a = exp(-(lambda+gamma));
double b = lambda+gamma;
std::vector<double> v;
v.push_back(a);
v.push_back(b);
return v;
}
std::vector<double> f2(double lambda, double gamma ,double alpha) {
std::vector<double> res;
res=f1(lambda, gamma);
res[0] *= alpha;
res[1] *= 1-alpha;
double lik = res[0]+res[1];
res.push_back(lik);
res.push_back(res[1]);
return res;
}
')
你对如何解决这个问题有什么建议吗?
您可以在cppFunction
:中使用includes
cppFunction('
std::vector<double> f2(double lambda, double gamma ,double alpha) {
std::vector<double> res;
res=f1(lambda, gamma);
res[0] *= alpha;
res[1] *= 1-alpha;
double lik = res[0]+res[1];
res.push_back(lik);
res.push_back(res[1]);
return res;
}
', includes = {'std::vector<double> f1( double lambda, double gamma) {
double a = exp(-(lambda+gamma));
double b = lambda+gamma;
std::vector<double> v;
v.push_back(a);
v.push_back(b);
return v;
};
'})
来源:感谢Federico Manigrasso的回答