from sympy import *
t = Symbol('t')
x = Function('x')(t)
y = Function('y')(t)
def partial_derivative(equa, param):
param_sub = Symbol('param_sub')
equa_subs1 = equa.subs(param, param_sub)
dequa_param_sub = diff(equa_subs1, param_sub)
dequa_param = dequa_param_sub.subs(param_sub, param)
return dequa_param
def lagrange(equa, param):
"""
param is a list of Function of time
must not use 'q1', 'q2', .... 'qv1', ... in the equation
"""
vel_var = [diff(e, t) for e in param]
dT_dqv = [partial_derivative(equa, p) for p in vel_var]
dT_dq = [partial_derivative(equa, p) for p in param]
dT_dqvt = [diff(e, t) for e in dT_dqv]
return dT_dqvt, dT_dq
On 31 oct, 20:51, Tim Lahey <[email protected]> wrote:
> On Sun, Oct 31, 2010 at 3:41 PM, Aaron S. Meurer <[email protected]> wrote:
>
> > I am still trying to fully understand the code below, but if one of the
> > subs is trying to do something like diff(f(x), x).subs(x, g(x)), then you
> > will get an error in SymPy for the above reason.
>
> > Aaron Meurer
>
> The basic approach is to substitute a symbol for f(x) say f1 and then
> take the derivative
> with respect to that. You know the substitution, so you can reverse
> it. Once the derivative
> is done, you reverse the substitution. Then, you can take the
> derivative with respect to
> x.
>
> So, the steps become.
>
> 1. Build list of symbols for functions.
> 2. Build a list of substitutions for the functions and the reverse.
> 3. Substitute for the functions.
> 4. Run derivatives with respect to the symbols.
> 5. Substitute functions for the symbols.
> 6. Take any derivatives with respect to the function variables.
>
> Hope that helps the explanation. A lot of what's done in the code is using
> zip and map to make the code fast, but I originally wrote it as above. Python
> has appropriate functions that this should still be fast.
>
> Cheers,
>
> Tim.
>
> --
> Tim Lahey
> PhD Candidate, Systems Design Engineering
> University of Waterloohttp://www.linkedin.com/in/timlahey
--
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.