Rcpp Eigen Map Error with MatrixXf

余生颓废 提交于 2019-12-11 05:34:08

问题


Why does the following code not compile?

library(Rcpp)
cppFunction('
    int rows(const NumericMatrix& X) {
        using Eigen::MatrixXf;  
        typedef Eigen::Map<MatrixXf> MapMat;
        MapMat X1(as<MapMat>(X));
        return X1.rows();
}', depends = "RcppEigen")

It throws the following error:

error: no matching function for call to 'Eigen::Map<Eigen::Matrix<float, -1, -1> >::Map(Rcpp::Vector<14, Rcpp::PreserveStorage>::iterator, int&, int&)'
         OUT get() {return OUT(vec.begin(), d_nrow, d_ncol );}

The same code works fine when I used MatrixXd instead.

Thanks.


回答1:


NumericMatrix uses type double (as opposed to float). Eigen does not support implicit type casting between matrices using different types. Your code appears to try and read the memory of a double NumericMatrix as a float Eigen matrix. Just use the MatrixXd type instead.



来源:https://stackoverflow.com/questions/40114103/rcpp-eigen-map-error-with-matrixxf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!