20

Is there some easy and fast way to convert a sparse matrix to a dense matrix of doubles?

Because my SparseMatrix is not sparse any more, but became dense after some matrix products.

Another question I have: The Eigen library has excellent performance, how is this possible? I don't understand why, because there are only header files, no compiled source.

sgvd
  • 3,819
  • 18
  • 31
user2165656
  • 445
  • 1
  • 3
  • 11
  • 2
    What does performance have to do with whether code is in `.h` files or in `.cpp` files? – us2012 Mar 21 '13 at 21:14
  • 3
    To understand how Eigen works and what makes it fast, have a better look at their docs (e.g. [What happens inside](http://eigen.tuxfamily.org/dox/TopicInsideEigenExample.html)) (which seeing the number of your questions would be useful in any case), and pick up a book on advance C++ and template programming. In any case, you should remove that question here and make a new one for it if after this you still are not sure about some details of how it works. – sgvd Mar 23 '13 at 14:50

1 Answers1

47

Let's declare two matrices:

SparseMatrix<double> spMat;
MatrixXd dMat;

Sparse to dense:

dMat = MatrixXd(spMat);

Dense to sparse:

spMat = dMat.sparseView();
Michael Koval
  • 8,207
  • 5
  • 42
  • 53
ggael
  • 28,425
  • 2
  • 65
  • 71