420 likes | 634 Views
Dive into Python's high-level, open-source capabilities for scientific computing, including NumPy, SciPy, and Matplotlib. Learn about interfacing with C/C++ and Fortran and enhancing productivity with IPython.
E N D
Python for Scientific Computing Wim R.M. Cardoen Center for High Performance Computing wim.cardoen@utah.edu Fall 2010
Overview • Python • NumPy • SciPy • Matplotlib • Python using C/C++ and Fortran • IPython
Why Python? • High-level interpreted language • Very clear syntax =>easy-to-read • Open source • Platform independent • Large number of libraries • Binding to all standard GUI kits (TkInter, Qt, ..) • Powerful automated testing tools • Easily integrated with C/C++, and Fortran • Actively used and extended by scientists
Python Language • Clear syntax -> easy to understand • Indentation for blocks • Use of namespace, exception handling • Automatic doc.: doc strings & pydoc • Dynamic typing • Supports both procedural & OO approach • Easy code testing • Garbage collecting • Functional programming, iterators,…
Numpy:Why? The “trad.” Python list: • can contain data of whatever type => memory el. may have different sizes • the list can grow & shrink dynamically • passing through list by loops/iterators => not very efficient for numerical calculations. We need something more efficient: python + fast math=> NumPy
Multi-dimensional array object i.e. ndarray 1. homogeneous (all have the same type) 2. indexing/slicing 3. vectorization 4. broadcasting • Derived objects such as masked arrays, matrices 1. matrix: derived from ndarray: always 2D attributes: T(transpose), H (hermit. conj.), I (invert) 2. masked arrays: missing or invalid entries • Universal func.: operates on ndarrays in el-by-el fashion • NumPy: basic toolkit for other packages e.g. SciPy,...
What’s under the hood? NumPy and Scipy require: • Fast Linear Algebra routines: BLAS, LAPACK / ATLAS / GOTO-BLAS • FFT Routines: routines for computing the DFT • UMFPACK: routines for solving unsymmetric sparse linear systems Alternative: MKL (Intel)
SciPy • collection of mathematical algorithms • contains several subpackages, e.g.: • scipy.linalg • scipy.fftpack • scipy.optimize • scipy.integrate • scipy.interpolate • scipy.special • …
MatplotLib • produces publication quality 2D plots • supports Latex commands • >= 0.99: inclusion of mplot3D (3D pics) Note: Mayavi: 3D Scientific Data Visualization http://code.enthought.com/projects/mayavi/
Interfacing C/C++ and Python Approaches for C/C++: • SWIG: SimpliedWrapper & Interface Generator • Weave: inclusion C/C++ code in Python • PyInline: inline other languages in Python • Pyrex: python with C-data types Approaches for Fortan: f2py f2py -c --fcompiler=gfortran -m essai essai.f90 import essai essai.trial(13) # subroutine trial(x) defined in “essai.f90”
Ipython Enhanced interactive Python shell • TAB-completion • explore objects using ? • %run command • debug python script • output/input cache • history • Suppress output • call system commands • …
Additional packages • MPI: MPI4PY • SymPy: Symbolic Math • PIL: Python Imaging Library • Mayavi Interactive 3D Visualization • PyMol : 3D Molecular Viewer • NLopt: Non-linear optimization
Links: • http://www.python.org/ • http://numpy.scipy.org • http://www.scipy.org • http://matplotlib.sourceforge.net • http://ipython.scipy.org