我是Rcpp和RcppEigen的新手。抱歉,这个问题是天真的。
这是我在R中编译的代码,

require(RcppEigen)
require(inline)

body <- "
  using Eigen::Map;
  using Eigen::MatrixXd;

  const Map<MatrixXd> X(as<Map<MatrixXd> >(X0));
  const Map<MatrixXd> Y(as<Map<MatrixXd> >(Y0));

  int n=X.rows();
  int p=X.cols();
  int nY=Y.cols();
  MatrixXd I(n,n);
  I.setIdentity(n,n);
  double SSE=(Y.transpose()*(I-X*(X.transpose()*X).inverse()*X.transpose())*Y).determinant();
  return wrap(n*log(SSE/n)+log(n)*p);
"
IC <- cxxfunction(signature(X0 = "matrix", Y0 = "matrix"), body, plugin = "RcppEigen")
IC(X1, Y)

其中X和Y分别是
 X1 <- matrix(c(1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,2,0,1,0,2,0,1,0,2,0,1,0,2,0,1,
           0,2,0,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,0,3,1,0,
           0,3,1,0,0,3.01),20,4,byrow=TRUE)
Y <- matrix(c(50,51,52,54,53,60,59,65,67,70,70,73,74,78,82,80,87,84,88,92),20,1)

我可以在R中成功运行上面的代码。但是现在我想添加一些string variabledouble numeric
require(RcppEigen)
require(Rcpp)
require(inline)

body <- "
//#include <string.h>
using Eigen::Map;
using Eigen::MatrixXd;
using Rcpp::as;

const Map<MatrixXd> X(as<Map<MatrixXd> >(X0));
const Map<MatrixXd> Y(as<Map<MatrixXd> >(Y0));
string Criteria = Rcpp::as<string>(Criteria0);
double sigma = Rcpp::as<double>(sigma0);
int n=X.rows();
int p=X.cols();
int nY=Y.cols();
MatrixXd I(n,n);
I.setIdentity(n,n);
double SSE=(Y.transpose()*(I-X*(X.transpose()*X).inverse()*X.transpose())*Y).determinant();

if (Criteria=='A1'){
  return wrap(n*log(SSE/n)+log(n)*p);
}
if (Criteria=='A2'){
  return wrap(n*log(SSE/n)+(2*p*nY*n+nY*(nY+1))/n-2/n+n+2);
}
if (Criteria=='A3'){
  return wrap(n*log(SSE/n)+2*(2+p)*(n*sigma/SSE)-2*(n*sigma/SSE)*(n*sigma/SSE));
}
"

IC <- cxxfunction(signature(X0 = "matrix", Y0 = "matrix",Criteria0="string",sigma0="numeric"), body, plugin = "RcppEigen")

IC(X1, Y,"A2",0)

但发生错误。
Error in compileCode(f, code, language = language, verbose = verbose) :
  Compilation ERROR, function(s)/method(s) not created! cygwin warning:
  MS-DOS style path detected: C:/R/R-31~1.1/etc/x64/Makeconf
  Preferred POSIX equivalent is: /cygdrive/c/R/R-31~1.1/etc/x64/Makeconf
  CYGWIN environment variable option "nodosfilewarning" turns off this warning.
  Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
filec2c6da05e50.cpp:48:14: warning: multi-character character constant [-Wmultichar]
filec2c6da05e50.cpp:51:14: warning: multi-character character constant [-Wmultichar]
filec2c6da05e50.cpp:54:14: warning: multi-character character constant [-Wmultichar]
In file included from C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include/Eigen/Core:305:0,
                 from C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include/Eigen/Dense:1,
                 from C:/Users/LJH/Documents/R/win-library/3.1/RcppEigen/include/RcppEigenForward.h:30,
                 from C:/U
In addition: Warning message:
running command 'C:/R/R-31~1.1/bin/x64/R CMD SHLIB filec2c6da05e50.cpp 2> filec2c6da05e50.cpp.err.txt' had status 1
>
> IC(X1, Y,"A2",0)
Error: could not find function "IC"

任何帮助将不胜感激。

最佳答案

进行以下更改:

  • 添加using std::string;
  • 使用Criteria == "A1"(即双等号和双引号,并在主体字符串周围加上单引号,以便可以在其中使用双引号。)
  • 关于c++ - 如何在RcppEigen中使用字符串变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26603105/

    10-12 22:32