Hans Petter Langtangen9783642093159, 3642093159
Table of contents :
1.1.1 Why Scripting is Useful in Computational Science……Page 21
1.1.2 Classification of Programming Languages……Page 24
1.1.3 Productive Pairs of Programming Languages……Page 25
1.1.4 Gluing Existing Applications……Page 26
1.1.5 Scripting Yields Shorter Code……Page 27
1.1.6 Efficiency……Page 28
1.1.7 Type-Specification (Declaration) of Variables……Page 29
1.1.8 Flexible Function Interfaces……Page 31
1.1.9 Interactive Computing……Page 32
1.1.10 Creating Code at Run Time……Page 33
1.1.11 Nested Heterogeneous Data Structures……Page 34
1.1.12 GUI Programming……Page 36
1.1.13 Mixed Language Programming……Page 37
1.1.14 When to Choose a Dynamically Typed Language……Page 39
1.1.15 Why Python?……Page 40
1.1.16 Script or Program?……Page 41
1.2 Preparations for Working with This Book……Page 42
2.1 A Scientific Hello World Script……Page 47
2.1.1 Executing Python Scripts……Page 48
2.1.2 Dissection of the Scientific Hello World Script……Page 49
2.2.1 Problem Specification……Page 52
2.2.3 Dissection……Page 53
2.2.4 Working with Files in Memory……Page 56
2.2.5 Efficiency Measurements……Page 57
2.2.6 Exercises……Page 58
2.3 Automating Simulation and Visualization……Page 60
2.3.1 The Simulation Code……Page 61
2.3.2 Using Gnuplot to Visualize Curves……Page 63
2.3.3 Functionality of the Script……Page 64
2.3.4 The Complete Code……Page 65
2.3.5 Dissection……Page 67
2.3.6 Exercises……Page 69
2.4 Conducting Numerical Experiments……Page 72
2.4.1 Wrapping a Loop Around Another Script……Page 73
2.4.2 Generating an HTML Report……Page 74
2.4.3 Making Animations……Page 76
2.4.4 Varying Any Parameter……Page 77
2.5 File Format Conversion……Page 80
2.5.1 The First Version of the Script……Page 81
2.5.2 The Second Version of the Script……Page 82
3.1.1 Recommended Python Documentation……Page 85
3.1.2 Testing Statements in the Interactive Shell……Page 86
3.1.3 Control Statements……Page 88
3.1.4 Running an Application……Page 89
3.1.5 File Reading and Writing……Page 90
3.1.6 Output Formatting……Page 92
3.1.7 Exercises……Page 93
3.2.1 Boolean Types……Page 94
3.2.2 The None Variable……Page 95
3.2.3 Numbers and Numerical Expressions……Page 96
3.2.4 Lists and Tuples……Page 97
3.2.5 Dictionaries……Page 104
3.2.6 Splitting and Joining Text……Page 107
3.2.7 String Operations……Page 108
3.2.8 Text Processing……Page 109
3.2.9 The Basics of a Python Class……Page 111
3.2.10 Determining a Variable’s Type……Page 113
3.2.11 Exercises……Page 116
3.3 Functions……Page 121
3.3.1 Keyword Arguments……Page 122
3.3.3 Variable Number of Arguments……Page 123
3.3.4 Call by Reference……Page 124
3.3.5 Treatment of Input and Output Arguments……Page 126
3.3.6 Function Objects……Page 127
3.4 Working with Files and Directories……Page 128
3.4.2 Testing File Types……Page 129
3.4.3 Copying and Renaming Files……Page 130
3.4.5 Splitting Pathnames……Page 131
3.4.7 Traversing Directory Trees……Page 132
3.4.8 Exercises……Page 135
4 Numerical Computing in Python……Page 141
4.1.1 Creating Arrays……Page 143
4.1.2 Array Indexing……Page 144
4.1.3 Array Computations……Page 146
4.1.4 Type Testing……Page 147
4.1.5 Hidden Temporary Arrays……Page 149
4.1.6 Exercises……Page 150
4.2 Vectorized Algorithms……Page 151
4.2.1 Arrays as Function Arguments……Page 152
4.2.2 Slicing……Page 153
4.2.3 Remark on Efficiency……Page 154
4.2.4 Exercises……Page 156
4.3.1 Random Numbers……Page 157
4.3.3 The Gnuplot Module……Page 159
4.3.4 Example: Curve Fitting……Page 162
4.3.5 Arrays on Structured Grids……Page 164
4.3.6 File I/O with NumPy Arrays……Page 166
4.3.7 Reading and Writing Tables with NumPy Arrays……Page 167
4.3.8 Functionality in the Numpytools Module……Page 170
4.3.9 Exercises……Page 172
4.4.1 The ScientificPython Package……Page 176
4.4.2 The SciPy Package……Page 181
4.4.3 The Python–Matlab Interface……Page 185
4.4.4 Some Useful Python Modules……Page 186
4.5 A Database for NumPy Arrays……Page 187
4.5.1 The Structure of the Database……Page 188
4.5.2 Pickling……Page 190
4.5.3 Formatted ASCII Storage……Page 191
4.5.4 Shelving……Page 192
4.5.5 Comparing the Various Techniques……Page 193
5.1.1 Applications of Mixed Language Programming……Page 195
5.1.2 Calling C from Python……Page 196
5.1.3 Automatic Generation of Wrapper Code……Page 198
5.2 Scientific Hello World Examples……Page 200
5.2.1 Combining Python and Fortran……Page 201
5.2.2 Combining Python and C……Page 206
5.2.3 Combining Python and C++ Functions……Page 212
5.2.4 Combining Python and C++ Classes……Page 214
5.2.5 Exercises……Page 217
5.3 A Simple Computational Steering Example……Page 218
5.3.1 Modified Time Loop for Repeated Simulations……Page 219
5.3.2 Creating a Python Interface……Page 220
5.3.3 The Steering Python Script……Page 222
5.3.4 Equipping the Steering Script with a GUI……Page 225
5.4 Scripting Interfaces to Large Libraries……Page 227
6.1.1 Introductory Topics……Page 231
6.1.2 The First Python/Tkinter Encounter……Page 234
6.1.3 Binding Events……Page 237
6.1.4 Changing the Layout……Page 238
6.1.5 The Final Scientific Hello World GUI……Page 242
6.1.6 An Alternative to Tkinter Variables……Page 244
6.1.7 About the Pack Command……Page 245
6.1.8 An Introduction to the Grid Geometry Manager……Page 247
6.1.9 Implementing a GUI as a Class……Page 249
6.1.10 A Simple Graphical Function Evaluator……Page 251
6.1.11 Exercises……Page 253
6.2.1 A Simulation and Visualization Script with a GUI……Page 255
6.2.2 Improving the Layout……Page 258
6.2.3 Exercises……Page 261
6.3 A List of Common Widget Operations……Page 262
6.3.2 Label……Page 265
6.3.4 Text Entry……Page 267
6.3.5 Balloon Help……Page 269
6.3.7 Slider……Page 270
6.3.9 Making a Simple Megawidget……Page 271
6.3.10 Menu Bar……Page 272
6.3.11 List Data……Page 274
6.3.12 Listbox……Page 275
6.3.13 Radio Button……Page 278
6.3.14 Combo Box……Page 279
6.3.15 Message Box……Page 280
6.3.16 User-Defined Dialogs……Page 282
6.3.17 Color-Picker Dialogs……Page 283
6.3.18 File Selection Dialogs……Page 286
6.3.19 Toplevel……Page 287
6.3.20 Some Other Types of Widgets……Page 288
6.3.21 Adapting Widgets to the User’s Resize Actions……Page 289
6.3.22 Customizing Fonts and Colors……Page 291
6.3.23 Widget Overview……Page 293
6.3.24 Exercises……Page 295
7 Web Interfaces and CGI Programming……Page 301
7.1.1 Web Forms and CGI Scripts……Page 302
7.1.2 Generating Forms in CGI Scripts……Page 305
7.1.3 Debugging CGI Scripts……Page 307
7.1.4 Security Issues……Page 309
7.1.5 A General Shell Script Wrapper for CGI Scripts……Page 310
7.2.1 A Class for Form Parameters……Page 312
7.2.2 Calling Other Programs……Page 315
7.2.3 Running Simulations……Page 316
7.2.4 Getting a CGI Script to Work……Page 317
7.2.5 Using Web Services from Scripts……Page 320
7.2.6 Exercises……Page 322
8.1.1 Parsing Command-Line Arguments……Page 325
8.1.2 Platform-Dependent Operations……Page 328
8.1.3 Run-Time Generation of Code……Page 329
8.1.4 Exercises……Page 330
8.2 Regular Expressions and Text Processing……Page 331
8.2.1 Motivation……Page 332
8.2.2 Special Characters……Page 335
8.2.3 Regular Expressions for Real Numbers……Page 336
8.2.5 Extracting Interval Limits……Page 340
8.2.6 Extracting Multiple Matches……Page 345
8.2.7 Splitting Text……Page 349
8.2.8 Pattern-Matching Modifiers……Page 350
8.2.10 Example: Swapping Arguments in Function Calls……Page 353
8.2.11 A General Substitution Script……Page 357
8.2.12 Debugging Regular Expressions……Page 358
8.2.13 Exercises……Page 360
8.3.1 Writing and Reading Python Data Structures……Page 370
8.3.2 Pickling Objects……Page 372
8.3.3 Shelving Objects……Page 374
8.3.4 Writing and Reading Zip Archive Files……Page 375
8.3.5 Downloading Internet Files……Page 376
8.3.6 Binary Input/Output……Page 377
8.4 Scripts Involving Local and Remote Hosts……Page 379
8.4.1 Secure Shell Commands……Page 380
8.4.2 Distributed Simulation and Visualization……Page 381
8.4.4 Threads……Page 383
8.5.1 Class Programming……Page 385
8.5.2 Checking the Class Type……Page 389
8.5.4 Static Data……Page 390
8.5.6 Special Methods……Page 391
8.5.8 Using a Class as a C-like Structure……Page 393
8.5.9 Attribute Access via String Names……Page 394
8.5.10 Example: Turning String Formulas into Functions……Page 395
8.5.11 Example: Class for Structured Grids……Page 396
8.5.13 Implementing Get/Set Functions via Properties……Page 399
8.5.14 Subclassing Built-in Types……Page 401
8.5.15 Copy and Assignment……Page 403
8.5.16 Building Class Interfaces at Run Time……Page 406
8.5.17 Building Flexible Class Interfaces……Page 409
8.5.18 Exercises……Page 416
8.6.1 Global, Local, and Class Variables……Page 419
8.6.2 Nested Functions……Page 421
8.6.3 Dictionaries of Variables in Namespaces……Page 422
8.7 Exceptions……Page 424
8.7.1 Handling Exceptions……Page 425
8.7.2 Raising Exceptions……Page 426
8.8.1 Constructing an Iterator……Page 427
8.8.2 A Pointwise Grid Iterator……Page 429
8.8.3 A Vectorized Grid Iterator……Page 433
8.8.4 Generators……Page 435
8.8.5 Some Aspects of Generic Programming……Page 437
8.9.1 CPU-Time Measurements……Page 441
8.9.2 Profiling Python Scripts……Page 444
8.9.3 Optimization of Python Code……Page 445
9.1 Problem Definition……Page 449
9.2.1 The Fortran Subroutine……Page 452
9.2.2 Building and Inspecting the Extension Module……Page 453
9.3.1 Generating an Erroneous Interface……Page 455
9.3.2 Array Storage in C and Fortran……Page 457
9.3.3 Input and Output Arrays as Function Arguments……Page 458
9.3.4 F2PY Interface Files……Page 464
9.3.5 Hiding Work Arrays……Page 468
9.4.1 Callbacks to Vectorized Python Functions……Page 469
9.4.2 Avoiding Callbacks to Python……Page 472
9.4.3 Compiled Inline Callback Functions……Page 473
9.6.1 Exercises……Page 476
10.1 C Programming with NumPy Arrays……Page 481
10.1.1 Basics of the NumPy C API……Page 482
10.1.2 The Handwritten Extension Code……Page 484
10.1.3 Sending Arguments from Python to C……Page 485
10.1.4 Consistency Checks……Page 486
10.1.5 Computing Array Values……Page 487
10.1.6 Returning an Output Array……Page 489
10.1.7 Convenient Macros……Page 490
10.1.8 Module Initialization……Page 491
10.1.9 Extension Module Template……Page 492
10.1.10 Compiling, Linking, and Debugging the Module……Page 494
10.1.11 Writing a Wrapper for a C Function……Page 495
10.2.1 Wrapping a NumPy Array in a C++ Object……Page 498
10.2.2 Using SCXX……Page 500
10.2.3 NumPy–C++ Class Conversion……Page 503
10.3.1 Efficiency……Page 512
10.3.2 Error Handling……Page 515
10.3.3 Summary……Page 516
10.3.4 Exercises……Page 517
11.1 Adding Plot Areas in GUIs……Page 523
11.1.1 The BLT Graph Widget……Page 524
11.1.2 Animation of Functions in BLT Graph Widgets……Page 530
11.1.3 Other Tools for Making GUIs with Plots……Page 532
11.1.4 Exercises……Page 534
11.2.1 Binding Events to Functions with Arguments……Page 537
11.2.2 A Text Widget with Tailored Keyboard Bindings……Page 540
11.2.3 A Fancy List Widget……Page 543
11.3 Animated Graphics with Canvas Widgets……Page 546
11.3.1 The First Canvas Encounter……Page 547
11.3.2 Coordinate Systems……Page 548
11.3.3 The Mathematical Model Class……Page 552
11.3.4 The Planet Class……Page 553
11.3.5 Drawing and Moving Planets……Page 555
11.3.6 Dragging Planets to New Positions……Page 556
11.3.7 Using Pmw’s Scrolled Canvas Widget……Page 560
11.4 Tools for Simulation & Visualization Scripts……Page 562
11.4.1 Restructuring the Script……Page 563
11.4.2 Representing a Parameter by a Class……Page 565
11.4.3 Improved Command-Line Script……Page 579
11.4.4 Improved GUI Script……Page 580
11.4.5 Improved CGI Script……Page 581
11.4.6 Parameters with Physical Dimensions……Page 582
11.4.7 Adding a Curve Plot Area……Page 584
11.4.8 Automatic Generation of Scripts……Page 586
11.4.9 Applications of the Tools……Page 587
11.4.10 Allowing Physical Units in Input Files……Page 592
11.4.11 Converting Input Files to GUIs……Page 596
12.1 Running Series of Computer Experiments……Page 599
12.1.1 Multiple Values of Input Parameters……Page 600
12.1.2 Implementation Details……Page 603
12.1.3 Further Applications……Page 608
12.2.1 Functions Defined by String Formulas……Page 612
12.2.2 A Unified Interface to Functions……Page 614
12.2.3 Interactive Drawing of Functions……Page 620
12.2.4 A Notebook for Selecting Functions……Page 626
12.3 Solving Partial Differential Equations……Page 632
12.3.1 Numerical Methods for 1D Wave Equations……Page 633
12.3.2 Implementations of 1D Wave Equations……Page 636
12.3.3 Classes for Solving 1D Wave Equations……Page 642
12.3.4 A Problem Solving Environment……Page 649
12.3.5 Numerical Methods for 2D Wave Equations……Page 655
12.3.6 Implementations of 2D Wave Equations……Page 658
A.1 Installation on Unix Systems……Page 669
A.1.2 Setting Some Environment Variables……Page 670
A.1.3 Installing Tcl/Tk and Additional Modules……Page 671
A.1.4 Installing Python……Page 672
A.1.5 Installing Python Modules……Page 674
A.1.7 Installing SWIG……Page 678
A.1.9 Testing the Installation of Scripting Utilities……Page 679
A.2 Installation on Windows Systems……Page 680
B.1.1 Single-File Modules……Page 685
B.1.2 Multi-File Modules……Page 688
B.1.3 Debugging and Troubleshooting……Page 690
B.2 Tools for Documenting Python Software……Page 692
B.2.1 Doc Strings……Page 693
B.2.2 Tools for Automatic Documentation……Page 694
B.3.1 Style Guide……Page 698
B.3.2 Pythonic Programming……Page 702
B.4.1 Automating Regression Tests……Page 707
B.4.2 Implementing a Tool for Regression Tests……Page 712
B.4.3 Writing a Test Script……Page 715
B.4.4 Verifying Output from Numerical Computations……Page 716
B.4.5 Automatic Doc String Testing……Page 720
B.4.6 Unit Testing……Page 722
B.5 Version Control Management……Page 724
B.5.1 Getting Started with CVS……Page 725
B.6 Exercises……Page 729
Reviews
There are no reviews yet.