Part VI. Scientific Computing in Python



Similar documents
Tentative NumPy Tutorial

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Scientific Programming in Python

CUDAMat: a CUDA-based matrix class for Python

Exercise 0. Although Python(x,y) comes already with a great variety of scientic Python packages, we might have to install additional dependencies:

5: Magnitude 6: Convert to Polar 7: Convert to Rectangular

Python. Python. 1 Python. M.Ulvrova, L.Pouilloux (ENS LYON) Informatique L3 Automne / 25

2+2 Just type and press enter and the answer comes up ans = 4

(!' ) "' # "*# "!(!' +,

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

Assignment 2: Option Pricing and the Black-Scholes formula The University of British Columbia Science One CS Instructor: Michael Gelbart

Recall the basic property of the transpose (for any A): v A t Aw = v w, v, w R n.

Data Visualization. Christopher Simpkins

Below is a very brief tutorial on the basic capabilities of Excel. Refer to the Excel help files for more information.

MATLAB Basics MATLAB numbers and numeric formats

CIS 192: Lecture 13 Scientific Computing and Unit Testing

FX 115 MS Training guide. FX 115 MS Calculator. Applicable activities. Quick Reference Guide (inside the calculator cover)

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

Chemical and Biological Engineering Calculations using Python 3. Jeffrey J. Heys

Linear Algebra Review. Vectors

Python for Chemistry in 21 days

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Engineering Problem Solving and Excel. EGN 1006 Introduction to Engineering

Analysis of System Performance IN2072 Chapter M Matlab Tutorial

Python for Computational Science and Engineering

Python programming guide for Earth Scientists. Maarten J. Waterloo and Vincent E.A. Post

AN INTRODUCTION TO NUMERICAL METHODS AND ANALYSIS

Introduction to Matlab

CD-ROM Appendix E: Matlab

MATLAB LECTURE NOTES. Dr. ADİL YÜCEL. Istanbul Technical University Department of Mechanical Engineering

Python for Scientific Computing.

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Advanced Functions and Modules

Linear Algebra Notes for Marsden and Tromba Vector Calculus

1 Introduction to Matrices

Display Format To change the exponential display format, press the [MODE] key 3 times.

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

Nonlinear Iterative Partial Least Squares Method

Gamma Distribution Fitting

CS3220 Lecture Notes: QR factorization and orthogonal transformations

Computational Physics With Python. Dr. Eric Ayars California State University, Chico

Introduction to Matrices for Engineers

Postprocessing with Python

Linear Algebra and TI 89

CRASH COURSE PYTHON. Het begint met een idee

Mean value theorem, Taylors Theorem, Maxima and Minima.

Modeling with Python

Content. Chapter 4 Functions Basic concepts on real functions 62. Credits 11

Lecture 5: Singular Value Decomposition SVD (1)

Introduction to the Finite Element Method

Numerical Methods I Eigenvalue Problems

Mathematics (MAT) MAT 061 Basic Euclidean Geometry 3 Hours. MAT 051 Pre-Algebra 4 Hours

How long is the vector? >> length(x) >> d=size(x) % What are the entries in the matrix d?

Operation Count; Numerical Linear Algebra

Eigenvalues and Eigenvectors

SAMPLE. Computer Algebra System (Classpad 330 using OS 3 or above) Application selector. Icolns that access working zones. Icon panel (Master toolbar)

Computational Mathematics with Python

Computational Mathematics with Python

Precalculus REVERSE CORRELATION. Content Expectations for. Precalculus. Michigan CONTENT EXPECTATIONS FOR PRECALCULUS CHAPTER/LESSON TITLES

MTH 437/537 Introduction to Numerical Analysis I Fall 2015

TI-Nspire CAS Graphing Calculator

from ztf_summerschool import source_lightcurve, barycenter_times %matplotlib inline

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

LAYOUT OF THE KEYBOARD

The Heat Equation. Lectures INF2320 p. 1/88

Programming Exercise 3: Multi-class Classification and Neural Networks

[1] Diagonal factorization

MBA Jump Start Program

u = [ 2 4 5] has one row with three components (a 3 v = [2 4 5] has three rows separated by semicolons (a 3 w = 2:5 generates the row vector w = [ 2 3

EXCEL Tutorial: How to use EXCEL for Graphs and Calculations.

AMS526: Numerical Analysis I (Numerical Linear Algebra)

Quick Tour of Mathcad and Examples

The NumPy array: a structure for efficient numerical computation

ANSA and μeta as a CAE Software Development Platform

Matrix Multiplication

Java Modules for Time Series Analysis

Similarity and Diagonalization. Similar Matrices

Programming Computer Vision with Python. Jan Erik Solem

Basic Concepts in Matlab

CITY UNIVERSITY LONDON. BEng Degree in Computer Systems Engineering Part II BSc Degree in Computer Systems Engineering Part III PART 2 EXAMINATION

GRADES 7, 8, AND 9 BIG IDEAS

GeoGebra. 10 lessons. Gerrit Stols

3. Interpolation. Closing the Gaps of Discretization... Beyond Polynomials

Electrical Engineering 103 Applied Numerical Computing

CONCEPT-II. Overview of demo examples

Variance Reduction. Pricing American Options. Monte Carlo Option Pricing. Delta and Common Random Numbers

Numerical Analysis Introduction. Student Audience. Prerequisites. Technology.

Lecture 2 Matrix Operations

NUMERICAL ANALYSIS PROGRAMS

Numerical Recipes in C++

A Brief Introduction to SPSS Factor Analysis

WEEK #3, Lecture 1: Sparse Systems, MATLAB Graphics

Gerrit Stols

The KaleidaGraph Guide to Curve Fitting

Computational Mathematics with Python

MACHINE LEARNING IN HIGH ENERGY PHYSICS

Transcription:

Part VI Scientific Computing in Python Compact Course @ GRS, June 03-07, 2013 80

More on Maths Module math Constants pi and e Functions that operate on int and float All return values float ceil (x) floor (x) exp (x) fabs ( x) # same as globally defined abs () ldexp (x, i) # x * 2** i log (x [, base ]) log10 (x) # == log (x, 10) modf ( x) # ( fractional, integer part ) pow (x, y) # x**y sqrt (x) Compact Course @ GRS, June 03-07, 2013 81

Module math (2) Trigonometric functions assume radians cos (x); cosh (x); acos (x) sin (x);... tan (x);... degrees ( x) # rad -> deg radians ( x) # deg -> rad Compact Course @ GRS, June 03-07, 2013 82

Module math (2) Trigonometric functions assume radians cos (x); cosh (x); acos (x) sin (x);... tan (x);... degrees ( x) # rad -> deg radians ( x) # deg -> rad inf/nan float (" inf ") float ("-inf ") float (" nan ") Compact Course @ GRS, June 03-07, 2013 82

Module math (2) Trigonometric functions assume radians cos (x); cosh (x); acos (x) sin (x);... tan (x);... degrees ( x) # rad -> deg radians ( x) # deg -> rad inf/nan float (" inf ") float ("-inf ") float (" nan ") Use module cmath for complex numbers Compact Course @ GRS, June 03-07, 2013 82

Now to Real Maths... Standard sequence types (list, tuple,... ) Can be used as arrays Can contain different types of objects Very flexible, but slow Loops are not very efficient either For efficient scientific computing, other datatypes and methods required Compact Course @ GRS, June 03-07, 2013 83

Now to Real Maths... Standard sequence types (list, tuple,... ) Can be used as arrays Can contain different types of objects Very flexible, but slow Loops are not very efficient either For efficient scientific computing, other datatypes and methods required Modules NumPy Matplotlib SciPy Compact Course @ GRS, June 03-07, 2013 83

NumPy Compact Course @ GRS, June 03-07, 2013 84

Module numpy Homogeneous arrays NumPy provides arbitrary-dimensional homogeneous arrays Example from numpy import * a = array ([[1,2,3],[4,5,6]]) print a type (a) a. shape print a [0,2] a [0,2] = -1 b = a*2 print b Compact Course @ GRS, June 03-07, 2013 85

Array creation Create from (nested) sequence type Direct access with method [] a = array ([1,2,3,4,5,6,7,8]) a [1] a = array ([[1,2,3,4],[5,6,7,8]]) a [1,1] a = array ([[[1,2],[3,4]],[[5,6],[7,8]]]) a [1,1,1] Compact Course @ GRS, June 03-07, 2013 86

Array creation Create from (nested) sequence type Direct access with method [] a = array ([1,2,3,4,5,6,7,8]) a [1] a = array ([[1,2,3,4],[5,6,7,8]]) a [1,1] a = array ([[[1,2],[3,4]],[[5,6],[7,8]]]) a [1,1,1] Properties of arrays a. ndim # number of dimensions a. shape # dimensions a. size # number of elements a. dtype # data type a. itemsize # number of bytes Compact Course @ GRS, June 03-07, 2013 86

Data Types Exact, C/C++-motivated type of array elements can be specified Otherwise, defaults are used Some types (different storage requirements): int_, int8, int16, int32, int64, float_, float8, float16, float32, float64, complex_, complex64, bool_, character, object_ Standard python type names result in default behaviour array ([[1,2,3],[4,5,6]], dtype = int ) array ([[1,2,3],[4,5,6]], dtype = complex ) array ([[1,2,3],[4,5,6]], dtype = int8 ) array ([[1,2,3],[4,5,1000]], dtype = int8 ) # wrong array ([[1,2,3],[4,5, "hi"]], dtype = object ) Exception: object_ stores pointers to objects Compact Course @ GRS, June 03-07, 2013 87

Create Arrays (Some) default matrices (optional parameter: dtype) arange ([a,] b [, stride ]) # as range, 1D zeros ( (3,4) ) ones ( (1,3,4) ) empty ( (3,4) ) # uninitialized ( fast ) linspace (a, b [, n]) # n equidistant in [a, b] logspace (a, b [, n]) # 10** a to 10** b identity (n) # 2d fromfunction ( lambda i,j: i+j, (3,4), dtype = int ) def f(i,j): return i+j fromfunction (f, (3,4), dtype = int ) Compact Course @ GRS, June 03-07, 2013 88

Manipulate Arrays Reshaping arrays a = arange (12) b = a. reshape ((3,4)) a. resize ((3,4)) # in - place! a. transpose () a. flatten () # Example use - case : a = arange (144) a. resize ((12,12)) Compact Course @ GRS, June 03-07, 2013 89

Create Arrays (2) Create/Copy from existing data a = arange (12); a. resize ((3,4)) copy (a) diag (a); tril (a); triu (a) empty_ like ( a) # copy shape zeros_like (a) ones_like (a) a = loadtxt (" matrix. txt ") # fromfile () if binary # plenty of options : comments, delim., usecols,... Matrix output a. tolist () savetxt (" matrix. txt ", a) # tofile () if binary Compact Course @ GRS, June 03-07, 2013 90

Array Access and Manipulation Typical slicing operations can be used Separate dimensions by comma a = arange (20); a. resize ((4,5)) a [1] a [1:2,:] a [:,::2] a [::2,::2] a [::2,::2] = [[0, -2, -4],[ -10, -12, -14]] a [1::2,1::2] = -1*a [1::2,1::2] Selective access a[ a > 3] a[ a > 3] = -1 Compact Course @ GRS, June 03-07, 2013 91

Array Access Iterating over entries for row in a: print row b = arange (30); b. resize ((2,3,4)) for row in b: for col in row : print col for entry in a. flat : print entry Compact Course @ GRS, June 03-07, 2013 92

Computing with Arrays Fast built-in methods working on arrays a = arange (12); a. resize ((3,4)) 3*a a **2 a+a^2 sin (a) sqrt (a) prod (a) sum (a) it = transpose ( a) x = array ([1,2,3]) y = array ([10,20,30]) inner (x, y) dot (it, x) cross (x,y) Compact Course @ GRS, June 03-07, 2013 93

Computing with Arrays There is much more... var () cov () std () mean () median () min () max () svd () tensordot ()... Matrices (with mat) are subclasses of ndarray, but strictly two-dimensional, with additional attributes m = mat (a) m. T # transpose m. I # inverse m. A # as 2d array m. H # conjugate transpose Compact Course @ GRS, June 03-07, 2013 94

Submodules Module numpy.random Draw from plenty of different distributions More powerful than module random Work on and return arrays from numpy. random import * binomial (10, 0.5) # 10 trials, success 50% binomial (10, 0.5, 15) randint (0, 10, 15) # [0,10), int rand () # [0,1) rand (3,4) # (3 x4) array Compact Course @ GRS, June 03-07, 2013 95

Submodules (2) Module numpy.linalg Core linear algebra tools norm (a); norm (x) inv (a) solve (a, b) # LAPACK LU decomp. det (a) eig (a) cholesky ( a) Compact Course @ GRS, June 03-07, 2013 96

Submodules (2) Module numpy.linalg Core linear algebra tools norm (a); norm (x) inv (a) solve (a, b) # LAPACK LU decomp. det (a) eig (a) cholesky ( a) Module numpy.fft Fourier transforms Compact Course @ GRS, June 03-07, 2013 96

Submodules (2) Module numpy.linalg Core linear algebra tools norm (a); norm (x) inv (a) solve (a, b) # LAPACK LU decomp. det (a) eig (a) cholesky ( a) Module numpy.fft Fourier transforms There is more... Compact Course @ GRS, June 03-07, 2013 96

Version Mania Current Situation SciPy Matplotlib NumPy pylab IPython python Compact Course @ GRS, June 03-07, 2013 97

Version Mania Problems: Numpy, scipy, pylab, ipython and matplotlib often used simultaneously The packages depend on each other (matplotlib uese numpy arrays, e.g.) Depending on OS (version), different packages may have to be installed (i.e. the module name in import command may be different!). Vision: All in one (new) module PyLab! exists as unofficial package Attention Name: again pylab! Compact Course @ GRS, June 03-07, 2013 98

Matplotlib Compact Course @ GRS, June 03-07, 2013 99

Matplotlib What is it? Object-oriented library for plotting 2D Designed to be similar to the matlab plotting functionality Designed to plot scientific data, built on numpy datastructures Compact Course @ GRS, June 03-07, 2013 100

Several Ways to do the Same python/ipython interactive > ipython import scipy, matplotlib. pylab x = scipy. randn (10000) matplotlib. pylab. hist (x, 100) > ipython import numpy. random, matplotlib. pylab x = numpy. random. randn (10000) matplotlib. pylab. hist (x, 100) ipython in pylab mode > ipython - pylab x = randn (10000) hist (x, 100) Compact Course @ GRS, June 03-07, 2013 101

Example - First Plot partially taken from http://matplotlib.sourceforge.net/users/screenshots.html from pylab import * x = arange (0.0, 2* pi, 0.01) y = sin (x) plot (x, y, linewidth =4) plot (x,y) xlabel ( Label for x axis ) ylabel ( Label for y axis ) title ( Simple plot of sin ) grid ( True ) show () Compact Course @ GRS, June 03-07, 2013 102

Example Using Subplots from pylab import * def f(t): s1 = cos (2* pi*t) e1 = exp (-t) return multiply (s1,e1) t1 = arange (0.0, 5.0, 0.1) t2 = arange (0.0, 5.0, 0.02) t3 = arange (0.0, 2.0, 0.01) show () # gives error but helps ; -) subplot (2,1,1) # rows, columns, which to show plot (t1, f(t1), go, t2, f(t2), k-- ) subplot (2,1,2) plot (t3, cos (2* pi*t3), r. ) Compact Course @ GRS, June 03-07, 2013 103

Example Using Subplots # previous slide continued subplot (2,1,1) grid ( True ) title ( A tale of 2 subplots ) ylabel ( Damped oscillation ) subplot (2,1,2) grid ( True ) xlabel ( time (s) ) ylabel ( Undamped ) Compact Course @ GRS, June 03-07, 2013 104

Example - Histogram # start ipython - pylab or use imports : # from matplotlib. mlab import * # from matplotlib. pyplot import * from numpy import * mu, sigma = 100, 15 x = mu + sigma * random. randn (10000) n, bins, patches = hist (x, 50, normed =1, \ facecolor = green, alpha =0.75) # add a best fit line y = normpdf ( bins, mu, sigma ) plot (bins, y, r--, linewidth =1) axis ([40, 160, 0, 0.03]) plt. show () Compact Course @ GRS, June 03-07, 2013 105

SciPy Compact Course @ GRS, June 03-07, 2013 106

More than NumPy? SciPy depends on NumPy Built to work on NumPy arrays Providing functionality for mathematics, science and engineering Still under development NumPy is mostly about (N-dimensional) arrays SciPy comprises a large number of tools using these arrays SciPy includes the NumPy functionality (only one import necessary) A lot more libraries for scientific computing are available, some of them using NumPy and SciPy Here, just a short overview will be given www.scipy.org for more material (incl. the content of the following slides) Compact Course @ GRS, June 03-07, 2013 107

SciPy Organisation - Subpackages cluster Clustering algorithms constants Physical and mathematical constants fftpack Fast Fourier Transform routines integrate Integration and ordinary differential equation solvers interpolate Interpolation and smoothing splines io Input and Output linalg Linear algebra maxentropy Maximum entropy methods ndimage N-dimensional image processing odr Orthogonal distance regression optimize Optimization and root-finding routines signal Signal processing sparse Sparse matrices and associated routines spatial Spatial data structures and algorithms special Special functions stats Statistical distributions and functions weave C/C++ integration Compact Course @ GRS, June 03-07, 2013 108

Special Functions Airy functions Elliptic functions Bessel functions (+ Zeros, Integrals, Derivatives, Spherical, Ricatti-) Struve functions A large number of statistical functions Gamma functions Legendre functions Orthogonal polynomials (Legendre, Chebyshev, Jacobi,...) Hypergeometric functios parabolic cylinder functions Mathieu functions Spheroidal wave functions Kelvin functions... Compact Course @ GRS, June 03-07, 2013 109

Example: Interpolation - Linear import numpy as np import matplotlib. pyplot as plt from scipy import interpolate x = np. arange (0,10) y = np.exp (-x /3.0) f = interpolate. interp1d (x, y) xnew = np. arange (0,9,0.1) plt. plot (x,y, o,xnew,f( xnew ), - ) plt. title ( Linear interpolation ) plt. show () Compact Course @ GRS, June 03-07, 2013 110

Example: Interpolation - Cubic Spline import numpy as np import matplotlib. pyplot as plt from scipy import interpolate x = np. arange (0, 2.25* np.pi, np.pi /4) y = np.sin (x) spline = interpolate. splrep (x,y,s =0) xnew = np. arange (0,2.02* np.pi,np.pi /50) ynew = interpolate. splev ( xnew, spline ) plt. plot (x,y, o,xnew, ynew ) plt. legend ([ Linear, Cubic Spline ]) plt. axis ([ -0.05,6.33, -1.05,1.05]) plt. title ( Cubic - spline interpolation ) plt. show () Compact Course @ GRS, June 03-07, 2013 111