GiNaC performs some automatic transformations on expressions, to simplify them and put them into a canonical form. Some examples:
ex MyEx1 = 2*x - 1 + x; // 3*x-1
ex MyEx2 = x - x; // 0
ex MyEx3 = cos(2*Pi); // 1
ex MyEx4 = x*y/x; // y
This behavior is usually referred to as automatic or anonymous evaluation. GiNaC only performs transformations that are
There are two types of automatic transformations in GiNaC that may not behave in an entirely obvious way at first glance:
ex MyEx5 = 2*(x + y); // 2*x+2*y
ex MyEx6 = z*(x + y); // z*(x+y)
The general rule is that when you construct expressions, GiNaC automatically creates them in canonical form, which might differ from the form you typed in your program. This may create some awkward looking output (‘-y+x’ instead of ‘x-y’) but allows for more efficient operation and usually yields some immediate simplifications.
Internally, the anonymous evaluator in GiNaC is implemented by the methods
ex ex::eval(int level = 0) const;
ex basic::eval(int level = 0) const;
but unless you are extending GiNaC with your own classes or functions, there
should never be any reason to call them explicitly. All GiNaC methods that
transform expressions, like subs() or normal(), automatically
re-evaluate their results.