Search code examples
compiler-errorsg++eigen3

Compilation error with Eigen: Eigen matrix size mismatch in if statement


I'm working with Eigen and static matrix sizes. I have a matrix thelp of size (NC-1)*USIZE x (NC-1)*USIZE. If NC is 1, I get a compilation error. The problematic section of the code is the following:

if (NC > 1) {
Eigen::Matrix<float,(NC-1)*USIZE,(NC-1)*USIZE> thelp;
for (unsigned int m=0;m<(NC-1);m++) {
  for(unsigned int n=0;n<=m;n++) {
    thelp.block<USIZE,USIZE>(m*USIZE,n*USIZE) = Eigen::Matrix<float,USIZE,USIZE>::Identity();
  }
}

The error I get is on the line where I do a block operation on thelp, and the error message is the following:

Eigen/src/Core/util/StaticAssert.h:115:9: error: ‘YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES’ is not a member of ‘Eigen::internal::static_assertion<false>’
     if (Eigen::internal::static_assertion<bool(CONDITION)>::MSG) {}

It obviously makes sense to get this error if the statement were executed when NC = 1 because then thelp would have size 0x0. Is there a way to formulate my code snippet that allows for NC = 1, so that I can keep using a statically sized matrix? Or can I somehow let the compiler know that this statement will not be executed because of the if expression?

Thank you for any help!


Solution

  • It seems I was a bit too quick to ask. I found out that there is a Eigen function setIdentity() that allows me to do what I want. I only needed to modify the original code snippet on the line that was causing me problems:

    Before:

    thelp.block<USIZE,USIZE>(m*USIZE,n*USIZE) = Eigen::Matrix<float,USIZE,USIZE>::Identity();
    

    After:

    thelp.block<USIZE,USIZE>(m*USIZE,n*USIZE).setIdentity();
    

    This way, I get around the error of trying to assign a USIZE x USIZE identity matrix to a 0x0 matrix when NC is equal to 1.