On Mon, Jun 16, 2008 at 12:10 AM, Robert Kern <[EMAIL PROTECTED]> wrote: > > On Sun, Jun 15, 2008 at 16:11, Ondrej Certik <[EMAIL PROTECTED]> wrote: >> >> Hi Luke, >> >> On Sun, Jun 15, 2008 at 9:00 PM, Luke <[EMAIL PROTECTED]> wrote: >>> >>> Ondrej, >>> I know that Sympy has the capability to do symbolic replacement like >>> you described. I guess what I'm looking for is more an algorithm to >>> help identify and automatically collect common subexpressions so that >>> repeated quantities are only calculated once. For each repeated >>> subexpression in an equation, an intermediate variable would be >>> introduced and assigned the value of the subexpression, and then all >>> occurrences of the repeated subexpression in the equation would be >>> replaced with the intermediate variable. By generating symbolic >> >> Does it also go in several layers, e.g >> >> sin(x) -> A >> sqrt(A**2) -> B >> >> etc.? >> >>> equations this way, when it comes time to actually use them with >>> numerical values (numerical integration of equations of motion, for >>> example), one avoids the repetitive computation of the same >>> quantity. >>> >>> In regards to providing an example -- writing an algorithm to automate >>> the example you did by hand would be exactly what I am looking to do. >> >> How should sympy know that you want to substitute for sin(x) and not >> sqrt(sin(x)) ? > > Actually, that example is not relevant since there is no repeated > subexpression. The idea is to reduce the amount of unnecessarily > repeated computation (usually numerical) when there are subexpressions > that pop up several times in the complete expression. For example, > Mathematica has an example of its common subexpression elimination: > > http://www.wolfram.com/technology/guide/subexpressiondetection.html
Ah, I got it now, thanks! Right, that'd be a very useful feature. i.e. when one has: 1/(sqrt(sin(x)+1)*sqrt(sin(x)+2)) then one can spare some CPU by first substituting for sin(x), evaluating and then substituting back. So the problem could be reformulated: evaluate the symbolic expression (numerically) using the least amount of (expensive) evaluations. Ok, that makes sense and it's very interesting. > On Sun, Jun 15, 2008 at 16:11, Ondrej Certik <[EMAIL PROTECTED]> wrote: >> I don't understand which criteria you use to substitute the >> subexpressions --- the most frequent subexpression? > > Here's some help I got a long time ago when I asked about how to do > this with Python ASTs. I never followed up on it since I had no way > (at that time) to regenerate Python code from the modified ASTs. > > http://groups.google.com/group/comp.lang.python/browse_thread/thread/de32cd3d1b5c3359/0c93c996bd1ad1e1 I see, you wanted to have sympy in 2003. :) I read the thread and indeed many people were interested in it and some even started to code something. Too bad noone has followed up and created sympy back then --- we would be much more advanced now. Anyway, I created an issue for this: http://code.google.com/p/sympy/issues/detail?id=891 Fredrik, you were investigating some approaches to evaluate expressions quickly, do you have any thoughts on this? Ondrej --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---