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
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.
The equations I am dealing with can be several thousand lines long at
times, so things get extremely messy.  However, within these
equations, many of the same quantities are used repeatedly, just like
in your example -- sin(theta) and cos(theta).

There is a program I use called Autolev, written by some people from
Stanford, and it does this introduction of intermediate variables very
effectively.  Unfortunately, Autolev isn't open source.  I am very
interested in writing an open source variant of Autolev, but this part
of the program is something that I know little about and am not sure
yet how to approach.

Thanks for the response and if you have any other ideas or references,
I'd love to hear them!  I'm guessing I'm going to need to study up on
searching, sorting, and regular expressions.

Thanks,
~Luke


On Jun 14, 11:10 am, "Ondrej Certik" <[EMAIL PROTECTED]> wrote:
> Hi Luke!
>
>
>
> On Fri, Jun 13, 2008 at 8:45 PM, Luke <[EMAIL PROTECTED]> wrote:
>
> > I'm working on some code that symbolically generates equations of
> > motion for mechanical systems.  I would like the equations to be
> > computationally efficient in that they don't repeatedly calculate
> > quantities that have previously been calculated -- i.e. if
> > cos(theta)*sin(alpha)*L is a subexpression that occurs in multiple
> > places in an equation, it would be identified and computed once and
> > stored in an intermediate variable that then replaces the
> > subexpression everywhere it occurs in the equation.  Does anybody know
> > a good algorithm for searching equations and building up a list of
> > subexpressions?  There could be many levels of subexpressions -- in
> > the example above, cos(theta) and sin(theta) themselves would like be
> > used in other subexpressions, so they could be considered
> > subexpressions themselves.
>
> > I have used a symbolic manipulator that has this feature but it is a
> > small closed source project and I don't know how they did it.  It is
> > very valuable though -- the difference in the file size for the right
> > hand sides of the differential equations for the system we are
> > studying is several orders of magnitude -- this makes integration
> > times *much* faster when this common subexpression replacement method
> > is used.
>
> > Any ideas or references on this subject?
>
> Thanks for your interest. I am not sure if I understand right, but would
> something like this solve the problem?
>
> In [1]: var("theta")
> Out[1]: θ
>
> In [2]: e = k/sqrt(sin(theta) * cos(theta))
>
> In [3]: e
> Out[3]:
>           k
> ─────────────────────
>   ⎽⎽⎽⎽⎽⎽⎽⎽   ⎽⎽⎽⎽⎽⎽⎽⎽
> ╲╱ cos(θ) *╲╱ sin(θ)
>
> In [4]: e.subs({cos(theta): y, sin(theta): z})
> Out[4]:
>      k
> ───────────
>   ⎽⎽⎽   ⎽⎽⎽
> ╲╱ y *╲╱ z
>
> Or are you looking for an algorithm to automatically collect common
> subexpressions? For that, at least for me it'd help if you could be
> more specific, i.e. give us some particular examples of functionality
> that you'd like sympy to do but it can't. We'll then think how to
> implement that.
>
> Thanks,
> Ondrej
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to