Part VI
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 g l o b a l l y 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
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 ")
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 ")
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 ")
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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
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
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Module numpy
Homogeneous arrays
•
NumPy provides arbitrary-dimensional homogeneous arrays
•
Example
from
numpy
import
*
a = array ([[1 ,2 ,3] ,[4 ,5 ,6]])
a
type ( a )
a . shape
a [0 ,2]
a [0 ,2] = -1
b = a *2
b
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 d i m e n s i o n s
a . shape
# d i m e n s i o n s
a . size
# number of e l e m e n t s
a . dtype
# data type
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 d i m e n s i o n s
a . shape
# d i m e n s i o n s
a . size
# number of e l e m e n t s
a . dtype
# data type
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 )
Create Arrays
•
(Some) default matrices (optional parameter: dtype)
arange ([ a ,] b [ , stride ])
# as range , 1 D
zeros ( (3 ,4) )
ones ( (1 ,3 ,4) )
empty ( (3 ,4) )
# u n i n i t i a l i z e d ( fast )
li nsp ace (a , b [ , n ])
# n e q u i d i s t a n t in [a , b ]
lo gsp ace (a , b [ , n ])
# 10** a to 10** b
id ent ity ( n )
# 2 d
f r o m f u n c t i o n (
lambda
i , j : i +j , (3 ,4) , dtype = int )
def
f (i , j ):
return
i + j
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Manipulate Arrays
•
Reshaping arrays
a = arange (12)
b = a . reshape ((3 ,4))
a . resize ((3 ,4))
# in - place !
a . t r a n s p o s e ()
a . flatten ()
# Example use - case :
a = arange (144)
a . resize ((12 ,12))
Create Arrays (2)
•
Create/Copy from existing data
a = arange (12); a . resize ((3 ,4))
copy ( a )
diag ( a ); tril ( a ); triu ( a )
e m p t y _ l i k e ( a )
# copy shape
z e r o s _ l i k e ( a )
o n e s _ l i k e ( a )
a = loadtxt (
" matrix . txt ")
# f r o m f i l e () if binary
# plenty of options : comments , delim . , usecols , ...
•
Matrix output
a . tolist ()
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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
Array Access
•
Iterating over entries
for
row
in
a :
row
b = arange (30); b . resize ((2 ,3 ,4))
for
row
in
b :
for
col
in
row :
col
for
entry
in
a . flat :
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 = t r a n s p o s e ( a )
x = array ([1 ,2 ,3])
y = array ([10 ,20 ,30])
inner (x , y )
dot ( it , x )
cross (x , y )
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 (with
mat
) are subclasses of
ndarray
, but strictly
two-dimensional, with additional attributes
m = mat ( a )
m . T
# t r a n s p o s e
m . I
# inverse
m . A
# as 2 d array
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Submodules
Module
numpy.random
•
Draw from plenty of different distributions
•
More powerful than module
random
•
Work on and return arrays
from
numpy . random
import
*
bi nom ial (10 , 0.5)
# 10 trials , success 50%
bi nom ial (10 , 0.5 , 15)
randint (0 , 10 , 15)
# [0 ,10) , int
rand ()
# [0 ,1)
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 )
ch ole sky ( a )
Module
•
Fourier transforms
There is more. . .
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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 )
ch ole sky ( a )
Module
numpy.fft
•
Fourier transforms
There is more. . .
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 )
ch ole sky ( a )
Module
numpy.fft
•
Fourier transforms
There is more. . .
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Version Mania
Current Situation
python
Matplotlib
SciPy
NumPy
IPython
pylab
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
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Several Ways to do the Same
python/ipython interactive
> ipython
import
scipy , m a t p l o t l i b . pylab
x = scipy . randn (10000)
m a t p l o t l i b . pylab . hist (x , 100)
> ipython
import
numpy . random , m a t p l o t l i b . pylab
x = numpy . random . randn (10000)
m a t p l o t l i b . pylab . hist (x , 100)
ipython in pylab mode
> ipython - pylab
x = randn (10000)
hist (x , 100)
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 , l i n e w i d t h =4)
plot (x , y )
xlabel (
’ Label for x axis ’)
ylabel (
’ Label for y axis ’)
title (’ Simple plot of sin ’
)
grid ( True )
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Example – Using Subplots
from
pylab
import
*
def
f ( t ):
s1 = cos (2* pi * t )
e1 = exp ( - t )
return
m ult ipl y ( 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)
Example – Using Subplots
# p r e v i o u s slide c o n t i n u e d
subplot (2 ,1 ,1)
grid ( True )
title (’A tale of 2 sub plo ts ’)
ylabel (
’ Damped o s c i l l a t i o n ’)
subplot (2 ,1 ,2)
grid ( True )
xlabel (
’ time ( s ) ’)
ylabel (
’ U nda mpe d ’)
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Example - Histogram
# start ipython - pylab or use imports :
# from m a t p l o t l i b . mlab import *
# from m a t p l o t l i b . pyplot import *
from
numpy
import
*
mu , sigma = 100 , 15
x = mu + sigma * random . randn (10000)
n , bins , patches = hist (x , 50 , normed =1 , \
f a c e c o l o r =
’ green ’, alpha =0.75)
# add a ’ best fit ’ line
y = normpdf ( bins , mu , sigma )
plot ( bins , y ,
’r - - ’, l i n e w i d t h =1)
axis ([40 , 160 , 0 , 0.03])
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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)
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
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
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
Example: Interpolation - Linear
import
numpy as np
import
m a t p l o t l i b . pyplot as plt
from
scipy
import
i n t e r p o l a t e
x = np . arange (0 ,10)
y = np . exp ( - x /3.0)
f = i n t e r p o l a t e . int erp 1d (x , y )
xnew = np . arange (0 ,9 ,0.1)
plt . plot (x ,y ,
’o ’, xnew , f ( xnew ) ,
’ - ’)
plt . title (
’ Linear i n t e r p o l a t i o n ’)
plt . show ()
Scientific Computing in Computer Science, Technische Universit¨at M ¨unchen
Example: Interpolation - Cubic Spline
import
numpy as np
import
m a t p l o t l i b . pyplot as plt
from
scipy
import
i n t e r p o l a t e
x = np . arange (0 , 2.25* np . pi , np . pi /4)
y = np . sin ( x )
spline = i n t e r p o l a t e . splrep (x ,y , s =0)
xnew = np . arange (0 ,2.02* np . pi , np . pi /50)
ynew = i n t e r p o l a t e . 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 i n t e r p o l a t i o n ’)