Warm-up -- a few new tricks

Variables can point to generic objects, be they integers, floats, strings, list, or even functions. This last case is particular useful as it allows us to create and pass functions around to other functions. def is the usual way to create functions (see above), but for short functions, there's a sweet trick called lambda-forms, in honor of Alonzo Church's Lambda calculus.

>>> f = lambda x : x**2
>>> f(1)
>>> f(2)
>>> map(f, range(10))

Two other useful hints, if you haven't seen them yet. First, you can use the up and down arrows in the python intepretter to navigate through old commands. Second, _ stands for result of the last calculation you performed. This can be very useful when working interactively, but should never be used in a script file. Try it ...

>>> 5**.5
>>> (_ - 1)*(_ + 1)

Better python tools - editors, interpretters, help

In practice, people don't use "python" for interactive work -- just to run their programs. If you put a bunch of python commands in a file "mycmds.py" and do

    $ python mycmds.py

those commands will be run, and you'll see any results from print commands or other output functions.


Many people like me use fancy text-editors to program, which is the root of the editor wars.

Python shells (python, ipython, jupyter)

The standard interpretter for the python language is called python also. You used it from a terminal window yesterday.

For fancier work, we usually use a tool with more bells and whistles. The most commonly used interpretor for development work is ipython. It colorizes input, tracks input and output for future reference, allows customization, and a bunch of other useful things. (Ipython uses a different prompt from ">>>". I'll keep using ">>>" from here on, just to signify python input, but you can type the commands into any python interpretor).

Recently, Enthought has pulled together a nice GUI called Canopy for interactive scientific computing with python. Canopy is free to students at university like you, and is what we'll mostly use. There is also an alternative, called Anaconda by Continuum Analytics.

Canopy and Anaconda include a 3rd interpretter that can be very useful. It is called jupyter, and provided a notebook-like environment in a web browsing window that can be used not just for python, but also for other languages.

Writting your first script

Because it is tedious to write out lengthy instructions every time you want to program a task, we usually save our commands to a file called a "script". Below is a simple program that prints a table for converting between Celcius and Farenheit.

  1. Open up "Canopy" (it is slow starting up the first time - be patient).

  2. Go to an "Editor" window. There will be several sub-windows. On the left is a file browser. On the top is an editor window. On the bottom is a command line interprettor window. Up top there is a button to "run" your code.

  3. Enter the following code into the editor window. This is what we call a script.

    print '-- My Celcius-to-Farenheit table -----'
    C = -20
    dC = 5
    while C <= 40:
        F = (9.0/5)*C + 32
        print C, F
        C = C + dC
    print '--------------------------------------'

    The new thing in this code is that it uses a "while"-loop. A while-loop tells the computer to keep doing a particular thing as long as the condition it is given holds true. In this case, as long as the temperature in C is less than or equal to 40 degrees.

  4. Save your new script as "c2f.py". Be sure to keep track of which directory you are saving it to. The Desktop is usually the best place at first. Hint: Generally, it is best not to include spaces or punctuation other than '_' or '.' in file names.

  5. Make a prediction about what you think will happen when you run this script.

  6. Click the "run" button to run the script. Does the output match your predictions?

  7. You can also run your script from the terminal without having to use Canopy. In a new terminal window, "cd" to the directory where you saved "c2f.py" and do

    $ python c2f.py

    The output you get should be the same as what you saw in Canopy.

  8. Switch back to Canopy.


On the Desktop, grab your new scripts and drag them to your PASS directory. If you do not save your scripts like this, they may be lost and gone forever after you log out.

A very good idea is to get a 500 mg or larger USB stick, on which you can keep all your python work with you at all times! That way, you can drop into any lab or computer and work at any time if you feel like it.

Python modules for doing calculations

In python, a "module" is a collection of related algorithms that people have found useful. Modules are like libraries in other languages. You can load a module into your python interpretter or shell script with the command "import"...

>>> import scipy

You can use help to see the contents of the module you have imported.

>>> help(scipy)

To use one of the functions from the module, you put the modules name as a prefix, like

>>> scipy.exp(0.)

Import submodules the same way, but with a "." used to separate names.

>>> import scipy.integrate

Function names can also be imported directly into the working namespace, although this can be dangerous and is discouraged.

>>> from scipy import exp
>>> exp(0.), exp(1.)

Arrays and matrices

Python calls vectors and matrices "arrays", You can create a vector with array([1.,2.,3.]), and a matrix with array([[1,2],[-2,3]]). Arrays are part of the numpy package (see below for link).

Matrix multiplication with arrays works a little different than you might expect. If A is an array and you type A * A, this does element-wise multiplication (known as the Haddamard or Matlab product). To do a matrix product, youhave to use "A.dot(A)".

>>> import numpy
>>> A = numpy.array([[1,2],[-2,3]])
>>> b = numpy.array([[5],[2]])
>>> A*A
>>> A.dot(A)
>>> A*b
>>> A.dot(b)

Here are some important modules that you should know about.

Getting help

One of the big improvements of python over preceding languages was the use of in-line documentation of code. This can be accessed in two easy ways.

Inside the python interpreter, documentation on functions and variable types can be found using the function help().

>>> help(range)
>>> x = 3
>>> help(x)

The help function is your friend. Use it often.

From the standard terminal, documentation can be found using the command pydoc.

$ pydoc topics list
$ pydoc keywords if
$ pydoc time

Symbolic computations

Like Maxima, Maple, and Mathematica, python can also do symbolic mathematical calculations, thanks to the sympy module. sympy is still in development and incomplete, but can already solve a wide variety of problems.

To do so, first we import the full sympy package. Then, we create any symbols we need. Once we have our symbols, we can do calculations.

>>> from sympy import *
>>> x,t = symbols('x,t')
# solve a cubic equation
>>> p = x ** 3 + 4*x ** 2 - 7*x - 10
>>> pprint(p)
>>> print solve(p,x)
# differentiate a cubic equation
>>> pprint(p.diff(x))
# we can also do many common integrations
>>> integrate(p,x)
>>> integrate(1/p,x)
# we can, for instance, check the fundamental theorem of calculus
>>> eq = 3 * x ** 2 + 3 - 5 * sin(2 * x)
>>> eq
>>> deq = eq.diff(x)
>>> deq
>>> ideq = integrate(deq,x)

So sympy is a free way to do all your calculus homework if you learn how to use it. If you use the right fonts, everything will come out looking very much like LaTex equations do. In fact, sympy will automatically convert your equations into LaTex markup for you!

>>> J = Integral( exp(x)/(1+x**2), (x,-1,2))
>>> J
>>> pprint(J)
>>> print latex(J)

The function pprint does "pretty printing" of mathematical expressions to make them easier to read on the screen. Unfortunately, it still messes things up a little sometimes. In general, such type setting is un-reliable, which is why we still use LaTex typesetting.

Here's another example.

>>> A = Matrix([[1,2],[3,0]])
>>> A
>>> pprint(A)
>>> A**-1

Multiplication of sympy matrices works the way you might expect matrix multiplication to work. If we multiply a matrix by its inverse, we expect to get the identity matrix, and we do.

>>> A * (A**-1)
>>> (A**-1) * A

To try to solve an ordinary differential, use dsolve.

>>> dsolve( x(t).diff(t) - x(t)*t, x(t))


So, we human beings have been doing calculations for a while now, and have developed some good algorithms to answer a variety of useful questions. Fortunately, you can take advantage of all of this hard work without having to recreate the wheel yourselves.

Record your answers to these questions as you go, LaTex them up, and send them to me in a PDF file before Friday. Remember to include a header with name and date.

  1. Find the approximate value of the uniq real solution of the transcendental equation ex+2x=0 using the bisection algorithm in scipy.optimize.bisect and scipy.optimize.ridder. (note that they give slightly different answers).

  2. Find the argument value x of the function f(x)=e2x3x where the global minimum occurs using scipy.optimize.golden. Use calculus to check the solution.

  3. Find the area under the curve 2+sin(sin(x)+x) over the interval [0,10] using scipy.integrate.quadrature.

  4. Find all the roots of the cubic polynomial equation x310x2+31x30=0. using the function numpy.roots()

  5. Find the uniq solution of the system of linear equations 10=11x5y, 16=3x7y6z, 12=14x6y+3z by converting the equations to matrix-vector form and using scipy.linalg.solve

  6. Find the slope and y-intercept of line y=mx+b that minimizes the square error of going through the four points (0,0), (9,9), (5,7), (6,5) by setting up the matrix system and using numpy.linalg.lstsq.

  7. Using sympy, find the steady-states of the system of differential equations and determine their local stability x.=rx(1x)βxyy.=βxyγy

  8. Calculate the first 10 decimal place of π from first principles.