Simple rational expressions are written down in GiNaC pretty much like
in other CAS or like expressions involving numerical variables in C.
The necessary operators +, -, * and / have
been overloaded to achieve this goal. When you run the following
code snippet, the constructor for an object of type mul is
automatically called to hold the product of a and b and
then the constructor for an object of type add is called to hold
the sum of that mul object and the number one:
...
symbol a("a"), b("b");
ex MyTerm = 1+a*b;
...
For exponentiation, you have already seen the somewhat clumsy (though C-ish)
statement pow(x,2); to represent x squared. This direct
construction is necessary since we cannot safely overload the constructor
^ in C++ to construct a power object. If we did, it would
have several counterintuitive and undesired effects:
2*x^2 would be parsed as (2*x)^2.
^, x^a^b would result in
(x^a)^b. This would be confusing since most (though not all) other CAS
interpret this as x^(a^b).
^ since it is then
hard to distinguish between the semantics as exponentiation and the one
for exclusive or. (It would be embarrassing to return 1 where one
has requested 2^3.)
All effects are contrary to mathematical notation and differ from the
way most other CAS handle exponentiation, therefore overloading ^
is ruled out for GiNaC's C++ part. The situation is different in
ginsh, there the exponentiation-^ exists. (Also note
that the other frequently used exponentiation operator ** does
not exist at all in C++).
To be somewhat more precise, objects of the three classes described
here, are all containers for other expressions. An object of class
power is best viewed as a container with two slots, one for the
basis, one for the exponent. All valid GiNaC expressions can be
inserted. However, basic transformations like simplifying
pow(pow(x,2),3) to x^6 automatically are only performed
when this is mathematically possible. If we replace the outer exponent
three in the example by some symbols a, the simplification is not
safe and will not be performed, since a might be 1/2 and
x negative.
Objects of type add and mul are containers with an
arbitrary number of slots for expressions to be inserted. Again, simple
and safe simplifications are carried out like transforming
3*x+4-x to 2*x+4.