is this correct ? to be honnest, it's my first docstring ...
On 10 nov, 16:50, Philippe <[email protected]> wrote: > from sympy import * > > t = Symbol('t') > > def partial_derivative(equa, param): > """param is a variable, must not be named 'param_sub' > >>> from sympy import * > >>> t = Symbol('t') > >>> k = Symbol('k') > >>> x = Function('x')(t) > >>> partial_derivative(k*diff(x, t)**2 + k * x, x) > k > >>> partial_derivative(k*diff(x, t)**2 + k * x, diff(x, t)) > 2*k*D(x(t), t)""" > > 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 the name 'param_sub' in the equation > >>> from sympy import * > >>> t = Symbol('t') > >>> k = Symbol('k') > >>> x = Function('x')(t) > >>> y = Function('y')(t) > >>> equa = diff(x, t)**2+diff(y, t)**2 + 5 * x > >>> dT_dqvt, dT_dq = lagrange(equa, [x, y]) > >>> print dT_dqvt > [2*D(x(t), t, t), 2*D(y(t), t, t)] > >>> print dT_dq > [5, 0] > >>> equa = k*diff(x, t)**2 + k * x > >>> dT_dqvt, dT_dq = lagrange(equa, [x]) > >>> print dT_dqvt > [2*k*D(x(t), t, t)] > >>> print dT_dq > [k]""" > > 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 > > if __name__ == "__main__": > import doctest > doctest.testmod() > > On 10 nov, 15:15, Andy Ray Terrel <[email protected]> wrote: > > > I think this would be a great contribution to the SymPy physics > > module. Can you put some usage doc strings? Then, we can put it in > > the appropriate places. > > > -- Andy > > > On Wed, Nov 10, 2010 at 3:10 AM, Philippe <[email protected]> wrote: > > > some usage if needed.. > > > this is my unittest file. > > > the lagrange and partial_derivative functions are in meca.py > > > > - - - - - - - - - - - - > > > > import unittest > > > from sympy import * > > > import meca > > > > class MyTest(unittest.TestCase): > > > > def test_lagrange_1(self): > > > t = Symbol('t') > > > x = Function('x')(t) > > > y = Function('y')(t) > > > dT_dqvt, dT_dq = meca.lagrange(diff(x, t)**2+diff(y, t)**2 + 5 > > > * x, [x, y]) > > > > dT_dqvt_0 = 2*diff(x, t, t) > > > dT_dqvt_1 = 2*diff(y, t, t) > > > > self.assertEquals(dT_dqvt, [dT_dqvt_0, dT_dqvt_1]) > > > self.assertEquals(dT_dq, [5, 0]) > > > > def test_lagrange_2(self): > > > t = Symbol('t') > > > k = Symbol('k') > > > x = Function('x')(t) > > > dT_dqvt, dT_dq = meca.lagrange(k*diff(x, t)**2 + k * x, [x]) > > > > dT_dqvt_0 = 2*k*diff(x, t, t) > > > > self.assertEquals(dT_dqvt, [dT_dqvt_0]) > > > self.assertEquals(dT_dq, [k]) > > > > def test_partial_derivative_1(self): > > > t = Symbol('t') > > > k = Symbol('k') > > > x = Function('x')(t) > > > dE_q = meca.partial_derivative(k*diff(x, t)**2 + k * x, x) > > > dE_q_sol = k > > > self.assertEquals(dE_q, dE_q_sol) > > > > def test_partial_derivative_2(self): > > > t = Symbol('t') > > > k = Symbol('k') > > > x = Function('x')(t) > > > dE_q = meca.partial_derivative(k*diff(x, t)**2 + k * x, > > > diff(x, t)) > > > dE_q_sol = 2*k*diff(x, t) > > > self.assertEquals(dE_q, dE_q_sol) > > > > if __name__ == '__main__': > > > unittest.main() > > > > - - - - - - - - - - - - > > > > On 9 nov, 17:18, Philippe <[email protected]> wrote: > > >> 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 > > > athttp://groups.google.com/group/sympy?hl=en. > > -- 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.
