Sie sind auf Seite 1von 87

Introduction to Scientific Computing with Python

Adjusted rom: http://www.nanohub.org/resources/!id"## $riginal Authors are: %ric &ones and 'ravis $liphant Many excellent resources on the web >> google: "learn python" some good example: http://www.diveintopython.org/toc/index.html http://www.scipy.org/Documentation

Topics

Introduction to Python Numeric Computing SciPy and its libraries

What Is Python?
ONE LINER

Python is an interpreted programming language that allows you to do almost anything possible with a compiled language (C C!! "ortran# without re$uiring all the comple%ity&
PYTHON HIGHLIGHTS

Automatic garbage collection Dynamic typing Interpreted and interactive Object-oriented

Batteries Included Free Portable Easy to earn and !se "ruly #odular

Who is using Python?


NATIONAL SPACE TELESCOPE LABORATORY 'ata processing and calibration for instruments on the (ubble Space Telescope& INDUSTRIAL LIGHT AND MAGIC 'igital )nimation PAINT SHOP PRO 8 Scripting .ngine for /)SC PaintShop Pro 0 photo1editing software CONOCOPHILLIPS 3il e%ploration tool suite LAWRENCE LIVERMORE NATIONAL LABORATORIES Scripting and e%tending parallel physics codes& py-PI is their doing& WALT DISNEY 'igital animation de2elopment en2ironment& REDHAT
)naconda* the +edhat ,inu% installer program* is written in Python&

ENTHOUGHT 4eophysics and .lectromagnetics engine scripting* algorithm de2elopment* and 2isuali5ation

,anguage Introduction

Interacti2e Calculator
# adding two values >>> 1 + 1 2 # setting a variable >>> a = 1 >>> a 1 # checking a variables type >>> type(a) <type 'int'> # an arbitrarily long integer >>> a = 1203 0!!03201 >>> a 1203405503201L >>> type(a) <type 'long'> >>>> type(a)"##na$e##==%long% True >>>> print type"##doc#_ type(name, bases, di t! # real nu$bers >>> b = 1"2 + 3"1 >>> b 4.2//////////////' >>> type(b) <type '#loat'> # co$ple' nu$bers >>> c = 2+1"!( >>> c (201.51!
T"e #our numeri types in $yt"on on 32%bit ar "ite tures are& integer (4 byte! long integer (any pre ision! &loat (' byte li(e )*s double! co$ple' (1+ byte! T"e ,umeri module, -"i " -e -ill see later, supports a larger number o# numeri types.

Comple% Numbers
CREATING COMPLEX NUMBERS EXTRACTING COMPONENTS # )se *(* or *+* &or i$aginary # to e'tract real and i$ # part" ,reate by *(real+imag()*- # co$ponent # or *co$ple'(real- imag)* " >>> a=1"!+0"!( >>> 1( . 1+ >>> a"real (%1001! 1.5 >>> 1( . co$ple'(0-1) >>> a"i$ag (-1+0j) 0.5 >>> (1+2()/(1+1() (1.5+0.5j) ABSOLUTE VALUE >>> a=1"!+0"!( >>> abs(a) 1.5'113''

Strings
CREATING STRINGS # using double 0uotes >>> s = 1hello world2 >>> print s hello world # single 0uotes also work >>> s = 3hello world4 >>> print s hello world STRING OPERATIONS # concatenating two strings >>> 1hello 1 + 1world2 3hello world4 # repeating a string >>> 1hello 1 . 3 3hello hello hello 4 STRING LENGTH >>> s = 1123 !2 >>> len(s) 5 FORMAT STRINGS # the 5 operator allows you # to supply values to a # &or$at string" 6he &or$at # string &ollows # , conventions" >>> s = 1so$e nu$bers72 >>> ' = 1"3 >>> y = 2 >>> s = 15s 5&- 5d2 5 (s-'-y) >>> print s some numbers& 1.34, 2

The strings
>>> s = 1hello world2 >>> s"split() 23"ello*, 3-orld*4 >>> 3 3"(oin(s"split()) "ello -orld :egular e'pressions7 re"$atch(rege'-sub(ect) re"search(rege'p-sub(ect) re"group() re"groups() re"sub(rege'- replace$ent-sub)

>>i$port re >>s=26he ti$e is 12730p$;2 >>> s"replace(4world4 -48ars4) >>$=re"$atch(*".ti$e is (".)p$*- s)) 3"ello 5ars* >>$"group(1) '12&30' # strip whitespace >>$"groups() >>> s = 19t hello 9n2 ('12&30',! >>> s"strip() >>$=re"search(r%ti$e".(9d+79d+)p$%-s) 3"ello* >>$"group(1) '12&30' >>re"sub(r%9d+79d+%-%2710%-s) 'T"e time is 2&10pm6'

-ulti1line Strings
# triple 0uotes are used # &or $utli<line strings >>> a = 222hello """ world222 >>> print a hello world # $ulti<line strings using # 192 to indicate continuation >>> a = 1hello 2 9 """ 1world2 >>> print a hello world # including the new line >>> a = 1hello9n2 9 """ 1world2 >>> print a hello world

,ist ob6ects
LIST CREATION WITH BRACKETS >>> l = =10-11-12-13-1 > >>> print l 210, 11, 12, 13, 144 CONCATENATING LIST # si$ply use the + operator >>> =10- 11> + =12-13> 210, 11, 12, 134 REPEATING ELEMENTS IN LISTS # the $ultiply operator # does the trick" >>> =10- 11> . 3 210, 11, 10, 11, 10, 114 range( !ar!" !#$" !e$% # the range $ethod is help&ul # &or creating a se0uence >>> range(!) 20, 1, 2, 3, 44 >>> range(2-?) 22, 3, 4, 5, +4 >>> range(2-?-2) 22, 4, +4

Inde%ing
RETREIVING AN ELEMENT # list # indices7 0 1 2 3 >>> l = =10-11-12-13-1 > >>> l=0> 10 SETTING AN ELEMENT >>> l=1> = 21 >>> print l 210, 21, 12, 13, 144 OUT OF BOUNDS >>> l=10>
6raceback (inner$ost last)7 @ile *Ainteractive input>*-line 1-in B Cnde'Drror7 list inde' out o& range

NEGATIVE INDICES # # # # # negative indices count backward &ro$ the end o& the list" indices7 <! < <3 <2 <1 >>> l = =10-11-12-13-1 > >>> l=<1> 14 >>> l=<2> 13
6he &irst ele$ent in an array has inde'=0 as in ," Take note Fortran programmers!

-ore on list ob6ects


LIST CONTAINING MULTIPLE TYPES # list containing integer# string- and another list" >>> l = =10-4eleven4-=12-13>> >>> l=1> 3ele7en* >>> l=2> 212, 134 # use $ultiple indices to # retrieve ele$ents &ro$ # nested lists" >>> l=2>=0> 12 LENGTH OF A LIST >>> len(l) 3 DELETING OB(ECT FROM LIST # use the del keyword >>> del l=2> >>> l 210,*ele7en*4 DOES THE LIST CONTAIN & ' # use in or not in >>> l = =10-11-12-13-1 > >>> 13 in l 1 >>> 13 not in l 0

Slicing
Slices e%tract a portion of a se$uence by specifying a lower and upper bound& The e%tracted elements start at lower and go up to* but do not include* the upper element& -athematically the range is 7lower*upper#&
SLICING LISTS # indices7 0 1 2 3 >>> l = =10-11-12-13-1 > # =10-11-12-13-1 > >>> l=173> 211, 124 # negative indices work also >>> l=17<2> 211, 124 >>> l=< 73> 211, 124 OMITTING INDICES # o$itted boundaries are # assu$ed to be the beginning # (or end) o& the list" # grab &irst three ele$ents >>> l=73> 210,11,124 # grab last two ele$ents >>> l=<27> 213,144

var=lower7upper>

) few methods for list ob6ects


#)e*+, !-a$$en0( & % #)e*+, !-re)#.e( & %

)dd the element % to the end of the list* some_list&


#)e*+, !-/#1n!( & %

'elete the first occurrence of % from the list&


#)e*+, !-re.er e( %

Count the number of times % occurs in the list&


#)e*+, !-,n0e&( & %

+e2erse the order of elements in the list&


#)e*+, !- #r!( /)$ %

+eturn the inde% of the first occurrence of % in the list&

8y default* sort the elements in ascending order& If a compare function is gi2en* use it to sort the list&

,ist methods in action


>>> l = =10-21-23-11-2 > # add an ele$ent to the list >>> l"append(11) >>> print l =10-21-23-11-2 -11> # how $any 11s are thereB >>> l"count(11) 2 # where does 11 &irst occurB >>> l"inde'(11) 3 # re$ove the &irst 11 >>> l"re$ove(11) >>> print l =10-21-23-2 -11> # sort the list >>> l"sort() >>> print l =10-11-21-23-2 > # reverse the list >>> l"reverse() >>> print l =2 -23-21-11-10>

-utable 2s& Immutable


MUTABLE OB(ECTS # 8utable ob(ects- such as # lists- can be changed # in<place" # insert new values into list >>> l = =10-11-12-13-1 > >>> l=173> = =!-E> >>> print l 210, 5, +, 13, 144
6he cFtringCG $odule treats strings like a &ile bu&&er and allows insertions" Ct4s use&ul when working with large strings or when speed is para$ount"

IMMUTABLE OB(ECTS # C$$utable ob(ects- such as # strings- cannot be changed # in<place" # try inserting values into # a string >>> s = 3abcde4 >>> s=173> = 3'y4
6raceback (inner$ost last)7 @ile *Ainteractive input>*-line 1-in B 6ypeDrror7 ob(ect doesn%t support slice assign$ent

# here4s how to do it >>> s = s=71> + 3'y4 + s=37> >>> print s 'a8yde'

'ictionaries
'ictionaries store key value pairs. Inde%ing a dictionary by a key returns the value associated with it&
DICTIONARY EXAMPLE # create an e$pty dictionary using curly brackets >>> record = HI >>> record=3&irst4> = 3+$es4 >>> record=3last4> = 38a'well4 >>> record=3born4> = 1J31 >>> print record H%&irst%7 %+$es%- %born%7 1J31- %last%7 %8a'well%I # create another dictionary with initial entries >>> new#record = H3&irst47 3+a$es4- 3$iddle473,lerk4I # now update the &irst dictionary with values &ro$ the new one >>> record"update(new#record) >>> print record H%&irst%7 %+a$es%- %$iddle%7 %,lerk%- %last%7%8a'well%- %born%7 1J31I

) few dictionary methods


#)e*0,/!-/+ear( % #)e*0,/!-4e2 ( %

+emo2e all 9ey 2alue pairs from the dictionary* some_di t&
#)e*0,/!-/#$2( %

+eturn a list of all the 9eys in the dictionary&


#)e*0,/!-.a+1e ( %

Create a copy of the dictionary


#)e*0,/!-3a *4e2( & %

+eturn a list of all the 2alues in the dictionary&


#)e*0,/!-,!e) ( %

Test whether the dictionary contains the 9ey 8&

+eturn a list of all the 9ey 2alue pairs in the dictionary&

'ictionary methods in action


>>> d = H3cows47 1-4dogs47!""" 3cats47 3I # create a copy" >>> dd = d"copy() >>> print dd H%dogs%7!-%cats%73-%cows%7 1I # test &or chickens" >>> d"has#key(3chickens4) 0 # get a list o& all keys >>> d"keys() =3cats4-4dogs4-4cows4> # get a list o& all values >>> d"values() =3- !- 1> # return the key/value pairs >>> d"ite$s() =(%cats%- 3)- (%dogs%- !)(%cows%- 1)> # clear the dictionary >>> d"clear() >>> print d HI

Tuples
Tuples are a se$uence of ob6ects 6ust li9e lists& :nli9e lists* tuples are immutable ob6ects& While there are some functions and statements that re$uire tuples* they are rare& ) good rule of thumb is to use lists whene2er you need a generic se$uence&
TUPLE EXAMPLE # tuples are built &ro$ a co$$a separated list enclosed by ( ) >>> t = (1-4two4) >>> print t (1-3two4) >>> t=0> 1 # assign$ents to tuples &ail >>> t=0> = 2 Tra eba ( (innermost last!& 9ile :<intera ti7e input>:, line 1, in ; Type<rror& ob1e t doesn't support item assignment

)ssignment
)ssignment creates ob6ect references&
>>> ' = =0- 1- 2> # y = ' cause ' and y to point # at the sa$e list >>> y = ' # changes to y also change ' >>> y=1> = E >>> print ' =0- E- 2> # re<assigning y to a new list # decouples the two lists >>> y = =3- >

' y ' y ' y

7 7 5

: : 6

9 9

-ultiple assignments
# creating a tuple without () >>> d = 1-2-3 >>> d (1- 2- 3) # $ultiple assign$ents &ro$ a # tuple >>> a-b-c = d >>> print b 2 # also works &or lists >>> a-b-c = =1-2-3> >>> print b 2

# $ultiple assign$ents >>> a-b-c = 1-2-3 >>> print b 2

If statements
i#=eli#=else pro2ide conditional

e%ecution of code bloc9s&


IF STATEMENT FORMAT IF EXAMPLE # a >>> >>> """ """ """ """ """ """ 1 si$ple i& state$ent ' = 10 i& ' > 07 print 1 eli& ' == 07 print 0 else7 print K1 < "it return >

i& Acondition>7 Astate$ents> eli& Acondition>7 Astate$ents> else7 Astate$ents>

Test ;alues
True means any non15ero number or non1empty ob6ect "alse means not true< 5ero* empty ob6ect* or Lone
EMPTY OB(ECTS # e$pty ob(ects evaluate &alse >>> ' = => >>> i& '7 """ print 1 """ else7 """ print 0 """ < "it return > 0

"or loops
"or loops iterate o2er a se$uence of ob6ects&
&or Aloop#var> in Ase0uence>7 Astate$ents>
TYPICAL SCENARIO >>> &or i in range(!)7 """ print i""" < "it return > 0 1 2 3 4 LOOPING OVER A STRING >>> &or i in 3abcde47 """ print i""" < "it return > a b d e LOOPING OVER A LIST >>> l==3dogs4-4cats4-4bears4> >>> accu$ = 34 >>> &or ite$ in l7 """ accu$ = accu$ + ite$ """ accu$ = accu$ + 3 3 """ < "it return > >>> print accu$ dogs ats bears

While loops
While loops iterate until a condition is met& while Acondition>7 Astate$ents>
WHILE LOOP # the condition tested is # whether lst is e$pty" >>> lst = range(3) >>> while lst7 """ print lst """ lst = lst=17> """ < "it return > 20, 1, 24 21, 24 224 BREAKING OUT OF A LOOP # breaking &ro$ an in&inite # loop" >>> i = 0 >>> while 17 """ i& i A 37 """ print i""" else7 """ break """ i = i + 1 """ < "it return > 0 1 2

)natomy of a function
The keyword de& indicates the start of a function. Function ar uments are listed separated by commas. They are passed by assignment. !ore on this later.

Indentation is used to indicate the contents of the function. It is not optional, but a part of the syntax.

de& add(arg0- arg1)7 a = arg0 + arg1 return a A colon ( : ) terminates


the function definition. An optional return statement specifies the value returned from the function. If return is omitted, the function returns the special value Lone.

3ur new function in action


# Me4ll create our &unction # on the &ly in the # interpreter" >>> de& add('-y)7 """ a = ' + y """ return a # test it out with nu$bers >>> ' = 2 >>> y = 3 >>> add('-y) 5 # how about stringsB >>> ' = 3&oo4 >>> y = 3bar4 >>> add('-y) 3#oobar* # &unctions can be assigned # to variables >>> &unc = add >>> &unc('-y) 3#oobar*

# how about nu$bers and stringsB >>> add(3abc%-1) Tra eba ( (innermost last!& 9ile :<intera ti7e input>:, line 1, in ; 9ile :<intera ti7e input>:, line 2, in add Type<rror& annot add type :int: to string

-ore about functions


# # # # Dvery &unction returns a value (or LGLD) but you don%t need to speci&y returned type; # @)L,6CGLOP Q:GR:O88CLR7 # *$ap(f nction- se! ence)* >>> de& cube(')7 return '.'.' """ >>> $ap(cube- range(1- E)) 21, ', 2>, +4, 1254 # *reduce (f nctionse! ence)* >>> de& add('-y)7 return '+y """ >>> reduce(add- range(1- 11)) 55 # *&ilter (f nctionse! ence)* >>> de& &(')7 return ' 5 2 ;= 0 """ >>> &ilter(&- range(2- 10)) 23, 5, >, /4

# @unction docu$entation >>> de& add('-y)7 """ ***this &unction """ adds two nu$bers*** """ a = ' + y """ return a # Nou can always retrieve # &unction docu$entation >>> print add"##doc## t"is #un tion adds t-o numbers

.2en more on functions


# buld<in &unction *dir* is # used to list all # de&initions in a $odule >>> i$port scipy >>> dir(scipy) ....................... ...<a lot o# stu#>... ....................... # Pa$bda &unction7 # Qython supports one<line $ini< # &unctions on the &ly" # Sorrowed &ro$ Pisp- la$bda # &unctions can be used anywhere # a &unction is re0uired" >>> de& &(')7 return '.' >>> $ap(&- range(!)) 20, 1, 4, /, 1+4 >> $ap(la$bda '7 '.'- range(!)) 20, 1, 4, /, 1+4

# $ore on la$bda &unction7 >>> a=range(10) >>> a"sort(la$bda '-y7 c$p(y-')) >>> print a 2/, ', >, +, 5, 4, 3, 2, 1, 04 >>> $ap(la$bda '7 '.2+10- range(!)) 210, 12, 14, 1+, 1'4 >>> print reduce(la$bda '-y7 '+y- range(!)) 10

-odules
EX8-PY # e'1"py QC = 3"1 1E de& su$(lst)7 tot = lst=0> &or value in lst=17>7 tot = tot + value return tot l = =0-1-2-3> print su$(l)- QC FROM SHELL =e(Tbull e(>U python e'1"py +, 3.141+ FROM INTERPRETER # load and e'ecute the $odule >>> i$port e'1 +, 3.141+ # get/set a $odule variable" >>> e'1"QC 3.1415//////////// >>> e'1"QC = 3"1 1!V >>> e'1"QC 3.1415'/////////// # call a $odule variable" >>> t = =2-3- > >>> e'1"su$(t) /

-odules cont.
INTERPRETER # load and e'ecute the $odule >>> i$port e'1 +, 3.141+ < edit #ile > # i$port $odule again >>> i$port e'1 # nothing happens;;; # use reload to &orce a # previously i$ported library # to be reloaded" >>> reload(e'1) 10, 3.1415/ EDITED EX8-PY # e'1"py version 2 QC = 3"1 1!V de& su$(lst)7 tot = 0 &or value in lst7 tot = tot + value return tot l = =0-1-2-3- > print su$(l)- QC

-odules cont. 2
-odules can be e%ecutable scripts or libraries or both&
EX9-PY 1 On e'a$ple $odule 1 QC = 3"1 1E de& su$(lst)7 222 Fu$ the values in a list" 222 tot = 0 &or value in lst7 tot = tot + value return tot EX9-PY CONTINUED de& add('-y)7 2 Odd two values"2 a = ' + y return a de& test()7 l = =0-1-2-3> assert( su$(l) == E) print 3test passed4 # this code runs only i& this # $odule is the $ain progra$ i& ##na$e## == 3##$ain##47 test()

Classes
SIMPLE PARTICLE CLASS
>>> class particle7 """ # ,onstructor $ethod """ de& ##init##(sel&-$ass- velocity)7 """ # assign attribute values o& new ob(ect """ sel&"$ass = $ass """ sel&"velocity = velocity """ # $ethod &or calculating ob(ect $o$entu$ """ de& $o$entu$(sel&)7 """ return sel&"$ass . sel&"velocity """ # a 1$agic2 $ethod de&ines ob(ect4s string representation """ de& ##repr##(sel&)7 """ $sg = *($752"1&- v752"1&)* 5 (sel&"$ass-sel&"velocity) """ return $sg

EXAMPLE
>>> a = particle(3"2- "1) >>> a (m&3.2, 7&4.1! >>> a"$o$entu$() 13.11/////////////

+eading files
FILE INPUT EXAMPLE
>>> results = => >>> & = open(3c799rcs"t't4-4r4) # read lines and discard header >>> lines = &"readlines()=17> >>> &"close() >>> &or l in lines7 """ # split line into &ields """ &ields = line"split() """ # convert te't to nu$bers """ &re0 = &loat(&ields=0>) """ vv = &loat(&ields=1>) """ hh = &loat(&ields=2>) """ # group W append to results """ all = =&re0-vv-hh> """ results"append(all) """ < "it return >

PRINTING THE RESULTS >>> &or i in results7 print i 2100.0, %20.30?, %31.20?4 2200.0, %22.>0?, %33.+0?4 EXAMPLE FILE; RCS-TXT #&re0 (8XY) vv (dS) hh (dS) 100 <20"3 <31"2 200 <22"? <33"E

-ore compact 2ersion


ITERATING ON A FILE AND LIST COMPREHENSIONS >>> results = => >>> & = open(3c799rcs"t't4-4r4) >>> &"readline() 3@#reA (5BC! 77 (dD! "" (dD!En' >>> &or l in &7 """ all = =&loat(val) &or val in l"split()> """ results"append(all) """ < "it return > >>> &or i in results7 """ print i """ < "it return > EXAMPLE FILE; RCS-TXT #&re0 (8XY) 100 200 vv (dS) <20"3 <22"? hh (dS) <31"2 <33"E

Same thing* one line


OBFUSCATED PYTHON CONTEST< >>> print ==&loat(val) &or val in l"split()> &or """ l in open(*c799te$p99rcs"t't*-*r*) """ i& l=0> ;=*#*>

EXAMPLE FILE; RCS-TXT #&re0 (8XY) 100 200 vv (dS) <20"3 <22"? hh (dS) <31"2 <33"E

Sorting
THE CMP METHOD # 6he builtin c$p('-y) # &unction co$pares two # ele$ents and returns # <1- 0- 1 # ' A y <<> <1 # ' == y <<> 0 # ' > y <<> 1 >>> c$p(0-1) %1 # Sy de&ault- sorting uses # the builtin c$p() $ethod >>> ' = =1- -2-3-0> >>> '"sort() >>> ' 20, 1, 2, 3, 44 CUSTOM CMP METHODS # de&ine a custo$ sorting # &unction to reverse the # sort ordering >>> de& descending('-y)7 """ return <c$p('-y)

# 6ry it out >>> '"sort(descending) >>> ' 24, 3, 2, 1, 04

Sorting
SORTING CLASS INSTANCES
# ,o$parison &unctions &or a variety o& particle values >>> de& by#$ass('-y)7 """ return c$p('"$ass-y"$ass) >>> de& by#velocity('-y)7 """ return c$p('"velocity-y"velocity) >>> de& by#$o$entu$('-y)7 """ return c$p('"$o$entu$()-y"$o$entu$()) # Forting particles in a >>> ' = =particle(1"2-3" >>> '"sort(by#$ass) >>> ' 2(m&1.2, 7&3.4!, (m&2.1, >>> '"sort(by#velocity) >>> ' 2(m&4.+, 7&0.>!, (m&2.1, >>> '"sort(by#$o$entu$) >>> ' 2(m&4.+, 7&0.>!, (m&1.2, list by their various properties )-particle(2"1-2"3)-particle( "E-"?)> 7&2.3!, (m&4.+, 7&0.>!4 7&2.3!, (m&1.2, 7&3.4!4 7&3.4!, (m&2.1, 7&2.3!4

Criticism of Python
FUNCTION ARGUMENTS
# Oll &unction argu$ents are called by re&erence" ,hanging data in # subroutine e&&ects global data; >>> de& su$(lst)7 """ tot=0 """ &or i in range(0-len(lst))7 """ lst=i>+=1 """ tot += lst=i> """ return tot >>> a=range(1- ) >>> su$(a) / >>> a 22,3,44 # ,an be &i'ed by >>> a=range(1- ) >>> a#copy = a=7> # be care&ul7 a#copy = a would not work >>> su$(a#copy) / >>> a 21,2,34

Criticism of Python
FUNCTION ARGUMENTS

Python does not support something li9e =const= in C!!& If users chec9s function declaration* it has no clue which arguments are meant as input (unchanged on e%it# and which are output
COPYING DATA

:ser has =no direct contact= with data structures& :ser might not be aware of data handling& Python is optimi5ed for speed 1> references&
>>> a==1-2-3-= -!>> >>> b=a=7> >>> a=0>=2 >>> b 21,2,3,24,544 >>> a=3>=0>=0 >>> b 21,2,3,20,544 # ,an be &i'ed by >>> i$port copy >>> a==1-2-3-= -!>> >>> b = copy"deepcopy(a) >>> a=3>=0>=0 >>> b 21,2,3,24,544

Criticism of Python
CLASS DATA In C!! class declaration unco2ers all important information about the class 1 class members (data and methods#& In Python* data comes into e%istence when used& :ser needs to read implementation of the class (much more code# to find class data and understand the logic of the class& This is particularly important in large scale codes& RELODING MODULES If you import a module in command1line interpreter* but the module was later changed on disc* you can reload the module by typing reload module%%% This reloads the particular module%%%* but does not recursi2ely reload modules that might also be changed on disc and are imported by the module%%%&

NumPy

NumPy and SciPy


(n )**+ ,umarray and ,umeric were merged into common project called ",um-y". $n top o it. /ci-y was build recently and spread very ast in scienti ic community. 0ome: http://www.scipy.org//ci-y IMPORT NUMPY AND SCIPY >>> &ro$ nu$py i$port . >>> i$port nu$py >>> nu$py"##version## *1.0.1* or better >>> &ro$ scipy i$port . >>> i$port scipy >>> scipty"##version## '0.5.2'

)rray 3perations
SIMPLE ARRAY MATH >>> a = array(=1-2-3- >) >>> b = array(=2-3- -!>) >>> a + b array(23, 5, >, /4! MATH FUNCTIONS # ,reate array &ro$ 0 to 10 >>> ' = arange(11") # $ultiply entire array by # scalar value >>> a = (2.pi)/10" >>> a 0.+2'31'530>1' >>> a.' array(2 0.,0.+2',?,+.2'34! # apply &unctions to array" >>> y = sin(a.')

,um$y defines the followin constants" pi F 3.1415/2+535/ e F 2.>1'2'1'2'4+

Introducing Numeric )rrays


SIMPLE ARRAY CREATION >>> a = array(=0-1-2-3>) >>> a array(20, 1, 2, 34! CHECKING THE TYPE >>> type(a) <type 'array'> NUMERIC TYPE OF ELEMENTS >>> a"typecode() 'l3 # 3l4 = Cnt BYTES IN AN ARRAY ELEMENT >>> a"ite$siYe() 4 ARRAY SHAPE >>> a"shape (4,! >>> shape(a) (4,! CONVERT TO PYTHON LIST >>> a"tolist() 20, 1, 2, 34 ARRAY INDEXING >>> a=0> 0 >>> a=0> = 10 >>> a 210, 1, 2, 34

-ulti1'imensional )rrays
MULTI=DIMENSIONAL ARRAYS >>> a = array(== 0- 1- 2- 3>=10-11-12-13>>) >>> a array(22 0, 1, 2, 34, 210,11,12,1344! (ROWS"COLUMNS% >>> shape(a) (2, 4! GET>SET ELEMENTS >>> a=1-3> 13
column row

ADDRESS FIRST ROW USING SINGLE INDEX >>> a=1> array(210, 11, 12, 134! FLATTEN TO 8D ARRAY >>> a"&lat array(0,1,2,3,10,11,12,%1! >>> ravel(a) array(0,1,2,3,10,11,12,%1! A-FLAT AND RAVEL(% REFERENCE ORIGINAL MEMORY >>> a"&lat=!> = <2 >>> a array(22 0, 1, 2, 34, 210,<2,12,%144!

>>> a=1-3> = <1 >>> a array(22 0, 1, 2, 34, 210,11,12,<144!

)rray Slicing
SLICING WORKS MUCH LIKE STANDARD PYTHON SLICING >>> a=0-37!> array(23, 44! >>> a= 7- 7> array(2244, 454, 254, 5544! >>> a=7-2> array(22,12,22,32,42,524! STRIDES ARE ALSO POSSIBLE >>> a=2772-772> array(==20- 22- 2 >= 0- 2>>)
7 87 97 57 67 ?7 8 88 98 58 68 ?8 9 89 99 59 69 ?9 5 85 95 55 65 ?5 6 86 96 56 66 ?6 ? 8? 9? 5? 6? ??

Slices )re +eferences


Slices are references to memory in original array. Changing values in a slice also changes the original array.
>>> a = array(=0-1-2>) # create a slice containing only the # last ele$ent o& a >>> b = a=273> >>> b=0> = 10 # changing b changed a; >>> a array(2 1, 2, 104!

)rray Constructor
array(se0uence- typecode=Lone- copy=1- savespace=0)

se0uence typecode copy savespace

- any type of #ython se$uence. %ested list create multi&dimensional arrays. - character (strin ). 'pecifies the numerical type of the array. If it is %one, the constructor makes its best uess at the numeric type. - if copy=0 and se$uence is an array ob(ect, the returned array is a reference that data. )therwise, a copy of the data in se0uence is made. - Forces %umeric to use the smallest possible numeric type for the array. Also, it prevents upcastin to a different type durin math operations with scalars. (see coercion section for more details)

)rray Constructor .%amples


FLOATING POINT ARRAYS DEFAULT TO DOUBLE PRECISION >>> a = array(=0-1"-2-3>) >>> a"dtype() 3d3 notice decimal USE TYPECODE TO REDUCE PRECISION >>> a = array(=0-1"-2-3>-%&%) >>> a"dtype() '#3 >>> len(a"&lat).a"ite$siYe() 1+ ARRAYS REFERENCING SAME DATA >>> a = array(=1-2-3- >) >>> b = array(a-copy=0) >>> b=1> = 10 >>> a array(2 1, 10, 3, 44!

BYTES FOR MAIN ARRAY STORAGE # &lat assures that # $ultidi$ensional arrays # work >>>len(a"&lat).a"ite$siYe 32

?@1bit Typecodes
C3ara/!er D 8 0 + i s 1 3one4 u w b $ B,! (B2!e % 1)7 3124 26 374 26 374 5) 364 5) 364 5) 364 12 3)4 7 314 5) 364 12 3)4 7 6 314 314 ,o$ple', )omple8+4 )omple80, )omple8', )omple81+, )omple832 @loat, 9loat+4 9loat0, 9loat', 9loat1+, 9loat32 Cnt Hnt32 Hnt1+ Hnt' GnsignedHnt32 GnsignedHnt1+ GnsignedHnt' $yIb1e t I0en!,@,er

*i hli hted typecodes correspond to #ython+s standard %umeric types.

)rray Creation "unctions


arange(start-stop=Lone-step=1-typecode=Lone)
Nearly identical to Pythons range(). Creates an array of values in the range [start,stop) with the s ecified step value. !llows non-integer values for start, stop, and step. "hen not s ecified, typecode is derived from the start, stop, and step values. >>> arange(0-2.pi-pi/ ) array(2 0.000, 0.>'5, 1.5>1, 2.35+, 3.142, 3./2>, 4.>12, 5.4/>4!

ones(shape-typecode=Lone-savespace=0) Yeros(shape-typecode=Lone-savespace=0)
shape is a num#er or se$uence s ecifying the dimensions of the array. %f typecode is not s ecified, it defaults to Cnt. >>> ones((2-3)-typecode=@loat32) array(22 1., 1., 1.4, 2 1., 1., 1.44,'#'!

)rray Creation "unctions (cont&#


identity(n-typecode=3l4)
&enerates an n #y n identity matri' with typecode = Cnt.

>>> identity( ) array(221, 0, 0, 04, 20, 1, 0, 04, 20, 0, 1, 04, 20, 0, 0, 144! >>> identity( -4&4) array(22 1., 0., 0., 0.4, 2 0., 1., 0., 0.4, 2 0., 0., 1., 0.4, 2 0., 0., 0., 1.44,'#'!

-athematic 8inary 3perators


a + b a < b a 5 b add(a-b) subtract(a-b) re$ainder(a-b) a . b a / b a .. b $ultiply(a-b) divide(a-b) power(a-b)

MULTIPLY BY A SCALAR >>> a = array((1-2)) >>> a.3" array(23., +.4! ELEMENT BY ELEMENT ADDITION >>> a = array(=1-2>) >>> b = array(=3- >) >>> a + b array(24, +4!

ADDITION USING AN OPERATOR FUNCTION >>> add(a-b) array(24, +4! IN PLACE OPERATION # Gverwrite contents o& a" # Faves array creation # overhead >>> add(a-b-a) # a += b array(24, +4! >>> a array(24, +4!

Comparison and ,ogical 3perators


e0ual greater#e0ual logical#and logical#not
9D EXAMPLE >>> a = array(((1-2-3- )-(2-3- -!))) >>> b = array(((1-2-!- )-(1-3- -!))) >>> a == b array(221, 1, 0, 14, 20, 1, 1, 144! # &unctional e0uivalent >>> e0ual(a-b) array(221, 1, 0, 14, 20, 1, 1, 144!

(==) (>=) (and) (not)

not#e0ual (;=) less (A) logical#or (or)

greater (>) less#e0ual (A=) logical#'or

8itwise 3perators
bitwise#and bitwise#or (W) (Z) invert ([) bitwise#'or right#shi&t(a-shi&ts) le&t#shi&t (a-shi&ts)

BITWISE EXAMPLES >>> a = array((1-2- -J)) >>> b = array((1E-32-E -12J)) >>> bitwise#and(a-b) array(2 1>, 34, +', 13+4! # bit inversion >>> a = array((1-2-3- )-)nsignedCntJ) >>> invert(a) array(2254, 253, 252, 2514,%b%! # surprising type conversion >>> le&t#shi&t(a-3) array(2 ', 1+, 24, 324,%i%! ,han ed from -nsignedHnt' to Hnt32

Trig and 3ther "unctions


TRIGONOMETRIC OTHERS

sin(') cos(') arccos(')

sinh(') cosh(') arccosh(')

arctan(') arctanh(') arcsin(') arcsinh(') arctan2('-y)

e'p(') log10(') absolute(') negative(') &loor(') hypot('-y) $a'i$u$('-y)


hypot('-y)

log(') s0rt(') con(ugate(') ceil(') &abs(') &$od('-y) $ini$u$('-y)

(lement #y element distance calculation using x . + y .

SciPy

32er2iew
CURRENT PACKAGES $pecial Functions %scipy&special' Input1Output %scipy&io'

+enetic Algorit2ms $ignal Processing %scipy&signal' %scipy&ga' Fourier "rans(orms %scipy&((tpac)' Optimi*ation %scipy&optimi*e' +eneral plotting %scipy&,plt.plt- gplt/' 0umerical Integration %scipy&integrate' inear Algebra %scipy&linalg' $tatistics %scipy&stats' Distributed 3omputing %scipy&co4' Fast E.ecution %4eave' 3lustering Algorit2ms %scipy&cluster' $parse #atrices5 %scipy&sparse'

8asic .n2ironment
CONVENIENCE FUNCTIONS
>>> in&o(linspace)
linspa e(start, stop, numF50, endpointF1, retstepF0! <7enly spa ed samples. Jeturn num e7enly spa ed samples #rom start to stop. H# endpointF1 t"en last sample is stop. H# retstep is 1 t"en return t"e step 7alue used.

in#o help system or scipy similar to dir or the rest o python

>>> linspace(<1-1-!)
array(2%1. , %0.5, 0. , 0.5, 1. 4!

linspa e get e9ually spaced points. r#=> also does this 3shorthand4

>>> r#=<1717!(>
array(2%1. , %0.5, 0. , 0.5, 1. 4!

>>> logspace(0-3- )
array(2 1., 10., 100., 1000.4!

logspa e get e9ually spaced points in log1* domain

>>> in&o(logspace)
logspa e(start, stop, numF50, endpointF1! <7enly spa ed samples on a logarit"mi s ale. H#

Jeturn num e7enly spa ed samples #rom 10KKstart to 10KKstop. endpointF1 t"en last sample is 10KKstop.

8asic .n2ironment
CONVENIENT MATRIX GENERATION AND MANIPULATION
>>> O = $at(31-2- \ -!-E\?-J-V4) >>> O=$at(==1-2- >-= -!-E>-=?-J-V>>) >>> print O
5atri8(221, 2, 44, 22, 5, 34, 2>, ', /44!

/imple creation o matrix with :;< meaning row separation

Matrix -ower Matrix Multiplication and Matrix (nverse

>>> print O..


5atri8(22 +4/>, /5'0, /'3+4, 2 >13', 105+1, 10'1'4, 21'434, 2>220, 2>/4544!

>>> print O.O"C


5atri8(22 1., 0., 0.4, 2 0., 1., 0.4, 2 0., 0., 1.44!

>>> print O"6


5atri8(221, 2, >4, 22, 5, '4, 24, 3, /44!

Matrix 'ranspose

-ore 8asic "unctions


TYPE HANDLING
isco$ple'ob( real#i&#close isco$ple' isrealob( isreal i$ag real isscalar isnegin& isposin& isin& is&inite isnan nan#to#nu$ co$$on#type cast typena$e

OTHER USEFUL FUNCTIONS


select e'tract insert &i' $od a$a' a$in ptp split hsplit vsplit dsplit apply#along# a'is su$ cu$su$ prod cu$prod di&& angle unwrap sort#co$ple' tri$#Yeros &liplr &lipud rotV0 eye diag &actorial &actorial2 co$b pade derivative li$its"]]]] roots poly any all disp uni0ue e'tract insert nansu$ nan$a' nanarg$a' nanarg$in nan$in

SHAPE MANIPULATION
s0ueeYe atleast#1d atleast#2d atleast#3d apply#over# a'es vstack hstack colu$n#stack dstack e'pand#di$s

Input and 3utput


/,$2-,# === Rea0,ng an0 Ar,!,ng ASCII @,+e
te8t#ile.t8t Ftudent 6est1 +ane +on +i$ VJ"3 ?"2 J "2 6est2 V "2 V"1 J!"3 6est3 V!"3 ! "2 V "1 6est V1"3 3 "? ?E"

=ead rom column 1 to the end =ead rom line 5 to the end

>>> a = io"read#array(3te't&ile"t't4-colu$ns=(1-<1)-lines=(3-<1)) >>> print a


22 /'.3 2 4>.2 2 '4.2 /4.2 4/.1 '5.3 /5.3 54.2 /4.1 /1.34 34.>4 >+.444

>>> b = io"read#array(3te't&ile"t't4-colu$ns=(1-<2)-lines=(3-<2)) >>> print b


22 /'.3 2 '4.2 /5.34 /4.144

=ead rom column 1 to the end every second column =ead rom line 5 to the end every second line

Input and 3utput


/,$2-,# === Rea0,ng an0 Ar,!,ng raA B,nar2 @,+e

id " open3 ile>name. permission"?rb?. ormat"?n?4


@lass or reading and writing binary iles into ,umeric arrays. Methods
A ile>name 'he complete path name to the ile to open. Apermission $pen the ile with given permissions: 3?r?. ?w?. ?a?4 or reading. writing. or appending. 'his is the same as the mode argument in the builtin open command. A ormat 'he byteBordering o the ile: 3C?native?. ?n?D. C?ieeeBle?. ?l?D. C?ieeeBbe?. ?b?D4 or native. littleB endian. or bigBendian. read read data rom ile and return ,umeric array write write to ile rom ,umeric array ort>read read 8ortranB ormatted binary data rom the ile. ort>write write 8ortranB ormatted binary data to the ile. rewind rewind to beginning o ile siEe get siEe o ile seeF seeF to some position in the ile tell return current position in ile close close the ile

"ew e%amples

.%amples of SciPy use

Integration
$uppose 4e 4ant to integrate Bessel (unction x >>> in&o(integrate) 0 .....<do umentation o# integrate module>..... >>> integrate"0uad(la$bda t7 special"(1(t)/t-0-pi) (1.0+2/10/>14/4,1.1'e%14!

dtJ/ (t ) 1 t

j1int.py module:
&ro$ scipy i$port . de& &un(')7 return integrate"0uad(la$bda t7 special"(1(t)/t-0-') '=r#=073070"01> &or t' in '7 print t'- &un(t')=0>

-inimi5ation
$uppose 4e 4ant to minimi*e t2e (unction >>> &ro$ scipy i$port . . ( x a ) + (y >>> i$port scipy >>> in&o(scipy) .... <do umentation o# all a7ailable modules> >>> in&o(opti$iYe) >>> in&o(opti$iYe"&$in#powell)

b) . = min

>>> de& &unc(('-y)-(a-b))7 return ('<a)..2+(y<b)..2 /tarting guess >>> opti$iYe"&$in#powell(&unc- (0-0)- ((!-E)-)) IpimiCation terminated su ess#ully, )urrent #un tion 7alue& 0.00000 additional arguments Hterations& 2 9un tion e7aluations& 3' array(25.,+.4!

+oot finding and integration


x

6&7 7&8 7&9 7&: 7&; 7&7 7 < 67

dtJ/ (t ) 1 t
0

'he unction

dtJ (t ) 1 t == / 2
/ 0

6<

;7

;<

has many solutions. /uppose we want to ind all solution in the range C*:1**D

Put it all together


&ro$ scipy i$port . ::: 9inds all solutions o# t"e eAuation Hntegrate211(t!=t,Lt,0,8M4 FF 1 in t"e range 8F20,1004 ::: de& &unc('-a)7 : )omputes Hntegrate211(t!=t,Lt,0,8M4 % a: return integrate"0uad(la$bda t7 special"(1(t)/t- 0- ')=0> < a @ 9inds appro8iate solutions o# t"e eAuation in t"e range 20&1004 ' = r#=0710070"2> @ reates an eAualy spa ed array b = $ap(la$bda t7 &unc(t-1)- ') @ e7aluates #un tion on t"is array Y = =>\ @ appro8imate solutions o# t"e eAuation &or i in range(1-len(b))7 @ i# t"e #un tion "anges sign, i& (b=i<1>.b=i>A0)7 Y"append('=i>) @ t"e solution is bra (eted print *^eros o& the e0uation in the interval =07100> are* (=0 &or Yt in Y7 print (- opti$iYe"&solve(&unc-Yt-(1-)) # calling root &inding routine- &inds all Yeros" (+=1

It ta9es around @ seconds to get


Geros o the e9uation in the interval C*:1**D are * ).2+H6767)6+H 1 +.2H)+6H6*51H ) 7.H+##*166#2H 5 11.7H))6)5#+ 6 16.##+H2H+5)# + 17.1)+122)6)) 2 )1.)+7**)H++5 H )6.5#5*16H2)7 7 )H.+)#6722H)7 # 5*.222#76*12 1* 55.7*+))75676 11 52.#66*55)+6# 1) 6*.*75)2#52*2 15 65.)))766151+ 16 62.52)27#227 1+ 6#.+*)H++*577 12 +).265**15*57 1H ++.H755#71775 17 +7.#)5#)17*57 1# 2).*26++5*+1+ )* 2+.)*+)H267*7 )1 27.562*H#6+#) )) H1.672#+1++76 )5 H6.2)H776*#62 )6 HH.H2772#HH72 )+ 7*.#*##*)6622 )2 76.*+*#H27+1# )H 7H.1#)*776### )7 #*.555)55+177 )# #5.6H66*7++6# 5* #2.21+21*27# 51 ##.H+275H5276

,inear )lgebra
/,$2-+,na+g === FAST LINEAR ALGEBRA

AU e ATLAS ,@ a.a,+aB+e === .er2 @a ! AL#A=+e.e+ a//e !# BLAS an0 LAPACK r#1!,ne ,n )#01+e linalg"&blas" an0 linalg"&lapack (FORTRAN #r0er% AH,g3 +e.e+ )a!r,& r#1!,ne
AL,near A+geBra Ba ,/ ; inv" solve" det" nor$" lsts0" pinv ADe/#)$# ,!,#n ; eig" lu" svd" orth" cholesky" 0r" schur AMa!r,& F1n/!,#n ; e'p$" log$" s0rt$" cos$" cosh$" &un$ (genera+
)a!r,& @1n/!,#n %

Some simple e%amples


>>> O=$atri'(rando$"rand(!-!)) # creates rando$ $atri' >>> O"C <in7erse o# t"e random matri8> >>> linalg"det(O) <determinant o# t"e matri8> >>> linalg"eigvals(O) <eigen7alues only> >>> linalg"eig(O) <eigen7alues and eigen7e tors> >>> linalg"svd(O) <NOP de omposition> >>> linalg"cholesky(O) <)"oles(y de omposition #or positi7e de#inite Q> >>> S=$atri'(rando$"rand(!-!)) >>> linalg"solve(O-S) <Nolution o# t"e eAuation Q.RFD>

Special "unctions
/,$2- $e/,a+

Includes over ;77 (unctions=


Airy- Elliptic- Bessel- +amma- >yper+eometric- $truve- ErrorOrt2ogonal Polynomials- Parabolic 3ylinder- #at2ieu$p2eroidal ?ave- @elvin FIRST ORDER BESSEL EXAMPLE
#environ$ent setup >>> i$port gui#thread >>> gui#thread"start() >>> &ro$ scipy i$port . >>> i$port scipy"plt as plt

>>> ' = r#=0710070"1> >>> (0' = special"(0(') >>> plt"plot('-(0')

Special "unctions
/,$2- $e/,a+ AIRY FUNCTIONS EXAMPLE
>>> Y = r#=<!71"!7100(> >>> vals = special"airy(Y) >>> 'plt"&igure(0- &ra$e=1color=%blue%) >>> 'plt"$atplot(Y-vals) >>> 'plt"legend(=%Oi%- %Oip%3Si3-%Sip%>color=%blue%) >>> 'plt"'label(%Y%color=%$agenta%) >>> 'plt"title(%Oiry @unctions and _erivatives3)

Statistics
/,$2- !a! === C#n!,n1#1 D, !r,B1!,#n over 7* continuous distributionsI

Methods pd& cd& rvs pp& stats

Statistics
/,$2- !a! === D, /re!e D, !r,B1!,#n 1* standard discrete distributions 3plus any arbitrary inite =J4 Methods pd& cd& rvs pp& stats

Statistics
/,$2- !a! === Ba ,/ S!a!, !,/a+ Ca+/1+a!,#n @#r a)$+e

Sstats"$ean (a+ # $ean) Sstats"std (a+ # std) Sstats"var Sstats"$o$ent Sstats"skew Sstats"kurtosis

/#)$1!e !3e a)$+e )ean /#)$1!e !3e a)$+e !an0ar0 0e.,a!,#n a)$+e .ar,an/e a)$+e /en!ra+ )#)en! a)$+e 4eA a)$+e 41r!# ,

Interpolation
/,$2-,n!er$#+a!e === Genera+ $1r$# e In!er$#+a!,#n

A8=0 +,near In!er$#+a!,ng C+a A@onstructs callable unction rom data points A8unction taFes vector o inputs and returns linear interpolants A8=0 an0 9=0 $+,ne ,n!er$#+a!,#n (FITPACK% A/plines up to order + A-arametric splines

Integration
/,$2-,n!egra!e === Genera+ $1r$# e In!egra!,#n

AOr0,nar2 D,@@eren!,a+ EC1a!,#n (ODE%


integrate"odeint- integrate"ode

ASa)$+e #@ a 8=0 @1n/!,#n


integrate"trapY 3trapeEoidal Method4. integrate"si$ps 3/impson Method4. integrate"ro$b 3=omberg Method4

AArB,!rar2 /a++aB+e @1n/!,#n


integrate"0uad 3general purpose4. integrate"dbl0uad 3double integration4. integrate"tpl0uad 3triple integration4. integrate"&i'ed#0uad 3 ixed order Kaussian integration4. integrate"0uadrature 3Kaussian 9uadrature to tolerance4. integrate"ro$berg 3=omberg4

Integration
/,$2-,n!egra!e === E&a)$+e

>>> de& &unc(')7 return integrate"0uad(cos-0-')=0> >>> vec&unc = vectoriYe(&unc) >>> >>> >>> >>> >>> ' = r#=072.pi7100(> '2 = '=77!> y = sin(') y2 = vec&unc('2) 'plt"plot('-y-'2-y2-%r'%)

3ptimi5ation
/,$2-#$!,),De === 1n/#n !ra,ne0 ),n,),Da!,#n an0 r##! @,n0,ng

Un/#n !ra,ne0 O$!,),Da!,#n


&$in 3,elderBMead simplex4. &$in#powell 3-owellLs method4. &$in#b&gs 3M8K/ 9uasiB,ewton method4. &$in#ncg 3,ewton conjugate gradient4. leasts0 3NevenbergBMar9uardt4. anneal 3simulated annealing global minimiEer4. brute 3brute orce global minimiEer4. Bren! 3excellent 1BD minimiEer4. g#+0en. Bra/4e!

C#n !ra,ne0 O$!,),Da!,#n


&$in#l#b&gs#b" &$in#tnc 3truncated newton code4. &$in#cobyla 3constrained optimiEation by linear approximation4. &$inbound 3interval constrained 1Bd minimiEer4

R##! @,n0,ng
&solve 3using M(,-A@O4. brent0. brenth. ridder. newton. bisect. &i'ed#point 3 ixed point e9uation solver4

3ptimi5ation
EXAMPLE; MINIMIEE BESSEL FUNCTION
# $ini$iYe 1st order bessel # &unction between and ? >>> &ro$ scipy"special i$port (1 >>> &ro$ scipy"opti$iYe i$port 9 &$inbound >>> >>> >>> >>> >>> >>> ' = r#=27?"17"1> (1' = (1(') plt"plot('-(1'-4<4) plt"hold(3on4) (1#$in = &$inbound((1- -?) plt"plot('-(1#$in-4ro4)

3ptimi5ation
EXAMPLE; SOLVING NONLINEAR EFUATIONS /olve the nonBlinear e9uations

starting location or search

>>> de& nonlin('-a-b-c)7 >>> '0-'1-'2 = ' >>> return =3.'0<cos('1.'2)+ a>>> '0.'0<J1.('1+0"1)..2 + sin('2)+b>>> e'p(<'0.'1)+20.'2+c> >>> a-b-c = <0"!-1"0E-(10.pi<3"0)/3 >>> root = opti$iYe"&solve(nonlin=0"1-0"1-<0"1>-args=(a-b-c)) >>> print root >>> print nonlin(root-a-b-c)
= 0"! 0" <0"!23E> =0"0- <2"23110 1V0e<12- ?" E0EVJ?2e<1 >

3ptimi5ation
EXAMPLE; MINIMIEING ROSENBROCK FUNCTION =osenbrocF unction WITHOUT DERIVATIVE
>>> >>> >>> >>> >>> '0>>> >>> rosen = opti$iYe"rosen i$port ti$e '0 = =1"3-0"?-0"J-1"V-1"2> start = ti$e"ti$e() 'opt = opti$iYe"&$in(rosenavegtol=1e<?) stop = ti$e"ti$e() print#stats(start- stop- 'opt) >>> >>> >>> >>> '0>>> >>>

USING DERIVATIVE
rosen#der = opti$iYe"rosen#der '0 = =1"3-0"?-0"J-1"V-1"2> start = ti$e"ti$e() 'opt = opti$iYe"&$in#b&gs(rosen&pri$e=rosen#der- avegtol=1e<?) stop = ti$e"ti$e() print#stats(start- stop- 'opt)

O$!,),Da!,#n !er),na!e0 1//e @1++2C1rren! @1n/!,#n .a+1e; 7-777777 I!era!,#n ; 58: F1n/!,#n e.a+1a!,#n ; ?55 F#1n0 ,n 7-787?9JJ9897I6 e/#n0 S#+1!,#n; G 8- 8- 8- 8- 8-H F1n/!,#n .a+1e; 9-:III?I:78?Ie=8? A.g- Err#r; 8-?595J7:8JJe=78

O$!,),Da!,#n !er),na!e0 1//e @1++2C1rren! @1n/!,#n .a+1e; 7-777777 I!era!,#n ; 888 F1n/!,#n e.a+1a!,#n ; 9:: Gra0,en! e.a+1a!,#n ; 889 F#1n0 ,n 7-7?9889879?78? e/#n0 S#+1!,#n; G 8- 8- 8- 8- 8-H F1n/!,#n .a+1e; 8-5I5J8756I?e=88 A.g- Err#r; 8-8596:756II9e=87

4) and Clustering
/,$2-ga === Ba ,/ Gene!,/ A+g#r,!3) O$!,),Da!,#n

=outines and classes to simpli y setting up a genome and running a genetic algorithm evolution
/,$2-/+1 !er === Ba ,/ C+1 !er,ng A+g#r,!3)

AOB er.a!,#n A3,!en,ng AVe/!#r C1an!,Da!,#n AK=)ean a+g#r,!3)

cluster"v0"whiten cluster"v0"v0 cluster"v0"k$eans

Das könnte Ihnen auch gefallen