Академический Документы
Профессиональный Документы
Культура Документы
Tobias Neckel: Scripting with Python... and beyond 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 ) ldexp (x , i ) log ( x [ , base ]) log10 ( x ) modf ( x ) pow (x , y ) sqrt ( x )
# same as globally defined abs () # x * 2** i # == log (x , 10) # ( fractional , integer part ) # x ** y
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 81
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 82
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 82
Very exible, but slow Loops are not very efcient either
required
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 83
Very exible, but slow Loops are not very efcient either
required
Modules
NumPy Matplotlib SciPy
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 83
NumPy
Tobias Neckel: Scripting with Python... and beyond 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
Tobias Neckel: Scripting with Python... and beyond 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]
Tobias Neckel: Scripting with Python... and beyond 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 a . shape a . size a . dtype a . itemsize # # # # # number of dimensions dimensions number of elements data type number of bytes
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 86
Data Types
Exact, C/C++-motivated type of array elements can be specied 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
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 87
Create Arrays
(Some) default matrices (optional parameter: dtype) arange ([ a ,] b [ , stride ]) zeros ( (3 ,4) ) ones ( (1 ,3 ,4) ) empty ( (3 ,4) ) linspace (a , b [ , n ]) logspace (a , b [ , n ]) identity ( n ) # as range , 1 D
# # # #
fromfunction ( lambda i , j : i +j , (3 ,4) , dtype = int ) def f (i , j ): return i + j fromfunction (f , (3 ,4) , dtype = int )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 88
Manipulate Arrays
Reshaping arrays a = arange (12) b = a . reshape ((3 ,4)) a . resize ((3 ,4)) a . transpose () a . flatten ()
# in - place !
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 89
a = loadtxt ( " matrix . txt " ) # fromfile () if binary # plenty of options : comments , delim . , usecols , ... Matrix output a . tolist () savetxt ( " matrix . txt " , a )
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 90
# tofile ()
if binary
Tobias Neckel: Scripting with Python... and beyond 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
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 92
Tobias Neckel: Scripting with Python... and beyond 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 () rand (3 ,4) # [0 ,1) #
(3 x4 ) array
Tobias Neckel: Scripting with Python... and beyond 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 )
Tobias Neckel: Scripting with Python... and beyond 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
Tobias Neckel: Scripting with Python... and beyond 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. . .
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 96
Version Mania
Current Situation
SciPy
python
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 97
Version Mania
Problems:
Numpy, scipy, pylab, ipython and matplotlib often used
The packages depend on each other (matplotlib uese numpy Depending on OS (version), different packages may have to be
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 98
Matplotlib
Tobias Neckel: Scripting with Python... and beyond 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 scientic data, built on numpy datastructures
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 100
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 102
# 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 )
Tobias Neckel: Scripting with Python... and beyond 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 ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 105
SciPy
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 106
necessary)
Here, just a short overview will be given www.scipy.org for more material (incl. the content of the
following slides)
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 107
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013
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 beyond Compact Course @ GRS, June 03 - 07, 2013 109
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 ()
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 110
Tobias Neckel: Scripting with Python... and beyond Compact Course @ GRS, June 03 - 07, 2013 111