【问题标题】:Efficient way to initialize larga, non-const Eigen Matrix初始化大型非 const 特征矩阵的有效方法
【发布时间】:2018-03-23 12:16:25
【问题描述】:

我有一个 4000×4 矩阵,需要用不同的值进行初始化。

我注意到以下在 GCC 中花费了很多时间,以至于它实际上会挂起编译器:

Eigen::Matrix<double,1000,500> mat; 
mat.setZero();

mat << 1,2,3,4,
       10,2,3,1,
       (etc)

所以,我想我也可以这样做:

int i=0;
mat.row(i++) << 1,2,3,4;
mat.row(i++) << 10,2,3,1;
(etc)

是否有更节省编译时间和运行时效率的方式来执行此操作?

【问题讨论】:

  • 对于如此大的矩阵,最好使用MatrixXd 而不是静态分配的。

标签: eigen eigen3


【解决方案1】:

只需将值存储在 POD 数组中(可能对齐)并在其上使用 Eigen::Map

EIGEN_ALIGN_TO_BOUNDARY(32) // align if you want to use SIMD
static const // leave the const, if you want to modify the data
double data[4*4]  = {  // 4000*4 in your case
        0, 1, 2, 3,
        4, 5, 6, 7,
        8, 9,10,11,
        12,13,14,15,
        // ...
};
// again, leave the const, if you want to modify `mat`:
// RowMajor is easier to read when defining `data`
const static Eigen::Map<const Eigen::Matrix<double, 4, 4, Eigen::RowMajor>, Eigen::Aligned32> mat(data);

【讨论】:

  • Eigen 是否允许将 mat 分配给 Eigen::Matrix?
  • @anr 是的,您可以像使用其他任何Eigen::Matrix 一样使用Eigen::Map&lt;...&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2023-04-11
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
相关资源
最近更新 更多