{ "metadata": { "name": "ScientificProgrammingI" }, "nbformat": 3, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "source": [ "Scientific Programming I
\n", "Berian James / berian@berkeley.edu" ] }, { "cell_type": "heading", "level": 2, "source": [ "1. Introduction to SciPy" ] }, { "cell_type": "markdown", "source": [ "Import NumPy and SciPy" ] }, { "cell_type": "code", "input": [ "import numpy as np" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "import scipy as sp" ], "language": "python", "outputs": [] }, { "cell_type": "markdown", "source": [ "Differences in temperament between NumPy and SciPy; see, e.g., http://www.scipy.org/NegativeSquareRoot" ] }, { "cell_type": "code", "input": [ "exp(pi*np.sqrt(-1)) + 1" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "exp(pi*sp.sqrt(-1)) + 1" ], "language": "python", "outputs": [] }, { "cell_type": "heading", "level": 3, "source": [ "Getting data in and out of SciPy" ] }, { "cell_type": "markdown", "source": [ "Import Matlab data into Python" ] }, { "cell_type": "code", "input": [ "import scipy.io as sio" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "struct = sio.loadmat('testbox.mat') # Imports Matlab data structure" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "struct" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "box = struct['box'] # Extracts data object from structure" ], "language": "python", "outputs": [] }, { "cell_type": "markdown", "source": [ "**Exercise: What are the dimensions of the imported array?**" ] }, { "cell_type": "heading", "level": 3, "source": [ "Vector and array manipulation" ] }, { "cell_type": "markdown", "source": [ "Construct sequence of n (linearly-spaced) numbers, from a to b" ] }, { "cell_type": "code", "input": [ "a = 50; b = 100-0.1; n = 57;" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "sp.linspace(a,b,n)" ], "language": "python", "outputs": [] }, { "cell_type": "markdown", "source": [ "Construct sequence of n (base-10 logarithmically-spaced) numbers between $10^a$ and $10^b$" ] }, { "cell_type": "code", "input": [ "a = -1; b = 1; n = 20;" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "sp.logspace(a,b,n)" ], "language": "python", "outputs": [] }, { "cell_type": "markdown", "source": [ "Construct coordinate array" ] }, { "cell_type": "code", "input": [ "x,y = np.mgrid[0:5,0:5]" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "x" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "y" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "np.sqrt(x**2 + y**2)" ], "language": "python", "outputs": [] }, { "cell_type": "markdown", "source": [ "Construct tiled array" ] }, { "cell_type": "code", "input": [ "x = np.linspace(0,10,11);" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "x" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "np.c_[x,x**2]" ], "language": "python", "outputs": [] }, { "cell_type": "heading", "level": 3, "source": [ "Symbolic computation with SymPy" ] }, { "cell_type": "markdown", "source": [ "Import SymPy (CAUTION!)" ] }, { "cell_type": "code", "input": [ "from sympy import *" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "1/2 + 1/3" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "Rational(1,2) + Rational(1,3)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "Rational(5,6).evalf(6)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "1./2. + 1./3." ], "language": "python", "outputs": [] }, { "cell_type": "heading", "level": 4, "source": [ "Calculus with symbolic variables" ] }, { "cell_type": "code", "input": [ "x = Symbol(\"x\")" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "limit( sin(x) / x, x, 0)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "diff(erf(x),x)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "integrate(1./sqrt(pi) * exp(-x**2),x)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "integrate(1./sqrt(pi) * exp(-x**2), (x,-oo,0) ).evalf(3)" ], "language": "python", "outputs": [] }, { "cell_type": "heading", "level": 4, "source": [ "Matrix compuation" ] }, { "cell_type": "code", "input": [ "M = Matrix( ([1, x], [x, 1]) )\n", "pprint(M)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "%load_ext sympyprinting" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "M.inv()" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "M.cholesky()" ], "language": "python", "outputs": [] }, { "cell_type": "heading", "level": 3, "source": [ "Interfacing with other languages" ] }, { "cell_type": "markdown", "source": [ "See files in f2py_example. Things to note:\n" ] }, { "cell_type": "markdown", "source": [ "*i) differences between subroutines and functions in Fortran are not really important here*" ] }, { "cell_type": "markdown", "source": [ "*ii) passing arrays is fine, but you need to pass their dimensions as well*" ] }, { "cell_type": "markdown", "source": [ "*iii) In general, SciPy contains many useful functions that work as quickly as Fortran/C. But if you need to crunch something inside a loop, or nested loops, a considerable speed-up is possible*" ] }, { "cell_type": "code", "input": [ "more example.f90" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "!f2py --fcompiler=gfortran -c example.f90 -m example" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "import example" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "a = 14; b = 78;" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "example.arithmetic(a, b)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "c, d = example.arithmetic(a, b)" ], "language": "python", "outputs": [] }, { "cell_type": "markdown", "source": [ "A weave example (courtesy of Nat Butler) is below" ] }, { "cell_type": "code", "input": [ "from scipy import weave" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "x = np.arange(1000)/1000.\n", "y = np.zeros(1000,dtype=np.double)\n", "n=len(x)" ], "language": "python", "outputs": [] }, { "cell_type": "code", "input": [ "code = \"\"\"\n", " int i;\n", " for (i=0;i