Boost logo

Ublas :

From: Vardan Akopian (vakopian_at_[hidden])
Date: 2007-09-20 15:32:05


On 9/20/07, Gunter Winkler <guwi17_at_[hidden]> wrote:
> Am Donnerstag, 20. September 2007 11:44 schrieb Vardan Akopian:
> > Given 2 sparse
> > matrices a and b, it looks like doing
> > noalias(b) = a
> > does not transfer a's full structure to b, i.e. a's structural 0's
> > are lost. For example after that assignment a.nnz() may not be equal
> > to b.nnz(). Note that, "b = a" works as expected. So is this intended
> > behavior?
>
> The answer can be found in matrix_assign.hpp at line 922. There is a
> check if the value to be assigned is zero. When this is true the
> assignment is skipped.

Thanks for finding that, matrix_assign.hpp is not the easiest file to
parse visually, even when I knew what I was looking for ;-)

>
> The expression noalias(b)=a is equivalent to b.assign(a) which directly
> calls the matrix_assign mentioned above. Thus all stored zeroes are
> dropped.
>
> The expression b=a does the same in most cases. Only if b and a are of
> the same type the assignment is optimized (see matrix.hpp line 2862) to
> exactly copy all internal arrays. Thus the structure is preserved.

Right. The question however is really the guarantee: is this
documented, and future-proven? Can code rely on that fact, or is it
just an implementation detail, that can change at any time.
Another question: is it a bug (by the user) to rely on information
about the internal structure of the compressed matrix (or the fact
that 2 matrices have the same structure)?

>
> We can now discuss whether this is a bug (missing optimization of
> assign) or a design decision. What do you think?

Or maybe the bug is the other way around: matrix_assign optimizing
where it shouldn't be?

>
> IMO the behavior of operator = () and assign should be identical.

I'm tempted to agree, but I can see that some people might want to
take advantage of either version. I for one needed the "structure
preservation" feature. Perhaps a clone() type of method could
accomplish such a thing?

I think any solution (including the status quo) is fine if it's
documented, and maybe even added as part of the sparse matrix concept.

-Vardan