You are on page 1of 6

1

Documentation and Resources

* Download: o Requirements: Python, IPython, Numpy, Scipy, Matplotlib o Windows: google "windows download (Python,IPython,Numpy,Scipy,Matplotlib" o Debian based: sudo apt-get install python ipython numpy scipy matplotlib * o * o o * o o Screencast for IPython http://showmedo.com/videos/video?name=1000010&fromSeriesID=100 Documentation for Plotting http://matplotlib.sourceforge.net/matplotlib.pyplot.html\#-subplot http://matplotlib.sourceforge.net/ Documentation for Arrays/Matrices/Linear Algebra http://www.scipy.org/Numpy_Example_List_With_Doc http://www.scipy.org/Cookbook

Getting Started with Numerical Computations in Python


Listing 1: Numpy examples

#!/ usr/bin/env python from numpy import from numpy . l i n a l g import # defining matrices A = a r r a y ( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] , dtype = f l o a t ) print 3 x3 identity matrix print eye ( 3 ) print 3 x3 matrix filled with 1 print o n e s ( ( 3 , 3 ) ) print repeat matrix along the first axis print r e p e a t (A, 2 , a x i s =1) print array with 3 elements equally spaced from 0 to 5 print l i n s p a c e ( 0 , 5 , 3 ) print populate matrix G = a r r a y ( [ [ s i n ( x)+ c o s ( y ) f o r x in l i n s p a c e ( 0 , 2 pi , 4 ) ] f o r y in l i n s p a c e ( 2 pi , 4 pi , 4 ) ] ) print G print functions operate elementwise on arrays G = s i n (G)

11

16

21

26

31

36

# modifying matrices print transpose of a 2 dim array G t r a n s p o s e d = G. T print second column of A print G [ : , 1 ] print second column and all rows but the last print G[ : 1 , 1 ] x = linspace (0 ,5 ,10) print whole vector x print x print every second element print x [ : : 2 ] print every second element in reverse print x [ : : 2 ]

# l i n a l g procedures print rank (G )= ,

2
41

GETTING STARTED WITH NUMERICAL COMPUTATIONS IN PYTHON rank (G) matrix multiplication if 2D arrays A \ cdot B dot ( o n e s ( ( 3 , 3 ) ) , d i a g ( [ 1 , 2 , 3 ] ) ) inner product dot ( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ) # works a l s o on l i s t s dyadic product v v^T outer ( [ 1 , 2 , 3 ] , [ 4 , 5 ] )

46

print print print print print print print

print elementwise multiplication A[i ,j ]* D[i ,j] print G G t r a n s p o s e d


51

56

print inverse of G try : i n v (G) # t h i s wont work since rank (G)=2 and G a 4x4 matrix except : print matrix inversion failed print solve A x=b b = array ( [ 1 , 2 , 3 ] ) A = d i a g ( o n e s ( 3 ) ) + d i a g ( o n e s ( 2 ) , k=1) x = s o l v e (A, b ) print x

+ d i a g ( o n e s ( 2 ) , k=1)

61

Listing 2: Pylab examples


2

#!/ usr/bin/env python from py la b import import numpy a s npy # f i g u r e with two overlayed plots x = linspace (0 ,10 ,100) y = sin (x) z = cos (x) figure () p1=p l o t ( x , y , b - ) p2=p l o t ( x , z , r. ) x l a b e l ( lala ) y l a b e l ( dumdedum ) l e g e n d ( ( p1 , p2 ) , ( foo , bar ) ) t e x t ( 2 , 0 . 5 , r some text with Latex code $ i arrow ( 0 , 0 , 2 , 1 ) g r i d ( on ) t i t l e ( foo ) s a v e f i g ( lala . eps ) # f i g u r e with subplots figure () subplot (2 ,2 ,1) p1=p l o t ( x , y , b - ) subplot (2 ,2 ,2) p2=p l o t ( x , y , r - ) subplot (2 ,2 ,3) p3=p l o t ( x , y , g - ) subplot (2 ,2 ,4) p4=p l o t ( x , y , k - ) s a v e f i g ( subplots . eps ) # f i g u r e with contour plots of z=f (x , y)= xT A x # A i n d e f i n i t e matrix A = diag ([3 , 2]) x i = npy . r [ 1 0 : 1 0 : 1 0 0 j ] X,Y = meshgrid ( x i , x i ) x = X. r a v e l ( ) # make i t 1D array y = Y. r a v e l ( ) xy = a s a r r a y ( z i p ( x , y ) ) . T z = d i a g ( dot ( xy . T, dot (A, xy ) ) ) # t h i s can d e f i n i t e l y be done more elegantly ; )

12

\ frac {d }{ dt } \ Psi =

H \ Psi $ )

17

22

27

32

37

42

47

52

57

z = z . reshape (100 ,100) figure () imshow ( z ) colorbar () s a v e f i g ( imshow . eps ) # Contour plot # figure () contour ( xi , xi , z ) colorbar () s a v e f i g ( contour . eps ) figure () # # ContourF plot # co n t o u r f ( xi , xi , z ) colorbar () s a v e f i g ( contourf . eps ) show ( ) #pops up some windows

Listing 3: Pitfalls examples


1

#!/ usr/bin/env python from numpy import # a l l objects are passed as references # f o r example d i c t i o n a r i e s ( hash maps, c a l l e d map in c++) a ={ foo : bar , 2 : 1 } print a c = a c [ foo ] = lala print c print a # or arrays A = ones ( ( 2 , 2 ) ) B = A B[ 0 , 0 ] = 1 2 . 3 2 3 print A # a l l ? No, int , f l o a t , etc are immutable in Python a = 13 b = a b = 32 print a # solution C = A. copy ( ) C[ 0 , 1 ] = 1 2 3 4 5 6 print A

11

16

21

26

31

# at the moment: integer d i v i s i o n by standard ( changes in Python 3000) print 2/3 # output : 0 print 2 . / 3 #output 0.666666666

Listing 4: general stu


1

#!/ usr/bin/env python from numpy import print print print print print print print output formatting fixed number of integer digits an int with at least 5 digits length : %5 d %23 fill whitespaces with 0 %05 d %43 floating point %2.7 f % 1.23254234

11

OBJECT ORIENTED PROGRAMMING %2.7 f % 1232323.2 exponential representation %e % ( 6 . 2 3 1 0 2 3 ) insert a string %s % INPUTSTRING

print print print print


16

21

# elementary datatypes print dictionary (= hash map ) mydict = { 1 : 0 , 2 : lala , myarray : a r r a y ( [ 1 , 2 , 3 ] ) } print access with print mydict [ 1 ] , mydict [ 2 ] , mydict [ myarray ] print list m y l i s t = [ 1 , 2 , 3 ]

26

31

class myclass : a=1 b=2 myobject = m y c l a s s ( ) myobject . a = 2 myobject . b = 3 myobject . c = 4 # t h i s a l s o works # looping and i f then e l s e f o r i in r a n g e ( 1 0 ) : # very slow ! ! ! print i i = 0 while True : # slow ! ! ! print i i = i + 1 i f i ==9: break e l i f i == 8 : print 8 yay ! else : print .

36

41

46

51

56

print normal functions def f (A, x ) : return dot (A, x ) x = linspace (0 ,9 ,10) A = ones ( ( 1 0 , 1 0 ) ) print f (A, x ) print lambda functions ( anonymous functions ) f = lambda x : x 2

61

print pass functions as argument of functions def f ( g ) : return g ( 1 . 5 ) print f (lambda x : x 2 )

Object Oriented Programming


Listing 5: Algorithmic dierentiation

#!/ usr/bin/env python import numpy class adouble ( o b j e c t ) : """ derive the class from object """ def i n i t ( s e l f , x , dx = 0 ) :

11

""" this function is automatically called when >>> a = adouble (0) , i.e. runs some initialization . Usually called CONSTRUCTOR """ self .x = x s e l f . dx = dx def str ( self ): """ string representation of adouble : i.e. the result you see when you do : >>> print adouble (0) """ return [%f ,% f] %( s e l f . x , s e l f . dx ) mul ( s e l f , rhs ) : """ multiplication between doubles and / or adboules a*b is equivalent to a. __mul__ (b) """ i f numpy . i s s c a l a r ( r h s ) : return a d o u b l e ( s e l f . x rhs , s e l f . dx r h s ) else : return a d o u b l e ( s e l f . x r h s . x , s e l f . x r h s . dx + s e l f . dx r h s . x ) rmul ( self , lhs ): """ 2 * adouble (0) is equivalent to 2. __mul__ ( adouble (0)) , but integers dont have this functionality . therefore this must be a right multipilcation , i.e. adouble (0). __mul (2) """ return s e l f l h s

16

def

21

26

def

31

if
36

41

name == " __main__ " : # # t h i s i s only executed i f t h i s s c r i p t i s c a l l e d # ## # by $python a l g o r i t h m i c d i f f e r e n t i a t i o n . py a = adouble ( 2 , 1 ) b = adouble (3) print a b print a 2 . print 2 a

1.0

foo

0.5 dumdedum

d some text with Latex code i dt = H

0.0

-0.5

-1.00

lala

Fig. 3.1: Output produced by Listing 2.


foo bar
8 10

OBJECT ORIENTED PROGRAMMING

1.0

1.0

0.5

0.5

0.0

0.0

-0.5

-0.5

-1.0 0 1.0

10

-1.0 0 1.0

10

0.5

0.5

0.0

0.0

-0.5

-0.5

-1.0 0

10

-1.0 0

10

Fig. 3.2: Output produced by Listing 2.


10 240 10 320

240 160 5 5 160

80

80 0 0

-80 -5 -80 -160 -5

-10 -10

-5

10

-160

-10 -10

-5

10

-240

(a)
0 250

(b)

20

200

150

40

100

50

60

-50

80

-100

-150

20

40

60

80

-200

(c)

Fig. 3.3: Output produced by Listing 2. File contour.eps in a) and contourf.eps in b). In c) imshow has been used, it plots the value of every matrix entry in a dierent color.

You might also like