Intro To Scientific Computing With Python
Intro 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
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
Batteries Included Free Portable Easy to earn and !se "ruly #odular
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!
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>
8y default* sort the elements in ascending order& If a compare function is gi2en* use it to sort the list&
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
'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
+emo2e all 9ey 2alue pairs from the dictionary* some_di t&
#)e*0,/!-/#$2( %
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- >
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
If statements
i#=eli#=else pro2ide conditional
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.
# 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
# @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
# $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
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)
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
)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.')
-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!
)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? ??
)rray Constructor
array(se0uence- typecode=Lone- copy=1- savespace=0)
- 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)
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
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,'#'!
>>> 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,'#'!
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!
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
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.
>>> 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!
>>> 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!
Matrix 'ranspose
SHAPE MANIPULATION
s0ueeYe atleast#1d atleast#2d atleast#3d apply#over# a'es vstack hstack colu$n#stack dstack e'pand#di$s
=ead rom column 1 to the end =ead rom line 5 to the end
=ead rom column 1 to the end every second column =ead rom line 5 to the end every second line
"ew e%amples
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!
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
,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 %
Special "unctions
/,$2- $e/,a+
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
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
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
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
>>> 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)