Skip to content

Assertion error with zero-sized matrices

Summary

I encountered some surprising behavior with matrices of size zero. Basically, you can't assign a fixed-size empty vector to a fixed-maximum-number-of-elements empty vector.

Environment

  • Operating System : Linux
  • Architecture : x64
  • Eigen Version : 3.3.7
  • Compiler Version : Gcc 9.4.0
  • Compile Flags : irrelevant
  • Vector Extension : irrelevant

Minimal Example

#include <eigen3/Eigen/Core>

int main() {

    Eigen::Matrix<double, Eigen::Dynamic, 1, 0, 0, 1> vector_with_max_num_rows;
    Eigen::Matrix<double, 0, 1> empty_vector;

    vector_with_max_num_rows = empty_vector; // This gives an assertion error: "Assertion `dst.rows() == dstRows && dst.cols() == dstCols' failed."

}

Steps to reproduce

  1. Compile above example
  2. Run
  3. Get assertion error

What is the current bug behavior?

I expected that Eigen deals with resizing zero-sized matrices for all kinds (dynamic, fixed-size, fixed-max-col-or-row-size). In this specific case, the number of rows.

However, since the resize implementation is empty for zero-sized matrices, the number of cols don't match after resize (which gives the assertion error).

In AssignEvaluator.h:

template<typename DstXprType,typename SrcXprType, typename T1, typename T2>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
void resize_if_allowed(DstXprType &dst, const SrcXprType& src, const internal::assign_op<T1,T2> &/*func*/)
{
  Index dstRows = src.rows();
  Index dstCols = src.cols();
  if(((dst.rows()!=dstRows) || (dst.cols()!=dstCols)))
    dst.resize(dstRows, dstCols);
  eigen_assert(dst.rows() == dstRows && dst.cols() == dstCols);
}

What is the expected correct behavior?

Resize actually changes the dimensions, such that the assertion is not triggered.

Edited by Robin Verschueren