Docsity

Python Scripting For Computational Science, Manuais, Projetos, Pesquisas de Informática

Pré-visualização parcial do texto

Baixe Python Scripting For Computational Science e outras Manuais, Projetos, Pesquisas em PDF para Informática, somente na Docsity!

Texts in Computational Science

and Engineering

Editors

Timothy J. Barth

Michael Griebel

David E. Keyes

Risto M. Nieminen

Dirk Roose

Tamar Schlick

Hans Petter Langtangen

Simula Research Laboratory Martin Linges vei 17, Fornebu P.O. Box 134 1325 Lysaker, Norway [email protected]

On leave from: Department of Informatics University of Oslo P.O. Box 1080 Blindern 0316 Oslo, Norway http://folk.uio.no/hpl

The author of this book has received financial support from the NFF – Norsk faglitterær forfatter- og oversetterforening.

ISBN 978-3-540-73915-9 e-ISBN 978-3-540-73916-

DOI 10.1007/978-3-540-73916-

Texts in Computational Science and Engineering ISSN 1611-

Library of Congress Control Number: 2007940499

Mathematics Subject Classification (2000): 65Y99, 68N01, 68N15, 68N19, 68N30, 97U50, 97U

© 2008, 2006, 2004 Springer-Verlag Berlin Heidelberg

This work is subject to copyright. All rights are reserved, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broad- casting, reproduction on microfilm or in any other way, and storage in data banks. Duplication of this publication or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, in its current version, and permission for use must always be obtained from Springer. Violations are liable to prosecution under the German Copyright Law.

The use of general descriptive names, registered names, trademarks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. Typesetting: by the author using a Springer TEX macro package Cover design: WMX Design GmbH, Heidelberg Production: LE-TEX Jelonek, Schmidt & Vöckler GbR, Leipzig

Printed on acid-free paper

9 8 7 6 5 4 3 2 1

springer.com

Preface to the Third Edition

Numerous readers of the second edition have notified me about misprints and possible improvements of the text and the associated computer codes. The resulting modifications have been incorporated in this new edition and its accompanying software. The major change between the second and third editions, however, is caused by the new implementation of Numerical Python, now called numpy. The new numpy package encourages a slightly different syntax compared to the old Numeric implementation, which was used in the previous editions. Since Numerical Python functionality appears in a lot of places in the book, there are hence a huge number of updates to the new suggested numpy syntax, especially in Chapters 4, 9, and 10. The second edition was based on Python version 2.3, while the third edition contains updates for version 2.5. Recent Python features, such as generator expressions (Chapter 8.9.4), Ctypes for interfacing shared libraries in C (Chapter 5.2.2), the with statement (Chapter 3.1.4), and the subprocess module for running external processes (Chapter 3.1.3) have been exemplified to make the reader aware of new tools. Regarding Chapter 3.1.3, os.system is not used in the book anymore, instead we recommend the commands or subprocess modules. Chapter 4.4.4 is new and gives a taste of symbolic mathematics in Python. Chapters 5 and 10 have been extended with new material. For example, F2PY and the Instant tool are very convenient for interfacing C code, and this topic is treated in detail in Chapters 5.2.2, 10.1.1, and 10.1.2 in the new edition. Installation of Python itself and the many add-on modules have become increasingly simpler over the years with setup.py scripts, which has made it natural to simplify the descriptions in Appendix A. The py4cs package with software tools associated with this book has un- dergone a major revision and extension, and the package is now maintained under the name scitools and distributed separately. The name py4cs is still offered as a nickname for scitools to make old scripts work. The new scitools package is backward compatible with py4cs from the second edition. Several people has helped me with preparing the new edition. In par- ticular, the substantial efforts of Pearu Peterson, Ilmar Wilbers, Johannes H. Ring, and Rolv E. Bredesen are highly appreciated. The Springer staff has, as always, been a great pleasure to work with. Special thanks go to Martin Peters, Thanh-Ha Le Thi, and Andrea K¨ohler for their extensive help with this and other book projects.

Oslo, September 2007 Hans Petter Langtangen

Preface to the First Edition

The primary purpose of this book is to help scientists and engineers work- ing intensively with computers to become more productive, have more fun, and increase the reliability of their investigations. Scripting in the Python programming language can be a key tool for reaching these goals [27,29]. The term scripting means different things to different people. By scripting I mean developing programs of an administering nature, mostly to organize your work, using languages where the abstraction level is higher and program- ming is more convenient than in Fortran, C, C++, or Java. Perl, Python, Ruby, Scheme, and Tcl are examples of languages supporting such high-level programming or scripting. To some extent Matlab and similar scientific com- puting environments also fall into this category, but these environments are mainly used for computing and visualization with built-in tools, while script- ing aims at gluing a range of different tools for computing, visualization, data analysis, file/directory management, user interfaces, and Internet communi- cation. So, although Matlab is perhaps the scripting language of choice in computational science today, my use of the term scripting goes beyond typi- cal Matlab scripts. Python stands out as the language of choice for scripting in computational science because of its very clean syntax, rich modulariza- tion features, good support for numerical computing, and rapidly growing popularity.

What Scripting is About. The simplest application of scripting is to write short programs (scripts) that automate manual interaction with the com- puter. That is, scripts often glue stand-alone applications and operating sys- tem commands. A primary example is automating simulation and visual- ization: from an effective user interface the script extracts information and generates input files for a simulation program, runs the program, archive data files, prepares input for a visualization program, creates plots and animations, and perhaps performs some data analysis. More advanced use of scripting includes rapid construction of graphical user interfaces (GUIs), searching and manipulating text (data) files, manag- ing files and directories, tailoring visualization and image processing environ- ments to your own needs, administering large sets of computer experiments, and managing your existing Fortran, C, or C++ libraries and applications directly from scripts. Scripts are often considerably faster to develop than the corresponding programs in a traditional language like Fortran, C, C++, or Java, and the code is normally much shorter. In fact, the high-level programming style and tools used in scripts open up new possibilities you would hardly consider as a Fortran or C programmer. Furthermore, scripts are for the most part truly cross-platform, so what you write on Windows runs without modifications

VIII Preface to the First Edition

on Unix and Macintosh, also when graphical user interfaces and operating system interactions are involved. The interest in scripting with Python has exploded among Internet service developers and computer system administrators. However, Python scripting has a significant potential in computational science and engineering (CSE) as well. Software systems such as Maple, Mathematica, Matlab, and S-PLUS/R are primary examples of very popular, widespread tools because of their simple and effective user interface. Python resembles the nature of these interfaces, but is a full-fledged, advanced, and very powerful programming language. With Python and the techniques explained in this book, you can actually create your own easy-to-use computational environment, which mir- rors the working style of Matlab-like tools, but tailored to your own number crunching codes and favorite visualization systems. Scripting enables you to develop scientific software that combines ”the best of all worlds”, i.e., highly different tools and programming styles for accomplishing a task. As a simple example, one can think of using a C++ library for creating a computational grid, a Fortran 77 library for solving partial differential equations on the grid, a C code for visualizing the solution, and Python for gluing the tools together in a high-level program, perhaps with an easy-to-use graphical interface.

Special Features of This Book. The current book addresses applications of scripting in CSE and is tailored to professionals and students in this field. The book differs from other scripting books on the market in that it has a different pedagogical strategy, a different composition of topics, and a different target audience. Practitioners in computational science and engineering seldom have the interest and time to sit down with a pure computer language book and figure out how to apply the new tools to their problem areas. Instead, they want to get quickly started with examples from their own world of applications and learn the tools while using them. The present book is written in this spirit – we dive into simple yet useful examples and learn about syntax and programming techniques during dissection of the examples. The idea is to get the reader started such that further development of the examples towards real-life applications can be done with the aid of online manuals or Python reference books.

Contents. The contents of the book can be briefly sketched as follows. Chap- ter 1 gives an introduction to what scripting is and what it can be good for in a computational science context. A quick introduction to scripting with Python, using examples of relevance to computational scientists and engi- neers, is provided in Chapter 2. Chapter 3 presents an overview of basic Python functionality, including file handling, data structures, functions, and operating system interaction. Numerical computing in Python, with particu- lar focus on efficient array processing, is the subject of Chapter 4. Python can easily call up Fortran, C, and C++ code, which is demonstrated in Chapter 5.

X Preface to the First Edition

Acknowledgements. The author appreciates the constructive comments from Arild Burud, Roger Hansen, and Tom Thorvaldsen on an earlier version of the manuscript. I will in particular thank the anonymous Springer referees of an even earlier version who made very useful suggestions, which led to a major revision and improvement of the book. Sylfest Glimsdal is thanked for his careful reading and detection of many errors in the present version of the book. I will also acknowledge all the input I have received from our enthusiastic team of scripters at Simula Research Laboratory: Are Magnus Bruaset, Xing Cai, Kent-Andre Mardal, Halvard Moe, Ola Skavhaug, Gunnar Staff, Magne Westlie, and ˚Asmund Ødeg˚ard. As always, the prompt support and advice from Martin Peters, Frank Holzwarth, Leonie Kunz, Peggy Glauch, and Thanh-Ha Le Thi at Springer have been essential to complete the book project. Software, updates, and an errata list associated with this book can be found on the Web page http://folk.uio.no/hpl/scripting. From this page you can also download a PDF version of the book. The PDF version is search- able, and references are hyperlinks, thus making it convenient to navigate in the text during software development.

Oslo, April 2004 Hans Petter Langtangen

Table of Contents

  • 1 Introduction
    • 1.1 Scripting versus Traditional Programming
      • 1.1.1 Why Scripting is Useful in Computational Science
      • 1.1.2 Classification of Programming Languages
      • 1.1.3 Productive Pairs of Programming Languages
      • 1.1.4 Gluing Existing Applications
      • 1.1.5 Scripting Yields Shorter Code
      • 1.1.6 Efficiency
      • 1.1.7 Type-Specification (Declaration) of Variables
      • 1.1.8 Flexible Function Interfaces
      • 1.1.9 Interactive Computing
      • 1.1.10 Creating Code at Run Time
      • 1.1.11 Nested Heterogeneous Data Structures
      • 1.1.12 GUI Programming
      • 1.1.13 Mixed Language Programming
      • 1.1.14 When to Choose a Dynamically Typed Language
      • 1.1.15 Why Python?
      • 1.1.16 Script or Program?
    • 1.2 Preparations for Working with This Book
  • 2 Getting Started with Python Scripting
    • 2.1 A Scientific Hello World Script
      • 2.1.1 Executing Python Scripts
      • 2.1.2 Dissection of the Scientific Hello World Script
    • 2.2 Working with Files and Data
      • 2.2.1 Problem Specification
      • 2.2.2 The Complete Code
      • 2.2.3 Dissection
      • 2.2.4 Working with Files in Memory
      • 2.2.5 Array Computing
      • 2.2.6 Interactive Computing and Debugging
      • 2.2.7 Efficiency Measurements
      • 2.2.8 Exercises
    • 2.3 Gluing Stand-Alone Applications
      • 2.3.1 The Simulation Code
      • 2.3.2 Using Gnuplot to Visualize Curves
      • 2.3.3 Functionality of the Script
      • 2.3.4 The Complete Code
      • 2.3.5 Dissection
      • 2.3.6 Exercises
    • 2.4 Conducting Numerical Experiments
      • 2.4.1 Wrapping a Loop Around Another Script
      • 2.4.2 Generating an HTML Report XII Table of Contents
      • 2.4.3 Making Animations
      • 2.4.4 Varying Any Parameter
    • 2.5 File Format Conversion
      • 2.5.1 A Simple Read/Write Script
      • 2.5.2 Storing Data in Dictionaries and Lists
      • 2.5.3 Making a Module with Functions
      • 2.5.4 Exercises
  • 3 Basic Python
    • 3.1 Introductory Topics
      • 3.1.1 Recommended Python Documentation
      • 3.1.2 Control Statements
      • 3.1.3 Running Applications
      • 3.1.4 File Reading and Writing
      • 3.1.5 Output Formatting
    • 3.2 Variables of Different Types
      • 3.2.1 Boolean Types
      • 3.2.2 The None Variable
      • 3.2.3 Numbers and Numerical Expressions
      • 3.2.4 Lists and Tuples
      • 3.2.5 Dictionaries
      • 3.2.6 Splitting and Joining Text
      • 3.2.7 String Operations
      • 3.2.8 Text Processing
      • 3.2.9 The Basics of a Python Class
      • 3.2.10 Copy and Assignment
      • 3.2.11 Determining a Variable’s Type
      • 3.2.12 Exercises
    • 3.3 Functions
      • 3.3.1 Keyword Arguments
      • 3.3.2 Doc Strings
      • 3.3.3 Variable Number of Arguments
      • 3.3.4 Call by Reference
      • 3.3.5 Treatment of Input and Output Arguments
      • 3.3.6 Function Objects
    • 3.4 Working with Files and Directories
      • 3.4.1 Listing Files in a Directory
      • 3.4.2 Testing File Types
      • 3.4.3 Removing Files and Directories
      • 3.4.4 Copying and Renaming Files
      • 3.4.5 Splitting Pathnames
      • 3.4.6 Creating and Moving to Directories
      • 3.4.7 Traversing Directory Trees
      • 3.4.8 Exercises
  • 4 Numerical Computing in Python Table of Contents XIII
    • 4.1 A Quick NumPy Primer
      • 4.1.1 Creating Arrays
      • 4.1.2 Array Indexing
      • 4.1.3 Loops over Arrays
      • 4.1.4 Array Computations
      • 4.1.5 More Array Functionality
      • 4.1.6 Type Testing
      • 4.1.7 Matrix Objects
      • 4.1.8 Exercises
    • 4.2 Vectorized Algorithms
      • 4.2.1 From Scalar to Array in Function Arguments
      • 4.2.2 Slicing
      • 4.2.3 Exercises
    • 4.3 More Advanced Array Computing
      • 4.3.1 Random Numbers
      • 4.3.2 Linear Algebra
      • 4.3.3 Plotting
      • 4.3.4 Example: Curve Fitting
      • 4.3.5 Arrays on Structured Grids
      • 4.3.6 File I/O with NumPy Arrays
      • 4.3.7 Functionality in the Numpyutils Module
      • 4.3.8 Exercises
    • 4.4 Other Tools for Numerical Computations
      • 4.4.1 The ScientificPython Package
      • 4.4.2 The SciPy Package
      • 4.4.3 The Python–Matlab Interface
      • 4.4.4 Symbolic Computing in Python
      • 4.4.5 Some Useful Python Modules
  • 5 Combining Python with Fortran, C, and C++.
    • 5.1 About Mixed Language Programming
      • 5.1.1 Applications of Mixed Language Programming
      • 5.1.2 Calling C from Python
      • 5.1.3 Automatic Generation of Wrapper Code
    • 5.2 Scientific Hello World Examples
      • 5.2.1 Combining Python and Fortran
      • 5.2.2 Combining Python and C
      • 5.2.3 Combining Python and C++ Functions
      • 5.2.4 Combining Python and C++ Classes
      • 5.2.5 Exercises
    • 5.3 A Simple Computational Steering Example
      • 5.3.1 Modified Time Loop for Repeated Simulations
      • 5.3.2 Creating a Python Interface
      • 5.3.3 The Steering Python Script
      • 5.3.4 Equipping the Steering Script with a GUI
    • 5.4 Scripting Interfaces to Large Libraries
  • 6 Introduction to GUI Programming XIV Table of Contents
    • 6.1 Scientific Hello World GUI
      • 6.1.1 Introductory Topics
      • 6.1.2 The First Python/Tkinter Encounter
      • 6.1.3 Binding Events
      • 6.1.4 Changing the Layout
      • 6.1.5 The Final Scientific Hello World GUI
      • 6.1.6 An Alternative to Tkinter Variables
      • 6.1.7 About the Pack Command
      • 6.1.8 An Introduction to the Grid Geometry Manager
      • 6.1.9 Implementing a GUI as a Class
      • 6.1.10 A Simple Graphical Function Evaluator
      • 6.1.11 Exercises
    • 6.2 Adding GUIs to Scripts
      • 6.2.1 A Simulation and Visualization Script with a GUI
      • 6.2.2 Improving the Layout
      • 6.2.3 Exercises
    • 6.3 A List of Common Widget Operations
      • 6.3.1 Frame
      • 6.3.2 Label
      • 6.3.3 Button
      • 6.3.4 Text Entry
      • 6.3.5 Balloon Help
      • 6.3.6 Option Menu
      • 6.3.7 Slider
      • 6.3.8 Check Button
      • 6.3.9 Making a Simple Megawidget
      • 6.3.10 Menu Bar
      • 6.3.11 List Data
      • 6.3.12 Listbox
      • 6.3.13 Radio Button
      • 6.3.14 Combo Box
      • 6.3.15 Message Box
      • 6.3.16 User-Defined Dialogs
      • 6.3.17 Color-Picker Dialogs
      • 6.3.18 File Selection Dialogs
      • 6.3.19 Toplevel
      • 6.3.20 Some Other Types of Widgets
      • 6.3.21 Adapting Widgets to the User’s Resize Actions
      • 6.3.22 Customizing Fonts and Colors
      • 6.3.23 Widget Overview
      • 6.3.24 Exercises
  • 7 Web Interfaces and CGI Programming Table of Contents XV
    • 7.1 Introductory CGI Scripts
      • 7.1.1 Web Forms and CGI Scripts
      • 7.1.2 Generating Forms in CGI Scripts
      • 7.1.3 Debugging CGI Scripts
      • 7.1.4 A General Shell Script Wrapper for CGI Scripts
      • 7.1.5 Security Issues
    • 7.2 Adding Web Interfaces to Scripts
      • 7.2.1 A Class for Form Parameters
      • 7.2.2 Calling Other Programs
      • 7.2.3 Running Simulations
      • 7.2.4 Getting a CGI Script to Work
      • 7.2.5 Using Web Applications from Scripts
      • 7.2.6 Exercises
  • 8 Advanced Python
    • 8.1 Miscellaneous Topics
      • 8.1.1 Parsing Command-Line Arguments
      • 8.1.2 Platform-Dependent Operations
      • 8.1.3 Run-Time Generation of Code
      • 8.1.4 Exercises
    • 8.2 Regular Expressions and Text Processing
      • 8.2.1 Motivation
      • 8.2.2 Special Characters
      • 8.2.3 Regular Expressions for Real Numbers
      • 8.2.4 Using Groups to Extract Parts of a Text
      • 8.2.5 Extracting Interval Limits
      • 8.2.6 Extracting Multiple Matches
      • 8.2.7 Splitting Text
      • 8.2.8 Pattern-Matching Modifiers
      • 8.2.9 Substitution and Backreferences
      • 8.2.10 Example: Swapping Arguments in Function Calls
      • 8.2.11 A General Substitution Script
      • 8.2.12 Debugging Regular Expressions
      • 8.2.13 Exercises
    • 8.3 Tools for Handling Data in Files
      • 8.3.1 Writing and Reading Python Data Structures
      • 8.3.2 Pickling Objects
      • 8.3.3 Shelving Objects
      • 8.3.4 Writing and Reading Zip and Tar Archive Files
      • 8.3.5 Downloading Internet Files
      • 8.3.6 Binary Input/Output
      • 8.3.7 Exercises
    • 8.4 A Database for NumPy Arrays
      • 8.4.1 The Structure of the Database
      • 8.4.2 Pickling
      • 8.4.3 Formatted ASCII Storage
    • 8.4.4 Shelving XVI Table of Contents
    • 8.4.5 Comparing the Various Techniques
  • 8.5 Scripts Involving Local and Remote Hosts
    • 8.5.1 Secure Shell Commands
    • 8.5.2 Distributed Simulation and Visualization
    • 8.5.3 Client/Server Programming
    • 8.5.4 Threads
  • 8.6 Classes
    • 8.6.1 Class Programming
    • 8.6.2 Checking the Class Type
    • 8.6.3 Private Data
    • 8.6.4 Static Data
    • 8.6.5 Special Attributes
    • 8.6.6 Special Methods
    • 8.6.7 Multiple Inheritance
    • 8.6.8 Using a Class as a C-like Structure
    • 8.6.9 Attribute Access via String Names
    • 8.6.10 New-Style Classes
    • 8.6.11 Implementing Get/Set Functions via Properties
    • 8.6.12 Subclassing Built-in Types
    • 8.6.13 Building Class Interfaces at Run Time
    • 8.6.14 Building Flexible Class Interfaces
    • 8.6.15 Exercises
  • 8.7 Scope of Variables
    • 8.7.1 Global, Local, and Class Variables
    • 8.7.2 Nested Functions
    • 8.7.3 Dictionaries of Variables in Namespaces
  • 8.8 Exceptions
    • 8.8.1 Handling Exceptions
    • 8.8.2 Raising Exceptions
  • 8.9 Iterators
    • 8.9.1 Constructing an Iterator
    • 8.9.2 A Pointwise Grid Iterator
    • 8.9.3 A Vectorized Grid Iterator
    • 8.9.4 Generators
    • 8.9.5 Some Aspects of Generic Programming
    • 8.9.6 Exercises
  • 8.10 Investigating Efficiency
    • 8.10.1 CPU-Time Measurements
    • 8.10.2 Profiling Python Scripts
    • 8.10.3 Optimization of Python Code
    • 8.10.4 Case Study on Numerical Efficiency
  • 9 Fortran Programming with NumPy Arrays Table of Contents XVII
    • 9.1 Problem Definition
    • 9.2 Filling an Array in Fortran
      • 9.2.1 The Fortran Subroutine
      • 9.2.2 Building and Inspecting the Extension Module
    • 9.3 Array Storage Issues
      • 9.3.1 Generating an Erroneous Interface
      • 9.3.2 Array Storage in C and Fortran
      • 9.3.3 Input and Output Arrays as Function Arguments
      • 9.3.4 F2PY Interface Files
      • 9.3.5 Hiding Work Arrays
    • 9.4 Increasing Callback Efficiency
      • 9.4.1 Callbacks to Vectorized Python Functions
      • 9.4.2 Avoiding Callbacks to Python
      • 9.4.3 Compiled Inline Callback Functions
    • 9.5 Summary
    • 9.6 Exercises
  • 10 C and C++ Programming with NumPy Arrays
    • 10.1 Automatic Interfacing of C/C++ Code
      • 10.1.1 Using F2PY
      • 10.1.2 Using Instant
      • 10.1.3 Using Weave
    • 10.2 C Programming with NumPy Arrays
      • 10.2.1 The Basics of the NumPy C API
      • 10.2.2 The Handwritten Extension Code
      • 10.2.3 Sending Arguments from Python to C
      • 10.2.4 Consistency Checks
      • 10.2.5 Computing Array Values
      • 10.2.6 Returning an Output Array
      • 10.2.7 Convenient Macros
      • 10.2.8 Module Initialization
      • 10.2.9 Extension Module Template
      • 10.2.10 Compiling, Linking, and Debugging the Module
      • 10.2.11 Writing a Wrapper for a C Function
    • 10.3 C++ Programming with NumPy Arrays
      • 10.3.1 Wrapping a NumPy Array in a C++ Object
      • 10.3.2 Using SCXX
      • 10.3.3 NumPy–C++ Class Conversion
    • 10.4 Comparison of the Implementations
      • 10.4.1 Efficiency
      • 10.4.2 Error Handling
      • 10.4.3 Summary
    • 10.5 Exercises
  • 11 More Advanced GUI Programming XVIII Table of Contents
    • 11.1 Adding Plot Areas in GUIs
      • 11.1.1 The BLT Graph Widget
      • 11.1.2 Animation of Functions in BLT Graph Widgets
      • 11.1.3 Other Tools for Making GUIs with Plots
      • 11.1.4 Exercises
    • 11.2 Event Bindings
      • 11.2.1 Binding Events to Functions with Arguments
      • 11.2.2 A Text Widget with Tailored Keyboard Bindings
      • 11.2.3 A Fancy List Widget
    • 11.3 Animated Graphics with Canvas Widgets
      • 11.3.1 The First Canvas Encounter
      • 11.3.2 Coordinate Systems
      • 11.3.3 The Mathematical Model Class
      • 11.3.4 The Planet Class
      • 11.3.5 Drawing and Moving Planets
      • 11.3.6 Dragging Planets to New Positions
      • 11.3.7 Using Pmw’s Scrolled Canvas Widget
    • 11.4 Simulation and Visualization Scripts
      • 11.4.1 Restructuring the Script
      • 11.4.2 Representing a Parameter by a Class
      • 11.4.3 Improved Command-Line Script
      • 11.4.4 Improved GUI Script
      • 11.4.5 Improved CGI Script
      • 11.4.6 Parameters with Physical Dimensions
      • 11.4.7 Adding a Curve Plot Area
      • 11.4.8 Automatic Generation of Scripts
      • 11.4.9 Applications of the Tools
      • 11.4.10 Allowing Physical Units in Input Files
      • 11.4.11 Converting Input Files to GUIs
  • 12 Tools and Examples
    • 12.1 Running Series of Computer Experiments
      • 12.1.1 Multiple Values of Input Parameters
      • 12.1.2 Implementation Details
      • 12.1.3 Further Applications
    • 12.2 Tools for Representing Functions
      • 12.2.1 Functions Defined by String Formulas
      • 12.2.2 A Unified Interface to Functions
      • 12.2.3 Interactive Drawing of Functions
      • 12.2.4 A Notebook for Selecting Functions
    • 12.3 Solving Partial Differential Equations
      • 12.3.1 Numerical Methods for 1D Wave Equations
      • 12.3.2 Implementations of 1D Wave Equations
      • 12.3.3 Classes for Solving 1D Wave Equations
      • 12.3.4 A Problem Solving Environment
      • 12.3.5 Numerical Methods for 2D Wave Equations
      • 12.3.6 Implementations of 2D Wave Equations Table of Contents XIX
      • 12.3.7 Exercises
  • A Setting up the Required Software Environment
    • A.1 Installation on Unix Systems
      • A.1.1 A Suggested Directory Structure
      • A.1.2 Setting Some Environment Variables
      • A.1.3 Installing Tcl/Tk and Additional Modules
      • A.1.4 Installing Python
      • A.1.5 Installing Python Modules
      • A.1.6 Installing Gnuplot
      • A.1.7 Installing SWIG
      • A.1.8 Summary of Environment Variables
      • A.1.9 Testing the Installation of Scripting Utilities
    • A.2 Installation on Windows Systems
  • B Elements of Software Engineering
    • B.1 Building and Using Modules
      • B.1.1 Single-File Modules
      • B.1.2 Multi-File Modules
      • B.1.3 Debugging and Troubleshooting
    • B.2 Tools for Documenting Python Software
      • B.2.1 Doc Strings
      • B.2.2 Tools for Automatic Documentation
    • B.3 Coding Standards
      • B.3.1 Style Guide
      • B.3.2 Pythonic Programming
    • B.4 Verification of Scripts
      • B.4.1 Automating Regression Tests
      • B.4.2 Implementing a Tool for Regression Tests
      • B.4.3 Writing a Test Script
      • B.4.4 Verifying Output from Numerical Computations
      • B.4.5 Automatic Doc String Testing
      • B.4.6 Unit Testing
    • B.5 Version Control Management
      • B.5.1 Mercurial
      • B.5.2 Subversion
    • B.6 Exercises
  • Bibliography
  • Index
  • Exercise 2.1 Become familiar with the electronic documentation List of Exercises
  • Exercise 2.2 Extend Exercise 2.1 with a loop
  • Exercise 2.3 Find five errors in a script
  • Exercise 2.4 Basic use of control structures
  • Exercise 2.5 Use standard input/output instead of files
  • Exercise 2.6 Read streams of (x, y) pairs from the command line
  • Exercise 2.7 Test for specific exceptions
  • Exercise 2.8 Sum columns in a file
  • Exercise 2.9 Estimate the chance of an event in a dice game
  • Exercise 2.10 Determine if you win or loose a hazard game
  • Exercise 2.11 Generate an HTML report from the simviz1.py script
  • Exercise 2.12 Generate a LATEX report from the simviz1.py script
  • Exercise 2.13 Compute time step values in the simviz1.py script
  • Exercise 2.14 Use Matlab for curve plotting in the simviz1.py script
  • Exercise 2.15 Combine curves from two simulations in one plot
  • Exercise 2.16 Combine two-column data files to a multi-column file
  • Exercise 2.17 Read/write Excel data files in Python
  • Exercise 3.1 Write format specifications in printf-style
  • Exercise 3.2 Write your own function for joining strings
  • Exercise 3.3 Write an improved function for joining strings
  • Exercise 3.4 Never modify a list you are iterating on
  • Exercise 3.5 Make a specialized sort function
  • Exercise 3.6 Check if your system has a specific program
  • Exercise 3.7 Find the paths to a collection of programs
  • Exercise 3.8 Use Exercise 3.7 to improve the simviz1.py script
  • Exercise 3.9 Use Exercise 3.7 to improve the loop4simviz2.py script
  • Exercise 3.10 Find the version number of a utility
  • Exercise 3.11 Automate execution of a family of similar commands
  • Exercise 3.12 Remove temporary files in a directory tree
  • Exercise 3.13 Find old and large files in a directory tree
  • Exercise 3.14 Remove redundant files in a directory tree
  • Exercise 3.15 Annotate a filename with the current date
  • Exercise 3.16 Automatic backup of recently modified files
  • Exercise 3.17 Search for a text in files with certain extensions
  • Exercise 3.18 Search directories for plots and make HTML report
  • Exercise 3.19 Fix Unix/Windows Line Ends
  • Exercise 4.1 Matrix-vector multiply with NumPy arrays
  • Exercise 4.2 Work with slicing and matrix multiplication
  • Exercise 4.3 Assignment and in-place NumPy array modifications
  • Exercise 4.4 Vectorize a constant function