Massimo Nocentini

May 2018: intro



Abstract
Translation of code in the introduction chapter of Functional Differential Geometry book.

In [46]:
from operator import attrgetter
from sympy import *
init_printing()


In [2]:
f, L, w = Function('f'), Function('L'), Function('w') # abstract, Lagrangian and path functions, respectively
t, q, q_point = symbols(r't q \dot{q}') # symbols for the Leibniz notation

In [3]:
Lagrangian_eq = Eq(Derivative(Derivative(L(t, q, q_point),q_point,evaluate=False),
                              t, evaluate=False) - L(t,q,q_point).diff(q),0)
Lagrangian_eq # compact Lagrangian equation, implicit indeed


Out[3]:
$$- \frac{\partial}{\partial q} L{\left (t,q,\dot{q} \right )} + \frac{\partial^{2}}{\partial \dot{q}\partial t} L{\left (t,q,\dot{q} \right )} = 0$$

In [4]:
Lagrangian_eq = Eq(Derivative(Subs(L(t, q, q_point).diff(q_point), [q,q_point], [w(t),w(t).diff(t)]),t) - 
                   Subs(L(t,q,q_point).diff(q), [q,q_point], [w(t),w(t).diff(t)]),0)
Lagrangian_eq


Out[4]:
$$\frac{d}{d t} \left. \frac{\partial}{\partial \dot{q}} L{\left (t,q,\dot{q} \right )} \right|_{\substack{ q=w{\left (t \right )}\\ \dot{q}=\frac{d}{d t} w{\left (t \right )} }} - \left. \frac{\partial}{\partial q} L{\left (t,q,\dot{q} \right )} \right|_{\substack{ q=w{\left (t \right )}\\ \dot{q}=\frac{d}{d t} w{\left (t \right )} }} = 0$$

In [5]:
Lagrangian_eq.doit() # a complex explosion by automatic computation


Out[5]:
$$- \frac{d}{d w{\left (t \right )}} L{\left (t,w{\left (t \right )},\frac{d}{d t} w{\left (t \right )} \right )} + \frac{d}{d t} w{\left (t \right )} \frac{d^{2}}{d w{\left (t \right )}d \frac{d}{d t} w{\left (t \right )}} L{\left (t,w{\left (t \right )},\frac{d}{d t} w{\left (t \right )} \right )} + \frac{d^{2}}{d \frac{d}{d t} w{\left (t \right )}d t} L{\left (t,w{\left (t \right )},\frac{d}{d t} w{\left (t \right )} \right )} + \frac{d^{2}}{d \frac{d}{d t} w{\left (t \right )}^{2}} L{\left (t,w{\left (t \right )},\frac{d}{d t} w{\left (t \right )} \right )} \frac{d^{2}}{d t^{2}} w{\left (t \right )} + \left. \frac{\partial}{\partial \xi_{2}} L{\left (t,\xi_{2},\frac{d}{d t} w{\left (t \right )} \right )} \right|_{\substack{ \xi_{2}=w{\left (t \right )} }} = 0$$

In [6]:
def diff_positional(f, i):
    a = IndexedBase('a')
    der = f.diff(f.args[i])
    def D(*args):
        #return Lambda([a[i] for i in range(len(f.args))], 
        return der.subs({f.args[i]:args[i] for i in range(len(f.args))}, simultaneous=True)
    return D # function D is a function of the meta-language, not in the object language

In [7]:
diff_positional(L(t, q, q_point), 2)(t,w(t),w(t).diff(t)) # :/


Out[7]:
$$\left. \frac{\partial}{\partial \dot{q}} L{\left (t,w{\left (t \right )},\dot{q} \right )} \right|_{\substack{ \dot{q}=\frac{d}{d t} w{\left (t \right )} }}$$

In [8]:
def diff_positional(f, i):
    return Function(r'\partial_{{{}}}{}'.format(i, str(f))) # the \partial is just a symbol, it hasn't meaning

In [9]:
(Derivative(diff_positional(L, 2)(t,w(t), w(t).diff(t)), t) - 
 diff_positional(L, 1)(t,w(t), w(t).diff(t))) # :(


Out[9]:
$$- \partial_{1}L{\left (t,w{\left (t \right )},\frac{d}{d t} w{\left (t \right )} \right )} + \frac{d}{d t} \partial_{2}L{\left (t,w{\left (t \right )},\frac{d}{d t} w{\left (t \right )} \right )}$$

In [10]:
Lagrangian_eq = Eq(Derivative(L(t,w(t), w(t).diff(t)).fdiff(argindex=3),t, evaluate=False) - 
                   L(t,w(t), w(t).diff(t)).fdiff(argindex=2),
                   0, evaluate=False)
Lagrangian_eq # :) , although it doesn't use "positional derivative" operator explicitly


Out[10]:
$$\frac{d}{d t} \left. \frac{\partial}{\partial \xi_{3}} L{\left (t,w{\left (t \right )},\xi_{3} \right )} \right|_{\substack{ \xi_{3}=\frac{d}{d t} w{\left (t \right )} }} - \left. \frac{\partial}{\partial \xi_{2}} L{\left (t,\xi_{2},\frac{d}{d t} w{\left (t \right )} \right )} \right|_{\substack{ \xi_{2}=w{\left (t \right )} }} = 0$$

In [11]:
def Derivative_eq(f, argindex=1):
    d = Dummy()
    args = [d if i+1 == argindex else a for i,a in enumerate(f.args)]
    lhs = f.fdiff(argindex)
    rhs = Subs(f.func(*args).diff(d), d, f.args[argindex-1])
    return Eq(lhs, rhs, evaluate=False)

Derivative_eq(f(t,q,q_point),2) # applicative Derivative operator


Out[11]:
$$\frac{\partial}{\partial q} f{\left (t,q,\dot{q} \right )} = \left. \frac{\partial}{\partial Dummy_{143}} f{\left (t,Dummy_{143},\dot{q} \right )} \right|_{\substack{ Dummy_{143}=q }}$$

In [12]:
def Gamma(w):
    return Lambda([t], (t, w(t), w(t).diff(t))) # state-space function

In [13]:
Gamma(w), Gamma(w)(t)


Out[13]:
$$\left ( \left( t \mapsto \left ( t, \quad w{\left (t \right )}, \quad \frac{d}{d t} w{\left (t \right )}\right ) \right), \quad \left ( t, \quad w{\left (t \right )}, \quad \frac{d}{d t} w{\left (t \right )}\right )\right )$$

In [14]:
Lagrangian_eq = Eq(Derivative(L(*Gamma(w)(t)).fdiff(argindex=3),t, evaluate=False) - 
                   L(t,w(t), w(t).diff(t)).fdiff(argindex=2),
                   0, evaluate=False)
Lagrangian_eq


Out[14]:
$$\frac{d}{d t} \left. \frac{\partial}{\partial \xi_{3}} L{\left (t,w{\left (t \right )},\xi_{3} \right )} \right|_{\substack{ \xi_{3}=\frac{d}{d t} w{\left (t \right )} }} - \left. \frac{\partial}{\partial \xi_{2}} L{\left (t,\xi_{2},\frac{d}{d t} w{\left (t \right )} \right )} \right|_{\substack{ \xi_{2}=w{\left (t \right )} }} = 0$$

In [15]:
class FunctionsComposition(Function):

    nargs = 2
    
    def _latex(self, sexp):
        return r' \circ '.join(map(latex, 
                                   map(lambda a: a.func if isinstance(a, Function) else a, 
                                       self.args)))
    
    def _eval_subs(self, old, new):
        f, g = self.args
        if old == f:
            return new.subs({f.args[0]:g}, simultaneous=True)

In [22]:
F_o_w = FunctionsComposition(Function(r'\mathcal{F}')(t), w(t))
F_o_w


Out[22]:
$$\mathcal{F} \circ w$$

In [23]:
F_o_w.subs({Function(r'\mathcal{F}')(t):1/(1-t)})


Out[23]:
$$\frac{1}{- w{\left (t \right )} + 1}$$

In [24]:
F_o_w.subs({w(t):2*t})


Out[24]:
$$\mathcal{F} \circ 2 t$$

In [25]:
_.subs({Function(r'\mathcal{F}')(t):2*t+1})


Out[25]:
$$4 t + 1$$

In [26]:
FunctionsComposition(L(t, w(t), w(t).diff(t)).fdiff(argindex=3),Gamma(w))


Out[26]:
$$\left. \frac{\partial}{\partial \xi_{3}} L{\left (t,w{\left (t \right )},\xi_{3} \right )} \right|_{\substack{ \xi_{3}=\frac{d}{d t} w{\left (t \right )} }} \circ \left( t \mapsto \left ( t, \quad w{\left (t \right )}, \quad \frac{d}{d t} w{\left (t \right )}\right ) \right)$$

In [40]:
m,v,k,q = symbols('m v k q')
Lagrangian_eq.subs({L:Lambda([t,q,v], (m*v**2)/2-(k*q**2)/2)}) # plug in the Lagrangian function


Out[40]:
$$\frac{\partial}{\partial t} \left. \frac{\partial}{\partial \xi_{3}}\left(\frac{\xi_{3}^{2} m}{2} - \frac{k}{2} w^{2}{\left (t \right )}\right) \right|_{\substack{ \xi_{3}=\frac{d}{d t} w{\left (t \right )} }} - \left. \frac{\partial}{\partial \xi_{2}}\left(- \frac{\xi_{2}^{2} k}{2} + \frac{m}{2} \left(\frac{d}{d t} w{\left (t \right )}\right)^{2}\right) \right|_{\substack{ \xi_{2}=w{\left (t \right )} }} = 0$$

In [41]:
eq = _.doit() # do derivatives
eq


Out[41]:
$$k w{\left (t \right )} + m \frac{d^{2}}{d t^{2}} w{\left (t \right )} = 0$$

In [42]:
a, omega, phi = symbols(r'a \omega \phi')
_.subs({w(t):a*cos(omega*t+phi)})


Out[42]:
$$a k \cos{\left (\omega t + \phi \right )} + m \frac{\partial^{2}}{\partial t^{2}} \left(a \cos{\left (\omega t + \phi \right )}\right) = 0$$

In [43]:
_.doit().factor()


Out[43]:
$$a \left(- \omega^{2} m + k\right) \cos{\left (\omega t + \phi \right )} = 0$$

In [44]:
solve(_, omega)


Out[44]:
$$\left [ - \sqrt{\frac{k}{m}}, \quad \sqrt{\frac{k}{m}}, \quad \frac{1}{t} \left(- \phi + \frac{\pi}{2}\right), \quad \frac{1}{t} \left(- \phi + \frac{3 \pi}{2}\right)\right ]$$

In [45]:
dsolve(eq, w(t)) # solve with respect to path function w(t)


Out[45]:
$$w{\left (t \right )} = C_{1} e^{- t \sqrt{- \frac{k}{m}}} + C_{2} e^{t \sqrt{- \frac{k}{m}}}$$