Tải bản đầy đủ (.pdf) (726 trang)

A primer on scientific programming with python

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.78 MB, 726 trang )


Editorial Policy
§1. Textbooks on topics in the field of computational science and engineering will be considered. They should be
written for courses in CSE education. Both graduate and undergraduate textbooks will be published in TCSE. Multidisciplinary topics and multidisciplinary teams of authors are especially welcome.
§2. Format: Only works in English will be considered. They should be submitted in camera-ready form according to
Springer-Verlag’s specifications.
Electronic material can be included if appropriate. Please contact the publisher.
Technical instructions and/or TEXmacros are available via
/>§3. Those considering a book which might be suitable for the series are strongly advised to contact the publisher or
the series editors at an early stage.

General Remarks
TCSE books are printed by photo-offset from the master-copy delivered in camera-ready form by the authors. For this
purpose Springer-Verlag provides technical instructions for the preparation of manuscripts. See also Editorial Policy.
Careful preparation of manuscripts will help keep production time short and ensure a satisfactory appearance of the
finished book.
The following terms and conditions hold:
Regarding free copies and royalties, the standard terms for Springer mathematics monographs and textbooks hold.
Please write to for details.
Authors are entitled to purchase further copies of their book and other Springer books for their personal use, at a
discount of 33,3% directly from Springer-Verlag.


Series Editors
Timothy J. Barth
NASA Ames Research Center
NAS Division
Moffertt Field, CA 94035, USA
e-mail:
Michael Griebel
Institut für Numerische Simulation


der Universität Bonn
Wagelerstr. 6
53115 Bonn, Germany
e-mail:
David E. Keyes
Department of Applied Physics
and Applied Mathematics
Columbia University
200 S. W. Mudd Building
500 W. 120th Street
New York, NY 10027, USA
e-mail:
Risto M. Nieminen
Laboratory of Physics
Helsinki University of Technology
02150 Espoo, Finland
e-mail:

Dirk Roose
Department of Computer Science
Katholieke Universiteit Leuven
Celestijnenlaan 200A
3001 Leuven-Heverlee, Belgium
e-mail:
Tamar Schlick
Department of Chemistry
Courant Institute of Mathematical
Sciences
New York University
and Howard Hughes Medical Insitute

251 Mercer Street
New York, NY 10012, USA
e-mail:
Editor at Springer: Martin Peters
Springer-Verlag, Mathematics Editorial IV
Tiergartenstrasse 17
D-69121 Heidelberg, Germany
Tel.: *49 (6221) 487-8185
Fax: *49 (6221) 487-8355
e-mail:


Texts
in Computational Science
and Engineering
1.

H.P. Langtangen, Computational Partial Differential Equations. Numerical Methods and Diffpack Programming. 2nd Edition

2.

A. Quarteroni, F. Saleri, Scientific Computing with MATLAB and Octave. 2nd Edition

3.

H.P. Langtangen, Python Scripting for Computational Science. 3rd Edition

4.

H. Gardner, G. Manduchi, Design Patterns for e-Science.


5.

M. Griebel, S. Knapek, G. Zumbusch, Numerical Simulation in Molecular Dynamics.

6.

H.P. Langtangen, A Primer on Scientific Programming with Python.

For further information on these books please have a look at our mathematics catalogue at the following URL:
www.springer.com/series/5151

Monographs
in Computational Science
and Engineering
1.

J. Sundnes, G.T. Lines, X. Cai, B.F. Nielsen, K.-A. Mardal, A. Tveito, Computing the Electrical Activity in
the Heart.

For further information on these books please have a look at our mathematics catalogue at the following URL:
www.springer.com/series/7417

Lecture Notes
in Computational Science
and Engineering
1.

D. Funaro, Spectral Elements for Transport-Dominated Equations.


2.

H.P. Langtangen, Computational Partial Differential Equations. Numerical Methods and Diffpack Programming.

3.

W. Hackbusch, G. Wittum (eds.), Multigrid Methods V.

4.

P. Deuflhard, J. Hermans, B. Leimkuhler, A.E. Mark, S. Reich, R.D. Skeel (eds.), Computational Molecular
Dynamics: Challenges, Methods, Ideas.

5.

D. Kröner, M. Ohlberger, C. Rohde (eds.), An Introduction to Recent Developments in Theory and Numerics
for Conservation Laws.


6.

S. Turek, Efficient Solvers for Incompressible Flow Problems. An Algorithmic and Computational Approach.

7.

R. von Schwerin, Multi Body System SIMulation. Numerical Methods, Algorithms, and Software.

8.

H.-J. Bungartz, F. Durst, C. Zenger (eds.), High Performance Scientific and Engineering Computing.


9.

T.J. Barth, H. Deconinck (eds.), High-Order Methods for Computational Physics.

10. H.P. Langtangen, A.M. Bruaset, E. Quak (eds.), Advances in Software Tools for Scientific Computing.
11. B. Cockburn, G.E. Karniadakis, C.-W. Shu (eds.), Discontinuous Galerkin Methods. Theory, Computation
and Applications.
12. U. van Rienen, Numerical Methods in Computational Electrodynamics. Linear Systems in Practical Applications.
13. B. Engquist, L. Johnsson, M. Hammill, F. Short (eds.), Simulation and Visualization on the Grid.
14. E. Dick, K. Riemslagh, J. Vierendeels (eds.), Multigrid Methods VI.
15. A. Frommer, T. Lippert, B. Medeke, K. Schilling (eds.), Numerical Challenges in Lattice Quantum Chromodynamics.
16. J. Lang, Adaptive Multilevel Solution of Nonlinear Parabolic PDE Systems. Theory, Algorithm, and Applications.
17. B.I. Wohlmuth, Discretization Methods and Iterative Solvers Based on Domain Decomposition.
18. U. van Rienen, M. Günther, D. Hecht (eds.), Scientific Computing in Electrical Engineering.
19. I. Babuška, P.G. Ciarlet, T. Miyoshi (eds.), Mathematical Modeling and Numerical Simulation in Continuum Mechanics.
20. T.J. Barth, T. Chan, R. Haimes (eds.), Multiscale and Multiresolution Methods. Theory and Applications.
21. M. Breuer, F. Durst, C. Zenger (eds.), High Performance Scientific and Engineering Computing.
22. K. Urban, Wavelets in Numerical Simulation. Problem Adapted Construction and Applications.
23. L.F. Pavarino, A. Toselli (eds.), Recent Developments in Domain Decomposition Methods.
24. T. Schlick, H.H. Gan (eds.), Computational Methods for Macromolecules: Challenges and Applications.
25. T.J. Barth, H. Deconinck (eds.), Error Estimation and Adaptive Discretization Methods in Computational
Fluid Dynamics.
26. M. Griebel, M.A. Schweitzer (eds.), Meshfree Methods for Partial Differential Equations.
27. S. Müller, Adaptive Multiscale Schemes for Conservation Laws.
28. C. Carstensen, S. Funken, W. Hackbusch, R.H.W. Hoppe, P. Monk (eds.), Computational Electromagnetics.
29. M.A. Schweitzer, A Parallel Multilevel Partition of Unity Method for Elliptic Partial Differential Equations.
30. T. Biegler, O. Ghattas, M. Heinkenschloss, B. van Bloemen Waanders (eds.), Large-Scale PDE-Constrained
Optimization.
31. M. Ainsworth, P. Davies, D. Duncan, P. Martin, B. Rynne (eds.), Topics in Computational Wave Propagation. Direct and Inverse Problems.

32. H. Emmerich, B. Nestler, M. Schreckenberg (eds.), Interface and Transport Dynamics. Computational
Modelling.
33. H.P. Langtangen, A. Tveito (eds.), Advanced Topics in Computational Partial Differential Equations. Numerical Methods and Diffpack Programming.


34. V. John, Large Eddy Simulation of Turbulent Incompressible Flows. Analytical and Numerical Results for
a Class of LES Models.
35. E. Bänsch (ed.), Challenges in Scientific Computing – CISC 2002.
36. B.N. Khoromskij, G. Wittum, Numerical Solution of Elliptic Differential Equations by Reduction to the
Interface.
37. A. Iske, Multiresolution Methods in Scattered Data Modelling.
38. S.-I. Niculescu, K. Gu (eds.), Advances in Time-Delay Systems.
39. S. Attinger, P. Koumoutsakos (eds.), Multiscale Modelling and Simulation.
40. R. Kornhuber, R. Hoppe, J. Périaux, O. Pironneau, O. Wildlund, J. Xu (eds.), Domain Decomposition
Methods in Science and Engineering.
41. T. Plewa, T. Linde, V.G. Weirs (eds.), Adaptive Mesh Refinement – Theory and Applications.
42. A. Schmidt, K.G. Siebert, Design of Adaptive Finite Element Software. The Finite Element Toolbox ALBERTA.
43. M. Griebel, M.A. Schweitzer (eds.), Meshfree Methods for Partial Differential Equations II.
44. B. Engquist, P. Lötstedt, O. Runborg (eds.), Multiscale Methods in Science and Engineering.
45. P. Benner, V. Mehrmann, D.C. Sorensen (eds.), Dimension Reduction of Large-Scale Systems.
46. D. Kressner, Numerical Methods for General and Structured Eigenvalue Problems.
47. A. Boriçi, A. Frommer, B. Joó, A. Kennedy, B. Pendleton (eds.), QCD and Numerical Analysis III.
48. F. Graziani (ed.), Computational Methods in Transport.
49. B. Leimkuhler, C. Chipot, R. Elber, A. Laaksonen, A. Mark, T. Schlick, C. Schütte, R. Skeel (eds.), New
Algorithms for Macromolecular Simulation.
50. M. Bücker, G. Corliss, P. Hovland, U. Naumann, B. Norris (eds.), Automatic Differentiation: Applications,
Theory, and Implementations.
51. A.M. Bruaset, A. Tveito (eds.), Numerical Solution of Partial Differential Equations on Parallel Computers.
52. K.H. Hoffmann, A. Meyer (eds.), Parallel Algorithms and Cluster Computing.
53. H.-J. Bungartz, M. Schäfer (eds.), Fluid–Structure Interaction.

54. J. Behrens, Adaptive Atmospheric Modeling.
55. O. Widlund, D. Keyes (eds.), Domain Decomposition Methods in Science and Engineering XVI.
56. S. Kassinos, C. Langer, G. Iaccarino, P. Moin (eds.), Complex Effects in Large Eddy Simulations.
57. M. Griebel, M. A Schweitzer (eds.), Meshfree Methods for Partial Differential Equations III.
58. A.N. Gorban, B. Kégl, D.C. Wunsch, A. Zinovyev (eds.), Principal Manifolds for Data Visualization and
Dimension Reduction.
59. H. Ammari (ed.), Modeling and Computations in Electromagnetics: A Volume Dedicated to Jean-Claude
Nédélec.
60. U. Langer, M. Discacciati, D. Keyes, O. Widlund, W. Zulehner (eds.), Domain Decomposition Methods in
Science and Engineering XVII.
61. T. Mathew, Domain Decomposition Methods for the Numerical Solution of Partial Differential Equations.
62. F. Graziani (ed.), Computational Methods in Transport: Verification and Validation.


63. M. Bebendorf, Hierarchical Matrices. A Means to Efficiently Solve Elliptic Boundary Value Problems.
64. C.H. Bischof, H.M. Bücker, P. Hovland, U. Naumann, J. Utke (eds.), Advances in Automatic Differentiation.
65. M. Griebel, M.A. Schweitzer (eds.), Meshfree Methods for Partial Differential Equations IV.
66. B. Engquist, P. Lötstedt, O. Runborg (eds.), Multiscale Modeling and Simulation in Science.
67. I.H. Tuncer, Ü. Gülcat, D.R. Emerson, K. Matsuno (eds.), Parallel Computational Fluid Dynamics.
68. S. Yip, T. Diaz de la Rubia (eds.), Scientific Modeling and Simulations.
69. A. Hegarty, N. Kopteva, E. O’Riordan, M. Stynes (eds.), BAIL 2008 – Boundary and Interior Layers.
70. M. Bercovier, M.J. Gander, R. Kornhuber, O. Widlund (eds.), Domain Decomposition Methods in Science
and Engineering XVIII.
For further information on these books please have a look at our mathematics catalogue at the following URL:
www.springer.com/series/3527


Texts in Computational Science
and Engineering
Editors

Timothy J. Barth
Michael Griebel
David E. Keyes
Risto M. Nieminen
Dirk Roose
Tamar Schlick

6


Hans Petter Langtangen

A Primer on Scientific
Programming with Python

123


Hans Petter Langtangen
Simula Research Laboratory
Martin Linges vei 17
1325 Lysaker, Fornebu
Norway


On leave from:
Department of Informatics
University of Oslo
P.O. Box 1080 Blindern
0316 Oslo, Norway

/>
ISSN 1611-0994
ISBN 978-3-642-02474-0
e-ISBN 978-3-642-02475-7
DOI 10.1007/978-3-642-02475-7
Springer Dordrecht Heidelberg London New York
Library of Congress Control Number: 2009931367
Mathematics Subject Classification (2000): 26-01, 34A05, 34A30, 34A34, 39-01, 40-01, 65D15, 65D25,
65D30, 68-01, 68N01, 68N19, 68N30, 70-01, 92D25, 97-04, 97U50
© Springer-Verlag Berlin Heidelberg 2009
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, broadcasting,
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 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.
Production: le-tex publishing services GmbH, Leipzig, Germany
Cover design: deblik, Berlin
Printed on acid-free paper
Springer is part of Springer Science+Business Media (www.springer.com)


Preface

The aim of this book is to teach computer programming using examples
from mathematics and the natural sciences. We have chosen to use the
Python programming language because it combines remarkable power

with very clean, simple, and compact syntax. Python is easy to learn
and very well suited for an introduction to computer programming.
Python is also quite similar to Matlab and a good language for doing
mathematical computing. It is easy to combine Python with compiled
languages, like Fortran, C, and C++, which are widely used languages
for scientific computations. A seamless integration of Python with Java
is offered by a special version of Python called Jython.
The examples in this book integrate programming with applications to mathematics, physics, biology, and finance. The reader is expected to have knowledge of basic one-variable calculus as taught in
mathematics-intensive programs in high schools. It is certainly an advantage to take a university calculus course in parallel, preferably containing both classical and numerical aspects of calculus. Although not
strictly required, a background in high school physics makes many of
the examples more meaningful.
Many introductory programming books are quite compact and focus
on listing functionality of a programming language. However, learning
to program is learning how to think as a programmer. This book has
its main focus on the thinking process, or equivalently: programming
as a problem solving technique. That is why most of the pages are
devoted to case studies in programming, where we define a problem and
explain how to create the corresponding program. New constructions
and programming styles (what we could call theory) is also usually
introduced via examples. Special attention is paid to verification of
programs and to finding errors. These topics are very demanding for
mathematical software, because we have approximation errors possibly
mixed with programming errors.
v


vi

By studying the many examples in the book, I hope readers will learn
how to think right and thereby write programs in a quicker and more

reliable way. Remember, nobody can learn programming by just reading – one has to solve a large amount of exercises hands on. Therefore,
the book is full of exercises of various types: modifications of existing
examples, completely new problems, or debugging of given programs.
To work with this book, you need to install Python version 2.6. In
Chapter 4 and later chapters, you also need the NumPy and SciTools
packages. To make curve plots with SciTools, you must have a plotting
program, for example, Gnuplot or Matplotlib. There is a web page
associated with this book, />which lists the software you need and explains briefly how to install
it. On this page, you will also find all the files associated with the
program examples in this book. Download book-examples.tar.gz, store
this file in some folder of your choice, and unpack it using WinZip
on Windows or the command tar xzf book-examples.tar.gz on Linux
and Mac. This unpacking yields a folder src with subfolders for the
various chapters in the book.
Contents. Chapter 1 introduces variables, objects, modules, and text
formatting through examples concerning evaluation of mathematical
formulas. Chapter 2 presents fundamental elements of programming:
loops, lists, and functions. This is a comprehensive and important chapter that should be digested before proceeding. How to read data into
programs and deal with errors in input are the subjects of Chapter 3.
Many of the examples in the first three chapters are strongly related.
Typically, formulas from the first chapter are encapsulated in functions
in the second chapter, and in the third chapter the input to the functions are fetched from the command line or from a question-answer
dialog with the user, and validity of the data is checked. Chapter 4
introduces arrays and array computing (including vectorization) and
how this is used for plotting y = f (x) curves. After the first four chapters, the reader should have enough knowledge of programming to solve
mathematical problems by “Matlab-style” programming.
Chapter 5 introduces mathematical modeling, using sequences and
difference equations. We also treat sound as a sequence. No new programming concepts are introduced in this chapter, the aim being to
consolidate the programming knowledge and apply it to mathematical
problems.

Chapter 6 explains how to work with files and text data. Class
programming, including user-defined types for mathematical computations (with overloaded operators), is introduced in Chapter 7. Chapter 8 deals with random numbers and statistical computing with applications to games and random walk. Object-oriented programming
(class hierarchies and inheritance) is the subject of Chapter 9. The

Preface


Preface

vii

key examples here deal with building toolkits for graphics and for numerical differentiation, integration, and solution of ordinary differential
equations.
Appendix A deals with functions on a mesh, numerical differentiation, and numerical integration. The next appendix gives an introduction to numerical solution of ordinary differential equations. These two
appendices provide the theoretical background on numerical methods
that are much in use in Chapters 7 and 9. Moreover, the appendices
exemplify basic programming from the first four chapters.
Appendix C shows how a complete project in physics can be solved
by mathematical modeling, numerical methods, and programming elements from the first four chapters. This project is a good example
on problem solving in computational science, where it is necessary to
integrate physics, mathematics, numerics, and computer science.
Appendix D is devoted to the art of debugging, and in fact problem
solving in general, while Appendix E deals with various more advanced
technical topics.
Most of the examples and exercises in this book are quite compact
and limited. However, many of the exercises are related, and together
they form larger projects in science, for example on Fourier Series (1.13,
2.39, 3.17, 3.18, 3.19, 4.20), Taylor series (2.38, 4.16, 4.18, 5.16, 5.17,
7.31), falling objects (7.25, 9.30, 7.26, 9.31, 9.32, 9.34), oscillatory population growth (5.21, 5.22, 6.28, 7.41, 7.42), visualization of web data
(6.22, 6.23, 6.24, 6.25), graphics and animation (9.36, 9.37, 9.38, 9.39),

optimization and finance (5.23, 8.42, 8.43), statistics and probability
(3.25, 3.26, 3.27, 8.19, 8.20, 8.21), random walk and statistical physics
(8.33, 8.34, 8.35, 8.36, 8.37, 8.38, 8.39, 8.40), noisy data analysis (8.44,
8.45, 8.46, 8.47, 9.40), numerical methods (5.12, 7.9, 7.22, 9.15, 9.16,
9.26, 9.27, 9.28), building a calculus calculator (9.41, 9.42, 9.43, 9.44),
and creating a toolkit for simulating oscillatory systems (9.45–9.52).
Chapters 1–9 and Appendix C form the core of an introductory firstsemester course on scientific programming (INF1100) at the University
of Oslo. Normally, each chapter is suited for a 2 × 45 min lecture, but
Chapters 2 and 7 are challenging, and each of them have consumed
two lectures in the course.
Acknowledgments. First, I want to express my thanks to Aslak Tveito
for his enthusiastic role in the initiation of this book project and for
writing Appendices A and B about numerical methods. Without Aslak
there would be no book. Another key contributor is Ilmar Wilbers. His
extensive efforts with assisting the book project and teaching the associated course (INF1100) at the University of Oslo are greatly appreciated. Without Ilmar and his solutions to numerous technical problems
the book would never have been completed. Johannes H. Ring also
deserves a special acknowledgment for the development of the Easyviz


viii

Preface

graphics tool, which is much used throughout this book, and for his
careful maintenance and support of software associated with this book.
Several people have helped to make substantial improvements of
the text, the exercises, and the associated software infrastructure. The
author is thankful to Ingrid Eide, Tobias Vidarssønn Langhoff, Mathias
Nedrebø, Arve Knudsen, Marit Sandstad, Lars Storjord, Fredrik Heffer
Valdmanis, and Torkil Vederhus for their contributions. Hakon Adler

is greatly acknowledged for his careful reading of various versions of
the manuscript. The professors Fred Espen Bent, Ørnulf Borgan, Geir
Dahl, Knut Mørken, and Geir Pedersen have contributed with many
exciting exercises from various application fields. Great thanks also go
to Jan Olav Langseth for creating the cover image.
This book and the associated course are parts of a comprehensive
reform at the University of Oslo, called Computers in Science Education. The goal of the reform is to integrate computer programming
and simulation in all bachelor courses in natural science where mathematical models are used. The present book lays the foundation for the
modern computerized problem solving technique to be applied in later
courses. It has been extremely inspiring to work with the driving forces
behind this reform, in particular the professors Morten Hjorth–Jensen,
Anders Malthe–Sørenssen, Knut Mørken, and Arnt Inge Vistnes.
The excellent assistance from the Springer and Le-TeX teams, consisting of Martin Peters, Thanh-Ha Le Thi, Ruth Allewelt, Peggy
Glauch-Ruge, Nadja Kroke, and Thomas Schmidt, is highly appreciated and ensured a smooth and rapid production of this book.

Oslo, May 2009

Hans Petter Langtangen


Contents

1

Computing with Formulas . . . . . . . . . . . . . . . . . . . . . . . . .
1.1 The First Programming Encounter: A Formula . . . . . . .
1.1.1 Using a Program as a Calculator . . . . . . . . . . . . .
1.1.2 About Programs and Programming . . . . . . . . . . .
1.1.3 Tools for Writing Programs . . . . . . . . . . . . . . . . . .
1.1.4 Using Idle to Write the Program . . . . . . . . . . . . . .

1.1.5 How to Run the Program . . . . . . . . . . . . . . . . . . . .
1.1.6 Verifying the Result . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.7 Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.8 Names of Variables . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.9 Reserved Words in Python . . . . . . . . . . . . . . . . . . .
1.1.10 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.1.11 Formatting Text and Numbers . . . . . . . . . . . . . . .
1.2 Computer Science Glossary . . . . . . . . . . . . . . . . . . . . . . . . .
1.3 Another Formula: Celsius-Fahrenheit Conversion . . . . . .
1.3.1 Potential Error: Integer Division . . . . . . . . . . . . . .
1.3.2 Objects in Python . . . . . . . . . . . . . . . . . . . . . . . . . .
1.3.3 Avoiding Integer Division . . . . . . . . . . . . . . . . . . . .
1.3.4 Arithmetic Operators and Precedence . . . . . . . . .
1.4 Evaluating Standard Mathematical Functions . . . . . . . . .
1.4.1 Example: Using the Square Root Function . . . . .
1.4.2 Example: Using More Mathematical Functions .
1.4.3 A First Glimpse of Round-Off Errors . . . . . . . . . .
1.5 Interactive Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5.1 Calculating with Formulas in the Interactive
Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5.2 Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.5.3 IPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.6 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1
1
2
2
3
4

7
8
8
9
10
10
11
13
18
19
20
21
21
22
22
25
25
26
27
28
29
31
ix


x

Contents

1.6.1

1.6.2
1.6.3

Complex Arithmetics in Python . . . . . . . . . . . . . .
Complex Functions in Python . . . . . . . . . . . . . . . .
Unified Treatment of Complex and Real
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.7.2 Summarizing Example: Trajectory of a Ball . . . .
1.7.3 About Typesetting Conventions in This Book . .
1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2

Basic Constructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1 Loops
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.1.6

2.2

2.3
2.4

2.5


3

and Lists for Tabular Data . . . . . . . . . . . . . . . . . . .
A Naive Solution . . . . . . . . . . . . . . . . . . . . . . . . . . .
While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . .
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Alternative Implementations with Lists and
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1.7 Nested Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1.8 Printing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1.9 Extracting Sublists . . . . . . . . . . . . . . . . . . . . . . . . . .
2.1.10 Traversing Nested Lists . . . . . . . . . . . . . . . . . . . . . .
2.1.11 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.1 Functions of One Variable . . . . . . . . . . . . . . . . . . .
2.2.2 Local and Global Variables . . . . . . . . . . . . . . . . . . .
2.2.3 Multiple Arguments . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.4 Multiple Return Values . . . . . . . . . . . . . . . . . . . . . .
2.2.5 Functions with No Return Values . . . . . . . . . . . . .
2.2.6 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . .
2.2.7 Doc Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.8 Function Input and Output . . . . . . . . . . . . . . . . . .
2.2.9 Functions as Arguments to Functions . . . . . . . . .
2.2.10 The Main Program . . . . . . . . . . . . . . . . . . . . . . . . .
2.2.11 Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . .
If Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.4.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4.2 Summarizing Example: Tabulate a Function . . . .
2.4.3 How to Find More Python Information . . . . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32
32
33
35
35
38
39
40
51
51
51
52
54
56
58
60
64
65
66
68
70
71
71
73
75

77
79
80
83
84
84
86
87
88
91
91
94
98
99

Input Data and Error Handling . . . . . . . . . . . . . . . . . . . 119
3.1 Asking Questions and Reading Answers . . . . . . . . . . . . . . 120


Contents

xi

3.1.1 Reading Keyboard Input . . . . . . . . . . . . . . . . . . . .
3.1.2 The Magic “eval” Function . . . . . . . . . . . . . . . . . . .
3.1.3 The Magic “exec” Function . . . . . . . . . . . . . . . . . . .
3.1.4 Turning String Expressions into Functions . . . . .
Reading from the Command Line . . . . . . . . . . . . . . . . . . .
3.2.1 Providing Input on the Command Line . . . . . . . .
3.2.2 A Variable Number of Command-Line

Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2.3 More on Command-Line Arguments . . . . . . . . . . .
3.2.4 Option–Value Pairs on the Command Line . . . . .
Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.1 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . .
3.3.2 Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . .
A Glimpse of Graphical User Interfaces . . . . . . . . . . . . . .
Making Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.5.1 Example: Compund Interest Formulas . . . . . . . . .
3.5.2 Collecting Functions in a Module File . . . . . . . . .
3.5.3 Using Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.6.2 Summarizing Example: Bisection Root Finding .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

128
129
130
132
133
136
139
141
142
143
148
150
150
152

160

Array Computing and Curve Plotting . . . . . . . . . . . .
4.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.1.1 The Vector Concept . . . . . . . . . . . . . . . . . . . . . . . . .
4.1.2 Mathematical Operations on Vectors . . . . . . . . . .
4.1.3 Vector Arithmetics and Vector Functions . . . . . .
4.2 Arrays in Python Programs . . . . . . . . . . . . . . . . . . . . . . . .
4.2.1 Using Lists for Collecting Function Data . . . . . . .
4.2.2 Basics of Numerical Python Arrays . . . . . . . . . . .
4.2.3 Computing Coordinates and Function Values . . .
4.2.4 Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3 Curve Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3.1 The SciTools and Easyviz Packages . . . . . . . . . . .
4.3.2 Plotting a Single Curve . . . . . . . . . . . . . . . . . . . . . .
4.3.3 Decorating the Plot . . . . . . . . . . . . . . . . . . . . . . . . .
4.3.4 Plotting Multiple Curves . . . . . . . . . . . . . . . . . . . .
4.3.5 Controlling Line Styles . . . . . . . . . . . . . . . . . . . . . .
4.3.6 Interactive Plotting Sessions . . . . . . . . . . . . . . . . .
4.3.7 Making Animations . . . . . . . . . . . . . . . . . . . . . . . . .
4.3.8 Advanced Easyviz Topics . . . . . . . . . . . . . . . . . . . .
4.3.9 Curves in Pure Text . . . . . . . . . . . . . . . . . . . . . . . .
4.4 Plotting Difficulties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.4.1 Piecewisely Defined Functions . . . . . . . . . . . . . . . .

169
170
170
171
173

175
175
176
177
178
179
180
181
183
183
185
189
190
193
198
199
199

3.2

3.3

3.4
3.5

3.6

3.7

4


120
121
125
126
127
127


xii

Contents

4.4.2 Rapidly Varying Functions . . . . . . . . . . . . . . . . . . .
4.4.3 Vectorizing StringFunction Objects . . . . . . . . . . .
More on Numerical Python Arrays . . . . . . . . . . . . . . . . . .
4.5.1 Copying Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.5.2 In-Place Arithmetics . . . . . . . . . . . . . . . . . . . . . . . .
4.5.3 Allocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.5.4 Generalized Indexing . . . . . . . . . . . . . . . . . . . . . . . .
4.5.5 Testing for the Array Type . . . . . . . . . . . . . . . . . .
4.5.6 Equally Spaced Numbers . . . . . . . . . . . . . . . . . . . .
4.5.7 Compact Syntax for Array Generation . . . . . . . . .
4.5.8 Shape Manipulation . . . . . . . . . . . . . . . . . . . . . . . . .
Higher-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . .
4.6.1 Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . . .
4.6.2 Two-Dimensional Numerical Python Arrays . . . .
4.6.3 Array Computing . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.6.4 Two-Dimensional Arrays and Functions of Two
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4.6.5 Matrix Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.7.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.7.2 Summarizing Example: Animating a Function . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

205
206
207
207
207
208
209
210
211
212
212
213
213
214
216

5

Sequences and Difference Equations . . . . . . . . . . . . . .
5.1 Mathematical Models Based on Difference Equations . .
5.1.1 Interest Rates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.2 The Factorial as a Difference Equation . . . . . . . .
5.1.3 Fibonacci Numbers . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.4 Growth of a Population . . . . . . . . . . . . . . . . . . . . . .

5.1.5 Logistic Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.6 Payback of a Loan . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.7 Taylor Series as a Difference Equation . . . . . . . . .
5.1.8 Making a Living from a Fortune . . . . . . . . . . . . . .
5.1.9 Newton’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1.10 The Inverse of a Function . . . . . . . . . . . . . . . . . . . .
5.2 Programming with Sound . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.1 Writing Sound to File . . . . . . . . . . . . . . . . . . . . . . .
5.2.2 Reading Sound from File . . . . . . . . . . . . . . . . . . . .
5.2.3 Playing Many Notes . . . . . . . . . . . . . . . . . . . . . . . .
5.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3.2 Summarizing Example: Music of a Sequence . . . .
5.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

235
236
237
239
240
241
242
244
245
246
247
251
253
253
254

255
256
256
257
260

6

Files, Strings, and Dictionaries . . . . . . . . . . . . . . . . . . . . 269

4.5

4.6

4.7

4.8

217
217
219
219
220
225


Contents

xiii


6.1 Reading Data from File . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.1.1 Reading a File Line by Line . . . . . . . . . . . . . . . . . .
6.1.2 Reading a Mixture of Text and Numbers . . . . . .
6.1.3 What Is a File, Really? . . . . . . . . . . . . . . . . . . . . . .
6.2 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.2.1 Making Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . .
6.2.2 Dictionary Operations . . . . . . . . . . . . . . . . . . . . . . .
6.2.3 Example: Polynomials as Dictionaries . . . . . . . . .
6.2.4 Example: File Data in Dictionaries . . . . . . . . . . . .
6.2.5 Example: File Data in Nested Dictionaries . . . . .
6.2.6 Example: Comparing Stock Prices . . . . . . . . . . . .
6.3 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.3.1 Common Operations on Strings . . . . . . . . . . . . . . .
6.3.2 Example: Reading Pairs of Numbers . . . . . . . . . .
6.3.3 Example: Reading Coordinates . . . . . . . . . . . . . . .
6.4 Reading Data from Web Pages . . . . . . . . . . . . . . . . . . . . . .
6.4.1 About Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.4.2 How to Access Web Pages in Programs . . . . . . . .
6.4.3 Example: Reading Pure Text Files . . . . . . . . . . . .
6.4.4 Example: Extracting Data from an HTML Page
6.5 Writing Data to File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.5.1 Example: Writing a Table to File . . . . . . . . . . . . .
6.5.2 Standard Input and Output as File Objects . . . .
6.5.3 Reading and Writing Spreadsheet Files . . . . . . . .
6.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.6.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
6.6.2 Summarizing Example: A File Database . . . . . . .
6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7


269
270
273
274
278
278
279
280
282
283
287
291
292
295
298
300
300
302
302
304
308
309
310
312
317
317
319
323


Introduction to Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
7.1 Simple Function Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.1.1 Problem: Functions with Parameters . . . . . . . . . .
7.1.2 Representing a Function as a Class . . . . . . . . . . . .
7.1.3 Another Function Class Example . . . . . . . . . . . . .
7.1.4 Alternative Function Class Implementations . . . .
7.1.5 Making Classes Without the Class Construct . . .
7.2 More Examples on Classes . . . . . . . . . . . . . . . . . . . . . . . . .
7.2.1 Bank Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2.2 Phone Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.2.3 A Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.3 Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.3.1 The Call Special Method . . . . . . . . . . . . . . . . . . . .
7.3.2 Example: Automagic Differentiation . . . . . . . . . . .
7.3.3 Example: Automagic Integration . . . . . . . . . . . . . .
7.3.4 Turning an Instance into a String . . . . . . . . . . . . .

338
338
340
346
347
349
352
352
354
355
356
357
357

360
362


xiv

Contents

7.3.5
7.3.6
7.3.7
7.3.8

7.4

7.5

7.6

7.7
7.8

7.9

8

Example: Phone Book with Special Methods . . .
Adding Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Example: Class for Polynomials . . . . . . . . . . . . . . .
Arithmetic Operations and Other Special

Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.3.9 More on Special Methods for String Conversion .
Example: Solution of Differential Equations . . . . . . . . . .
7.4.1 A Function for Solving ODEs . . . . . . . . . . . . . . . .
7.4.2 A Class for Solving ODEs . . . . . . . . . . . . . . . . . . . .
7.4.3 Verifying the Implementation . . . . . . . . . . . . . . . . .
7.4.4 Example: Logistic Growth . . . . . . . . . . . . . . . . . . .
Example: Class for Vectors in the Plane . . . . . . . . . . . . . .
7.5.1 Some Mathematical Operations on Vectors . . . . .
7.5.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.5.3 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Example: Class for Complex Numbers . . . . . . . . . . . . . . .
7.6.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.6.2 Illegal Operations . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.6.3 Mixing Complex and Real Numbers . . . . . . . . . . .
7.6.4 Special Methods for “Right” Operands . . . . . . . . .
7.6.5 Inspecting Instances . . . . . . . . . . . . . . . . . . . . . . . . .
Static Methods and Attributes . . . . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.8.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7.8.2 Summarizing Example: Interval Arithmetics . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

363
365
365
369
370
372
373

374
376
377
378
378
378
380
382
382
383
384
387
388
389
391
391
392
397

Random Numbers and Simple Games . . . . . . . . . . . . 417
8.1 Drawing Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . .
8.1.1 The Seed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.1.2 Uniformly Distributed Random Numbers . . . . . .
8.1.3 Visualizing the Distribution . . . . . . . . . . . . . . . . . .
8.1.4 Vectorized Drawing of Random Numbers . . . . . .
8.1.5 Computing the Mean and Standard Deviation . .
8.1.6 The Gaussian or Normal Distribution . . . . . . . . .
8.2 Drawing Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.2.1 Random Integer Functions . . . . . . . . . . . . . . . . . . .
8.2.2 Example: Throwing a Die . . . . . . . . . . . . . . . . . . . .

8.2.3 Drawing a Random Element from a List . . . . . . .
8.2.4 Example: Drawing Cards from a Deck . . . . . . . . .
8.2.5 Example: Class Implementation of a Deck . . . . .
8.3 Computing Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.3.1 Principles of Monte Carlo Simulation . . . . . . . . . .
8.3.2 Example: Throwing Dice . . . . . . . . . . . . . . . . . . . . .
8.3.3 Example: Drawing Balls from a Hat . . . . . . . . . . .

418
418
419
420
421
422
423
424
425
426
427
427
429
432
432
433
435


Contents

xv


8.3.4

8.4

8.5

8.6

8.7

8.8

8.9

9

Example: Policies for Limiting Population
Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Simple Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.4.1 Guessing a Number . . . . . . . . . . . . . . . . . . . . . . . . .
8.4.2 Rolling Two Dice . . . . . . . . . . . . . . . . . . . . . . . . . . .
Monte Carlo Integration . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.5.1 Standard Monte Carlo Integration . . . . . . . . . . . .
8.5.2 Computing Areas by Throwing Random Points .
Random Walk in One Space Dimension . . . . . . . . . . . . . .
8.6.1 Basic Implementation . . . . . . . . . . . . . . . . . . . . . . .
8.6.2 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.6.3 Random Walk as a Difference Equation . . . . . . . .
8.6.4 Computing Statistics of the Particle Positions . .

8.6.5 Vectorized Implementation . . . . . . . . . . . . . . . . . . .
Random Walk in Two Space Dimensions . . . . . . . . . . . . .
8.7.1 Basic Implementation . . . . . . . . . . . . . . . . . . . . . . .
8.7.2 Vectorized Implementation . . . . . . . . . . . . . . . . . . .
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.8.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8.8.2 Summarizing Example: Random Growth . . . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Object-Oriented Programming . . . . . . . . . . . . . . . . . . . .
9.1 Inheritance and Class Hierarchies . . . . . . . . . . . . . . . . . . .
9.1.1 A Class for Straight Lines . . . . . . . . . . . . . . . . . . . .
9.1.2 A First Try on a Class for Parabolas . . . . . . . . . .
9.1.3 A Class for Parabolas Using Inheritance . . . . . . .
9.1.4 Checking the Class Type . . . . . . . . . . . . . . . . . . . .
9.1.5 Attribute versus Inheritance . . . . . . . . . . . . . . . . . .
9.1.6 Extending versus Restricting Functionality . . . . .
9.1.7 Superclass for Defining an Interface . . . . . . . . . . .
9.2 Class Hierarchy for Numerical Differentiation . . . . . . . . .
9.2.1 Classes for Differentiation . . . . . . . . . . . . . . . . . . . .
9.2.2 A Flexible Main Program . . . . . . . . . . . . . . . . . . . .
9.2.3 Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2.4 Alternative Implementation via Functions . . . . . .
9.2.5 Alternative Implementation via Functional
Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.2.6 Alternative Implementation via a Single Class . .
9.3 Class Hierarchy for Numerical Integration . . . . . . . . . . . .
9.3.1 Numerical Integration Methods . . . . . . . . . . . . . . .
9.3.2 Classes for Integration . . . . . . . . . . . . . . . . . . . . . . .
9.3.3 Using the Class Hierarchy . . . . . . . . . . . . . . . . . . . .

9.3.4 About Object-Oriented Programming . . . . . . . . .
9.4 Class Hierarchy for Numerical Methods for ODEs . . . . .

437
440
440
440
443
443
446
447
448
449
449
450
451
453
453
455
456
456
457
463
479
479
480
481
481
483
484

485
486
488
488
491
492
495
496
497
499
499
501
504
507
508


xvi

Contents

9.4.1 Mathematical Problem . . . . . . . . . . . . . . . . . . . . . .
9.4.2 Numerical Methods . . . . . . . . . . . . . . . . . . . . . . . . .
9.4.3 The ODE Solver Class Hierarchy . . . . . . . . . . . . .
9.4.4 The Backward Euler Method . . . . . . . . . . . . . . . . .
9.4.5 Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.4.6 Application 1: u′ = u . . . . . . . . . . . . . . . . . . . . . . . .
9.4.7 Application 2: The Logistic Equation . . . . . . . . . .
9.4.8 Application 3: An Oscillating System . . . . . . . . . .
9.4.9 Application 4: The Trajectory of a Ball . . . . . . . .

9.5 Class Hierarchy for Geometric Shapes . . . . . . . . . . . . . . .
9.5.1 Using the Class Hierarchy . . . . . . . . . . . . . . . . . . . .
9.5.2 Overall Design of the Class Hierarchy . . . . . . . . .
9.5.3 The Drawing Tool . . . . . . . . . . . . . . . . . . . . . . . . . .
9.5.4 Implementation of Shape Classes . . . . . . . . . . . . .
9.5.5 Scaling, Translating, and Rotating a Figure . . . .
9.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.6.1 Chapter Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9.6.2 Summarizing Example: Input Data Reader . . . . .
9.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

508
510
511
515
518
518
519
521
523
525
526
527
529
530
534
538
538
540
546


A Discrete Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573
A.1 Discrete Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A.1.1 The Sine Function . . . . . . . . . . . . . . . . . . . . . . . . . .
A.1.2 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A.1.3 Evaluating the Approximation . . . . . . . . . . . . . . . .
A.1.4 Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A.2 Differentiation Becomes Finite Differences . . . . . . . . . . . .
A.2.1 Differentiating the Sine Function . . . . . . . . . . . . . .
A.2.2 Differences on a Mesh . . . . . . . . . . . . . . . . . . . . . . .
A.2.3 Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A.3 Integration Becomes Summation . . . . . . . . . . . . . . . . . . . .
A.3.1 Dividing into Subintervals . . . . . . . . . . . . . . . . . . .
A.3.2 Integration on Subintervals . . . . . . . . . . . . . . . . . .
A.3.3 Adding the Subintervals . . . . . . . . . . . . . . . . . . . . .
A.3.4 Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A.4 Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A.4.1 Approximating Functions Close to One Point . . .
A.4.2 Approximating the Exponential Function . . . . . .
A.4.3 More Accurate Expansions . . . . . . . . . . . . . . . . . . .
A.4.4 Accuracy of the Approximation . . . . . . . . . . . . . . .
A.4.5 Derivatives Revisited . . . . . . . . . . . . . . . . . . . . . . . .
A.4.6 More Accurate Difference Approximations . . . . .
A.4.7 Second-Order Derivatives . . . . . . . . . . . . . . . . . . . .
A.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

573
574
576
576

577
579
580
580
582
583
584
585
586
587
589
589
589
590
592
594
595
597
599


Contents

xvii

B Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 605
B.1
B.2
B.3
B.4

B.5
B.6
B.7

The Simplest Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Exponential Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Logistic Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A General Ordinary Differential Equation . . . . . . . . . . . .
A Simple Pendulum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
A Model for the Spread of a Disease . . . . . . . . . . . . . . . . .
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

606
608
612
614
615
619
621

C A Complete Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 625
C.1 About the Problem: Motion and Forces in Physics . . . . .
C.1.1 The Physical Problem . . . . . . . . . . . . . . . . . . . . . . .
C.1.2 The Computational Algorithm . . . . . . . . . . . . . . .
C.1.3 Derivation of the Mathematical Model . . . . . . . . .
C.1.4 Derivation of the Algorithm . . . . . . . . . . . . . . . . . .
C.2 Program Development and Testing . . . . . . . . . . . . . . . . . .
C.2.1 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C.2.2 Callback Functionality . . . . . . . . . . . . . . . . . . . . . . .
C.2.3 Making a Module . . . . . . . . . . . . . . . . . . . . . . . . . . .

C.2.4 Verification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C.3 Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
C.3.1 Simultaneous Computation and Plotting . . . . . . .
C.3.2 Some Applications . . . . . . . . . . . . . . . . . . . . . . . . . .
C.3.3 Remark on Choosing ∆t . . . . . . . . . . . . . . . . . . . . .
C.3.4 Comparing Several Quantities in Subplots . . . . .
C.3.5 Comparing Approximate and Exact Solutions . .
C.3.6 Evolution of the Error as ∆t Decreases . . . . . . . .
C.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

626
626
628
628
631
632
632
635
636
637
639
639
642
643
644
645
646
649

D Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 651

D.1 Using a Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
D.2 How to Debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
D.2.1 A Recipe for Program Writing and Debugging . .
D.2.2 Application of the Recipe . . . . . . . . . . . . . . . . . . . .

651
653
654
656

E Technical Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669
E.1 Different Ways of Running Python Programs . . . . . . . . .
E.1.1 Executing Python Programs in IPython . . . . . . .
E.1.2 Executing Python Programs on Unix . . . . . . . . . .
E.1.3 Executing Python Programs on Windows . . . . . .
E.1.4 Executing Python Programs on Macintosh . . . . .
E.1.5 Making a Complete Stand-Alone Executable . . .
E.2 Integer and Float Division . . . . . . . . . . . . . . . . . . . . . . . . . .
E.3 Visualizing a Program with Lumpy . . . . . . . . . . . . . . . . . .
E.4 Doing Operating System Tasks in Python . . . . . . . . . . . .
E.5 Variable Number of Function Arguments . . . . . . . . . . . . .

669
669
669
671
673
673
673
674

675
678


xviii

Contents

E.5.1 Variable Number of Positional Arguments . . . . .
E.5.2 Variable Number of Keyword Arguments . . . . . .
E.6 Evaluating Program Efficiency . . . . . . . . . . . . . . . . . . . . . .
E.6.1 Making Time Measurements . . . . . . . . . . . . . . . . .
E.6.2 Profiling Python Programs . . . . . . . . . . . . . . . . . . .

679
681
683
683
685

Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689


List of Exercises

Exercise
Exercise
Exercise
Exercise

Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise
Exercise

1.1
1.2
1.3

1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.11
1.12
1.13
1.14
1.15
1.16
1.17
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
2.10
2.11
2.12
2.13

Compute 1+1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Write a “Hello, World!” program . . . . . . . . . . . . . . .

Convert from meters to British length units . . . . .
Compute the mass of various substances . . . . . . . .
Compute the growth of money in a bank . . . . . . . .
Find error(s) in a program . . . . . . . . . . . . . . . . . . . .
Type in program text . . . . . . . . . . . . . . . . . . . . . . . .
Type in programs and debug them . . . . . . . . . . . . .
Evaluate a Gaussian function . . . . . . . . . . . . . . . . . .
Compute the air resistance on a football . . . . . . . .
Define objects in IPython . . . . . . . . . . . . . . . . . . . . .
How to cook the perfect egg . . . . . . . . . . . . . . . . . . .
Evaluate a function defined by a sum . . . . . . . . . . .
Derive the trajectory of a ball . . . . . . . . . . . . . . . . .
Find errors in the coding of formulas . . . . . . . . . . .
Find errors in Python statements . . . . . . . . . . . . . .
Find errors in the coding of a formula . . . . . . . . . .
Make a Fahrenheit–Celsius conversion table . . . . .
Generate odd numbers . . . . . . . . . . . . . . . . . . . . . . .
Store odd numbers in a list . . . . . . . . . . . . . . . . . . .
Generate odd numbers by the range function . . . .
Simulate operations on lists by hand . . . . . . . . . . .
Make a table of values from formula (1.1) . . . . . . .
Store values from formula (1.1) in lists . . . . . . . . . .
Work with a list . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Generate equally spaced coordinates . . . . . . . . . . .
Use a list comprehension to solve Exer. 2.9 . . . . . .
Store data from Exer. 2.7 in a nested list . . . . . . .
Compute a mathematical sum . . . . . . . . . . . . . . . . .
Simulate a program by hand . . . . . . . . . . . . . . . . . .

42

43
43
43
43
43
43
44
44
45
45
46
46
47
48
48
49
99
99
100
100
100
100
100
100
100
101
101
101
101
xix



×