python day4

Upload: anidcohen9058

Post on 04-Jun-2018

238 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 Python Day4

    1/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Part VI

    Scientific Computing in Python

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 80

  • 8/13/2019 Python Day4

    2/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    More on Maths

    Modulemath

    Constants piand e

    Functions that operate onintand float

    All return values float

    c e i l ( x )f l o o r ( x )

    e x p ( x )

    f a b s ( x ) # s am e as g lo ba ll y d ef in ed ab s ()

    l de xp ( x , i ) # x * 2** i

    l og ( x [ , b as e ])

    l o g 1 0 ( x ) # == log (x , 10)modf ( x ) # ( f r a ct i on a l , i n te g er p ar t )

    p ow ( x , y ) # x ** y

    s q r t ( x )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 81

  • 8/13/2019 Python Day4

    3/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module math (2)

    Trigonometric functions assume radians

    c os ( x ); c os h ( x ); a co s ( x)

    s in ( x ); . ..

    t an ( x ); . ..

    d e g r e e s ( x ) # rad -> d eg r a d i a n s ( x ) # deg -> r ad

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 82

  • 8/13/2019 Python Day4

    4/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module math (2)

    Trigonometric functions assume radians

    c os ( x ); c os h ( x ); a co s ( x)

    s in ( x ); . ..

    t an ( x ); . ..

    d e g r e e s ( x ) # rad -> d eg r a d i a n s ( x ) # deg -> r ad

    inf/nan

    f l o a t (" i n f " )

    f l o a t (" - i n f " )

    f l o a t (" n a n " )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 82

  • 8/13/2019 Python Day4

    5/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module math (2)

    Trigonometric functions assume radians

    c os ( x ); c os h ( x ); a co s ( x)

    s in ( x ); . ..

    t an ( x ); . ..

    d e g r e e s ( x ) # rad -> d eg r a d i a n s ( x ) # deg -> r ad

    inf/nan

    f l o a t (" i n f " )

    f l o a t (" - i n f " )

    f l o a t (" n a n " )

    Use module cmathfor complex numbers

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 82

  • 8/13/2019 Python Day4

    6/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 83

  • 8/13/2019 Python Day4

    7/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 83

  • 8/13/2019 Python Day4

    8/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    NumPy

    Tobias Neckel: Scripting with Python... and beyond

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

  • 8/13/2019 Python Day4

    9/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Module numpy

    Homogeneous arrays

    NumPy provides arbitrary-dimensional homogeneous arrays

    Example

    from numpy import *

    a = a rr ay ( [ [1 , 2 , 3 ] , [4 , 5 , 6 ]] )print a

    t y p e ( a )

    a . s h a p e

    print a [ 0 , 2 ]

    a [0 ,2] = -1

    b = a * 2print b

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 85

  • 8/13/2019 Python Day4

    10/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array creation

    Create from (nested) sequence type

    Direct access with method []

    a = a rr ay ( [1 , 2 , 3 ,4 , 5 , 6 ,7 , 8] )

    a[1]

    a = a r ra y ( [ [1 , 2 , 3 , 4] , [ 5 , 6 , 7 , 8 ]] )

    a [ 1 , 1 ]

    a = a r ra y ( [ [ [1 , 2] , [3 , 4] ] , [ [5 , 6] , [7 , 8] ]] )

    a [ 1 , 1 , 1 ]

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 86

  • 8/13/2019 Python Day4

    11/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array creation

    Create from (nested) sequence type

    Direct access with method []

    a = a rr ay ( [1 , 2 , 3 ,4 , 5 , 6 ,7 , 8] )

    a[1]

    a = a r ra y ( [ [1 , 2 , 3 , 4] , [ 5 , 6 , 7 , 8 ]] )

    a [ 1 , 1 ]

    a = a r ra y ( [ [ [1 , 2] , [3 , 4] ] , [ [5 , 6] , [7 , 8] ]] )a [ 1 , 1 , 1 ]

    Properties of arrays

    a . n d i m # n um be r of d im en si on s

    a . s h a p e # d i m en s io n s

    a . s i z e # n um be r of e le me nt s

    a . d t y p e # data type

    a . i t e m s i z e # n umb er o f byt es

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 86

  • 8/13/2019 Python Day4

    12/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    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

    a r ra y ( [ [1 , 2 , 3 ] , [4 , 5 , 6] ] , d ty p e = i nt )

    a r ra y ( [ [1 , 2 , 3 ] , [4 , 5 , 6] ] , d ty p e = c o m pl e x )

    a r ra y ( [ [1 , 2 , 3 ] , [4 , 5 , 6] ] , d ty p e = i n t8 )a r ra y ( [ [1 , 2 , 3] , [ 4 , 5 , 1 00 0] ] , d t yp e = i n t8 ) # w ro ng

    a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , " h i " ]] , d t yp e = o b j ec t )

    Exception: object_stores pointers to objects

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 87

  • 8/13/2019 Python Day4

    13/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Create Arrays

    (Some) default matrices (optional parameter: dtype)

    a ra ng e ( [a , ] b [ , s tr id e ] ) # as range , 1 D

    z er os ( (3 , 4) )

    o ne s ( (1 , 3 ,4) )

    e mp ty ( (3 , 4) ) # u ni n it ia l iz e d ( f as t )

    l in sp ac e (a , b [ , n ]) # n e qu id is ta nt in [a , b]

    l og sp ac e (a , b [ , n ]) # 10** a to 10** b

    i d e n t i t y ( n ) # 2 d

    f r o m f u n c t i o n ( lambda i , j : i +j , (3 , 4) , d ty pe = i nt )

    def f ( i , j ) :return i + j

    f r o mf u n ct i o n ( f , ( 3 ,4 ) , d t yp e = i n t )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 88

  • 8/13/2019 Python Day4

    14/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Manipulate Arrays

    Reshaping arrays

    a = a ra ng e ( 12 )

    b = a . r e sh ap e ( (3 , 4 ))

    a . r e s i z e ( ( 3 , 4 ) ) # in - p la ce !

    a . t r a n s p o s e ( )a . f l a t t e n ( )

    # E xa mp le use - c as e :

    a = a ra ng e ( 14 4)

    a . r e s i z e ( ( 1 2 , 1 2 ) )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 89

  • 8/13/2019 Python Day4

    15/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Create Arrays (2)

    Create/Copy from existing data

    a = a ra ng e ( 1 2) ; a . r es iz e ( (3 , 4 ))

    c o p y ( a )

    d ia g ( a ); t ri l ( a ); t ri u ( a)

    e m p t y _ l i k e ( a ) # c op y s ha pe

    z e r o s _ l i k e ( a )o n e s _ l i k e ( a )

    a = l oa dt xt ( " m a t r i x . t x t " ) # f ro mf il e () if b in ar y

    # p le nt y of o pt io ns : c om me nt s , d el im . , u se co ls , . ..

    Matrix output

    a . t o l i s t ( )

    s a v e t x t (" m a t r i x . t x t " , a ) # t of il e () if b i na ry

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 90

    S i ifi C i i C S i T h i h U i i M h

  • 8/13/2019 Python Day4

    16/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array Access and Manipulation

    Typical slicing operations can be used

    Separate dimensions by comma

    a = a ra ng e ( 2 0) ; a . r es iz e ( (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

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 91

    S i tifi C ti i C t S i T h i h U i it t M h

  • 8/13/2019 Python Day4

    17/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Array Access

    Iterating over entries

    for row in a :

    print row

    b = a ra ng e ( 3 0) ; b . r es iz e ( (2 , 3 , 4) )for row in b :

    for col in row:

    print col

    for entry in a . f l a t :

    print entry

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 92

    Scientific Computing in Computer Science Technische Universitat Munchen

  • 8/13/2019 Python Day4

    18/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Computing with Arrays

    Fast built-in methods working on arrays

    a = a ra ng e ( 1 2) ; a . r es iz e ( (3 , 4 ))

    3* a

    a**2

    a + a ^ 2

    s i n ( a )

    s q r t ( a )p r o d ( a )

    s u m ( a )

    it = t ra ns po se ( a )

    x = a rr ay ( [1 , 2 , 3])y = a rr ay ( [ 10 , 2 0 , 30 ])

    i nn er ( x , y )

    d ot ( it , x )

    c r o s s ( x , y )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 93

    Scientific Computing in Computer Science Technische Universitat Munchen

  • 8/13/2019 Python Day4

    19/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Computing with Arrays

    There is much more. . .

    var () cov () std ()

    mean () median ()

    min () max ()

    svd()

    t e n s o r d o t ( )

    ...

    Matrices (withmat) are subclasses of ndarray, but strictlytwo-dimensional, with additional attributes

    m = mat ( a )

    m . T # t r an sp os em . I # i nv er se

    m . A # as 2 d ar ray

    m . H # c o nj ug at e t ra ns po s e

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 94

    Scientific Computing in Computer Science Technische Universitat Munchen

  • 8/13/2019 Python Day4

    20/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Submodules

    Modulenumpy.random

    Draw from plenty of different distributions

    More powerful than module random

    Work on and return arrays

    from numpy.random import *

    b i no m ia l ( 10 , 0 .5 ) # 10 t ri al s , s uc ce ss 5 0%

    b in om ia l ( 10 , 0.5 , 1 5)

    r an di nt ( 0 , 10 , 1 5) # [0 , 10) , i nt

    rand() # [0 , 1)r a n d ( 3 , 4 ) # (3 x4 ) arr ay

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 95

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    21/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

    Submodules (2)

    Modulenumpy.linalg

    Core linear algebra tools

    n or m ( a ) ; n or m ( x )

    i n v ( a )

    s ol ve ( a , b ) # L AP AC K LU d ec om p .d e t ( a )

    e i g ( a )

    c h o l e s k y ( a )

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 96

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    22/38

    p g p ,

    Submodules (2)

    Modulenumpy.linalg

    Core linear algebra tools

    n or m ( a ) ; n or m ( x )

    i n v ( a )

    s ol ve ( a , b ) # L AP AC K LU d ec om p .d e t ( a )

    e i g ( a )

    c h o l e s k y ( a )

    Modulenumpy.fft

    Fourier transforms

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 96

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    23/38

    Submodules (2)

    Modulenumpy.linalg

    Core linear algebra tools

    n or m ( a ) ; n or m ( x )

    i n v ( a )

    s ol ve ( a , b ) # L AP AC K LU d ec om p .d e t ( a )

    e i g ( a )

    c h o l e s k y ( a )

    Modulenumpy.fft

    Fourier transforms

    There is more. . .

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 96

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    24/38

    Version Mania

    Current Situation

    python

    MatplotlibSciPy

    NumPy

    IPythonpylab

    Tobias Neckel: Scripting with Python... and beyond

    Compact Course @ GRS, June 03 - 07, 2013 97

  • 8/13/2019 Python Day4

    25/38

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    26/38

    Matplotlib

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 99

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    27/38

    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

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 100

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    28/38

    Several Ways to do the Same

    python/ipython interactive

    > i py th on

    import s c ip y , m a t pl o tl i b . p y l ab

    x = s ci py . r a nd n ( 1 00 00 )

    m atplo tlib . pylab . hist (x , 100)

    > i py th onimport n u m py . r an d om , m a t p l o tl i b . p y l ab

    x = n um p y . r a nd o m . r a nd n ( 1 0 0 00 )

    m atplo tlib . pylab . hist (x , 100)

    ipython in pylab mode

    > i py th on - p yl ab

    x = r an dn ( 1 00 00 )

    h is t ( x , 1 00 )

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 101

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    29/38

    Example - First Plot

    partially taken fromhttp://matplotlib.sourceforge.net/users/screenshots.html

    from pylab import *

    x = a ra ng e (0.0 , 2* pi , 0 .0 1)

    y = sin (x )p lo t (x , y , l in ew i dt h = 4)

    p l o t ( x , y )

    x l a b e l ( L a b e l f o r x a x i s )

    y l a b e l ( L a b e l f o r y a x i s )

    title( S i m p l e p l o t o f s i n )g r i d ( T r u e )

    show()

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 102

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    30/38

    Example Using Subplots

    from pylab import *def f ( t ) :

    s1 = cos ( 2* p i *t )

    e1 = exp ( -t )

    return m u l t i p l y ( s 1 , e 1 )

    t1 = a ra ng e (0.0 , 5.0 , 0 .1)t2 = a ra ng e (0.0 , 5.0 , 0 .0 2)

    t3 = a ra ng e (0.0 , 2.0 , 0 .0 1)

    show() # giv es e rro r but hel ps ; -)

    s u b p l o t ( 2 , 1 , 1 ) # rows , co lum ns , w hi ch to s ho w p lo t ( t1 , f ( t 1 ) , g o , t2 , f ( t2 ), k - - )

    s u b p l o t ( 2 , 1 , 2 )

    p lo t ( t3 , c os ( 2* p i * t 3 ) , r . )

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 103

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    31/38

    Example Using Subplots

    # p re vi ou s s li de c on ti nu ed

    s u b p l o t ( 2 , 1 , 1 )

    g r i d ( T r u e )

    title( A t a l e o f 2 s u b p l o t s )y l a b e l ( D a m p e d o s c i l l a t i o n )

    s u b p l o t ( 2 , 1 , 2 )

    g r i d ( T r u e )

    x l a b e l ( t i m e ( s ) )

    y l a b e l ( U n d a m p e d )

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 104

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    32/38

    Example - Histogram

    # s ta rt i py th on - py la b or us e i mp or ts :# f ro m m at p lo tl i b . m la b i mp or t *

    # f ro m m at p lo tl i b . p yp lo t i mp or t *

    from numpy import *

    mu , sigma = 100 , 15

    x = mu + s ig ma * r an do m . ra nd n ( 10 00 0)n , bins , p at ch es = hist (x , 50 , no rm ed =1 , \

    f a c e c o l o r = g r e e n , a l ph a = 0 . 7 5)

    # add a best fit line

    y = n orm pd f ( bins , mu , s igm a )

    p lo t ( bi ns , y , r - - , l i ne w id t h = 1 )

    a xi s ([40 , 160 , 0 , 0 .0 3] )

    p l t . s h o w ( )

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 105

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    33/38

    SciPy

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 106

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    34/38

    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 importnecessary)

    A lot more libraries for scientific computing are available, some ofthem using NumPy and SciPy

    Here, just a short overview will be given

    www.scipy.orgfor more material (incl. the content of thefollowing slides)

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 107

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    35/38

    SciPy Organisation - Subpackages

    cluster Clustering algorithmsconstants Physical and mathematical constantsfftpack Fast Fourier Transform routinesintegrate Integration and ordinary differential equation solversinterpolate Interpolation and smoothing splinesio Input and Outputlinalg Linear algebra

    maxentropy Maximum entropy methodsndimage N-dimensional image processingodr Orthogonal distance regressionoptimize Optimization and root-finding routinessignal Signal processing

    sparse Sparse matrices and associated routinesspatial Spatial data structures and algorithmsspecial Special functionsstats Statistical distributions and functionsweave C/C++ integration

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 108

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    36/38

    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

    ...

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 109

    Scientific Computing in Computer Science, Technische Universitat Munchen

  • 8/13/2019 Python Day4

    37/38

    Example: Interpolation - Linear

    import n ump y as np

    import m a tp l ot l i b . p y p lo t a s p lt

    from scipy import interpolate

    x = np . a r an ge ( 0 , 10 )

    y = np . e xp ( - x / 3. 0)

    f = i nt e rp ol a te . i nt er p1 d ( x , y )

    x ne w = n p . ar an ge ( 0 ,9 , 0 .1 )

    p l t . p l o t ( x , y , o , x n e w , f ( x n e w ) , - )

    p l t . t i t l e ( L i n e a r i n t e r p o l a t i o n )

    p l t . s h o w ( )

    Tobias Neckel: Scripting with Python... and beyondCompact Course @ GRS, June 03 - 07, 2013 110

  • 8/13/2019 Python Day4

    38/38