# [GiNaC-list] Symbol/Constant Ordering

Alexei Sheplyakov varg at metalica.kh.ua
Thu Dec 11 11:44:21 CET 2008

```Hello,

On Wed, Dec 10, 2008 at 11:55:38PM +0000, Freddie Witherden wrote:
> I am wondering how one might go about changing the order in which
> variables appear in an expression.

Basically, the answer is: don't do that, it *really* hurts. See e.g.
http://www.ginac.de/pipermail/ginac-list/2008-October/001431.html for
more details.

> E.g. if one had: symbol x("x"),
> y("y"), z("z"); ex myExpr = x * y * z * Pi; cout << myExpr; currently
> yields x*y*Pi*z.

The order of terms in the *output* is completely different thing. And it
doesn't have to match the order of terms in the internal representation.
So, if you want to print terms in some specific way, write a custom printing
function (or even a printing context).

> If, for instance, I wanted Pi * x * y * z, how would I go about this?
> While the documentation speaks about ordering of vectors/maps using
> ex_is_less and a mention that it is used internally by GiNaC it does
> not (as far as I can tell) explain how to do it for expressions.

First of all, the ordering provided by ex_is_less is very different from
what you need. Its purpose is completely different: ex_is_less is
a [reasonably] efficient strict weak ordering on expressions (thus one can
establish canonical order of terms reasonably fast, use expressions as
a keys for associative arrays, etc.)

Secondly, GiNaC (internally) sorts terms in sums and products with ex_is_less
(or its equivalent). If you sort them once more, nothing is going to change.

> (Is it possible to get it as a vector, which could then be sorted using a
> custom function?)

Yes.

ex e = /* something */ ;
exvector ev;
std::copy(e.begin(), e.end(), std::back_inserter(ev));

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: 835 bytes
Desc: Digital signature
Url : http://www.cebix.net/pipermail/ginac-list/attachments/20081211/7a15a156/attachment.sig
```