Registered Member
|
Hi,
I don't get the result I expect to get when assigning a sparse matrix to another. Even: SparseMatrix<double> L(5,5); L(0,0) = 1; L = -L; results in L resets to zeros. It's as if the assignment operator works as resizeLike (non conservative). |
Registered Member
|
why don't you test the following code.
L = -1 * L.eval(); cheers, yeonchool |
Registered Member
|
Okay, so you have this magic undocumented function, which performs an assignment as expected. But don't you think it's a bug that the assignment operator doesn't behave like an assignment operator?
EDIT: Okay, so it is in the docs, but what does this cryptic: "the matrix or vector obtained by evaluating this expression." means? |
Registered Member
|
Perhaps you need some more explanation. This is because you have the same matrix on the left-hand side and on the right-hand side of the assignment operator (we call this "the aliasing problem"). If you write M = -L, then everything works as expected. So, what you need to do is something like:
The reason for this unfortunate behaviour is that it is impossible to write code that is efficient for both the case "M = -L" and "L = -L" (actually, in this case it may be possible to fix it, but not in general). It is noted in the documentation, on tutorial page 2 at http://eigen.tuxfamily.org/dox/Tutorial ... metic.html and we have a whole page about it at http://eigen.tuxfamily.org/dox/TopicAliasing.html . However, that is only in the context of dense matrices. If L is a dense matrix, then "L = -L" will in fact do what you expect. I'm afraid the documentation of sparse matrices is lagging a bit behind. |
Registered Member
|
Ho, I see. Somehow I remembered that it happens also for different variables assignment, else I would have immediately considered the aliasing problem.
Thanks |
Moderator
|
hm, this should really be fixed. Actually with sparse matrices we should alway evaluate into a temporary and then swap pointers. I'm working on a patch.
|
Registered users: Baidu [Spider], Bing [Bot], Google [Bot]