## Fast updating a existent sparsematrix?

medic123
Registered Member
Posts
25
Karma
0
OS

### Fast updating a existent sparsematrix?

Fri Nov 09, 2012 1:53 pm
Hi,

I use two sparsematrices and two dense vectors that have to be updated frequently during an iterative solving process. The matrices are very sparse, it exists indeed only one Element != 0 per column, wich position stays the same, but the value has to be updated. Currently I use the CoeffRef method to search for the specific Element position, give the References to a method of my own and assign a new value to the element. Three values at once are calculated by a relativ complicated (big therms, exp functions...) mathematical function and then assigned to the references taken as parameter. Looks like this (eg in a for-loop):

Code: Select all
`foo1[i]->calculateNewValues(DenseVector1.coeffRef(i), SparseMatrix1.coeffRef(row,col1), SparseMatrix1.coeffRef(row,col2), double1, double2, double3);  foo2[j]->calculateNewValues(DenseVector2.coeffRef(i), SparseMatrix2.coeffRef(row,col1), SparseMatrix2.coeffRef(row,col2), double4, double5, double6);  `

But coeffRef method is usually not a fast way to fill a sparsematrix . Is it ok for filling the dense vectros? . Can you imaging a faster way to do the same?
I see 2 more possibilities:

1) Create a new matrix in every iteration-step and fill them with a triplet list wich has been updated.
2) Somehow iterate over the inner vectors of the sm in order to put new values to the entries.

Some suggestions?

greetings medic
ggael
Moderator
Posts
2020
Karma
15
OS

### Re: Fast updating a existent sparsematrix?

Sat Nov 10, 2012 1:50 pm
For dense objects, using coeffRef is perfectly fine. For sparse matrices, if there is only 1 non zero per inner vector, and that the sparcity pattern does not change, coeffRef is a O(1) operation, so very fast!

Note that you should bench in release mode only, i.e., with optimizations enabled, and with -DNDEBUG.
medic123
Registered Member
Posts
25
Karma
0
OS

### Re: Fast updating a existent sparsematrix?

Mon Nov 12, 2012 11:27 am
Thank you for your expert-knowledge, nice to know that I must not do something fancy and eigen is just fast.

I want to leave this topic open until I tested the whole algorithm within this this week, just in case... I will post if everything worked fine and the thread is solved.

Greeting, medic
medic123
Registered Member
Posts
25
Karma
0
OS

### Re: Fast updating a existent sparsematrix?

Mon Nov 19, 2012 1:06 pm
Works fine and is as fast as manually iterating over the inner vectors. Solved...

## Who is online

Registered users: Baidu [Spider], Bing [Bot], cylverbak, Exabot [Bot], Google [Bot], google01103, khsien, koriun, Majestic-12 [Bot], MSN [Bot], Steve T