1. Trang chủ
  2. » Công Nghệ Thông Tin

A Primer on Scientific Programming with Python pptx

832 2,1K 2
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề A Primer on Scientific Programming with Python
Tác giả Hans Petter Langtangen
Chuyên ngành Computational Science and Engineering
Thể loại Sách giáo trình
Năm xuất bản Third Edition
Định dạng
Số trang 832
Dung lượng 7,18 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

A widely used strategy for software developerswho want to write Python code that works with both versions, is todevelop for v2.7, which is very close to what is accepted in version 3,and

Trang 5

A Primer on Scientific Programming

with Python

Third Edition

Trang 6

Simula Research Laboratory

ISSN 1611-0994 Texts in Computational Science and Engineering

ISBN 978-3-642-30292-3 ISBN 978-3-642-30293-0 (eBook)

DOI 10.1007/978-3-642-30293-0

Springer Heidelberg New York Dordrecht London

Library of Congress Control Number: 2012942179

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, 2011, 2012

This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work Duplication of this pub- lication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer Permis- sions for use may be obtained through RightsLink at the Copyright Clearance Center Violations are liable

to prosecution under the respective Copyright Law.

The use of general descriptive names, registered names, trademarks, service marks, 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.

While the advice and information in this book are believed to be true and accurate at the date of tion, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors

publica-or omissions that may be made The publisher makes no warranty, express publica-or implied, with respect to the material contained herein.

Printed on acid-free paper

Springer is part of Springer Science+Business Media (www.springer.com)

Trang 7

The aim of this book is to teach computer programming using ples from mathematics and the natural sciences We have chosen to usethe Python programming language because it combines remarkable ex-pressive power with very clean, simple, and compact syntax Python iseasy to learn and very well suited for an introduction to computer pro-gramming Python is also quite similar to Matlab and a good languagefor doing mathematical computing It is easy to combine Python withcompiled languages, like Fortran, C, and C++, which are widely usedlanguages for scientific computations A seamless integration of Pythonwith Java is offered by a special version of Python called Jython.The examples in this book integrate programming with applications

exam-to mathematics, physics, biology, and finance The reader is expected exam-tohave 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 sical and numerical aspects of calculus Although not strictly required,

clas-a bclas-ackground in high school physics mclas-akes mclas-any of the exclas-amples moremeaningful

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 aproblem solving technique That is why most of the pages are devoted

to case studies in programming, where we define a problem and explainhow to create the corresponding program New constructions and pro-gramming styles (what we could call theory) is also usually introducedvia examples Special attention is paid to verification of programs and

to finding errors These topics are very demanding for mathematicalsoftware, because the unavoidable numerical approximation errors arepossibly mixed with programming mistakes

v

Trang 8

By studying the many examples in the book, I hope readers willlearn how to think right and thereby write programs in a quicker andmore reliable way Remember, nobody can learn programming by justreading – one has to solve a large amount of exercises hands on Thebook is therefore full of exercises of various types: modifications ofexisting examples, completely new problems, or debugging of givenprograms.

To work with this book, I recommend to use Python version 2.7 though version 2.6 will work for most of the material) For Chapters 5–9and Appendices A–E you need the NumPy, Matplotlib, SciTools pack-ages, and for Appendix G Cython is also required There is a web pageassociated with this book, http:/hplgit.github.com/scipro-primer,which lists the software you need and explains briefly how to install

(al-it This page also contains all the files associated with the programexamples in this book

Python Version 2 or 3? A common problem among Python

program-mers is to choose between version 2 or 3, which at the time of thiswriting means choosing between version 2.7 and 3.3 The general rec-ommendation is to go for version 3, but programs are then not com-patible with version 2 and vice versa There is still a problem thatmuch useful mathematical software in Python has not yet been ported

to version 3 Therefore, scientific computing with Python still goesmostly with version 2 A widely used strategy for software developerswho want to write Python code that works with both versions, is todevelop for v2.7, which is very close to what is accepted in version 3,and then use the translation tool 2to3 to automatically translate the

code to version 3

When using v2.7, one should employ the newest syntax and modulesthat make the differences between version 2 and 3 very small Thisstrategy is adopted in the present book Only two differences betweenversions 2 and 3 are expected to be significant for the programs inthe book: a/b implies float division in version 3 if a and b are inte-gers, and print ’Hello’ in version 2 must be turned into a function

to any annoying problems when future readers study the book’s v2.7examples, but program in version 3 Anyway, running 2to3 on the ex-ample files generates the corresponding version 3 code

Contents Chapter 1 introduces variables, objects, modules, and text

formatting through examples concerning evaluation of mathematicalformulas Chapter 2 presents programming with while and for loops

as well as lists, including nested lists The next chapter deals with twoother fundamental concepts in programming: functions and if-else

tests Successful further reading of the book demands that Chapters 1–

3 are digested

Trang 9

How to read data into programs and deal with errors in input are the

subjects of Chapter 4 Chapter 5 introduces arrays and array computing

(including vectorization) and how this is used for plotting y = f (x)

curves and making animation of curves Many of the examples in the

first five chapters are strongly related Typically, formulas from the first

chapter are used to produce tables of numbers in the second chapter

Then the formulas are encapsulated in functions in the third chapter

In the next chapter, the input to the functions are fetched from the

command line, or from a question-answer dialog with the user, and

validity checks of the input are added The formulas are then shown

as graphs in Chapter 5 After having studied Chapters 1–5, the reader

should have enough knowledge of programming to solve mathematical

problems by what many refer to as “Matlab-style” programming

Chapter 6 explains how to work with files and text data Class

pro-gramming, 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

applica-tions to games and random walks Object-oriented programming, in the

meaning of class hierarchies and inheritance, is the subject of

Chap-ter 9 The key examples here deal with building toolkits for numerical

differentiation and integration as well as graphics

Appendix A introduces mathematical modeling, using sequences and

difference equations We also treat sound as a sequence Only

program-ming concepts from Chapters 1–5 are used in this appendix, the aim

being to consolidate basic programming knowledge and apply it to

mathematical problems Some important mathematical topics are

in-troduced via difference equations in a simple way: Newton’s method,

Taylor series, inverse functions, and dynamical systems

Appendix B deals with functions on a mesh, numerical

differenti-ation, and numerical integration A simple introduction to ordinary

differential equations and their numerical treatment is provided in

Ap-pendix C ApAp-pendix D shows how a complete project in physics can be

solved by mathematical modeling, numerical methods, and

program-ming elements from Chapters 1–5 This project is a good example on

problem solving in computational science, where it is necessary to

in-tegrate physics, mathematics, numerics, and computer science

How to create software for solving systems of ordinary differential

equations, primarily using classes and object-oriented programming,

is the subject of Appendix E The material in this appendix brings

together many of the programming concepts from Chapters 1–9 in a

mathematical setting and ends up with a flexible and general tool for

solving differential equations

Appendix F is devoted to the art of debugging, and in fact problem

solving in general Speeding up numerical computations in Python by

Trang 10

migrating code to C via Cython is exemplified in Appendix G Finally,Appendix H deals with various more advanced technical topics.Most of the examples and exercises in this book are quite short.However, many of the exercises are related, and together they formlarger projects, for example on Fourier Series (3.13, 4.18–4.20, 5.30,5.31), numerical integration (3.5–3.8, 5.38–5.39, A.16), Taylor series(3.30, 5.21, 5.28, A.18–A.19, 7.29), piecewise constant functions (3.24–3.28, 5.23, 5.36–5.37, 7.19–7.25), inverse functions (7.26, E.7–E.10),falling objects (E.11–E.13, E.31–E.32), oscillatory population growth(A.23–A.25, 7.40–7.41), epidemic disease modeling (E.35–E.42), analy-sis of web data (6.22, 6.27–6.29), optimization and finance (A.26, 8.44–8.45), statistics and probability (4.24–4.26, 8.23–8.25), hazard games(8.8–8.14), random walk and statistical physics (8.34–8.42), noisy dataanalysis (8.46–8.50), numerical methods (5.14–5.16, 7.8–7.9, A.12, 7.28,9.16–9.18, E.21–E.29), building a calculus calculator (7.42, 7.43, 9.19,9.20), and creating a toolkit for simulating vibrating engineering sys-tems (E.44–E.49).

Chapters 1–9 and Appendix E have from 2007 formed the core of anintroductory first semester course on scientific programming (INF1100)

at the University of Oslo

Changes to the First Edition Besides numerous corrections of

mis-prints, the second edition features a major reorganization of several

chapters Chapter 2 in the first edition, Basic Constructions, was a

comprehensive chapter, both with respect to length and topics Thischapter has therefore been split in two for the second edition: a new

Chapter 2 Loops and Lists and a new Chapter 3 Functions and

Branch-ing A new Chapter 2.1.4 explicitly explains how to implement a

sum-mation expression by a loop, and later examples present alternativeimplementations

All text and program files that used the getopt module to parsecommand-line options in the first edition now make use of the sim-pler and more flexibleargparsemodule (new in Python v2.7/3.1) Thematerial on curve plotting in Chapter 5 has been thoroughly revised.Now we give an introduction to plotting with Matplotlib as well asSciTools/Easyviz

While the first edition almost exclusively used “star import” for venience (e.g.,from numpy import *andfrom scitools.std import *),the second edition tries to adhere to the standard import numpy as

con-np However, in mathematical formulas that are to work with scalar

and array variables, we do not want an explicit prefix Avoiding the

namespace prefixes is important for making formulas as close to themathematical notation as possible as well as for making the transitionfrom or to Matlab smooth The two import styles have different meritsand applications The choice of style in various examples is carefullythought through in the second edition

Trang 11

Chapter 5 in the first edition, Sequences and Difference Equations,

has now become Appendix A Chapter 6 in the first edition, Files,

Strings, and Dictionaries, has been substantially revised Chapter 6.4

on downloading and interpreting data from web pages have now

com-pletely new examples Exercises are also reworked to fit with the new

examples

The material on differential equations in chapters on classes (Ch 7

and 9 in the first edition) has been extracted, reworked, slightly

ex-panded, and placed in Appendix E This restructuring allows a more

flexible treatment of differential equations, and parts of this important

topic can be addressed right after Chapter 3, if desired

To distinguish between Python’s random module and the one in

edition Now random always refers to Python’s random module, while

oc-casionally as numpy.random) The associated software has been revised

similarly

Changes to the Second Edition Many typos have been fixed in the

third edition, a lot of examples have been improved, some material has

been reorganized and extended, some material is new, several exercises

have been removed and many new ones added, and numerous exercises

are reformulated after feedback from teachers The associated SciTools

package is also extensively upgraded

The reorganized and extended material covers Chapter 4.2.4

on command-line parsing, Chapter 5.5 on vectorization, and

Ap-pendix E.2.8 on building simulation software for ODEs The new

ma-terial consists of Chapter 6.2.4 on dictionaries with default values and

ordering, Chapter 9.4 on making a drawing program, Appendix A.1.7

on integrals as difference equations, Appendix G on using Cython and

combining Python with fast C code, and the bioinformatics examples

in Chapters 3.3, 6.6, 8.3.4, and 9.5

Four new projects are added: numerical integration (Exercises 3.5–

3.8, 5.38–5.39, A.16), piecewise constant functions (Exercises 3.24–3.28,

5.23, 5.36–5.37, 7.19–7.25), inverse functions (Exercises 7.26, E.7–E.10),

and epidemic modeling (Exercises E.35–E.42)

The software for ODEs derived in Appendix E and the drawing

pro-gram from Chapter 9.4 have been much further developed into the

packages Odespy and Pysketcher, both available from github.com

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 B and C 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 help

estab-lishing the associated course (INF1100) at the University of Oslo are

Trang 12

greatly appreciated Without Ilmar and his solutions to numerous nical problems the book would never have been completed Johannes

tech-H Ring also deserves a special acknowledgment for the development ofthe Easyviz graphics tool and for his careful maintenance and support

of software associated with the book Professor Loyce Adams studiedthe entire book, solved all the exercises, found numerous errors, andsuggested many improvements Her contributions are so much appre-ciated I also want to thank Geir Kjetil Sandve for being the primaryauthor of the third edition’s new series of computational bioinformat-ics examples in Chapters 3.3, 6.6, 8.3.4, and 9.5, with contributionsfrom Sveinung Gundersen, Ksenia Khelik, Halfdan Rydbeck, and KaiTrengereid

Several people have helped to make substantial improvements of thetext, the exercises, and the associated software components The author

is thankful to Ingrid Eide, St˚ale Zerener Haugnæss, Kristian Hiorth,Arve Knudsen, Tobias Vidarssønn Langhoff, Martin Vonheim Larsen,Kine Veronica Lund, Solveig Masvie, H˚akon Møller, Rebekka Mørken,Mathias Nedrebø, Marit Sandstad, Helene Norheim Semmerud, LarsStorjord, Fredrik Heffer Valdmanis, and Torkil Vederhus for their con-tributions Hakon Adler is greatly acknowledged for his careful reading

of early various versions of the manuscript The professors Fred pen Bent, Ørnulf Borgan, Geir Dahl, Knut Mørken, and Geir Pedersenhave contributed with many exciting exercises from various applicationfields Great thanks also go to Jan Olav Langseth for creating the coverimage

Es-This book and the associated course are parts of a comprehensive

re-form at the University of Oslo, called Computing in Science Education.

The goal of the reform is to integrate computer programming and ulation in all bachelor courses in natural science where mathematicalmodels are used The present book lays the foundation for the moderncomputerized problem solving technique to be applied in later courses

sim-It has been extremely inspiring to work with the driving forces behindthis reform, especially the professors Morten Hjorth–Jensen, AndersMalthe–Sørenssen, Knut Mørken, and Arnt Inge Vistnes

The excellent assistance from the Springer system, in particularMartin Peters, Thanh-Ha Le Thi, Ruth Allewelt, Peggy Glauch-Ruge,Nadja Kroke, Thomas Schmidt, Patrick Waltemate, Donatas Akma-naviˇcius, and Edita Baronait˙e, is highly appreciated, and ensured asmooth and rapid production of all editions of this book

Trang 13

1 Computing with Formulas 1

1.1 The First Programming Encounter: A Formula 1

1.1.1 Using a Program as a Calculator 2

1.1.2 About Programs and Programming 2

1.1.3 Tools for Writing Programs 3

1.1.4 Using Idle to Write the Program 4

1.1.5 How to Run the Program 7

1.1.6 Verifying the Result 8

1.1.7 Using Variables 8

1.1.8 Names of Variables 9

1.1.9 Reserved Words in Python 10

1.1.10 Comments 10

1.1.11 Formatting Text and Numbers 11

1.2 Computer Science Glossary 14

1.3 Another Formula: Celsius-Fahrenheit Conversion 19

1.3.1 Potential Error: Integer Division 19

1.3.2 Objects in Python 20

1.3.3 Avoiding Integer Division 21

1.3.4 Arithmetic Operators and Precedence 22

1.4 Evaluating Standard Mathematical Functions 23

1.4.1 Example: Using the Square Root Function 23

1.4.2 Example: Using More Mathematical Functions 25 1.4.3 A First Glimpse of Round-Off Errors 26

1.5 Interactive Computing 27

1.5.1 Using the Python Shell 27

1.5.2 Type Conversion 28

1.5.3 IPython 29

1.6 Complex Numbers 32

1.6.1 Complex Arithmetics in Python 33

xi

Trang 14

1.6.2 Complex Functions in Python 33

1.6.3 Unified Treatment of Complex and Real Functions 34 1.7 Summary 35

1.7.1 Chapter Topics 35

1.7.2 Example: Trajectory of a Ball 39

1.7.3 About Typesetting Conventions in This Book 40

1.8 Exercises 41

2 Loops and Lists 49

2.1 While Loops 49

2.1.1 A Naive Solution 49

2.1.2 While Loops 50

2.1.3 Boolean Expressions 52

2.1.4 Loop Implementation of a Sum 54

2.2 Lists 55

2.2.1 Basic List Operations 55

2.2.2 For Loops 58

2.3 Alternative Implementations with Lists and Loops 60

2.3.1 While Loop Implementation of a For Loop 60

2.3.2 The Range Construction 60

2.3.3 For Loops with List Indices 61

2.3.4 Changing List Elements 63

2.3.5 List Comprehension 63

2.3.6 Traversing Multiple Lists Simultaneously 64

2.4 Nested Lists 64

2.4.1 A Table as a List of Rows or Columns 65

2.4.2 Printing Objects 66

2.4.3 Extracting Sublists 67

2.4.4 Traversing Nested Lists 69

2.5 Tuples 71

2.6 Summary 72

2.6.1 Chapter Topics 72

2.6.2 Example: Analyzing List Data 75

2.6.3 How to Find More Python Information 78

2.7 Exercises 79

3 Functions and Branching 87

3.1 Functions 87

3.1.1 Functions of One Variable 87

3.1.2 Local and Global Variables 89

3.1.3 Multiple Arguments 91

3.1.4 Multiple Return Values 93

3.1.5 Functions with No Return Values 95

3.1.6 Keyword Arguments 96

3.1.7 Doc Strings 99

Trang 15

3.1.8 Function Input and Output 100

3.1.9 Functions as Arguments to Functions 100

3.1.10 The Main Program 102

3.1.11 Lambda Functions 103

3.2 Branching 104

3.2.1 If-Else Blocks 104

3.2.2 Inline If Tests 106

3.3 Mixing Loops, Branching, and Functions in Bioinformatics Examples 107

3.3.1 Counting Letters in DNA Strings 107

3.3.2 Efficiency Assessment 113

3.4 Summary 115

3.4.1 Chapter Topics 115

3.4.2 Example: Numerical Integration 116

3.5 Exercises 120

4 Input Data and Error Handling 137

4.1 Asking Questions and Reading Answers 138

4.1.1 Reading Keyboard Input 138

4.1.2 The Magic “eval” Function 139

4.1.3 The Magic “exec” Function 143

4.1.4 Turning String Expressions into Functions 144

4.2 Reading from the Command Line 145

4.2.1 Providing Input on the Command Line 145

4.2.2 A Variable Number of Command-Line Arguments 146 4.2.3 More on Command-Line Arguments 147

4.2.4 Option–Value Pairs on the Command Line 148

4.3 Handling Errors 152

4.3.1 Exception Handling 153

4.3.2 Raising Exceptions 156

4.4 A Glimpse of Graphical User Interfaces 158

4.5 Making Modules 161

4.5.1 Example: Interest on Bank Deposits 161

4.5.2 Collecting Functions in a Module File 162

4.5.3 Using Modules 167

4.6 Summary 169

4.6.1 Chapter Topics 169

4.6.2 Example: Bisection Root Finding 172

4.7 Exercises 180

5 Array Computing and Curve Plotting 187

5.1 Vectors 188

5.1.1 The Vector Concept 188

5.1.2 Mathematical Operations on Vectors 189

5.1.3 Vector Arithmetics and Vector Functions 191

Trang 16

5.2 Arrays in Python Programs 193

5.2.1 Using Lists for Collecting Function Data 193

5.2.2 Basics of Numerical Python Arrays 194

5.2.3 Computing Coordinates and Function Values 195

5.2.4 Vectorization 196

5.3 Curve Plotting 198

5.3.1 Matplotlib; Pylab 198

5.3.2 Matplotlib; Pyplot 202

5.3.3 SciTools and Easyviz 204

5.3.4 Making Animations 209

5.3.5 Curves in Pure Text 214

5.4 Plotting Difficulties 215

5.4.1 Piecewisely Defined Functions 216

5.4.2 Rapidly Varying Functions 218

5.5 More Advanced Vectorization of Functions 219

5.5.1 Vectorizing StringFunction Objects 220

5.5.2 Vectorization of the Heaviside Function 221

5.5.3 Vectorization of a Hat Function 224

5.6 More on Numerical Python Arrays 226

5.6.1 Copying Arrays 226

5.6.2 In-Place Arithmetics 227

5.6.3 Allocating Arrays 228

5.6.4 Generalized Indexing 228

5.6.5 Testing for the Array Type 229

5.6.6 Compact Syntax for Array Generation 230

5.6.7 Shape Manipulation 230

5.7 Higher-Dimensional Arrays 231

5.7.1 Matrices and Arrays 231

5.7.2 Two-Dimensional Numerical Python Arrays 232

5.7.3 Array Computing 235

5.7.4 Two-Dimensional Arrays and Functions of Two Variables 235

5.7.5 Matrix Objects 236

5.8 Summary 237

5.8.1 Chapter Topics 237

5.8.2 Example: Animating a Function 239

5.9 Exercises 243

6 Files, Strings, and Dictionaries 257

6.1 Reading Data from File 257

6.1.1 Reading a File Line by Line 258

6.1.2 Reading a Mixture of Text and Numbers 261

6.1.3 What Is a File, Really? 262

6.2 Dictionaries 266

6.2.1 Making Dictionaries 266

Trang 17

6.2.2 Dictionary Operations 267

6.2.3 Example: Polynomials as Dictionaries 269

6.2.4 Dictionaries with Default Values and Ordering 271

6.2.5 Example: File Data in Dictionaries 273

6.2.6 Example: File Data in Nested Dictionaries 274

6.2.7 Example: Comparing Stock Prices 278

6.3 Strings 282

6.3.1 Common Operations on Strings 282

6.3.2 Example: Reading Pairs of Numbers 286

6.3.3 Example: Reading Coordinates 288

6.4 Reading Data from Web Pages 291

6.4.1 About Web Pages 291

6.4.2 How to Access Web Pages in Programs 292

6.4.3 Example: Reading Pure Text Files 293

6.4.4 Example: Extracting Data from HTML 295

6.5 Writing Data to File 296

6.5.1 Example: Writing a Table to File 296

6.5.2 Standard Input and Output as File Objects 298

6.5.3 Reading and Writing Spreadsheet Files 300

6.6 Examples from Analyzing DNA 305

6.6.1 Computing Frequencies 305

6.6.2 Analyzing the Frequency Matrix 312

6.6.3 Finding Base Frequencies 315

6.6.4 Translating Genes into Proteins 317

6.6.5 Some Humans Can Drink Milk, While Others Cannot 322

6.7 Summary 323

6.7.1 Chapter Topics 323

6.7.2 Example: A File Database 325

6.8 Exercises 329

7 Introduction to Classes 341

7.1 Simple Function Classes 342

7.1.1 Problem: Functions with Parameters 342

7.1.2 Representing a Function as a Class 344

7.1.3 Another Function Class Example 350

7.1.4 Alternative Function Class Implementations 351

7.1.5 Making Classes Without the Class Construct 353

7.2 More Examples on Classes 356

7.2.1 Bank Accounts 356

7.2.2 Phone Book 358

7.2.3 A Circle 359

7.3 Special Methods 360

7.3.1 The Call Special Method 361

7.3.2 Example: Automagic Differentiation 361

Trang 18

7.3.3 Example: Automagic Integration 364

7.3.4 Turning an Instance into a String 366

7.3.5 Example: Phone Book with Special Methods 367

7.3.6 Adding Objects 369

7.3.7 Example: Class for Polynomials 369

7.3.8 Arithmetic Operations and Other Special Methods 373

7.3.9 Special Methods for String Conversion 374

7.4 Example: Class for Vectors in the Plane 375

7.4.1 Some Mathematical Operations on Vectors 376

7.4.2 Implementation 376

7.4.3 Usage 378

7.5 Example: Class for Complex Numbers 379

7.5.1 Implementation 380

7.5.2 Illegal Operations 381

7.5.3 Mixing Complex and Real Numbers 382

7.5.4 Special Methods for “Right” Operands 384

7.5.5 Inspecting Instances 386

7.6 Static Methods and Attributes 387

7.7 Summary 388

7.7.1 Chapter Topics 388

7.7.2 Example: Interval Arithmetics 389

7.8 Exercises 395

8 Random Numbers and Simple Games 413

8.1 Drawing Random Numbers 414

8.1.1 The Seed 414

8.1.2 Uniformly Distributed Random Numbers 415

8.1.3 Visualizing the Distribution 416

8.1.4 Vectorized Drawing of Random Numbers 417

8.1.5 Computing the Mean and Standard Deviation 418

8.1.6 The Gaussian or Normal Distribution 419

8.2 Drawing Integers 420

8.2.1 Random Integer Functions 421

8.2.2 Example: Throwing a Die 422

8.2.3 Drawing a Random Element from a List 422

8.2.4 Example: Drawing Cards from a Deck 423

8.2.5 Example: Class Implementation of a Deck 425

8.3 Computing Probabilities 428

8.3.1 Principles of Monte Carlo Simulation 428

8.3.2 Example: Throwing Dice 429

8.3.3 Example: Drawing Balls from a Hat 432

8.3.4 Random Mutations of Genes 434

8.3.5 Example: Policies for Limiting Population Growth 439 8.4 Simple Games 442

Trang 19

8.4.1 Guessing a Number 442

8.4.2 Rolling Two Dice 443

8.5 Monte Carlo Integration 446

8.5.1 Standard Monte Carlo Integration 446

8.5.2 Area Computing by Throwing Random Points 448

8.6 Random Walk in One Space Dimension 450

8.6.1 Basic Implementation 451

8.6.2 Visualization 452

8.6.3 Random Walk as a Difference Equation 452

8.6.4 Computing Statistics of the Particle Positions 453

8.6.5 Vectorized Implementation 454

8.7 Random Walk in Two Space Dimensions 456

8.7.1 Basic Implementation 456

8.7.2 Vectorized Implementation 457

8.8 Summary 459

8.8.1 Chapter Topics 459

8.8.2 Example: Random Growth 460

8.9 Exercises 466

9 Object-Oriented Programming 483

9.1 Inheritance and Class Hierarchies 483

9.1.1 A Class for Straight Lines 484

9.1.2 A First Try on a Class for Parabolas 485

9.1.3 A Class for Parabolas Using Inheritance 485

9.1.4 Checking the Class Type 487

9.1.5 Attribute Versus Inheritance 488

9.1.6 Extending Versus Restricting Functionality 489

9.1.7 Superclass for Defining an Interface 490

9.2 Class Hierarchy for Numerical Differentiation 492

9.2.1 Classes for Differentiation 493

9.2.2 A Flexible Main Program 496

9.2.3 Extensions 497

9.2.4 Alternative Implementation via Functions 500

9.2.5 Alternative Implementation via Functional Programming 501

9.2.6 Alternative Implementation via a Single Class 502

9.3 Class Hierarchy for Numerical Integration 504

9.3.1 Numerical Integration Methods 504

9.3.2 Classes for Integration 505

9.3.3 Using the Class Hierarchy 509

9.3.4 About Object-Oriented Programming 511

9.4 Class Hierarchy for Making Drawings 513

9.4.1 Using the Object Collection 514

9.4.2 Example of Classes for Geometric Objects 523

9.4.3 Adding Functionality via Recursion 528

Trang 20

9.4.4 Scaling, Translating, and Rotating a Figure 531

9.5 Classes for DNA Analysis 534

9.5.1 Class for Regions 534

9.5.2 Class for Genes 534

9.5.3 Subclasses 539

9.6 Summary 541

9.6.1 Chapter Topics 541

9.6.2 Example: Input Data Reader 542

9.7 Exercises 548

A Sequences and Difference Equations 557

A.1 Mathematical Models Based on Difference Equations 558

A.1.1 Interest Rates 559

A.1.2 The Factorial as a Difference Equation 561

A.1.3 Fibonacci Numbers 562

A.1.4 Growth of a Population 563

A.1.5 Logistic Growth 564

A.1.6 Payback of a Loan 566

A.1.7 The Integral as a Difference Equation 567

A.1.8 Taylor Series as a Difference Equation 569

A.1.9 Making a Living from a Fortune 571

A.1.10 Newton’s Method 571

A.1.11 The Inverse of a Function 575

A.2 Programming with Sound 577

A.2.1 Writing Sound to File 578

A.2.2 Reading Sound from File 579

A.2.3 Playing Many Notes 580

A.2.4 Music of a Sequence 580

A.3 Exercises 583

B Introduction to Discrete Calculus 593

B.1 Discrete Functions 593

B.1.1 The Sine Function 594

B.1.2 Interpolation 596

B.1.3 Evaluating the Approximation 596

B.1.4 Generalization 597

B.2 Differentiation Becomes Finite Differences 599

B.2.1 Differentiating the Sine Function 600

B.2.2 Differences on a Mesh 600

B.2.3 Generalization 602

B.3 Integration Becomes Summation 603

B.3.1 Dividing into Subintervals 604

B.3.2 Integration on Subintervals 605

B.3.3 Adding the Subintervals 606

B.3.4 Generalization 607

Trang 21

B.4 Taylor Series 609

B.4.1 Approximating Functions Close to One Point 609

B.4.2 Approximating the Exponential Function 609

B.4.3 More Accurate Expansions 610

B.4.4 Accuracy of the Approximation 612

B.4.5 Derivatives Revisited 614

B.4.6 More Accurate Difference Approximations 615

B.4.7 Second-Order Derivatives 617

B.5 Exercises 619

C Introduction to Differential Equations 625

C.1 The Simplest Case 626

C.2 Exponential Growth 628

C.3 Logistic Growth 633

C.4 A Simple Pendulum 634

C.5 A Model for the Spread of a Disease 637

C.6 Exercises 639

D A Complete Differential Equation Project 641

D.1 About the Problem: Motion and Forces in Physics 641

D.1.1 The Physical Problem 641

D.1.2 The Computational Algorithm 644

D.1.3 Derivation of the Mathematical Model 644

D.1.4 Derivation of the Algorithm 646

D.2 Program Development and Testing 648

D.2.1 Implementation 648

D.2.2 Callback Functionality 651

D.2.3 Making a Module 652

D.2.4 Verification 653

D.3 Visualization 655

D.3.1 Simultaneous Computation and Plotting 655

D.3.2 Some Applications 658

D.3.3 Remark on Choosing Δt 658

D.3.4 Comparing Several Quantities in Subplots 659

D.3.5 Comparing Approximate and Exact Solutions 660

D.3.6 Evolution of the Error as Δt Decreases 661

D.4 Exercises 665

E Programming of Differential Equations 667

E.1 Scalar Ordinary Differential Equations 668

E.1.1 Examples on Right-Hand-Side Functions 668

E.1.2 The Forward Euler Scheme 670

E.1.3 Function Implementation 671

E.1.4 Verifying the Implementation 671

E.1.5 From Discrete to Continuous Solution 672

E.1.6 Switching Numerical Method 673

Trang 22

E.1.7 Class Implementation 674E.1.8 Example: Logistic Growth 677E.2 Systems of Ordinary Differential Equations 677E.2.1 Mathematical Problem 678E.2.2 Example of a System of ODEs 680E.2.3 From Scalar ODE Code to Systems 681E.2.4 Numerical Methods 684E.2.5 The ODE Solver Class Hierarchy 685E.2.6 The Backward Euler Method 688E.2.7 Application 1: u  =−u 691

E.2.8 Application 2: The Logistic Equation 693E.2.9 Application 3: An Oscillating System 700E.2.10 Application 4: The Trajectory of a Ball 702E.2.11 Further Developments of ODESolver 704E.3 Exercises 705

F Debugging 735

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

G Migrating Python to Compiled Code 753

G.1 Pure Python Code for Monte Carlo Simulation 754G.1.1 The Computational Problem 754G.1.2 A Scalar Python Implementation 754G.1.3 A Vectorized Python Implementation 755G.2 Migrating Scalar Python Code to Cython 757G.2.1 A Plain Cython Implementation 757G.2.2 A Better Cython Implementation 759G.3 Migrating Code to C 761G.3.1 Writing a C Program 761G.3.2 Migrating Loops to C Code via F2PY 762G.3.3 Migrating Loops to C Code via Cython 764G.3.4 Comparing Efficiency 765

H Technical Topics 767

H.1 Different Ways of Running Python Programs 767H.1.1 Executing Python Programs in IPython 767H.1.2 Executing Python Programs on Unix 767H.1.3 Executing Python Programs on Windows 769H.1.4 Executing Python Programs on Macintosh 771H.1.5 Making a Complete Stand-Alone Executable 771H.2 Integer and Float Division 771H.3 Visualizing a Program with Lumpy 772H.4 Doing Operating System Tasks in Python 774

Trang 23

H.5 Variable Number of Function Arguments 776

H.5.1 Variable Number of Positional Arguments 777

H.5.2 Variable Number of Keyword Arguments 779

H.6 Evaluating Program Efficiency 781

H.6.1 Making Time Measurements 781

H.6.2 Profiling Python Programs 783

References 785

Index 787

Trang 25

Exercise 1.1 Compute 1+1 43Exercise 1.2 Write a “Hello, World!” program 43Exercise 1.3 Derive and compute a formula 43Exercise 1.4 Convert from meters to British length units 43Exercise 1.5 Compute the mass of various substances 43Exercise 1.6 Compute the growth of money in a bank 43Exercise 1.7 Find error(s) in a program 44Exercise 1.8 Type in program text 44Exercise 1.9 Type in programs and debug them 44Exercise 1.10 Evaluate a Gaussian function 45Exercise 1.11 Compute the air resistance on a football 45Exercise 1.12 How to cook the perfect egg 46Exercise 1.13 Derive the trajectory of a ball 46Exercise 1.14 Find errors in the coding of formulas 47Exercise 1.15 Explain why a program does not work 47Exercise 1.16 Find errors in Python statements 47Exercise 1.17 Find errors in the coding of a formula 48Exercise 2.1 Make a Fahrenheit–Celsius conversion table 79Exercise 2.2 Write an approximate Fahrenheit–Celsius

conversion table 79Exercise 2.3 Generate odd numbers 79Exercise 2.4 Store odd numbers in a list 79Exercise 2.5 Generate odd numbers by a list comprehension 80Exercise 2.6 Make a table of values from a formula 80Exercise 2.7 Store values from a formula in lists 80Exercise 2.8 Work with a list 80Exercise 2.9 Simulate operations on lists by hand 80Exercise 2.10 Generate equally spaced coordinates 80Exercise 2.11 Use a list comprehension to solve Exer 2.10 81Exercise 2.12 Compute a mathematical sum 81

xxiii

Trang 26

Exercise 2.13 Use a for loop in Exer 2.12 81Exercise 2.14 Simulate a program by hand 81Exercise 2.15 Explore the Python Library Reference 82Exercise 2.16 Index a nested lists 82Exercise 2.17 Construct a double for loop over a nested list 82Exercise 2.18 Store data in lists in Exercise 2.2 82Exercise 2.19 Store data from Exer 2.7 in a nested list 82Exercise 2.20 Convert nested list comprehensions to nested

standard loops 83Exercise 2.21 Values of boolean expressions 83Exercise 2.22 Explore round-off errors from a large number of

inverse operations 83Exercise 2.23 Explore what zero can be on a computer 84Exercise 2.24 Compare two real numbers on a computer 84Exercise 2.25 Interpret a code 84Exercise 2.26 Explore problems with inaccurate indentation 85Exercise 2.27 Simulate nested loops by hand 85Exercise 2.28 Explore punctuation in Python programs 86Exercise 2.29 Investigate a for loop over a changing list 86Exercise 3.1 Write a Fahrenheit–Celsius conversion function 120Exercise 3.2 Write the program in Exer 2.12 as a function 120Exercise 3.3 Write a function for solvingax2+ bx + c = 0 120

Exercise 3.4 Implement the sum function 120Exercise 3.5 Integrate a function by one trapezoid 121Exercise 3.6 Integrate a function by two trapezoids 121Exercise 3.7 Derive the general Trapezoidal integration rule 121Exercise 3.8 Derive the general Midpoint integration rule 122Exercise 3.9 Compute the area of an arbitrary triangle 122Exercise 3.10 Compute the length of a path 123Exercise 3.11 Approximateπ 123

Exercise 3.12 Write various hello-world functions 123Exercise 3.13 Approximate a function by a sum of sines 124Exercise 3.14 Implement a Gaussian function 124Exercise 3.15 Make a function of the formula in Exer 1.12 124Exercise 3.16 Write a function for numerical differentiation 125Exercise 3.17 Make an adaptive Trapezoidal integration rule 125Exercise 3.18 Compute a polynomial via a product 126Exercise 3.19 Implement the factorial function 126Exercise 3.20 Compute velocity and acceleration from position

data; one dimension 126Exercise 3.21 Compute velocity and acceleration from position

data; two dimensions 127Exercise 3.22 Find the max and min values of a function 127Exercise 3.23 Find the max and min elements in a list 127Exercise 3.24 Implement the Heaviside function 128

Trang 27

Exercise 3.25 Implement a smoothed Heaviside function 128

Exercise 3.26 Implement an indicator function 128

Exercise 3.27 Implement a piecewise constant function 128

Exercise 3.28 Apply indicator functions 129

Exercise 3.29 Rewrite a mathematical function 129

Exercise 3.30 Make a table for approximations of cos x 130

Exercise 3.31 Use None in keyword arguments 130

Exercise 3.32 Write a sort function for a list of 4-tuples 131

Exercise 3.33 Find prime numbers 132

Exercise 3.34 Find pairs of characters 132

Exercise 3.35 Count substrings 132

Exercise 3.36 Explain why a program works 132

Exercise 3.37 Resolve a problem with a function 132

Exercise 3.38 Determine the types of some objects 133

Exercise 3.39 Explain the difference between if and elif 133

Exercise 3.40 Find an error in a program 134

Exercise 3.41 Find programming errors 134

Exercise 4.1 Make an interactive program 180

Exercise 4.2 Read from the command line in Exer 4.1 180

Exercise 4.3 Use exceptions in Exer 4.2 180

Exercise 4.4 Read input from the keyboard 180

Exercise 4.5 Read input from the command line 180

Exercise 4.6 Prompt the user for input to a formula 180

Exercise 4.7 Read command line input a formula 180

Exercise 4.8 Make the program from Exer 4.7 safer 181

Exercise 4.9 Test more in the program from Exer 4.7 181

Exercise 4.10 Raise an exception in Exer 4.9 181

Exercise 4.11 Compute the distance it takes to stop a car 181

Exercise 4.12 Look up calendar functionality 181

Exercise 4.13 Use the StringFunction tool 181

Exercise 4.14 Extend a program from Ch 4.2.1 181

Exercise 4.15 Why we test for specific exception types 182

Exercise 4.16 Make a simple module 182

Exercise 4.17 Make a useful main program for Exer 4.16 182

Exercise 4.18 Make a module in Exer 3.13 182

Exercise 4.19 Extend the module from Exer 4.18 183

Exercise 4.20 Use options and values in Exer 4.19 183

Exercise 4.21 Check if mathematical identities hold on a

computer 183Exercise 4.22 Improve input to the program in Exer 4.21 183

Exercise 4.23 Apply the program from Exer 4.22 184

Exercise 4.24 Compute the binomial distribution 184

Exercise 4.25 Apply the binomial distribution 185

Exercise 4.26 Compute probabilities with the Poisson

distribution 185

Trang 28

Exercise 5.1 Fill lists with function values 243Exercise 5.2 Fill arrays; loop version 244Exercise 5.3 Fill arrays; vectorized version 244Exercise 5.4 Plot a function 244Exercise 5.5 Apply a function to a vector 244Exercise 5.6 Simulate by hand a vectorized expression 245Exercise 5.7 Demonstrate array slicing 245Exercise 5.8 Replace list operations by array computing 245Exercise 5.9 Plot a formula 245Exercise 5.10 Plot a formula for several parameters 245

Exercise 5.11 Specify the x and y axes in Exer 5.10 245

Exercise 5.12 Plot exact and inexact Fahrenheit–Celsius

formulas 246Exercise 5.13 Plot the trajectory of a ball 246Exercise 5.14 Implement Lagrange’s interpolation formula 246Exercise 5.15 Plot the polynomial in Exer 5.14 247Exercise 5.16 Investigate the polynomial in Exer 5.14 247Exercise 5.17 Plot a wave packet 247Exercise 5.18 Judge a plot 248Exercise 5.19 Plot the viscosity of water 248Exercise 5.20 Explore a complicated function graphically 248

Exercise 5.21 Plot Taylor polynomial approximations to sin x 248

Exercise 5.22 Animate a wave packet 249Exercise 5.23 Animate a smoothed Heaviside function 249Exercise 5.24 Animate two-scale temperature variations 249Exercise 5.25 Improve the solution in Exer 5.24 250

Exercise 5.26 Animate a sequence of approximations to π 250

Exercise 5.27 Animate a planet’s orbit 250Exercise 5.28 Animate the evolution of Taylor polynomials 251Exercise 5.29 Plot the velocity profile for pipeflow 252Exercise 5.30 Plot the functions from Exer 3.13 252Exercise 5.31 Make a movie of the functions from Exer 3.13 252Exercise 5.32 Plot functions from the command line 253Exercise 5.33 Improve the program from Exercise 5.32 253Exercise 5.34 Demonstrate energy concepts from physics 253Exercise 5.35 Plot a w-like function 254Exercise 5.36 Plot a piecewise constant function 254Exercise 5.37 Vectorize a piecewise constant function 254Exercise 5.38 Visualize approximations in the Midpoint

integration rule 254Exercise 5.39 Visualize approximations in the Trapezoidal

integration rule 254Exercise 5.40 Experience overflow in a function 254Exercise 5.41 Experience less overflow in a function 255Exercise 5.42 Extend Exer 5.5 to a rank 2 array 256

Trang 29

Exercise 5.43 Explain why array computations fail 256

Exercise 6.1 Read a two-column data file 329

Exercise 6.2 Read a data file 329

Exercise 6.3 Simplify the implementation of Exer 6.1 329

Exercise 6.4 Fit a polynomial to data 329

Exercise 6.5 Read acceleration data and find velocities 330

Exercise 6.6 Read acceleration data and plot velocities 331

Exercise 6.7 Find velocity from GPS coordinates 331

Exercise 6.8 Make a dictionary from a table 331

Exercise 6.9 Explore syntax differences: lists vs dictionaries 332

Exercise 6.10 Improve the program from Ch 6.2.5 332

Exercise 6.11 Interpret output from a program 332

Exercise 6.12 Make a dictionary 333

Exercise 6.13 Make a nested dictionary 333

Exercise 6.14 Make a nested dictionary from a file 333

Exercise 6.15 Compute the area of a triangle 333

Exercise 6.16 Compare data structures for polynomials 333

Exercise 6.17 Compute the derivative of a polynomial 333

Exercise 6.18 Generalize the program from Ch 6.2.7 334

Exercise 6.19 Write function data to file 334

Exercise 6.20 Specify functions on the command line 334

Exercise 6.21 Interpret function specifications 335

Exercise 6.22 Compare average temperatures in cities 336

Exercise 6.23 Try Word or OpenOffice to write a program 336

Exercise 6.24 Evaluate objects in a boolean context 337

Exercise 6.25 Fit a polynomial to experimental data 337

Exercise 6.26 Generate an HTML report with figures 337

Exercise 6.27 Extract information from a weather page 338

Exercise 6.28 Compare alternative weather forecasts 338

Exercise 6.29 Improve the output in Exercise 6.28 338

Exercise 6.30 Allow different types for a function argument 338

Exercise 6.31 Make a function more robust 339

Exercise 6.32 Find proportion of bases inside/outside exons 339

Exercise 7.1 Make a function class 395

Exercise 7.2 Extend the class from Ch 7.2.1 395

Exercise 7.3 Make classes for a rectangle and a triangle 395

Exercise 7.4 Make a class for straight lines 395

Exercise 7.5 Improve the constructor in Exer 7.4 396

Exercise 7.6 Make a class for quadratic functions 396

Exercise 7.7 Make a class for linear springs 396

Exercise 7.8 Wrap functions in a class 397

Exercise 7.9 Extend the constructor in Exer 7.8 397

Exercise 7.10 Deduce a class implementation 398

Exercise 7.11 Use special methods in Exer 7.1 398

Exercise 7.12 Extend the class from Ch 7.2.1 398

Trang 30

Exercise 7.13 Implement a class for numerical differentiation 398Exercise 7.14 Verify a program 399Exercise 7.15 Test methods for numerical differentiation 399Exercise 7.16 Modify a class for numerical differentiation 400Exercise 7.17 Make a class for summation of series 400Exercise 7.18 Apply the differentiation class from Ch 7.3.2 401Exercise 7.19 Make a class for the Heaviside function 401Exercise 7.20 Add vectorization to the class in Exer 7.19 401Exercise 7.21 Equip the class in Exer 7.19 with plotting 401Exercise 7.22 Make a class for the indicator function 402Exercise 7.23 Make a class for piecewise constant functions 402Exercise 7.24 Extend the class in Exer 7.23 with plot

functionality 402Exercise 7.25 Make a module for piecewise constant functions 402Exercise 7.26 Use classes for computing inverse functions 403Exercise 7.27 Vectorize a class for numerical integration 403Exercise 7.28 Speed up repeated integral calculations 404Exercise 7.29 Apply a polynomial class 404Exercise 7.30 Find a bug in a class for polynomials 404Exercise 7.31 Implement subtraction of polynomials 405Exercise 7.32 Represent a polynomial by a NumPy array 405Exercise 7.33 Vectorize a class for polynomials 405Exercise 7.34 Use a dict to hold polynomial coefficients; add 405Exercise 7.35 Use a dict to hold polynomial coefficients; mul 406Exercise 7.36 Extend class Vec2D to work with lists/tuples 406Exercise 7.37 Extend class Vec2D to 3D vectors 406Exercise 7.38 Use NumPy arrays in class Vec2D 406Exercise 7.39 Use classes in the program from Ch 6.7.2 407Exercise 7.40 Use a class in Exer A.25 407Exercise 7.41 Apply the class from Exer 7.40 interactively 408Exercise 7.42 Find local and global extrema of a function 408Exercise 7.43 Improve the accuracy in Exer 7.42 410Exercise 7.44 Find the optimal production for a company 410Exercise 7.45 Extend the program from Exer 7.44 412Exercise 8.1 Flip a coin N times 466

Exercise 8.2 Compute a probability 466Exercise 8.3 Choose random colors 466Exercise 8.4 Draw balls from a hat 466Exercise 8.5 Computing probabilities of rolling dice 466Exercise 8.6 Estimate the probability in a dice game 466Exercise 8.7 Compute the probability of hands of cards 467Exercise 8.8 Decide if a dice game is fair 467Exercise 8.9 Adjust the game in Exer 8.8 467Exercise 8.10 Generalize the game from Chap 8.3.2 467Exercise 8.11 Compare two playing strategies 467

Trang 31

Exercise 8.12 Solve Exercise 8.11 with different no of dice 468

Exercise 8.13 Extend Exercise 8.12 468

Exercise 8.14 Investigate the winning chances of some games 468

Exercise 8.15 Compute probabilities of throwing two dice 468

Exercise 8.16 Play with vectorized boolean expressions 468

Exercise 8.17 Vectorize the program from Exer 8.1 469

Exercise 8.18 Vectorize the code in Exer 8.2 469

Exercise 8.19 Throw dice and compute a small probability 469

Exercise 8.20 Difference equation for random numbers 469

Exercise 8.21 Make a class for drawing balls from a hat 470

Exercise 8.22 Independent vs dependent random numbers 470

Exercise 8.23 Compute the probability of flipping a coin 470

Exercise 8.24 Extend Exer 8.23 471

Exercise 8.25 Simulate the problems in Exer 4.25 471

Exercise 8.26 Simulate a poker game 471

Exercise 8.27 Write a non-vectorized version of a code 471

Exercise 8.28 Estimate growth in a simulation model 472

Exercise 8.29 Investigate guessing strategies for Ch 8.4.1 472

Exercise 8.30 Make a vectorized solution to Exer 8.8 472

Exercise 8.31 Compute π by a Monte Carlo method 472

Exercise 8.32 Implement a variant of Exer 8.31 473

Exercise 8.33 Compute π by a random sum 473

Exercise 8.34 1D random walk with drift 473

Exercise 8.35 1D random walk until a point is hit 473

Exercise 8.36 Simulate making a fortune from gaming 473

Exercise 8.37 Make a class for 2D random walk 474

Exercise 8.38 Vectorize the class code from Exer 8.37 474

Exercise 8.39 2D random walk with walls; scalar version 475

Exercise 8.40 2D random walk with walls; vectorized version 475

Exercise 8.41 Simulate the mixture of gas molecules 475

Exercise 8.42 Solve a variant of Exer 8.41 475

Exercise 8.43 Guess beer brands 476

Exercise 8.44 Simulate stock prices 476

Exercise 8.45 Compute with option prices in finance 476

Exercise 8.46 Compute velocity and acceleration 478

Exercise 8.47 Differentiate noisy signals 478

Exercise 8.48 Model the noise in the data in Exer 8.47 479

Exercise 8.49 Reduce the noise in Exer 8.47 480

Exercise 8.50 Make a class for differentiating noisy data 480

Exercise 8.51 Speed up Markov chain mutation 481

Exercise 9.1 Demonstrate the magic of inheritance 548

Exercise 9.2 Inherit from classes in Ch 9.1 549

Exercise 9.3 Inherit more from classes in Ch 9.1 549

Exercise 9.4 Reverse the class hierarchy from Ch 9.1 549

Exercise 9.5 Make circle a subclass of an ellipse 549

Trang 32

Exercise 9.6 Make super- and subclass for a point 550Exercise 9.7 Modify a function class by subclassing 550Exercise 9.8 Explore the accuracy of difference formulas 550Exercise 9.9 Implement a subclass 551Exercise 9.10 Make classes for numerical differentiation 551Exercise 9.11 Implement a new subclass for differentiation 551Exercise 9.12 Understand if a class can be used recursively 551Exercise 9.13 Represent people by a class hierarchy 551Exercise 9.14 Add a new class in a class hierarchy 553Exercise 9.15 Change the user interface of a class hierarchy 553Exercise 9.16 Compute convergence rates of numerical

integration methods 553Exercise 9.17 Add common functionality in a class hierarchy 554Exercise 9.18 Make a class hierarchy for root finding 554Exercise 9.19 Make a calculus calculator class 555Exercise 9.20 Extend Exer 9.19 556Exercise 9.21 Make line drawing of a person; program 556Exercise 9.22 Make line drawing of a person; class 556Exercise 9.23 Animate a person with waving hands 556Exercise A.1 Determine the limit of a sequence 583Exercise A.2 Determine the limit of a sequence 583Exercise A.3 Experience convergence problems 583Exercise A.4 Compute π via sequences 584

Exercise A.5 Reduce memory usage of difference equations 584Exercise A.6 Compute the development of a loan 584Exercise A.7 Solve a system of difference equations 584Exercise A.8 Extend the model (A.32)–(A.33) 584Exercise A.9 Experiment with the program from Exer A.8 585Exercise A.10 Change index in a difference equation 585Exercise A.11 Construct time points from dates 585Exercise A.12 Solve nonlinear equations by Newton’s method 586Exercise A.13 Visualize the convergence of Newton’s method 586Exercise A.14 Implement the Secant method 587Exercise A.15 Test different methods for root finding 587Exercise A.16 Make difference equations for the Midpoint rule 587Exercise A.17 Compute the arc length of a curve 587

Exercise A.18 Find difference equations for computing sin x 588 Exercise A.19 Find difference equations for computing cos x 588

Exercise A.20 Make a guitar-like sound 588Exercise A.21 Damp the bass in a sound file 589Exercise A.22 Damp the treble in a sound file 589Exercise A.23 Demonstrate oscillatory solutions of (A.13) 590Exercise A.24 Improve the program from Exer A.23 590Exercise A.25 Generate an HTML report 591Exercise A.26 Simulate the price of wheat 591

Trang 33

Exercise B.1 Interpolate a discrete function 619

Exercise B.2 Study a function for different parameter values 619

Exercise B.3 Study a function and its derivative 620

Exercise B.4 Use the Trapezoidal method 620

Exercise B.5 Compute a sequence of integrals 621

Exercise B.6 Use the Trapezoidal method 621

Exercise B.7 Compute trigonometric integrals 622

Exercise B.8 Plot functions and their derivatives 623

Exercise B.9 Use the Trapezoidal method 623

Exercise C.1 Solve a nonhomogeneous linear ODE 639

Exercise C.2 Solve a nonlinear ODE 639

Exercise C.3 Solve an ODE for y(x) 639

Exercise C.4 Experience instability of an ODE 640

Exercise C.5 Solve an ODE with time-varying growth 640

Exercise D.1 Use a w function with a step 665

Exercise D.2 Make a callback function in Exercise D.1 665

Exercise D.3 Improve input to the simulation program 665

Exercise E.1 Solve a simple ODE in two ways 705

Exercise E.2 Use the ODESolver hierarchy to solve a simple

ODE 705Exercise E.3 Solve an ODE for emptying a tank 705

Exercise E.4 Scale the logistic equation 706

Exercise E.5 Compute logistic growth with time-varying

carrying capacity 707Exercise E.6 Solve an ODE for the arc length 707

Exercise E.7 Compute inverse functions by solving an ODE 707

Exercise E.8 Generalize the implementation in Exer E.7 708

Exercise E.9 Extend the implementation in Exer E.8 708

Exercise E.10 Compute inverse functions by interpolation 709

Exercise E.11 Simulate a falling or rising body in a fluid 709

Exercise E.12 Check the solution’s limit in Exer E.11 711

Exercise E.13 Visualize the different forces in Exer E.11 711

Exercise E.14 Solve an ODE until constant solution 711

Exercise E.15 Use classes in Exer E.14 712

Exercise E.16 Scale away parameters in Exer E.14 712

Exercise E.17 Use the 4th-order Runge-Kutta on (C.34) 713

Exercise E.18 Compare ODE methods 713

Exercise E.19 Compare ODE methods 713

Exercise E.20 Solve two coupled ODEs for radioactive decay 714

Exercise E.21 Code a 2nd-order Runge-Kutta method; function 714

Exercise E.22 Code a 2nd-order Runge-Kutta method; class 714

Exercise E.23 Make a subclass for Heun’s method 714

Exercise E.24 Make a subclass for the Midpoint method 715

Exercise E.25 Make a subclass for an Adams-Bashforth method 715

Exercise E.26 Implement the iterated Midpoint method; function 715

Trang 34

Exercise E.27 Implement the iterated Midpoint method; class 715Exercise E.28 Make a subclass for the iterated Midpoint method 716Exercise E.29 Study convergence of numerical methods for ODEs 716Exercise E.30 Solve an ODE specified on the command line 716Exercise E.31 Find the body’s position in Exer E.11 717Exercise E.32 Add the effect of air resistance on a ball 717Exercise E.33 Solve an ODE system for an electric circuit 718Exercise E.34 Compare methods for solving (E.76)–(E.77) 718Exercise E.35 Simulate the spreading of a disease 718Exercise E.36 Make a more flexible code in Exer E.35 720Exercise E.37 Introduce vaccination in Exer E.35 721Exercise E.38 Introduce a vaccination campaign in Exer E.37 721Exercise E.39 Find optimal vaccination period in Exer E.38 721Exercise E.40 Simulate human–zombie interaction 722Exercise E.41 Simulate an entire zombie movie 724Exercise E.42 Simulate a war on zombies 725Exercise E.43 Explore predator-prey population interactions 725Exercise E.44 Formulate a 2nd-order ODE as a system 726Exercise E.45 Solve the system in Exer E.44 in a special case 727Exercise E.46 Make a tool for analyzing oscillatory solutions 728Exercise E.47 Enhance the code from Exer E.45 729Exercise E.48 Allow flexible choice of functions in Exer E.47 732Exercise E.49 Use the modules from Exer E.47 and E.48 733Exercise E.50 Model the economy of fishing 734

Trang 35

Our first examples on computer programming involve programs that

evaluate mathematical formulas You will learn how to write and run

a Python program, how to work with variables, how to compute with

mathematical functions such as e x and sin x, and how to use Python

for interactive calculations

We assume that you are somewhat familiar with computers so that

you know what files and folders1 are, how you move between folders,

how you change file and folder names, and how you write text and save

it in a file

All the program examples associated with this chapter can be found

as files in the folder src/formulas We refer to the preface for how to

download the folder tree src containing all the program files for this

book

1.1 The First Programming Encounter: A Formula

The first formula we shall consider concerns the vertical motion of a

ball thrown up in the air From Newton’s second law of motion one can

set up a mathematical model for the motion of the ball and find that

the vertical position of the ball, called y, varies with time t according

to the following formula2:

y(t) = v0t −1

2gt

1 Another frequent word for folder is directory.

2 This formula neglects air resistance, which is usually small unless v0 is large – see

Exer-cise 1.11

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

Texts in Computational Science and Engineering 6,

DOI 10.1007/978-3-642-30293-0 1 , c Springer-Verlag Berlin Heidelberg 2012

1

Trang 36

Here, v0is the initial velocity of the ball, g is the acceleration of gravity, and t is time Observe that the y axis is chosen such that the ball starts

at y = 0 when t = 0.

To get an overview of the time it takes for the ball to move upwards

and return to y = 0 again, we can look for solutions to the equation

That is, the ball returns after 2v0/g seconds, and it is therefore

reason-able to restrict the interest of (1.1) to t ∈ [0, 2v0/g].

1.1.1 Using a Program as a Calculator

Our first program will evaluate (1.1) for a specific choice of v0, g, and

t Choosing v0 = 5 m/s and g = 9.81 m/s2 makes the ball come back

after t = 2v0/g ≈ 1 s This means that we are basically interested in

the time interval [0, 1] Say we want to compute the height of the ball

at time t = 0.6 s From (1.1) we have

y = 5 · 0.6 −1

2· 9.81 · 0.62

This arithmetic expression can be evaluated and its value can be printed

by a very simple one-line Python program:

print 5*0.6 - 0.5*9.81*0.6**2The four standard arithmetic operators are written as +, -, *, and /

in Python and most other computer languages The exponentiation

employs a double asterisk notation in Python, e.g., 0.62 is written as

0.6**2.Our task now is to create the program and run it, and this will bedescribed next

1.1.2 About Programs and Programming

A computer program is just a sequence of instructions to the computer,written in a computer language Most computer languages look some-what similar to English, but they are very much simpler The number

of words and associated instructions is very limited, so to perform acomplicated operation we must combine a large number of differenttypes of instructions The program text, containing the sequence of in-structions, is stored in one or more files The computer can only doexactly what the program tells the computer to do

Trang 37

Another perception of the word “program” is a file that can be run

(“double-clicked”) to perform a task Sometimes this is a file with

tex-tual instructions (which is the case with Python), and sometimes this

file is a translation of all the program text to a more efficient and

computer-friendly language that is quite difficult to read for a human

All the programs in this chapter consist of short text stored in a single

file Other programs that you have used frequently, for instance

Fire-fox or Internet Explorer for reading web pages, consist of program text

distributed over a large number of files, written by a large number of

people over many years One single file contains the machine-efficient

translation of the whole program, and this is normally the file that

you “double-click” on when starting the program In general, the word

“program” means either this single file or the collection of files with

textual instructions

Programming is obviously about writing programs, but this process

is more than writing the correct instructions in a file First, we must

understand how a problem can be solved by giving a sequence of

in-structions to the computer This is usually the most difficult thing

with programming Second, we must express this sequence of

instruc-tions correctly in a computer language and store the corresponding

text in a file (the program) Third, we must run the program, check

the validity of the results, and usually enter a fourth phase where errors

in the program must be found and corrected Mastering this process

requires a lot of training, which implies making a large number of

pro-grams (exercises in this book, for instance) and getting the propro-grams

to work

1.1.3 Tools for Writing Programs

Since programs consist of plain text, we need to write this text with the

help of another program that can store the text in a file You have most

likely extensive experience with writing text on a computer, but for

writing your own programs you need special programs, called editors,

which preserve exactly the characters you type The widespread word

processors, Microsoft Word being a primary example3, are aimed at

producing nice-looking reports These programs format the text and

are not good tools for writing your own programs, even though they can

save the document in a pure text format Spaces are often important in

Python programs, and editors for plain text give you complete control

of the spaces and all other characters in the program file

3 Other examples are OpenOffice, TextEdit, iWork Pages, and BBEdit Chapter 6.1.3 gives

some insight into why such programs are not suitable for writing your own Python programs.

Trang 38

Emacs, XEmacs, Vim, and Gedit are popular editors for writing grams on Linux or Unix systems, including Mac4 computers On Win-dows we recommend Notepad++ or the Window versions of Emacs,Vim, or Gedit None of these programs are part of a standard Win-dows installation.

pro-A special editor for Python programs comes with the Python ware This editor is called Idle and is usually installed under the name

reachable from the Python entry in the Start menu Idle has a gentlelearning curve, but is mainly restricted to writing Python programs.Completely general editors, such as Emacs and Vim, have a steeperlearning curve and can be used for any text files, including reports instudent projects

More advanced development environments also exist for Python Fornumerical programming the Spyder software is of particular interest as

it provides a graphical environment much like that of Matlab

1.1.4 Using Idle to Write the Program

Let us explain in detail how we can use Idle to write our one-lineprogram from Chapter1.1.1 Idle may not become your favorite editorfor writing Python programs, yet we recommend to follow the stepsbelow to get in touch with Idle and try it out You can simply replacethe Idle instructions by similar actions in your favorite editor, Emacsfor instance

First, create a folder where your Python programs can be located.Here we choose a folder namemytestunder your home folder To write

and run Python programs, you will need a terminal window on ux/Unix or Mac, sometimes called a console window, or an MS-DOS

Lin-window or command prompt on Windows Launch such a Lin-window and

use the cd (change directory) command to move to themytest folder

If you have not made the folder with a graphical file & folder manageryou must create the folder by the commandmkdir mytest(mkdirstandsfor make directory)

The next step is to start Idle This can be done by writing idle&

(Linux) or start idle (Windows) in the terminal window tively, you can launch Idle from the Start menu on Windows Fig-ure 1.1 displays a terminal window where we create the folder, move

Alterna-to the folder, and start Idle5

4 On Mac, you may want to download a more “Mac-like” editor such as the Really Simple Text program.

5 The ampersand after idle is Linux specific On Windows you have to write start idle instead The ampersand postfix or the start prefix makes it possible to continue with other commands in the terminal window while the program, here Idle, is running This is important for program testing where we often do a lot of edit-and-run cycles, which means that we frequently switch between the editor and the terminal window.

Trang 39

Fig 1.1 A terminal window on a Linux/Unix/Mac machine where we create a folder

( mkdir ), move to the folder ( cd ), and start Idle.

If a window now appears on the screen, with “Python Shell” in the

title bar of the window, go to its File menu and choose New Window

The window that now pops up is the Idle editor (having the window

name “Untitled”) Move the cursor inside this window and write the

line

print 5*0.6 - 0.5*9.81*0.6**2

followed by pressing the Return key The Idle window looks as in

Fig-ure1.2

Fig 1.2 An Idle editor window containing our first one-line program.

Your program is now in the Idle editor, but before you can run it,

the program text must be saved in a file Choose File and then Save

As As usual, such a command launches a new window where you can

fill in the name of the file where the program is to be stored And

as always, you must first check that you are in the right folder, or

directory which is Idle’s word for the same thing The upper line in

the file dialog window contains the folder name Clicking on the bar to

the right (after the directory/folder name), gives a possibility to move

upwards in the folder hierarchy, and clicking on the folder icon to the

right of the bar, moves just one folder upwards To go down in the

Trang 40

folder tree, you simply double-click a folder icon in the main window

of this dialog You must now navigate to themytestfolder under yourhome folder If you started Idle from the terminal window, there is

no need to navigate and change folder Simply fill in the name of theprogram Any name will do, but we suggest that you choose the name

this book The file extension.pyis common for Python programs, butnot strictly required6

Press the Save button and move back to the terminal window Makesure you have a new file ball1.py here, by running the command ls

(on Linux/Unix and Mac) ordir(on Windows) The output should be

a text containing the name of the program file You can now jump tothe paragraph “How to Run the Program”, but it might be a good idea

to read the warning below first

Warning About Typing Program Text Even though a program is just

a text, there is one major difference between a text in a program and

a text intended to be read by a human When a human reads a text,she or he is able to understand the message of the text even if the text

is not perfectly precise or if there are grammar errors If our one-lineprogram was expressed as

write 5*0.6 - 0.5*9.81*0.6^2most humans would interpretwrite and print as the same thing, andmany would also interpret6^2as 62 In the Python language, however,

from the exponentiation 6**2 Our communication with a computerthrough a program must be perfectly precise without a single grammarerror7 The computer will only do exactly what we tell it to do Anyerror in the program, however small, may affect the program There is

a chance that we will never notice it, but most often an error causesthe program to stop or produce wrong results The conclusion is thatcomputers have a much more pedantic attitude to language than what(most) humans have

Now you understand why any program text must be carefully typed,paying attention to the correctness of every character If you try outprogram texts from this book, make sure that you type them inexactly

as you see them in the book Blanks, for instance, are often

impor-tant in Python, so it is a good habit to always count them and typethem in correctly Any attempt not to follow this advice will cause youfrustrations, sweat, and maybe even tears

6 Some editors, like Emacs, have many features that make it easier to write Python grams, but these features will not be automatically available unless the program file has a py extension.

pro-7 “Programming demands significantly higher standard of accuracy Things don’t simply have to make sense to another human being, they must make sense to a computer.” – Donald Knuth [4, p 18], computer scientist, 1938–.

Ngày đăng: 22/03/2014, 17:20

TỪ KHÓA LIÊN QUAN