使用 MatrixXf 的 Rcpp 特征映射错误

Rcpp Eigen Map Error with MatrixXf

为什么下面的代码编译不通过?

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")

它抛出以下错误:

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 );}

当我改用 MatrixXd 时,相同的代码工作正常。

谢谢。

NumericMatrix 使用类型 double(相对于 float)。 Eigen 不支持使用不同类型的矩阵之间的隐式类型转换。您的代码似乎试图将 double NumericMatrix 的内存读取为 float 本征矩阵。只需使用 MatrixXd 类型即可。