|
1 Computing with Formulas |
|
|
1 | (50) |
|
1.1 The First Programming Encounter: a Formula |
|
|
1 | (11) |
|
1.1.1 Using a Program as a Calculator |
|
|
2 | (1) |
|
1.1.2 About Programs and Programming |
|
|
2 | (1) |
|
1.1.3 Tools for Writing Programs |
|
|
3 | (1) |
|
1.1.4 Writing and Running Your First Python Program |
|
|
4 | (1) |
|
1.1.5 Warning About Typing Program Text |
|
|
5 | (1) |
|
1.1.6 Verifying the Result |
|
|
6 | (1) |
|
|
6 | (1) |
|
|
6 | (1) |
|
1.1.9 Reserved Words in Python |
|
|
7 | (1) |
|
|
8 | (1) |
|
1.1.11 Formatting Text and Numbers |
|
|
9 | (3) |
|
1.2 Computer Science Glossary |
|
|
12 | (4) |
|
1.3 Another Formula: Celsius-Fahrenheit Conversion |
|
|
16 | (4) |
|
1.3.1 Potential Error: Integer Division |
|
|
16 | (1) |
|
|
17 | (1) |
|
1.3.3 Avoiding Integer Division |
|
|
18 | (2) |
|
1.3.4 Arithmetic Operators and Precedence |
|
|
20 | (1) |
|
1.4 Evaluating Standard Mathematical Functions |
|
|
20 | (4) |
|
1.4.1 Example: Using the Square Root Function |
|
|
20 | (3) |
|
1.4.2 Example: Computing with sinh x |
|
|
23 | (1) |
|
1.4.3 A First Glimpse of Rounding Errors |
|
|
23 | (1) |
|
1.5 Interactive Computing |
|
|
24 | (5) |
|
1.5.1 Using the Python Shell |
|
|
25 | (1) |
|
|
26 | (1) |
|
|
27 | (2) |
|
|
29 | (4) |
|
1.6.1 Complex Arithmetics in Python |
|
|
30 | (1) |
|
1.6.2 Complex Functions in Python |
|
|
31 | (1) |
|
1.6.3 Unified Treatment of Complex and Real Functions |
|
|
31 | (2) |
|
|
33 | (2) |
|
1.7.1 Basic Differentiation and Integration |
|
|
33 | (1) |
|
|
34 | (1) |
|
1.7.3 Taylor Series and More |
|
|
35 | (1) |
|
|
35 | (6) |
|
|
35 | (4) |
|
1.8.2 Example: Trajectory of a Ball |
|
|
39 | (1) |
|
1.8.3 About Typesetting Conventions in This Book |
|
|
40 | (1) |
|
|
41 | (10) |
|
|
51 | (40) |
|
|
51 | (6) |
|
|
51 | (1) |
|
|
52 | (2) |
|
2.1.3 Boolean Expressions |
|
|
54 | (2) |
|
2.1.4 Loop Implementation of a Sum |
|
|
56 | (1) |
|
|
57 | (5) |
|
2.2.1 Basic List Operations |
|
|
57 | (3) |
|
|
60 | (2) |
|
2.3 Alternative Implementations with Lists and Loops |
|
|
62 | (5) |
|
2.3.1 While Loop Implementation of a for Loop |
|
|
62 | (1) |
|
2.3.2 The Range Construction |
|
|
63 | (1) |
|
2.3.3 For Loops with List Indices |
|
|
64 | (1) |
|
2.3.4 Changing List Elements |
|
|
65 | (1) |
|
|
66 | (1) |
|
2.3.6 Traversing Multiple Lists Simultaneously |
|
|
66 | (1) |
|
|
67 | (7) |
|
2.4.1 A table as a List of Rows or Columns |
|
|
67 | (1) |
|
|
68 | (2) |
|
2.4.3 Extracting Sublists |
|
|
70 | (2) |
|
2.4.4 Traversing Nested Lists |
|
|
72 | (2) |
|
|
74 | (1) |
|
|
75 | (7) |
|
|
75 | (3) |
|
2.6.2 Example: Analyzing List Data |
|
|
78 | (2) |
|
2.6.3 How to Find More Python Information |
|
|
80 | (2) |
|
|
82 | (9) |
|
3 Functions and Branching |
|
|
91 | (58) |
|
|
91 | (19) |
|
3.1.1 Mathematical Functions as Python Functions |
|
|
91 | (2) |
|
3.1.2 Understanding the Program Flow |
|
|
93 | (1) |
|
3.1.3 Local and Global Variables |
|
|
94 | (2) |
|
|
96 | (1) |
|
3.1.5 Function Argument or Global Variable? |
|
|
97 | (1) |
|
3.1.6 Beyond Mathematical Functions |
|
|
98 | (1) |
|
3.1.7 Multiple Return Values |
|
|
99 | (1) |
|
|
100 | (1) |
|
3.1.9 Functions with No Return Values |
|
|
101 | (2) |
|
|
103 | (2) |
|
|
105 | (2) |
|
3.1.12 Functions as Arguments to Functions |
|
|
107 | (2) |
|
|
109 | (1) |
|
|
110 | (1) |
|
|
110 | (3) |
|
|
111 | (2) |
|
|
113 | (1) |
|
3.3 Mixing Loops, Branching, and Functions in Bioinformatics Examples |
|
|
113 | (8) |
|
3.3.1 Counting Letters in DNA Strings |
|
|
114 | (4) |
|
3.3.2 Efficiency Assessment |
|
|
118 | (2) |
|
3.3.3 Verifying the Implementations |
|
|
120 | (1) |
|
|
121 | (6) |
|
|
121 | (2) |
|
3.4.2 Example: Numerical Integration |
|
|
123 | (4) |
|
|
127 | (22) |
|
4 User Input and Error Handling |
|
|
149 | (78) |
|
4.1 Asking Questions and Reading Answers |
|
|
150 | (1) |
|
4.1.1 Reading Keyboard Input |
|
|
150 | (1) |
|
4.2 Reading from the Command Line |
|
|
151 | (3) |
|
4.2.1 Providing Input on the Command Line |
|
|
151 | (1) |
|
4.2.2 A Variable Number of Command-Line Arguments |
|
|
152 | (1) |
|
4.2.3 More on Command-Line Arguments |
|
|
153 | (1) |
|
4.3 Turning User Text into Live Objects |
|
|
154 | (7) |
|
4.3.1 The Magic Eval Function |
|
|
154 | (4) |
|
4.3.2 The Magic Exec Function |
|
|
158 | (2) |
|
4.3.3 Turning String Expressions into Functions |
|
|
160 | (1) |
|
4.4 Option-Value Pairs on the Command Line |
|
|
161 | (4) |
|
4.4.1 Basic Usage of the Argparse Module |
|
|
162 | (1) |
|
4.4.2 Mathematical Expressions as Values |
|
|
163 | (2) |
|
4.5 Reading Data from File |
|
|
165 | (6) |
|
4.5.1 Reading a File Line by Line |
|
|
166 | (1) |
|
4.5.2 Alternative Ways of Reading a File |
|
|
167 | (2) |
|
4.5.3 Reading a Mixture of Text and Numbers |
|
|
169 | (2) |
|
|
171 | (8) |
|
4.6.1 Example: Writing a Table to File |
|
|
171 | (2) |
|
4.6.2 Standard Input and Output as File Objects |
|
|
173 | (3) |
|
4.6.3 What is a File, Really? |
|
|
176 | (3) |
|
|
179 | (6) |
|
|
180 | (3) |
|
|
183 | (2) |
|
4.8 A Glimpse of Graphical User Interfaces |
|
|
185 | (3) |
|
|
188 | (13) |
|
4.9.1 Example: Interest on Bank Deposits |
|
|
188 | (1) |
|
4.9.2 Collecting Functions in a Module File |
|
|
189 | (1) |
|
|
190 | (2) |
|
4.9.4 Verification of the Module Code |
|
|
192 | (1) |
|
|
193 | (2) |
|
4.9.6 Doc Strings in Modules |
|
|
195 | (1) |
|
|
196 | (3) |
|
4.9.8 Distributing Modules |
|
|
199 | (1) |
|
4.9.9 Making Software Available on the Internet |
|
|
200 | (1) |
|
4.10 Making Code for Python 2 and 3 |
|
|
201 | (3) |
|
4.10.1 Basic Differences Between Python 2 and 3 |
|
|
201 | (1) |
|
4.10.2 Turning Python 2 Code into Python 3 Code |
|
|
202 | (2) |
|
|
204 | (12) |
|
|
204 | (4) |
|
4.11.2 Example: Bisection Root Finding |
|
|
208 | (8) |
|
|
216 | (11) |
|
5 Array Computing and Curve Plotting |
|
|
227 | (106) |
|
|
228 | (4) |
|
|
228 | (1) |
|
5.1.2 Mathematical Operations on Vectors |
|
|
229 | (2) |
|
5.1.3 Vector Arithmetics and Vector Functions |
|
|
231 | (1) |
|
5.2 Arrays in Python Programs |
|
|
232 | (6) |
|
5.2.1 Using Lists for Collecting Function Data |
|
|
232 | (1) |
|
5.2.2 Basics of Numerical Python Arrays |
|
|
233 | (2) |
|
5.2.3 Computing Coordinates and Function Values |
|
|
235 | (1) |
|
|
236 | (2) |
|
|
238 | (18) |
|
5.3.1 MATLAB-Style Plotting with Matplotlib |
|
|
238 | (5) |
|
5.3.2 Matplotlib; Pyplot Prefix |
|
|
243 | (1) |
|
5.3.3 SciTools and Easyviz |
|
|
244 | (5) |
|
|
249 | (5) |
|
|
254 | (1) |
|
5.3.6 Curve Plots in Pure Text |
|
|
255 | (1) |
|
5.4 Plotting Difficulties |
|
|
256 | (4) |
|
5.4.1 Piecewisely Defined Functions |
|
|
256 | (3) |
|
5.4.2 Rapidly Varying Functions |
|
|
259 | (1) |
|
5.5 More Advanced Vectorization of Functions |
|
|
260 | (7) |
|
5.5.1 Vectorization of StringFunction Objects |
|
|
260 | (1) |
|
5.5.2 Vectorization of the Heaviside Function |
|
|
261 | (4) |
|
5.5.3 Vectorization of a Hat Function |
|
|
265 | (2) |
|
5.6 More on Numerical Python Arrays |
|
|
267 | (5) |
|
|
267 | (1) |
|
5.6.2 In-Place Arithmetics |
|
|
268 | (1) |
|
|
269 | (1) |
|
5.6.4 Generalized Indexing |
|
|
269 | (1) |
|
5.6.5 Testing for the Array Type |
|
|
270 | (1) |
|
5.6.6 Compact Syntax for Array Generation |
|
|
271 | (1) |
|
|
271 | (1) |
|
5.7 High-Performance Computing with Arrays |
|
|
272 | (5) |
|
5.7.1 Scalar Implementation |
|
|
272 | (1) |
|
5.7.2 Vectorized Implementation |
|
|
273 | (1) |
|
5.7.3 Memory-Saving Implementation |
|
|
273 | (2) |
|
5.7.4 Analysis of Memory Usage |
|
|
275 | (1) |
|
5.7.5 Analysis of the CPU Time |
|
|
276 | (1) |
|
5.8 Higher-Dimensional Arrays |
|
|
277 | (6) |
|
5.8.1 Matrices and Arrays |
|
|
277 | (1) |
|
5.8.2 Two-Dimensional Numerical Python Arrays |
|
|
278 | (3) |
|
|
281 | (1) |
|
|
282 | (1) |
|
5.9 Some Common Linear Algebra Operations |
|
|
283 | (9) |
|
5.9.1 Inverse, Determinant, and Eigenvalues |
|
|
283 | (1) |
|
|
283 | (1) |
|
|
284 | (1) |
|
5.9.4 Sum and Extreme Values |
|
|
284 | (2) |
|
|
286 | (1) |
|
5.9.6 Transpose and Upper/Lower Triangular Parts |
|
|
286 | (1) |
|
5.9.7 Solving Linear Systems |
|
|
287 | (1) |
|
5.9.8 Matrix Row and Column Operations |
|
|
287 | (1) |
|
5.9.9 Computing the Rank of a Matrix |
|
|
288 | (1) |
|
5.9.10 Symbolic Linear Algebra |
|
|
289 | (3) |
|
5.10 Plotting of Scalar and Vector Fields |
|
|
292 | (4) |
|
|
292 | (1) |
|
|
293 | (1) |
|
5.10.3 Parameterized Curve |
|
|
293 | (1) |
|
|
294 | (1) |
|
5.10.5 The Gradient Vector Field |
|
|
294 | (2) |
|
|
296 | (3) |
|
|
296 | (1) |
|
|
297 | (2) |
|
5.11.3 Vector Field Plots |
|
|
299 | (1) |
|
|
299 | (8) |
|
|
300 | (3) |
|
|
303 | (1) |
|
5.12.3 Vector Field Plots |
|
|
303 | (1) |
|
5.12.4 A 3D Scalar Field and Its Gradient Field |
|
|
304 | (2) |
|
|
306 | (1) |
|
|
307 | (6) |
|
|
307 | (1) |
|
5.13.2 Example: Animating a Function |
|
|
308 | (5) |
|
|
313 | (20) |
|
6 Dictionaries and Strings |
|
|
333 | (76) |
|
|
333 | (18) |
|
6.1.1 Making Dictionaries |
|
|
334 | (1) |
|
6.1.2 Dictionary Operations |
|
|
334 | (2) |
|
6.1.3 Example: Polynomials as Dictionaries |
|
|
336 | (2) |
|
6.1.4 Dictionaries with Default Values and Ordering |
|
|
338 | (3) |
|
6.1.5 Example: Storing File Data in Dictionaries |
|
|
341 | (1) |
|
6.1.6 Example: Storing File Data in Nested Dictionaries |
|
|
342 | (5) |
|
6.1.7 Example: Reading and Plotting Data Recorded at Specific Dates |
|
|
347 | (4) |
|
|
351 | (9) |
|
6.2.1 Common Operations on Strings |
|
|
351 | (4) |
|
6.2.2 Example: Reading Pairs of Numbers |
|
|
355 | (3) |
|
6.2.3 Example: Reading Coordinates |
|
|
358 | (2) |
|
6.3 Reading Data from Web Pages |
|
|
360 | (9) |
|
|
361 | (1) |
|
6.3.2 How to Access Web Pages in Programs |
|
|
362 | (1) |
|
6.3.3 Example: Reading Pure Text Files |
|
|
363 | (2) |
|
6.3.4 Example: Extracting Data from HTML |
|
|
365 | (1) |
|
6.3.5 Handling Non-English Text |
|
|
366 | (3) |
|
6.4 Reading and Writing Spreadsheet Files |
|
|
369 | (6) |
|
|
369 | (1) |
|
|
370 | (1) |
|
6.4.3 Processing Spreadsheet Data |
|
|
371 | (1) |
|
|
372 | (1) |
|
6.4.5 Representing Number Cells with Numerical Python Arrays |
|
|
373 | (1) |
|
6.4.6 Using More High-Level Numerical Python Functionality |
|
|
374 | (1) |
|
6.5 Examples from Analyzing DNA |
|
|
375 | (19) |
|
6.5.1 Computing Frequencies |
|
|
375 | (7) |
|
6.5.2 Analyzing the Frequency Matrix |
|
|
382 | (3) |
|
6.5.3 Finding Base Frequencies |
|
|
385 | (3) |
|
6.5.4 Translating Genes into Proteins |
|
|
388 | (5) |
|
6.5.5 Some Humans Can Drink Milk, While Others Cannot |
|
|
393 | (1) |
|
6.6 Making Code that is Compatible with Python 2 and 3 |
|
|
394 | (2) |
|
6.6.1 More Basic Differences Between Python 2 and 3 |
|
|
394 | (2) |
|
6.6.2 Turning Python 2 Code into Python 3 Code |
|
|
396 | (1) |
|
|
396 | (6) |
|
|
396 | (2) |
|
6.7.2 Example: A File Database |
|
|
398 | (4) |
|
|
402 | (7) |
|
7 Introduction to Classes |
|
|
409 | (80) |
|
7.1 Simple Function Classes |
|
|
409 | (17) |
|
7.1.1 Challenge: Functions with Parameters |
|
|
410 | (2) |
|
7.1.2 Representing a Function as a Class |
|
|
412 | (5) |
|
|
417 | (2) |
|
7.1.4 Another Function Class Example |
|
|
419 | (1) |
|
7.1.5 Alternative Function Class Implementations |
|
|
420 | (2) |
|
7.1.6 Making Classes Without the Class Construct |
|
|
422 | (2) |
|
|
424 | (2) |
|
7.2 More Examples on Classes |
|
|
426 | (6) |
|
|
426 | (2) |
|
|
428 | (2) |
|
|
430 | (2) |
|
|
432 | (19) |
|
7.3.1 The Call Special Method |
|
|
432 | (1) |
|
7.3.2 Example: Automagic Differentiation |
|
|
433 | (5) |
|
7.3.3 Example: Automagic Integration |
|
|
438 | (2) |
|
7.3.4 Turning an Instance into a String |
|
|
440 | (1) |
|
7.3.5 Example: Phone Book with Special Methods |
|
|
441 | (2) |
|
|
443 | (1) |
|
7.3.7 Example: Class for Polynomials |
|
|
443 | (6) |
|
7.3.8 Arithmetic Operations and Other Special Methods |
|
|
449 | (1) |
|
7.3.9 Special Methods for String Conversion |
|
|
449 | (2) |
|
7.4 Example: Class for Vectors in the Plane |
|
|
451 | (4) |
|
7.4.1 Some Mathematical Operations on Vectors |
|
|
451 | (1) |
|
|
452 | (2) |
|
|
454 | (1) |
|
7.5 Example: Class for Complex Numbers |
|
|
455 | (8) |
|
|
455 | (2) |
|
|
457 | (1) |
|
7.5.3 Mixing Complex and Real Numbers |
|
|
457 | (2) |
|
7.5.4 Dynamic, Static, Strong, Weak, and Duck Typing |
|
|
459 | (1) |
|
7.5.5 Special Methods for "Right" Operands |
|
|
460 | (1) |
|
7.5.6 Inspecting Instances |
|
|
461 | (2) |
|
7.6 Static Methods and Attributes |
|
|
463 | (1) |
|
|
464 | (6) |
|
|
464 | (2) |
|
7.7.2 Example: Interval Arithmetic |
|
|
466 | (4) |
|
|
470 | (19) |
|
8 Random Numbers and Simple Games |
|
|
489 | (78) |
|
8.1 Drawing Random Numbers |
|
|
489 | (8) |
|
|
490 | (1) |
|
8.1.2 Uniformly Distributed Random Numbers |
|
|
491 | (1) |
|
8.1.3 Visualizing the Distribution |
|
|
492 | (1) |
|
8.1.4 Vectorized Drawing of Random Numbers |
|
|
493 | (1) |
|
8.1.5 Computing the Mean and Standard Deviation |
|
|
494 | (2) |
|
8.1.6 The Gaussian or Normal Distribution |
|
|
496 | (1) |
|
|
497 | (10) |
|
8.2.1 Random Integer Functions |
|
|
498 | (1) |
|
8.2.2 Example: Throwing a Die |
|
|
498 | (3) |
|
8.2.3 Drawing a Random Element from a List |
|
|
501 | (1) |
|
8.2.4 Example: Drawing Cards from a Deck |
|
|
502 | (2) |
|
8.2.5 Example: Class Implementation of a Deck |
|
|
504 | (3) |
|
8.3 Computing Probabilities |
|
|
507 | (15) |
|
8.3.1 Principles of Monte Carlo Simulation |
|
|
507 | (1) |
|
8.3.2 Example: Throwing Dice |
|
|
508 | (3) |
|
8.3.3 Example: Drawing Balls from a Hat |
|
|
511 | (2) |
|
8.3.4 Random Mutations of Genes |
|
|
513 | (6) |
|
8.3.5 Example: Policies for Limiting Population Growth |
|
|
519 | (3) |
|
|
522 | (4) |
|
|
522 | (1) |
|
|
523 | (3) |
|
8.5 Monte Carlo Integration |
|
|
526 | (8) |
|
8.5.1 Derivation of Monte Carlo Integration |
|
|
526 | (2) |
|
8.5.2 Implementation of Standard Monte Carlo Integration |
|
|
528 | (3) |
|
8.5.3 Area Computing by Throwing Random Points |
|
|
531 | (3) |
|
8.6 Random Walk in One Space Dimension |
|
|
534 | (5) |
|
8.6.1 Basic Implementation |
|
|
534 | (1) |
|
|
535 | (1) |
|
8.6.3 Random Walk as a Difference Equation |
|
|
536 | (1) |
|
8.6.4 Computing Statistics of the Particle Positions |
|
|
536 | (1) |
|
8.6.5 Vectorized Implementation |
|
|
537 | (2) |
|
8.7 Random Walk in Two Space Dimensions |
|
|
539 | (3) |
|
8.7.1 Basic Implementation |
|
|
539 | (2) |
|
8.7.2 Vectorized Implementation |
|
|
541 | (1) |
|
|
542 | (7) |
|
|
542 | (2) |
|
8.8.2 Example: Random Growth |
|
|
544 | (5) |
|
|
549 | (18) |
|
9 Object-Oriented Programming |
|
|
567 | (78) |
|
9.1 Inheritance and Class Hierarchies |
|
|
567 | (9) |
|
9.1.1 A Class for Straight Lines |
|
|
568 | (1) |
|
9.1.2 A First Try on a Class for Parabolas |
|
|
569 | (1) |
|
9.1.3 A Class for Parabolas Using Inheritance |
|
|
569 | (2) |
|
9.1.4 Checking the Class Type |
|
|
571 | (1) |
|
9.1.5 Attribute vs Inheritance: has-a vs is-a Relationship |
|
|
572 | (2) |
|
9.1.6 Superclass for Defining an Interface |
|
|
574 | (2) |
|
9.2 Class Hierarchy for Numerical Differentiation |
|
|
576 | (13) |
|
9.2.1 Classes for Differentiation |
|
|
577 | (2) |
|
|
579 | (2) |
|
9.2.3 A flexible Main Program |
|
|
581 | (1) |
|
|
582 | (3) |
|
9.2.5 Alternative Implementation via Functions |
|
|
585 | (1) |
|
9.2.6 Alternative Implementation via Functional Programming |
|
|
586 | (1) |
|
9.2.7 Alternative Implementation via a Single Class |
|
|
587 | (2) |
|
9.3 Class Hierarchy for Numerical Integration |
|
|
589 | (10) |
|
9.3.1 Numerical Integration Methods |
|
|
589 | (1) |
|
9.3.2 Classes for Integration |
|
|
590 | (4) |
|
|
594 | (1) |
|
9.3.4 Using the Class Hierarchy |
|
|
595 | (2) |
|
9.3.5 About Object-Oriented Programming |
|
|
597 | (2) |
|
9.4 Class Hierarchy for Making Drawings |
|
|
599 | (21) |
|
9.4.1 Using the Object Collection |
|
|
600 | (9) |
|
9.4.2 Example of Classes for Geometric Objects |
|
|
609 | (5) |
|
9.4.3 Adding Functionality via Recursion |
|
|
614 | (4) |
|
9.4.4 Scaling, Translating, and Rotating a Figure |
|
|
618 | (2) |
|
9.5 Classes for DNA Analysis |
|
|
620 | (7) |
|
|
620 | (1) |
|
|
621 | (5) |
|
|
626 | (1) |
|
|
627 | (8) |
|
|
627 | (2) |
|
9.6.2 Example: Input Data Reader |
|
|
629 | (6) |
|
|
635 | (10) |
|
A Sequences and Difference Equations |
|
|
645 | (38) |
|
A.1 Mathematical Models Based on Difference Equations |
|
|
646 | (19) |
|
|
647 | (2) |
|
A.1.2 The Factorial as a Difference Equation |
|
|
649 | (1) |
|
|
650 | (1) |
|
A.1.4 Growth of a Population |
|
|
651 | (1) |
|
|
652 | (2) |
|
|
654 | (1) |
|
A.1.7 The Integral as a Difference Equation |
|
|
655 | (2) |
|
A.1.8 Taylor Series as a Difference Equation |
|
|
657 | (1) |
|
A.1.9 Making a Living from a Fortune |
|
|
658 | (1) |
|
|
659 | (4) |
|
A.1.11 The Inverse of a Function |
|
|
663 | (2) |
|
A.2 Programming with Sound |
|
|
665 | (6) |
|
A.2.1 Writing Sound to File |
|
|
666 | (1) |
|
A.2.2 Reading Sound from File |
|
|
667 | (1) |
|
|
667 | (1) |
|
A.2.4 Music of a Sequence |
|
|
668 | (3) |
|
|
671 | (12) |
|
B Introduction to Discrete Calculus |
|
|
683 | (32) |
|
|
683 | (5) |
|
|
684 | (1) |
|
|
685 | (1) |
|
B.1.3 Evaluating the Approximation |
|
|
686 | (1) |
|
|
687 | (1) |
|
B.2 Differentiation Becomes Finite Differences |
|
|
688 | (5) |
|
B.2.1 Differentiating the Sine Function |
|
|
689 | (1) |
|
B.2.2 Differences on a Mesh |
|
|
690 | (2) |
|
|
692 | (1) |
|
B.3 Integration Becomes Summation |
|
|
693 | (6) |
|
B.3.1 Dividing into Subintervals |
|
|
693 | (2) |
|
B.3.2 Integration on Subintervals |
|
|
695 | (1) |
|
B.3.3 Adding the Subintervals |
|
|
696 | (1) |
|
|
697 | (2) |
|
|
699 | (10) |
|
B.4.1 Approximating Functions Close to One Point |
|
|
699 | (1) |
|
B.4.2 Approximating the Exponential Function |
|
|
699 | (1) |
|
B.4.3 More Accurate Expansions |
|
|
700 | (2) |
|
B.4.4 Accuracy of the Approximation |
|
|
702 | (2) |
|
B.4.5 Derivatives Revisited |
|
|
704 | (1) |
|
B.4.6 More Accurate Difference Approximations |
|
|
705 | (2) |
|
B.4.7 Second-Order Derivatives |
|
|
707 | (2) |
|
|
709 | (6) |
|
C Introduction to differential equations |
|
|
715 | (16) |
|
|
716 | (2) |
|
|
718 | (5) |
|
|
723 | (1) |
|
|
724 | (3) |
|
C.5 A Model for the Spreading of a Disease |
|
|
727 | (2) |
|
|
729 | (2) |
|
D A Complete Differential Equation Project |
|
|
731 | (26) |
|
D.1 About the Problem: Motion and Forces in Physics |
|
|
731 | (6) |
|
D.1.1 The Physical Problem |
|
|
731 | (2) |
|
D.1.2 The Computational Algorithm |
|
|
733 | (1) |
|
D.1.3 Derivation of the Mathematical Model |
|
|
734 | (2) |
|
D.1.4 Derivation of the Algorithm |
|
|
736 | (1) |
|
D.2 Program Development and Testing |
|
|
737 | (9) |
|
|
737 | (3) |
|
D.2.2 Callback Functionality |
|
|
740 | (2) |
|
|
742 | (1) |
|
|
743 | (3) |
|
|
746 | (9) |
|
D.3.1 Simultaneous Computation and Plotting |
|
|
746 | (2) |
|
|
748 | (1) |
|
D.3.3 Remark on Choosing At |
|
|
749 | (1) |
|
D.3.4 Comparing Several Quantities in Subplots |
|
|
750 | (1) |
|
D.3.5 Comparing Approximate and Exact Solutions |
|
|
751 | (1) |
|
D.3.6 Evolution of the Error as At Decreases |
|
|
752 | (3) |
|
|
755 | (2) |
|
E Programming of Differential Equations |
|
|
757 | (78) |
|
E.1 Scalar Ordinary Differential Equations |
|
|
758 | (14) |
|
E.1.1 Examples on Right-Hand-Side Functions |
|
|
758 | (1) |
|
E.1.2 The Forward Euler Scheme |
|
|
759 | (1) |
|
E.1.3 Function Implementation |
|
|
760 | (1) |
|
E.1.4 Verifying the Implementation |
|
|
761 | (2) |
|
E.1.5 From Discrete to Continuous Solution |
|
|
763 | (1) |
|
E.1.6 Switching Numerical Method |
|
|
764 | (1) |
|
E.1.7 Class Implementation |
|
|
764 | (5) |
|
E.1.8 Logistic Growth via a Function-Based Approach |
|
|
769 | (1) |
|
E.1.9 Logistic Growth via a Class-Based Approach |
|
|
769 | (3) |
|
E.2 Systems of Ordinary Differential Equations |
|
|
772 | (7) |
|
E.2.1 Mathematical Problem |
|
|
773 | (1) |
|
E.2.2 Example of a System of ODEs |
|
|
774 | (1) |
|
E.2.3 Function Implementation |
|
|
775 | (2) |
|
E.2.4 Class Implementation |
|
|
777 | (2) |
|
E.3 The ODESolver Class Hierarchy |
|
|
779 | (23) |
|
|
779 | (1) |
|
E.3.2 Construction of a Solver Hierarchy |
|
|
780 | (3) |
|
E.3.3 The Backward Euler Method |
|
|
783 | (2) |
|
|
785 | (2) |
|
E.3.5 Example: Exponential Decay |
|
|
787 | (2) |
|
E.3.6 Example: The Logistic Equation with Problem and Solver Classes |
|
|
789 | (8) |
|
E.3.7 Example: An Oscillating System |
|
|
797 | (2) |
|
E.3.8 Application 4: The Trajectory of a Ball |
|
|
799 | (2) |
|
E.3.9 Further Developments of ODESolver |
|
|
801 | (1) |
|
|
802 | (33) |
|
|
835 | (22) |
|
|
835 | (3) |
|
|
838 | (19) |
|
F.2.1 A Recipe for Program Writing and Debugging |
|
|
838 | (3) |
|
F.2.2 Application of the Recipe |
|
|
841 | (12) |
|
F.2.3 Getting Help from a Code Analyzer |
|
|
853 | (4) |
|
G Migrating Python to Compiled Code |
|
|
857 | (14) |
|
G.1 Pure Python Code for Monte Carlo Simulation |
|
|
857 | (3) |
|
G.1.1 The Computational Problem |
|
|
858 | (1) |
|
G.1.2 A Scalar Python Implementation |
|
|
858 | (1) |
|
G.1.3 A Vectorized Python Implementation |
|
|
859 | (1) |
|
G.2 Migrating Scalar Python Code to Cython |
|
|
860 | (5) |
|
G.2.1 A Plain Cython Implementation |
|
|
860 | (3) |
|
G.2.2 A Better Cython Implementation |
|
|
863 | (2) |
|
|
865 | (6) |
|
G.3.1 Writing a C Program |
|
|
865 | (1) |
|
G.3.2 Migrating Loops to C Code via F2PY |
|
|
866 | (1) |
|
G.3.3 Migrating Loops to C Code via Cython |
|
|
867 | (1) |
|
G.3.4 Comparing Efficiency |
|
|
868 | (3) |
|
|
871 | (36) |
|
H.1 Getting Access to Python |
|
|
871 | (7) |
|
|
871 | (1) |
|
H.1.2 Installing Software on Your Laptop: Mac OS X and Windows |
|
|
872 | (1) |
|
H.1.3 Anaconda and Spyder |
|
|
873 | (1) |
|
H.1.4 VMWare Fusion Virtual Machine |
|
|
874 | (3) |
|
H.1.5 Dual Boot on Windows |
|
|
877 | (1) |
|
H.1.6 Vagrant Virtual Machine |
|
|
877 | (1) |
|
H.2 How to Write and Run a Python Program |
|
|
878 | (2) |
|
H.2.1 The Need for a Text Editor |
|
|
878 | (2) |
|
|
880 | (1) |
|
H.3 The SageMathCloud and Wakari Web Services |
|
|
880 | (2) |
|
H.3.1 Basic Intro to SageMathCloud |
|
|
880 | (1) |
|
H.3.2 Basic Intro to Wakari |
|
|
881 | (1) |
|
H.3.3 Installing Your Own Python Packages |
|
|
881 | (1) |
|
H.4 Writing IPython Notebooks |
|
|
882 | (2) |
|
H.4.1 A Simple Program in the Notebook |
|
|
882 | (1) |
|
H.4.2 Mixing Text, Mathematics, Code, and Graphics |
|
|
882 | (2) |
|
H.5 Different Ways of Running Python Programs |
|
|
884 | (4) |
|
H.5.1 Executing Python Programs in iPython |
|
|
884 | (1) |
|
H.5.2 Executing Python Programs in Unix |
|
|
884 | (1) |
|
H.5.3 Executing Python Programs in Windows |
|
|
885 | (2) |
|
H.5.4 Executing Python Programs in Mac OS X |
|
|
887 | (1) |
|
H.5.5 Making a Complete Stand-Alone Executable |
|
|
887 | (1) |
|
H.6 Doing Operating System Tasks in Python |
|
|
888 | (3) |
|
H.7 Variable Number of Function Arguments |
|
|
891 | (5) |
|
H.7.1 Variable Number of Positional Arguments |
|
|
891 | (3) |
|
H.7.2 Variable Number of Keyword Arguments |
|
|
894 | (2) |
|
H.8 Evaluating Program Efficiency |
|
|
896 | (3) |
|
H.8.1 Making Time Measurements |
|
|
896 | (2) |
|
H.8.2 Profiling Python Programs |
|
|
898 | (1) |
|
|
899 | (8) |
|
H.9.1 Requirements of the Test Function |
|
|
900 | (1) |
|
H.9.2 Writing the Test Function; Precomputed Data |
|
|
900 | (1) |
|
H.9.3 Writing the Test Function; Exact Numerical Solution |
|
|
901 | (1) |
|
H.9.4 Testing of Function Robustness |
|
|
902 | (2) |
|
H.9.5 Automatic Execution of Tests |
|
|
904 | (3) |
References |
|
907 | (2) |
Index |
|
909 | |