{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Scientific Libraries with Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although coding with Python is very versatile and allows many advanced features that are useful when manipulating massive data (a common task in science), Python is still a multipurpose language, what implies that scientific routines and functions cannot (should not) be supported within its basic core. Nevertheless, there are many different scientific libraries that can extend the capabilities of Python to scientific implementations in a natural way. Two of the most used libraries are NumPy and SciPy, intended for manipulating mathematical objects more efficiently and for extending and including numerical methods, respectively. Another less used libraries like SymPy are intended for manipulating analytical expressions, i.e. a CAS (Computer Algebraic System).\n", "\n", "\n", "Installation of these libraries is often an easy task. In most of the Linux distros you should find them in the official repositories." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Official Pages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See the official pages of the libraries for new versions, news and manuals.\n", "\n", "**NumPy:**\n", "\n", "[http://www.numpy.org/](http://www.numpy.org/)\n", "\n", "**SciPy**\n", "\n", "[http://www.scipy.org/](http://www.scipy.org/)\n", "\n", "**SymPy**\n", "\n", "[http://www.sympy.org/en/index.html](http://www.sympy.org/en/index.html)\n", "\n", "**Anaconda**\n", "\n", "[https://store.continuum.io/cshop/anaconda/](https://store.continuum.io/cshop/anaconda/)\n", "\n", "*Anaconda is an interesting proposal that integrates many standard scientific libraries with Python*\n", "\n", "\n", "There are many different scientific libraries for Python with many different uses, even for very specific tasks. However, as we are interested in general numerical methods, we will focus only on NumPy and Scipy.\n", "\n", "- - - " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Official Pages](#Official-Pages)\n", "- [**NumPy**](#NumPy)\n", " - [Basic Use](#Basic-Use)\n", " - [Importing and basic math](#Importing-and-basic-math)\n", " - [Lists vs NumPy arrays](#Lists-vs-NumPy-arrays)\n", " - [Advanced features of arrays](#Advanced-features-of-arrays)\n", " - [Miscellaneous Functions](#Miscellaneous-Functions)\n", "- [**SciPy**](#Scipy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - " ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "
\n", " \n", "
\n", "\n", "\n", "NumPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NumPy is the fundamental package for scientific computing with Python. It contains among other things:\n", "\n", "* a powerful N-dimensional array object\n", "* sophisticated (broadcasting) functions\n", "* tools for integrating C/C++ and Fortran code\n", "* useful linear algebra, Fourier transform, and random number capabilities\n", "\n", "Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data. Arbitrary data-types can be defined. This allows NumPy to seamlessly and speedily integrate with a wide variety of databases." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Basic Use" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Importing and basic math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NumPy can be imported in several different ways:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Importing all the functions of NumPy\n", "from numpy import *" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "#Importing NumPy as numpy\n", "import numpy" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "#Importing NumPy with the alias of np (recommended)\n", "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If imported with an alias, NumPy functions and features are accessed by using the alias before" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Basic functions\n", "print np.exp(10.5), np.log(52.3), np.log10(63.9), np.sqrt(10.0)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "36315.5026742 3.95699637107 1.80550085816 3.16227766017\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "#Trigonometric functions\n", "print np.sin(5.0), np.cos(9.6), np.arcsin(0.5), np.arctan(5)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " -0.958924274663 -0.984687855794 0.523598775598 1.37340076695\n" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The value of PI is %1.6f\"%( np.pi )" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The value of PI is 3.141593\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Lists vs NumPy arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Supported methods for lists**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x.append x.count x.extend x.index x.insert x.pop x.remove x.reverse x.sort" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Supported methods for NumPy arrays**\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "x.T x.clip x.dot x.item x.prod x.setfield x.take\n", "x.all x.compress x.dtype x.itemset x.ptp x.setflags x.tofile\n", "x.any x.conj x.dump x.itemsize x.put x.shape x.tolist\n", "x.argmax x.conjugate x.dumps x.max x.ravel x.size x.tostring\n", "x.argmin x.copy x.fill x.mean x.real x.sort x.trace\n", "x.argsort x.ctypes x.flags x.min x.repeat x.squeeze x.transpose\n", "x.astype x.cumprod x.flat x.nbytes x.reshape x.std x.var\n", "x.base x.cumsum x.flatten x.ndim x.resize x.strides x.view\n", "x.byteswap x.data x.getfield x.newbyteorder x.round x.sum \n", "x.choose x.diagonal x.imag x.nonzero x.searchsorted x.swapaxes\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Common**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Lists and numpy arrays can both store any type of data\n", "x1 = [1.2, 3.5, 1.9]\n", "x2 = np.array([1.6, -2.6, 6.9])\n", "print x1, x2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1.2, 3.5, 1.9] [ 1.6 -2.6 6.9]\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "#For lists, new elements can be added using append method\n", "x1 = [1.2, 3.5, 1.9]\n", "x1.append(5.9)\n", "\n", "#For arrays, new elements can be added using append function of NumPy\n", "x2 = np.array([1.6, -2.6, 6.9])\n", "x2 = np.append(x2,3)\n", "\n", "print x1,x2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1.2, 3.5, 1.9, 5.9] [ 1.6 -2.6 6.9 3. ]\n" ] } ], "prompt_number": 24 }, { "cell_type": "code", "collapsed": false, "input": [ "#A list can be converted into a numpy array\n", "x = [1.1,3.4,1.0]\n", "x = np.array(x)\n", "#And a numpy array into a list as well\n", "x = list(x)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Lists**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Operator + for lists is overloaded for concatenating \n", "x1 = [1,2,3]\n", "x2 = [3,2,1]\n", "print x1+x2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 2, 3, 3, 2, 1]\n" ] } ], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "#Operator + for numpy arrays is overloaded for adding\n", "x1 = np.array([1,2,3])\n", "x2 = np.array([3,2,1])\n", "print x1+x2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[4 4 4]\n" ] } ], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "#Lists do not support other operators\n", "x1 = [1.2,4.8,6.9]\n", "x2 = [2.6,2.8,1.1]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "#Multiplication\n", "print x1*x2" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'list'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mx1\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mx2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'list'" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "#Division\n", "print x1/x2" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for /: 'list' and 'list'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m#Division\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mx1\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mx2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'list' and 'list'" ] } ], "prompt_number": 19 }, { "cell_type": "code", "collapsed": false, "input": [ "#Subtraction\n", "print x1-x2" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for -: 'list' and 'list'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mx1\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mx2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'list' and 'list'" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "#Power\n", "print x1**x2" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for ** or pow(): 'list' and 'list'", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mx1\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mx2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'list' and 'list'" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**NumPy arrays**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Numpy arrays support any mathematical operation (element by element)\n", "x1 = np.array([1.2,4.8,6.9])\n", "x2 = np.array([2.6,2.8,1.1])\n", "\n", "print \"Adding\", x1+x2\n", "print \"Multiplication\", x1*x2\n", "print \"Division\", x1/x2\n", "print \"Subtraction\", x1-x2\n", "print \"Power\", x1**x2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Adding [ 3.8 7.6 8. ]\n", "Multiplication [ 3.12 13.44 7.59]\n", "Division [ 0.46153846 1.71428571 6.27272727]\n", "Subtraction [-1.4 2. 5.8]\n", "Power [ 1.6064649 80.81192733 8.37016462]\n" ] } ], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note:** Matrices can be represented as NumPy arrays where each element is a row vector. Nevertheless, be careful when multiply arrays, the operator * is overloaded in such a way that single elements are multiplied one by one, what is different than multiplication of matrices." ] }, { "cell_type": "code", "collapsed": false, "input": [ "A = np.array([[1,2],[3,4]])\n", "B = np.array([[4,3],[2,1]])\n", "print A \n", "print B\n", "print A*B" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[1 2]\n", " [3 4]]\n", "[[4 3]\n", " [2 1]]\n", "[[4 6]\n", " [6 4]]\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Advanced features of arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NumPy arrays have a number of interesting features that facilitate complex tasks. Next it is shown some of the more useful ones." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#It is possible to access elements of a numpy array using booleans\n", "x = np.array([1,2,3,4])\n", "y = np.array([True, False, True, False])\n", "x[y]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 26, "text": [ "array([1, 3])" ] } ], "prompt_number": 26 }, { "cell_type": "code", "collapsed": false, "input": [ "#Operators >, <, >=, <= and == are also overloaded for numpy arrays\n", "x = np.array([0,5,8,0])\n", "y = np.array([0,6,5,1])\n", "print x>y\n", "print x4" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[False False True False]\n", "[False True False True]\n", "[ True False False False]\n", "[False True True False]\n" ] } ], "prompt_number": 32 }, { "cell_type": "code", "collapsed": false, "input": [ "#Combining these features, we can perform searches and comparisons far more efficient\n", "x = np.array([1,4,2,6,8,4,3,0,9,1,3,6,7,])\n", "#A new list with numbers greater than 4\n", "print x[x>4]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[6 8 9 6 7]\n" ] } ], "prompt_number": 33 }, { "cell_type": "code", "collapsed": false, "input": [ "#Native methods of numpy arrays allow to calculate basic quantities\n", "x = np.array([1,4,2,6,8,4,3,0,9,1,3,6,7,])\n", "#Maximum element\n", "print \"Maximum element\", x.max()\n", "#Minimum element\n", "print \"Minimum element\", x.min()\n", "#Sorted arguments of the array\n", "print \"Sorted arguments\", x.argsort()\n", "#Sorted array\n", "print \"Sorted array\", x[x.argsort()]\n", "#Mean value\n", "print \"Mean value\", x.mean()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Maximum element 9\n", "Minimum element 0\n", "Sorted arguments [ 7 0 9 2 6 10 1 5 3 11 12 4 8]\n", "Sorted array [0 1 1 2 3 3 4 4 6 6 7 8 9]\n", "Mean value 4.15384615385\n" ] } ], "prompt_number": 49 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Miscellaneous Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "NumPy includes many general purpose functions that complement the capabilities of python. We are interested here specially in functions for creating ordered arrays, storing and loading data as well as histograms, tasks that will be continuously required for the activities of the course." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Create an array of 1's with a given size (even 2D sizes)\n", "x = np.ones(5)\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 1. 1. 1. 1. 1.]\n" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "#Create an array of zeros with a given size (even 2D sizes)\n", "x = np.zeros( (2,5) )\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 0. 0. 0. 0. 0.]\n", " [ 0. 0. 0. 0. 0.]]\n" ] } ], "prompt_number": 17 }, { "cell_type": "code", "collapsed": false, "input": [ "#Create an array with a given range and a number of intervals\n", "x = np.linspace( -np.pi, np.pi, 10 )\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[-3.14159265 -2.44346095 -1.74532925 -1.04719755 -0.34906585 0.34906585\n", " 1.04719755 1.74532925 2.44346095 3.14159265]\n" ] } ], "prompt_number": 21 }, { "cell_type": "code", "collapsed": false, "input": [ "#Create an array with a given range and a given step\n", "x = np.arange( 1, 5, 0.2 )\n", "print x" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 1. 1.2 1.4 1.6 1.8 2. 2.2 2.4 2.6 2.8 3. 3.2 3.4 3.6 3.8\n", " 4. 4.2 4.4 4.6 4.8]\n" ] } ], "prompt_number": 22 }, { "cell_type": "code", "collapsed": false, "input": [ "#Using the function savetxt, it is possible to store data from a numpy array\n", "data = np.array([[3.2, 2.1],[3.1, 4.1]])\n", "np.savetxt( \"file.dat\", data, fmt=\"%1.5e %1.5e\" )" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "#In the same way, using the function loadtxt it is possible to load external data files\n", "data = np.loadtxt(\"file.dat\")\n", "#Data is then a multidimensional array with the loaded data\n", "print data" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 3.2 2.1]\n", " [ 3.1 4.1]]\n" ] } ], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "#Histograms are also useful and NumPy provides functions to do so\n", "x = [1,5,3,7,2,4,6,8,9,5,2,3,5,6,7,8,3,4,5]\n", "np.histogram(x)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 13, "text": [ "(array([1, 2, 3, 2, 0, 4, 2, 2, 2, 1]),\n", " array([ 1. , 1.8, 2.6, 3.4, 4.2, 5. , 5.8, 6.6, 7.4, 8.2, 9. ]))" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Other useful functions will be covered when needed during the course." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "
\n", "\n", "\n", "SciPy\n", "==" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "SciPy is a collection of mathematical algorithms and convenience functions built on the Numpy extension of Python. It adds significant power to the interactive Python session by providing the user with high-level commands and classes for manipulating and visualizing data. With SciPy an interactive Python session becomes a data-processing and system-prototyping environment rivaling sytems such as MATLAB, IDL, Octave, R-Lab, and SciLab.\n", "\n", "Some of the packages included with SciPy are:\n", "\n", "* Special functions (**scipy.special**)\n", "* Integration (**scipy.integrate**)\n", "* Optimization (**scipy.optimize**)\n", "* Interpolation (**scipy.interpolate**)\n", "* Fourier Transforms (**scipy.fftpack**)\n", "* Signal Processing (**scipy.signal**)\n", "* Linear Algebra (**scipy.linalg**)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each of these packages must be imported separately." ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Importing integrate package\n", "import scipy.integrate as integ" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The integrate package then includes the next functions:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "integ.Tester integ.fixed_quad integ.odepack integ.quadrature integ.test \n", "integ.complex_ode integ.newton_cotes integ.quad integ.romb integ.tplquad \n", "integ.cumtrapz integ.ode integ.quad_explain integ.romberg integ.trapz \n", "integ.dblquad integ.odeint integ.quadpack integ.simps integ.vode " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Almost each of the numerical methods that will be covered during the course can be found in SciPy. In next classes we will explore the offered options by SciPy according to the specific methods covered." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- - - " ] } ], "metadata": {} } ] }