{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to numpy and matplotlib\n", "\n", "In this tutorial, we'll focus on the two most important Python packages for scientific computing: **numpy** and **matplotlib**. \n", "\n", "First, a word about IPython: [IPython](http://www.ipython.org) is a collection of tools for interactive programming in [Python](http://www.python.org/). 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.\n", "\n", "## Numpy\n", "Python includes a package for numerical computation called numpy, which will be an essential tool in this course. You may find the following links helpful: \n", "\n", "- [Numpy tutorial](http://www.scipy.org/Tentative_NumPy_Tutorial) \n", "- [Numpy tutorial video](http://showmedo.com/videotutorials/series?name=i9KuJuNcG) \n", "- [NumPy: lock 'n load](http://mentat.za.net/numpy/intro/intro.html)\n", "\n", "If you want something really exhaustive, read either of the books by Hans Petter Langtangen (KAUST has access to the e-books). " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get started, we import the numpy module. Import statements like this are the typical way of getting access to functions in Python. Here we just import the numpy module and tell Python that we want to refer to numpy by the short abbreviation \"np\":" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Arrays\n", "\n", "The most important Numpy class is the ndarray, which is similar to an array in Matlab. \n", "Numpy includes basic utility functions for creating arrays that are similar to those in Matlab:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.linspace(0,1,5)\n", "print x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Try changing the inputs to the *linspace* function and see if you can determine exactly what it does.**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "y = np.arange(0,1,0.2)\n", "print y" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Try changing the inputs to the *arange* function to determine exactly what it does.** Another way to find out what a function does is to type its name, followed by a question mark:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.arange?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can resize the help window (to get it out of your way) by dragging the divider.\n", "\n", "The syntax for creating a multidimensional array in numpy is also similar to Matlab: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "A = np.array([[1,2,3],[4,5,6],[7,8,9]])\n", "print A\n", "print A.shape" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's easy to manipulate arrays in various useful ways: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "B = np.arange(1,10)\n", "print B\n", "print B.reshape((3,3))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that there are two pairs of parentheses used when calling the reshape command, because we are passing as argument the Python tuple (3,3), which is the shape of the resulting array." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indexing\n", "\n", "You can slice numpy arrays just as in Fortran 90 or Matlab, but the indexing is a little different. Can you figure out how it differs? Try the examples below:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print A[0,0]\n", "print A[1:3,0:2]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can index in some slightly fancier ways, too: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "print A[:,1:]\n", "print A[-1,:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Array and matrix multiplication\n", "How does multiplication of arrays work? Let's see: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "print M*M" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, numpy just multiplies componentwise. If you want to do matrix-matrix (or matrix-vector) multiplication, use the dot function: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.dot(M,M)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Linear algebra\n", "What else can you do with ndarrays? Besides basic arithmetic, there is a nice linear algebra package: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.linalg?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.linalg.norm(x)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use this package to solve a linear system of equations: $Mx=b$" ] }, { "cell_type": "code", "collapsed": false, "input": [ "M = np.array([[0,2],[8,0]])\n", "b = np.array([1,2])\n", "print M\n", "print b" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.linalg.solve(M,b)\n", "print x" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Can you think of an easy way to check that $x$ is the correct solution? Program your check in the box below.**" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also solve eigenvalue problems:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lamda,V = np.linalg.eig(M)\n", "print lamda\n", "print V" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how we have put two variables on the left of the equals sign, to assign the outputs of *eig()* to two different variables. What are the two outputs? **How can you check that these outputs are correct? Program it in the box below.**" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matplotlib\n", "For plotting we will use the [matplotlib](http://matplotlib.org/) package." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline\n", "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The line beginning with a \"%\" is referred to as a magic function. It will make it so that our 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\".\n", "\n", "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 want to use the numpy versions of the sine and exponential functions, which operate on arrays (the math module versions operate only on scalars): " ] }, { "cell_type": "code", "collapsed": false, "input": [ "x=np.linspace(0,4,1000)\n", "f=np.sin(np.exp(x))\n", "plt.plot(x,f)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We won't spend much time on matplotlib. It's straightforward to use and you will generally only get into its advanced functionality if you are customizing the look of a plot for a publication. There is a nice matplotlib tutorial [here](http://matplotlib.sourceforge.net/users/pyplot_tutorial.html). The matplotlib documentation in general is excellent. \n", "\n", "### Listing functions\n", "If you type the name of a Python variable or module, followed by a period, and press the Tab key, you'll get a list of many of the things you can do with that variable or module. Try it out:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Gotcha: copies and views \n", "See if you can figure out what's happening in the following code. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = np.arange(12)\n", "print a\n", "b=a\n", "b[5]=0\n", "print a" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Try playing around with some of the things you found in the box below. See if you can do the following: \n", "1. Create a 10x10 matrix whose $(i,j)$ entry is equal to $i\\times j$ without using a loop. Hint: use np.fromfunction(). \n", "2. Split your 10x10 matrix into five $10 \\times 2$ matrices with one line of code. Hint: use np.hsplit()**" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A warning about speed\n", "\n", "Like MATLAB, Python is **slow**! Like MATLAB, it is particularly slow when using **loops** with many iterations, nested loops, or deeply nested function calls. For the exercises in this course, Python will be sufficiently fast as long as you don't use loops excessively.\n", "\n", "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](http://cython.org/), [f2py](http://www.f2py.com/), or other similar packages to automatically incorporate that code into your Python program. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Other packages\n", "Besides numpy and matplotlib, there are many other useful Python packages for scientific computing. Here is a short list: \n", "\n", "- [scipy](http://www.scipy.org/) - optimization, ODEs, sparse linear algebra, etc.\n", "- [sympy](http://sympy.org/) - symbolic computation\n", "- [pandas](http://pandas.pydata.org/) - data analysis\n", "- [mpi4py](http://mpi4py.scipy.org/) - parallel computing\n", "- [petsc4py](http://code.google.com/p/petsc4py/), [pytrilinos](http://trilinos.sandia.gov/packages/pytrilinos/) - Python bindings for the \"big 2\" parallel scientific libraries\n", "- [pyCUDA](http://mathema.tician.de/software/pycuda), [pyOpenCL](http://mathema.tician.de/software/pyopencl) - GPGPU computing\n", "- [FENiCS](http://fenicsproject.org/), [FiPy](http://www.ctcms.nist.gov/fipy/), [PyClaw](http://clawpack.github.io/doc/pyclaw/) - solve complicated PDEs with very sophisticated numerical methods\n", "- [networkX](http://networkx.github.com/), [pygraphviz](http://networkx.lanl.gov/pygraphviz/) - graphs" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }