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

Sheplyakov Alexei varg at theor.jinr.ru
Tue Feb 20 19:31:37 CET 2007


Hello!

On Tue, Feb 20, 2007 at 09:44:54AM -0300, Charlls Quarra wrote:

> This morning i found a non-hacky (less-hacky?) way to
> do it.

Could you please explain why vector<matrix> is not good enough?
I admit there might be some reason for that, but I can't see any.

> matrix foo(1 , 2);
> 	for (int i=0 ; i< 2 ; i++)
> 	{
> 		foo(0,i) = matrix(2,2);
> 	}
> 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).


> you make it sound quite final though, so i'll assume you meant
> that there was no way to do it strictly thru the ex_to<> interface,
> and no that there was no way to do it "at all"

It is impossible to do it in sane, efficient way. This is _feature_.
 
> of course that this leaves unaswered how to perform simple assignments
> like t3_idx( qi , qj , qk ) = foo from the wrapper interface object...
> I guess ill have to think about that some more

1) Use some C++ container (with ex as an element type)
2) Write your own class which provides such an operator (using GiNaC::matrix
   as a starting point)
3) Use some other [template] library which provide multidimensional 
   arrays (with ex as an element type).

Everything else should be a bug (in GiNaC, not in your code) or evil
hack (a la reinterpret_cast).


Best regards,
 Alexei

-- 
All science is either physics or stamp collecting.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: Digital signature
Url : http://www.cebix.net/pipermail/ginac-list/attachments/20070220/686dc556/attachment.pgp


More information about the GiNaC-list mailing list