This lesson is a preamble to the HyperPython short course, in which you will learn some basic theory of hyperbolic conservation laws and how to solve them numerically.
In this lesson, we'll learn about the Python programming language and the IPython notebook, as well as the Python packages numpy and matplotlib. If you're already familiar with these, you may skip to Lesson 1, where the real fun begins.
This isn't a very thorough introduction -- we'll just learn the things that are essential for the course. Some resources for learning more about Python and its use in scientific computing are linked at the end of this notebook.
To get through the course, you need not have used Python or studied hyperbolic PDEs before. But it is assumed that you have programmed in some language and that you are familiar with basic differential equations and numerical methods.
To run the code in this course, you'll need an installation of Python, numpy, matplotlib, and Clawpack. The easiest way to get them all is to use SageMathCloud -- just create a free account, start a new project, open a terminal, and type
git clone git@github.com:ketch/HyperPython.git
Open this notebook there and you're off.
You can also use Wakari, or install everything locally on your own machine. For local installation, Anaconda is convenient, or you can just use pip. All of these are free.
The code for this course is written in Python, which is a programming language designed to promote code that is easy to read and write. Python has become one of the most important languages in scientific computing. It is high-level like MATLAB, but unlike MATLAB it is free and is intended as a general-purpose language.
IPython is a collection of tools for interactive programming in Python. Most importantly for us, IPython includes an interactive shell and a browser-based notebook. The notebook (which you are using now) allows you to run Python code in your web browser; just click on a cell with code and hit shift+enter. Try it with the cell below.
print "The best conservation laws are hyperbolic!"
Try changing the message in the box and running it again.
The box of code above is called a cell. You can use the menu and toolbar near the top of your browser to insert or delete cells or save the notebook. The text you're reading is also in a cell, which you can edit (just double-click on this text). You'll also see math in these cells; the math is written using LaTeX. For example:
$$e^{i \pi} = -1.$$The menu bar above also has buttons to run cells or to stop code running.
You can find a huge collection of interesting IPython notebooks on a wide range of topics here. Since notebooks combine text, mathematics, and executable code, they're a great way to learn.
Running the code cell below will reformat the notebook to make things a bit easier to read, and will also help you keep track of running code later on. The same cell appears at the top of each of the later lessons in the course.
from IPython.core.display import HTML
css_file = './custom.css'
HTML(open(css_file, "r").read())
Now we'll get into the meat of Python. For this first lesson, I recommend that you open an IPython session in a terminal and type the code yourself, rather than just executing the code here in the notebook.
Python has many built-in functions, like print
, but most functions are inside modules, which aren't loaded unless you import
them:
from math import sqrt
print sqrt(2)
We can also import a whole module:
import math
print math.sqrt(2)
What else is in the math
module? You can find out using IPython's tab completion. Just put your cursor at the end of the line below and hit tab
:
math.
What does math.atan2
do, for instance? You can find out with IPython's magic help function, invoked by using the question mark after a function name. When you're done reading the help, you can close the pager below by clicking on the "x".
math.atan2?
A list is an ordered collection of values or objects.
x = [1,2,3,4]
You can ask for one or more items from a list as follows:
print x[0]
print x[1:3]
Two important things to remember:
You can quickly make lists of numbers using the range
function:
y = range(1,5)
print y
for i in range(5):
print i
print 'in the loop'
print 'finished'
A for loop just iterates over a list. Notice how the contents of the loop are indented. Python knows the loop has ended when it finds a line that is not indented.
To nest loops, indent again:
for i in range(5):
print i
for j in range(2):
print 'in the inner loop'
print 'finished'
Don't forget the colon!
def square(x):
return x**2
print square(5)
Notice how the contents of the function are indented (just like the for loop). Python knows the function has ended when it finds a line that is not indented.
Python includes a package for numerical computation called numpy, which will be an essential tool in this course. To get started, we import the numpy module. We will also tell Python that we want to refer to numpy by the short abbreviation "np":
import numpy as np
The most important Numpy class is the array
. You can make arrays just like you would in MATLAB:
x = np.linspace(0, 1, 5)
print x
y = np.arange(0, 1, 0.2)
print y
Like range
, arange
omits the final value.
Arrays are like lists, except that you can perform math with them in an easier and faster way:
print x+y
print x*y
The syntax for creating a multidimensional array in numpy is also similar to Matlab:
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print A
You can slice numpy arrays just as in Fortran 90 or Matlab, but (as with lists) the arrays are indexed from zero and you don't get the last element of a slice.
print A[0,0]
print A[1:3,0:2]
You can index in some slightly fancier ways, too:
print A[:,1:]
print A[-1,:]
You can do lots of other things with arrays. Type "A." (notice the period!) and press tab
to see some of them:
For plotting we will use the matplotlib package. It works a lot like MATLAB's plotting functions.
%matplotlib inline
import matplotlib.pyplot as plt
The line beginning with a "%" is referred to as a magic function. It makes plots appear in the browser, rather than in a separate window. If you want to know about all of IPython's magic functions, just type "%magic".
Now for a very simple example. Suppose we want to plot the function $\sin(\exp(x))$ on the interval $x\in(0,4)$. We'll use the numpy versions of the sine and exponential functions, which operate on arrays (the math module versions operate only on scalars):
x=np.linspace(0,4,1000)
f=np.sin(np.exp(x))
plt.plot(x,f)
We'll also see later how to make animations with Matplotlib and IPython.
Like MATLAB, Python is relatively slow -- especially when using loops with many iterations, nested loops, or deeply nested function calls. For the exercises in this course, Python will be sufficiently fast, but you should use numpy slicing whenever possible.
We will often need to compute the difference of each pair of successive entries of an array. Here are two ways to do it. Which is faster? We can find out using another "magic" function.
N = 1000000
a = np.random.rand(N)
b = np.zeros(N-1)
%%timeit
for i in range(len(b)):
b[i] = a[i+1]-a[i]
%%timeit
b = a[1:]-a[:-1]
For large-scale computational problems, you shouldn't use Python for any code that needs to be fast. Instead, you can write/generate code in C or Fortran and use weave, cython, f2py, or other similar packages to automatically incorporate that code into your Python program.
Toward the end of the course, we'll use the PyClaw package, which includes Fortran code and is built using f2py.
Be careful when dividing numbers in Python. If you divide with integers, Python may not do what you expect. In scientific computing, you usually want to use floating point numbers.
print 1/2
print 1.0/2.0
Hopefully this brief adventure has convinced you that Python is a worthwhile programming language. Some of the reasons I chose Python as the language for this course (and other courses I teach) are:
For a longer discussion with links to some scientific studies of the value of Python, see this blog post by Lorena Barba.
We've only scratched the surface of Python here. You'll learn more in the rest of the course -- and it's great to learn by doing. But if you want a really solid foundation, you may want to take a look at these:
Besides numpy and matplotlib, there are many other useful Python packages for scientific computing. Here is a short list: