c++ - 将 Rcpp::NumericVector 转换为 Eigen::VectorXd

标签 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 对象有关?

最佳答案

如果我在 >>> 之间再添加一个空格,它对我有用:

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

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

关于c++ - 将 Rcpp::NumericVector 转换为 Eigen::VectorXd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32328180/

相关文章:

c# - 将对象从 C# 传递到 C++ 得到 "cannot use this indirection"?

c++ - 在映射键的 vector 中查找多个字符串

c++ - Rcpp 评估错误 : subscript out of bounds

c++ - Eigen库是在修改C++语法吗

c++ - 未调用更具体的重载

c++ - 在堆上创建链式匿名对象

python - 使用特征张量复制 TensorFlows Conv2D 操作

c++ - 如何在 Eigen 中有效地使用逆和行列式?

c++ - 限制用户输入的数字只能保留到小数点后 2 位 C/C++

c++ - Rcpp 参数范围向上传播?