Automatic evaluation/simplification

Christian Bauer cbauer at
Wed Nov 13 14:57:37 CET 2002


On Tue, Nov 12, 2002 at 10:34:16PM +0100, Roberto Bagnara wrote:
> Yes, the FAQ says it is completely automatic.  However, is
> there a way to switch it off?

Sort of. There's the method basic::hold() which sets status_flags::evaluated
in the object, which in turn tells the evaluator to stop. To be effective,
however, this must be called before the object is wrapped in an 'ex' (before
ex::construct_from_basic() is called). Therefore it works best with functions
and other cases where you explicitly call the constructor of a GiNaC object:

    ex e;
    symbol x("x");

    e = GiNaC::sin(0);
    cout << e << endl;
    e = GiNaC::sin(0).hold();
    cout << e << endl;
    e = mul(x, 0).hold();
    cout << e << endl;

This prints "0", "sin(0)" and "0*x", respectively.

In the last case, (x*0).hold() doesn't work (there's no ex::hold()), neither
does ex_to<basic>(x*0).hold(), because once the 'basic' becomes an 'ex', it's
too late for hold().

Also, this only works for evaluations done in the eval() method. Especially
in the case of sums and products, the bulk of the evaluation work (sorting
and combining terms) is done by the constructors, and hold() can actually do
more harm than good here:

    e = add(x, -x).hold();

This produces an (invalid) empty 'add' object (which add::eval() would
have turned into a numeric zero).

The only way to get 'held' objects to re-evaluate is to manually clear
the 'evaluated' flag:



  / Coding on PowerPC and proud of it

More information about the GiNaC-list mailing list