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

Sparse matrix assignment

Tags: None
(comma "," separated)
zoharl
Registered Member
Posts
55
Karma
0
OS

Sparse matrix assignment

Wed Jul 18, 2012 4:47 pm
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).
User avatar
fearhope
Registered Member
Posts
20
Karma
0
OS

Re: Sparse matrix assignment

Wed Jul 18, 2012 8:43 pm
why don't you test the following code.

L = -1 * L.eval();

cheers,
yeonchool
zoharl
Registered Member
Posts
55
Karma
0
OS

Re: Sparse matrix assignment

Thu Jul 19, 2012 6:51 am
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?
jitseniesen
Registered Member
Posts
204
Karma
2

Re: Sparse matrix assignment

Thu Jul 19, 2012 9:49 am
zoharl wrote: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).
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:
Code: Select all
SparseMatrix<double> L(5,5), tmp;
L(0,0) = 1;
tmp = L;
L = -tmp;
fearbone's code with eval() is a shortcut for achieving this.

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.
zoharl
Registered Member
Posts
55
Karma
0
OS

Re: Sparse matrix assignment

Thu Jul 19, 2012 10:39 am
Ho, I see. Somehow I remembered that it happens also for different variables assignment, else I would have immediately considered the aliasing problem.
Thanks
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Sparse matrix assignment

Fri Jul 20, 2012 8:15 pm
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.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]