Getting Access to Python
A.5 Writing IPython Notebooks
A.4.2 Basic Intro to Wakari
After having logged in at thewakari.iosite, you automatically enter an IPython notebook with a short introduction to how the notebook can be used. Click on theNew Notebook button to start a new notebook. Wakari enables creating and editing plain Python files too: click on theAdd fileicon in pane to the left, fill in the program name, and you enter an editor where you can write a program. Pressing Executelaunches an IPython session in a terminal window, where you can run the program byrun prog.pyifprog.pyis the name of the program. To download the file, selecttest2.pyin the left pane and click on theDownload fileicon.
There is a pull-down menu where you can choose what type of terminal window you want: a plain Unix shell, an IPython shell, or an IPython shell with Matplotlib for plotting. Using the latter, you can run plain Python programs or commands with graphics. Just choose the type of terminal and click on+Tabto make a new terminal window of the chosen type.
A.4.3 Installing Your Own Python Packages
Both SageMathCloud and Wakari let you install your own Python packages. To install any packagepackagenameavailable atPyPi15, run
Terminal
pip install --user packagename
To install the SciTools package, which is useful when working with this book, run the command
Terminal
pip install --user -e \
git+https://github.com/hplgit/scitools.git#egg=scitools
A.5 Writing IPython Notebooks
The IPython notebook is a splendid interactive tool for doing science, but it can also be used as a platform for developing Python code. You can either run it locally on your computer or in a web service like SageMathCloud or Wakari. In- stallation on your computer is trivial on Ubuntu, just sudo apt-get install ipython-notebook, and also onWindows and Mac16 by using Anaconda or En- thought Canopy for the Python installation.
The interface to the notebook is a web browser: you write all the code and see all the results in the browser window. There are excellent YouTube videos on how to use the IPython notebook, so here we provide a very quick “step zero” to get anyone started.
15https://pypi.python.org/pypi
16http://ipython.org/install.html
A.5.1 A Simple Program in the Notebook
Start the IPython notebook locally by the commandipython notebookor go to SageMathCloud or Wakari as described above. The default input area is acellfor Python code. Type
g = 9.81 v0 = 5 t = 0.6
y = v0*t - 0.5*g*t**2
in a cell andrun the cellby clicking onRun Selected(notebook running locally on your machine) or on the “play” button (notebook running in the cloud). This action will execute the Python code and initialize the variablesg,v0,t, andy. You can then writeprint yin a new cell, execute that cell, and see the output of this state- ment in the browser. It is easy to go back to a cell, edit the code, and re-execute it.
To download the notebook to your computer, choose theFile – Download as menu and select the type of file to be downloaded: the original notebook format (.ipynbfile extension) or a plain Python program version of the notebook (.pyfile extension).
A.5.2 Mixing Text, Mathematics, Code, and Graphics
The real strength of IPython notebooks arises when you want to write a report to document how a problem can be explored and solved. As a teaser, open a new notebook, click in the first cell, and chooseMarkdownas format (notebook running locally) or switch fromCodeto Markdownin the pull-down menu (notebook in the cloud). The cell is now a text field where you can write text withMarkdown17 syntax. Mathematics can be entered as LATEX code. Try some text with inline math- ematics and an equation on a separate line:
Plot the curve $y=f(x)$, where
$$
f(x) = e^{-x}\sin (2\pi x),\quad x\in [0, 4]
$$
Execute the cell and you will see nicely typeset mathematics in the browser. In the new cell, add some code to plotf .x/:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline # make plots inline in the notebook x = np.linspace(0, 4, 101)
y = np.exp(-x)*np.sin(2*pi*x) plt.plot(x, y, ’b-’)
plt.xlabel(’x’); plt.ylabel(’y’)
17http://daringfireball.net/projects/markdown/syntax
A.5 Writing IPython Notebooks 217
Executing these statements results in a plot in the browser, see Fig.A.2. It was popular to start the notebook byipython notebook –pylabto import everything fromnumpyandmatplotlib.pyplotand make all plots inline, but the–pylab option is now officiallydiscouraged18. If you want the notebook to behave more as MATLAB and not use thenpandpltprefix, you can instead of the first three lines above write%pylab.
Fig. A.2 Example on an IPython notebook
18http://carreau.github.io/posts/10-No-PyLab-Thanks.ipynb.html
1. L. Baochuan. Introduction to Numerical Methods. 2015. http://en.wikibooks.org/wiki/
Introduction_to_Numerical_Methods.
2. O. Certik et al. SymPy: Python library for symbolic mathematics.http://sympy.org/.
3. S. D. Conte and C. de Boor. Elementary Numerical Analysis - An Algorithmic Approach.
McGraw-Hill, third edition, 1980.
4. I. Danaila, P. Joly, S. M. Kaber, and M. Postel. An Introduction to Scientific Computing.
Springer, 2007.
5. C. Greif and U. M. Ascher. A First Course in Numerical Methods. Computational Science and Engineering. SIAM, 2011.
6. D. W. Harder and R. Numerical Analysis for Engineering. 2015. https://ece.uwaterloo.ca/
~dwharder/NumericalAnalysis/.
7. ScientificPython software package.http://starship.python.net/crew/hinsen.
8. J. D. Hunter. Matplotlib: a 2D graphics environment. Computing in Science & Engineering, 9, 2007.
9. J. D. Hunter et al. Matplotlib: Software package for 2D graphics.http://matplotlib.org/.
10. E. Jones, T. E. Oliphant, P. Peterson, et al. SciPy scientific computing library for Python.
http://scipy.org.
11. J. Kiusalaas. Numerical Methods in Engineering with Python. Cambridge, second edition, 2014.
12. H. P. Langtangen. DocOnce publishing platform.https://github.com/hplgit/doconce.
13. H. P. Langtangen.A Primer on Scientific Programming with Python. Texts in Computational Science and Engineering. Springer, fifth edition, 2016.
14. H. P. Langtangen and J. H. Ring. SciTools: Software tools for scientific computing. https://
github.com/hplgit/scitools.
15. R. LeVeque. Finite Difference Methods for Ordinary and Partial Differential Equations:
Steady-State and Time-Dependent Problems. SIAM, 2007.
16. T. Lyche and J.-L. Merrien.Exercises in Computational Mathematics with MATLAB. Springer, 2014.
17. C. Moler. Numerical Computing with MATLAB. SIAM, 2004. http://se.mathworks.com/
moler/chapters.html.
18. S. Nakamura. Numerical Analysis and Graphic Visualization with Matlab. Prentice Hall, second edition, 2002.
19. T. E. Oliphant. Python for scientific computing. Computing in Science & Engineering, 9, 2007.
219