书籍信息:
作者: Hans Petter Langtangen
出版社: Springer
副标题: Texts in Computational Science and Engineering
页数: 756
定价: $69.95
ISBN: 9783540739159
简介:
- The goal of this book is to teach computational scientists how to develop tailored, flexible, and human-efficient working environments built from small programs (scripts) written in the easy-to-learn, high-level language Python. The focus is on examples and applications of relevance to computational scientists: gluing existing applications and tools, e.g. for automating simulation, data analysis, and visualization; steering simulations and computational experiments; equipping old programs with graphical user interfaces; making computational Web applications; and creating interactive interfaces with a Maple/Matlab-like syntax to numerical applications in C/C++ or Fortran. In short, scripting with Python makes you much more productive, increases the reliability of your scientific work and lets you have more fun - on Unix, Windows and Macintosh. All the tools and examples in this book are open source codes. The third edition is compatible with the new NumPy implementation and features updated information, correction of errors, and improved associated software tools.
内容目录:
Table of Contents
1 Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Scripting versus Traditional Programming. . . . . . . . . . . . . . . . . 1
1.1.1 Why Scripting is Useful in Computational Science . . . 2
1.1.2 Classification of Programming Languages . . . . . . . . . . 4
1.1.3 Productive Pairs of Programming Languages . . . . . . . 5
1.1.4 Gluing Existing Applications . . . . . . . . . . . . . . . . . . . . . 6
1.1.5 Scripting Yields Shorter Code . . . . . . . . . . . . . . . . . . . . 7
1.1.6 Efficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1.7 Type-Specification (Declaration) of Variables . . . . . . . 9
1.1.8 Flexible Function Interfaces . . . . . . . . . . . . . . . . . . . . . . 11
1.1.9 Interactive Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.1.10 Creating Code at Run Time . . . . . . . . . . . . . . . . . . . . . . 13
1.1.11 Nested Heterogeneous Data Structures . . . . . . . . . . . . . 14
1.1.12 GUI Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.1.13 Mixed Language Programming . . . . . . . . . . . . . . . . . . . 17
1.1.14 When to Choose a Dynamically Typed Language . . . 19
1.1.15 Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.1.16 Script or Program?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.2 Preparations for Working with This Book . . . . . . . . . . . . . . . . . 22
2 Getting Started with Python Scripting . . . . . . . . . . . . 27
2.1 A Scientific Hello World Script . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.1.1 Executing Python Scripts . . . . . . . . . . . . . . . . . . . . . . . . 28
2.1.2 Dissection of the Scientific Hello World Script . . . . . . 29
2.2 Working with Files and Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.2.1 Problem Specification . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.2.2 The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2.3 Dissection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.2.4 Working with Files in Memory. . . . . . . . . . . . . . . . . . . . 36
2.2.5 Array Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.2.6 Interactive Computing and Debugging . . . . . . . . . . . . . 39
2.2.7 Efficiency Measurements . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.2.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.3 Gluing Stand-Alone Applications . . . . . . . . . . . . . . . . . . . . . . . . 46
2.3.1 The Simulation Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
2.3.2 Using Gnuplot to Visualize Curves . . . . . . . . . . . . . . . . 49
2.3.3 Functionality of the Script . . . . . . . . . . . . . . . . . . . . . . . 50
2.3.4 The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.3.5 Dissection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.3.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.4 Conducting Numerical Experiments . . . . . . . . . . . . . . . . . . . . . . 58
2.4.1 Wrapping a Loop Around Another Script . . . . . . . . . . 59
2.4.2 Generating an HTML Report. . . . . . . . . . . . . . . . . . . . . 60
2.4.3 Making Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.4.4 Varying Any Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.5 File Format Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
2.5.1 A Simple Read/Write Script. . . . . . . . . . . . . . . . . . . . . . 66
2.5.2 Storing Data in Dictionaries and Lists . . . . . . . . . . . . . 68
2.5.3 Making a Module with Functions . . . . . . . . . . . . . . . . . 69
2.5.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
3 BasicPython. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
3.1 Introductory Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
3.1.1 Recommended Python Documentation. . . . . . . . . . . . . 74
3.1.2 Control Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
3.1.3 Running Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
3.1.4 File Reading and Writing . . . . . . . . . . . . . . . . . . . . . . . . 78
3.1.5 Output Formatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
3.2 Variables of Different Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.2.1 Boolean Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3.2.2 The None Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3.2.3 Numbers and Numerical Expressions . . . . . . . . . . . . . . 82
3.2.4 Lists and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
3.2.5 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.2.6 Splitting and Joining Text . . . . . . . . . . . . . . . . . . . . . . . 94
3.2.7 String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.2.8 Text Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.2.9 The Basics of a Python Class . . . . . . . . . . . . . . . . . . . . . 98
3.2.10 Copy and Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.2.11 Determining a Variable’s Type. . . . . . . . . . . . . . . . . . . . 104
3.2.12 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
3.3.1 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
3.3.2 Doc Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
3.3.3 Variable Number of Arguments . . . . . . . . . . . . . . . . . . . 112
3.3.4 Call by Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
3.3.5 Treatment of Input and Output Arguments . . . . . . . . 115
3.3.6 Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.4 Working with Files and Directories . . . . . . . . . . . . . . . . . . . . . . . 117
3.4.1 Listing Files in a Directory . . . . . . . . . . . . . . . . . . . . . . . 118
3.4.2 Testing File Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
3.4.3 Removing Files and Directories . . . . . . . . . . . . . . . . . . . 119
3.4.4 Copying and Renaming Files . . . . . . . . . . . . . . . . . . . . . 120
3.4.5 Splitting Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
3.4.6 Creating and Moving to Directories . . . . . . . . . . . . . . . 122
3.4.7 Traversing Directory Trees . . . . . . . . . . . . . . . . . . . . . . . 122
3.4.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
4 Numerical Computing in Python . . . . . . . . . . . . . . . . . . . 131
4.1 A Quick NumPy Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
4.1.1 Creating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
4.1.2 Array Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
4.1.3 Loops over Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
4.1.4 Array Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
4.1.5 More Array Functionality . . . . . . . . . . . . . . . . . . . . . . . . 142
4.1.6 Type Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.1.7 Matrix Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
4.1.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
4.2 Vectorized Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
4.2.1 From Scalar to Array in Function Arguments . . . . . . . 147
4.2.2 Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
4.2.3 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
4.3 More Advanced Array Computing. . . . . . . . . . . . . . . . . . . . . . . . 151
4.3.1 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.3.2 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
4.3.3 Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
4.3.4 Example: Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.3.5 Arrays on Structured Grids . . . . . . . . . . . . . . . . . . . . . . 159
4.3.6 File I/O with NumPy Arrays . . . . . . . . . . . . . . . . . . . . . 163
4.3.7 Functionality in the Numpyutils Module . . . . . . . . . . . 165
4.3.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
4.4 Other Tools for Numerical Computations . . . . . . . . . . . . . . . . . 173
4.4.1 The ScientificPython Package . . . . . . . . . . . . . . . . . . . . 173
4.4.2 The SciPy Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
4.4.3 The Python–Matlab Interface . . . . . . . . . . . . . . . . . . . . 183
4.4.4 Symbolic Computing in Python . . . . . . . . . . . . . . . . . . . 184
4.4.5 Some Useful Python Modules . . . . . . . . . . . . . . . . . . . . . 186
5 Combining Python with Fortran, C, and C++. . . . 189
5.1 About Mixed Language Programming . . . . . . . . . . . . . . . . . . . . 189
5.1.1 Applications of Mixed Language Programming . . . . . . 190
5.1.2 Calling C from Python . . . . . . . . . . . . . . . . . . . . . . . . . . 190
5.1.3 Automatic Generation of Wrapper Code . . . . . . . . . . . 192
5.2 Scientific Hello World Examples . . . . . . . . . . . . . . . . . . . . . . . . . 194
5.2.1 Combining Python and Fortran . . . . . . . . . . . . . . . . . . . 195
5.2.2 Combining Python and C . . . . . . . . . . . . . . . . . . . . . . . . 201
5.2.3 Combining Python and C++ Functions . . . . . . . . . . . . 208
5.2.4 Combining Python and C++ Classes . . . . . . . . . . . . . . 210
5.2.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
5.3 A Simple Computational Steering Example. . . . . . . . . . . . . . . . 215
5.3.1 Modified Time Loop for Repeated Simulations . . . . . . 216
5.3.2 Creating a Python Interface . . . . . . . . . . . . . . . . . . . . . . 217
5.3.3 The Steering Python Script . . . . . . . . . . . . . . . . . . . . . . 218
5.3.4 Equipping the Steering Script with a GUI . . . . . . . . . . 222
5.4 Scripting Interfaces to Large Libraries . . . . . . . . . . . . . . . . . . . . 223
6 Introduction to GUI Programming . . . . . . . . . . . . . . . . . 227