Problems with expand()

Richard B. Kreckel kreckel at
Tue Mar 5 17:29:01 CET 2002


On Thu, 28 Feb 2002, Tatiana Zolo wrote:
> We have a few questions about GinaC's behaviour.
> For our purposes, it would be useful to have a slightly more general
> form of expand that takes care of non-ambiguous iterated exponentials,
> as in the case
> (2^x)^3
> which we would like to see in the form
> 2^(3*x)   or even   8^x
> We understand that there are cases when this should not be done
> automatically, but we believe that this is an instance when there
> is no harm in simplifying since 2 is a positive real number and
> there is no question of complex numbers being involved. A "real"
> version of expand would be very useful for us.
> Do you plan to add these features to a future release of GiNaC?

Not really.  At least, unless somebody provides a detailed analysis of
all these transformations and suggests an implementation in form of
patches.  Then we would need to look at the impact of these patches...

I noticed that Maple and Mathematica don't expand it either, but simplify
the result to the form you desire.  Per se, this doesn't rule out an
implementation in the expand method, but it suggests that it doesn't
belong there.  (2^x) is not a polynomial, strictly speaking, because it
has a symbolic exponent.

For now, I suggest that you implement the functionalty yourself, using
cleverly designed function objects that traverse the expression tree.

Below comes a brain-dead, quick-and-dirty hack that converts (2^x)^3 to
8^x.  You shouldn't use it, I wrote it in 2 minutes.  But it may give you
a start if you haven't got further meanwhile anyhow...

Richard B. Kreckel
<Richard.Kreckel at Uni-Mainz.DE>

struct my_power_mangler : public GiNaC::map_function {
    my_power_mangler() {}
    ex operator()(const ex& e) {
        if (is_a<power>(e) && is_a<power>(e.op(0))) {
            if (e.op(0).op(0).info(info_flags::integer) && e.op(1).info(info_flags::integer))
                return pow(pow(e.op(0).op(0),e.op(1)),e.op(0).op(1));

More information about the GiNaC-list mailing list