Python Scripting for Computational Science (2nd ed.) [Langtangen 2005-12-21].pdf

(4274 KB) Pobierz
Texts in Computational Science
and Engineering
Editors
Timothy J. Barth
Michael Griebel
David E. Keyes
Risto M. Nieminen
Dirk Roose
Tamar Schlick
3
Hans Petter Langtangen
Python Scripting
for Computational
Science
Second Edition
With 62 Figures
123
Hans Petter Langtangen
Simula Research Laboratory
Martin Linges vei 17, Fornebu
P.O. Box 134
1325 Lysaker, Norway
email: hpl@simula.no
On leave from:
Department of Informatics
University of Oslo
P.O. Box 1080 Blindern
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.
Library of Congress Control Number: 2005936346
Mathematics Subject Classification (2000):
65Y99, 68N01, 68N15, 68N19, 68N30, 97U50, 97U70
ISSN 1611-0994
ISBN-10 3-540-29415-5 Springer Berlin Heidelberg New York
ISBN-13 978-3-540-29415-3 Springer Berlin Heidelberg New York
ISBN 3-540-43508-5 1st Edition Springer Berlin Heidelberg New York
This work is subject to copyright. All rights are reserved, whether the whole or part of the material is
concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broad-
casting, reproduction on microfilm 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 for prosecution under the German Copyright Law.
Springer is a part of Springer Science+Business Media
springeronline.com
© Springer-Verlag Berlin Heidelberg 2004, 2006
Printed in Germany
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 TEX macro package
Cover design:
design & production,
Heidelberg
Production: LE-TEX Jelonek, Schmidt & Vöckler GbR, Leipzig
Printed on acid-free paper
46/3142/YL - 5 4 3 2 1 0
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.7, 4.3.8, 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 material
on generators in Chapter 8.9.4, and an example in 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 efficiency 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.2.4, and
10.3.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 above 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
K¨hler in the Springer system for all their help with preparing a new edition.
o
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 [26,28].
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