< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162148044238007122032091154

Scientific Computing

Python is extensively used for scientific computing because it enables a rapid prototyping and execution of a number of functions. Scientists and engineers often have needs for high-performance computation tools that are also easy to use and modify. Many also want to be able to use a general-purpose language instead of a specialized tool, allowing them to integrate networking, GUI's, and so on in their high-performance work. Several modules have been developed to address these needs around the Python language.

In this section, I cover the Numeric Python extensions (NumPy), which provide efficient operations on large multidimensional arrays, and it has proven to be the right choice when talking about scientific computing with Python.

Besides NumPy, many other scientific tools are available. The Python community has created several extensions for manipulating data and functions, interfaces to data plotting libraries, storage solutions for scientific data, and much more. If you want to deeply discuss scientific computing with Python, you can look for the plot-sig (the Plotting Special Interest Group).

If you spend some time browsing around scientific Web pages, you will be surprised about the number of people who are really using Python for their projects.

For more information, visit the following Web sites:

Scientific computing topic at Python's Web site:

								
							http://www.python.org/topics/scicomp/
						
							

Simple Numerical Recipes in Python was written by William Park to describe few elementary numerical routines in Python:

								
							http://www.python.org/topics/scicomp/recipes_in_python.html
						
							

Python for Science—An Introduction to Python for Scientists:

								
							http://starship.python.net/crew/hinsen/
						
							

Numerical Extensions

The most powerful way to face scientific computing in Python systems is to use Python Numerical Extensions (commonly known as NumPy). The Numerical Python extensions were originally written by Jim Hugunin (JPython's author), but the responsibility to continue the project now belongs to a group of python users from the Lawrence Livermore National Laboratory. The languages that were used to guide the development of NumPy include Basis, MATLAB, FORTRAN, S and S+, and others.

The NumPy package adds a fast, compact multidimensional array language facility to Python. One-dimensional arrays are similar to standard Python sequences and two-dimensional arrays are similar to matrices from linear algebra. This package also includes tools for working with linear algebra, Fast Fourier Transforms (FFTs), random numbers, and so forth.

In addition, NumPy adds two new types to Python: A sequence type (to implement multidimensional arrays), and a new type of function called a universal function (ufunc). Numeric Python consists of a set of modules:

Numeric.py (and Its Helper Modules multiarray, umath, and fast_umath)

This module defines two new object types and a set of functions that manipulate these objects, as well as converting them and other Python types. The objects are the new array object (technically called multiarray objects), and universal functions (technically ufunc objects). The array objects are generally homogeneous collections of potentially large numbers of numbers. Universal functions (ufuncs) are functions that operate on arrays and other sequences.

The Numeric module provides, in addition to the functions needed to create the previous objects, a set of powerful functions to manipulate arrays, select subsets of arrays based on the contents of other arrays, and other array-processing operations. Note that only Numeric need be imported.

RandomArray.py (and Its Helper Module ranlib)

This module provides a high-level interface to a random-number generator (ranlib), which supplies a uniform distribution generator of pseudo-random numbers, as well as some convenience functions:

For more information, check out Additions to RandomArray Module, by Lee A. Barford:

										
									http://numpy.sourceforge.net/RandomArray-additions.html
								
									

FFT.py (and Its Helper Module fftpack)

This module provides a high-level interface to the fast Fourier transform routines implemented in the FFT-PACK library if it is available, or to the compatible but less optimized fftpack library that ships with Numeric Python.

The FFT module provides a high-level interface to the fast Fourier transform routines, which are implemented in the FFTPACK library. It performs one- and two-dimensional FFT's, forward and backwards (inverse FFTs), and includes efficient routines for FFTs of real-valued arrays. It is most efficient for arrays whose size is a power of two.

LinearAlgebra.py (and Its Helper Module lapack_litemodule)

This module provides a high-level interface to the linear algebra routines implemented in the LAPACK library if it is available, or to the compatible but less optimized lapack_lite library that ships with Numeric Python. It includes functions to solve systems of linear equations and linear least squares problems, invert matrices, compute eigenvalues and eigenvectors, generalized inverses, determinants, as well as perform singular value decomposition.

People such as scientists and engineers—who need to manipulate large arrays of numbers quickly, efficiently, and stylishly—find in these extensions a great tool, whose power is compared against other numeric languages such as MATLAB and IDL.

A good point is that everything you can do using Numerical Python is also possible to be written using core Python data structures, such as lists and tuples. The problem is that the program will run much too slow. However, if you have a couple of huge Numerical Python arrays, the speed of adding them up is close to the speed of doing it in C. Therefore, processing sophisticated numeric operations using NumPy provides similar results as running the same process using a compiled language, but without the compile time overhead or having to worry about bugs in the low-level array operations.

The following links are great sources of information about the Numeric Python extensions:

Numerical Python

										
									http://numpy.sourceforge.net
								
									

Numerical Python—Documentation

You should consider taking a look at the official documentation for NumPy. The tutorial walks you through a set of numeric manipulations.

										
									http://numpy.sourceforge.net/numpy.pdf
								
									

Numerical Python Project

The Numerical Python Project Page has releases, links to the FTP site, a bug tracking system, and a browser for the source repository plus instructions on how to use CVS anonymously.

										
									http://numpy.sourceforge.net
								
									

and

										
									http://sourceforge.net/project/?group_id=1369
								
									

Numerical Python arrays in C extension modules

										
									http://starship.python.net/crew/hinsen/NumPyExtensions.html
								
									

Writing C Extensions using Numerical Python

										
									http://oliphant.netpedia.net/packages/Numerical_Extensions.pdf.gz
								
									

Installing NumPy

Note that before building Numerical Python, you need to obtain and install the Distutils package.

Tip

The Distutils package will be distributed with Python beginning with the 1.6 release. Its purpose is to define a standard for installing Python modules. For details, check out http://www.python.org/sigs/distutils-sig/



Currently, NumPy has two distribution options available.

On Win32 platforms, such as Microsoft Windows 95, 98, and NT, a binary installer is available at

								
							ftp://ftp-icf.llnl.gov/pub/python/NumPy.exe
						
							

This installer is simple to use (simply double-click on the NumPy.exe file and answer questions on each screen in turn). Running this installer will perform all the needed modifications to your Python installation so that NumPy works.

For both UNIX and other platforms, NumPy must be compiled from the source. The source distribution for NumPy is part of the LLNLPython distribution, which is available at

							
						ftp://ftp-icf.llnl.gov/pub/python/Numeric-xx.y.tgz
					
						

There is also RPMs for Linux available from the numpy Web site at http://sourceforge.net/project/filelist.php?group_id=1369.

The file is a gzipped tarfile that should be uncompressed using the gunzip program and un-tarred with the tar program:

							
csh> gunzip Numeric-xx.y.tgz
csh> tar xf Numeric-xx.y.tar

						

Follow the instructions found in the top-level directory for compilation and installation procedures.

The standard Python installer for the Macintosh (available at http://www.python.org/download/download_mac.html) optionally installs the NumPy extensions, although these are typically not the most up-to-date files.

Other Scientific Extensions

Next, you have access to some extra Python extension modules that deal with scientific computation.

ScientificPython

ScientificPython is a collection of Python modules that are useful for scientific computing. In this collection, you will find modules that cover basic geometry (vectors, tensors, transformations, vector, and tensor fields), quaternions, automatic derivatives, (linear) interpolation, polynomials, elementary statistics, nonlinear least-squares fits, unit calculations, Fortran-compatible text formatting, 3D visualization via VRML, and two Tk widgets for simple line plots and 3D wireframe models. For more information, check out the following site:

							
						http://starship.python.net/crew/hinsen/scientific.html
					
						
Pyfort (The Python/Fortran Connection Tool)

Pyfort allows you to wrap your own Fortran routines in Python. For more information, check out

							
						http://pyfortran.sourceforge.net
					
						
RNG

RNG is a random number package from LLNL. For more information, check out

							
						ftp://numpy.sourceforge.net/pub/numpy/RNG-2.0.tgz
					
						
pyclimate

This package contains some tools used for climate variability analysis. It makes extensive use of Numerical Python. For more information, check out

							
						http://lcdx00.wm.lc.ehu.es/~jsaenz/pyclimate/index.html
					
						
GmatH

GmatH is a Gnome interface to the Numerical Python extensions. For more information, check out

							
						http://gmath.sourceforge.net/index.html
					
						
Real

real.py is a library that introduces a new class, called Real, of arbitrarily precise numbers, allowing computations with "infinite" precision. This package handles a floating point number with a large number of decimal places (more than double by far). For more information, check out

							
ftp://ftp.python.org/pub/python/contrib-09-Dec-1999/DataStructures/
real-accurate.pyar
					
						

Computer Programming for Everybody

Some great efforts are being made in bringing Python to classrooms in order to prepare young people for our new computer reality.

Bringing a computer language to the class is not a new idea. Many schools already teach some kind of programming language. However, Python is a very high-level language, a human-readable language, not just computer-readable, it has a more up-to-date design, and what you learn from Python can be adapted to other languages.

Everyone needs to know a little about computers these days, no matter what profession is chosen. It is said that some day in the near future, everyone will have to know how to code a computer program. Python is a great language that possesses all the features required for teaching computer logic to tomorrow's scientists.

For more information, see the following:

Computer Programming for Everybody, by Guido van Rossum

									
								http://www.python.org/doc/essays/cp4e.html
							
								

EDU-SIG: Python in Education

									
								http://www.python.org/sigs/edu-sig/
							
								

Check out the following four-part essay entitled Numeracy + Computer Literacy series by Kirby Urner, who uses Python to teach various math concepts in the Oregon Curriculum Network. This material will give you a clear idea of how Python can be approached for education.

						
					http://www.inetarena.com/~pdx4d/ocn/cp4e.html
				
					


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

applications
      Distutils
array objects
arrays
      one-dimensional
      two-dimensional
classes
      Real
classrooms
      bringing Python to
computing
     scientific
            use of Python in 2nd 3rd 4th 5th 6th
Distutils package
extensions
      Numerical Python (NumPy) 2nd 3rd 4th 5th
fast_umath module
FFT.py module
fftpack module
functions
      universal
GmatH module
Hugunin, Jim
installing
      NumPy 2nd
lapack_litemodule module
Lawrence Livermore National Library
LinearAlgebra.py module
modules
      fast_umath
      FFT.py
      fftpack
      GmatH
      lapack_litemodule
      LinearAlgebra.py
      multiarray
      Numeric.py
      pyclimate
      Pyfort
      real.py
      umath
multiarray module
Numeracy + Computer Literacy
Numeric.py module
Numerical Python (NumPy) extensions 2nd 3rd 4th 5th
NumPy (Numerical Python) 2nd 3rd 4th 5th
objects
      array
one-dimensional arrays
Oregon Curriculum Network
Plotting Special Interest Group
programs
      Distutils
pyclimate module
Pyfort module
Real class
real.py module
scientific computing
      use of Python in 2nd 3rd 4th 5th 6th
software
      Distutils
two-dimensional arrays
umath module
universal functions
Urner, Kirby

© 2002, O'Reilly & Associates, Inc.