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 11/18/2018
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 11/18/2018
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,… 11/18/2018
11/18/2018
11/18/2018
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 11/18/2018
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,... 11/18/2018
11/18/2018
NumPy and Scipy require: Fast Linear Algebra routines: 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) 11/18/2018
SciPy collection of mathematical algorithms contains several subpackages, e.g.: scipy.linalg scipy.fftpack scipy.optimize scipy.integrate scipy.interpolate scipy.special … 11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
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/ 11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
Source: http://en.wikipedia.org/wiki/Delaunay_triangulation 11/18/2018
Interfacing C/C++ and Python Approaches for C/C++: SWIG: Simplied Wrapper & 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” 11/18/2018
11/18/2018
11/18/2018
11/18/2018
11/18/2018
Ipython Enhanced interactive Python shell TAB-completion explore objects using ? %run command debug python script output/input cache history Suppress output call system commands … 11/18/2018
Additional packages MPI: MPI4PY SymPy: Symbolic Math PIL: Python Imaging Library Mayavi Interactive 3D Visualization PyMol : 3D Molecular Viewer NLopt: Non-linear optimization 11/18/2018
Links: http://www.python.org/ http://numpy.scipy.org http://www.scipy.org http://matplotlib.sourceforge.net http://ipython.scipy.org 11/18/2018