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 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to