How to multiply a sparse matrix by a dense martix with eigen?

2.3k Views Asked by At

I am trying to multiply a sparse matrix by a dense matrix in Eigen in C++ (the dimensions of course match). The following doesn't seem to work.

Here is a MWE:

  #include <Eigen/Dense>
  #include <Eigen/Sparse>

  using namespace Eigen;

  int main()
  {
        SparseMatrix<double> s;
        s.resize(3,3);
        MatrixXf d(3,3);

        MatrixXf d2(3,3);

        // gives an error
        s*d

        // doesn't give an error
        d*d2
 }

EDIT: The page here suggests that it should work smoothly, yet it doesn't... http://eigen.tuxfamily.org/dox/group__TutorialSparse.html

1

There are 1 best solutions below

0
On

Your problem is is not sparse-dense, but rather mixed types, Xhich isn't allowed. Your sparse matrix is of type double while the dense matrices are of type float (MatrixXF). Change MatrixXf to MatrixXd (or cast to double) and it works fine.