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

SparseMatrix: insertion of vector

Tags: None
(comma "," separated)
davidhigh
Registered Member
Posts
18
Karma
0

SparseMatrix: insertion of vector

Fri Jan 13, 2012 5:38 pm
Hi,

I have asked a similar question two months ago in this forum, but I didn't exactly understand the answer...so here is a bit easier formulation:

I construct my symmetric SparseMatrix row-by-row (col-by-col) basically in the following way:

Code: Select all
const int N=10;
Eigen::SparseMatrix<double> M(N,N);
for(int j=0;j<N;++j)
{
   std::vector<int> ind;
   std::vector<double> val;

   // fill ind and val.
   // sort them to obtain ordered indices.

   //insert
   for(int i=0;i<(int) ind.size();++i)
      M.insert(j,ind[i])=val[i];
}



Is this the right way to insert these vectors into my matrix?
Does it go more efficiently?

Does one also have direct access to the Matrix' underlying data, so that I could avoid the copy step?

Thanks in advance,
David
davidhigh
Registered Member
Posts
18
Karma
0

Re: SparseMatrix: insertion of vector

Tue Jan 17, 2012 10:09 pm
push

its really important for me, and according to my tests, it is the insertion step in the above code snippet that slows it down drastically.

what is the right way to do it?

best,
David
jitseniesen
Registered Member
Posts
204
Karma
2
You can speed your code up by calling reserve(). See the section "Filling a sparse matrix" of the tutorial at http://eigen.tuxfamily.org/dox-devel/Tu ... parse.html .

As I understand it, you can access the data with M.innerIndexPtr(), M.innerNonZeroPtr(), M.outerIndexPtr() and M.valuePtr(). These point to the arrays mentioned in the section "Sparse matrix representation" in the same tutorial page.
davidhigh
Registered Member
Posts
18
Karma
0
Thank you for the answer.

I already knew from the tutorial that I should call reserve(). The problem is that I have only a single vector at a time and no idea about the nonzeros of any later vectors.

So, can I call reserve for each row/column anew? Maybe starting with a vector that contains only the size of the first row/column and else only zeros, then with this vector where the second zero is replaced by the size of the second row/column, then with the same vector where the third zero is replaced by the size of the third row/column and so on?

Best,
David
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
cannot you have an upper bound on the nnz per column or rows ?

Another solution would be to use a RowMajor matrix, and use a non documented insertion procedure:

for(int j...)
{
M.startVec(j);
for(i)
M.insertBack(j,i) = val;
}
M.finalize();

until we add the possibility to fill a matrix from a raw list of triplet (i,j,val)...
davidhigh
Registered Member
Posts
18
Karma
0
Thank you!


Bookmarks



Who is online

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