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

summation of sparse matrix

Tags: None
(comma "," separated)
Akkawe
Registered Member
Posts
35
Karma
0
OS

summation of sparse matrix

Thu Jan 26, 2012 4:02 pm
Hi,
I'd like to sum many sparse matrix. I studied the tutorial about sparse matrix and i have seen that it is possible to use the + operator.
But the += ?

My code is:
Code: Select all
SparseMatrix<double>  K(500,500);
vector <SparseMatrix<double> *>::iterator iter=theDomain->VecOfMatr.begin();
while (iter != theDomain->VecOfMatr.end())
      K +=  *(*iter++);

this code doesn't work in runtime.
if i changed it with, (for example to sum only the first two) :
Code: Select all
Kglobal=*(TheDomain->VecOfMatr.at(0))+*(TheDomain->VecOfMatr.at(1));

it works.

So could you tell me the right but also the more efficient way to sum sparse matrices?
Thanks
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: summation of sparse matrix

Thu Jan 26, 2012 5:01 pm
you can do:

while(...)
K = (K + **iter).eval();

however, summing up sparse matrices is not an efficient operations. Do you really need to compute the individual sparse matrices? cannot you directly assemble their sum?

How many sparse matrices do you have to sum up?
Akkawe
Registered Member
Posts
35
Karma
0
OS

Re: summation of sparse matrix

Thu Jan 26, 2012 6:45 pm
ggael wrote:you can do:

while(...)
K = (K + **iter).eval();

however, summing up sparse matrices is not an efficient operations. Do you really need to compute the individual sparse matrices? cannot you directly assemble their sum?

How many sparse matrices do you have to sum up?


It's a finite element procedure.
To take my "code" more general as possible, it's better to sum up at the end of the procedure when the matrices are assembled and not before (when are small and dense).
The number of the matrices is related to the user but usually we can speak about 6 sparse matrices.
I will try if it's too slow
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: summation of sparse matrix

Sat Jan 28, 2012 10:18 am
If you pick up a snapshot of the last devel branch you can also simply do something like:
Code: Select all
    typedef Triplet<double> T;
    std::vector<T> tripletList;
    triplets.reserve(estimation_of_entries);
    for(...)
    {
      // ...
      tripletList.push_back(T(i,j,v_ij));
    }
    SparseMatrixType m(rows,cols);
    m.setFromTriplets(tripletList.begin(), tripletList.end());


The duplicates will be summed up by the setFromTriplets function in O(tripletList.size()) time.
Akkawe
Registered Member
Posts
35
Karma
0
OS

Re: summation of sparse matrix

Wed Mar 28, 2012 12:25 pm
ggael wrote:If you pick up a snapshot of the last devel branch you can also simply do something like:
Code: Select all
    typedef Triplet<double> T;
    std::vector<T> tripletList;
    triplets.reserve(estimation_of_entries);
    for(...)
    {
      // ...
      tripletList.push_back(T(i,j,v_ij));
    }
    SparseMatrixType m(rows,cols);
    m.setFromTriplets(tripletList.begin(), tripletList.end());


The duplicates will be summed up by the setFromTriplets function in O(tripletList.size()) time.

Thank you

P.S.
In the third line, triplets should be tripletList ?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: summation of sparse matrix

Thu Mar 29, 2012 9:56 pm
In the third line, triplets should be tripletList ?


right


Bookmarks



Who is online

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