--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
Aaron Meurer
diff --git a/sympy/core/symbol.py b/sympy/core/symbol.py
index 635c2c5..e1d36b1 100644
--- a/sympy/core/symbol.py
+++ b/sympy/core/symbol.py
@@ -24,6 +24,11 @@ class Symbol(AtomicExpr, Boolean):
"""
+ def _eval_derivative(self, s):
+ from sympy.core.function import Derivative
+ print 'test'
+ return Derivative(self, s)
+
is_comparable = False
__slots__ = ['is_commutative', 'name']
Then I get
In [1]: diff(x, x)
test
Out[1]:
d
──(x)
dx
So it's definitely being called. It should be the same for any
subclass of Symbol.
The idea, for what you want to do, would be to make x.diff(t) return
some kind of object instead of 0 (i.e., make x kind of like x(t), but
the object returned would be a Symbol subclass, so that it can be
passed to diff()).
Aaron Meurer
That seems to be a recent "optimisation" from commit 2361dd86. You
should revert this to the old behaviour: call
expr._eval_derivative(symbol) in all cases.
Aaron Meurer
Renato
Aaron Meurer
I see. But then there needs to be some way to indicate when a
derivative is partial or total, right? Because dx/dt = x'(t) but
\partial x/\partial t = 0. A subclass of Symbol can handle the first
overloading _eval_derivative and free_symbols, and keeping "internal
variables" on which it depends (like time), The second is harder
though.
Renato
Aaron Meurer
I implemented Euler-Lagrange equations in Maple. I've put the code for
doing it below. Basically, I create symbols for each of the functions
and substitute these. Then, I take the derivative, and revert back.
This code is quite fast in Maple (at the time I originally wrote the
code, it was more than 2 orders of magnitude faster than Maple's
equivalent) and I think most of the functionality necessary is in
Python and SymPy. It should be fairly straightforward to convert to
SymPy. This code handles any number of variables (as a list), but it
only does the traditional Euler-Lagrange equation, not some of the
others that can arise due to Calculus of Variations.
I hope this helps.
Cheers,
Tim.
---
# Calculate the Mass (d/dt(dL_dqv)) and Stiffness (dL_dq) contributions to the
# Euler-Lagrange equation. Note that the Stiffness contribution does
not include the negative sign.
EulerLagrange := proc(Lagrangian::anything, variables::list)
local num_list, qv_name, vel_var, qv_subs, qv_unsubs, Lagrange_subs1,
Lagrange_subs2, dL_dqv1, dL_dqv2, dL_dqv, dL_dqvt, dL_dq, dL_dq1,
dL_dq2, dL_dq3, q_name, q_subs, q_unsubs:
# create a list of indices from 1 to the number of variables used in
the formulation
num_list := [seq(i,i=1..nops(variables))]:
# Define a list of generalized velocity and position variables
qv_name := map2(cat,qv,num_list):
q_name := map2(cat,q,num_list):
# Equate the time derivatives of the system variable to the
generalized velocities
# also define the reverse mapping
vel_var := map(diff,variables,t):
qv_subs := zip(equate,vel_var,qv_name):
qv_unsubs := zip(equate,qv_name,vel_var):
# Equate the generalized positions to the system variables and define
the reverse mapping
q_subs := zip(equate,variables,q_name):
q_unsubs := zip(equate,q_name,variables):
# Convert the Lagrangian to the generalized position and velocity variables
Lagrange_subs1 := subs(qv_subs,Lagrangian):
Lagrange_subs2 := subs(q_subs,Lagrange_subs1):
# Differentiate the Lagrangian with respect to the generalized
velocities and positions
dL_dqv1 := map2(diff,Lagrange_subs2,qv_name):
dL_dq1 := map2(diff,Lagrange_subs2,q_name):
# Revert back to the system variables
dL_dq2 := map2(subs,qv_unsubs,dL_dq1):
dL_dqv2 := map2(subs,qv_unsubs,dL_dqv1):
dL_dqv := map2(subs,q_unsubs,dL_dqv2):
dL_dq := map2(subs,q_unsubs,dL_dq2):
dL_dqvt := map(diff,dL_dqv,t):
# Return the two components of the Euler-Lagrange Equation
return (dL_dqvt, dL_dq):
end proc:
--
Tim Lahey
PhD Candidate, Systems Design Engineering
University of Waterloo
http://about.me/tjlahey
Sorry, I thought about one thing and wrote another. I was thinking
something like L(v, x, t) = v**2/2 - x**2/2, then take dL/dt = v *
v'(t) + x * x'(t), but ∂L/∂t = 0. I know one should explicit in a
partial derivative which other variables are to be held constant, but
this "convention" was used when I took classes on mechanics, and it
really make things shorter to write -- and anyway, how woud you write
(∂L/∂t)_{v, x} in sympy?
Renato
Using a class that overrides some methods will only be visible to
users when creating the variables, it could be something like "x =
DynamicSymbol(x, t)" instead of "x = Symbol(x)". All the rest would be
just the same, I believe. Is this too "user-annoying"?
Renato
I think this can be solved overloading the free_symbols method in your
class, making it always include the time variable, besides itself.
Renato