Substituting expressions

Pearu Peterson pearu at cens.ioc.ee
Wed Apr 4 22:01:42 CEST 2001


Hi!

First, I would like to thank you for GiNaC. It's a great effort. 
It is also very useful library for me -- I am using it from Python and I
am very satisfied with the combination of GiNaC's speed and Python's
flexibility.

Problem:
Now, while using GiNaC I noticed that the `subs' method is rather limited.
Currently with the `subs' method one can replace only `symbol' and `idx'
objects in an expression. 

Suggestion:
I would like to suggest extending the `subs' method for other objects as
well. In particular, for those that can be identified with the `has'
method. I think, that this criterion for substitutions is both general and
practical.

Example:
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)
(This is a simplified case, in my particular problem the sum contains more
than 20 terms and the function f is multivariate).
My plan was to substitute `f(x)' with a symbol, say `fx', then collect
with respect to `fx', and finally put `f(x)' back to `fx'.
This was impossible due to `subs' limitation. 
Finally I found a way to achieve my goal: divide by `f(x)', expand,
multiply by `f(x)'. But this is just an ugly trick and does not seem
right. It does not work for expressions like
   a*f(x)+b*f(x)+c*f(x)+d
that again requires other ugly tricks.

I think that extending the `subs' method will avoid these problems and
makes the use of GiNaC even easier.

Consequences:
There are also other methods that only accept `symbol' objects, such us
`collect', `degree', `coeff' etc that has the same limitations as `subs'.
But note that with extended `subs', you don't have to extend these
methods at all: [`subs' -> apply method -> `subs' back] will do the job.

What do you think?

Regards,
	Pearu




More information about the GiNaC-devel mailing list