[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.2 Automatic evaluation and canonicalization of expressions
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
- at most of complexity O(n log n)
- algebraically correct, possibly except for a set of measure zero (e.g. x/x is transformed to 1 although this is incorrect for x=0)
There are two types of automatic transformations in GiNaC that may not behave in an entirely obvious way at first glance:
- 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.
-
Expressions of the form 'number times sum' are automatically expanded (this
has to do with GiNaC's internal representation of sums and products). For
example
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.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |