Reputation: 4946
It seems that Eigen does not check dynamic matrices dimensions. For example, if I execute the following code:
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;
It outputs:
EA:
1 3
2 3
2 4
EB:
1 3
2 3
2 4
EC:
7 12
8 15
10 18
How do I force Eigen to check matrices dimensions at run-time? This would be really useful for beginners and for debugging.
Upvotes: 0
Views: 528
Reputation: 19130
Dimension checking only happens when NDEBUG
macro is not defined. This normally means a debug build.
Example without NDEBUG
, where the checks successfully abort the program:
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)
And with 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
Upvotes: 3