【问题标题】:Force Eigen to check matrices dimensions at run-time强制 Eigen 在运行时检查矩阵尺寸
【发布时间】:2019-03-25 03:30:57
【问题描述】:

Eigen 似乎不检查动态矩阵的维度。例如,如果我执行以下代码:

auto EA = Eigen::MatrixXf(3, 2);
auto EB = Eigen::MatrixXf(3, 2);
for (auto i = 0; i < 3; ++i)
{
  for (auto j = 0; j < 2; ++j)
  {
    EA(i,j) = i + j + 1;
    EB(i,j) = i + j + 1;
  }
}
auto EC = EA*EB;
std::cout << "EA: " << std::endl << EA << std::endl;
std::cout << "EB: " << std::endl << EB << std::endl;
std::cout << "EC: " << std::endl << EC << std::endl;

它输出:

EA:
1 3
2 3
2 4
EB:
1 3
2 3
2 4
EC:
 7 12
 8 15
10 18

如何强制 Eigen 在运行时检查矩阵维度?这对于初学者和调试非常有用。

【问题讨论】:

  • 它实际上是在使用调试标志编译时检查尺寸。我不确定我的问题是否仍然相关。

标签: c++ eigen


【解决方案1】:

尺寸检查仅在未定义 NDEBUG 宏时进行。这通常意味着调试版本。

没有NDEBUG 的示例,其中检查成功中止程序:

g++ test.cpp -o test -I /usr/include/eigen3 && ./test
test: /usr/include/eigen3/Eigen/src/Core/ProductBase.h:102: Eigen::ProductBase<Derived, Lhs, Rhs>::ProductBase(const Lhs&, const Rhs&) [with Derived = Eigen::GeneralProduct<Eigen::Matrix<float, -1, -1>, Eigen::Matrix<float, -1, -1>, 5>; Lhs = Eigen::Matrix<float, -1, -1>; Rhs = Eigen::Matrix<float, -1, -1>]: Assertion `a_lhs.cols() == a_rhs.rows() && "invalid matrix product" && "if you wanted a coeff-wise or a dot product use the respective explicit functions"' failed.
Aborted (core dumped)

还有NDEBUG:

g++ test.cpp -o test -I /usr/include/eigen3 -DNDEBUG && ./test
EA: 
1 2
2 3
3 4
EB: 
1 2
2 3
3 4
EC: 
 5  8
 8 13
11 18

【讨论】:

  • 是的,我对此写了评论。感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 2013-07-18
  • 2012-07-12
  • 2011-12-05
相关资源
最近更新 更多