📄 154.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Scientific Computing</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="148.html" class="navtitle">9. Other Advanced Topics</a> > <span class="nonavtitle">Scientific Computing</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="153.html" title="Restricted Execution Mode"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=154" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="154.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="155.html" title="Regular Expressions"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A40%3A31+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148044238007122032091154</font><a href="read2.asp?bookname=0672319942&snode=154&now=5%2F31%2F2002+4%3A40%3A31+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Scientific Computing</h3>
<p>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.</p>
<P>In this section, I cover the Numeric Python extensions (<TT Class="monofont">NumPy</TT>), which provide efficient operations on large multidimensional arrays, and it has proven to be the right choice when talking about scientific computing with Python.<A Name="idx1073745413"></a>
<A NAMe="idx1073745414"></a>
</p>
<p>Besides <tt class="monofont">NumPy,</tt> 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 <a name="idx1073745415"></a>Plotting Special Interest Group).</p>
<P>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.</p>
<p>For more information, visit the following Web sites:</P>
<bloCkquoTe>
<p>
<p>Scientific computing topic at Python's Web site:</P>
</P>
<P>
<P>
<pre>
<a TARGet="_blank" hrEF="http://www.python.org/topics/scicomp/">http://www.python.org/topics/scicomp/</A>
</Pre>
</p>
</p>
<P>
<P>
<I>Simple Numerical Recipes in Python</I> was written by William Park to describe few elementary numerical routines in Python:</p>
</p>
<p>
<p>
<pre>
<a target="_blank" href="http://www.python.org/topics/scicomp/recipes_in_python.html">http://www.python.org/topics/scicomp/recipes_in_python.html</a>
</pRe>
</p>
</P>
<p>
<p>
<i>Python for Science桝n Introduction to Python for Scientists:</I>
</p>
</p>
<p>
<p>
<Pre>
<a TARGet="_blank" hrEF="http://starship.python.net/crew/hinsen/">http://starship.python.net/crew/hinsen/</A>
</Pre>
</p>
</p>
</BLOCkquoTE>
<H4>
Numerical Extensions</H4>
<p>The most powerful way to face scientific computing in Python systems is to use Python Numerical Extensions (commonly known as <i>NumPy</i>). The Numerical Python extensions were originally written by <a name="idx1073745416"></a>Jim Hugunin (JPython's author), but the responsibility to continue the project now belongs to a group of python users from the <a name="idx1073745417"></a>Lawrence Livermore National Laboratory. The languages that were used to guide the development of NumPy include Basis, MATLAB, FORTRAN, S and S+, and others.</p>
<p>The NumPy package adds a fast, compact multidimensional array language facility to Python. <a naMe="idx1073745418"></a>
<A namE="idx1073745419"></a>
<i>One-dimensional arrays</i> are similar to standard Python sequences and <a Name="idx1073745420"></A>
<A NAme="idx1073745421"></a>
<i>two-dimensional arrays</I> are similar to matrices from linear algebra. This package also includes tools for working with linear algebra, <I>Fast Fourier Transforms (FFTs),</I> random numbers, and so forth.</P>
<p>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 (<tt cLASS="monofont">ufunc</tt>). Numeric Python consists of a set of modules:<a nAME="idx1073745422"></A>
<a name="idx1073745423"></a>
<a name="idx1073745424"></a>
<a name="idx1073745425"></a>
<a nAme="idx1073745426"></A>
</p>
<h5>
<tT clasS="monofont">Numeric.py</tt> (and Its Helper Modules <tT CLAss="monofont">multiarray, umath,</tt> and <TT CLass="monofont">fast_umath</tT>)</H5>
<P>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 <Tt claSS="monofont">array</TT> object (technically called <tt class="monofont">multiarray</tt> objects), and universal functions (technically <tt class="monofont">ufunc</tt> objects). The <a nAme="idx1073745427"></A>
<a naMe="idx1073745428"></a>array objects are generally homogeneous collections of potentially large numbers of numbers. <a nAme="idx1073745429"></a>
<A NAMe="idx1073745430"></a>Universal functions (<tt CLASs="monofont">ufuncs</tt>) are functions that operate on arrays and other sequences.</p>
<P>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.<A NAme="idx1073745431"></a>
<a NAME="idx1073745432"></a>
<a name="idx1073745433"></a>
<a name="idx1073745434"></a>
<a name="idx1073745435"></a>
<a NamE="idx1073745436"></a>
<a nAme="idx1073745437"></a>
<a Name="idx1073745438"></A>
</P>
<H5>
<Tt claSS="monofont">RandomArray.py</TT> (and Its Helper Module <tt clASS="monofont">ranlib</Tt>)</h5>
<p>This module provides a high-level interface to a random-number generator (<tT CLAss="monofont">ranlib</tt>), which supplies a uniform distribution generator of pseudo-random numbers, as well as some convenience functions:</p>
<blockquote>
<p>
<p>For more information, check out <i>Additions to RandomArray Module,</i> by Lee A. Barford:</p>
</P>
<p>
<p>
<Pre>
<a TargeT="_blank" hreF="http://numpy.sourceforge.net/randomarray-additions.html">http://numpy.sourceforge.net/RandomArray-additions.html</A>
</PRe>
</p>
</p>
</bLOCKquotE>
<H5>
<TT clasS="monofont">
<A NAme="idx1073745439"></a>
<a name="idx1073745440"></a>
<a name="idx1073745441"></a>
<a name="idx1073745442"></A>FFT.py</tt> (and Its Helper Module <Tt clAss="monofont">fftpack</tt>)</H5>
<p>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 <tt CLASs="monofont">fftpack</tt> library that ships with Numeric Python.</p>
<P>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.</P>
<H5>
<Tt claSS="monofont">LinearAlgebra.py</TT> (and Its Helper Module <tt clASS="monofont">lapack_litemodule</Tt>)</h5>
<p>This module provides a high-level interface to the linear algebra routines implemented in the <tt class="monofont">LAPACK</tt> library if it is available, or to the compatible but less optimized <tt class="monofont">lapack_lite</tT> 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.<a nAme="idx1073745443"></a>
<A name="idx1073745444"></A>
<a naME="idx1073745445"></A>
<A name="idx1073745446"></A>
<A NAme="idx1073745447"></a>
<a NAME="idx1073745448"></a>
<a naME="idx1073745449"></A>
<A name="idx1073745450"></a>
<a name="idx1073745451"></a>
</p>
<p>People such as scientists and engineers梬ho need to manipulate large arrays of numbers quickly, efficiently, and stylishly梖ind in these extensions a great tool, whose power is compared against other numeric languages such as MATLAB and IDL.</p>
<p>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.</p>
<p>The following links are great sources of information about the Numeric Python extensions:</p>
<blOckQuotE>
<p>
<p>Numerical Python</p>
</p>
<P>
<p>
<prE>
<A TArget="_blank" HREF="http://numpy.sourceforge.net">http://numpy.sourceforge.net</a>
</pre>
</P>
</P>
<P>
<P>Numerical Python桪ocumentation</p>
</p>
<p>
<p>You should consider taking a look at the official documentation for NumPy. The tutorial walks you through a set of numeric manipulations.</P>
</P>
<P>
<P>
<pre>
<a target="_blank" href="http://numpy.sourceforge.net/numpy.pdf">http://numpy.sourceforge.net/numpy.pdf</a>
</pre>
</p>
</p>
<P>
<p>Numerical Python Project</p>
</P>
<p>
<p>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.<a Name="idx1073745452"></a>
<A namE="idx1073745453"></A>
<A Name="idx1073745454"></a>
<A NAMe="idx1073745455"></a>
<a nAME="idx1073745456"></A>
</p>
</p>
<p>
<p>
<PRE>
<A target="_blank" href="http://numpy.sourceforge.net">http://numpy.sourceforge.net</a>
</pre>
</p>
</p>
<p>
<p>and</p>
</p>
<P>
<p>
<pRe>
<a tArget="_blank" Href="http://sourceforge.net/project/?group_id=1369">http://sourceforge.net/project/?group_id=1369</A>
</PRE>
</p>
</p>
<p>
<p>Numerical Python arrays in C extension modules</P>
</P>
<P>
<P>
<pre>
<a TARGet="_blank" hrEF="http://starship.python.net/crew/hinsen/NumPyExtensions.html">http://starship.python.net/crew/hinsen/NumPyExtensions.html</A>
</Pre>
</p>
</p>
<p>
<p>Writing C Extensions using Numerical Python</p>
</p>
<p>
<p>
<pre>
<a target="_blank" HreF="http://oliphant.netpedia.net/packages/numerical_extensions.pdf.gz">http://oliphant.netpedia.net/packages/Numerical_Extensions.pdf.gz</a>
</prE>
</p>
</p>
</blOckqUOTE>
<h5>
Installing <tt cLASS="monofont">NumPy</tt>
</h5>
<p>Note that before building Numerical Python, you need to obtain and install the <A NAMe="idx1073745457"></a>
<a nAME="idx1073745458"></A>
<a name="idx1073745459"></a>
<a name="idx1073745460"></a>
<tt class="monofont">Distutils</tT> package.</p>
<dIv clAss="note"><p cLass="notetitle"><B>Tip</B></P><P>
<p>The <tt cLASS="monofont">Distutils</tt> 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 <a tARGEt="_blank" hreF="http://www.python.org/sigs/distutils-sig/">http://www.python.org/sigs/distutils-sig/</A>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -