La lecture en ligne est gratuite
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
Télécharger Lire

Python for Scientific Computing

De
29 pages
PythonOverviewScientificToolsAdditionalTools&ResourcesPythonforScientificComputingIvanLimaWoodsHoleOceanographicInstitutionDept. ofMarineChemistry&Goechemistry(ivan@whoi.edu)November14,2006IvanLima(ivan@whoi.edu) PythonforScientificComputingPythonOverviewScientificToolsAdditionalTools&ResourcesOutlinePythonOverviewScientificToolsArrayObjectModulesGraphics&PlottingScientificPackagesExtendingPythonAdditionalTools&ResourcesIvanLima(ivan@whoi.edu) PythonforScientificComputingPythonOverviewScientificToolsAdditionalTools&ResourcesWhatisPython?Pythonisanelegantandrobustprogramminglanguagethatcombinesthepowerandflexibilityoftraditionalcompiledlanguageswiththeease of use ofsimplerscriptingandinterpretedlanguages.I HighlevelI InterpretedI ScalableI ExtensibleI PortableI Easytolearn,readandmaintainI RobustI ObjectOrientedI VersatileIvanLima(ivan@whoi.edu) PythonforScientificComputingPythonOverviewScientificToolsAdditionalTools&ResourcesWhyPython?1. Free&opensource2. Availableonawidevarietyofplatforms3. Bettersupportforarrayswithmorethan2dimensions4. BettersupportforwrappingFORTRAN77/90/95&C/C++code5. Farbettermemorymanagement6. Muchwiderlibrarysupportfornon numericalwork7. FarmoreoptionsforafullfeaturedGUI8. Easiertocreatestand aloneapplicationson anyplatform.9. TrulyObjectOrientedIvanLima(ivan@whoi.edu) ...
Voir plus Voir moins
Python Overview Scientific Tools Additional Tools & Resources
Python for Scientific Computing
Ivan Lima
Woods Hole Oceanographic Institution Dept. of Marine Chemistry & Goechemistry (udew@na.iohiv)
November 14, 2006
Ivan Lima (ivan@whoi.edu)
Python for Scientific Computing
Outline
Python Overview Scientific Tools Additional Tools & Resources
Python Overview
Scientific Tools Array Object Modules Graphics & Plotting Scientific Packages Extending Python
Additional Tools & Resources
Ivan Lima (ivan@whoi.edu)
Python for Scientific Computing
Python Overview Scientific Tools Additional Tools & Resources
What is Python?
Python is anelegant and robustprogramming language that combines thepower and flexibilityof traditional compiled languages with the ease-of-useof simpler scripting and interpreted languages. IHigh level IInterpreted IScalable IExtensible IPortable IEasy to learn, read and maintain IRobust IObject Oriented IVersatile
Ivan Lima (ivan@whoi.edu) Python for Scientific Computing
Why Python?
Python Overview Scientific Tools Additional Tools & Resources
1.Free & open source 2.Available on a wide variety of platforms 3.Better support for arrays with more than 2 dimensions 4.Better support for wrapping FORTRAN 77/90/95 & C/C++ code 5.Far better memory management 6.Much wider library support for non-numerical work 7.Far more options for a full featured GUI 8.Easier to create stand-alone applications onanyplatform. 9.Truly Object Oriented
Ivan Lima (ivan@whoi.edu for Scientific Computing) Python
Numeric
Python Overview Scientific Tools Additional Tools & Resources
IOriginal array module IVery successful ILarge code base IStill around ILimited
Ivan Lima (ivan@whoi.edu)
Array Object Modules Graphics & Plotting Scientific Packages Extending Python
Python for Scientific Computing
Numarray
Python Overview Scientific Tools Additional Tools & Resources
Array Object Modules Graphics & Plotting Scientific Packages Extending Python
ICreated at the STSCI IVery powerful & lots of new features IFast for large arrays but slow for small arrays IIncompatible with existing code base (Numeric/SciPy) ISlow acceptance IFragmented community
Ivan Lima (ivan@whoi.edu)
Python for Scientific Computing
NumPy
Python Overview Scientific Tools Additional Tools & Resources
Array Object Modules Graphics & Plotting Scientific Packages Extending Python
IBest of both Numeric & Numarray IVery powerful & flexible ICore array object for SciPy IInclude migration tools for Numeric INumeric development has ceased INumarray development moving to NumPy
Ivan Lima (ivan@whoi.edu for Scientific Computing) Python
Python Overview Scientific Tools Additional Tools & Resources
Python/NumPy Example
>>> import numpy >>> a = numpy.arange(12) >>> print a [ 0 1 2 3 4 5 6 7 8 9 10 11] >>> print a.size, a.ndim, a.shape 12 1 (12,) >>> a = numpy.reshape(a,(3,4)) >>> print a [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] >>> print a.size, a.ndim, a.shape 12 2 (3, 4) >>> print a.min(), a.max(), a.mean(), a.std() 0 11 5.5 3.45205252953 >>> print a[:,0] [0 4 8] >>> print a[2,:] [ 8 9 10 11] >>> b = numpy.arange(3) >>> c = numpy.arange(4) >>> b * c ValueError: shape mismatch: objects cannot be broadcast to a single shape >>> print b, c [0 1 2] [0 1 2 3]
Ivan Lima (ivan@whoi.edu)
Array Object Modules Graphics & Plotting Scientific Packages Extending Python
>>> print numpy.reshape(b,(3,1)) [[0] [1] [2]] >>> print numpy.reshape(b,(3,1)) * c [[0 0 0 0] [0 1 2 3] [0 2 4 6]] >>> print b[:,numpy.newaxis] * c [[0 0 0 0] [0 1 2 3] [0 2 4 6]] >>> print a.shape, c.shape (3, 4) (4,) >>> print a * c [[ 0 1 4 9] [ 0 5 12 21] [ 0 9 20 33]] >>> print a.shape, b.shape (3, 4) (3,) >>> print a * b[:,numpy.newaxis] [[ 0 0 0 0] [ 4 5 6 7] [16 18 20 22]] >>> print a.shape, b[:,numpy.newaxis].shape (3, 4) (3,1)
Python for Scientific Computing
Python Overview Scientific Tools Additional Tools & Resources
Matplotlib/Basemap
Array Object Modules Graphics & Plotting Scientific Packages Extending Python
IEmulates Matlab syntax (pylab interface) IGood for interactive sessions IObject Oriented IExcellent 2-D graphics & some 3-D capability IMap toolkit (Basemap) IWorks with Numeric, Numarray & NumPy
Ivan Lima (ivan@whoi.edu) Python for Scientific Computing
Array Object Modules PyStchioenntiOvcerTvoioelws Graphics & Plotting Additional Tools & ResoScientific Packages urcesExtending Python
Matplotlib Example: Simple Plot
>>> import numpy as N >>> import pylab as pl >>> x = N.arange(0,1,0.01) * 5 * N.pi >>> pl.plot(x,N.sin(x)) >>> pl.xlabel(’x’) >>> pl.ylabel(’sin(x)’) >>> pl.title(’Plot Title’)
Ivan Lima (ivan@whoi.edu)
Python for Scientific Computing
Python OverviewArray Object Modules Scientific Tools Graphics & Plotting Additional Tools & ResourcesScientific Packages Extending Python
Matplotlib Example: 2-D Plot
>>> import numpy as N >>> import pylab as pl >>> def z_func(x,y): ... return (1-x+x**3+y**5)*N.exp(-(x**2+y**2)) ... >>> x = N.arange(-3.0,3.0,0.025) >>> y = N.arange(-3.0,3.0,0.025) >>> X,Y = pl.meshgrid(x, y) >>> Z = z_func(X, Y) >>> im = pl.imshow(Z,interpolation=’bilinear’,/ ... cmap=pl.cm.Spectral) >>> cset = pl.contour(Z,N.arange(-1.2,1.6,0.2),/ ... linewidths=2,cmap=pl.cm.hot) >>> pl.clabel(cset,inline=True,fmt=’%1.1f’,/ ... fontsize=10) >>> pl.colorbar(im) >>> pl.axis(’off’) >>> pl.title(’$z=(1-x+xˆ3+yˆ5) eˆ-(xˆ2+yˆ2)$’)
Ivan Lima (ivan@whoi.edu)
Python for Scientific Computing