AMath 586, Spring Quarter 2019 at the University of Washington. For other notebooks, see Index.ipynb or the [Index of all notebooks on Github].
(https://github.com/rjleveque/amath586s2019/blob/master/notebooks/Index.ipynb). The Python pdb module is useful for debugging Python code.
This notebook shows a simple example.
%matplotlib inline
from pylab import *
from scipy.linalg import expm
The code below is supposed to solve $u'(t) = Iu(t)$ with $u(0) = \eta$ in the $5\times 5$ case where $I$ is the identity matrix and all elements of $\eta$ are equal to 1.
def utrue(t):
# Solve u'(t) = A u(t) with A = I, u(0) = eta
A = eye(5) # 5x5 identity matrix
eta = array([1.,1.,1.,1.])
u = dot(expm(A*t),eta)
return u
utrue(1.)
The error message above some information about where the error occurred.
We can get more information by turning on the pdb debugger, using the Jupyter "magic" command pdb. If you execute this a second time, it turns the debugger off.
pdb
utrue(1.)
Note that it now gives a ipdb> prompt at the point where the exception occurred. You can query the state of variable, e.g. by typing an expresssion or a print statement and then hitting Enter. Type q and Enter to quit (which you have to do before you can execute any other cell).
pdb
You can also put in a breakpoint to probe the state at some point, even if it is not giving an error. Here's a corrected version of the code with a breakpoint added.
from pdb import set_trace
def utrue(t):
# Solve u'(t) = A u(t) with A = I, u(0) = eta
A = eye(5) # 5x5 identity matrix
eta = array([1.,1.,1.,1.,1.])
u = dot(expm(A*t),eta)
print('This breakpoint is after setting A, eta, and u')
set_trace() # breakpoint here
return u
utrue(1.)
You can end the (Pdb) prompt with q to quit, or if things are working right, with c to continue executing from this point.
If you happen to delete a cell when it's waiting for pdb input and the notebook hangs, remember that you can select Interrupt from the Kernel menu.