[GiNaC-devel] Creating non-commutative symbols
Alexei Sheplyakov
alexei.sheplyakov at gmail.com
Sat Feb 19 22:13:20 CET 2011
Hi again,
> maybe I should explain why I need non-commutative symbols. Without them,
> this code:
>
> #include <ginac/ginac.h>
>
> using namespace GiNaC;
>
> int main() {
> symbol M1("M1");
>
> symbol x("x"), y("y");
> matrix M2(2,2);
> M2 = x, 0, y, 0;
>
> matrix M3(2,2);
> M3 = 0, x, x, 0;
>
> symbol M4("M3");
>
> ex nc;
> nc = M1 * M4;
> nc = nc.subs(M1 == M2);
> nc = nc.subs(M4 == M3);
> nc.evalm();
Perhaps you meant nc = nc.evalm();
> }
>
> throws
> matrix::mul_scalar(): non-commutative scalar
>
> Is that the expected behaviour?
Yes, sort of. Substituting a commutative object with a non-commutative one
is ill defined. I.e. the order of terms in the expression `x*y' does not
matter, and GiNaC can (and does) shuffle terms to canonicalize the expression
(in order to make the internal representation of `x*y' and 'y*x' exactly
the same, so the check for equality x*y == y*x is very fast, as it boils
down to comparing two pointers). Now if one substitutes both x and y with
non-commutative objects (say, X, and Y, respectively), the result is undefined:
it can be either X*Y or Y*X (and it might change from run to run).
> OK, I will do that. But if I read your other explanations correctly,
> even a separate class will not really solve the problem I described
> above?
I guess explaining GiNaC that those symbols does not commute with matrices
would help (override return_type_tinfo()). I haven't checked it (yet), though.
Best regards,
Alexei
More information about the GiNaC-devel
mailing list