This example documents the displaytools extension for the IPython Notebook.


In [1]:
%load_ext displaytools
#%reload_ext displaytools

In [2]:
import sympy as sp
from sympy import sin, cos
from sympy.abc import t, pi

In [3]:
x = 2*pi*t
y1 = cos(x)
y2 = cos(x)*t
ydot1 = y1.diff(t) ##
ydot2 = y2.diff(t) ##
ydot1_obj = y1.diff(t, evaluate=False) ##


-2*pi*sin(2*pi*t)
---
-2*pi*t*sin(2*pi*t) + cos(2*pi*t)
---
Derivative(cos(2*pi*t), t)
---

Note that the equation sign (i.e., =) must be enclosed by two spaces, i.e.: lhs = rhs.


If the variable name is also desired this can be triggered by ##:


In [4]:
ydot1 = y1.diff(t) ##:
ydot2 = y2.diff(t) ##:
ydot1_obj = y1.diff(t, evaluate=False) ##:


ydot1 := -2*pi*sin(2*pi*t)
---
ydot2 := -2*pi*t*sin(2*pi*t) + cos(2*pi*t)
---
ydot1_obj := Derivative(cos(2*pi*t), t)
---

Printing can be combined with LaTeX rendering:


In [5]:
sp.interactive.printing.init_printing(1)

In [6]:
ydot1 = y1.diff(t) ##:
ydot2 = y2.diff(t) ##:
ydot1_obj = y1.diff(t, evaluate=False) ##:


ydot1 := $$- 2 \pi \sin{\left (2 \pi t \right )}$$
---
ydot2 := $$- 2 \pi t \sin{\left (2 \pi t \right )} + \cos{\left (2 \pi t \right )}$$
---
ydot1_obj := $$\frac{\partial}{\partial t} \cos{\left (2 \pi t \right )}$$
---

If there is no assignment taking place, ## nevertheless causes the display of the respective result.


In [7]:
y1.diff(t,t) ##
y2.diff(t,t) ##


$$- 4 \pi^{2} \cos{\left (2 \pi t \right )}$$
___
$$- 4 \pi \left(\pi t \cos{\left (2 \pi t \right )} + \sin{\left (2 \pi t \right )}\right)$$
___

Transposition

Sometimes, it can save much space if some return value is displayed in transposed form (while still being assigned not transposed).


In [8]:
xx = sp.Matrix(sp.symbols('x1:11')) ##T
yy = sp.Matrix(sp.symbols('y1:11')) ##:T
xx + yy ##T


$$\left[\begin{matrix}x_{1} & x_{2} & x_{3} & x_{4} & x_{5} & x_{6} & x_{7} & x_{8} & x_{9} & x_{10}\end{matrix}\right]$$
---
yy.T := $$\left[\begin{matrix}y_{1} & y_{2} & y_{3} & y_{4} & y_{5} & y_{6} & y_{7} & y_{8} & y_{9} & y_{10}\end{matrix}\right]$$
---
$$\left[\begin{matrix}x_{1} + y_{1} & x_{2} + y_{2} & x_{3} + y_{3} & x_{4} + y_{4} & x_{5} + y_{5} & x_{6} + y_{6} & x_{7} + y_{7} & x_{8} + y_{8} & x_{9} + y_{9} & x_{10} + y_{10}\end{matrix}\right]$$
___

Why this extension might be useful?

  • It saves space in the final document, when intermediate results shall be displayed (e.g. for didactic purpose)
    • allows to focus more on the content/results instead of boilerplate printing code
  • It saves typing effort during the development process (when much more internal information is of interest to understand and debug the code)