Substituting expressions

Christian Bauer cbauer at student.physik.uni-mainz.de
Wed Apr 4 22:41:29 CEST 2001


Hi!

On Wed, Apr 04, 2001 at 10:01:42PM +0200, Pearu Peterson wrote:
> I was driven to make such a suggestion after unsuccessful tries to simplify
> the following expression
>    a*f(x)+b*f(x)+c*f(x)
> to
>    (a+b+c)*f(x)

In the current (0.8.0) version of GiNaC you could use to_rational() to
get f(x) replaced by a symbol, e.g.

    symbol a("a"), b("b"), c("c"), x("x");
    ex e = a * sin(x) + b * sin(x) + c * sin(x);

    lst l;
    ex f = e.to_rational(l);
    cout << f.collect(sin(x).to_rational(l)).subs(l) << endl;

In the next version of GiNaC it will be possible to use degree(), ldegree(),
coeff(), tcoeff(), lcoeff() and collect() with more object types (constants,
indexed objects and functions), so you will be able to simply write

    cout << e.collect(sin(x)) << endl;

Bye,
Christian

-- 
  / Coding on PowerPC and proud of it
\/ http://www.uni-mainz.de/~bauec002/



More information about the GiNaC-devel mailing list