Python_Scripting_for_Computational_Science_Third_Edition.pdf

(5164 KB) Pobierz
281053219 UNPDF
Hans Petter Langtangen
Python Scripting
for Computational
Science
Third Edition
With 62 Figures
123
Hans Petter Langtangen
Simula Research Laboratory
Martin Linges vei 17, Fornebu
P. O. B o x 1 3 4
1325 Lysaker, Norway
hpl@simula.no
On leave from:
Department of Informatics
University of Oslo
P. O. B o x 1 0 8 0 B l i n d e r n
0316 Oslo, Norway
http://folk.uio.no/hpl
The author of this book has received financial support from the NFF – Norsk faglitterær
forfatter- og oversetterforening.
ISBN 978-3-540-73915-9
e-ISBN 978-3-540-73916-6
DOI 10.1007/978-3-540-73916-6
Texts in Computational Science and Engineering ISSN 1611-0994
Library of Congress Control Number: 2007940499
Mathematics Subject Classification (2000): 65Y99, 68N01, 68N15, 68N19, 68N30, 97U50, 97U70
© 2008, 2006, 2004 Springer-Verlag Berlin Heidelberg
This work is subject to copyright. All rights are reserved, whether the whole or part of the material is
concerned, specif ically the rights of translation, reprinting, reuse of illustrations, recitation, broad-
casting, reproduction on microf ilm or in any other way, and storage in data banks. Duplication of
this publication or parts thereof is permitted only under the provisions of the German Copyright Law
of September 9, 1965, in its current version, and permission for use must always be obtained from
Springer. Violations are liable to prosecution under the German Copyright Law.
The use of general descriptive names, registered names, trademarks, etc. in this publication does
not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
Typesetting: by the author using a Springer T E X macro package
Cover design: WMX Design GmbH, Heidelberg
Production: LE-T E X Jelonek, Schmidt & Vöckler GbR, Leipzig
Printed on acid-free paper
987654321
springer.com
281053219.001.png
Preface to the Third Edition
Numerous readers of the second edition have notified me about misprints and
possible improvements of the text and the associated computer codes. The
resulting modifications have been incorporated in this new edition and its
accompanying software.
The major change between the second and third editions, however, is
caused by the new implementation of Numerical Python, now called numpy .
The new numpy package encourages a slightly different syntax compared to
the old Numeric implementation, which was used in the previous editions.
Since Numerical Python functionality appears in a lot of places in the book,
there are hence a huge number of updates to the new suggested numpy syntax,
especially in Chapters 4, 9, and 10.
The second edition was based on Python version 2.3, while the third
edition contains updates for version 2.5. Recent Python features, such as
generator expressions (Chapter 8.9.4), Ctypes for interfacing shared libraries
in C (Chapter 5.2.2), the with statement (Chapter 3.1.4), and the subprocess
module for running external processes (Chapter 3.1.3) have been exemplified
to make the reader aware of new tools. Regarding Chapter 3.1.3, os.system
is not used in the book anymore, instead we recommend the commands or
subprocess modules.
Chapter 4.4.4 is new and gives a taste of symbolic mathematics in Python.
Chapters 5 and 10 have been extended with new material. For example,
F2PY and the Instant tool are very convenient for interfacing C code, and
this topic is treated in detail in Chapters 5.2.2, 10.1.1, and 10.1.2 in the
new edition. Installation of Python itself and the many add-on modules have
become increasingly simpler over the years with setup.py scripts, which has
made it natural to simplify the descriptions in Appendix A.
The py4cs package with software tools associated with this book has un-
dergone a major revision and extension, and the package is now maintained
under the name scitools and distributed separately. The name py4cs is still
offered as a nickname for scitools to make old scripts work. The new scitools
package is backward compatible with py4cs from the second edition.
Several people has helped me with preparing the new edition. In par-
ticular, the substantial efforts of Pearu Peterson, Ilmar Wilbers, Johannes
H. Ring, and Rolv E. Bredesen are highly appreciated.
The Springer staff has, as always, been a great pleasure to work with.
Special thanks go to Martin Peters, Thanh-Ha Le Thi, and Andrea Kohler
for their extensive help with this and other book projects.
Oslo, September 2007
Hans Petter Langtangen
Preface to the Second Edition
The second edition features new material, reorganization of text, improved
examples and software tools, updated information, and correction of errors.
This is mainly the result of numerous eager readers around the world who
have detected misprints, tested program examples, and suggested alternative
ways of doing things. I am greatful to everyone who has sent emails and
contributed with improvements. The most important changes in the second
edition are briefly listed below.
Already in the introductory examples in Chapter 2 the reader now gets a
glimpse of Numerical Python arrays, interactive computing with the IPython
shell, debugging scripts with the aid of IPython and Pdb, and turning “flat”
scripts into reusable modules (Chapters 2.2.5, 2.2.6, and 2.5.3 are added).
Several parts of Chapter 4 on numerical computing have been extended (es-
pecially Chapters 4.3.5, 4.3.6, 4.3.7, and 4.4). Many smaller changes have
been implemented in Chapter 8; the larger ones concern exemplifying Tar
archives instead of ZIP archives in Chapter 8.3.4, rewriting of the mate-
rial on generators in Chapter 8.9.4, and an example in Chapter 8.6.13 on
adding new methods to a class without touching the original source code
and without changing the class name. Revised and additional tips on opti-
mizing Python code have been included in Chapter 8.10.3, while the new
Chapter 8.10.4 contains a case study on the eciency of various implemen-
tations of a matrix-vector product. To optimize Python code, we now also
introduce the Psyco and Weave tools (see Chapters 8.10.4, 9.1, 10.1.3, and
10.4.1). To reduce complexity of the principal software example in Chapters 9
and 10, I have removed evaluation of string formulas. Instead, one can use
the revised StringFunction tool from Chapter 12.2.1 (the text and software
regarding this tool have been completely rewritten). Appendix B.5 has been
totally rewritten: now I introduce Subversion instead of CVS, which results
in simpler recipes and shorter text. Many new Python tools have emerged
since the first printing and comments about some of these are inserted many
places in the text.
Numerous sections or paragraphs have been expanded, condensed, or re-
moved. The sequence of chapters is hardly changed, but a couple of sections
have been moved. The numbering of the exercises is altered as a result of
both adding and removing exerises.
Finally, I want to thank Martin Peters, Thanh-Ha Le Thi, and Andrea
Kohler in the Springer system for all their help with preparing a new edition.
Oslo, October 2005
Hans Petter Langtangen
Preface to the First Edition
The primary purpose of this book is to help scientists and engineers work-
ing intensively with computers to become more productive, have more fun,
and increase the reliability of their investigations. Scripting in the Python
programming language can be a key tool for reaching these goals [27,29].
The term scripting means different things to different people. By scripting
I mean developing programs of an administering nature, mostly to organize
your work, using languages where the abstraction level is higher and program-
ming is more convenient than in Fortran, C, C++, or Java. Perl, Python,
Ruby, Scheme, and Tcl are examples of languages supporting such high-level
programming or scripting. To some extent Matlab and similar scientific com-
puting environments also fall into this category, but these environments are
mainly used for computing and visualization with built-in tools, while script-
ing aims at gluing a range of different tools for computing, visualization, data
analysis, file/directory management, user interfaces, and Internet communi-
cation. So, although Matlab is perhaps the scripting language of choice in
computational science today, my use of the term scripting goes beyond typi-
cal Matlab scripts. Python stands out as the language of choice for scripting
in computational science because of its very clean syntax, rich modulariza-
tion features, good support for numerical computing, and rapidly growing
popularity.
What Scripting is About. The simplest application of scripting is to write
short programs (scripts) that automate manual interaction with the com-
puter. That is, scripts often glue stand-alone applications and operating sys-
tem commands. A primary example is automating simulation and visual-
ization: from an effective user interface the script extracts information and
generates input files for a simulation program, runs the program, archive data
files, prepares input for a visualization program, creates plots and animations,
and perhaps performs some data analysis.
More advanced use of scripting includes rapid construction of graphical
user interfaces (GUIs), searching and manipulating text (data) files, manag-
ing files and directories, tailoring visualization and image processing environ-
ments to your own needs, administering large sets of computer experiments,
and managing your existing Fortran, C, or C++ libraries and applications
directly from scripts.
Scripts are often considerably faster to develop than the corresponding
programs in a traditional language like Fortran, C, C++, or Java, and the
code is normally much shorter. In fact, the high-level programming style and
tools used in scripts open up new possibilities you would hardly consider as
a Fortran or C programmer. Furthermore, scripts are for the most part truly
cross-platform, so what you write on Windows runs without modifications
Zgłoś jeśli naruszono regulamin