使用 Rcpp 从 C++ 中调用 R 的 optim 函数
Calling R's optim function from within C++ using Rcpp
我是 Rcpp 的新手,在我的代码中,我必须从 C++ 调用 R 函数 "optim"。参考了很多例子,还是有错误:"static assertion failed: cannot convert type to SEXP"。这是我的代码,问题是最后一个函数:
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace RcppArmadillo;
using namespace arma;
using namespace std;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
double fr(arma::colvec x){
double result = 100 * (x(2) - x(1) * x(1)) * (x(2) - x(1) * x(1)) + (1 - x(1)) * (1 - x(1));
return result;
}
typedef double (*funcPtr)(arma::colvec x);
// [[Rcpp::export]]
XPtr<funcPtr> putFunPtrInXPtr(){
return(XPtr<funcPtr>(new funcPtr(&fr)));
}
// [[Rcpp::export]]
arma::colvec callOptimFun(SEXP x) {
RNGScope scope;
Rcpp::Environment stats("package:stats");
Rcpp::Function optim = stats["optim"];
XPtr<funcPtr> xpfun = putFunPtrInXPtr();
funcPtr fun = *xpfun;
Rcpp::List y = optim(x, fun);
arma::colvec r = y["par"];
return r;
}
不幸的是,我为我的最后一个功能尝试了很多方法,并且所有方法都出现相同的错误。这些是我的尝试:
1.
// [[Rcpp::export]]
Rcpp::List callOptimFun(arma::colvec x) {
\....
Rcpp::List y = optim(x, fun);
return y;
}
2.
// [[Rcpp::export]]
Rcpp::List callOptimFun(arma::colvec x) {
\....
Rcpp::List y = optim(x, fun);
return y;
}
3.
// [[Rcpp::export]]
Rcpp::List callOptimFun(SEXP x) {
\....
Rcpp::List y = optim(x, fun);
return y;
}
我不熟悉C++。可能是什么问题?谢谢!
在这种情况下,函数指针的使用是有问题的。 Rcpp 有一种特殊类型的包装器,它要求 C++ 函数不能导出到 R 中,称为 Rcpp::InternalFunction
。在此实现下,您可以轻松地将 R 的 optim
与来自 C++ 的 C++ 函数合并!
此外,我修改了fr
函数中的元素访问索引。进行此修改的原因是您遇到了越界错误,因为 C++ 中的索引位于从零开始的系统上,而不是像 [= 这样的从一开始的系统上20=]R。 (例如 x(0)
与 R 中的 x[1]
相同)
#include<RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
double fr(arma::vec x){
double result = 100 * (x(1) - x(0) * x(0)) * (x(1) - x(0) * x(0)) + (1 - x(0)) * (1 - x(0));
return result;
}
// [[Rcpp::export]]
arma::vec optim_rcpp(const arma::vec& init_val){
Rcpp::Environment stats("package:stats");
Rcpp::Function optim = stats["optim"];
Rcpp::List opt_results = optim(Rcpp::_["par"] = init_val,
Rcpp::_["fn"] = Rcpp::InternalFunction(&fr),
Rcpp::_["method"] = "BFGS");
// Extract and coerce from list.
arma::vec out = Rcpp::as<arma::vec>(opt_results[0]);
return out;
}
我是 Rcpp 的新手,在我的代码中,我必须从 C++ 调用 R 函数 "optim"。参考了很多例子,还是有错误:"static assertion failed: cannot convert type to SEXP"。这是我的代码,问题是最后一个函数:
#include <RcppArmadillo.h>
using namespace Rcpp;
using namespace RcppArmadillo;
using namespace arma;
using namespace std;
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
double fr(arma::colvec x){
double result = 100 * (x(2) - x(1) * x(1)) * (x(2) - x(1) * x(1)) + (1 - x(1)) * (1 - x(1));
return result;
}
typedef double (*funcPtr)(arma::colvec x);
// [[Rcpp::export]]
XPtr<funcPtr> putFunPtrInXPtr(){
return(XPtr<funcPtr>(new funcPtr(&fr)));
}
// [[Rcpp::export]]
arma::colvec callOptimFun(SEXP x) {
RNGScope scope;
Rcpp::Environment stats("package:stats");
Rcpp::Function optim = stats["optim"];
XPtr<funcPtr> xpfun = putFunPtrInXPtr();
funcPtr fun = *xpfun;
Rcpp::List y = optim(x, fun);
arma::colvec r = y["par"];
return r;
}
不幸的是,我为我的最后一个功能尝试了很多方法,并且所有方法都出现相同的错误。这些是我的尝试: 1.
// [[Rcpp::export]]
Rcpp::List callOptimFun(arma::colvec x) {
\....
Rcpp::List y = optim(x, fun);
return y;
}
2.
// [[Rcpp::export]]
Rcpp::List callOptimFun(arma::colvec x) {
\....
Rcpp::List y = optim(x, fun);
return y;
}
3.
// [[Rcpp::export]]
Rcpp::List callOptimFun(SEXP x) {
\....
Rcpp::List y = optim(x, fun);
return y;
}
我不熟悉C++。可能是什么问题?谢谢!
在这种情况下,函数指针的使用是有问题的。 Rcpp 有一种特殊类型的包装器,它要求 C++ 函数不能导出到 R 中,称为 Rcpp::InternalFunction
。在此实现下,您可以轻松地将 R 的 optim
与来自 C++ 的 C++ 函数合并!
此外,我修改了fr
函数中的元素访问索引。进行此修改的原因是您遇到了越界错误,因为 C++ 中的索引位于从零开始的系统上,而不是像 [= 这样的从一开始的系统上20=]R。 (例如 x(0)
与 R 中的 x[1]
相同)
#include<RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
double fr(arma::vec x){
double result = 100 * (x(1) - x(0) * x(0)) * (x(1) - x(0) * x(0)) + (1 - x(0)) * (1 - x(0));
return result;
}
// [[Rcpp::export]]
arma::vec optim_rcpp(const arma::vec& init_val){
Rcpp::Environment stats("package:stats");
Rcpp::Function optim = stats["optim"];
Rcpp::List opt_results = optim(Rcpp::_["par"] = init_val,
Rcpp::_["fn"] = Rcpp::InternalFunction(&fr),
Rcpp::_["method"] = "BFGS");
// Extract and coerce from list.
arma::vec out = Rcpp::as<arma::vec>(opt_results[0]);
return out;
}