A Primer on Scientific Programming with Python

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

The book serves as a first introduction to computer programming of scientific applications, using the high-level Python language. The exposition is example- and problem-oriented, where the applications are taken from mathematics, numerical calculus, statistics, physics, biology, and finance. The book teaches "Matlab-style" and procedural programming as well as object-oriented programming. High school mathematics is a required background, and it is advantageous to study classical and numerical one-variable calculus in parallel with reading this book. Besides learning how to program computers, the reader will also learn how to solve mathematical problems, arising in various branches of science and engineering, with the aid of numerical methods and programming. By blending programming, mathematics and scientific applications, the book lays a solid foundation for practicing computational science.

Author(s): Hans Petter Langtangen
Edition: 3
Publisher: Springer Science & Business Media
Year: 2012

Language: English
Pages: 798

Cover
Preface
Contents
List of Exercises
1 Computing with Formulas
1.1 The First Programming Encounter: A Formula
1.1.1 Using a Program as a Calculator
1.1.2 About Programs and Programming
1.1.3 Tools for Writing Programs
1.1.4 Using Idle to Write the Program
Warning About Typing Program Text
1.1.5 How to Run the Program
1.1.6 Verifying the Result
1.1.7 Using Variables
1.1.8 Names of Variables
1.1.9 Reserved Words in Python
1.1.10 Comments
1.1.11 Formatting Text and Numbers
Printf Syntax
Format String Syntax
The Newline Character
1.2 Computer Science Glossary
1.3 Another Formula: Celsius-Fahrenheit Conversion
1.3.1 Potential Error: Integer Division
Straightforward Coding of the Formula
Verifying the Results
Float and Integer Division
1.3.2 Objects in Python
1.3.3 Avoiding Integer Division
Comment
1.3.4 Arithmetic Operators and Precedence
1.4 Evaluating Standard Mathematical Functions
1.4.1 Example: Using the Square Root Function
Problem
The Program
Two Ways of Importing a Module
Import with New Names
1.4.2 Example: Using More Mathematical Functions
1.4.3 A First Glimpse of Round-Off Errors
1.5 Interactive Computing
1.5.1 Using the Python Shell
1.5.2 Type Conversion
1.5.3 IPython
Running Programs
Quick Recovery of Previous Output
TAB Completion
Recovering Previous Commands
Running Unix/Windows Commands
Remark
1.6 Complex Numbers
1.6.1 Complex Arithmetics in Python
1.6.2 Complex Functions in Python
1.6.3 Unified Treatment of Complex and Real Functions
1.7 Summary
1.7.1 Chapter Topics
Program Files
Programs Must Be Accurate!
Variables
Comment Lines
Object Types
Operators
Common Mathematical Functions
Print
Integer Division
Complex Numbers
Terminology
1.7.2 Example: Trajectory of a Ball
Problem
Solution
1.7.3 About Typesetting Conventions in This Book
1.8 Exercises
What Does It Mean to Solve an Exercise?
2 Loops and Lists
2.1 While Loops
2.1.1 A Naive Solution
2.1.2 While Loops
2.1.3 Boolean Expressions
2.1.4 Loop Implementation of a Sum
2.2 Lists
2.2.1 Basic List Operations
2.2.2 For Loops
The Nature of For Loops
Making the Table
2.3 Alternative Implementations with Lists and Loops
2.3.1 While Loop Implementation of a For Loop
2.3.2 The Range Construction
2.3.3 For Loops with List Indices
2.3.4 Changing List Elements
2.3.5 List Comprehension
2.3.6 Traversing Multiple Lists Simultaneously
2.4 Nested Lists
2.4.1 A Table as a List of Rows or Columns
2.4.2 Printing Objects
Modules for Pretty Print of Objects
Manual Printing
2.4.3 Extracting Sublists
Example
2.4.4 Traversing Nested Lists
2.5 Tuples
2.6 Summary
2.6.1 Chapter Topics
While Loops
Lists
Nested Lists
Tuples
For Loops
Pretty Print
Terminology
2.6.2 Example: Analyzing List Data
Problem
Solution
2.6.3 How to Find More Python Information
2.7 Exercises
3 Functions and Branching
3.1 Functions
3.1.1 Functions of One Variable
3.1.2 Local and Global Variables
3.1.3 Multiple Arguments
3.1.4 Multiple Return Values
3.1.5 Functions with No Return Values
3.1.6 Keyword Arguments
Example: Function with Default Parameters
Example: Computing a Sum with Default Tolerance
3.1.7 Doc Strings
3.1.8 Function Input and Output
3.1.9 Functions as Arguments to Functions
The Behavior of the Numerical Derivative as h → 0
3.1.10 The Main Program
3.1.11 Lambda Functions
3.2 Branching
3.2.1 If-Else Blocks
3.2.2 Inline If Tests
3.3 Mixing Loops, Branching, and Functions in Bioinformatics Examples
3.3.1 Counting Letters in DNA Strings
List Iteration
String Iteration
Program Flow
Index Iteration
While Loops
Summing a Boolean List
Inline If Test
Using Boolean Values Directly
List Comprehensions
Using a Sum Iterator
Extracting Indices
Using Python's Library
3.3.2 Efficiency Assessment
Generating Random DNA Strings
Measuring CPU Time
3.4 Summary
3.4.1 Chapter Topics
User-Defined Functions
Keyword Arguments
If Tests
Inline If Tests
Terminology
3.4.2 Example: Numerical Integration
Problem
Solution
3.5 Exercises
4 Input Data and Error Handling
4.1 Asking Questions and Reading Answers
4.1.1 Reading Keyboard Input
4.1.2 The Magic “eval” Function
4.1.3 The Magic “exec” Function
4.1.4 Turning String Expressions into Functions
4.2 Reading from the Command Line
4.2.1 Providing Input on the Command Line
4.2.2 A Variable Number of Command-Line Arguments
4.2.3 More on Command-Line Arguments
4.2.4 Option-Value Pairs on the Command Line
4.3 Handling Errors
4.3.1 Exception Handling
Testing for a Specific Exception
Examples on Exception Types
Digression
4.3.2 Raising Exceptions
Example
4.4 A Glimpse of Graphical User Interfaces
4.5 Making Modules
4.5.1 Example: Interest on Bank Deposits
4.5.2 Collecting Functions in a Module File
Test Block
Flexible Test Blocks
4.5.3 Using Modules
4.6 Summary
4.6.1 Chapter Topics
Question and Answer Input
Getting Command-Line Arguments
Using Option-Value Pairs
Generating Code on the Fly
Turning String Formulas into Python Functions
Handling Exceptions
Raising Exceptions
Modules
4.6.2 Example: Bisection Root Finding
Problem
Solution
Verification
Making a Function
Making a Module
Using the Module
A Flexible Program for Solving f(x) = 0
4.7 Exercises
5 Array Computing and Curve Plotting
5.1 Vectors
5.1.1 The Vector Concept
5.1.2 Mathematical Operations on Vectors
5.1.3 Vector Arithmetics and Vector Functions
5.2 Arrays in Python Programs
5.2.1 Using Lists for Collecting Function Data
5.2.2 Basics of Numerical Python Arrays
5.2.3 Computing Coordinates and Function Values
5.2.4 Vectorization
5.3 Curve Plotting
5.3.1 Matplotlib; Pylab
A Basic Plot
Decorating the Plot
Plotting Multiple Curves
Placing Several Plots in One Figure
5.3.2 Matplotlib; Pyplot
5.3.3 SciTools and Easyviz
Importing SciTools and Easyviz
A Basic Plot
Decorating the Plot
Plotting Multiple Curves
Changing Backend
Placing Several Plots in One Figure
5.3.4 Making Animations
Example
Animation in Matplotlib
Remarks on Filenames
Movie Formats
5.3.5 Curves in Pure Text
5.4 Plotting Difficulties
5.4.1 Piecewisely Defined Functions
Example: The Heaviside Function
Example: A Hat Function
5.4.2 Rapidly Varying Functions
5.5 More Advanced Vectorization of Functions
5.5.1 Vectorizing StringFunction Objects
5.5.2 Vectorization of the Heaviside Function
Loop
Automatic Vectorization
Mixing Boolean and Floating-Point Calculations
Manual Vectorization
5.5.3 Vectorization of a Hat Function
5.6 More on Numerical Python Arrays
5.6.1 Copying Arrays
5.6.2 In-Place Arithmetics
5.6.3 Allocating Arrays
5.6.4 Generalized Indexing
5.6.5 Testing for the Array Type
Example: Vectorizing a Constant Function
5.6.6 Compact Syntax for Array Generation
5.6.7 Shape Manipulation
5.7 Higher-Dimensional Arrays
5.7.1 Matrices and Arrays
5.7.2 Two-Dimensional Numerical Python Arrays
5.7.3 Array Computing
Remark
5.7.4 Two-Dimensional Arrays and Functions of Two Variables
5.7.5 Matrix Objects
5.8 Summary
5.8.1 Chapter Topics
Array Computing
Plotting Curves
Making Movies
5.8.2 Example: Animating a Function
Problem
Solution
Scaling
5.9 Exercises
6 Files, Strings, and Dictionaries
6.1 Reading Data from File
6.1.1 Reading a File Line by Line
For Loop over Lines
While Loop over Lines
Reading a File into a String
6.1.2 Reading a Mixture of Text and Numbers
6.1.3 What Is a File, Really?
Pure Text Files
Word Processor Files
Image Files
Music Files
PDF Files
Remarks
6.2 Dictionaries
6.2.1 Making Dictionaries
6.2.2 Dictionary Operations
Remark
6.2.3 Example: Polynomials as Dictionaries
6.2.4 Dictionaries with Default Values and Ordering
Dictionaries with Default Values
Ordered Dictionaries
6.2.5 Example: File Data in Dictionaries
Problem
Solution
6.2.6 Example: File Data in Nested Dictionaries
Problem
Algorithm
Implementation
Dissection
6.2.7 Example: Comparing Stock Prices
Problem
Solution
Generalization
6.3 Strings
6.3.1 Common Operations on Strings
Substring Specification
Searching for Substrings
Substitution
String Splitting
Upper and Lower Case
Strings Are Constant
Strings with Digits Only
Whitespace
Joining Strings
6.3.2 Example: Reading Pairs of Numbers
Problem
Solution
6.3.3 Example: Reading Coordinates
Problem
Solution 1: Substring Extraction
Solution 2: String Search
Solution 3: String Split
6.4 Reading Data from Web Pages
6.4.1 About Web Pages
6.4.2 How to Access Web Pages in Programs
6.4.3 Example: Reading Pure Text Files
6.4.4 Example: Extracting Data from HTML
6.5 Writing Data to File
6.5.1 Example: Writing a Table to File
Problem
Solution
6.5.2 Standard Input and Output as File Objects
Redirecting Standard Input, Output, and Error
Note
6.5.3 Reading and Writing Spreadsheet Files
Reading CSV Files
Processing Data
Writing CSV Files
Remark
Representing Number Cells with Numerical Python Arrays
6.6 Examples from Analyzing DNA
6.6.1 Computing Frequencies
Separate Frequency Lists
Nested List
Dictionary for More Convenient Indexing
Numerical Python Array
Dictionary of Lists
Dictionary of Dictionaries
Using Dictionaries with Default Values
Remark
Using Arrays and Vectorization
6.6.2 Analyzing the Frequency Matrix
List of Lists Frequency Matrix
Dict of Dicts Frequency Matrix
6.6.3 Finding Base Frequencies
6.6.4 Translating Genes into Proteins
6.6.5 Some Humans Can Drink Milk, While Others Cannot
6.7 Summary
6.7.1 Chapter Topics
File Operations
Downloading Internet Files
Dictionaries
Strings
6.7.2 Example: A File Database
Problem
Solution
6.8 Exercises
7 Introduction to Classes
7.1 Simple Function Classes
7.1.1 Problem: Functions with Parameters
Problem
A Bad Solution: Global Variables
7.1.2 Representing a Function as a Class
Implementation
Usage and Dissection
The self Variable
Extension of the Class
Remark
Using Methods as Ordinary Functions
Doc Strings
7.1.3 Another Function Class Example
Remark
7.1.4 Alternative Function Class Implementations
7.1.5 Making Classes Without the Class Construct
First Remark
Second Remark
7.2 More Examples on Classes
7.2.1 Bank Accounts
7.2.2 Phone Book
7.2.3 A Circle
Remark
7.3 Special Methods
7.3.1 The Call Special Method
7.3.2 Example: Automagic Differentiation
Problem
Solution
Application: Newton's Method
7.3.3 Example: Automagic Integration
A Simple Implementation
Remark
7.3.4 Turning an Instance into a String
7.3.5 Example: Phone Book with Special Methods
Remark
7.3.6 Adding Objects
7.3.7 Example: Class for Polynomials
Implementation
Usage
Pretty Print of Polynomials
7.3.8 Arithmetic Operations and Other Special Methods
7.3.9 Special Methods for String Conversion
Recreating Objects from Strings
7.4 Example: Class for Vectors in the Plane
7.4.1 Some Mathematical Operations on Vectors
7.4.2 Implementation
7.4.3 Usage
Comment
7.5 Example: Class for Complex Numbers
7.5.1 Implementation
7.5.2 Illegal Operations
7.5.3 Mixing Complex and Real Numbers
Computer Science Discussion
7.5.4 Special Methods for “Right” Operands
Remark
7.5.5 Inspecting Instances
7.6 Static Methods and Attributes
7.7 Summary
7.7.1 Chapter Topics
Classes
Special Methods
7.7.2 Example: Interval Arithmetics
Problem
Solution
7.8 Exercises
8 Random Numbers and Simple Games
8.1 Drawing Random Numbers
8.1.1 The Seed
8.1.2 Uniformly Distributed Random Numbers
8.1.3 Visualizing the Distribution
8.1.4 Vectorized Drawing of Random Numbers
Warning
8.1.5 Computing the Mean and Standard Deviation
8.1.6 The Gaussian or Normal Distribution
8.2 Drawing Integers
8.2.1 Random Integer Functions
8.2.2 Example: Throwing a Die
8.2.3 Drawing a Random Element from a List
8.2.4 Example: Drawing Cards from a Deck
8.2.5 Example: Class Implementation of a Deck
8.3 Computing Probabilities
8.3.1 Principles of Monte Carlo Simulation
8.3.2 Example: Throwing Dice
Straightforward Solution
Vectorization
Exact Solution
A Game
Decide If a Game Is Fair
8.3.3 Example: Drawing Balls from a Hat
8.3.4 Random Mutations of Genes
A Simple Mutation Model
Vectorized Version
A Markov Chain Mutation Model
8.3.5 Example: Policies for Limiting Population Growth
8.4 Simple Games
8.4.1 Guessing a Number
The Game
The Implementation
8.4.2 Rolling Two Dice
The Game
The Implementation
Example
A Class Version
8.5 Monte Carlo Integration
8.5.1 Standard Monte Carlo Integration
8.5.2 Area Computing by Throwing Random Points
8.6 Random Walk in One Space Dimension
8.6.1 Basic Implementation
8.6.2 Visualization
8.6.3 Random Walk as a Difference Equation
8.6.4 Computing Statistics of the Particle Positions
8.6.5 Vectorized Implementation
8.7 Random Walk in Two Space Dimensions
8.7.1 Basic Implementation
8.7.2 Vectorized Implementation
8.8 Summary
8.8.1 Chapter Topics
Drawing Random Numbers
Typical Probability Computation
Statistical Measures
8.8.2 Example: Random Growth
Problem
Solution
8.9 Exercises
9 Object-Oriented Programming
9.1 Inheritance and Class Hierarchies
9.1.1 A Class for Straight Lines
9.1.2 A First Try on a Class for Parabolas
9.1.3 A Class for Parabolas Using Inheritance
Program Flow
9.1.4 Checking the Class Type
9.1.5 Attribute Versus Inheritance
9.1.6 Extending Versus Restricting Functionality
9.1.7 Superclass for Defining an Interface
9.2 Class Hierarchy for Numerical Differentiation
9.2.1 Classes for Differentiation
Computer Science Remark
9.2.2 A Flexible Main Program
9.2.3 Extensions
Application
9.2.4 Alternative Implementation via Functions
9.2.5 Alternative Implementation via Functional Programming
9.2.6 Alternative Implementation via a Single Class
Remark
9.3 Class Hierarchy for Numerical Integration
9.3.1 Numerical Integration Methods
9.3.2 Classes for Integration
9.3.3 Using the Class Hierarchy
9.3.4 About Object-Oriented Programming
9.4 Class Hierarchy for Making Drawings
9.4.1 Using the Object Collection
Basic Drawing
Groups of Objects
Changing Line Styles and Colors
The Figure Composition as an Object Hierarchy
Animation: Translating the Vehicle
Animation: Rolling the Wheels
9.4.2 Example of Classes for Geometric Objects
Simple Geometric Objects
Class Curve
Compound Geometric Objects
9.4.3 Adding Functionality via Recursion
Basic Principles of Recursion
Explaining Recursion
9.4.4 Scaling, Translating, and Rotating a Figure
Scaling
Translation
Rotation
9.5 Classes for DNA Analysis
9.5.1 Class for Regions
9.5.2 Class for Genes
Basic Features of class Gene
Flexible Constructor
Other Methods
9.5.3 Subclasses
Acknowledgments
9.6 Summary
9.6.1 Chapter Topics
Subclass Example
Subclassing in General
9.6.2 Example: Input Data Reader
Problem
Solution
Prompting the User
Reading from File
Reading from the Command Line
Reading from a GUI
More Flexibility in the Superclass
Demonstrating the Tool
9.7 Exercises
A Sequences and Difference Equations
A.1 Mathematical Models Based on Difference Equations
A.1.1 Interest Rates
A.1.2 The Factorial as a Difference Equation
A.1.3 Fibonacci Numbers
A.1.4 Growth of a Population
A.1.5 Logistic Growth
A.1.6 Payback of a Loan
A.1.7 The Integral as a Difference Equation
A.1.8 Taylor Series as a Difference Equation
A.1.9 Making a Living from a Fortune
A.1.10 Newton's Method
A.1.11 The Inverse of a Function
A.2 Programming with Sound
A.2.1 Writing Sound to File
A.2.2 Reading Sound from File
A.2.3 Playing Many Notes
A.2.4 Music of a Sequence
Problem
Solution
A.3 Exercises
B Introduction to Discrete Calculus
B.1 Discrete Functions
B.1.1 The Sine Function
B.1.2 Interpolation
B.1.3 Evaluating the Approximation
B.1.4 Generalization
B.2 Differentiation Becomes Finite Differences
B.2.1 Differentiating the Sine Function
B.2.2 Differences on a Mesh
B.2.3 Generalization
B.3 Integration Becomes Summation
B.3.1 Dividing into Subintervals
B.3.2 Integration on Subintervals
B.3.3 Adding the Subintervals
B.3.4 Generalization
B.4 Taylor Series
B.4.1 Approximating Functions Close to One Point
B.4.2 Approximating the Exponential Function
B.4.3 More Accurate Expansions
B.4.4 Accuracy of the Approximation
B.4.5 Derivatives Revisited
B.4.6 More Accurate Difference Approximations
B.4.7 Second-Order Derivatives
B.5 Exercises
C Introduction to Differential Equations
C.1 The Simplest Case
C.2 Exponential Growth
Analytical Solution
Numerical Solution
C.3 Logistic Growth
C.4 A Simple Pendulum
C.5 A Model for the Spread of a Disease
C.6 Exercises
D A Complete Differential Equation Project
D.1 About the Problem: Motion and Forces in Physics
D.1.1 The Physical Problem
Goal of the Computations
The Key Quantities
D.1.2 The Computational Algorithm
D.1.3 Derivation of the Mathematical Model
D.1.4 Derivation of the Algorithm
D.2 Program Development and Testing
D.2.1 Implementation
D.2.2 Callback Functionality
D.2.3 Making a Module
D.2.4 Verification
D.3 Visualization
D.3.1 Simultaneous Computation and Plotting
Fixing Axes
D.3.2 Some Applications
D.3.3 Remark on Choosing Δt
D.3.4 Comparing Several Quantities in Subplots
D.3.5 Comparing Approximate and Exact Solutions
D.3.6 Evolution of the Error as Δt Decreases
D.4 Exercises
E Programming of Differential Equations
E.1 Scalar Ordinary Differential Equations
E.1.1 Examples on Right-Hand-Side Functions
E.1.2 The Forward Euler Scheme
E.1.3 Function Implementation
E.1.4 Verifying the Implementation
E.1.5 From Discrete to Continuous Solution
E.1.6 Switching Numerical Method
E.1.7 Class Implementation
Class Wrapping of a Function
Switching Numerical Method
A More Flexible Class
E.1.8 Example: Logistic Growth
E.2 Systems of Ordinary Differential Equations
E.2.1 Mathematical Problem
E.2.2 Example of a System of ODEs
E.2.3 From Scalar ODE Code to Systems
Function Implementation
Allowing Lists
Class Implementation
E.2.4 Numerical Methods
E.2.5 The ODE Solver Class Hierarchy
The Superclass
The Forward Euler Method
Remark
The 4th-order Runge-Kutta Method
E.2.6 The Backward Euler Method
E.2.7 Application 1: u' = -u
E.2.8 Application 2: The Logistic Equation
Basic Problem and Solver Classes
Computing an Appropriate Δt
Dealing with Time-Dependent Coefficients
Reading Input
E.2.9 Application 3: An Oscillating System
E.2.10 Application 4: The Trajectory of a Ball
E.2.11 Further Developments of ODESolver
E.3 Exercises
F Debugging
F.1 Using a Debugger
F.2 How to Debug
F.2.1 A Recipe for Program Writing and Debugging
F.2.2 Application of the Recipe
Problem
Solution
Refining the User Interface
G Migrating Python to Compiled Code
G.1 Pure Python Code for Monte Carlo Simulation
G.1.1 The Computational Problem
G.1.2 A Scalar Python Implementation
G.1.3 A Vectorized Python Implementation
G.2 Migrating Scalar Python Code to Cython
G.2.1 A Plain Cython Implementation
G.2.2 A Better Cython Implementation
G.3 Migrating Code to C
G.3.1 Writing a C Program
G.3.2 Migrating Loops to C Code via F2PY
G.3.3 Migrating Loops to C Code via Cython
G.3.4 Comparing Efficiency
H Technical Topics
H.1 Different Ways of Running Python Programs
H.1.1 Executing Python Programs in IPython
H.1.2 Executing Python Programs on Unix
H.1.3 Executing Python Programs on Windows
Double-Clicking Python Files
Gnuplot Plots on Windows
H.1.4 Executing Python Programs on Macintosh
H.1.5 Making a Complete Stand-Alone Executable
H.2 Integer and Float Division
H.3 Visualizing a Program with Lumpy
H.4 Doing Operating System Tasks in Python
H.5 Variable Number of Function Arguments
H.5.1 Variable Number of Positional Arguments
Example
H.5.2 Variable Number of Keyword Arguments
Example
H.6 Evaluating Program Efficiency
H.6.1 Making Time Measurements
The time Module
The timeit Module
Hardware Information
H.6.2 Profiling Python Programs
References
Index