This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Fast Filling a Sparse Matrix (eigen v3.1)

Tags: None
(comma "," separated)
Iliman
Registered Member
Posts
3
Karma
0
Hello there,

I was wandering what is the fasted way to fill a sparse matrix in eigen v3.1. At the moment I am using what I believe to be the easiest way and not particularly the fasted, using insert():
Code: Select all

SparseMatrix<float> A(faces.size()*9+3,faces.size()*3);//faces.size=12894

for(unsigned int i = 0; i < faces.size(); i++)
{
....
   A.insert(row,n*3)=-1;
   A.insert(row+1,n*3+1)=-1;
   A.insert(row+2,n*3+2)=-1;
....
}


This almost takes 2 minutes to fill. Is there a faster way of doing this? For example is there a way to access and set directly the internal elements of the sparse matrix representation such as the values vector or inner/outer indices.

To be more clear,in Matlab it is possible to construct a sparse matrix by specifying 3 vector-arrays(values,column-indices,row-indices) and this takes less than a second.

Many Thanks
jitseniesen
Registered Member
Posts
204
Karma
2
It's faster if you can call reserve() with a good estimate of the number of non-zero entries per row, see the tutorial at http://eigen.tuxfamily.org/dox-devel/Tu ... rseFilling and the documentation of reserve() at http://eigen.tuxfamily.org/dox-devel/cl ... 38c6bd67d4 . I think the class MappedSparseMatrix allows you to construct a sparse matrix from three arrays, but I'm not sure; the (sparse) documentation is at http://eigen.tuxfamily.org/dox-devel/cl ... atrix.html .
Iliman
Registered Member
Posts
3
Karma
0
Thanks for your reply,

Calling reserve does not seem to make any difference but I"ll take a look at the MappedSparseMatrix class. From a first glance it seems to be exactly what I am looking for.

Cheers
Iliman
Registered Member
Posts
3
Karma
0
Having tested the reserve function it really seems to make a difference. The problem was that I had miscalculated the estimated nonzero terms. It takes 2 milliseconds now:D

Thanks
javadre
Registered Member
Posts
1
Karma
0
Hi there,
lliman, would it be possible to let me know how you solved the problem? would it be possible to pass me the sample code using reserve?
I would be grateful.
the bests.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]