问题
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