I have a small script (I am using colab notebooks) to compute the equations of motion for a double pendulum.
Here is what I have:
# First the libraries are imported.
import numpy as np
import sympy as sp
import sympy.physics.mechanics as mech
mech.init_vprinting()
# Symbols
g = sp.symbols('g', real=True, positive = True)
t = sp.symbols('t', real=True, positive = True)
L1 = sp.symbols('L1', real=True, positive = True)
L2 = sp.symbols('L2', real=True, positive = True)
m1 = sp.symbols('m1', real=True, positive = True)
m2 = sp.symbols('m2', real=True, positive = True)
A = sp.Function('A')(t)
B = sp.Function('B')(t)
#Position mass 1
X1 = L1*sp.sin(A)
Y1 = -L1*sp.cos(A)
#Position mass 2
X2 = L1*sp.sin(A) + L2*sp.sin(B)
Y2 = -L1*sp.cos(A) - L2*sp.cos(B)
# Potential energy
V = m1*g*Y1 + m2*g*Y2
# Kinetic energy
T = sp.Rational(1,2) * m1 * ( X1.diff(t)**2 + Y1.diff(t)**2 ) + sp.Rational(1,2) * m2 * ( X2.diff(t)**2 + Y2.diff(t)**2 )
T = T.simplify()
print(f'visualisation of the function through the print command: \n {T}')
print('\n This would be the visualisation of the same function, but writing the variable alone \n')
T
The issue I have is the following, I want to have in print the symbol $\dot{A}$
(A with a dot on top), instead of Derivative(A(t), t)
. I found this thread where they explain that one way is to add the lines:
import sympy.physics.mechanics as mech
mech.init_vprinting()
which I did, and it works if I just write the variable. However it does not work if I print the variable through the print command.
Then, the question would be: Is there a way to use the symbol with the dot on top instead of the derivative(A,t)
, but that works when in use with the print command?
This is what I get:
I try by using the lines import sympy.physics.mechanics as mech mech.init_vprinting()
, but I do not get the symbol \dot{A} on the expression, rather derivative(A,t)
There are two things here.
When using the sympy.physics.mechanics
module, for the Latex printer to work properly, the time variable must not have any assumption. So, replace t = sp.symbols('t', real=True, positive = True)
with t = sp.symbols('t')
or t = mech.dynamicsymbols._t
.
Addressing the string visualization in a robust and complete way is not easy. A proper solution would require some hooks into Python's internal, which is time consuming. If you could adapt your way of coding print statements, it would significantly simplify the task. Consider this command, for example:
print(f'visualisation of the function through the print command: \n {T}')
Here, I believe Python is going to format T
with str
and then combine it with the other string. If you could rewrite it without using f-strings, or format
or "string %s" % T
, we could create our custom printing method:
from sympy.physics.vector.printing import VectorStrPrinter
printer = VectorStrPrinter()
def myprint(*values, **kwargs):
v = " ".join([printer.doprint(v) for v in values])
print(v)
myprint('visualisation of the function through the print command: \n ', T)
# visualisation of the function through the print command:
# L1**2*m1*A'**2/2 + m2*(L1**2*A'**2 + 2*L1*L2*cos(A - B)*A'*B' + L2**2*B'**2)/2
Note that Derivative(A(t), t)
was replaced with A'
.