Hi, I am wondering how one might go about changing the order in which variables appear in an expression. 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. 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. (Is it possible to get it as a vector, which could then be sorted using a custom function?) Regards, Freddie.
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.
participants (2)
-
Alexei Sheplyakov
-
Freddie Witherden