This is an example for the displaytools extension for the IPython Notebook.

The extension introduces some "magic" comments (like ## and ##: ) which trigger additional output (normally only the return value of the last line of a cell is printed). See Why is this useful?


In [17]:
%load_ext displaytools3
%reload_ext displaytools3


The displaytools3 extension is already loaded. To reload it, use:
  %reload_ext displaytools3

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

In [19]:
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{\left (2 \pi t \right )}$$
---
$$- 2 \pi t \sin{\left (2 \pi t \right )} + \cos{\left (2 \pi t \right )}$$
---
$$\frac{\partial}{\partial t} \cos{\left (2 \pi t \right )}$$
---

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 [20]:
ydot1 = y1.diff(t) ##:
ydot2 = y2.diff(t) ##:
ydot1_obj = y1.diff(t, evaluate=False) ##:


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

Printing can be combined with LaTeX rendering:


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

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


$$\texttt{ydot1} := - 2 \pi \sin{\left (2 \pi t \right )}$$
---
$$\texttt{ydot2} := - 2 \pi t \sin{\left (2 \pi t \right )} + \cos{\left (2 \pi t \right )}$$
---
$$\texttt{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 [23]:
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). Compare these examples:


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

xx.shape, yy.shape ##


$$\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]$$
---
$$\texttt{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 ( 10, \quad 1\right )$$
$$\left ( 10, \quad 1\right )$$
___

In [25]:
# combination with other comments
a = 3 # comment ##:


$$\texttt{a} := 3$$
---

In [28]:
# Multiline statements and indended lines are not yet supported:
a = [1, 
     2] ##:

if 1:
    b = [10, 20] ##:


c = [100, 200] ##:


$$\texttt{c} := \left [ 100, \quad 200\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 internal information is of interest to understand and debug the code)