Python for Scientists Python Python is a free, free, open open source, source, easy-to easy-to-us -usee softwa software re tool tool that that offers offers a signific significant ant alternat alternativ ivee to proprietary packages such as Matlab and Mathematica. This book covers everything the working scientist needs to know to start using Python effectively. The author explains scientific Python from scratch, showing how easy it is to implement and test non-trivial mathematical algorithms and guiding the reader through the many freely available add-on modules. A range of examples, relevant to many different fields, illustrate the program’s capabilities. In particular, readers are shown how to use pre-existing legacy code (usually in Fortran77) within the Python environment, thus avoiding avoiding the need to master the original code. Instead of exercises the book contains useful snippets of tested code which the reader can adapt to handle problems in their own field, allowing students and researchers with little computer expertise to get up and running as soon as possible.
Python for Scientists Python Python is a free, free, open open source, source, easy-to easy-to-us -usee softwa software re tool tool that that offers offers a signific significant ant alternat alternativ ivee to proprietary packages such as Matlab and Mathematica. This book covers everything the working scientist needs to know to start using Python effectively. The author explains scientific Python from scratch, showing how easy it is to implement and test non-trivial mathematical algorithms and guiding the reader through the many freely available add-on modules. A range of examples, relevant to many different fields, illustrate the program’s capabilities. In particular, readers are shown how to use pre-existing legacy code (usually in Fortran77) within the Python environment, thus avoiding avoiding the need to master the original code. Instead of exercises the book contains useful snippets of tested code which the reader can adapt to handle problems in their own field, allowing students and researchers with little computer expertise to get up and running as soon as possible.
Python for Scientists JOHN M. STEW ART Department of Applied Mathematics & Theoretical Physics University of Cambridge
University Printing House, Cambridge CB2 8BS, United Kingdom Cambridge University Press is part of the University of Cambridge. It furthers the University’s mission by disseminating knowledge in the pursuit of education, learning and research at the highest international levels of excellence. www.cambridge.org Information on this title: www.cambridge.org/9781107061392 C John M. Stewart 2014
This publication is in copyright. Subject to statutory exception and to the provisions of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. First published 2014 Printed in the United Kingdom by Clays, St Ives plc A catalogue record for this publication is available from the British Library Library of Congress Cataloguing in Publication data Stewart, John, 1943 July 1– Python for scientists / John M. Stewart. pages cm Includes bibliographical references and index. ISBN 978-1-107-06139-2 (hardback) 1. Science – Data processing. 2. Python (Computer program language) Q183.9.S865 2014 005.13 3 – dc23 2014010571
I. Title.
ISBN 978-1-107-06139-2 Hardback ISBN 978-1-107-68642-7 Paperback Cambridge University Press has no responsibility for the persistence or accuracy of URLs for external or third-party internet websites referred to in this publication, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate.
Contents
Preface
page xi
1
Introduction 1.1 Scientific software 1.2 The plan of this book 1.3 Can Python compete with compiled languages? 1.4 Limitations of this book 1.5 Installing Python and add-ons
2
Getting started with IPython 2.1 Generalities 2.2 Tab completion 2.3 Introspection 2.4 History 2.5 Magic commands 2.6 The magic %run command
11 11 12 12 13 14 15
3
A short Python tutorial 3.1 Typing Python 3.2 Objects and identifiers 3.3 Numbers 3.3.1 Integers 3.3.2 Real numbers 3.3.3 Boolean numbers 3.3.4 Complex numbers 3.4 Namespaces and modules 3.5 Container objects 3.5.1 Lists 3.5.2 List indexing 3.5.3 List slicing 3.5.4 List mutability 3.5.5 Tuples 3.5.6 Strings 3.5.7 Dictionaries
20 20 21 23 23 23 24 25 25 27 27 28 28 30 31 31 32
1 1 4 8 9 9
vi
Contents
3.6 3.7
3.8
3.9 3.10 3.11 4
Python if statements Loop constructs 3.7.1 The Python for loop 3.7.2 The Python continue statement 3.7.3 The Python break statement 3.7.4 List comprehensions 3.7.5 Python while loops Functions 3.8.1 Syntax and scope 3.8.2 Positional arguments 3.8.3 Keyword arguments 3.8.4 Variable number of positional arguments 3.8.5 Variable number of keyword arguments 3.8.6 The Python print function 3.8.7 Anonymous functions Introduction to Python classes The structure of Python Prime numbers: a worked example
Numpy 4.1 One-dimensional arrays 4.1.1 Ab initio constructors 4.1.2 Look alike constructors 4.1.3 Arithmetical operations on vectors 4.1.4 Ufuncs 4.1.5 Logical operations on vectors 4.2 Two-dimensional arrays 4.2.1 Broadcasting 4.2.2 Ab initio constructors 4.2.3 Look alike constructors 4.2.4 Operations on arrays and ufuncs 4.3 Higher-dimensional arrays 4.4 Domestic input and output 4.4.1 Discursive output and input 4.4.2 Numpy text output and input 4.4.3 Numpy binary output and input 4.5 Foreign input and output 4.5.1 Small amounts of data 4.5.2 Large amounts of data 4.6 Miscellaneous ufuncs 4.6.1 Maxima and minima 4.6.2 Sums and products 4.6.3 Simple statistics 4.7 Polynomials
32 33 34 35 35 36 37 37 38 41 41 41 42 42 44 45 47 48 52 54 54 55 56 58 59 62 63 64 65 66 66 67 67 69 69 70 70 71 71 71 72 72 73
Contents
4.8
4.9
4.7.1 Converting data to coe fficients 4.7.2 Converting coefficients to data 4.7.3 Manipulating polynomials in coefficient form Linear algebra 4.8.1 Basic operations on matrices 4.8.2 More specialized operations on matrices 4.8.3 Solving linear systems of equations More numpy and beyond 4.9.1 Scipy 4.9.2 Scikits
vii
73 73 74 74 74 75 75 76 76 77
5
Two-dimensional graphics 5.1 Introduction 5.2 Getting started: simple figures 5.2.1 Front-ends 5.2.2 Back-ends 5.2.3 A simple figure 5.2.4 Interactive controls 5.3 Cartesian plots 5.3.1 The matplotlib plot function 5.3.2 Curve styles 5.3.3 Marker styles 5.3.4 Axes, grid, labels and title 5.3.5 A not-so-simple example: partial sums of Fourier series 5.4 Polar plots 5.5 Error bars 5.6 Text and annotations 5.7 Displaying mathematical formulae 5.7.1 Non-LATEX users 5.7.2 LATEX users 5.7.3 Alternatives for LATEX users 5.8 Contour plots 5.9 Compound figures 5.9.1 Multiple figures 5.9.2 Multiple plots 5.10 Animations 5.10.1 In situ animations 5.10.2 Movies 5.11 Mandelbrot sets: a worked example
79 79 80 80 80 81 82 83 83 83 84 85 86 87 88 89 90 90 91 92 92 95 95 96 98 99 100 102
6
Three-dimensional graphics 6.1 Introduction 6.1.1 Three-dimensional data sets 6.1.2 The reduction to two dimensions
107 107 107 108
viii
Contents
6.2 6.3
6.4
6.5
6.6
Visualization software A three-dimensional curve 6.3.1 Visualizing the curve with mplot3d 6.3.2 Visualizing the curve with mlab A simple surface 6.4.1 Visualizing the simple surface with mplot3d 6.4.2 Visualizing the simple surface with mlab A parametrically defined surface 6.5.1 Visualizing Enneper’s surface using mplot3d 6.5.2 Visualizing Enneper’s surface using mlab Three-dimensional visualization of a Julia set
109 110 111 112 114 114 116 117 117 119 120
7
Ordinary differential equations 7.1 Initial value problems 7.2 Basic concepts 7.3 The odeint function 7.3.1 Theoretical background 7.3.2 Practical usage 7.4 Two-point boundary value problems 7.4.1 Introduction 7.4.2 Formulation of the boundary value problem 7.4.3 A simple example 7.4.4 A linear eigenvalue problem 7.4.5 A non-linear boundary value problem 7.5 Delay diff erential equations 7.5.1 A model equation 7.5.2 More general equations and their numerical solution 7.5.3 The logistic equation 7.5.4 The Mackey–Glass equation 7.6 Stochastic diff erential equations 7.6.1 The Wiener process 7.6.2 The Itoˆ calculus 7.6.3 Itoˆ and Stratanovich stochastic integrals 7.6.4 Numerical solution of stochastic diff erential equations
122 122 122 125 125 127 132 132 133 135 136 138 142 143 144 145 147 150 150 152 155 156
8
Partial differential equations: a pseudospectral approach 8.1 Initial-boundary value problems 8.2 Method of lines 8.3 Spatial derivatives via finite diff erencing 8.4 Spatial derivatives by spectral techniques for periodic problems 8.5 The IVP for spatially periodic problems 8.6 Spectral techniques for non-periodic problems 8.7 An introduction to f2py 8.7.1 Simple examples with scalar arguments
163 163 164 164 165 167 170 172 172
Contents
8.8 8.9
9
8.7.2 Vector arguments 8.7.3 A simple example with multi-dimensional arguments 8.7.4 Undiscussed features of f2py A real-life f2py example Worked example: Burgers’ equation 8.9.1 Boundary conditions: the traditional approach 8.9.2 Boundary conditions: the penalty approach
Case study: multigrid 9.1 The one-dimensional case 9.1.1 Linear elliptic equations 9.1.2 Smooth and rough modes 9.2 The tools of multigrid 9.2.1 Relaxation methods 9.2.2 Residual and error 9.2.3 Prolongation and restriction 9.3 Multigrid schemes 9.3.1 The two-grid algorithm 9.3.2 The V-cycle scheme 9.3.3 The full multigrid scheme (FMG) 9.4 A simple Python multigrid implementation 9.4.1 Utility functions 9.4.2 Smoothing functions 9.4.3 Multigrid functions
ix
174 175 176 177 178 179 179 184 185 185 186 186 186 189 190 191 192 193 194 195 196 197 199
Appendix A Installing a Python environment A.1 Installing Python packages A.2 Communicating with Python A.2.1 Editors for programming A.2.2 The IPython-editor interaction A.2.3 The two windows approach A.2.4 Calling the editor from within IPython A.2.5 Calling IPython from within the editor A.2.6 The IPython pager A.3 The Python Package Index
205 205 206 206 207 207 208 208 208 209
Appendix B Fortran77 subroutines for pseudospectral methods References Index
210 216 218
Preface
I have used computers as an aid to scientific research for over 40 years. During that time, hardware has become cheap, fast and powerful. However, software relevant to the working scientist has become progressively more complicated. My favourite textbooks on Fortran90 and C ++ run to 1200 and 1600 pages respectively. And then we need documentation on mathematics libraries and graphics packages. A newcomer going down this route is going to have to invest significant amounts of time and energy in order to write useful programmes. This has led to the emergence of “scientific packages” such as Matlab or Mathematica which avoid the complications of compiled languages, separate mathematics libraries and graphics packages. I have used them and found them very convenient for executing the tasks envisaged by their developers. However, I also found them very di fficult to extend beyond these boundaries, and so I looked for alternative approaches. Some years ago, a computer science colleague suggested that I should take a look at Python. At that time, it was clear that Python had great potential but a very flaky implementation. It was however free and open-source, and was attracting what has turned out to be a very e ff ective army of developers. More recently, their e ff orts have coordinated to produce a formidable package consisting of a small core language surrounded by a wealth of add-on libraries or modules. A select group of these can and do replicate the facilities of the conventional scientific packages. More importantly an informed, intelligent user of Python and its modules can carry out major projects usually entrusted to dedicated programmers using Fortran, C etc. There is a marginal loss of execution speed, but this is more than compensated for by the vastly telescoped development time. The purpose of this book is to explain to working scientists the utility of this relatively unknown resource. Most scientists will have some computer familiarity and programming awareness, although not necessarily with Python and I shall take advantage of this. Therefore, unlike many books which set out to “teach” a language, this one is not just a brisk trot through the reference manuals. Python has many powerful but unfamiliar facets, and these need more explanation than the familiar ones. In particular, if you encounter in this text a reference to the “beginner” or the “unwary”, it signifies a point which is not made clear in the documentation, and has caught out this author at least once. The first six chapters, plus Appendix A, cover almost everything the working scientist needs to know in order to get started in using Python e ff ectively. My editor and some referees suggested that I should devote the second half of the book to problems in
xii
Preface
a particular field. This would have led to a series of books, “Python for Biochemists”, “Python for Crystallographers”,. . . , all with a common first half. Instead I have chosen to cover just three topics, which however, should be far more widely applicable in many diff erent fields. Chapter 7 covers four radically di ff erent types of ordinary di ff erential equations and shows how to use the various relevant black boxes, which are often Python wrappers around tried and trusted Fortran codes. The next chapter while ostensibly about pseudospectral approaches to evolutionary partial di ff erential equations, actually covers a topic of great utility to many scientists, namely how to reuse legacy code, usually written in Fortran77 within Python at Fortran-like speeds, without understanding Fortran. The final chapter about solving very large linear systems via multigrid is also a case history in how to use object-oriented programming meaningfully in a scientific context. If readers look carefully and critically at these later chapters, they should gain the practical expertise to handle problems in their own field. Acknowledgments are due to the many Python developers who have produced and documented a very useful tool, and also to the very many who have published code snippets on the web, a great aid to the tyro, such as this author. Many of my colleagues have off ered valuable advice. Des Higham generously consented to my borrowing his ideas for the last quarter of Chapter 7. I am especially grateful to Oliver Rinne who read carefully and critically an early draft. At Cambridge University Press, my Production Editor, Jennifer Murphy and my Copy Editor, Anne Rix have exhibited their customary expertise. Last but not least I thank the Department of Applied Mathematics and Theoretical Physics, Cambridge for continuing to o ff er me office space after my retirement, which has facilitated the production of this book. Writing a serious book is not a trivial task and so I am rather more than deeply grateful for the near-infinite patience of Mary, the “Python-widow”, which made this book possible!