MatrixXf的Rcpp特征映射错误 [英] Rcpp Eigen Map Error with MatrixXf

查看:49
本文介绍了MatrixXf的Rcpp特征映射错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下代码无法编译?

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

它引发以下错误:

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时,相同的代码也可以正常工作.

The same code works fine when I used MatrixXd instead.

谢谢.

推荐答案

NumericMatrix 使用类型 double (而不是 float ).本征不支持使用不同类型的矩阵之间的隐式类型转换.您的代码似乎试图读取 double NumericMatrix 的内存,作为 float 本征矩阵.只需使用 MatrixXd 类型即可.

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.

这篇关于MatrixXf的Rcpp特征映射错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆