Contents
xvii
4.1.5 Calling a Function Defined with Positional Parameters . . . 83
4.1.6 A Function with Two Return Values . . . . . . . . . . . . . . . . . . . 85
4.1.7 Calling a Function Defined with Keyword Parameters . . . . 85
4.1.8 A Function with Another Function as Input Argument . . . . 86
4.1.9 Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.1.10 A Function with Several Return Statements . . . . . . . . . . . . . 87
4.2 Programming as a Step-Wise Strategy . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.2.1 Making a Times Tables Test . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.2.2 The 1st Version of Our Code . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.2.3 The 2nd Version of Our Code . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.2.4 The 3rd Version of Our Code . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
5 Some More Python Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.1 Lists and Tuples: Alternatives to Arrays . . . . . . . . . . . . . . . . . . . . . . . 103
5.2 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.2.1 The Fourth Version of Our Times Tables Program . . . . . . . . 106
5.3 Symbolic Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
5.3.1 Numerical Versus Symbolic Computations . . . . . . . . . . . . . . 111
5.3.2 SymPy: Some Basic Functionality . . . . . . . . . . . . . . . . . . . . . 112
5.3.3 Symbolic Calculations with Some Other Tools . . . . . . . . . . 112
5.4 Making Our Own Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.4.1 A Naive Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
5.4.2 A Module for Vertical Motion . . . . . . . . . . . . . . . . . . . . . . . . 115
5.4.3 Module or Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.5 Files: Read and Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
5.6 Measuring Execution Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
5.6.1 The timeit Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
5.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6 Computing Integrals and Testing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
6.1 Basic Ideas of Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . 132
6.2 The Composite Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.2.1 The General Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
6.2.2 A General Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
6.2.3 A Specific Implementation: What’s the Problem? . . . . . . . . 139
6.3 The Composite Midpoint Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.3.1 The General Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
6.3.2 A General Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.3.3 Comparing the Trapezoidal and the Midpoint Methods . . . 145
6.4 Vectorizing the Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
6.4.1 Vectorizing the Midpoint Rule . . . . . . . . . . . . . . . . . . . . . . . . 146
6.4.2 Vectorizing the Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . . . 147
6.4.3 Speed up Gained with Vectorization . . . . . . . . . . . . . . . . . . . 148
6.5 Rate of Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
6.6 Testing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
6.6.1 Problems with Brief Testing Procedures . . . . . . . . . . . . . . . . 150
6.6.2 Proper Test Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
xvii
4.1.5 Calling a Function Defined with Positional Parameters . . . 83
4.1.6 A Function with Two Return Values . . . . . . . . . . . . . . . . . . . 85
4.1.7 Calling a Function Defined with Keyword Parameters . . . . 85
4.1.8 A Function with Another Function as Input Argument . . . . 86
4.1.9 Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.1.10 A Function with Several Return Statements . . . . . . . . . . . . . 87
4.2 Programming as a Step-Wise Strategy . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.2.1 Making a Times Tables Test . . . . . . . . . . . . . . . . . . . . . . . . . . 89
4.2.2 The 1st Version of Our Code . . . . . . . . . . . . . . . . . . . . . . . . . 90
4.2.3 The 2nd Version of Our Code . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.2.4 The 3rd Version of Our Code . . . . . . . . . . . . . . . . . . . . . . . . . 93
4.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
5 Some More Python Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
5.1 Lists and Tuples: Alternatives to Arrays . . . . . . . . . . . . . . . . . . . . . . . 103
5.2 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.2.1 The Fourth Version of Our Times Tables Program . . . . . . . . 106
5.3 Symbolic Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
5.3.1 Numerical Versus Symbolic Computations . . . . . . . . . . . . . . 111
5.3.2 SymPy: Some Basic Functionality . . . . . . . . . . . . . . . . . . . . . 112
5.3.3 Symbolic Calculations with Some Other Tools . . . . . . . . . . 112
5.4 Making Our Own Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.4.1 A Naive Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
5.4.2 A Module for Vertical Motion . . . . . . . . . . . . . . . . . . . . . . . . 115
5.4.3 Module or Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.5 Files: Read and Write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
5.6 Measuring Execution Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
5.6.1 The timeit Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
5.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
6 Computing Integrals and Testing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
6.1 Basic Ideas of Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . 132
6.2 The Composite Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.2.1 The General Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
6.2.2 A General Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
6.2.3 A Specific Implementation: What’s the Problem? . . . . . . . . 139
6.3 The Composite Midpoint Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.3.1 The General Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
6.3.2 A General Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
6.3.3 Comparing the Trapezoidal and the Midpoint Methods . . . 145
6.4 Vectorizing the Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
6.4.1 Vectorizing the Midpoint Rule . . . . . . . . . . . . . . . . . . . . . . . . 146
6.4.2 Vectorizing the Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . . . 147
6.4.3 Speed up Gained with Vectorization . . . . . . . . . . . . . . . . . . . 148
6.5 Rate of Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
6.6 Testing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
6.6.1 Problems with Brief Testing Procedures . . . . . . . . . . . . . . . . 150
6.6.2 Proper Test Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
