Search code examples
c++c++14sparse-matrixeigen3

Eigen 3 SpaseMatrix<bool> - SparseMatrix<bool> multiplication warning on GCC 8.2


I want to multiply Boolean sparse matrices using Eigen 3.3.

The following code compiles on GCC 8.2 and Clang 6:

#include <Eigen/Sparse>
#include <iostream>

int main()
{
    Eigen::SparseMatrix<bool> A { 3, 3 };
    Eigen::SparseMatrix<bool> B { 3, 1 };

    A(0, 1) = true;
    A(1, 0) = true;
    A(2, 2) = true;
    B(0, 0) = true;
    B(0, 2) = true;

    std::cout << A * B << std::endl;
    return 0;
}

It prints the result I was expecting, [1 0 1], and it compiles without any warnings on Clang 6. However, on GCC 8.2, I got the following annoying warning:

/usr/include/eigen3/Eigen/src/SparseCore/ConservativeSparseSparseProduct.h:65:25: warning: ‘*’ in boolean context, suggest ‘&&’ instead [-Wint-in-bool-context]
       values[i] = x * y;

Is there a way of override the Boolean multiply to logical or, &&, only when multiplying sparse matrices? Or is there any other workaround? I cannot switch to dense matrices/arrays, since I'm working with really large matrices in the real code. Even with this warning, the performance of this operation is really good.

I've already tried:

  • Using the development version of Eigen from its mercurial repo: It raises the same warning;
  • (A * B).template cast<bool> as described in this stackoverflow question : It raises the same warning;
  • A && B: It raises an runtime error (it seems that it tries to execute a scalar/coefficient wise product): Assertion aLhs.rows() == aRhs.rows() && aLhs.cols() == aRhs.cols()' failed..

Solution

  • I suppose you use -Wall as a GCC compiler flag.

    You can turn off that specific warning by -Wall -Wno-int-in-bool-context