[GiNaC-devel] Term ordering.
alexei.sheplyakov at gmail.com
Sun Jan 22 18:14:14 CET 2012
> when I do (e.g. in ginsh)
> -a * (r^n-R^n);
> I get
> a * (R^n-r^n)
> for n <= 3
> -a * (r^n-R^n)
> for n > 3
The terms of sums and products (and some other things like the arguments of
symmetric functions, the indices of symmetric tensors etc.) are re-ordered
into a canonical form that is deterministic, but not lexicographical or in
any other way easy to guess (it almost always depends on the number and order
of the symbols you define). However, constructing the same expression twice,
either implicitly or explicitly, will always result in the same canonical form.
Also note that the canonical form might change from run to run:
for i in `seq 1 100`; do
echo "a = x; b = y; x - y;" | ginsh
done | sort -n | uniq
Depending on luck, canonical form of the same expression (x-y) might be
either 'x-y' or '-y+x' (of course it's the same during one run).
The terms of sums (and products) are stored as an array sorted according
to the hash value. Using such a data structure makes collecting similar
terms reasonably fast. As a side effect it makes the order of terms
`unpredictable', that is, deterministic, but not easy to guess (that's
a property of any reasonable hash function).
Please note: this is not a bug. Fast(er) automatic evaluation is much more
important than a pretty output. Given that input and output expressions
typically contain ~ 10^4 terms (or more) the exact term order does not
really matter anyway. If you really need a fancy output, write a custom
This question pops up quite often , should we add it to the FAQ?
More information about the GiNaC-devel