将Rcpp :: NumericVector转换为Eigen :: VectorXd

时间:2015-09-01 09:35:32

标签: c++ rcpp eigen

以前曾问过类似的问题:

Converting between NumericVector/Matrix and VectorXd/MatrixXd in Rcpp(Eigen) to perform Cholesky solve

问题是,我在 fastLm.cpp 中找到的代码(最后)对我不起作用。

Rcpp::NumericVector X( (SEXP) R.parseEval("x <- 1:10"));

Eigen::Map<Eigen::VectorXd> XS(Rcpp::as<Eigen::Map<Eigen::VectorXd>>(X)); //!!not working

它出现以下错误:

error: no matching constructor for initialization of 'Eigen::Map<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::Stride<0, 0> >'
                    Exporter( SEXP x ) : t(x){}

和(除了其他人):

note: candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type 'SEXP' (aka 'SEXPREC *') to 'const Eigen::Map<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 0, Eigen::Stride<0, 0> >'
template<typename PlainObjectType, int MapOptions, typename StrideType> class Map

它可能与SEXP对象有关吗?

1 个答案:

答案 0 :(得分:2)

如果我在>>

之间再添加一个空格,它对我有用
R> cppFunction("bool conv(NumericVector X) { \
      Eigen::Map<Eigen::VectorXd> \
      XS(Rcpp::as<Eigen::Map<Eigen::VectorXd> >(X)); return true; } ", 
           depends="RcppEigen")
R> conv(1:4)
[1] TRUE
R>

另外,我不认为你可以说

X((SEXP) R.parseEval("x <- 1:10"))

除非您使用RInside,它有自己的Eigen示例 - 并为此只做

cd examples/eigen
make
./rinside_sample0
./rinside_sample1

两者仍然在我的安装中运行。

相关问题