[GiNaC-list] write access to entries of submatrices [Was: multi index storage of expressions]

Charlls Quarra charlls_quarra at yahoo.com.ar
Tue Feb 20 20:16:08 CET 2007

--- Sheplyakov Alexei <varg at theor.jinr.ru> escribió:

> > matrix sub = ex_to<matrix>(foo( 0, 1));
> [unnecessary] copy operation
> > 	sub( 1 , 1) = 34;
> > 	foo( 0 , 1 ) = sub;
> yet another [unncessary] copy operation
> > cout << "yes --> " << ex_to<matrix>( foo( 0 , 1 )
> > )(1,1) << endl;
> > 
> > if this is breaks refcounting too,
> It does not. But it involves two copy operations...
> > then i guess one should ask for a revision why is
> that
> > matrix entries behave like unidirectional
> membranes
> > (you can put but cannot change)
> Actually this is documented in the manual: see the
> section "Expressions
> are reference counted" in the the "Internal
> structures" chapter. Since
> I dislike RTFM as an answer (although I think
> reading manuals *does*
> help), I'll briefly summarise it here. GiNaC is
> designed to handle large
> expressions, so at minimum it needs to store them in
> efficient way.
> Reference counting and copy on write semantics are
> simple and powerful
> techniques to achive this. The price to pay is
> "undirectional membrane"
> behaviour (no direct write access to class data).

Oh well, first you said that it wasn't possible to do
it in ginac and i should look somewhere else, now you
speak about it being inefficient.
I happen to have read that chapter that you RTFM me
before asking about write access, and precisely on
that page it says that copy is done lazily, so in fact
the copy operation:

matrix sub = ex_to<matrix>( mat(1,2) );

should be "light-weight", at least if the matrix is
really just a handle like the ex.

now, when i do
 sub(3,4) = 15;

this is arguably doing a copy (again, according to the
RTFMed chapter) because both sub and mat(1,2) point to
the same object, but the write operation on a handle
triggers the copy.
However, now that we are talking about _efficiency_,
this is not unavoidable, at least from what
refcounting refers. If one would do:

matrix sub = ex_to<matrix>( mat( 1 , 2 ) );
mat( 1 , 2 ) = 0; // <-- this kills the first
sub(3, 4) = 15; // no copy should be involved anymore,
there is a single reference to the underlying matrix
mat( 1 , 2 ) = sub;

so in this case, Apparently no deep copy should be

I know i'm new to the list and probably you feel you
have to be so overly cynical and aggresive to make
some sort of social standpoint. I actually wasnt
trying to be fixed on doing this with matrix or ginac
objects only other than esthetical/completeness
reasons: I even could come around to consider doing it
with std::vector after all (or even better, blitz),
but that was off the point by several miles. I was
really just trying to learn ginac by doing something
that i would need commonly. I was also intrigued if
others don't seem to require multi indexing (i use it
all the time on maple using lists of lists, but it
doesnt seem to be efficient with deep stacking). 

Anyways, thanks for your comments. they have been
enlightening at several levels



Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 

More information about the GiNaC-list mailing list