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
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/
participants (2)
-
Christian Bauer
-
Pearu Peterson