【问题标题】:convert Rcpp::NumericVector to Eigen::VectorXd将 Rcpp::NumericVector 转换为 Eigen::VectorXd
【发布时间】:2015-09-01 09:35:31
【问题描述】:

之前有人问过类似的问题,这里:

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

【问题讨论】:

    标签: c++ rcpp eigen


    【解决方案1】:

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

    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
    

    在我的安装中,它们仍然可以正常运行。

    【讨论】:

    • 是的,我使用 RInside - 忘了提。我在examples/eigen中查看了rinside_eigen0。在我包含 RcppEigen 之后,它现在正在工作。谢谢!
    • 显然,这本身不是问题,但在您的实际问题的上下文中是“噪音”。所以你发现你只是用错误的方式检测了 RInside - 它发生了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多