The goal of this project is to work through the Structure and Interpretation of Classical Mechanics book using Python and the sympy library.
Each chapter of the book will have a notebook dedicated in the notebooks folder.
So far I have only completed the first section.
from sympy import symbols, Rational, integrate, Function
from sympy import Matrix
x = Function('x')
y = Function('y')
z = Function('z')
t = symbols('t')
m = symbols('m')
q = Matrix([x(t), y(t), z(t)])
def velocity(local):
return Matrix(local[4:7])
def l_free_particle(mass):
def inner(local):
v = velocity(local)
return Rational(1,2) * mass * (v.transpose() * v)[0]
return inner
def gamma(q, t):
# Returns the local tuple
return Matrix([t, q, q.diff()])
def lagrangian_action(L, q, t1, t2):
return integrate(L(gamma(q,t)), (t, t1, t2))
test_path = Matrix([4 * t + 7, 3 * t + 5, 2 * t + 1])
test_path
$\displaystyle \left[\begin{matrix}4 t + 7\3 t + 5\2 t + 1\end{matrix}\right]$
l_free_particle(3.0)(gamma(q,t))
lagrangian_action(l_free_particle(3.0), test_path, 0.0, 10.0)