Search code examples
c++eigen

The default initial state of Eigen matrices with dynamic size


What state does the default constructor Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> leave the matrix in? Is it a 0x0 matrix?

In particular, if a variable is declared Eigen::MatrixXd A;, how could I later test whether something has been assigned to A? With A.size()==0, or is there some special test?


Solution

  • From the documentation,

    A default constructor is always available, never performs any dynamic memory allocation, and never initializes the matrix coefficients.

    Note that, using the default constructor, the dynamic matrices don't have their sizes assigned either. So,

    Eigen::MatrixXd a;
    

    is a 0x0 matrix, while

    Eigen::MatrixXd b(10, 10);
    

    is a 10x10 matrix, with the coefficients uninitialized;

    Yes, you can check if the matrix is still 0x0 to verify if something has been assigned to it.

    Note that if the size is not dynamic, it will have a defined number of rows and/or columns, and this will make it harder to know if something has been assigned to it: you should initialize the values and then check if they have been changed.