'Pretty print Expressions in Drake
Is there some built in function to print expressions in a readable way? For example, this expression;
<Expression "( - (x(2) * (2 * (x(0) * P(0,0)) + (x(1) * P(1,0)) + (x(1) * P(0,1)) + (x(2) * P(2,0)) + (x(2) * P(0,2)) + (x(3) * P(3,0)) + (x(3) * P(0,3)) + 2 * (lambda(0) * Q(0,0)) + 2 * (lambda(1) * Q(0,1)))) - (x(3) * ((x(0) * P(1,0)) + (x(0) * P(0,1)) + 2 * (x(1) * P(1,1)) + (x(2) * P(2,1)) + (x(2) * P(1,2)) + (x(3) * P(3,1)) + (x(3) * P(1,3)) + 2 * (lambda(0) * Q(1,0)) + 2 * (lambda(1) * Q(1,1)))) - ((9.8000000000000007 * x(1) + (x(0) * K(0)) + (x(1) * K(1)) + (x(2) * K(2)) + (x(3) * K(3))) * ((x(0) * P(2,0)) + (x(0) * P(0,2)) + (x(1) * P(2,1)) + (x(1) * P(1,2)) + 2 * (x(2) * P(2,2)) + (x(3) * P(3,2)) + (x(3) * P(2,3)) + 2 * (lambda(0) * Q(2,0)) + 2 * (lambda(1) * Q(2,1)))) - ((19.600000000000001 * x(1) + lambda(0) - lambda(1) + (x(0) * K(0)) + (x(1) * K(1)) + (x(2) * K(2)) + (x(3) * K(3))) * ((x(0) * P(3,0)) + (x(0) * P(0,3)) + (x(1) * P(3,1)) + (x(1) * P(1,3)) + (x(2) * P(3,2)) + (x(2) * P(2,3)) + 2 * (x(3) * P(3,3)) + 2 * (lambda(0) * Q(3,0)) + 2 * (lambda(1) * Q(3,1)))))">
Would be a lot easier to see if it was organized by the matrix and variable dot products.
Solution 1:[1]
If you're working in a notebook workflow, then we have a ToLatex method.
My favorite way to use it is, e.g.:
from IPython.display import Markdown, display
from pydrake.all import ToLatex, MultibodyPlant, Parser
from underactuated import FindResource, ManipulatorEquations
plant = MultibodyPlant(time_step=0)
parser = Parser(plant)
parser.AddModelFromFile(FindResource("models/double_pendulum.urdf"))
plant.Finalize()
# Evaluate the dynamics numerically
q = [0.1, 0.1]
v = [1, 1]
(M, Cv, tauG, B, tauExt) = ManipulatorDynamics(plant, q, v)
display(Markdown("$M = " + ToLatex(M, precision=2) + "$"))
display(Markdown("$Cv = " + ToLatex(Cv, precision=2) + "$"))
display(Markdown("$\\tau_G = " + ToLatex(tauG, precision=2) + "$"))
display(Markdown("$B = " + ToLatex(B, precision=2) + "$"))
display(Markdown("$\\tau_{ext} = " + ToLatex(tauExt, precision=2) + "$"))
as seen in the intro chapter notebook from my underactuated course notes: https://deepnote.com/project/52e7e101-429f-4aef-a373-e4cca7980cfe/%2Fintro.ipynb
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Russ Tedrake |

