2,249 441 19MB
Pages 1574 Page size 399.402 x 675.78 pts Year 2005
Numerical Recipes in Fortran 77 The Art of Scientific Computing Second Edition
Volume 1 of Fortran Numerical Recipes
William H. Press Harvard-Smithsonian Center for Astrophysics
Saul A. Teukolsky Department of Physics, Cornell University
William T. Vetterling Polaroid Corporation
Brian P. Flannery EXXON Research and Engineering Company
Published by the Press Syndicate of the University of Cambridge The Pitt Building, Trumpington Street, Cambridge CB2 1RP 40 West 20th Street, New York, NY 10011-4211, USA 10 Stamford Road, Oakleigh, Melbourne 3166, Australia c Cambridge University Press 1986, 1992 Copyright except for §13.10, which is placed into the public domain, and except for all other computer programs and procedures, which are c Numerical Recipes Software 1986, 1992, 1997 Copyright All Rights Reserved. Some sections of this book were originally published, in different form, in Computers c American Institute of Physics, 1988–1992. in Physics magazine, Copyright First Edition originally published 1986; Second Edition originally published 1992 as Numerical Recipes in FORTRAN: The Art of Scientific Computing Reprinted with corrections, 1993, 1994, 1995. Reprinted with corrections, 1996, 1997, as Numerical Recipes in Fortran 77: The Art of Scientific Computing (Vol. 1 of Fortran Numerical Recipes) This reprinting is corrected to software version 2.08 Printed in the United States of America Typeset in TEX Without an additional license to use the contained software, this book is intended as a text and reference book, for reading purposes only. A free license for limited use of the software by the individual owner of a copy of this book who personally types one or more routines into a single computer is granted under terms described on p. xxi. See the section “License Information” (pp. xx–xxiii) for information on obtaining more general licenses at low cost. Machine-readable media containing the software in this book, with included licenses for use on a single screen, are available from Cambridge University Press. See the order form at the back of the book, email to “[email protected]” (North America) or “[email protected]” (rest of world), or write to Cambridge University Press, 110 Midland Avenue, Port Chester, NY 10573 (USA), for further information. The software may also be downloaded, with immediate purchase of a license also possible, from the Numerical Recipes Software Web Site (http://www.nr.com). Unlicensed transfer of Numerical Recipes programs to any other format, or to any computer except one that is specifically licensed, is strictly prohibited. Technical questions, corrections, and requests for information should be addressed to Numerical Recipes Software, P.O. Box 243, Cambridge, MA 02238 (USA), email “[email protected]”, or fax 781 863-1739. Library of Congress Cataloging in Publication Data Numerical recipes in Fortran 77 : the art of scientific computing / William H. Press . . . [et al.]. – 2nd ed. Includes bibliographical references (p. ) and index. ISBN 0-521-43064-X 1. Numerical analysis–Computer programs. 2. Science–Mathematics–Computer programs. 3. FORTRAN (Computer program language) I. Press, William H. QA297.N866 1992 519.400285053–dc20 92-8876 A catalog record for this book is available from the British Library. ISBN ISBN ISBN ISBN ISBN ISBN
0 0 0 0 0 0
521 43064 521 57439 521 43721 521 57440 521 57608 521 57607
X 0 0 4 3 5
Volume 1 (this book) Volume 2 Example book in FORTRAN FORTRAN diskette (IBM 3.500) CDROM (IBM PC/Macintosh) CDROM (UNIX)
Contents
Plan of the Two-Volume Edition
xiii
Preface to the Second Edition
xv
Preface to the First Edition License Information
xx
Computer Programs by Chapter and Section 1
Preliminaries
Solution of Linear Algebraic Equations 2.0 Introduction 2.1 Gauss-Jordan Elimination 2.2 Gaussian Elimination with Backsubstitution 2.3 LU Decomposition and Its Applications 2.4 Tridiagonal and Band Diagonal Systems of Equations 2.5 Iterative Improvement of a Solution to Linear Equations 2.6 Singular Value Decomposition 2.7 Sparse Linear Systems 2.8 Vandermonde Matrices and Toeplitz Matrices 2.9 Cholesky Decomposition 2.10 QR Decomposition 2.11 Is Matrix Inversion an N 3 Process?
3
xxiv 1
1.0 Introduction 1.1 Program Organization and Control Structures 1.2 Error, Accuracy, and Stability
2
xviii
1 5 18
22 22 27 33 34 42 47 51 63 82 89 91 95
Interpolation and Extrapolation
99
3.0 Introduction 3.1 Polynomial Interpolation and Extrapolation 3.2 Rational Function Interpolation and Extrapolation 3.3 Cubic Spline Interpolation 3.4 How to Search an Ordered Table 3.5 Coefficients of the Interpolating Polynomial 3.6 Interpolation in Two or More Dimensions
99 102 104 107 110 113 116
v
vi
4
Contents
Integration of Functions 4.0 Introduction 4.1 Classical Formulas for Equally Spaced Abscissas 4.2 Elementary Algorithms 4.3 Romberg Integration 4.4 Improper Integrals 4.5 Gaussian Quadratures and Orthogonal Polynomials 4.6 Multidimensional Integrals
5
Evaluation of Functions 5.0 Introduction 5.1 Series and Their Convergence 5.2 Evaluation of Continued Fractions 5.3 Polynomials and Rational Functions 5.4 Complex Arithmetic 5.5 Recurrence Relations and Clenshaw’s Recurrence Formula 5.6 Quadratic and Cubic Equations 5.7 Numerical Derivatives 5.8 Chebyshev Approximation 5.9 Derivatives or Integrals of a Chebyshev-approximated Function 5.10 Polynomial Approximation from Chebyshev Coefficients 5.11 Economization of Power Series 5.12 Pad´e Approximants 5.13 Rational Chebyshev Approximation 5.14 Evaluation of Functions by Path Integration
6
Special Functions 6.0 Introduction 6.1 Gamma Function, Beta Function, Factorials, Binomial Coefficients 6.2 Incomplete Gamma Function, Error Function, Chi-Square Probability Function, Cumulative Poisson Function 6.3 Exponential Integrals 6.4 Incomplete Beta Function, Student’s Distribution, F-Distribution, Cumulative Binomial Distribution 6.5 Bessel Functions of Integer Order 6.6 Modified Bessel Functions of Integer Order 6.7 Bessel Functions of Fractional Order, Airy Functions, Spherical Bessel Functions 6.8 Spherical Harmonics 6.9 Fresnel Integrals, Cosine and Sine Integrals 6.10 Dawson’s Integral 6.11 Elliptic Integrals and Jacobian Elliptic Functions 6.12 Hypergeometric Functions
7
Random Numbers 7.0 Introduction 7.1 Uniform Deviates
123 123 124 130 134 135 140 155
159 159 159 163 167 171 172 178 180 184 189 191 192 194 197 201
205 205 206 209 215 219 223 229 234 246 248 252 254 263
266 266 267
Contents
7.2 Transformation Method: Exponential and Normal Deviates 7.3 Rejection Method: Gamma, Poisson, Binomial Deviates 7.4 Generation of Random Bits 7.5 Random Sequences Based on Data Encryption 7.6 Simple Monte Carlo Integration 7.7 Quasi- (that is, Sub-) Random Sequences 7.8 Adaptive and Recursive Monte Carlo Methods
8
Sorting 8.0 Introduction 8.1 Straight Insertion and Shell’s Method 8.2 Quicksort 8.3 Heapsort 8.4 Indexing and Ranking 8.5 Selecting the M th Largest 8.6 Determination of Equivalence Classes
9
Root Finding and Nonlinear Sets of Equations 9.0 Introduction 9.1 Bracketing and Bisection 9.2 Secant Method, False Position Method, and Ridders’ Method 9.3 Van Wijngaarden–Dekker–Brent Method 9.4 Newton-Raphson Method Using Derivative 9.5 Roots of Polynomials 9.6 Newton-Raphson Method for Nonlinear Systems of Equations 9.7 Globally Convergent Methods for Nonlinear Systems of Equations
10 Minimization or Maximization of Functions 10.0 Introduction 10.1 Golden Section Search in One Dimension 10.2 Parabolic Interpolation and Brent’s Method in One Dimension 10.3 One-Dimensional Search with First Derivatives 10.4 Downhill Simplex Method in Multidimensions 10.5 Direction Set (Powell’s) Methods in Multidimensions 10.6 Conjugate Gradient Methods in Multidimensions 10.7 Variable Metric Methods in Multidimensions 10.8 Linear Programming and the Simplex Method 10.9 Simulated Annealing Methods
11 Eigensystems 11.0 Introduction 11.1 Jacobi Transformations of a Symmetric Matrix 11.2 Reduction of a Symmetric Matrix to Tridiagonal Form: Givens and Householder Reductions 11.3 Eigenvalues and Eigenvectors of a Tridiagonal Matrix 11.4 Hermitian Matrices 11.5 Reduction of a General Matrix to Hessenberg Form
vii 277 281 287 290 295 299 306
320 320 321 323 327 329 333 337
340 340 343 347 352 355 362 372 376
387 387 390 395 399 402 406 413 418 423 436
449 449 456 462 469 475 476
viii
Contents
11.6 The QR Algorithm for Real Hessenberg Matrices 11.7 Improving Eigenvalues and/or Finding Eigenvectors by Inverse Iteration
12 Fast Fourier Transform 12.0 Introduction 12.1 Fourier Transform of Discretely Sampled Data 12.2 Fast Fourier Transform (FFT) 12.3 FFT of Real Functions, Sine and Cosine Transforms 12.4 FFT in Two or More Dimensions 12.5 Fourier Transforms of Real Data in Two and Three Dimensions 12.6 External Storage or Memory-Local FFTs
13 Fourier and Spectral Applications 13.0 Introduction 13.1 Convolution and Deconvolution Using the FFT 13.2 Correlation and Autocorrelation Using the FFT 13.3 Optimal (Wiener) Filtering with the FFT 13.4 Power Spectrum Estimation Using the FFT 13.5 Digital Filtering in the Time Domain 13.6 Linear Prediction and Linear Predictive Coding 13.7 Power Spectrum Estimation by the Maximum Entropy (All Poles) Method 13.8 Spectral Analysis of Unevenly Sampled Data 13.9 Computing Fourier Integrals Using the FFT 13.10 Wavelet Transforms 13.11 Numerical Use of the Sampling Theorem
14 Statistical Description of Data 14.0 Introduction 14.1 Moments of a Distribution: Mean, Variance, Skewness, and So Forth 14.2 Do Two Distributions Have the Same Means or Variances? 14.3 Are Two Distributions Different? 14.4 Contingency Table Analysis of Two Distributions 14.5 Linear Correlation 14.6 Nonparametric or Rank Correlation 14.7 Do Two-Dimensional Distributions Differ? 14.8 Savitzky-Golay Smoothing Filters
15 Modeling of Data 15.0 Introduction 15.1 Least Squares as a Maximum Likelihood Estimator 15.2 Fitting Data to a Straight Line 15.3 Straight-Line Data with Errors in Both Coordinates 15.4 General Linear Least Squares 15.5 Nonlinear Models
480 487
490 490 494 498 504 515 519 525
530 530 531 538 539 542 551 557 565 569 577 584 600
603 603 604 609 614 622 630 633 640 644
650 650 651 655 660 665 675
Contents
15.6 Confidence Limits on Estimated Model Parameters 15.7 Robust Estimation
16 Integration of Ordinary Differential Equations 16.0 Introduction 16.1 Runge-Kutta Method 16.2 Adaptive Stepsize Control for Runge-Kutta 16.3 Modified Midpoint Method 16.4 Richardson Extrapolation and the Bulirsch-Stoer Method 16.5 Second-Order Conservative Equations 16.6 Stiff Sets of Equations 16.7 Multistep, Multivalue, and Predictor-Corrector Methods
17 Two Point Boundary Value Problems 17.0 Introduction 17.1 The Shooting Method 17.2 Shooting to a Fitting Point 17.3 Relaxation Methods 17.4 A Worked Example: Spheroidal Harmonics 17.5 Automated Allocation of Mesh Points 17.6 Handling Internal Boundary Conditions or Singular Points
18 Integral Equations and Inverse Theory 18.0 Introduction 18.1 Fredholm Equations of the Second Kind 18.2 Volterra Equations 18.3 Integral Equations with Singular Kernels 18.4 Inverse Problems and the Use of A Priori Information 18.5 Linear Regularization Methods 18.6 Backus-Gilbert Method 18.7 Maximum Entropy Image Restoration
19 Partial Differential Equations 19.0 Introduction 19.1 Flux-Conservative Initial Value Problems 19.2 Diffusive Initial Value Problems 19.3 Initial Value Problems in Multidimensions 19.4 Fourier and Cyclic Reduction Methods for Boundary Value Problems 19.5 Relaxation Methods for Boundary Value Problems 19.6 Multigrid Methods for Boundary Value Problems
20 Less-Numerical Algorithms 20.0 Introduction 20.1 Diagnosing Machine Parameters 20.2 Gray Codes
ix 684 694
701 701 704 708 716 718 726 727 740
745 745 749 751 753 764 774 775
779 779 782 786 788 795 799 806 809
818 818 825 838 844 848 854 862
881 881 881 886
x
Contents
20.3 Cyclic Redundancy and Other Checksums 20.4 Huffman Coding and Compression of Data 20.5 Arithmetic Coding 20.6 Arithmetic at Arbitrary Precision
888 896 902 906
References for Volume 1
916
Index of Programs and Dependencies (Vol. 1)
920
General Index to Volumes 1 and 2
Contents of Volume 2: Numerical Recipes in Fortran 90 Preface to Volume 2 Foreword by Michael Metcalf
viii x
License Information
xvii
21
Introduction to Fortran 90 Language Features
935
22
Introduction to Parallel Programming
962
23
Numerical Recipes Utilities for Fortran 90
987
Fortran 90 Code Chapters
1009
B1
Preliminaries
1010
B2
Solution of Linear Algebraic Equations
1014
B3
Interpolation and Extrapolation
1043
B4
Integration of Functions
1052
B5
Evaluation of Functions
1070
B6
Special Functions
1083
B7
Random Numbers
1141
B8
Sorting
1167
B9
Root Finding and Nonlinear Sets of Equations
1182
B10 Minimization or Maximization of Functions
1201
B11 Eigensystems
1225
B12 Fast Fourier Transform
1235
Contents
xi
B13 Fourier and Spectral Applications
1253
B14 Statistical Description of Data
1269
B15 Modeling of Data
1285
B16 Integration of Ordinary Differential Equations
1297
B17 Two Point Boundary Value Problems
1314
B18 Integral Equations and Inverse Theory
1325
B19 Partial Differential Equations
1332
B20 Less-Numerical Algorithms
1343
References for Volume 2
1359
Appendices C1
Listing of Utility Modules (nrtype and nrutil)
1361
C2
Listing of Explicit Interfaces
1384
C3
Index of Programs and Dependencies (Vol. 2)
1434
General Index to Volumes 1 and 2
1447
xii
Plan of the Two-Volume Edition
xiii
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Fortran, long the epitome of stability, is once again a language in flux. Fortran 90 is not just the long-awaited updating of traditional Fortran 77 to modern computing practices, but also demonstrates Fortran’s decisive bid to be the language of choice for parallel programming on multiprocessor computers. At the same time, Fortran 90 is completely backwards-compatible with all Fortran 77 code. So, users with legacy code, or who choose to use only older language constructs, will still get the benefit of updated and actively maintained compilers. As we, the authors of Numerical Recipes, watched the gestation and birth of Fortran 90 by its governing standards committee (an interesting process described by a leading Committee member, Michael Metcalf, in the Foreword to our Volume 2), it became clear to us that the right moment for moving Numerical Recipes from Fortran 77 to Fortran 90 was sooner, rather than later. On the other hand, it was equally clear that Fortran-77-style programming — no matter whether with Fortran 77 or Fortran 90 compilers — is, and will continue for a long time to be, the “mother tongue” of a large population of active scientists, engineers, and other users of numerical computation. This is not a user base that we would willingly or knowingly abandon. The solution was immediately clear: a two-volume edition of the Fortran Numerical Recipes consisting of Volume 1 (this one, a corrected reprinting of the previous one-volume edition), now retitled Numerical Recipes in Fortran 77, and a completely new Volume 2, titled Numerical Recipes in Fortran 90: The Art of Parallel Scientific Computing. Volume 2 begins with three chapters (21, 22, and 23) that extend the narrative of the first volume to the new subjects of Fortran 90 language features, parallel programming methodology, and the implementation of certain useful utility functions in Fortran 90. Then, in exact correspondence with Volume 1’s Chapters 1–20, are new chapters B1–B20, devoted principally to the listing and explanation of new Fortran 90 routines. With a few exceptions, each Fortran 77 routine in Volume 1 has a corresponding new Fortran 90 version in Volume 2. (The exceptions are a few new capabilities, notably in random number generation and in multigrid PDE solvers, that are unique to Volume 2’s Fortran 90.) Otherwise, there is no duplication between the volumes. The detailed explanation of the algorithms in this Volume 1 is intended to apply to, and be essential for, both volumes. In other words: You can use this Volume 1 without having Volume 2, but you can’t use Volume 2 without Volume 1. We think that there is much to be gained by having and using both volumes: Fortran 90’s parallel language constructions are not only useful for present and future multiprocessor machines; they also allow for the elegant and concise formulation of many algorithms on ordinary single-processor computers. We think that essentially all Fortran programmers will want gradually to migrate into Fortran 90 and into a mode of “thinking parallel.” We have written Volume 2 specifically to help with this important transition. Volume 2’s discussion of parallel programming is focused on those issues of direct relevance to the Fortran 90 programmer. Some more general aspects of parallel programming, such as communication costs, synchronization of multiple processers,
xiv
Plan of the Two-Volume Edition
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
etc., are touched on only briefly. We provide references to the extensive literature on these more specialized topics. A special note to C programmers: Right now, there is no effort at producing a parallel version of C that is comparable to Fortran 90 in maturity, acceptance, and stability. We think, therefore, that C programmers will be well served by using Volume 2, either in conjuction with this Volume 1 or else in conjunction with the sister volume Numerical Recipes in C: The Art of Scientific Computing, for an educational excursion into Fortran 90, its parallel programming constructions, and the numerical algorithms that capitalize on them. C and C++ programming have not been far from our minds as we have written this two-volume version. We think you will find that time spent in absorbing the principal lessons of Volume 2’s Chapters 21–23 will be amply repaid in the future, as C and C++ eventually develop standard parallel extensions.
Preface to the Second Edition
xv
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Our aim in writing the original edition of Numerical Recipes was to provide a book that combined general discussion, analytical mathematics, algorithmics, and actual working programs. The success of the first edition puts us now in a difficult, though hardly unenviable, position. We wanted, then and now, to write a book that is informal, fearlessly editorial, unesoteric, and above all useful. There is a danger that, if we are not careful, we might produce a second edition that is weighty, balanced, scholarly, and boring. It is a mixed blessing that we know more now than we did six years ago. Then, we were making educated guesses, based on existing literature and our own research, about which numerical techniques were the most important and robust. Now, we have the benefit of direct feedback from a large reader community. Letters to our alter-ego enterprise, Numerical Recipes Software, are in the thousands per year. (Please, don’t telephone us.) Our post office box has become a magnet for letters pointing out that we have omitted some particular technique, well known to be important in a particular field of science or engineering. We value such letters, and digest them carefully, especially when they point us to specific references in the literature. The inevitable result of this input is that this Second Edition of Numerical Recipes is substantially larger than its predecessor, in fact about 50% larger both in words and number of included programs (the latter now numbering well over 300). “Don’t let the book grow in size,” is the advice that we received from several wise colleagues. We have tried to follow the intended spirit of that advice, even as we violate the letter of it. We have not lengthened, or increased in difficulty, the book’s principal discussions of mainstream topics. Many new topics are presented at this same accessible level. Some topics, both from the earlier edition and new to this one, are now set in smaller type that labels them as being “advanced.” The reader who ignores such advanced sections completely will not, we think, find any lack of continuity in the shorter volume that results. Here are some highlights of the new material in this Second Edition: • a new chapter on integral equations and inverse methods • a detailed treatment of multigrid methods for solving elliptic partial differential equations • routines for band diagonal linear systems • improved routines for linear algebra on sparse matrices • Cholesky and QR decomposition • orthogonal polynomials and Gaussian quadratures for arbitrary weight functions • methods for calculating numerical derivatives • Pad´e approximants, and rational Chebyshev approximation • Bessel functions, and modified Bessel functions, of fractional order; and several other new special functions • improved random number routines • quasi-random sequences • routines for adaptive and recursive Monte Carlo integration in highdimensional spaces • globally convergent methods for sets of nonlinear equations
xvi
Preface to the Second Edition
simulated annealing minimization for continuous control spaces fast Fourier transform (FFT) for real data in two and three dimensions fast Fourier transform (FFT) using external storage improved fast cosine transform routines wavelet transforms Fourier integrals with upper and lower limits spectral analysis on unevenly sampled data Savitzky-Golay smoothing filters fitting straight line data with errors in both coordinates a two-dimensional Kolmogorov-Smirnoff test the statistical bootstrap method embedded Runge-Kutta-Fehlberg methods for differential equations high-order methods for stiff differential equations a new chapter on “less-numerical” algorithms, including Huffman and arithmetic coding, arbitrary precision arithmetic, and several other topics. Consult the Preface to the First Edition, following, or the Table of Contents, for a list of the more “basic” subjects treated.
Acknowledgments It is not possible for us to list by name here all the readers who have made useful suggestions; we are grateful for these. In the text, we attempt to give specific attribution for ideas that appear to be original, and not known in the literature. We apologize in advance for any omissions. Some readers and colleagues have been particularly generous in providing us with ideas, comments, suggestions, and programs for this Second Edition. We especially want to thank George Rybicki, Philip Pinto, Peter Lepage, Robert Lupton, Douglas Eardley, Ramesh Narayan, David Spergel, Alan Oppenheim, Sallie Baliunas, Scott Tremaine, Glennys Farrar, Steven Block, John Peacock, Thomas Loredo, Matthew Choptuik, Gregory Cook, L. Samuel Finn, P. Deuflhard, Harold Lewis, Peter Weinberger, David Syer, Richard Ferch, Steven Ebstein, and William Gould. We have been helped by Nancy Lee Snyder’s mastery of a complicated TEX manuscript. We express appreciation to our editors Lauren Cowles and Alan Harvey at Cambridge University Press, and to our production editor Russell Hahn. We remain, of course, grateful to the individuals acknowledged in the Preface to the First Edition. Special acknowledgment is due to programming consultant Seth Finkelstein, who influenced many of the routines in this book, and wrote or rewrote many more routines in its C-language twin and the companion Example books. Our project has benefited enormously from Seth’s talent for detecting, and following the trail of, even very slight anomalies (often compiler bugs, but occasionally our errors), and from his good programming sense. We prepared this book for publication on DEC and Sun workstations running the UNIX operating system, and on a 486/33 PC compatible running MS-DOS 5.0/Windows 3.0. (See §1.0 for a list of additional computers used in program tests.) We enthusiastically recommend the principal software used: GNU Emacs, TEX, Perl, Adobe Illustrator, and PostScript. Also used were a variety of FORTRAN compilers — too numerous (and sometimes too buggy) for individual
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
• • • • • • • • • • • • • •
Preface to the Second Edition
xvii
June, 1992
William H. Press Saul A. Teukolsky William T. Vetterling Brian P. Flannery
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
acknowledgment. It is a sobering fact that our standard test suite (exercising all the routines in this book) has uncovered compiler bugs in a large majority of the compilers tried. When possible, we work with developers to see that such bugs get fixed; we encourage interested compiler developers to contact us about such arrangements. WHP and SAT acknowledge the continued support of the U.S. National Science Foundation for their research on computational methods. D.A.R.P.A. support is acknowledged for §13.10 on wavelets.
Preface to the First Edition
xviii
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
We call this book Numerical Recipes for several reasons. In one sense, this book is indeed a “cookbook” on numerical computation. However there is an important distinction between a cookbook and a restaurant menu. The latter presents choices among complete dishes in each of which the individual flavors are blended and disguised. The former — and this book — reveals the individual ingredients and explains how they are prepared and combined. Another purpose of the title is to connote an eclectic mixture of presentational techniques. This book is unique, we think, in offering, for each topic considered, a certain amount of general discussion, a certain amount of analytical mathematics, a certain amount of discussion of algorithmics, and (most important) actual implementations of these ideas in the form of working computer routines. Our task has been to find the right balance among these ingredients for each topic. You will find that for some topics we have tilted quite far to the analytic side; this where we have felt there to be gaps in the “standard” mathematical training. For other topics, where the mathematical prerequisites are universally held, we have tilted towards more in-depth discussion of the nature of the computational algorithms, or towards practical questions of implementation. We admit, therefore, to some unevenness in the “level” of this book. About half of it is suitable for an advanced undergraduate course on numerical computation for science or engineering majors. The other half ranges from the level of a graduate course to that of a professional reference. Most cookbooks have, after all, recipes at varying levels of complexity. An attractive feature of this approach, we think, is that the reader can use the book at increasing levels of sophistication as his/her experience grows. Even inexperienced readers should be able to use our most advanced routines as black boxes. Having done so, we hope that these readers will subsequently go back and learn what secrets are inside. If there is a single dominant theme in this book, it is that practical methods of numerical computation can be simultaneously efficient, clever, and — important — clear. The alternative viewpoint, that efficient computational methods must necessarily be so arcane and complex as to be useful only in “black box” form, we firmly reject. Our purpose in this book is thus to open up a large number of computational black boxes to your scrutiny. We want to teach you to take apart these black boxes and to put them back together again, modifying them to suit your specific needs. We assume that you are mathematically literate, i.e., that you have the normal mathematical preparation associated with an undergraduate degree in a physical science, or engineering, or economics, or a quantitative social science. We assume that you know how to program a computer. We do not assume that you have any prior formal knowledge of numerical analysis or numerical methods. The scope of Numerical Recipes is supposed to be “everything up to, but not including, partial differential equations.” We honor this in the breach: First, we do have one introductory chapter on methods for partial differential equations (Chapter 19). Second, we obviously cannot include everything else. All the so-called “standard” topics of a numerical analysis course have been included in this book:
Preface to the First Edition
xix
Acknowledgments Many colleagues have been generous in giving us the benefit of their numerical and computational experience, in providing us with programs, in commenting on the manuscript, or in general encouragement. We particularly wish to thank George Rybicki, Douglas Eardley, Philip Marcus, Stuart Shapiro, Paul Horowitz, Bruce Musicus, Irwin Shapiro, Stephen Wolfram, Henry Abarbanel, Larry Smarr, Richard Muller, John Bahcall, and A.G.W. Cameron. We also wish to acknowledge two individuals whom we have never met: Forman Acton, whose 1970 textbook Numerical Methods that Work (New York: Harper and Row) has surely left its stylistic mark on us; and Donald Knuth, both for his series of books on The Art of Computer Programming (Reading, MA: AddisonWesley), and for TEX, the computer typesetting language which immensely aided production of this book. Research by the authors on computational methods was supported in part by the U.S. National Science Foundation. October, 1985
William H. Press Brian P. Flannery Saul A. Teukolsky William T. Vetterling
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
linear equations (Chapter 2), interpolation and extrapolation (Chaper 3), integration (Chaper 4), nonlinear root-finding (Chapter 9), eigensystems (Chapter 11), and ordinary differential equations (Chapter 16). Most of these topics have been taken beyond their standard treatments into some advanced material which we have felt to be particularly important or useful. Some other subjects that we cover in detail are not usually found in the standard numerical analysis texts. These include the evaluation of functions and of particular special functions of higher mathematics (Chapters 5 and 6); random numbers and Monte Carlo methods (Chapter 7); sorting (Chapter 8); optimization, including multidimensional methods (Chapter 10); Fourier transform methods, including FFT methods and other spectral methods (Chapters 12 and 13); two chapters on the statistical description and modeling of data (Chapters 14 and 15); and two-point boundary value problems, both shooting and relaxation methods (Chapter 17). The programs in this book are included in ANSI-standard FORTRAN-77. Versions of the book in C, Pascal, and BASIC are available separately. We have more to say about the FORTRAN language, and the computational environment assumed by our routines, in §1.1 (Introduction).
License Information
Disclaimer of Warranty We make no warranties, express or implied, that the programs contained in this volume are free of error, or are consistent with any particular standard of merchantability, or that they will meet your requirements for any particular application. They should not be relied on for solving a problem whose incorrect solution could result in injury to a person or loss of property. If you do use the programs in such a manner, it is at your own risk. The authors and publisher disclaim all liability for direct or consequential damages resulting from your use of the programs.
How to Get the Code onto Your Computer Pick one of the following methods: • You can type the programs from this book directly into your computer. In this case, the only kind of license available to you is the free “immediate license” (see below). You are not authorized to transfer or distribute a machine-readable copy to any other person, nor to have any other person type the programs into a computer on your behalf. We do not want to hear bug reports from you if you choose this option, because experience has shown that virtually all reported bugs in such cases are typing errors! • You can download the Numerical Recipes programs electronically from the Numerical Recipes On-Line Software Store, located at our Web site (http://www.nr.com). They are packaged as a password-protected file, and you’ll need to purchase a license to unpack them. You can get a single-screen license and password immediately, on-line, from the On-Line Store, with fees ranging from $50 (PC, Macintosh, educational institutions’ UNIX) to $140 (general UNIX). Downloading the packaged software from the On-Line Store is also the way to start if you want to acquire a more general (multiscreen, site, or corporate) license.
xx
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Read this section if you want to use the programs in this book on a computer. You’ll need to read the following Disclaimer of Warranty, get the programs onto your computer, and acquire a Numerical Recipes software license. (Without this license, which can be the free “immediate license” under terms described below, the book is intended as a text and reference book, for reading purposes only.)
License Information
xxi
Types of License Offered Here are the types of licenses that we offer. Note that some types are automatically acquired with the purchase of media from Cambridge University Press, or of an unlocking password from the Numerical Recipes On-Line Software Store, while other types of licenses require that you communicate specifically with Numerical Recipes Software (email: [email protected] or fax: 781 863-1739). Our Web site http://www.nr.com has additional information. • [“Immediate License”] If you are the individual owner of a copy of this book and you type one or more of its routines into your computer, we authorize you to use them on that computer for your own personal and noncommercial purposes. You are not authorized to transfer or distribute machine-readable copies to any other person, or to use the routines on more than one machine, or to distribute executable programs containing our routines. This is the only free license. • [“Single-Screen License”] This is the most common type of low-cost license, with terms governed by our Single Screen (Shrinkwrap) License document (complete terms available through our Web site). Basically, this license lets you use Numerical Recipes routines on any one screen (PC, workstation, X-terminal, etc.). You may also, under this license, transfer pre-compiled, executable programs incorporating our routines to other, unlicensed, screens or computers, providing that (i) your application is noncommercial (i.e., does not involve the selling of your program for a fee), (ii) the programs were first developed, compiled, and successfully run on a licensed screen, and (iii) our routines are bound into the programs in such a manner that they cannot be accessed as individual routines and cannot practicably be unbound and used in other programs. That is, under this license, your program user must not be able to use our programs as part of a program library or “mix-and-match” workbench. Conditions for other types of commercial or noncommercial distribution may be found on our Web site (http://www.nr.com).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
• You can purchase media containing the programs from Cambridge University Press. Diskette versions are available in IBM-compatible format for machines running Windows 3.1, 95, or NT. CDROM versions in ISO9660 format for PC, Macintosh, and UNIX systems are also available; these include both Fortran and C versions (as well as versions in Pascal and BASIC from the first edition) on a single CDROM. Diskettes purchased from Cambridge University Press include a single-screen license for PC or Macintosh only. The CDROM is available with a singlescreen license for PC or Macintosh (order ISBN 0 521 576083), or (at a slightly higher price) with a single-screen license for UNIX workstations (order ISBN 0 521 576075). Orders for media from Cambridge University Press can be placed at 800 872-7423 (North America only) or by email to [email protected] (North America) or [email protected] (rest of world). Or, visit the Web sites http://www.cup.org (North America) or http://www.cup.cam.ac.uk (rest of world).
xxii
License Information
• [“Course Right-to-Copy License”] Instructors at accredited educational institutions who have adopted this book for a course, and who have already purchased a Single Screen License (either acquired with the purchase of media, or from the Numerical Recipes On-Line Software Store), may license the programs for use in that course as follows: Mail your name, title, and address; the course name, number, dates, and estimated enrollment; and advance payment of $5 per (estimated) student to Numerical Recipes Software, at this address: P.O. Box 243, Cambridge, MA 02238 (USA). You will receive by return mail a license authorizing you to make copies of the programs for use by your students, and/or to transfer the programs to a machine accessible to your students (but only for the duration of the course).
About Copyrights on Computer Programs Like artistic or literary compositions, computer programs are protected by copyright. Generally it is an infringement for you to copy into your computer a program from a copyrighted source. (It is also not a friendly thing to do, since it deprives the program’s author of compensation for his or her creative effort.) Under copyright law, all “derivative works” (modified versions, or translations into another computer language) also come under the same copyright as the original work. Copyright does not protect ideas, but only the expression of those ideas in a particular form. In the case of a computer program, the ideas consist of the program’s methodology and algorithm, including the necessary sequence of steps adopted by the programmer. The expression of those ideas is the program source code (particularly any arbitrary or stylistic choices embodied in it), its derived object code, and any other derivative works. If you analyze the ideas contained in a program, and then express those ideas in your own completely different implementation, then that new program implementation belongs to you. That is what we have done for those programs in this book that are not entirely of our own devising. When programs in this book are said to be “based” on programs published in copyright sources, we mean that the ideas are the same. The expression of these ideas as source code is our own. We believe that no material in this book infringes on an existing copyright.
Trademarks Several registered trademarks appear within the text of this book: Sun is a trademark of Sun Microsystems, Inc. SPARC and SPARCstation are trademarks of SPARC International, Inc. Microsoft, Windows 95, Windows NT, PowerStation, and MS are trademarks of Microsoft Corporation. DEC, VMS, Alpha AXP, and
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
• [“Multi-Screen, Server, Site, and Corporate Licenses”] The terms of the Single Screen License can be extended to designated groups of machines, defined by number of screens, number of machines, locations, or ownership. Significant discounts from the corresponding single-screen prices are available when the estimated number of screens exceeds 40. Contact Numerical Recipes Software (email: [email protected] or fax: 781 863-1739) for details.
License Information
xxiii
Attributions The fact that ideas are legally “free as air” in no way supersedes the ethical requirement that ideas be credited to their known originators. When programs in this book are based on known sources, whether copyrighted or in the public domain, published or “handed-down,” we have attempted to give proper attribution. Unfortunately, the lineage of many programs in common circulation is often unclear. We would be grateful to readers for new or corrected information regarding attributions, which we will attempt to incorporate in subsequent printings.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
ULTRIX are trademarks of Digital Equipment Corporation. IBM is a trademark of International Business Machines Corporation. Apple and Macintosh are trademarks of Apple Computer, Inc. UNIX is a trademark licensed exclusively through X/Open Co. Ltd. IMSL is a trademark of Visual Numerics, Inc. NAG refers to proprietary computer software of Numerical Algorithms Group (USA) Inc. PostScript and Adobe Illustrator are trademarks of Adobe Systems Incorporated. Last, and no doubt least, Numerical Recipes (when identifying products) is a trademark of Numerical Recipes Software.
Computer Programs by Chapter and Section flmoon julday badluk caldat
calculate phases of the moon by date Julian Day number from calendar date Friday the 13th when the moon is full calendar date from Julian day number
2.1
gaussj
2.3 2.3 2.4 2.4 2.4 2.4 2.5 2.6 2.6 2.6 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.7 2.8 2.8 2.9 2.9 2.10 2.10 2.10 2.10 2.10
ludcmp lubksb tridag banmul bandec banbks mprove svbksb svdcmp pythag cyclic sprsin sprsax sprstx sprstp sprspm sprstm linbcg snrm atimes asolve vander toeplz choldc cholsl qrdcmp qrsolv rsolv qrupdt rotate
Gauss-Jordan matrix inversion and linear equation solution linear equation solution, LU decomposition linear equation solution, backsubstitution solution of tridiagonal systems multiply vector by band diagonal matrix band diagonal systems, decomposition band diagonal systems, backsubstitution linear equation solution, iterative improvement singular value backsubstitution singular value decomposition of a matrix calculate (a2 + b2 )1/2 without overflow solution of cyclic tridiagonal systems convert matrix to sparse format product of sparse matrix and vector product of transpose sparse matrix and vector transpose of sparse matrix pattern multiply two sparse matrices threshold multiply two sparse matrices biconjugate gradient solution of sparse systems used by linbcg for vector norm used by linbcg for sparse multiplication used by linbcg for preconditioner solve Vandermonde systems solve Toeplitz systems Cholesky decomposition Cholesky backsubstitution QR decomposition QR backsubstitution right triangular backsubstitution update a QR decomposition Jacobi rotation used by qrupdt
3.1 3.2 3.3 3.3 3.4
polint ratint spline splint locate
polynomial interpolation rational function interpolation construct a cubic spline cubic spline interpolation search an ordered table by bisection xxiv
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1.0 1.1 1.1 1.1
Computer Programs by Chapter and Section
xxv
hunt polcoe polcof polin2 bcucof bcuint splie2 splin2
search a table when calls are correlated polynomial coefficients from table of values polynomial coefficients from table of values two-dimensional polynomial interpolation construct two-dimensional bicubic two-dimensional bicubic interpolation construct two-dimensional spline two-dimensional spline interpolation
4.2 4.2 4.2 4.3 4.4 4.4 4.4 4.4 4.4 4.4 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.6
trapzd qtrap qsimp qromb midpnt qromo midinf midsql midsqu midexp qgaus gauleg gaulag gauher gaujac gaucof orthog quad3d
trapezoidal rule integrate using trapezoidal rule integrate using Simpson’s rule integrate using Romberg adaptive method extended midpoint rule integrate using open Romberg adaptive method integrate a function on a semi-infinite interval integrate a function with lower square-root singularity integrate a function with upper square-root singularity integrate a function that decreases exponentially integrate a function by Gaussian quadratures Gauss-Legendre weights and abscissas Gauss-Laguerre weights and abscissas Gauss-Hermite weights and abscissas Gauss-Jacobi weights and abscissas quadrature weights from orthogonal polynomials construct nonclassical orthogonal polynomials integrate a function over a three-dimensional space
5.1 5.3 5.3 5.3 5.7 5.8 5.8 5.9 5.9 5.10 5.10 5.11 5.12 5.13
eulsum ddpoly poldiv ratval dfridr chebft chebev chder chint chebpc pcshft pccheb pade ratlsq
sum a series by Euler–van Wijngaarden algorithm evaluate a polynomial and its derivatives divide one polynomial by another evaluate a rational function numerical derivative by Ridders’ method fit a Chebyshev polynomial to a function Chebyshev polynomial evaluation derivative of a function already Chebyshev fitted integrate a function already Chebyshev fitted polynomial coefficients from a Chebyshev fit polynomial coefficients of a shifted polynomial inverse of chebpc; use to economize power series Pad´e approximant from power series coefficients rational fit by least-squares method
6.1 6.1 6.1 6.1
gammln factrl bico factln
logarithm of gamma function factorial function binomial coefficients function logarithm of factorial function
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
3.4 3.5 3.5 3.6 3.6 3.6 3.6 3.6
xxvi
Computer Programs by Chapter and Section
beta gammp gammq gser gcf erf erfc erfcc expint ei betai betacf bessj0 bessy0 bessj1 bessy1 bessy bessj bessi0 bessk0 bessi1 bessk1 bessk bessi bessjy beschb bessik airy sphbes plgndr frenel cisi dawson rf rd rj rc ellf elle ellpi sncndn hypgeo hypser hypdrv
beta function incomplete gamma function complement of incomplete gamma function series used by gammp and gammq continued fraction used by gammp and gammq error function complementary error function complementary error function, concise routine exponential integral En exponential integral Ei incomplete beta function continued fraction used by betai Bessel function J0 Bessel function Y0 Bessel function J1 Bessel function Y1 Bessel function Y of general integer order Bessel function J of general integer order modified Bessel function I0 modified Bessel function K0 modified Bessel function I1 modified Bessel function K1 modified Bessel function K of integer order modified Bessel function I of integer order Bessel functions of fractional order Chebyshev expansion used by bessjy modified Bessel functions of fractional order Airy functions spherical Bessel functions jn and yn Legendre polynomials, associated (spherical harmonics) Fresnel integrals S(x) and C(x) cosine and sine integrals Ci and Si Dawson’s integral Carlson’s elliptic integral of the first kind Carlson’s elliptic integral of the second kind Carlson’s elliptic integral of the third kind Carlson’s degenerate elliptic integral Legendre elliptic integral of the first kind Legendre elliptic integral of the second kind Legendre elliptic integral of the third kind Jacobian elliptic functions complex hypergeometric function complex hypergeometric function, series evaluation complex hypergeometric function, derivative of
7.1 7.1
ran0 ran1
random deviate by Park and Miller minimal standard random deviate, minimal standard plus shuffle
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
6.1 6.2 6.2 6.2 6.2 6.2 6.2 6.2 6.3 6.3 6.4 6.4 6.5 6.5 6.5 6.5 6.5 6.5 6.6 6.6 6.6 6.6 6.6 6.6 6.7 6.7 6.7 6.7 6.7 6.8 6.9 6.9 6.10 6.11 6.11 6.11 6.11 6.11 6.11 6.11 6.11 6.12 6.12 6.12
Computer Programs by Chapter and Section
xxvii
ran2 ran3 expdev gasdev gamdev poidev bnldev irbit1 irbit2 psdes ran4 sobseq vegas rebin miser ranpt
random deviate by L’Ecuyer long period plus shuffle random deviate by Knuth subtractive method exponential random deviates normally distributed random deviates gamma-law distribution random deviates Poisson distributed random deviates binomial distributed random deviates random bit sequence random bit sequence “pseudo-DES” hashing of 64 bits random deviates from DES-like hashing Sobol’s quasi-random sequence adaptive multidimensional Monte Carlo integration sample rebinning used by vegas recursive multidimensional Monte Carlo integration get random point, used by miser
8.1 8.1 8.1 8.2 8.2 8.3 8.4 8.4 8.4 8.5 8.5 8.5 8.6 8.6
piksrt piksr2 shell sort sort2 hpsort indexx sort3 rank select selip hpsel eclass eclazz
sort an array by straight insertion sort two arrays by straight insertion sort an array by Shell’s method sort an array by quicksort method sort two arrays by quicksort method sort an array by heapsort method construct an index for an array sort, use an index to sort 3 or more arrays construct a rank table for an array find the N th largest in an array find the N th largest, without altering an array find M largest values, without altering an array determine equivalence classes from list determine equivalence classes from procedure
9.0 9.1 9.1 9.1 9.2 9.2 9.2 9.3 9.4 9.4 9.5 9.5
scrsho zbrac zbrak rtbis rtflsp rtsec zriddr zbrent rtnewt rtsafe laguer zroots
9.5 9.5
zrhqr qroot
graph a function to search for roots outward search for brackets on roots inward search for brackets on roots find root of a function by bisection find root of a function by false-position find root of a function by secant method find root of a function by Ridders’ method find root of a function by Brent’s method find root of a function by Newton-Raphson find root of a function by Newton-Raphson and bisection find a root of a polynomial by Laguerre’s method roots of a polynomial by Laguerre’s method with deflation roots of a polynomial by eigenvalue methods complex or double root of a polynomial, Bairstow
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
7.1 7.1 7.2 7.2 7.3 7.3 7.3 7.4 7.4 7.5 7.5 7.7 7.8 7.8 7.8 7.8
xxviii
Computer Programs by Chapter and Section
mnewt lnsrch newt fdjac fmin broydn
Newton’s method for systems of equations search along a line, used by newt globally convergent multi-dimensional Newton’s method finite-difference Jacobian, used by newt norm of a vector function, used by newt secant method for systems of equations
10.1 10.1 10.2 10.3 10.4 10.4 10.5 10.5 10.5 10.6 10.6 10.7 10.8 10.8 10.8 10.8 10.9 10.9 10.9 10.9 10.9 10.9 10.9 10.9
mnbrak golden brent dbrent amoeba amotry powell linmin f1dim frprmn df1dim dfpmin simplx simp1 simp2 simp3 anneal revcst revers trncst trnspt metrop amebsa amotsa
bracket the minimum of a function find minimum of a function by golden section search find minimum of a function by Brent’s method find minimum of a function using derivative information minimize in N -dimensions by downhill simplex method evaluate a trial point, used by amoeba minimize in N -dimensions by Powell’s method minimum of a function along a ray in N -dimensions function used by linmin minimize in N -dimensions by conjugate gradient alternative function used by linmin minimize in N -dimensions by variable metric method linear programming maximization of a linear function linear programming, used by simplx linear programming, used by simplx linear programming, used by simplx traveling salesman problem by simulated annealing cost of a reversal, used by anneal do a reversal, used by anneal cost of a transposition, used by anneal do a transposition, used by anneal Metropolis algorithm, used by anneal simulated annealing in continuous spaces evaluate a trial point, used by amebsa
11.1 11.1 11.2 11.3 11.5 11.5 11.6
jacobi eigsrt tred2 tqli balanc elmhes hqr
eigenvalues and eigenvectors of a symmetric matrix eigenvectors, sorts into order by eigenvalue Householder reduction of a real, symmetric matrix eigensolution of a symmetric tridiagonal matrix balance a nonsymmetric matrix reduce a general matrix to Hessenberg form eigenvalues of a Hessenberg matrix
12.2 12.3 12.3 12.3 12.3 12.3 12.4
four1 twofft realft sinft cosft1 cosft2 fourn
fast Fourier transform (FFT) in one dimension fast Fourier transform of two real functions fast Fourier transform of a single real function fast sine transform fast cosine transform with endpoints “staggered” fast cosine transform fast Fourier transform in multidimensions
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
9.6 9.7 9.7 9.7 9.7 9.7
Computer Programs by Chapter and Section
xxix
rlft3 fourfs fourew
FFT of real data in two or three dimensions FFT for huge data sets on external media rewind and permute files, used by fourfs
13.1 13.2 13.4 13.6 13.6 13.6 13.7 13.8 13.8 13.8 13.9 13.9 13.10 13.10 13.10 13.10 13.10
convlv correl spctrm memcof fixrts predic evlmem period fasper spread dftcor dftint wt1 daub4 pwtset pwt wtn
convolution or deconvolution of data using FFT correlation or autocorrelation of data using FFT power spectrum estimation using FFT evaluate maximum entropy (MEM) coefficients reflect roots of a polynomial into unit circle linear prediction using MEM coefficients power spectral estimation from MEM coefficients power spectrum of unevenly sampled data power spectrum of unevenly sampled larger data sets extirpolate value into array, used by fasper compute endpoint corrections for Fourier integrals high-accuracy Fourier integrals one-dimensional discrete wavelet transform Daubechies 4-coefficient wavelet filter initialize coefficients for pwt partial wavelet transform multidimensional discrete wavelet transform
14.1 14.2 14.2 14.2 14.2 14.2 14.3 14.3 14.3 14.3 14.3 14.4 14.4 14.5 14.6 14.6 14.6 14.6 14.7 14.7 14.7 14.7 14.8
moment ttest avevar tutest tptest ftest chsone chstwo ksone kstwo probks cntab1 cntab2 pearsn spear crank kendl1 kendl2 ks2d1s quadct quadvl ks2d2s savgol
calculate moments of a data set Student’s t-test for difference of means calculate mean and variance of a data set Student’s t-test for means, case of unequal variances Student’s t-test for means, case of paired data F -test for difference of variances chi-square test for difference between data and model chi-square test for difference between two data sets Kolmogorov-Smirnov test of data against model Kolmogorov-Smirnov test between two data sets Kolmogorov-Smirnov probability function contingency table analysis using chi-square contingency table analysis using entropy measure Pearson’s correlation between two data sets Spearman’s rank correlation between two data sets replaces array elements by their rank correlation between two data sets, Kendall’s tau contingency table analysis using Kendall’s tau K–S test in two dimensions, data vs. model count points by quadrants, used by ks2d1s quadrant probabilities, used by ks2d1s K–S test in two dimensions, data vs. data Savitzky-Golay smoothing coefficients
15.2
fit
least-squares fit data to a straight line
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
12.5 12.6 12.6
xxx
Computer Programs by Chapter and Section
fitexy chixy lfit covsrt svdfit svdvar fpoly fleg mrqmin mrqcof fgauss medfit rofunc
fit data to a straight line, errors in both x and y used by fitexy to calculate a χ2 general linear least-squares fit by normal equations rearrange covariance matrix, used by lfit linear least-squares fit by singular value decomposition variances from singular value decomposition fit a polynomial using lfit or svdfit fit a Legendre polynomial using lfit or svdfit nonlinear least-squares fit, Marquardt’s method used by mrqmin to evaluate coefficients fit a sum of Gaussians using mrqmin fit data to a straight line robustly, least absolute deviation fit data robustly, used by medfit
16.1 16.1 16.2 16.2 16.2 16.3 16.4 16.4 16.4 16.5 16.6 16.6 16.6 16.6 16.6
rk4 rkdumb rkqs rkck odeint mmid bsstep pzextr rzextr stoerm stiff jacobn derivs simpr stifbs
integrate one step of ODEs, fourth-order Runge-Kutta integrate ODEs by fourth-order Runge-Kutta integrate one step of ODEs with accuracy monitoring Cash-Karp-Runge-Kutta step used by rkqs integrate ODEs with accuracy monitoring integrate ODEs by modified midpoint method integrate ODEs, Bulirsch-Stoer step polynomial extrapolation, used by bsstep rational function extrapolation, used by bsstep integrate conservative second-order ODEs integrate stiff ODEs by fourth-order Rosenbrock sample Jacobian routine for stiff sample derivatives routine for stiff integrate stiff ODEs by semi-implicit midpoint rule integrate stiff ODEs, Bulirsch-Stoer step
17.1 17.2 17.3 17.3 17.3 17.3 17.4 17.4 17.4 17.4
shoot shootf solvde bksub pinvs red sfroid difeq sphoot sphfpt
solve two point boundary value problem by shooting ditto, by shooting to a fitting point two point boundary value problem, solve by relaxation backsubstitution, used by solvde diagonalize a sub-block, used by solvde reduce columns of a matrix, used by solvde spheroidal functions by method of solvde spheroidal matrix coefficients, used by sfroid spheroidal functions by method of shoot spheroidal functions by method of shootf
18.1 18.1 18.2 18.3 18.3 18.3
fred2 fredin voltra wwghts kermom quadmx
solve linear Fredholm equations of the second kind interpolate solutions obtained with fred2 linear Volterra equations of the second kind quadrature weights for an arbitrarily singular kernel sample routine for moments of a singular kernel sample routine for a quadrature matrix
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
15.3 15.3 15.4 15.4 15.4 15.4 15.4 15.4 15.5 15.5 15.5 15.7 15.7
Computer Programs by Chapter and Section
xxxi
fredex
example of solving a singular Fredholm equation
19.5 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6 19.6
sor mglin rstrct interp addint slvsml relax resid copy fill0 maloc mgfas relax2 slvsm2 lop matadd matsub anorm2
elliptic PDE solved by successive overrelaxation method linear elliptic PDE solved by multigrid method half-weighting restriction, used by mglin, mgfas bilinear prolongation, used by mglin, mgfas interpolate and add, used by mglin solve on coarsest grid, used by mglin Gauss-Seidel relaxation, used by mglin calculate residual, used by mglin utility used by mglin, mgfas utility used by mglin memory allocation utility used by mglin, mgfas nonlinear elliptic PDE solved by multigrid method Gauss-Seidel relaxation, used by mgfas solve on coarsest grid, used by mgfas applies nonlinear operator, used by mgfas utility used by mgfas utility used by mgfas utility used by mgfas
20.1 20.2 20.3 20.3 20.3 20.4 20.4 20.4 20.4 20.5 20.5 20.5 20.6 20.6 20.6 20.6 20.6 20.6 20.6
machar igray icrc1 icrc decchk hufmak hufapp hufenc hufdec arcmak arcode arcsum mpops mpmul mpinv mpdiv mpsqrt mp2dfr mppi
diagnose computer’s floating arithmetic Gray code and its inverse cyclic redundancy checksum, used by icrc cyclic redundancy checksum decimal check digit calculation or verification construct a Huffman code append bits to a Huffman code, used by hufmak use Huffman code to encode and compress a character use Huffman code to decode and decompress a character construct an arithmetic code encode or decode a character using arithmetic coding add integer to byte string, used by arcode multiple precision arithmetic, simpler operations multiple precision multiply, using FFT methods multiple precision reciprocal multiple precision divide and remainder multiple precision square root multiple precision conversion to decimal base multiple precision example, compute many digits of π
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
18.3
Preliminaries
1.0 Introduction This book, like its predecessor edition, is supposed to teach you methods of numerical computing that are practical, efficient, and (insofar as possible) elegant. We presume throughout this book that you, the reader, have particular tasks that you want to get done. We view our job as educating you on how to proceed. Occasionally we may try to reroute you briefly onto a particularly beautiful side road; but by and large, we will guide you along main highways that lead to practical destinations. Throughout this book, you will find us fearlessly editorializing, telling you what you should and shouldn’t do. This prescriptive tone results from a conscious decision on our part, and we hope that you will not find it irritating. We do not claim that our advice is infallible! Rather, we are reacting against a tendency, in the textbook literature of computation, to discuss every possible method that has ever been invented, without ever offering a practical judgment on relative merit. We do, therefore, offer you our practical judgments whenever we can. As you gain experience, you will form your own opinion of how reliable our advice is. We presume that you are able to read computer programs in FORTRAN, that being the language of this version of Numerical Recipes (Second Edition). The book Numerical Recipes in C (Second Edition) is separately available, if you prefer to program in that language. Earlier editions of Numerical Recipes in Pascal and Numerical Recipes Routines and Examples in BASIC are also available; while not containing the additional material of the Second Edition versions in C and FORTRAN, these versions are perfectly serviceable if Pascal or BASIC is your language of choice. When we include programs in the text, they look like this: SUBROUTINE flmoon(n,nph,jd,frac) INTEGER jd,n,nph REAL frac,RAD PARAMETER (RAD=3.14159265/180.) Our programs begin with an introductory comment summarizing their purpose and explaining their calling sequence. This routine calculates the phases of the moon. Given an integer n and a code nph for the phase desired (nph = 0 for new moon, 1 for first quarter, 2 for full, 3 for last quarter), the routine returns the Julian Day Number jd, and the fractional part of a day frac to be added to it, of the nth such phase since January, 1900. Greenwich Mean Time is assumed. INTEGER i REAL am,as,c,t,t2,xtra c=n+nph/4. This is how we comment an individual line. t=c/1236.85 t2=t**2
1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Chapter 1.
2
Chapter 1.
Preliminaries
A few remarks about our typographical conventions and programming style are in order at this point: • It is good programming practice to declare all variables and identifiers in explicit “type” statements (REAL, INTEGER, etc.), even though the implicit declaration rules of FORTRAN do not require this. We will always do so. (As an aside to non-FORTRAN programmers, the implicit declaration rules are that variables which begin with the letters i,j,k,l,m,n are implicitly declared to be type INTEGER, while all other variables are implicitly declared to be type REAL. Explicit declarations override these conventions.) • In sympathy with modular and object-oriented programming practice, we separate, typographically, a routine’s “public” or “interface” section from its “private” or “implementation” section. We do this even though FORTRAN is by no means a modular or object-oriented language: the separation makes sense simply as good programming style. • The public section contains the calling interface and declarations of its variables. We find it useful to consider PARAMETER statements, and their associated declarations, as also being in the public section, since a user may want to modify parameter values to suit a particular purpose. COMMON blocks are likewise usually part of the public section, since they involve communication between routines. • As the last entry in the public section, we will, where applicable, put a standardized comment line with the word USES (not a FORTRAN keyword), followed by a list of all external subroutines and functions that the routine references, excluding built-in FORTRAN functions. (For examples, see the routines in §6.1.) • An introductory comment, set in type as an indented paragraph, separates the public section from the private or implementation section. • Within the introductory comments, as well as in the text, we will frequently use the notation a(1:m) to mean “the array elements a(1), a(2), . . . , a(m).” Likewise, notations like b(2:7) or c(1:m,1:n) are to be
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
as=359.2242+29.105356*c You aren’t really intended to understand this alam=306.0253+385.816918*c+0.010730*t2 gorithm, but it does work! jd=2415020+28*n+7*nph xtra=0.75933+1.53058868*c+(1.178e-4-1.55e-7*t)*t2 if(nph.eq.0.or.nph.eq.2)then xtra=xtra+(0.1734-3.93e-4*t)*sin(RAD*as)-0.4068*sin(RAD*am) else if(nph.eq.1.or.nph.eq.3)then xtra=xtra+(0.1721-4.e-4*t)*sin(RAD*as)-0.6280*sin(RAD*am) else pause ’nph is unknown in flmoon’ This is how we will indicate error conditions. endif if(xtra.ge.0.)then i=int(xtra) else i=int(xtra-1.) endif jd=jd+i frac=xtra-i return END
1.0 Introduction
•
•
•
interpreted as ranges of array indices. (This use of colon to denote ranges comes from FORTRAN-77’s syntax for array declarators and character substrings.) The implementation section contains the declarations of variables that are used only internally in the routine, any necessary SAVE statements for static variables (variables that must be preserved between calls to the routine), and of course the routine’s actual executable code. Case is not significant in FORTRAN, so it can be used to promote readability. Our convention is to use upper case for two different, nonconflicting, purposes. First, nonexecutable compiler keywords are in upper case (e.g., SUBROUTINE, REAL, COMMON); second, parameter identifiers are in upper case. The reason for capitalizing parameters is that, because their values are liable to be modified, the user often needs to scan the implementation section of code to see exactly how the parameters are used. For simplicity, we adopt the convention of handling all errors and exceptional cases by the pause statement. In general, we do not intend that you continue program execution after a pause occurs, but FORTRAN allows you to do so — if you want to see what kind of wrong answer or catastrophic error results. In many applications, you will want to modify our programs to do more sophisticated error handling, for example to return with an error flag set, or call an error-handling routine. In the printed form of this book, we take some special typographical liberties regarding statement labels, and do . . . continue constructions. These are described in §1.1. Note that no such liberties are taken in the machine-readable Numerical Recipes diskettes, where all routines are in standard ANSI FORTRAN-77.
Computational Environment and Program Validation Our goal is that the programs in this book be as portable as possible, across different platforms (models of computer), across different operating systems, and across different FORTRAN compilers. As surrogates for the large number of possible combinations, we have tested all the programs in this book on the combinations of machines, operating systems, and compilers shown on the accompanying table. More generally, the programs should run without modification on any compiler that implements the ANSI FORTRAN-77 standard. At the time of writing, there are not enough installed implementations of the successor FORTRAN-90 standard to justify our using any of its more advanced features. Since FORTRAN-90 is backwardscompatible with FORTRAN-77, there should be no difficulty in using the programs in this book on FORTRAN-90 compilers, as they become available. In validating the programs, we have taken the program source code directly from the machine-readable form of the book’s manuscript, to decrease the chance of propagating typographical errors. “Driver” or demonstration programs that we used as part of our validations are available separately as the Numerical Recipes Example Book (FORTRAN), as well as in machine-readable form. If you plan to use more than a few of the programs in this book, or if you plan to use programs in this book on more than one different computer, then you may find it useful to obtain a copy of these demonstration programs.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
•
3
4
Chapter 1.
Preliminaries
Tested Machines and Compilers Hardware
O/S Version
Compiler Version
MS-DOS 5.0
Microsoft Fortran 5.1
IBM RS6000
AIX 3.0
IBM AIX XL FORTRAN Compiler/6000
IBM PC-RT
BSD UNIX 4.3
“UNIX Fortran 77”
DEC VAX 4000
VMS 5.4
VAX Fortran 5.4
DEC VAXstation 2000
BSD UNIX 4.3
Berkeley f77 2.0 (4.3 bsd, SCCS lev. 6)
DECstation 5000/200
ULTRIX 4.2
DEC Fortran for ULTRIX RISC 3.1
DECsystem 5400
ULTRIX 4.1
MIPS f77 2.10
Sun SPARCstation 2
SunOS 4.1
Sun Fortran 1.4 (SC 1.0)
Apple Macintosh
System 6.0.7 / MPW 3.2
Absoft Fortran 77 Compiler 3.1.2
Of course we would be foolish to claim that there are no bugs in our programs, and we do not make such a claim. We have been very careful, and have benefitted from the experience of the many readers who have written to us. If you find a new bug, please document it and tell us!
Compatibility with the First Edition If you are accustomed to the Numerical Recipes routines of the First Edition, rest assured: almost all of them are still here, with the same names and functionalities, often with major improvements in the code itself. In addition, we hope that you will soon become equally familiar with the added capabilities of the more than 100 routines that are new to this edition. We have retired a small number of First Edition routines, those that we believe to be clearly dominated by better methods implemented in this edition. A table, following, lists the retired routines and suggests replacements. First Edition users should also be aware that some routines common to both editions have alterations in their calling interfaces, so are not directly “plug compatible.” A fairly complete list is: chsone, chstwo, covsrt, dfpmin, laguer, lfit, memcof, mrqcof, mrqmin, pzextr, ran4, realft, rzextr, shoot, shootf. There may be others (depending in part on which printing of the First Edition is taken for the comparison). If you have written software of any appreciable complexity that is dependent on First Edition routines, we do not recommend blindly replacing them by the corresponding routines in this book. We do recommend that any new programming efforts use the new routines.
About References You will find references, and suggestions for further reading, listed at the end of most sections of this book. References are cited in the text by bracketed numbers like this [1]. Because computer algorithms often circulate informally for quite some time before appearing in a published form, the task of uncovering “primary literature”
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
IBM PC compatible 486/33
1.1 Program Organization and Control Structures
5
Previous Routines Omitted from This Edition Name(s)
Replacement(s)
Comment
mglin or mgfas
better method
COSFT
cosft1 or cosft2
choice of boundary conditions
CEL, EL2
rf, rd, rj, rc
better algorithms
DES, DESKS
ran4 now uses psdes
was too slow
MDIAN1, MDIAN2
select, selip
more general
QCKSRT
sort
name change (SORT is now hpsort)
RKQC
rkqs
better method
SMOOFT
use convlv with coefficients from savgol
SPARSE
linbcg
more general
is sometimes quite difficult. We have not attempted this, and we do not pretend to any degree of bibliographical completeness in this book. For topics where a substantial secondary literature exists (discussion in textbooks, reviews, etc.) we have consciously limited our references to a few of the more useful secondary sources, especially those with good references to the primary literature. Where the existing secondary literature is insufficient, we give references to a few primary sources that are intended to serve as starting points for further reading, not as complete bibliographies for the field. The order in which references are listed is not necessarily significant. It reflects a compromise between listing cited references in the order cited, and listing suggestions for further reading in a roughly prioritized order, with the most useful ones first. The remaining two sections of this chapter review some basic concepts of programming (control structures, etc.) and of numerical analysis (roundoff error, etc.). Thereafter, we plunge into the substantive material of the book. CITED REFERENCES AND FURTHER READING: Meeus, J. 1982, Astronomical Formulae for Calculators, 2nd ed., revised and enlarged (Richmond, VA: Willmann-Bell). [1]
1.1 Program Organization and Control Structures We sometimes like to point out the close analogies between computer programs, on the one hand, and written poetry or written musical scores, on the other. All three present themselves as visual media, symbols on a two-dimensional page or computer screen. Yet, in all three cases, the visual, two-dimensional, frozen-in-time representation communicates (or is supposed to communicate) something rather
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
ADI
6
Chapter 1.
Preliminaries
k=(2-j)*(1+3*j)/2
if you want to permute cyclically one of the values j = (0, 1, 2) into respectively k = (1, 2, 0). You will regret it later, however, when you try to understand that line. Better, and likely also faster, is k=j+1 if (k.eq.3) k=0
Many programming stylists would even argue for the ploddingly literal if (j.eq.0) then k=1 else if (j.eq.1) then k=2
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
different, namely a process that unfolds in time. A poem is meant to be read; music, played; a program, executed as a sequential series of computer instructions. In all three cases, the target of the communication, in its visual form, is a human being. The goal is to transfer to him/her, as efficiently as can be accomplished, the greatest degree of understanding, in advance, of how the process will unfold in time. In poetry, this human target is the reader. In music, it is the performer. In programming, it is the program user. Now, you may object that the target of communication of a program is not a human but a computer, that the program user is only an irrelevant intermediary, a lackey who feeds the machine. This is perhaps the case in the situation where the business executive pops a diskette into a desktop computer and feeds that computer a black-box program in binary executable form. The computer, in this case, doesn’t much care whether that program was written with “good programming practice” or not. We envision, however, that you, the readers of this book, are in quite a different situation. You need, or want, to know not just what a program does, but also how it does it, so that you can tinker with it and modify it to your particular application. You need others to be able to see what you have done, so that they can criticize or admire. In such cases, where the desired goal is maintainable or reusable code, the targets of a program’s communication are surely human, not machine. One key to achieving good programming practice is to recognize that programming, music, and poetry — all three being symbolic constructs of the human brain — are naturally structured into hierarchies that have many different nested levels. Sounds (phonemes) form small meaningful units (morphemes) which in turn form words; words group into phrases, which group into sentences; sentences make paragraphs, and these are organized into higher levels of meaning. Notes form musical phrases, which form themes, counterpoints, harmonies, etc.; which form movements, which form concertos, symphonies, and so on. The structure in programs is equally hierarchical. Appropriately, good programming practice brings different techniques to bear on the different levels [1-3]. At a low level is the ascii character set. Then, constants, identifiers, operands, operators. Then program statements, like a(j+1)=b+c/3.0. Here, the best programming advice is simply be clear, or (correspondingly) don’t be too tricky. You might momentarily be proud of yourself at writing the single line
1.1 Program Organization and Control Structures
7
else if (j.eq.2) then k=0 else pause ’never get here’ endif
swap=a(j) a(j)=b(j) b(j)=swap
which makes immediate sense to any programmer as the exchange of two variables, while sum=0.0 ans=0.0 n=1
is very likely to be an initialization of variables prior to some iterative process. This level of hierarchy in a program is usually evident to the eye. It is good programming practice to put in comments at this level, e.g., “initialize” or “exchange variables.” The next level is that of control structures. These are things like the if. . .then. . .else clauses in the example above, do loops, and so on. This level is sufficiently important, and relevant to the hierarchical level of the routines in this book, that we will come back to it just below. At still higher levels in the hierarchy, we have (in FORTRAN) subroutines, functions, and the whole “global” organization of the computational task to be done. In the musical analogy, we are now at the level of movements and complete works. At these levels, modularization and encapsulation become important programming concepts, the general idea being that program units should interact with one another only through clearly defined and narrowly circumscribed interfaces. Good modularization practice is an essential prerequisite to the success of large, complicated software projects, especially those employing the efforts of more than one programmer. It is also good practice (if not quite as essential) in the less massive programming tasks that an individual scientist, or reader of this book, encounters. Some computer languages, such as Modula-2 and C++, promote good modularization with higher-level language constructs, absent in FORTRAN-77. In Modula-2, for example, subroutines, type definitions, and data structures can be encapsulated into “modules” that communicate through declared public interfaces and whose internal workings are hidden from the rest of the program [4]. In the C++ language, the key concept is “class,” a user-definable generalization of data type that provides for data hiding, automatic initialization of data, memory management, dynamic typing, and operator overloading (i.e., the user-definable extension of operators like + and * so as to be appropriate to operands in any particular class) [5]. Properly used in defining the data structures that are passed between program units, classes
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
on the grounds that it is both clear and additionally safeguarded from wrong assumptions about the possible values of j. Our preference among the implementations is for the middle one. In this simple example, we have in fact traversed several levels of hierarchy: Statements frequently come in “groups” or “blocks” which make sense only taken as a whole. The middle fragment above is one example. Another is
8
Chapter 1.
Preliminaries
Control Structures An executing program unfolds in time, but not strictly in the linear order in which the statements are written. Program statements that affect the order in which statements are executed, or that affect whether statements are executed, are called control statements. Control statements never make useful sense by themselves. They make sense only in the context of the groups or blocks of statements that they in turn control. If you think of those blocks as paragraphs containing sentences, then the control statements are perhaps best thought of as the indentation of the paragraph and the punctuation between the sentences, not the words within the sentences. We can now say what the goal of structured programming is. It is to make program control manifestly apparent in the visual presentation of the program. You see that this goal has nothing at all to do with how the computer sees the program. As already remarked, computers don’t care whether you use structured programming or not. Human readers, however, do care. You yourself will also care, once you discover how much easier it is to perfect and debug a well-structured program than one whose control structure is obscure.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
can clarify and circumscribe these units’ public interfaces, reducing the chances of programming error and also allowing a considerable degree of compile-time and run-time error checking. Beyond modularization, though depending on it, lie the concepts of objectoriented programming. Here a programming language, such as C++ or Turbo Pascal 5.5 [6], allows a module’s public interface to accept redefinitions of types or actions, and these redefinitions become shared all the way down through the module’s hierarchy (so-called polymorphism). For example, a routine written to invert a matrix of real numbers could — dynamically, at run time — be made able to handle complex numbers by overloading complex data types and corresponding definitions of the arithmetic operations. Additional concepts of inheritance (the ability to define a data type that “inherits” all the structure of another type, plus additional structure of its own), and object extensibility (the ability to add functionality to a module without access to its source code, e.g., at run time), also come into play. We have not attempted to modularize, or make objects out of, the routines in this book, for at least two reasons. First, the chosen language, FORTRAN-77, does not really make this possible. Second, we envision that you, the reader, might want to incorporate the algorithms in this book, a few at a time, into modules or objects with a structure of your own choosing. There does not exist, at present, a standard or accepted set of “classes” for scientific object-oriented computing. While we might have tried to invent such a set, doing so would have inevitably tied the algorithmic content of the book (which is its raison d’ˆetre) to some rather specific, and perhaps haphazard, set of choices regarding class definitions. On the other hand, we are not unfriendly to the goals of modular and objectoriented programming. Within the limits of FORTRAN, we have therefore tried to structure our programs to be “object friendly,” principally via the clear delineation of interface vs. implementation (§1.0) and the explicit declaration of variables. Within our implementation sections, we have paid particular attention to the practices of structured programming, as we now discuss.
1.1 Program Organization and Control Structures
9
Catalog of Standard Structures Iteration. example
In FORTRAN, simple iteration is performed with a do loop, for
do 10 j=2,1000 b(j)=a(j-1) a(j-1)=j 10 continue
Notice how we always indent the block of code that is acted upon by the control structure, leaving the structure itself unindented. The statement label 10 in this example is a tame label. The majority of modern implementations of FORTRAN-77 provide a nonstandard language extension that obviates the tame label. Originally
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
You accomplish the goals of structured programming in two complementary ways. First, you acquaint yourself with the small number of essential control structures that occur over and over again in programming, and that are therefore given convenient representations in most programming languages. You should learn to think about your programming tasks, insofar as possible, exclusively in terms of these standard control structures. In writing programs, you should get into the habit of representing these standard control structures in consistent, conventional ways. “Doesn’t this inhibit creativity?” our students sometimes ask. Yes, just as Mozart’s creativity was inhibited by the sonata form, or Shakespeare’s by the metrical requirements of the sonnet. The point is that creativity, when it is meant to communicate, does well under the inhibitions of appropriate restrictions on format. Second, you avoid, insofar as possible, control statements whose controlled blocks or objects are difficult to discern at a glance. This means, in practice, that you must try to avoid statement labels and goto’s. It is not the goto’s that are dangerous (although they do interrupt one’s reading of a program); the statement labels are the hazard. In fact, whenever you encounter a statement label while reading a program, you will soon become conditioned to get a sinking feeling in the pit of your stomach. Why? Because the following questions will, by habit, immediately spring to mind: Where did control come from in a branch to this label? It could be anywhere in the routine! What circumstances resulted in a branch to this label? They could be anything! Certainty becomes uncertainty, understanding dissolves into a morass of possibilities. Some older languages, notably 1966 FORTRAN and to a lesser extent FORTRAN77, require statement labels in the construction of certain standard control structures. We will see this in more detail below. This is a demerit for these languages. In such cases, you must use labels as required. But you should never branch to them independently of the standard control structure. If you must branch, let it be to an additional label, one that is not masquerading as part of a standard control structure. We call labels that are part of a standard construction and never otherwise branched to tame labels. They do not interfere with structured programming in any way, except possibly typographically as distractions to the eye. Some examples are now in order to make these considerations more concrete (see Figure 1.1.1).
increment index
block
block
break condition
true
true
block
Figure 1.1.1. Standard control structures used in structured programming: (a) DO iteration; (b) DO WHILE iteration; (c) DO UNTIL iteration; (d) BREAK iteration; (e) IF structure; (f) obsolete form of DO iteration found in FORTRAN-66, where the block is executed once even if the iteration condition is initially not satisfied.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
BREAK iteration (d)
DO UNTIL iteration (c)
false until condition false
DO WHILE iteration (b) DO iteration (a)
block block
true no
false while condition yes iteration complete?
Preliminaries Chapter 1.
10
...
IF structure (e)
block
increment index
yes
FORTRAN-66 DO (obsolete) (f )
Figure 1.1.1. Standard control structures used in structured programming (see caption on previous page).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
iteration complete? no
else block block block block
true true true
false else if condition
... false else if condition false if condition
11 1.1 Program Organization and Control Structures
12
Chapter 1.
Preliminaries
introduced in Digital Equipment Corporations’s VAX-11 FORTRAN, the “enddo” statement is used as
In fact, it was a terrible mistake that the American National Standard for FORTRAN-77 (ANSI X3.9–1978) failed to provide an enddo or equivalent construction. This mistake by the people who write standards, whoever they are, presents us now, more than 15 years later, with a painful quandary: Do we stick to the standard, and clutter our programs with tame labels? Or do we adopt a nonstandard (albeit widely implemented) FORTRAN construction like enddo? We have adopted a compromise position. Standards, even imperfect standards, are terribly important and highly necessary in a time of rapid evolution in computers and their applications. Therefore, all machine-readable forms of our programs (e.g., the diskettes that you can order from the publisher — see back of this book) are strictly FORTRAN-77 compliant. (Well, almost strictly: there is a minor anomaly regarding bit manipulation functions, see below.) In particular, do blocks always end with labeled continue statements, as in the first example above. In the printed version of this book, however, we make use of typography to mitigate the standard’s deficiencies. The statement label that follows the do is printed in small type — as a signal that it is a tame label that you can safely ignore. And, the word “continue” is printed as “enddo”, which you may regard as a very peculiar change of font! The example above, in our adopted typographical format, is do 10 j=2,1000 b(j)=a(j-1) a(j-1)=j enddo 10
(Notice that we also take the typographical liberty of writing the tame label after the “continue” statement, rather than before.) A nested do loop looks like this: do 12 j=1,20 s(j)=0. do 11 k=5,10 s(j)=s(j)+a(j,k) enddo 11 enddo 12
Generally, the numerical values of the tame labels are chosen to put the enddo’s (labeled continue’s on the diskette) into ascending numerical order, hence the do 12 before the do 11 in the above example. IF structure. In this structure the FORTRAN-77 standard is exemplary. Here is a working program that consists dominantly of if control statements:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
do j=2,1000 b(j)=a(j-1) a(j-1)=j enddo
1.1 Program Organization and Control Structures
13
(Astronomers number each 24-hour period, starting and ending at noon, with a unique integer, the Julian Day Number [7]. Julian Day Zero was a very long time ago; a convenient reference point is that Julian Day 2440000 began at noon of May 23, 1968. If you know the Julian Day Number that begins at noon of a given calendar date, then the day of the week of that date is obtained by adding 1 and taking the result modulo base 7; a zero answer corresponds to Sunday, 1 to Monday, . . . , 6 to Saturday.) Do-While iteration. Most good languages, except FORTRAN, provide for structures like the following C example: while (n N . When this occurs there is, in general, no solution vector x to equation (2.0.1), and the set of equations is said to be overdetermined. It happens frequently, however, that the best “compromise” solution is sought, the one that comes closest to satisfying all equations simultaneously. If closeness is defined in the least-squares sense, i.e., that the sum of the squares of the differences between the left- and right-hand sides of equation (2.0.1) be minimized, then the overdetermined linear problem reduces to a
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Here the subroutine has to be told both the logical size of the matrix that you want to invert (here n = 4), and the physical size of the array in which it is stored (here np = 10). This seems like a trivial point, and we are sorry to belabor it. But it turns out that most reported failures of standard linear equation and matrix manipulation packages are due to user errors in passing inappropriate logical or physical dimensions!
26
Chapter 2.
Solution of Linear Algebraic Equations
(usually) solvable linear problem, called the • Linear least-squares problem. The reduced set of equations to be solved can be written as the N ×N set of equations (2.0.4)
where AT denotes the transpose of the matrix A. Equations (2.0.4) are called the normal equations of the linear least-squares problem. There is a close connection between singular value decomposition and the linear least-squares problem, and the latter is also discussed in §2.6. You should be warned that direct solution of the normal equations (2.0.4) is not generally the best way to find least-squares solutions. Some other topics in this chapter include • Iterative improvement of a solution (§2.5) • Various special forms: symmetric positive-definite (§2.9), tridiagonal (§2.4), band diagonal (§2.4), Toeplitz (§2.8), Vandermonde (§2.8), sparse (§2.7) • Strassen’s “fast matrix inversion” (§2.11).
Standard Subroutine Packages We cannot hope, in this chapter or in this book, to tell you everything there is to know about the tasks that have been defined above. In many cases you will have no alternative but to use sophisticated black-box program packages. Several good ones are available. LINPACK was developed at Argonne National Laboratories and deserves particular mention because it is published, documented, and available for free use. A successor to LINPACK, LAPACK, is now becoming available. Packages available commercially include those in the IMSL and NAG libraries. You should keep in mind that the sophisticated packages are designed with very large linear systems in mind. They therefore go to great effort to minimize not only the number of operations, but also the required storage. Routines for the various tasks are usually provided in several versions, corresponding to several possible simplifications in the form of the input coefficient matrix: symmetric, triangular, banded, positive definite, etc. If you have a large matrix in one of these forms, you should certainly take advantage of the increased efficiency provided by these different routines, and not just use the form provided for general matrices. There is also a great watershed dividing routines that are direct (i.e., execute in a predictable number of operations) from routines that are iterative (i.e., attempt to converge to the desired answer in however many steps are necessary). Iterative methods become preferable when the battle against loss of significance is in danger of being lost, either due to large N or because the problem is close to singular. We will treat iterative methods only incompletely in this book, in §2.7 and in Chapters 18 and 19. These methods are important, but mostly beyond our scope. We will, however, discuss in detail a technique which is on the borderline between direct and iterative methods, namely the iterative improvement of a solution that has been obtained by direct methods (§2.5).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
(AT · A) · x = (AT · b)
2.1 Gauss-Jordan Elimination
27
CITED REFERENCES AND FURTHER READING: Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press). Gill, P.E., Murray, W., and Wright, M.H. 1991, Numerical Linear Algebra and Optimization, vol. 1 (Redwood City, CA: Addison-Wesley).
Dongarra, J.J., et al. 1979, LINPACK User’s Guide (Philadelphia: S.I.A.M.). Coleman, T.F., and Van Loan, C. 1988, Handbook for Matrix Computations (Philadelphia: S.I.A.M.). Forsythe, G.E., and Moler, C.B. 1967, Computer Solution of Linear Algebraic Systems (Englewood Cliffs, NJ: Prentice-Hall). Wilkinson, J.H., and Reinsch, C. 1971, Linear Algebra, vol. II of Handbook for Automatic Computation (New York: Springer-Verlag). Westlake, J.R. 1968, A Handbook of Numerical Matrix Inversion and Solution of Linear Equations (New York: Wiley). Johnson, L.W., and Riess, R.D. 1982, Numerical Analysis, 2nd ed. (Reading, MA: AddisonWesley), Chapter 2. Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), Chapter 9.
2.1 Gauss-Jordan Elimination For inverting a matrix, Gauss-Jordan elimination is about as efficient as any other method. For solving sets of linear equations, Gauss-Jordan elimination produces both the solution of the equations for one or more right-hand side vectors b, and also the matrix inverse A−1 . However, its principal weaknesses are (i) that it requires all the right-hand sides to be stored and manipulated at the same time, and (ii) that when the inverse matrix is not desired, Gauss-Jordan is three times slower than the best alternative technique for solving a single linear set (§2.3). The method’s principal strength is that it is as stable as any other direct method, perhaps even a bit more stable when full pivoting is used (see below). If you come along later with an additional right-hand side vector, you can multiply it by the inverse matrix, of course. This does give an answer, but one that is quite susceptible to roundoff error, not nearly as good as if the new vector had been included with the set of right-hand side vectors in the first instance. For these reasons, Gauss-Jordan elimination should usually not be your method of first choice, either for solving linear equations or for matrix inversion. The decomposition methods in §2.3 are better. Why do we give you Gauss-Jordan at all? Because it is straightforward, understandable, solid as a rock, and an exceptionally good “psychological” backup for those times that something is going wrong and you think it might be your linear-equation solver. Some people believe that the backup is more than psychological, that GaussJordan elimination is an “independent” numerical method. This turns out to be mostly myth. Except for the relatively minor differences in pivoting, described below, the actual sequence of operations performed in Gauss-Jordan elimination is very closely related to that performed by the routines in the next two sections.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag), Chapter 4.
28
Chapter 2.
Solution of Linear Algebraic Equations
Elimination on Column-Augmented Matrices Consider the linear matrix equation
a11 a21 a31 a41
a12 a22 a32 a42
a13 a23 a33 a43
=
a14 x11 x12 x13 y11 a24 x21 x22 x23 y21 t x t x t y a34 · x31 32 33 31 a44 x41 x42 x43 y41
b11 b12 b13 1 b21 t b22 t b23 t 0 0 b31 b32 b33 0 b41 b42 b43
0 1 0 0
0 0 1 0
y12 y22 y32 y42
y13 y23 y33 y43
y14 y24 y34 y44
0 0 0 1
(2.1.1)
Here the raised dot (·) signifies matrix multiplication, while the operator t just signifies column augmentation, that is, removing the abutting parentheses and making a wider matrix out of the operands of the t operator. It should not take you long to write out equation (2.1.1) and to see that it simply states that xij is the ith component (i = 1, 2, 3, 4) of the vector solution of the jth right-hand side (j = 1, 2, 3), the one whose coefficients are bij , i = 1, 2, 3, 4; and that the matrix of unknown coefficients yij is the inverse matrix of aij . In other words, the matrix solution of [A] · [x1 t x2 t x3 t Y] = [b1 t b2 t b3 t 1]
(2.1.2)
where A and Y are square matrices, the bi ’s and xi ’s are column vectors, and 1 is the identity matrix, simultaneously solves the linear sets A · x1 = b 1
A · x2 = b2
A · x3 = b3
(2.1.3)
and A·Y = 1
(2.1.4)
Now it is also elementary to verify the following facts about (2.1.1): • Interchanging any two rows of A and the corresponding rows of the b’s and of 1, does not change (or scramble in any way) the solution x’s and Y. Rather, it just corresponds to writing the same set of linear equations in a different order. • Likewise, the solution set is unchanged and in no way scrambled if we replace any row in A by a linear combination of itself and any other row, as long as we do the same linear combination of the rows of the b’s and 1 (which then is no longer the identity matrix, of course).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
For clarity, and to avoid writing endless ellipses (· · ·) we will write out equations only for the case of four equations and four unknowns, and with three different righthand side vectors that are known in advance. You can write bigger matrices and extend the equations to the case of N × N matrices, with M sets of right-hand side vectors, in completely analogous fashion. The routine implemented below is, of course, general.
2.1 Gauss-Jordan Elimination
29
Pivoting In “Gauss-Jordan elimination with no pivoting,” only the second operation in the above list is used. The first row is divided by the element a11 (this being a trivial linear combination of the first row with any other row — zero coefficient for the other row). Then the right amount of the first row is subtracted from each other row to make all the remaining ai1 ’s zero. The first column of A now agrees with the identity matrix. We move to the second column and divide the second row by a22 , then subtract the right amount of the second row from rows 1, 3, and 4, so as to make their entries in the second column zero. The second column is now reduced to the identity form. And so on for the third and fourth columns. As we do these operations to A, we of course also do the corresponding operations to the b’s and to 1 (which by now no longer resembles the identity matrix in any way!). Obviously we will run into trouble if we ever encounter a zero element on the (then current) diagonal when we are going to divide by the diagonal element. (The element that we divide by, incidentally, is called the pivot element or pivot.) Not so obvious, but true, is the fact that Gauss-Jordan elimination with no pivoting (no use of the first or third procedures in the above list) is numerically unstable in the presence of any roundoff error, even when a zero pivot is not encountered. You must never do Gauss-Jordan elimination (or Gaussian elimination, see below) without pivoting! So what is this magic pivoting? Nothing more than interchanging rows (partial pivoting) or rows and columns (full pivoting), so as to put a particularly desirable element in the diagonal position from which the pivot is about to be selected. Since we don’t want to mess up the part of the identity matrix that we have already built up, we can choose among elements that are both (i) on rows below (or on) the one that is about to be normalized, and also (ii) on columns to the right (or on) the column we are about to eliminate. Partial pivoting is easier than full pivoting, because we don’t have to keep track of the permutation of the solution vector. Partial pivoting makes available as pivots only the elements already in the correct column. It turns out that partial pivoting is “almost” as good as full pivoting, in a sense that can be made mathematically precise, but which need not concern us here (for discussion and references, see [1]). To show you both variants, we do full pivoting in the routine in this section, partial pivoting in §2.3. We have to state how to recognize a particularly desirable pivot when we see one. The answer to this is not completely known theoretically. It is known, both theoretically and in practice, that simply picking the largest (in magnitude) available element as the pivot is a very good choice. A curiosity of this procedure, however, is that the choice of pivot will depend on the original scaling of the equations. If we take the third linear equation in our original set and multiply it by a factor of a million, it
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
• Interchanging any two columns of A gives the same solution set only if we simultaneously interchange corresponding rows of the x’s and of Y. In other words, this interchange scrambles the order of the rows in the solution. If we do this, we will need to unscramble the solution by restoring the rows to their original order. Gauss-Jordan elimination uses one or more of the above operations to reduce the matrix A to the identity matrix. When this is accomplished, the right-hand side becomes the solution set, as one sees instantly from (2.1.2).
30
Chapter 2.
Solution of Linear Algebraic Equations
Finally, let us consider the storage requirements of the method. With a little reflection you will see that at every stage of the algorithm, either an element of A is predictably a one or zero (if it is already in a part of the matrix that has been reduced to identity form) or else the exactly corresponding element of the matrix that started as 1 is predictably a one or zero (if its mate in A has not been reduced to the identity form). Therefore the matrix 1 does not have to exist as separate storage: The matrix inverse of A is gradually built up in A as the original A is destroyed. Likewise, the solution vectors x can gradually replace the right-hand side vectors b and share the same storage, since after each column in A is reduced, the corresponding row entry in the b’s is never again used. Here is the routine for Gauss-Jordan elimination with full pivoting:
*
SUBROUTINE gaussj(a,n,np,b,m,mp) INTEGER m,mp,n,np,NMAX REAL a(np,np),b(np,mp) PARAMETER (NMAX=50) Linear equation solution by Gauss-Jordan elimination, equation (2.1.1) above. a(1:n,1:n) is an input matrix stored in an array of physical dimensions np by np. b(1:n,1:m) is an input matrix containing the m right-hand side vectors, stored in an array of physical dimensions np by mp. On output, a(1:n,1:n) is replaced by its matrix inverse, and b(1:n,1:m) is replaced by the corresponding set of solution vectors. Parameter: NMAX is the largest anticipated value of n. INTEGER i,icol,irow,j,k,l,ll,indxc(NMAX),indxr(NMAX), ipiv(NMAX) The integer arrays ipiv, indxr, and indxc are used REAL big,dum,pivinv for bookkeeping on the pivoting. do 11 j=1,n ipiv(j)=0 enddo 11 do 22 i=1,n This is the main loop over the columns to be rebig=0. duced. do 13 j=1,n This is the outer loop of the search for a pivot eleif(ipiv(j).ne.1)then ment. do 12 k=1,n if (ipiv(k).eq.0) then if (abs(a(j,k)).ge.big)then big=abs(a(j,k)) irow=j icol=k endif else if (ipiv(k).gt.1) then pause ’singular matrix in gaussj’ endif enddo 12 endif enddo 13 ipiv(icol)=ipiv(icol)+1 We now have the pivot element, so we interchange rows, if needed, to put the pivot element on the diagonal. The columns are not physically interchanged, only relabeled:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
is almost guaranteed that it will contribute the first pivot; yet the underlying solution of the equations is not changed by this multiplication! One therefore sometimes sees routines which choose as pivot that element which would have been largest if the original equations had all been scaled to have their largest coefficient normalized to unity. This is called implicit pivoting. There is some extra bookkeeping to keep track of the scale factors by which the rows would have been multiplied. (The routines in §2.3 include implicit pivoting, but the routine in this section does not.)
2.1 Gauss-Jordan Elimination
31
Row versus Column Elimination Strategies The above discussion can be amplified by a modest amount of formalism. Row operations on a matrix A correspond to pre- (that is, left-) multiplication by some simple
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
indxc(i), the column of the ith pivot element, is the ith column that is reduced, while indxr(i) is the row in which that pivot element was originally located. If indxr(i) 6= indxc(i) there is an implied column interchange. With this form of bookkeeping, the solution b’s will end up in the correct order, and the inverse matrix will be scrambled by columns. if (irow.ne.icol) then do 14 l=1,n dum=a(irow,l) a(irow,l)=a(icol,l) a(icol,l)=dum enddo 14 do 15 l=1,m dum=b(irow,l) b(irow,l)=b(icol,l) b(icol,l)=dum enddo 15 endif indxr(i)=irow We are now ready to divide the pivot row by the pivot indxc(i)=icol element, located at irow and icol. if (a(icol,icol).eq.0.) pause ’singular matrix in gaussj’ pivinv=1./a(icol,icol) a(icol,icol)=1. do 16 l=1,n a(icol,l)=a(icol,l)*pivinv enddo 16 do 17 l=1,m b(icol,l)=b(icol,l)*pivinv enddo 17 do 21 ll=1,n Next, we reduce the rows... if(ll.ne.icol)then ...except for the pivot one, of course. dum=a(ll,icol) a(ll,icol)=0. do 18 l=1,n a(ll,l)=a(ll,l)-a(icol,l)*dum enddo 18 do 19 l=1,m b(ll,l)=b(ll,l)-b(icol,l)*dum enddo 19 endif enddo 21 enddo 22 This is the end of the main loop over columns of the reduction. do 24 l=n,1,-1 It only remains to unscramble the solution in view if(indxr(l).ne.indxc(l))then of the column interchanges. We do this by indo 23 k=1,n terchanging pairs of columns in the reverse order that the permutation was built up. dum=a(k,indxr(l)) a(k,indxr(l))=a(k,indxc(l)) a(k,indxc(l))=dum enddo 23 endif enddo 24 return And we are done. END
32
Chapter 2.
Solution of Linear Algebraic Equations
matrix R. For example, the matrix R with components 1 if i = j and i 6= 2, 4 Rij = 1 if i = 2, j = 4 1 if i = 4, j = 2 0 otherwise
(2.1.5)
A·x =b (· · · R3 · R2 · R1 · A) · x = · · · R3 · R2 · R1 · b (1) · x = · · · R3 · R2 · R1 · b
(2.1.6)
x = · · · R3 · R2 · R1 · b The key point is that since the R’s build from right to left, the right-hand side is simply transformed at each stage from one vector to another. Column operations, on the other hand, correspond to post-, or right-, multiplications by simple matrices, call them C. The matrix in equation (2.1.5), if right-multiplied onto a matrix A, will interchange A’s second and fourth columns. Elimination by column operations involves (conceptually) inserting a column operator, and also its inverse, between the matrix A and the unknown vector x: A·x= b A · C1 ·
C−1 1
·x= b
−1 A · C1 · C2 · C−1 2 · C1 · x = b
(A · C1 · C2 ·
C3 · · ·) · · · C−1 3
·
C−1 2
·
C−1 1
(2.1.7)
·x= b
−1 −1 (1) · · · C−1 3 · C2 · C1 · x = b
which (peeling of the C−1 ’s one at a time) implies a solution x = C 1 · C2 · C3 · · · b
(2.1.8)
Notice the essential difference between equation (2.1.8) and equation (2.1.6). In the latter case, the C’s must be applied to b in the reverse order from that in which they become known. That is, they must all be stored along the way. This requirement greatly reduces the usefulness of column operations, generally restricting them to simple permutations, for example in support of full pivoting.
CITED REFERENCES AND FURTHER READING: Wilkinson, J.H. 1965, The Algebraic Eigenvalue Problem (New York: Oxford University Press). [1] Carnahan, B., Luther, H.A., and Wilkes, J.O. 1969, Applied Numerical Methods (New York: Wiley), Example 5.2, p. 282. Bevington, P.R. 1969, Data Reduction and Error Analysis for the Physical Sciences (New York: McGraw-Hill), Program B-2, p. 298. Westlake, J.R. 1968, A Handbook of Numerical Matrix Inversion and Solution of Linear Equations (New York: Wiley). Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §9.3–1.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
effects the interchange of rows 2 and 4. Gauss-Jordan elimination by row operations alone (including the possibility of partial pivoting) consists of a series of such left-multiplications, yielding successively
2.2 Gaussian Elimination with Backsubstitution
33
2.2 Gaussian Elimination with Backsubstitution
Suppose that in doing Gauss-Jordan elimination, as described in §2.1, we at each stage subtract away rows only below the then-current pivot element. When a22 is the pivot element, for example, we divide the second row by its value (as before), but now use the pivot row to zero only a32 and a42 , not a12 (see equation 2.1.1). Suppose, also, that we do only partial pivoting, never interchanging columns, so that the order of the unknowns never needs to be modified. Then, when we have done this for all the pivots, we will be left with a reduced equation that looks like this (in the case of a single right-hand side vector):
a011 0 0 0
a012 a022 0 0
a013 a023 a033 0
0 a014 x1 b1 0 a24 x2 b02 · = 0 a034 x3 b3 0 a44 x4 b04
(2.2.1)
Here the primes signify that the a’s and b’s do not have their original numerical values, but have been modified by all the row operations in the elimination to this point. The procedure up to this point is termed Gaussian elimination.
Backsubstitution But how do we solve for the x’s? The last x (x4 in this example) is already isolated, namely x4 = b04 /a044
(2.2.2)
With the last x known we can move to the penultimate x, x3 =
1 0 [b − x4 a034 ] a033 3
(2.2.3)
and then proceed with the x before that one. The typical step is N X 1 0 x i = 0 bi − a0ij xj aii
(2.2.4)
j=i+1
The procedure defined by equation (2.2.4) is called backsubstitution. The combination of Gaussian elimination and backsubstitution yields a solution to the set of equations.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The usefulness of Gaussian elimination with backsubstitution is primarily pedagogical. It stands between full elimination schemes such as Gauss-Jordan, and triangular decomposition schemes such as will be discussed in the next section. Gaussian elimination reduces a matrix not all the way to the identity matrix, but only halfway, to a matrix whose components on the diagonal and above (say) remain nontrivial. Let us now see what advantages accrue.
34
Chapter 2.
Solution of Linear Algebraic Equations
CITED REFERENCES AND FURTHER READING: Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §9.3–1. Isaacson, E., and Keller, H.B. 1966, Analysis of Numerical Methods (New York: Wiley), §2.1.
Johnson, L.W., and Riess, R.D. 1982, Numerical Analysis, 2nd ed. (Reading, MA: AddisonWesley), §2.2.1. Westlake, J.R. 1968, A Handbook of Numerical Matrix Inversion and Solution of Linear Equations (New York: Wiley).
2.3 LU Decomposition and Its Applications Suppose we are able to write the matrix A as a product of two matrices, L·U=A
(2.3.1)
where L is lower triangular (has elements only on the diagonal and below) and U is upper triangular (has elements only on the diagonal and above). For the case of
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The advantage of Gaussian elimination and backsubstitution over Gauss-Jordan elimination is simply that the former is faster in raw operations count: The innermost loops of Gauss-Jordan elimination, each containing one subtraction and one multiplication, are executed N 3 and N 2 M times (where there are N equations and M unknowns). The corresponding loops in Gaussian elimination are executed only 13 N 3 times (only half the matrix is reduced, and the increasing numbers of predictable zeros reduce the count to one-third), and 12 N 2 M times, respectively. Each backsubstitution of a right-hand side is 12 N 2 executions of a similar loop (one multiplication plus one subtraction). For M N (only a few right-hand sides) Gaussian elimination thus has about a factor three advantage over Gauss-Jordan. (We could reduce this advantage to a factor 1.5 by not computing the inverse matrix as part of the Gauss-Jordan scheme.) For computing the inverse matrix (which we can view as the case of M = N right-hand sides, namely the N unit vectors which are the columns of the identity matrix), Gaussian elimination and backsubstitution at first glance require 13 N 3 (matrix reduction) + 12 N 3 (right-hand side manipulations) + 12 N 3 (N backsubstitutions) = 43 N 3 loop executions, which is more than the N 3 for Gauss-Jordan. However, the unit vectors are quite special in containing all zeros except for one element. If this is taken into account, the right-side manipulations can be reduced to only 16 N 3 loop executions, and, for matrix inversion, the two methods have identical efficiencies. Both Gaussian elimination and Gauss-Jordan elimination share the disadvantage that all right-hand sides must be known in advance. The LU decomposition method in the next section does not share that deficiency, and also has an equally small operations count, both for solution with any number of right-hand sides, and for matrix inversion. For this reason we will not implement the method of Gaussian elimination as a routine.
35
2.3 LU Decomposition and Its Applications
a 4 × 4 matrix A, for example, equation (2.3.1) would look like this:
α11 α21 α31 α41
0 α22 α32 α42
0 0 α33 α43
0 β11 0 0 · 0 0 α44 0
β12 β22 0 0
β13 β23 β33 0
β14 β24 β34 β44
=
a11 a21 a31 a41
a12 a22 a32 a42
a13 a23 a33 a43
a14 a24 a34 a44
We can use a decomposition such as (2.3.1) to solve the linear set A · x = (L · U) · x = L · (U · x) = b
(2.3.3)
by first solving for the vector y such that L·y=b
(2.3.4)
U·x=y
(2.3.5)
and then solving
What is the advantage of breaking up one linear set into two successive ones? The advantage is that the solution of a triangular set of equations is quite trivial, as we have already seen in §2.2 (equation 2.2.4). Thus, equation (2.3.4) can be solved by forward substitution as follows, y1 = yi =
b1 α11
1 bi − αii
i−1 X
αij yj
(2.3.6) i = 2, 3, . . . , N
j=1
while (2.3.5) can then be solved by backsubstitution exactly as in equations (2.2.2)– (2.2.4), yN βNN N X 1 xi = βij xj yi − βii
xN =
(2.3.7) i = N − 1, N − 2, . . . , 1
j=i+1
Equations (2.3.6) and (2.3.7) total (for each right-hand side b) N 2 executions of an inner loop containing one multiply and one add. If we have N right-hand sides which are the unit column vectors (which is the case when we are inverting a matrix), then taking into account the leading zeros reduces the total execution count of (2.3.6) from 12 N 3 to 16 N 3 , while (2.3.7) is unchanged at 12 N 3 . Notice that, once we have the LU decomposition of A, we can solve with as many right-hand sides as we then care to, one at a time. This is a distinct advantage over the methods of §2.1 and §2.2.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
(2.3.2)
36
Chapter 2.
Solution of Linear Algebraic Equations
Performing the LU Decomposition How then can we solve for L and U, given A? First, we write out the i, jth component of equation (2.3.1) or (2.3.2). That component always is a sum beginning with The number of terms in the sum depends, however, on whether i or j is the smaller number. We have, in fact, the three cases, ij:
αi1 β1j + αi2 β2j + · · · + αii βjj = aij αi1 β1j + αi2 β2j + · · · + αij βjj = aij
(2.3.9) (2.3.10)
Equations (2.3.8)–(2.3.10) total N 2 equations for the N 2 + N unknown α’s and β’s (the diagonal being represented twice). Since the number of unknowns is greater than the number of equations, we are invited to specify N of the unknowns arbitrarily and then try to solve for the others. In fact, as we shall see, it is always possible to take αii ≡ 1
i = 1, . . . , N
(2.3.11)
A surprising procedure, now, is Crout’s algorithm, which quite trivially solves the set of N 2 + N equations (2.3.8)–(2.3.11) for all the α’s and β’s by just arranging the equations in a certain order! That order is as follows: • Set αii = 1, i = 1, . . . , N (equation 2.3.11). • For each j = 1, 2, 3, . . . , N do these two procedures: First, for i = 1, 2, . . ., j, use (2.3.8), (2.3.9), and (2.3.11) to solve for βij , namely βij = aij −
i−1 X
αik βkj .
(2.3.12)
k=1
(When i = 1 in 2.3.12 the summation term is taken to mean zero.) Second, for i = j + 1, j + 2, . . . , N use (2.3.10) to solve for αij , namely ! j−1 X 1 aij − (2.3.13) αik βkj . αij = βjj k=1
Be sure to do both procedures before going on to the next j. If you work through a few iterations of the above procedure, you will see that the α’s and β’s that occur on the right-hand side of equations (2.3.12) and (2.3.13) are already determined by the time they are needed. You will also see that every aij is used only once and never again. This means that the corresponding αij or βij can be stored in the location that the a used to occupy: the decomposition is “in place.” [The diagonal unity elements αii (equation 2.3.11) are not stored at all.] In brief, Crout’s method fills in the combined matrix of α’s and β’s, β11 β12 β13 β14 α21 β22 β23 β24 (2.3.14) α31 α32 β33 β34 α41 α42 α43 β44 by columns from left to right, and within each column from top to bottom (see Figure 2.3.1).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
αi1 β1j + · · · = aij
37
2.3 LU Decomposition and Its Applications
a c e g
i
b d
di
f h
ag
j
on
al
su
bd
el
em
en
ia
ts
go
na
x
le
le
etc.
m
en
ts
Figure 2.3.1. Crout’s algorithm for LU decomposition of a matrix. Elements of the original matrix are modified in the order indicated by lower case letters: a, b, c, etc. Shaded boxes show the previously modified elements that are used in modifying two typical elements, each indicated by an “x”.
What about pivoting? Pivoting (i.e., selection of a salubrious pivot element for the division in equation 2.3.13) is absolutely essential for the stability of Crout’s method. Only partial pivoting (interchange of rows) can be implemented efficiently. However this is enough to make the method stable. This means, incidentally, that we don’t actually decompose the matrix A into LU form, but rather we decompose a rowwise permutation of A. (If we keep track of what that permutation is, this decomposition is just as useful as the original one would have been.) Pivoting is slightly subtle in Crout’s algorithm. The key point to notice is that equation (2.3.12) in the case of i = j (its final application) is exactly the same as equation (2.3.13) except for the division in the latter equation; in both cases the upper limit of the sum is k = j − 1 (= i − 1). This means that we don’t have to commit ourselves as to whether the diagonal element βjj is the one that happens to fall on the diagonal in the first instance, or whether one of the (undivided) αij ’s below it in the column, i = j + 1, . . . , N , is to be “promoted” to become the diagonal β. This can be decided after all the candidates in the column are in hand. As you should be able to guess by now, we will choose the largest one as the diagonal β (pivot element), then do all the divisions by that element en masse. This is Crout’s
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
etc.
x
38
Chapter 2.
Solution of Linear Algebraic Equations
method with partial pivoting. Our implementation has one additional wrinkle: It initially finds the largest element in each row, and subsequently (when it is looking for the maximal pivot element) scales the comparison as if we had initially scaled all the equations to make their maximum coefficient equal to unity; this is the implicit pivoting mentioned in §2.1. Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE ludcmp(a,n,np,indx,d) INTEGER n,np,indx(n),NMAX REAL d,a(np,np),TINY PARAMETER (NMAX=500,TINY=1.0e-20) Largest expected n, and a small number. Given a matrix a(1:n,1:n), with physical dimension np by np, this routine replaces it by the LU decomposition of a rowwise permutation of itself. a and n are input. a is output, arranged as in equation (2.3.14) above; indx(1:n) is an output vector that records the row permutation effected by the partial pivoting; d is output as ±1 depending on whether the number of row interchanges was even or odd, respectively. This routine is used in combination with lubksb to solve linear equations or invert a matrix. INTEGER i,imax,j,k REAL aamax,dum,sum,vv(NMAX) vv stores the implicit scaling of each row. d=1. No row interchanges yet. do 12 i=1,n Loop over rows to get the implicit scaling informaaamax=0. tion. do 11 j=1,n if (abs(a(i,j)).gt.aamax) aamax=abs(a(i,j)) enddo 11 if (aamax.eq.0.) pause ’singular matrix in ludcmp’ No nonzero largest element. vv(i)=1./aamax Save the scaling. enddo 12 do 19 j=1,n This is the loop over columns of Crout’s method. do 14 i=1,j-1 This is equation (2.3.12) except for i = j. sum=a(i,j) do 13 k=1,i-1 sum=sum-a(i,k)*a(k,j) enddo 13 a(i,j)=sum enddo 14 aamax=0. Initialize for the search for largest pivot element. do 16 i=j,n This is i = j of equation (2.3.12) and i = j + 1 . . . N sum=a(i,j) of equation (2.3.13). do 15 k=1,j-1 sum=sum-a(i,k)*a(k,j) enddo 15 a(i,j)=sum dum=vv(i)*abs(sum) Figure of merit for the pivot. if (dum.ge.aamax) then Is it better than the best so far? imax=i aamax=dum endif enddo 16 if (j.ne.imax)then Do we need to interchange rows? do 17 k=1,n Yes, do so... dum=a(imax,k) a(imax,k)=a(j,k) a(j,k)=dum enddo 17 d=-d ...and change the parity of d. vv(imax)=vv(j) Also interchange the scale factor. endif indx(j)=imax if(a(j,j).eq.0.)a(j,j)=TINY If the pivot element is zero the matrix is singular (at least to the precision of the algorithm). For some applications on singular matrices, it is desirable to substitute TINY for zero.
2.3 LU Decomposition and Its Applications
Now, finally, divide by the pivot element.
Go back for the next column in the reduction.
Here is the routine for forward substitution and backsubstitution, implementing equations (2.3.6) and (2.3.7).
SUBROUTINE lubksb(a,n,np,indx,b) INTEGER n,np,indx(n) REAL a(np,np),b(n) Solves the set of n linear equations A · X = B. Here a is input, not as the matrix A but rather as its LU decomposition, determined by the routine ludcmp. indx is input as the permutation vector returned by ludcmp. b(1:n) is input as the right-hand side vector B, and returns with the solution vector X. a, n, np, and indx are not modified by this routine and can be left in place for successive calls with different right-hand sides b. This routine takes into account the possibility that b will begin with many zero elements, so it is efficient for use in matrix inversion. INTEGER i,ii,j,ll REAL sum ii=0 When ii is set to a positive value, it will become the indo 12 i=1,n dex of the first nonvanishing element of b. We now do the forward substitution, equation (2.3.6). The only new ll=indx(i) wrinkle is to unscramble the permutation as we go. sum=b(ll) b(ll)=b(i) if (ii.ne.0)then do 11 j=ii,i-1 sum=sum-a(i,j)*b(j) enddo 11 else if (sum.ne.0.) then ii=i A nonzero element was encountered, so from now on we will endif have to do the sums in the loop above. b(i)=sum enddo 12 do 14 i=n,1,-1 Now we do the backsubstitution, equation (2.3.7). sum=b(i) do 13 j=i+1,n sum=sum-a(i,j)*b(j) enddo 13 b(i)=sum/a(i,i) Store a component of the solution vector X. enddo 14 return All done! END
The LU decomposition in ludcmp requires about 13 N 3 executions of the inner loops (each with one multiply and one add). This is thus the operation count for solving one (or a few) right-hand sides, and is a factor of 3 better than the Gauss-Jordan routine gaussj which was given in §2.1, and a factor of 1.5 better than a Gauss-Jordan routine (not given) that does not compute the inverse matrix. For inverting a matrix, the total count (including the forward and backsubstitution as discussed following equation 2.3.7 above) is ( 13 + 16 + 12 )N 3 = N 3 , the same as gaussj.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
if(j.ne.n)then dum=1./a(j,j) do 18 i=j+1,n a(i,j)=a(i,j)*dum enddo 18 endif enddo 19 return END
39
40
Chapter 2.
Solution of Linear Algebraic Equations
To summarize, this is the preferred way to solve the linear set of equations A · x = b: call ludcmp(a,n,np,indx,d) call lubksb(a,n,np,indx,b)
call lubksb(a,n,np,indx,b)
not, of course, with the original matrix A, but with a and indx as were already returned from ludcmp.
Inverse of a Matrix Using the above LU decomposition and backsubstitution routines, it is completely straightforward to find the inverse of a matrix column by column. INTEGER np,indx(np) REAL a(np,np),y(np,np) ... do 12 i=1,n Set up identity matrix. do 11 j=1,n y(i,j)=0. enddo 11 y(i,i)=1. enddo 12 call ludcmp(a,n,np,indx,d) Decompose the matrix just once. do 13 j=1,n Find inverse by columns. call lubksb(a,n,np,indx,y(1,j)) Note that FORTRAN stores two-dimensional matrices by column, so y(1,j) is the address of the jth column of y. enddo 13
The matrix y will now contain the inverse of the original matrix a, which will have been destroyed. Alternatively, there is nothing wrong with using a Gauss-Jordan routine like gaussj (§2.1) to invert a matrix in place, again destroying the original. Both methods have practically the same operations count. Incidentally, if you ever have the need to compute A−1 · B from matrices A and B, you should LU decompose A and then backsubstitute with the columns of B instead of with the unit vectors that would give A’s inverse. This saves a whole matrix multiplication, and is also more accurate.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The answer x will be returned in b. Your original matrix A will have been destroyed. If you subsequently want to solve a set of equations with the same A but a different right-hand side b, you repeat only
2.3 LU Decomposition and Its Applications
41
Determinant of a Matrix The determinant of an LU decomposed matrix is just the product of the diagonal elements, N Y
βjj
(2.3.15)
j=1
We don’t, recall, compute the decomposition of the original matrix, but rather a decomposition of a rowwise permutation of it. Luckily, we have kept track of whether the number of row interchanges was even or odd, so we just preface the product by the corresponding sign. (You now finally know the purpose of returning d in the routine ludcmp.) Calculation of a determinant thus requires one call to ludcmp, with no subsequent backsubstitutions by lubksb. INTEGER np,indx(np) REAL a(np,np) ... call ludcmp(a,n,np,indx,d) do 11 j=1,n d=d*a(j,j) enddo 11
This returns d as ±1.
The variable d now contains the determinant of the original matrix a, which will have been destroyed. For a matrix of any substantial size, it is quite likely that the determinant will overflow or underflow your computer’s floating-point dynamic range. In this case you can modify the loop of the above fragment and (e.g.) divide by powers of ten, to keep track of the scale separately, or (e.g.) accumulate the sum of logarithms of the absolute values of the factors and the sign separately.
Complex Systems of Equations If your matrix A is real, but the right-hand side vector is complex, say b + id, then (i) LU decompose A in the usual way, (ii) backsubstitute b to get the real part of the solution vector, and (iii) backsubstitute d to get the imaginary part of the solution vector. If the matrix itself is complex, so that you want to solve the system (A + iC) · (x + iy) = (b + id)
(2.3.16)
then there are two possible ways to proceed. The best way is to rewrite ludcmp and lubksb as complex routines. Complex modulus substitutes for absolute value in the construction of the scaling vector vv and in the search for the largest pivot elements. Everything else goes through in the obvious way, with complex arithmetic used as needed. A quick-and-dirty way to solve complex systems is to take the real and imaginary parts of (2.3.16), giving A·x−C·y=b C·x+A·y=d which can be written as a 2N × 2N set of real equations, A −C x b · = C A y d
(2.3.17)
(2.3.18)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
det =
42
Chapter 2.
Solution of Linear Algebraic Equations
and then solved with ludcmp and lubksb in their present forms. This scheme is a factor of 2 inefficient in storage, since A and C are stored twice. It is also a factor of 2 inefficient in time, since the complex multiplies in a complexified version of the routines would each use 4 real multiplies, while the solution of a 2N × 2N problem involves 8 times the work of an N × N one. If you can tolerate these factor-of-two inefficiencies, then equation (2.3.18) is an easy way to proceed.
Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press), Chapter 4. Dongarra, J.J., et al. 1979, LINPACK User’s Guide (Philadelphia: S.I.A.M.). Forsythe, G.E., Malcolm, M.A., and Moler, C.B. 1977, Computer Methods for Mathematical Computations (Englewood Cliffs, NJ: Prentice-Hall), §3.3, and p. 50. Forsythe, G.E., and Moler, C.B. 1967, Computer Solution of Linear Algebraic Systems (Englewood Cliffs, NJ: Prentice-Hall), Chapters 9, 16, and 18. Westlake, J.R. 1968, A Handbook of Numerical Matrix Inversion and Solution of Linear Equations (New York: Wiley). Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag), §4.2. Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §9.11. Horn, R.A., and Johnson, C.R. 1985, Matrix Analysis (Cambridge: Cambridge University Press).
2.4 Tridiagonal and Band Diagonal Systems of Equations The special case of a system of linear equations that is tridiagonal, that is, has nonzero elements only on the diagonal plus or minus one column, is one that occurs frequently. Also common are systems that are band diagonal, with nonzero elements only along a few diagonal lines adjacent to the main diagonal (above and below). For tridiagonal sets, the procedures of LU decomposition, forward- and backsubstitution each take only O(N ) operations, and the whole solution can be encoded very concisely. The resulting routine tridag is one that we will use in later chapters. Naturally, one does not reserve storage for the full N × N matrix, but only for the nonzero components, stored as three vectors. The set of equations to be solved is
b1 a2
c1 b2
0 c2
··· ··· ··· · · · aN−1 ··· 0
bN−1 aN
u1 r1 u2 r2 · ··· = ··· cN−1 uN−1 rN−1 bN uN rN
(2.4.1)
Notice that a1 and cN are undefined and are not referenced by the routine that follows.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
CITED REFERENCES AND FURTHER READING:
2.4 Tridiagonal and Band Diagonal Systems of Equations
43
There is no pivoting in tridag. It is for this reason that tridag can fail (pause) even when the underlying matrix is nonsingular: A zero pivot can be encountered even for a nonsingular matrix. In practice, this is not something to lose sleep about. The kinds of problems that lead to tridiagonal linear sets usually have additional properties which guarantee that the algorithm in tridag will succeed. For example, if |bj | > |aj | + |cj |
j = 1, . . . , N
(2.4.2)
(called diagonal dominance) then it can be shown that the algorithm cannot encounter a zero pivot. It is possible to construct special examples in which the lack of pivoting in the algorithm causes numerical instability. In practice, however, such instability is almost never encountered — unlike the general matrix problem where pivoting is essential. The tridiagonal algorithm is the rare case of an algorithm that, in practice, is more robust than theory says it should be. Of course, should you ever encounter a problem for which tridag fails, you can instead use the more general method for band diagonal systems, now described (routines bandec and banbks). Some other matrix forms consisting of tridiagonal with a small number of additional elements (e.g., upper right and lower left corners) also allow rapid solution; see §2.7.
Band Diagonal Systems Where tridiagonal systems have nonzero elements only on the diagonal plus or minus one, band diagonal systems are slightly more general and have (say) m1 ≥ 0 nonzero elements immediately to the left of (below) the diagonal and m2 ≥ 0 nonzero elements immediately to its right (above it). Of course, this is only a useful classification if m1 and m2 are both N . In that case, the solution of the linear system by LU decomposition can be accomplished much faster, and in much less storage, than for the general N × N case.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE tridag(a,b,c,r,u,n) INTEGER n,NMAX REAL a(n),b(n),c(n),r(n),u(n) PARAMETER (NMAX=500) Solves for a vector u(1:n) of length n the tridiagonal linear set given by equation (2.4.1). a(1:n), b(1:n), c(1:n), and r(1:n) are input vectors and are not modified. Parameter: NMAX is the maximum expected value of n. INTEGER j REAL bet,gam(NMAX) One vector of workspace, gam is needed. if(b(1).eq.0.)pause ’tridag: rewrite equations’ If this happens then you should rewrite your equations as a set of order N − 1, with u2 trivially eliminated. bet=b(1) u(1)=r(1)/bet do 11 j=2,n Decomposition and forward substitution. gam(j)=c(j-1)/bet bet=b(j)-a(j)*gam(j) if(bet.eq.0.)pause ’tridag failed’ Algorithm fails; see below. u(j)=(r(j)-a(j)*u(j-1))/bet enddo 11 do 12 j=n-1,1,-1 Backsubstitution. u(j)=u(j)-gam(j+1)*u(j+1) enddo 12 return END
44
Chapter 2.
Solution of Linear Algebraic Equations
The precise definition of a band diagonal matrix with elements aij is that aij = 0
j > i + m2
when
or
i > j + m1
(2.4.3)
3 4 9 0 0 0 0
1 1 2 3 0 0 0
0 5 6 5 7 0 0
0 0 5 8 9 3 0
0 0 0 9 3 8 2
0 0 0 0 2 4 4
0 0 0 0 0 6 4
(2.4.4)
which has N = 7, m1 = 2, and m2 = 1, is stored compactly as the 7 × 4 matrix, x x x 4 9 2 3 5 7 9 3 8 2 4
3 1 6 8 3 4 4
1 5 5 9 2 6 x
(2.4.5)
Here x denotes elements that are wasted space in the compact format; these will not be referenced by any manipulations and can have arbitrary values. Notice that the diagonal of the original matrix appears in column m1 + 1, with subdiagonal elements to its left, superdiagonal elements to its right. The simplest manipulation of a band diagonal matrix, stored compactly, is to multiply it by a vector to its right. Although this is algorithmically trivial, you might want to study the following routine carefully, as an example of how to pull nonzero elements aij out of the compact storage format in an orderly fashion. Notice that, as always, the logical and physical dimensions of a two-dimensional array can be different. Our convention is to pass N , m1 , m2 , and the physical dimensions np ≥ N and mp ≥ m1 + 1 + m2 . SUBROUTINE banmul(a,n,m1,m2,np,mp,x,b) INTEGER m1,m2,mp,n,np REAL a(np,mp),b(n),x(n) Matrix multiply b = A · x, where A is band diagonal with m1 rows below the diagonal and m2 rows above. The input vector x and output vector b are stored as x(1:n) and b(1:n), respectively. The array a(1:n,1:m1+m2+1) stores A as follows: The diagonal elements are in a(1:n,m1+1). Subdiagonal elements are in a(j :n,1:m1) (with j > 1 appropriate to the number of elements on each subdiagonal). Superdiagonal elements are in a(1:j ,m1+2:m1+m2+1) with j < n appropriate to the number of elements on each superdiagonal. INTEGER i,j,k do 12 i=1,n b(i)=0. k=i-m1-1 do 11 j=max(1,1-k),min(m1+m2+1,n-k) b(i)=b(i)+a(i,j)*x(j+k) enddo 11 enddo 12 return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Band diagonal matrices are stored and manipulated in a so-called compact form, which results if the matrix is tilted 45◦ clockwise, so that its nonzero elements lie in a long, narrow matrix with m1 + 1 + m2 columns and N rows. This is best illustrated by an example: The band diagonal matrix
2.4 Tridiagonal and Band Diagonal Systems of Equations
45
SUBROUTINE bandec(a,n,m1,m2,np,mp,al,mpl,indx,d) INTEGER m1,m2,mp,mpl,n,np,indx(n) REAL d,a(np,mp),al(np,mpl),TINY PARAMETER (TINY=1.e-20) Given an n × n band diagonal matrix A with m1 subdiagonal rows and m2 superdiagonal rows, compactly stored in the array a(1:n,1:m1+m2+1) as described in the comment for routine banmul, this routine constructs an LU decomposition of a rowwise permutation of A. The upper triangular matrix replaces a, while the lower triangular matrix is returned in al(1:n,1:m1). indx(1:n) is an output vector which records the row permutation effected by the partial pivoting; d is output as ±1 depending on whether the number of row interchanges was even or odd, respectively. This routine is used in combination with banbks to solve band-diagonal sets of equations. INTEGER i,j,k,l,mm REAL dum mm=m1+m2+1 if(mm.gt.mp.or.m1.gt.mpl.or.n.gt.np) pause ’bad args in bandec’ l=m1 do 13 i=1,m1 Rearrange the storage a bit. do 11 j=m1+2-i,mm a(i,j-l)=a(i,j) enddo 11 l=l-1 do 12 j=mm-l,mm a(i,j)=0. enddo 12 enddo 13 d=1. l=m1 do 18 k=1,n For each row... dum=a(k,1) i=k if(l.lt.n)l=l+1 do 14 j=k+1,l Find the pivot element. if(abs(a(j,1)).gt.abs(dum))then dum=a(j,1) i=j endif enddo 14 indx(k)=i if(dum.eq.0.) a(k,1)=TINY Matrix is algorithmically singular, but proceed anyway with TINY pivot (desirable in some applications). if(i.ne.k)then Interchange rows. d=-d do 15 j=1,mm dum=a(k,j) a(k,j)=a(i,j) a(i,j)=dum enddo 15 endif do 17 i=k+1,l Do the elimination. dum=a(i,1)/a(k,1) al(k,i-k)=dum do 16 j=2,mm
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
It is not possible to store the LU decomposition of a band diagonal matrix A quite as compactly as the compact form of A itself. The decomposition (essentially by Crout’s method, see §2.3) produces additional nonzero “fill-ins.” One straightforward storage scheme is to return the upper triangular factor (U ) in the same space that A previously occupied, and to return the lower triangular factor (L) in a separate compact matrix of size N × m1 . The diagonal elements of U (whose product, times d = ±1, gives the determinant) are returned in the first column of A’s storage space. The following routine, bandec, is the band-diagonal analog of ludcmp in §2.3:
46
Chapter 2.
Solution of Linear Algebraic Equations
a(i,j-1)=a(i,j)-dum*a(k,j) enddo 16 a(i,mm)=0. enddo 17 enddo 18 return END
SUBROUTINE banbks(a,n,m1,m2,np,mp,al,mpl,indx,b) INTEGER m1,m2,mp,mpl,n,np,indx(n) REAL a(np,mp),al(np,mpl),b(n) Given the arrays a, al, and indx as returned from bandec, and given a right-hand side vector b(1:n), solves the band diagonal linear equations A · x = b. The solution vector x overwrites b(1:n). The other input arrays are not modified, and can be left in place for successive calls with different right-hand sides. INTEGER i,k,l,mm REAL dum mm=m1+m2+1 if(mm.gt.mp.or.m1.gt.mpl.or.n.gt.np) pause ’bad args in banbks’ l=m1 do 12 k=1,n Forward substitution, unscrambling the permuted rows as we i=indx(k) go. if(i.ne.k)then dum=b(k) b(k)=b(i) b(i)=dum endif if(l.lt.n)l=l+1 do 11 i=k+1,l b(i)=b(i)-al(k,i-k)*b(k) enddo 11 enddo 12 l=1 do 14 i=n,1,-1 Backsubstitution. dum=b(i) do 13 k=2,l dum=dum-a(i,k)*b(k+i-1) enddo 13 b(i)=dum/a(i,1) if(l.lt.mm) l=l+1 enddo 14 return END
The routines bandec and banbks are based on the Handbook routines bandet1 and bansol1 in [1].
CITED REFERENCES AND FURTHER READING: Keller, H.B. 1968, Numerical Methods for Two-Point Boundary-Value Problems (Waltham, MA: Blaisdell), p. 74.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Some pivoting is possible within the storage limitations of bandec, and the above routine does take advantage of the opportunity. In general, when TINY is returned as a diagonal element of U , then the original matrix (perhaps as modified by roundoff error) is in fact singular. In this regard, bandec is somewhat more robust than tridag above, which can fail algorithmically even for nonsingular matrices; bandec is thus also useful (with m1 = m2 = 1) for some ill-behaved tridiagonal systems. Once the matrix A has been decomposed, any number of right-hand sides can be solved in turn by repeated calls to banbks, the backsubstitution routine whose analog in §2.3 is lubksb.
2.5 Iterative Improvement of a Solution to Linear Equations
47
Dahlquist, G., and Bjorck, A. 1974, Numerical Methods (Englewood Cliffs, NJ: Prentice-Hall), Example 5.4.3, p. 166. Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §9.11. Wilkinson, J.H., and Reinsch, C. 1971, Linear Algebra, vol. II of Handbook for Automatic Computation (New York: Springer-Verlag), Chapter I/6. [1]
2.5 Iterative Improvement of a Solution to Linear Equations Obviously it is not easy to obtain greater precision for the solution of a linear set than the precision of your computer’s floating-point word. Unfortunately, for large sets of linear equations, it is not always easy to obtain precision equal to, or even comparable to, the computer’s limit. In direct methods of solution, roundoff errors accumulate, and they are magnified to the extent that your matrix is close to singular. You can easily lose two or three significant figures for matrices which (you thought) were far from singular. If this happens to you, there is a neat trick to restore the full machine precision, called iterative improvement of the solution. The theory is very straightforward (see Figure 2.5.1): Suppose that a vector x is the exact solution of the linear set A·x=b
(2.5.1)
You don’t, however, know x. You only know some slightly wrong solution x + δx, where δx is the unknown error. When multiplied by the matrix A, your slightly wrong solution gives a product slightly discrepant from the desired right-hand side b, namely A · (x + δx) = b + δb
(2.5.2)
Subtracting (2.5.1) from (2.5.2) gives A · δx = δb
(2.5.3)
But (2.5.2) can also be solved, trivially, for δb. Substituting this into (2.5.3) gives A · δx = A · (x + δx) − b
(2.5.4)
In this equation, the whole right-hand side is known, since x + δx is the wrong solution that you want to improve. It is essential to calculate the right-hand side in double precision, since there will be a lot of cancellation in the subtraction of b. Then, we need only solve (2.5.4) for the error δx, then subtract this from the wrong solution to get an improved solution. An important extra benefit occurs if we obtained the original solution by LU decomposition. In this case we already have the LU decomposed form of A, and all we need do to solve (2.5.4) is compute the right-hand side and backsubstitute! The code to do all this is concise and straightforward:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press), §4.3.
48
Chapter 2.
Solution of Linear Algebraic Equations
A
δx x+
b
δx
δb
A− 1
Figure 2.5.1. Iterative improvement of the solution to A · x = b. The first guess x + δx is multiplied by A to produce b + δb. The known vector b is subtracted, giving δb. The linear set with this right-hand side is inverted, giving δx. This is subtracted from the first guess giving an improved solution x.
C
SUBROUTINE mprove(a,alud,n,np,indx,b,x) INTEGER n,np,indx(n),NMAX REAL a(np,np),alud(np,np),b(n),x(n) PARAMETER (NMAX=500) Maximum anticipated value of n. USES lubksb Improves a solution vector x(1:n) of the linear set of equations A · X = B. The matrix a(1:n,1:n), and the vectors b(1:n) and x(1:n) are input, as is the dimension n. Also input is alud, the LU decomposition of a as returned by ludcmp, and the vector indx also returned by that routine. On output, only x(1:n) is modified, to an improved set of values. INTEGER i,j REAL r(NMAX) DOUBLE PRECISION sdp do 12 i=1,n Calculate the right-hand side, accumulating the residsdp=-b(i) ual in double precision. do 11 j=1,n sdp=sdp+dble(a(i,j))*dble(x(j)) enddo 11 r(i)=sdp enddo 12 call lubksb(alud,n,np,indx,r) Solve for the error term, do 13 i=1,n and subtract it from the old solution. x(i)=x(i)-r(i) enddo 13 return END
You should note that the routine ludcmp in §2.3 destroys the input matrix as it LU decomposes it. Since iterative improvement requires both the original matrix and its LU decomposition, you will need to copy A before calling ludcmp. Likewise lubksb destroys b in obtaining x, so make a copy of b also. If you don’t mind this extra storage, iterative improvement is highly recommended: It is a process of order only N 2 operations (multiply vector by matrix, and backsubstitute — see discussion following equation 2.3.7); it never hurts; and it can really give you your money’s worth if it saves an otherwise ruined solution on which you have already spent of order N 3 operations.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
b + δb x
49
2.5 Iterative Improvement of a Solution to Linear Equations
You can call mprove several times in succession if you want. Unless you are starting quite far from the true solution, one call is generally enough; but a second call to verify convergence can be reassuring.
More on Iterative Improvement
R ≡ 1 − B0 · A
(2.5.5)
which is supposed to be “small” (we will be more precise below). Note that therefore B0 · A = 1 − R
(2.5.6)
Next consider the following formal manipulation: −1 −1 A−1 = A−1 · (B−1 · B−1 · B0 0 · B0 ) = (A 0 ) · B0 = (B0 · A)
= (1 − R)−1 · B0 = (1 + R + R2 + R3 + · · ·) · B0
(2.5.7)
We can define the nth partial sum of the last expression by Bn ≡ (1 + R + · · · + Rn ) · B0
(2.5.8)
so that B∞ → A−1 , if the limit exists. It now is straightforward to verify that equation (2.5.8) satisfies some interesting recurrence relations. As regards solving A · x = b, where x and b are vectors, define x n ≡ Bn · b
(2.5.9)
xn+1 = xn + B0 · (b − A · xn )
(2.5.10)
Then it is easy to show that This is immediately recognizable as equation (2.5.4), with −δx = xn+1 − xn , and with B0 taking the role of A−1 . We see, therefore, that equation (2.5.4) does not require that the LU decompositon of A be exact, but only that the implied residual R be small. In rough terms, if the residual is smaller than the square root of your computer’s roundoff error, then after one application of equation (2.5.10) (that is, going from x0 ≡ B0 · b to x1 ) the first neglected term, of order R2 , will be smaller than the roundoff error. Equation (2.5.10), like equation (2.5.4), moreover, can be applied more than once, since it uses only B0 , and not any of the higher B’s. A much more surprising recurrence which follows from equation (2.5.8) is one that more than doubles the order n at each stage: B2n+1 = 2Bn − Bn · A · Bn
n = 0, 1, 3, 7, . . .
(2.5.11)
Repeated application of equation (2.5.11), from a suitable starting matrix B0 , converges quadratically to the unknown inverse matrix A−1 (see §9.4 for the definition of “quadratically”). Equation (2.5.11) goes by various names, including Schultz’s Method and Hotelling’s Method; see Pan and Reif [1] for references. In fact, equation (2.5.11) is simply the iterative Newton-Raphson method of root-finding (§9.4) applied to matrix inversion. Before you get too excited about equation (2.5.11), however, you should notice that it involves two full matrix multiplications at each iteration. Each matrix multiplication involves N 3 adds and multiplies. But we already saw in §§2.1–2.3 that direct inversion of A requires only N 3 adds and N 3 multiplies in toto. Equation (2.5.11) is therefore practical only when special circumstances allow it to be evaluated much more rapidly than is the case for general matrices. We will meet such circumstances later, in §13.10.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
It is illuminating (and will be useful later in the book) to give a somewhat more solid analytical foundation for equation (2.5.4), and also to give some additional results. Implicit in the previous discussion was the notion that the solution vector x + δx has an error term; but we neglected the fact that the LU decomposition of A is itself not exact. A different analytical approach starts with some matrix B0 that is assumed to be an approximate inverse of the matrix A, so that B0 · A is approximately the identity matrix 1. Define the residual matrix R of B0 as
50
Chapter 2.
Solution of Linear Algebraic Equations
In the spirit of delayed gratification, let us nevertheless pursue the two related issues: When does the series in equation (2.5.7) converge; and what is a suitable initial guess B0 (if, for example, an initial LU decomposition is not feasible)? We can define the norm of a matrix as the largest amplification of length that it is able to induce on a vector, |R · v| |v|
(2.5.12)
If we let equation (2.5.7) act on some arbitrary right-hand side b, as one wants a matrix inverse to do, it is obvious that a sufficient condition for convergence is kRk < 1
(2.5.13)
Pan and Reif [1] point out that a suitable initial guess for B0 is any sufficiently small constant times the matrix transpose of A, that is, B0 = AT
R = 1 − AT · A
or
(2.5.14)
To see why this is so involves concepts from Chapter 11; we give here only the briefest sketch: AT · A is a symmetric, positive definite matrix, so it has real, positive eigenvalues. In its diagonal representation, R takes the form R = diag(1 − λ1 , 1 − λ2 , . . . , 1 − λN )
(2.5.15)
where all the λi ’s are positive. Evidently any satisfying 0 < < 2/(maxi λi ) will give kRk < 1. It is not difficult to show that the optimal choice for , giving the most rapid convergence for equation (2.5.11), is = 2/(max λi + min λi ) i
(2.5.16)
i
Rarely does one know the eigenvalues of AT · A in equation (2.5.16). Pan and Reif derive several interesting bounds, which are computable directly from A. The following choices guarantee the convergence of Bn as n → ∞, X X X 2 ≤1 ajk or ≤1 max |aij | × max |aij | (2.5.17) j,k
i
j
j
i
The latter expression is truly a remarkable formula, which Pan and Reif derive by noting that the vector norm in equation (2.5.12) need not be the usual L2 norm, but can instead be either the L∞ (max) norm, or the L1 (absolute value) norm. See their work for details. Another approach, with which we have had some success, is to estimate the largest eigenvalue statistically, by calculating si ≡ |A · vi |2 for several unit vector vi ’s with randomly chosen directions in N -space. The largest eigenvalue λ can then be bounded by the maximum of 2 max si and 2N Var(si )/µ(si ), where Var and µ denote the sample variance and mean, respectively. CITED REFERENCES AND FURTHER READING: Johnson, L.W., and Riess, R.D. 1982, Numerical Analysis, 2nd ed. (Reading, MA: AddisonWesley), §2.3.4, p. 55. Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press), p. 74. Dahlquist, G., and Bjorck, A. 1974, Numerical Methods (Englewood Cliffs, NJ: Prentice-Hall), §5.5.6, p. 183. Forsythe, G.E., and Moler, C.B. 1967, Computer Solution of Linear Algebraic Systems (Englewood Cliffs, NJ: Prentice-Hall), Chapter 13. Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §9.5, p. 437. Pan, V., and Reif, J. 1985, in Proceedings of the Seventeenth Annual ACM Symposium on Theory of Computing (New York: Association for Computing Machinery). [1]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
kRk ≡ max v6=0
51
2.6 Singular Value Decomposition
2.6 Singular Value Decomposition
=
A
U
w1 ·
w2
·
··· ···
VT
wN
(2.6.1)
The matrices U and V are each orthogonal in the sense that their columns are orthonormal,
M X
Uik Uin = δkn
1≤k≤N 1≤n≤N
(2.6.2)
Vjk Vjn = δkn
1≤k≤N 1≤n≤N
(2.6.3)
i=1 N X j=1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
There exists a very powerful set of techniques for dealing with sets of equations or matrices that are either singular or else numerically very close to singular. In many cases where Gaussian elimination and LU decomposition fail to give satisfactory results, this set of techniques, known as singular value decomposition, or SVD, will diagnose for you precisely what the problem is. In some cases, SVD will not only diagnose the problem, it will also solve it, in the sense of giving you a useful numerical answer, although, as we shall see, not necessarily “the” answer that you thought you should get. SVD is also the method of choice for solving most linear least-squares problems. We will outline the relevant theory in this section, but defer detailed discussion of the use of SVD in this application to Chapter 15, whose subject is the parametric modeling of data. SVD methods are based on the following theorem of linear algebra, whose proof is beyond our scope: Any M × N matrix A whose number of rows M is greater than or equal to its number of columns N , can be written as the product of an M × N column-orthogonal matrix U, an N × N diagonal matrix W with positive or zero elements (the singular values), and the transpose of an N × N orthogonal matrix V. The various shapes of these matrices will be made clearer by the following tableau:
52
Chapter 2.
Solution of Linear Algebraic Equations
or as a tableau,
UT
U
=
VT
·
=
1
V
(2.6.4) Since V is square, it is also row-orthonormal, V · VT = 1. The SVD decomposition can also be carried out when M < N . In this case the singular values wj for j = M + 1, . . . , N are all zero, and the corresponding columns of U are also zero. Equation (2.6.2) then holds only for k, n ≤ M . The decomposition (2.6.1) can always be done, no matter how singular the matrix is, and it is “almost” unique. That is to say, it is unique up to (i) making the same permutation of the columns of U, elements of W, and columns of V (or rows of VT ), or (ii) forming linear combinations of any columns of U and V whose corresponding elements of W happen to be exactly equal. An important consequence of the permutation freedom is that for the case M < N , a numerical algorithm for the decomposition need not return zero wj ’s for j = M + 1, . . . , N ; the N − M zero singular values can be scattered among all positions j = 1, 2, . . . , N . At the end of this section, we give a routine, svdcmp, that performs SVD on an arbitrary matrix A, replacing it by U (they are the same shape) and returning W and V separately. The routine svdcmp is based on a routine by Forsythe et al. [1], which is in turn based on the original routine of Golub and Reinsch, found, in various forms, in [2-4] and elsewhere. These references include extensive discussion of the algorithm used. As much as we dislike the use of black-box routines, we are going to ask you to accept this one, since it would take us too far afield to cover its necessary background material here. Suffice it to say that the algorithm is very stable, and that it is very unusual for it ever to misbehave. Most of the concepts that enter the algorithm (Householder reduction to bidiagonal form, diagonalization by QR procedure with shifts) will be discussed further in Chapter 11. If you are as suspicious of black boxes as we are, you will want to verify yourself that svdcmp does what we say it does. That is very easy to do: Generate an arbitrary matrix A, call the routine, and then verify by matrix multiplication that (2.6.1) and (2.6.4) are satisfied. Since these two equations are the only defining requirements for SVD, this procedure is (for the chosen A) a complete end-to-end check. Now let us find out what SVD is good for.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
·
2.6 Singular Value Decomposition
53
SVD of a Square Matrix
A−1 = V · [diag (1/wj )] · UT
(2.6.5)
The only thing that can go wrong with this construction is for one of the wj ’s to be zero, or (numerically) for it to be so small that its value is dominated by roundoff error and therefore unknowable. If more than one of the wj ’s have this problem, then the matrix is even more singular. So, first of all, SVD gives you a clear diagnosis of the situation. Formally, the condition number of a matrix is defined as the ratio of the largest (in magnitude) of the wj ’s to the smallest of the wj ’s. A matrix is singular if its condition number is infinite, and it is ill-conditioned if its condition number is too large, that is, if its reciprocal approaches the machine’s floating-point precision (for example, less than 10−6 for single precision or 10−12 for double). For singular matrices, the concepts of nullspace and range are important. Consider the familiar set of simultaneous equations A·x=b
(2.6.6)
where A is a square matrix, b and x are vectors. Equation (2.6.6) defines A as a linear mapping from the vector space x to the vector space b. If A is singular, then there is some subspace of x, called the nullspace, that is mapped to zero, A · x = 0. The dimension of the nullspace (the number of linearly independent vectors x that can be found in it) is called the nullity of A. Now, there is also some subspace of b that can be “reached” by A, in the sense that there exists some x which is mapped there. This subspace of b is called the range of A. The dimension of the range is called the rank of A. If A is nonsingular, then its range will be all of the vector space b, so its rank is N . If A is singular, then the rank will be less than N . In fact, the relevant theorem is “rank plus nullity equals N .” What has this to do with SVD? SVD explicitly constructs orthonormal bases for the nullspace and range of a matrix. Specifically, the columns of U whose same-numbered elements wj are nonzero are an orthonormal set of basis vectors that span the range; the columns of V whose same-numbered elements wj are zero are an orthonormal basis for the nullspace. Now let’s have another look at solving the set of simultaneous linear equations (2.6.6) in the case that A is singular. First, the set of homogeneous equations, where b = 0, is solved immediately by SVD: Any column of V whose corresponding wj is zero yields a solution. When the vector b on the right-hand side is not zero, the important question is whether it lies in the range of A or not. If it does, then the singular set of equations does have a solution x; in fact it has more than one solution, since any vector in the nullspace (any column of V with a corresponding zero wj ) can be added to x in any linear combination.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
If the matrix A is square, N × N say, then U, V, and W are all square matrices of the same size. Their inverses are also trivial to compute: U and V are orthogonal, so their inverses are equal to their transposes; W is diagonal, so its inverse is the diagonal matrix whose elements are the reciprocals of the elements wj . From (2.6.1) it now follows immediately that the inverse of A is
54
Chapter 2.
Solution of Linear Algebraic Equations
If we want to single out one particular member of this solution-set of vectors as 2 a representative, we might want to pick the one with the smallest length |x| . Here is how to find that vector using SVD: Simply replace 1/wj by zero if wj = 0. (It is not very often that one gets to set ∞ = 0 !) Then compute (working from right to left) (2.6.7)
This will be the solution vector of smallest length; the columns of V that are in the nullspace complete the specification of the solution set. Proof: Consider |x + x0 |, where x0 lies in the nullspace. Then, if W−1 denotes the modified inverse of W with some elements zeroed, |x + x0 | = V · W−1 · UT · b + x0 = V · (W−1 · UT · b + VT · x0 ) = W−1 · UT · b + VT · x0
(2.6.8)
Here the first equality follows from (2.6.7), the second and third from the orthonormality of V. If you now examine the two terms that make up the sum on the right-hand side, you will see that the first one has nonzero j components only where wj 6= 0, while the second one, since x0 is in the nullspace, has nonzero j components only where wj = 0. Therefore the minimum length obtains for x0 = 0, q.e.d. If b is not in the range of the singular matrix A, then the set of equations (2.6.6) has no solution. But here is some good news: If b is not in the range of A, then equation (2.6.7) can still be used to construct a “solution” vector x. This vector x will not exactly solve A · x = b. But, among all possible vectors x, it will do the closest possible job in the least squares sense. In other words (2.6.7) finds x which minimizes r ≡ |A · x − b|
(2.6.9)
The number r is called the residual of the solution. The proof is similar to (2.6.8): Suppose we modify x by adding some arbitrary x0 . Then A · x − b is modified by adding some b0 ≡ A · x0 . Obviously b0 is in the range of A. We then have A · x − b + b0 = (U · W · VT ) · (V · W−1 · UT · b) − b + b0 = (U · W · W−1 · UT − 1) · b + b0 = U · (W · W−1 − 1) · UT · b + UT · b0 = (W · W−1 − 1) · UT · b + UT · b0
(2.6.10)
Now, (W · W−1 − 1) is a diagonal matrix which has nonzero j components only for wj = 0, while UT b0 has nonzero j components only for wj 6= 0, since b0 lies in the range of A. Therefore the minimum obtains for b0 = 0, q.e.d. Figure 2.6.1 summarizes our discussion of SVD thus far.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
x = V · [diag (1/wj )] · (UT · b)
55
2.6 Singular Value Decomposition
A
A⋅x = b (a) null space of A solutions of A⋅x = d
solutions of A ⋅ x = c′
SVD “solution” of A ⋅ x = c
range of A
d
c′
c
SVD solution of A⋅x = d ( b) Figure 2.6.1. (a) A nonsingular matrix A maps a vector space into one of the same dimension. The vector x is mapped into b, so that x satisfies the equation A · x = b. (b) A singular matrix A maps a vector space into one of lower dimensionality, here a plane into a line, called the “range” of A. The “nullspace” of A is mapped to zero. The solutions of A · x = d consist of any one particular solution plus any vector in the nullspace, here forming a line parallel to the nullspace. Singular value decomposition (SVD) selects the particular solution closest to zero, as shown. The point c lies outside of the range of A, so A · x = c has no solution. SVD finds the least-squares best compromise solution, namely a solution of A · x = c0 , as shown.
In the discussion since equation (2.6.6), we have been pretending that a matrix either is singular or else isn’t. That is of course true analytically. Numerically, however, the far more common situation is that some of the wj ’s are very small but nonzero, so that the matrix is ill-conditioned. In that case, the direct solution methods of LU decomposition or Gaussian elimination may actually give a formal solution to the set of equations (that is, a zero pivot may not be encountered); but the solution vector may have wildly large components whose algebraic cancellation, when multiplying by the matrix A, may give a very poor approximation to the right-hand vector b. In such cases, the solution vector x obtained by zeroing the
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
b
x
56
Chapter 2.
Solution of Linear Algebraic Equations
SUBROUTINE svbksb(u,w,v,m,n,mp,np,b,x) INTEGER m,mp,n,np,NMAX REAL b(mp),u(mp,np),v(np,np),w(np),x(np) PARAMETER (NMAX=500) Maximum anticipated value of n. Solves A · X = B for a vector X, where A is specified by the arrays u, w, v as returned by svdcmp. m and n are the logical dimensions of a, and will be equal for square matrices. mp and np are the physical dimensions of a. b(1:m) is the input right-hand side. x(1:n) is the output solution vector. No input quantities are destroyed, so the routine may be called sequentially with different b’s. INTEGER i,j,jj REAL s,tmp(NMAX) do 12 j=1,n Calculate U T B. s=0. if(w(j).ne.0.)then Nonzero result only if wj is nonzero. do 11 i=1,m s=s+u(i,j)*b(i) enddo 11 s=s/w(j) This is the divide by wj . endif tmp(j)=s enddo 12 do 14 j=1,n Matrix multiply by V to get answer. s=0. do 13 jj=1,n s=s+v(j,jj)*tmp(jj) enddo 13 x(j)=s enddo 14 return END
Note that a typical use of svdcmp and svbksb superficially resembles the typical use of ludcmp and lubksb: In both cases, you decompose the left-hand matrix A just once, and then can use the decomposition either once or many times with different right-hand sides. The crucial difference is the “editing” of the singular
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
small wj ’s and then using equation (2.6.7) is very often better (in the sense of the residual |A · x − b| being smaller) than both the direct-method solution and the SVD solution where the small wj ’s are left nonzero. It may seem paradoxical that this can be so, since zeroing a singular value corresponds to throwing away one linear combination of the set of equations that we are trying to solve. The resolution of the paradox is that we are throwing away precisely a combination of equations that is so corrupted by roundoff error as to be at best useless; usually it is worse than useless since it “pulls” the solution vector way off towards infinity along some direction that is almost a nullspace vector. In doing this, it compounds the roundoff problem and makes the residual |A · x − b| larger. SVD cannot be applied blindly, then. You have to exercise some discretion in deciding at what threshold to zero the small wj ’s, and/or you have to have some idea what size of computed residual |A · x − b| is acceptable. As an example, here is a “backsubstitution” routine svbksb for evaluating equation (2.6.7) and obtaining a solution vector x from a right-hand side b, given that the SVD of a matrix A has already been calculated by a call to svdcmp. Note that this routine presumes that you have already zeroed the small wj ’s. It does not do this for you. If you haven’t zeroed the small wj ’s, then this routine is just as ill-conditioned as any direct method, and you are misusing SVD.
2.6 Singular Value Decomposition
57
values before svbksb is called:
SVD for Fewer Equations than Unknowns If you have fewer linear equations M than unknowns N , then you are not expecting a unique solution. Usually there will be an N − M dimensional family of solutions. If you want to find this whole solution space, then SVD can readily do the job. The SVD decomposition will yield N − M zero or negligible wj ’s, since M < N . There may be additional zero wj ’s from any degeneracies in your M equations. Be sure that you find this many small wj ’s, and zero them before calling svbksb, which will give you the particular solution vector x. As before, the columns of V corresponding to zeroed wj ’s are the basis vectors whose linear combinations, added to the particular solution, span the solution space.
SVD for More Equations than Unknowns This situation will occur in Chapter 15, when we wish to find the least-squares solution to an overdetermined set of linear equations. In tableau, the equations to be solved are
· x = b
A
(2.6.11)
The proofs that we gave above for the square case apply without modification to the case of more equations than unknowns. The least-squares solution vector x is
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
REAL a(np,np),u(np,np),w(np),v(np,np),b(np),x(np) ... do 12 i=1,n Copy a into u if you don’t want it to be destroyed. do 11 j=1,n u(i,j)=a(i,j) enddo 11 enddo 12 call svdcmp(u,n,n,np,np,w,v) SVD the square matrix a. wmax=0. Will be the maximum singular value obtained. do 13 j=1,n if(w(j).gt.wmax)wmax=w(j) enddo 13 wmin=wmax*1.0e-6 This is where we set the threshold for singular values do 14 j=1,n allowed to be nonzero. The constant is typical, but not universal. You have to experiment with if(w(j).lt.wmin)w(j)=0. your own application. enddo 14 call svbksb(u,w,v,n,n,np,np,b,x) Now we can backsubstitute.
58
Chapter 2.
Solution of Linear Algebraic Equations
given by (2.6.7), which, with nonsquare matrices, looks like this,
V
· diag(1/wj ) ·
UT
· b (2.6.12)
In general, the matrix W will not be singular, and no wj ’s will need to be set to zero. Occasionally, however, there might be column degeneracies in A. In this case you will need to zero some small wj values after all. The corresponding column in V gives the linear combination of x’s that is then ill-determined even by the supposedly overdetermined set. Sometimes, although you do not need to zero any wj ’s for computational reasons, you may nevertheless want to take note of any that are unusually small: Their corresponding columns in Vare linear combinations of x’s which are insensitive to your data. In fact, you may then wish to zero these wj ’s, to reduce the number of free parameters in the fit. These matters are discussed more fully in Chapter 15.
Constructing an Orthonormal Basis Suppose that you have N vectors in an M -dimensional vector space, with N ≤ M . Then the N vectors span some subspace of the full vector space. Often you want to construct an orthonormal set of N vectors that span the same subspace. The textbook way to do this is by Gram-Schmidt orthogonalization, starting with one vector and then expanding the subspace one dimension at a time. Numerically, however, because of the build-up of roundoff errors, naive Gram-Schmidt orthogonalization is terrible. The right way to construct an orthonormal basis for a subspace is by SVD: Form an M × N matrix A whose N columns are your vectors. Run the matrix through svdcmp. The columns of the matrix U (which in fact replaces A on output from svdcmp) are your desired orthonormal basis vectors. You might also want to check the output wj ’s for zero values. If any occur, then the spanned subspace was not, in fact, N dimensional; the columns of U corresponding to zero wj ’s should be discarded from the orthonormal basis set. (QR factorization, discussed in §2.10, also constructs an orthonormal basis, see [5].)
Approximation of Matrices Note that equation (2.6.1) can be rewritten to express any matrix Aij as a sum of outer products of columns of U and rows of VT , with the “weighting factors” being the singular values wj , Aij =
N X k=1
wk Uik Vjk
(2.6.13)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
x =
2.6 Singular Value Decomposition
59
SVD Algorithm Here is the algorithm for constructing the singular value decomposition of any matrix. See §11.2–§11.3, and also [4-5] , for discussion relating to the underlying method.
C
SUBROUTINE svdcmp(a,m,n,mp,np,w,v) INTEGER m,mp,n,np,NMAX REAL a(mp,np),v(np,np),w(np) PARAMETER (NMAX=500) Maximum anticipated value of n. USES pythag Given a matrix a(1:m,1:n), with physical dimensions mp by np, this routine computes its singular value decomposition, A = U · W · V T . The matrix U replaces a on output. The diagonal matrix of singular values W is output as a vector w(1:n). The matrix V (not the transpose V T ) is output as v(1:n,1:n). INTEGER i,its,j,jj,k,l,nm REAL anorm,c,f,g,h,s,scale,x,y,z,rv1(NMAX),pythag g=0.0 Householder reduction to bidiagonal form. scale=0.0 anorm=0.0 do 25 i=1,n l=i+1 rv1(i)=scale*g g=0.0 s=0.0 scale=0.0 if(i.le.m)then do 11 k=i,m scale=scale+abs(a(k,i)) enddo 11 if(scale.ne.0.0)then do 12 k=i,m a(k,i)=a(k,i)/scale s=s+a(k,i)*a(k,i) enddo 12 f=a(i,i) g=-sign(sqrt(s),f) h=f*g-s a(i,i)=f-g do 15 j=l,n s=0.0 do 13 k=i,m s=s+a(k,i)*a(k,j) enddo 13 f=s/h do 14 k=i,m a(k,j)=a(k,j)+f*a(k,i) enddo 14
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
If you ever encounter a situation where most of the singular values wj of a matrix A are very small, then A will be well-approximated by only a few terms in the sum (2.6.13). This means that you have to store only a few columns of U and V (the same k ones) and you will be able to recover, with good accuracy, the whole matrix. Note also that it is very efficient to multiply such an approximated matrix by a vector x: You just dot x with each of the stored columns of V, multiply the resulting scalar by the corresponding wk , and accumulate that multiple of the corresponding column of U. If your matrix is approximated by a small number K of singular values, then this computation of A · x takes only about K(M + N ) multiplications, instead of M N for the full matrix.
60
Chapter 2.
Solution of Linear Algebraic Equations
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
enddo 15 do 16 k=i,m a(k,i)=scale*a(k,i) enddo 16 endif endif w(i)=scale *g g=0.0 s=0.0 scale=0.0 if((i.le.m).and.(i.ne.n))then do 17 k=l,n scale=scale+abs(a(i,k)) enddo 17 if(scale.ne.0.0)then do 18 k=l,n a(i,k)=a(i,k)/scale s=s+a(i,k)*a(i,k) enddo 18 f=a(i,l) g=-sign(sqrt(s),f) h=f*g-s a(i,l)=f-g do 19 k=l,n rv1(k)=a(i,k)/h enddo 19 do 23 j=l,m s=0.0 do 21 k=l,n s=s+a(j,k)*a(i,k) enddo 21 do 22 k=l,n a(j,k)=a(j,k)+s*rv1(k) enddo 22 enddo 23 do 24 k=l,n a(i,k)=scale*a(i,k) enddo 24 endif endif anorm=max(anorm,(abs(w(i))+abs(rv1(i)))) enddo 25 do 32 i=n,1,-1 Accumulation of right-hand transformations. if(i.lt.n)then if(g.ne.0.0)then do 26 j=l,n Double division to avoid possible underflow. v(j,i)=(a(i,j)/a(i,l))/g enddo 26 do 29 j=l,n s=0.0 do 27 k=l,n s=s+a(i,k)*v(k,j) enddo 27 do 28 k=l,n v(k,j)=v(k,j)+s*v(k,i) enddo 28 enddo 29 endif do 31 j=l,n v(i,j)=0.0 v(j,i)=0.0 enddo 31 endif v(i,i)=1.0
2.6 Singular Value Decomposition
2
g=rv1(i) l=i enddo 32 do 39 i=min(m,n),1,-1 Accumulation of left-hand transformations. l=i+1 g=w(i) do 33 j=l,n a(i,j)=0.0 enddo 33 if(g.ne.0.0)then g=1.0/g do 36 j=l,n s=0.0 do 34 k=l,m s=s+a(k,i)*a(k,j) enddo 34 f=(s/a(i,i))*g do 35 k=i,m a(k,j)=a(k,j)+f*a(k,i) enddo 35 enddo 36 do 37 j=i,m a(j,i)=a(j,i)*g enddo 37 else do 38 j= i,m a(j,i)=0.0 enddo 38 endif a(i,i)=a(i,i)+1.0 enddo 39 do 49 k=n,1,-1 Diagonalization of the bidiagonal form: Loop over do 48 its=1,30 singular values, and over allowed iterations. do 41 l=k,1,-1 Test for splitting. nm=l-1 Note that rv1(1) is always zero. if((abs(rv1(l))+anorm).eq.anorm) goto 2 if((abs(w(nm))+anorm).eq.anorm) goto 1 enddo 41 c=0.0 Cancellation of rv1(l), if l > 1. s=1.0 do 43 i=l,k f=s*rv1(i) rv1(i)=c*rv1(i) if((abs(f)+anorm).eq.anorm) goto 2 g=w(i) h=pythag(f,g) w(i)=h h=1.0/h c= (g*h) s=-(f*h) do 42 j=1,m y=a(j,nm) z=a(j,i) a(j,nm)=(y*c)+(z*s) a(j,i)=-(y*s)+(z*c) enddo 42 enddo 43 z=w(k) if(l.eq.k)then Convergence. if(z.lt.0.0)then Singular value is made nonnegative. w(k)=-z do 44 j=1,n v(j,k)=-v(j,k) enddo 44
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
61
62
Solution of Linear Algebraic Equations
endif goto 3 endif if(its.eq.30) pause ’no convergence in svdcmp’ x=w(l) Shift from bottom 2-by-2 minor. nm=k-1 y=w(nm) g=rv1(nm) h=rv1(k) f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y) g=pythag(f,1.0) f=((x-z)*(x+z)+h*((y/(f+sign(g,f)))-h))/x c=1.0 Next QR transformation: s=1.0 do 47 j=l,nm i=j+1 g=rv1(i) y=w(i) h=s*g g=c*g z=pythag(f,h) rv1(j)=z c=f/z s=h/z f= (x*c)+(g*s) g=-(x*s)+(g*c) h=y*s y=y*c do 45 jj=1,n x=v(jj,j) z=v(jj,i) v(jj,j)= (x*c)+(z*s) v(jj,i)=-(x*s)+(z*c) enddo 45 z=pythag(f,h) w(j)=z Rotation can be arbitrary if z = 0. if(z.ne.0.0)then z=1.0/z c=f*z s=h*z endif f= (c*g)+(s*y) x=-(s*g)+(c*y) do 46 jj=1,m y=a(jj,j) z=a(jj,i) a(jj,j)= (y*c)+(z*s) a(jj,i)=-(y*s)+(z*c) enddo 46 enddo 47 rv1(l)=0.0 rv1(k)=f w(k)=x enddo 48 continue enddo 49 return END
FUNCTION pythag(a,b) REAL a,b,pythag Computes (a2 + b2 )1/2 without destructive underflow or overflow.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
3
Chapter 2.
2.7 Sparse Linear Systems
63
(Double precision versions of svdcmp, svbksb, and pythag, named dsvdcmp, dsvbksb, and dpythag, are used by the routine ratlsq in §5.13. You can easily make the conversions, or else get the converted routines from the Numerical Recipes diskette.) CITED REFERENCES AND FURTHER READING: Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press), §8.3 and Chapter 12. Lawson, C.L., and Hanson, R. 1974, Solving Least Squares Problems (Englewood Cliffs, NJ: Prentice-Hall), Chapter 18. Forsythe, G.E., Malcolm, M.A., and Moler, C.B. 1977, Computer Methods for Mathematical Computations (Englewood Cliffs, NJ: Prentice-Hall), Chapter 9. [1] Wilkinson, J.H., and Reinsch, C. 1971, Linear Algebra, vol. II of Handbook for Automatic Computation (New York: Springer-Verlag), Chapter I.10 by G.H. Golub and C. Reinsch. [2] Dongarra, J.J., et al. 1979, LINPACK User’s Guide (Philadelphia: S.I.A.M.), Chapter 11. [3] Smith, B.T., et al. 1976, Matrix Eigensystem Routines — EISPACK Guide, 2nd ed., vol. 6 of Lecture Notes in Computer Science (New York: Springer-Verlag). Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag), §6.7. [4] Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press), §5.2.6. [5]
2.7 Sparse Linear Systems A system of linear equations is called sparse if only a relatively small number of its matrix elements aij are nonzero. It is wasteful to use general methods of linear algebra on such problems, because most of the O(N 3 ) arithmetic operations devoted to solving the set of equations or inverting the matrix involve zero operands. Furthermore, you might wish to work problems so large as to tax your available memory space, and it is wasteful to reserve storage for unfruitful zero elements. Note that there are two distinct (and not always compatible) goals for any sparse matrix method: saving time and/or saving space. We have already considered one archetypal sparse form in §2.4, the band diagonal matrix. In the tridiagonal case, e.g., we saw that it was possible to save
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
REAL absa,absb absa=abs(a) absb=abs(b) if(absa.gt.absb)then pythag=absa*sqrt(1.+(absb/absa)**2) else if(absb.eq.0.)then pythag=0. else pythag=absb*sqrt(1.+(absa/absb)**2) endif endif return END
64
Chapter 2.
Solution of Linear Algebraic Equations
If your particular pattern of sparsity is not a simple one, then you may wish to try an analyze/factorize/operate package, which automates the procedure of figuring out how fill-ins are to be minimized. The analyze stage is done once only for each pattern of sparsity. The factorize stage is done once for each particular matrix that fits the pattern. The operate stage is performed once for each right-hand side to be used with the particular matrix. Consult [2,3] for references on this. The NAG library [4] has an analyze/factorize/operate capability. A substantial collection of routines for sparse matrix calculation is also available from IMSL [5] as the Yale Sparse Matrix Package [6]. You should be aware that the special order of interchanges and eliminations, prescribed by a sparse matrix method so as to minimize fill-ins and arithmetic operations, generally acts to decrease the method’s numerical stability as compared to, e.g., regular LU decomposition with pivoting. Scaling your problem so as to make its nonzero matrix elements have comparable magnitudes (if you can do it) will sometimes ameliorate this problem. In the remainder of this section, we present some concepts which are applicable to some general classes of sparse matrices, and which do not necessarily depend on details of the pattern of sparsity.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
both time (order N instead of N 3 ) and space (order N instead of N 2 ). The method of solution was not different in principle from the general method of LU decomposition; it was just applied cleverly, and with due attention to the bookkeeping of zero elements. Many practical schemes for dealing with sparse problems have this same character. They are fundamentally decomposition schemes, or else elimination schemes akin to Gauss-Jordan, but carefully optimized so as to minimize the number of so-called fill-ins, initially zero elements which must become nonzero during the solution process, and for which storage must be reserved. Direct methods for solving sparse equations, then, depend crucially on the precise pattern of sparsity of the matrix. Patterns that occur frequently, or that are useful as way-stations in the reduction of more general forms, already have special names and special methods of solution. We do not have space here for any detailed review of these. References listed at the end of this section will furnish you with an “in” to the specialized literature, and the following list of buzz words (and Figure 2.7.1) will at least let you hold your own at cocktail parties: • tridiagonal • band diagonal (or banded) with bandwidth M • band triangular • block diagonal • block tridiagonal • block triangular • cyclic banded • singly (or doubly) bordered block diagonal • singly (or doubly) bordered block triangular • singly (or doubly) bordered band diagonal • singly (or doubly) bordered band triangular • other (!) You should also be aware of some of the special sparse forms that occur in the solution of partial differential equations in two or more dimensions. See Chapter 19.
65
2.7 Sparse Linear Systems
zeros
zeros
(a)
( b)
(c)
(d)
(e)
(f )
(g)
(h)
(i)
( j)
(k)
Figure 2.7.1. Some standard forms for sparse matrices. (a) Band diagonal; (b) block triangular; (c) block tridiagonal; (d) singly bordered block diagonal; (e) doubly bordered block diagonal; (f) singly bordered block triangular; (g) bordered band-triangular; (h) and (i) singly and doubly bordered band diagonal; (j) and (k) other! (after Tewarson) [1].
Sherman-Morrison Formula Suppose that you have already obtained, by herculean effort, the inverse matrix A−1 of a square matrix A. Now you want to make a “small” change in A, for example change one element aij , or a few elements, or one row, or one column. Is there any way of calculating the corresponding change in A−1 without repeating
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
zeros
66
Chapter 2.
Solution of Linear Algebraic Equations
your difficult labors? Yes, if your change is of the form A → (A + u ⊗ v)
(2.7.1)
(A + u ⊗ v)−1 = (1 + A−1 · u ⊗ v)−1 · A−1 = (1 − A−1 · u ⊗ v + A−1 · u ⊗ v · A−1 · u ⊗ v − . . .) · A−1 = A−1 − A−1 · u ⊗ v · A−1 (1 − λ + λ2 − . . .) = A−1 −
(A−1 · u) ⊗ (v · A−1 ) 1+λ (2.7.2)
where λ ≡ v · A−1 · u
(2.7.3)
The second line of (2.7.2) is a formal power series expansion. In the third line, the associativity of outer and inner products is used to factor out the scalars λ. The use of (2.7.2) is this: Given A−1 and the vectors u and v, we need only perform two matrix multiplications and a vector dot product, z ≡ A−1 · u
w ≡ (A−1 )T · v
λ=v·z
(2.7.4)
to get the desired change in the inverse A−1
→
A−1 −
z⊗w 1+λ
(2.7.5)
The whole procedure requires only 3N 2 multiplies and a like number of adds (an even smaller number if u or v is a unit vector). The Sherman-Morrison formula can be directly applied to a class of sparse problems. If you already have a fast way of calculating the inverse of A (e.g., a tridiagonal matrix, or some other standard sparse form), then (2.7.4)–(2.7.5) allow you to build up to your related but more complicated form, adding for example a row or column at a time. Notice that you can apply the Sherman-Morrison formula more than once successively, using at each stage the most recent update of A−1 (equation 2.7.5). Of course, if you have to modify every row, then you are back to an N 3 method. The constant in front of the N 3 is only a few times worse than the better direct methods, but you have deprived yourself of the stabilizing advantages of pivoting — so be careful. For some other sparse problems, the Sherman-Morrison formula cannot be directly applied for the simple reason that storage of the whole inverse matrix A−1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
for some vectors u and v. If u is a unit vector ei , then (2.7.1) adds the components of v to the ith row. (Recall that u ⊗ v is a matrix whose i, jth element is the product of the ith component of u and the jth component of v.) If v is a unit vector ej , then (2.7.1) adds the components of u to the jth column. If both u and v are proportional to unit vectors ei and ej respectively, then a term is added only to the element aij . The Sherman-Morrison formula gives the inverse (A + u ⊗ v)−1 , and is derived briefly as follows:
2.7 Sparse Linear Systems
67
is not feasible. If you want to add only a single correction of the form u ⊗ v, and solve the linear system (A + u ⊗ v) · x = b
(2.7.6)
A·y=b
A·z= u
for the vectors y and z. In terms of these, v·y z x=y− 1 + (v · z)
(2.7.7)
(2.7.8)
as we see by multiplying (2.7.2) on the right by b.
Cyclic Tridiagonal Systems So-called cyclic tridiagonal systems occur quite frequently, and are a good example of how to use the Sherman-Morrison formula in the manner just described. The equations have the form b 1 c1 0 · · · x1 r1 β a 2 b 2 c2 · · · x2 r2 ··· · · · · = · · · (2.7.9) · · · aN−1 bN−1 cN−1 xN−1 rN−1 α ··· 0 aN bN xN rN This is a tridiagonal system, except for the matrix elements α and β in the corners. Forms like this are typically generated by finite-differencing differential equations with periodic boundary conditions (§19.4). We use the Sherman-Morrison formula, treating the system as tridiagonal plus a correction. In the notation of equation (2.7.6), define vectors u and v to be γ 1 0 0 . . . . u= . v= (2.7.10) . 0 0 α β/γ Here γ is arbitrary for the moment. Then the matrix A is the tridiagonal part of the matrix in (2.7.9), with two terms modified: b01 = b1 − γ,
b0N = bN − αβ/γ
(2.7.11)
We now solve equations (2.7.7) with the standard tridiagonal algorithm, and then get the solution from equation (2.7.8). The routine cyclic below implements this algorithm. We choose the arbitrary parameter γ = −b1 to avoid loss of precision by subtraction in the first of equations (2.7.11). In the unlikely event that this causes loss of precision in the second of these equations, you can make a different choice.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
then you proceed as follows. Using the fast method that is presumed available for the matrix A, solve the two auxiliary problems
68
C
Chapter 2.
Solution of Linear Algebraic Equations
Woodbury Formula If you want to add more than a single correction term, then you cannot use (2.7.8) repeatedly, since without storing a new A−1 you will not be able to solve the auxiliary problems (2.7.7) efficiently after the first step. Instead, you need the Woodbury formula, which is the block-matrix version of the Sherman-Morrison formula, (A + U · VT )−1
i h = A−1 − A−1 · U · (1 + VT · A−1 · U)−1 · VT · A−1
(2.7.12)
Here A is, as usual, an N × N matrix, while U and V are N × P matrices with P < N and usually P N . The inner piece of the correction term may become clearer if written as the tableau,
U
−1 · 1 + VT · A−1 · U ·
VT
(2.7.13)
where you can see that the matrix whose inverse is needed is only P × P rather than N × N .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE cyclic(a,b,c,alpha,beta,r,x,n) INTEGER n,NMAX REAL alpha,beta,a(n),b(n),c(n),r(n),x(n) PARAMETER (NMAX=500) USES tridag Solves for a vector x(1:n) the “cyclic” set of linear equations given by equation (2.7.9). a, b, c, and r are input vectors, while alpha and beta are the corner entries in the matrix. The input is not modified. INTEGER i REAL fact,gamma,bb(NMAX),u(NMAX),z(NMAX) if(n.le.2)pause ’n too small in cyclic’ if(n.gt.NMAX)pause ’NMAX too small in cyclic’ gamma=-b(1) Avoid subtraction error in forming bb(1). bb(1)=b(1)-gamma Set up the diagonal of the modified tridiagonal system. bb(n)=b(n)-alpha*beta/gamma do 11 i=2,n-1 bb(i)=b(i) enddo 11 call tridag(a,bb,c,r,x,n) Solve A · x = r. u(1)=gamma Set up the vector u. u(n)=alpha do 12 i=2,n-1 u(i)=0. enddo 12 call tridag(a,bb,c,u,z,n) Solve A · z = u. fact=(x(1)+beta*x(n)/gamma)/(1.+z(1)+beta*z(n)/gamma) Form v · x/(1 + v · z). do 13 i=1,n Now get the solution vector x. x(i)=x(i)-fact*z(i) enddo 13 return END
2.7 Sparse Linear Systems
69
The relation between the Woodbury formula and successive applications of the ShermanMorrison formula is now clarified by noting that, if U is the matrix formed by columns out of the P vectors u1 , . . . , uP , and V is the matrix formed by columns out of the P vectors v1 , . . . , vP , V ≡ v1 · · · vP
then two ways of expressing the same correction to A are ! P X uk ⊗ vk = (A + U · VT ) A+
(2.7.14)
(2.7.15)
k=1
(Note that the subscripts on u and v do not denote components, but rather distinguish the different column vectors.) Equation (2.7.15) reveals that, if you have A−1 in storage, then you can either make the P corrections in one fell swoop by using (2.7.12), inverting a P × P matrix, or else make them by applying (2.7.5) P successive times. If you don’t have storage for A−1 , then you must use (2.7.12) in the following way: To solve the linear equation ! P X A+ (2.7.16) uk ⊗ vk · x = b k=1
first solve the P auxiliary problems A · z 1 = u1 A · z 2 = u2 ···
(2.7.17)
A · z P = uP and construct the matrix Z by columns from the z’s obtained, Z ≡ z1 · · · zP
(2.7.18)
Next, do the P × P matrix inversion H ≡ (1 + VT · Z)−1
(2.7.19)
Finally, solve the one further auxiliary problem A·y =b In terms of these quantities, the solution is given by h i x = y − Z · H · (VT · y)
(2.7.20)
(2.7.21)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
U ≡ u 1 · · · u P
70
Chapter 2.
Solution of Linear Algebraic Equations
Inversion by Partitioning
where P and S are square matrices of size p × p and s × s respectively (p + s = N ). The matrices Q and R are not necessarily square, and have sizes p × s and s × p, respectively. If the inverse of A is partitioned in the same manner, # " e e Q P −1 A = (2.7.23) e e R S e R, e Q, e e then P, S, which have the same sizes as P, Q, R, S, respectively, can be found by either the formulas e = (P − Q · S−1 · R)−1 P e = −(P − Q · S−1 · R)−1 · (Q · S−1 ) Q e = −(S−1 · R) · (P − Q · S−1 · R)−1 R
(2.7.24)
e S = S−1 + (S−1 · R) · (P − Q · S−1 · R)−1 · (Q · S−1 ) or else by the equivalent formulas e = P−1 + (P−1 · Q) · (S − R · P−1 · Q)−1 · (R · P−1 ) P e = −(P−1 · Q) · (S − R · P−1 · Q)−1 Q e = −(S − R · P−1 · Q)−1 · (R · P−1 ) R
(2.7.25)
e S = (S − R · P−1 · Q)−1 The parentheses in equations (2.7.24) and (2.7.25) highlight repeated factors that you may wish to compute only once. (Of course, by associativity, you can instead do the matrix multiplications in any order you like.) The choice between using e or e equation (2.7.24) and (2.7.25) depends on whether you want P S to have the simpler formula; or on whether the repeated expression (S − R · P−1 · Q)−1 is easier to calculate than the expression (P − Q · S−1 · R)−1 ; or on the relative sizes of P and S; or on whether P−1 or S−1 is already known. Another sometimes useful formula is for the determinant of the partitioned matrix, det A = det P det(S − R · P−1 · Q) = det S det(P − Q · S−1 · R)
(2.7.26)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Once in a while, you will encounter a matrix (not even necessarily sparse) that can be inverted efficiently by partitioning. Suppose that the N × N matrix A is partitioned into P Q A= (2.7.22) R S
71
2.7 Sparse Linear Systems
Indexed Storage of Sparse Matrices
In row-indexed compact storage, matrix (2.7.27) is represented by the two arrays of length 11, as follows index k
1
2
3
4
5
6
7
8
9
10
11
ija(k)
7
8
8
10
11
12
3
2
4
5
4
sa(k)
3.
4.
5.
0.
5.
x
1.
7.
9.
2.
6.
(2.7.28)
Here x is an arbitrary value. Notice that, according to the storage rules, the value of N (namely 5) is ija(1)-2, and the length of each array is ija(ija(1)-1)-1, namely 11.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
We have already seen (§2.4) that tri- or band-diagonal matrices can be stored in a compact format that allocates storage only to elements which can be nonzero, plus perhaps a few wasted locations to make the bookkeeping easier. What about more general sparse matrices? When a sparse matrix of logical size N × N contains only a few times N nonzero elements (a typical case), it is surely inefficient — and often physically impossible — to allocate storage for all N 2 elements. Even if one did allocate such storage, it would be inefficient or prohibitive in machine time to loop over all of it in search of nonzero elements. Obviously some kind of indexed storage scheme is required, one that stores only nonzero matrix elements, along with sufficient auxiliary information to determine where an element logically belongs and how the various elements can be looped over in common matrix operations. Unfortunately, there is no one standard scheme in general use. Knuth [7] describes one method. The Yale Sparse Matrix Package [6] and ITPACK [8] describe several other methods. For most applications, we favor the storage scheme used by PCGPACK [9], which is almost the same as that described by Bentley [10], and also similar to one of the Yale Sparse Matrix Package methods. The advantage of this scheme, which can be called row-indexed sparse storage mode, is that it requires storage of only about two times the number of nonzero matrix elements. (Other methods can require as much as three or five times.) For simplicity, we will treat only the case of square matrices, which occurs most frequently in practice. To represent a matrix A of logical size N × N , the row-indexed scheme sets up two one-dimensional arrays, call them sa and ija. The first of these stores matrix element values in single or double precision as desired; the second stores integer values. The storage rules are: • The first N locations of sa store A’s diagonal matrix elements, in order. (Note that diagonal elements are stored even if they are zero; this is at most a slight storage inefficiency, since diagonal elements are nonzero in most realistic applications.) • Each of the first N locations of ija stores the index of the array sa that contains the first off-diagonal element of the corresponding row of the matrix. (If there are no off-diagonal elements for that row, it is one greater than the index in sa of the most recently stored element of a previous row.) • Location 1 of ija is always equal to N + 2. (It can be read to determine N .) • Location N + 1 of ija is one greater than the index in sa of the last off-diagonal element of the last row. (It can be read to determine the number of nonzero elements in the matrix, or the logical length of the arrays sa and ija.) Location N + 1 of sa is not used and can be set arbitrarily. • Entries in sa at locations ≥ N + 2 contain A’s off-diagonal values, ordered by rows and, within each row, ordered by columns. • Entries in ija at locations ≥ N +2 contain the column number of the corresponding element in sa. While these rules seem arbitrary at first sight, they result in a rather elegant storage scheme. As an example, consider the matrix 3. 0. 1. 0. 0. 0. 4. 0. 0. 0. (2.7.27) 0. 7. 5. 9. 0. 0. 0. 0. 0. 2. 0. 0. 0. 6. 5.
72
Chapter 2.
Solution of Linear Algebraic Equations
SUBROUTINE sprsin(a,n,np,thresh,nmax,sa,ija) INTEGER n,nmax,np,ija(nmax) REAL thresh,a(np,np),sa(nmax) Converts a square matrix a(1:n,1:n) with physical dimension np into row-indexed sparse storage mode. Only elements of a with magnitude ≥thresh are retained. Output is in two linear arrays with physical dimension nmax (an input parameter): sa(1:) contains array values, indexed by ija(1:). The logical sizes of sa and ija on output are both ija(ija(1)-1)-1 (see text). INTEGER i,j,k do 11 j=1,n Store diagonal elements. sa(j)=a(j,j) enddo 11 ija(1)=n+2 Index to 1st row off-diagonal element, if any. k=n+1 do 13 i=1,n Loop over rows. do 12 j=1,n Loop over columns. if(abs(a(i,j)).ge.thresh)then if(i.ne.j)then Store off-diagonal elements and their columns. k=k+1 if(k.gt.nmax)pause ’nmax too small in sprsin’ sa(k)=a(i,j) ija(k)=j endif endif enddo 12 ija(i+1)=k+1 As each row is completed, store index to next. enddo 13 return END
The single most important use of a matrix in row-indexed sparse storage mode is to multiply a vector to its right. In fact, the storage mode is optimized for just this purpose. The following routine is thus very simple. SUBROUTINE sprsax(sa,ija,x,b,n) INTEGER n,ija(*) REAL b(n),sa(*),x(n) Multiply a matrix in row-index sparse storage arrays sa and ija by a vector x(1:n), giving a vector b(1:n). INTEGER i,k if (ija(1).ne.n+2) pause ’mismatched vector and matrix in sprsax’ do 12 i=1,n b(i)=sa(i)*x(i) Start with diagonal term. do 11 k=ija(i),ija(i+1)-1 Loop over off-diagonal terms. b(i)=b(i)+sa(k)*x(ija(k)) enddo 11 enddo 12 return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The diagonal element in row i is sa(i), and the off-diagonal elements in that row are in sa(k) where k loops from ija(i) to ija(i+1)-1, if the upper limit is greater or equal to the lower one (as in FORTRAN do loops). Here is a routine, sprsin, that converts a matrix from full storage mode into row-indexed sparse storage mode, throwing away any elements that are less than a specified threshold. Of course, the principal use of sparse storage mode is for matrices whose full storage mode won’t fit into your machine at all; then you have to generate them directly into sparse format. Nevertheless sprsin is useful as a precise algorithmic definition of the storage scheme, for subscale testing of large problems, and for the case where execution time, rather than storage, furnishes the impetus to sparse storage.
2.7 Sparse Linear Systems
73
It is also simple to multiply the transpose of a matrix by a vector to its right. (We will use this operation later in this section.) Note that the transpose matrix is not actually constructed.
(Double precision versions of sprsax and sprstx, named dsprsax and dsprstx, are used by the routine atimes later in this section. You can easily make the conversion, or else get the converted routines from the Numerical Recipes diskettes.) In fact, because the choice of row-indexed storage treats rows and columns quite differently, it is quite an involved operation to construct the transpose of a matrix, given the matrix itself in row-indexed sparse storage mode. When the operation cannot be avoided, it is done as follows: An index of all off-diagonal elements by their columns is constructed (see §8.4). The elements are then written to the output array in column order. As each element is written, its row is determined and stored. Finally, the elements in each column are sorted by row.
C
5
SUBROUTINE sprstp(sa,ija,sb,ijb) INTEGER ija(*),ijb(*) REAL sa(*),sb(*) USES iindexx Version of indexx with all REAL variables changed to INTEGER. Construct the transpose of a sparse square matrix, from row-index sparse storage arrays sa and ija into arrays sb and ijb. INTEGER j,jl,jm,jp,ju,k,m,n2,noff,inc,iv REAL v n2=ija(1) Linear size of matrix plus 2. do 11 j=1,n2-2 Diagonal elements. sb(j)=sa(j) enddo 11 call iindexx(ija(n2-1)-ija(1),ija(n2),ijb(n2)) Index all off-diagonal elements by their columns. jp=0 do 13 k=ija(1),ija(n2-1)-1 Loop over output off-diagonal elements. m=ijb(k)+n2-1 Use index table to store by (former) columns. sb(k)=sa(m) do 12 j=jp+1,ija(m) Fill in the index to any omitted rows. ijb(j)=k enddo 12 jp=ija(m) Use bisection to find which row element m is in and put that jl=1 into ijb(k). ju=n2-1 if (ju-jl.gt.1) then jm=(ju+jl)/2 if(ija(jm).gt.m)then ju=jm else
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE sprstx(sa,ija,x,b,n) INTEGER n,ija(*) REAL b(n),sa(*),x(n) Multiply the transpose of a matrix in row-index sparse storage arrays sa and ija by a vector x(1:n), giving a vector b(1:n). INTEGER i,j,k if (ija(1).ne.n+2) pause ’mismatched vector and matrix in sprstx’ do 11 i=1,n Start with diagonal terms. b(i)=sa(i)*x(i) enddo 11 do 13 i=1,n Loop over off-diagonal terms. do 12 k=ija(i),ija(i+1)-1 j=ija(k) b(j)=b(j)+sa(k)*x(i) enddo 12 enddo 13 return END
74
2
3
4
Solution of Linear Algebraic Equations
jl=jm endif goto 5 endif ijb(k)=jl enddo 13 do 14 j=jp+1,n2-1 ijb(j)=ija(n2-1) enddo 14 Make a final pass to sort each row by Shell sort algorithm. do 16 j=1,n2-2 jl=ijb(j+1)-ijb(j) noff=ijb(j)-1 inc=1 inc=3*inc+1 if(inc.le.jl)goto 1 continue inc=inc/3 do 15 k=noff+inc+1,noff+jl iv=ijb(k) v=sb(k) m=k if(ijb(m-inc).gt.iv)then ijb(m)=ijb(m-inc) sb(m)=sb(m-inc) m=m-inc if(m-noff.le.inc)goto 4 goto 3 endif ijb(m)=iv sb(m)=v enddo 15 if(inc.gt.1)goto 2 enddo 16 return END
The above routine embeds internally a sorting algorithm from §8.1, but calls the external routine iindexx to construct the initial column index. This routine is identical to indexx, as listed in §8.4, except that the latter’s two REAL declarations should be changed to integer. (The Numerical Recipes diskettes include both indexx and iindexx.) In fact, you can often use indexx without making these changes, since many computers have the property that numerical values will sort correctly independently of whether they are interpreted as floating or integer values. As final examples of the manipulation of sparse matrices, we give two routines for the multiplication of two sparse matrices. These are useful for techniques to be described in §13.10. In general, the product of two sparse matrices is not itself sparse. One therefore wants to limit the size of the product matrix in one of two ways: either compute only those elements of the product that are specified in advance by a known pattern of sparsity, or else compute all nonzero elements, but store only those whose magnitude exceeds some threshold value. The former technique, when it can be used, is quite efficient. The pattern of sparsity is specified by furnishing an index array in row-index sparse storage format (e.g., ija). The program then constructs a corresponding value array (e.g., sa). The latter technique runs the danger of excessive compute times and unknown output sizes, so it must be used cautiously. With row-index storage, it is much more natural to multiply a matrix (on the left) by the transpose of a matrix (on the right), so that one is crunching rows on rows, rather than rows on columns. Our routines therefore calculate A · BT , rather than A · B. This means that you have to run your right-hand matrix through the transpose routine sprstp before sending it to the matrix multiply routine. The two implementing routines,sprspm for “pattern multiply” and sprstmfor “threshold multiply” are quite similar in structure. Both are complicated by the logic of the various
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 2.
2.7 Sparse Linear Systems
75
combinations of diagonal or off-diagonal elements for the two input streams and output stream.
1
2
3
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
SUBROUTINE sprspm(sa,ija,sb,ijb,sc,ijc) INTEGER ija(*),ijb(*),ijc(*) REAL sa(*),sb(*),sc(*) Matrix multiply A · BT where A and B are two sparse matrices in row-index storage mode, and BT is the transpose of B. Here, sa and ija store the matrix A; sb and ijb store the matrix B. This routine computes only those components of the matrix product that are prespecified by the input index array ijc, which is not modified. On output, the arrays sc and ijc give the product matrix in row-index storage mode. For sparse matrix multiplication, this routine will often be preceded by a call to sprstp, so as to construct the transpose of a known matrix into sb, ijb. INTEGER i,ijma,ijmb,j,m,ma,mb,mbb,mn REAL sum if (ija(1).ne.ijb(1).or.ija(1).ne.ijc(1)) pause ’sprspm sizes do not match’ do 13 i=1,ijc(1)-2 Loop over rows. j=i Set up so that first pass through loop does the diagm=i onal component. mn=ijc(i) sum=sa(i)*sb(i) continue Main loop over each component to be output. mb=ijb(j) do 11 ma=ija(i),ija(i+1)-1 Loop through elements in A’s row. Convoluted logic, ijma=ija(ma) following, accounts for the various combinations if(ijma.eq.j)then of diagonal and off-diagonal elements. sum=sum+sa(ma)*sb(j) else if(mb.lt.ijb(j+1))then ijmb=ijb(mb) if(ijmb.eq.i)then sum=sum+sa(i)*sb(mb) mb=mb+1 goto 2 else if(ijmb.lt.ijma)then mb=mb+1 goto 2 else if(ijmb.eq.ijma)then sum=sum+sa(ma)*sb(mb) mb=mb+1 goto 2 endif endif endif enddo 11 do 12 mbb=mb,ijb(j+1)-1 Exhaust the remainder of B’s row. if(ijb(mbb).eq.i)then sum=sum+sa(i)*sb(mbb) endif enddo 12 sc(m)=sum sum=0.e0 Reset indices for next pass through loop. if(mn.ge.ijc(i+1))goto 3 m=mn mn=mn+1 j=ijc(m) goto 1 continue enddo 13 return END
76
Solution of Linear Algebraic Equations
SUBROUTINE sprstm(sa,ija,sb,ijb,thresh,nmax,sc,ijc) INTEGER nmax,ija(*),ijb(*),ijc(nmax) REAL thresh,sa(*),sb(*),sc(nmax) Matrix multiply A · BT where A and B are two sparse matrices in row-index storage mode, and BT is the transpose of B. Here, sa and ija store the matrix A; sb and ijb store the matrix B. This routine computes all components of the matrix product (which may be nonsparse!), but stores only those whose magnitude exceeds thresh. On output, the arrays sc and ijc (whose maximum size is input as nmax) give the product matrix in row-index storage mode. For sparse matrix multiplication, this routine will often be preceded by a call to sprstp, so as to construct the transpose of a known matrix into sb, ijb. INTEGER i,ijma,ijmb,j,k,ma,mb,mbb REAL sum if (ija(1).ne.ijb(1)) pause ’sprstm sizes do not match’ k=ija(1) ijc(1)=k do 14 i=1,ija(1)-2 Loop over rows of A, do 13 j=1,ijb(1)-2 and rows of B. if(i.eq.j)then sum=sa(i)*sb(j) else sum=0.e0 endif mb=ijb(j) do 11 ma=ija(i),ija(i+1)-1 Loop through elements in A’s row. Convoluted logic, ijma=ija(ma) following, accounts for the various combinations if(ijma.eq.j)then of diagonal and off-diagonal elements. sum=sum+sa(ma)*sb(j) else if(mb.lt.ijb(j+1))then ijmb=ijb(mb) if(ijmb.eq.i)then sum=sum+sa(i)*sb(mb) mb=mb+1 goto 2 else if(ijmb.lt.ijma)then mb=mb+1 goto 2 else if(ijmb.eq.ijma)then sum=sum+sa(ma)*sb(mb) mb=mb+1 goto 2 endif endif endif enddo 11 do 12 mbb=mb,ijb(j+1)-1 Exhaust the remainder of B’s row. if(ijb(mbb).eq.i)then sum=sum+sa(i)*sb(mbb) endif enddo 12 if(i.eq.j)then Where to put the answer... sc(i)=sum else if(abs(sum).gt.thresh)then if(k.gt.nmax)pause ’sprstm: nmax to small’ sc(k)=sum ijc(k)=j k=k+1 endif enddo 13 ijc(i+1)=k enddo 14 return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
Chapter 2.
77
2.7 Sparse Linear Systems
Conjugate Gradient Method for a Sparse System So-called conjugate gradient methods provide a quite general means for solving the N × N linear system A·x =b
(2.7.29)
1 x·A·x−b·x 2 This function is minimized when its gradient f (x) =
(2.7.30)
∇f = A · x − b
(2.7.31)
is zero, which is equivalent to (2.7.29). The minimization is carried out by generating a succession of search directions pk and improved minimizers xk . At each stage a quantity αk is found that minimizes f (xk + αk pk ), and xk+1 is set equal to the new point xk + αk pk . The pk and xk are built up in such a way that xk+1 is also the minimizer of f over the whole vector space of directions already taken, {p1 , p2 , . . . , pk }. After N iterations you arrive at the minimizer over the entire vector space, i.e., the solution to (2.7.29). Later, in §10.6, we will generalize this “ordinary” conjugate gradient algorithm to the minimization of arbitrary nonlinear functions. Here, where our interest is in solving linear, but not necessarily positive definite or symmetric, equations, a different generalization is important, the biconjugate gradient method. This method does not, in general, have a simple connection with function minimization. It constructs four sequences of vectors, rk , rk , pk , pk , k = 1, 2, . . . . You supply the initial vectors r1 and r1 , and set p1 = r1 , p1 = r1 . Then you carry out the following recurrence: rk · rk pk · A · pk = rk − αk A · pk
αk = rk+1
rk+1 = rk − αk AT · pk
(2.7.32)
rk+1 · rk+1 rk · rk = rk+1 + βk pk
βk = pk+1
pk+1 = rk+1 + βk pk This sequence of vectors satisfies the biorthogonality condition ri · rj = ri · rj = 0,
j a)
(4.4.3)
(b > a)
(4.4.4)
0
If the singularity is at the upper limit, use the identity Z
b
f(x)dx = a
1 1−γ
Z
(b−a)1−γ
γ
1
t 1−γ f(b − t 1−γ )dt 0
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE midinf(funk,aa,bb,s,n) INTEGER n REAL aa,bb,s,funk EXTERNAL funk This routine is an exact replacement for midpnt, i.e., returns as s the nth stage of refinement of the integral of funk from aa to bb, except that the function is evaluated at evenly spaced points in 1/x rather than in x. This allows the upper limit bb to be as large and positive as the computer allows, or the lower limit aa to be as large and negative, but not both. aa and bb must have the same sign. INTEGER it,j REAL a,b,ddel,del,sum,tnm,func,x func(x)=funk(1./x)/x**2 This statement function effects the change of variable. b=1./aa These two statements change the limits of integration aca=1./bb cordingly. if (n.eq.1) then From this point on, the routine is exactly identical to midpnt. s=(b-a)*func(0.5*(a+b)) else it=3**(n-2) tnm=it del=(b-a)/(3.*tnm) ddel=del+del x=a+0.5*del sum=0. do 11 j=1,it sum=sum+func(x) x=x+ddel sum=sum+func(x) x=x+del enddo 11 s=(s+(b-a)*sum/tnm)/3. endif return END
139
4.4 Improper Integrals
If there is a singularity at both limits, divide the integral at an interior breakpoint as in the example above. Equations (4.4.3) and (4.4.4) are particularly simple in the case of inverse square-root singularities, a case that occurs frequently in practice: Z
√ b−a
Z
b
2tf(a + t2 )dt
(b > a)
(4.4.5)
2tf(b − t2 )dt
(b > a)
(4.4.6)
0
for a singularity at a, and Z
√ b−a
Z
b
f(x)dx = a
0
for a singularity at b. Once again, we can implement these changes of variable transparently to the user by defining substitute routines for midpnt which make the change of variable automatically: SUBROUTINE midsql(funk,aa,bb,s,n) INTEGER n REAL aa,bb,s,funk EXTERNAL funk This routine is an exact replacement for midpnt, except that it allows for an inverse squareroot singularity in the integrand at the lower limit aa. INTEGER it,j REAL ddel,del,sum,tnm,x,func,a,b func(x)=2.*x*funk(aa+x**2) b=sqrt(bb-aa) a=0. if (n.eq.1) then The rest of the routine is exactly like midpnt and is omitted.
Similarly, SUBROUTINE midsqu(funk,aa,bb,s,n) INTEGER n REAL aa,bb,s,funk EXTERNAL funk This routine is an exact replacement for midpnt, except that it allows for an inverse squareroot singularity in the integrand at the upper limit bb. INTEGER it,j REAL ddel,del,sum,tnm,x,func,a,b func(x)=2.*x*funk(bb-x**2) b=sqrt(bb-aa) a=0. if (n.eq.1) then The rest of the routine is exactly like midpnt and is omitted.
One last example should suffice to show how these formulas are derived in general. Suppose the upper limit of integration is infinite, and the integrand falls off exponentially. Then we want a change of variable that maps e−x dx into (±)dt (with the sign chosen to keep the upper limit of the new variable larger than the lower limit). Doing the integration gives by inspection t = e−x
or
x = − log t
(4.4.7)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
f(x)dx = a
140
Chapter 4.
Integration of Functions
so that Z
Z
x=∞
t=e−a
f(x)dx = x=a
f(− log t) t=0
dt t
(4.4.8)
SUBROUTINE midexp(funk,aa,bb,s,n) INTEGER n REAL aa,bb,s,funk EXTERNAL funk This routine is an exact replacement for midpnt, except that bb is assumed to be infinite (value passed not actually used). It is assumed that the function funk decreases exponentially rapidly at infinity. INTEGER it,j REAL ddel,del,sum,tnm,x,func,a,b func(x)=funk(-log(x))/x b=exp(-aa) a=0. if (n.eq.1) then The rest of the routine is exactly like midpnt and is omitted.
CITED REFERENCES AND FURTHER READING: Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathematical Association of America), Chapter 4. Dahlquist, G., and Bjorck, A. 1974, Numerical Methods (Englewood Cliffs, NJ: Prentice-Hall), §7.4.3, p. 294. Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag), §3.7, p. 152.
4.5 Gaussian Quadratures and Orthogonal Polynomials In the formulas of §4.1, the integral of a function was approximated by the sum of its functional values at a set of equally spaced points, multiplied by certain aptly chosen weighting coefficients. We saw that as we allowed ourselves more freedom in choosing the coefficients, we could achieve integration formulas of higher and higher order. The idea of Gaussian quadratures is to give ourselves the freedom to choose not only the weighting coefficients, but also the location of the abscissas at which the function is to be evaluated: They will no longer be equally spaced. Thus, we will have twice the number of degrees of freedom at our disposal; it will turn out that we can achieve Gaussian quadrature formulas whose order is, essentially, twice that of the Newton-Cotes formula with the same number of function evaluations. Does this sound too good to be true? Well, in a sense it is. The catch is a familiar one, which cannot be overemphasized: High order is not the same as high accuracy. High order translates to high accuracy only when the integrand is very smooth, in the sense of being “well-approximated by a polynomial.”
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The user-transparent implementation would be
4.5 Gaussian Quadratures and Orthogonal Polynomials
141
Z
b
W (x)f(x)dx ≈ a
N X
wj f(xj )
(4.5.1)
j=1
is exact if f(x) is a polynomial. For example, to do the integral Z
1
−1
exp(− cos2 x) √ dx 1 − x2
(4.5.2)
(not a very natural looking integral, it must be admitted), we might well be interested in a Gaussian quadrature formula based on the choice W (x) = √
1 1 − x2
(4.5.3)
in the interval (−1, 1). (This particular choice is called Gauss-Chebyshev integration, for reasons that will become clear shortly.) Notice that the integration formula (4.5.1) can also be written with the weight function W (x) not overtly visible: Define g(x) ≡ W (x)f(x) and vj ≡ wj /W (xj ). Then (4.5.1) becomes Z
b
g(x)dx ≈ a
N X
vj g(xj )
(4.5.4)
j=1
Where did the function W (x) go? It is lurking there, ready to give high-order accuracy to integrands of the form polynomials times W (x), and ready to deny highorder accuracy to integrands that are otherwise perfectly smooth and well-behaved. When you find tabulations of the weights and abscissas for a given W (x), you have to determine carefully whether they are to be used with a formula in the form of (4.5.1), or like (4.5.4). Here is an example of a quadrature routine that contains the tabulated abscissas and weights for the case W (x) = 1 and N = 10. Since the weights and abscissas are, in this case, symmetric around the midpoint of the range of integration, there are actually only five distinct values of each: SUBROUTINE qgaus(func,a,b,ss) REAL a,b,ss,func EXTERNAL func Returns as ss the integral of the function func between a and b, by ten-point GaussLegendre integration: the function is evaluated exactly ten times at interior points in the range of integration. INTEGER j
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
There is, however, one additional feature of Gaussian quadrature formulas that adds to their usefulness: We can arrange the choice of weights and abscissas to make the integral exact for a class of integrands “polynomials times some known function W (x)” rather than for the usual class of integrands “polynomials.” The function W (x) can then be chosen to remove integrable singularities from the desired integral. Given W (x), in other words, and given an integer N , we can find a set of weights wj and abscissas xj such that the approximation
142
Chapter 4.
Integration of Functions
The above routine illustrates that one can use Gaussian quadratures without necessarily understanding the theory behind them: One just locates tabulated weights and abscissas in a book (e.g., [1] or [2]). However, the theory is very pretty, and it will come in handy if you ever need to construct your own tabulation of weights and abscissas for an unusual choice of W (x). We will therefore give, without any proofs, some useful results that will enable you to do this. Several of the results assume that W (x) does not change sign inside (a, b), which is usually the case in practice. The theory behind Gaussian quadratures goes back to Gauss in 1814, who used continued fractions to develop the subject. In 1826 Jacobi rederived Gauss’s results by means of orthogonal polynomials. The systematic treatment of arbitrary weight functions W (x) using orthogonal polynomials is largely due to Christoffel in 1877. To introduce these orthogonal polynomials, let us fix the interval of interest to be (a, b). We can define the “scalar product of two functions f and g over a weight function W ” as Z b hf|gi ≡ W (x)f(x)g(x)dx (4.5.5) a
The scalar product is a number, not a function of x. Two functions are said to be orthogonal if their scalar product is zero. A function is said to be normalized if its scalar product with itself is unity. A set of functions that are all mutually orthogonal and also all individually normalized is called an orthonormal set. We can find a set of polynomials (i) that includes exactly one polynomial of order j, called pj (x), for each j = 0, 1, 2, . . ., and (ii) all of which are mutually orthogonal over the specified weight function W (x). A constructive procedure for finding such a set is the recurrence relation p−1 (x) ≡ 0 p0 (x) ≡ 1
(4.5.6)
pj+1 (x) = (x − aj )pj (x) − bj pj−1(x)
j = 0, 1, 2, . . .
where hxpj |pj i hpj |pj i hpj |pj i bj = hpj−1 |pj−1i
aj =
j = 0, 1, . . . (4.5.7) j = 1, 2, . . .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
REAL dx,xm,xr,w(5),x(5) The abscissas and weights. SAVE w,x DATA w/.2955242247,.2692667193,.2190863625,.1494513491,.0666713443/ DATA x/.1488743389,.4333953941,.6794095682,.8650633666,.9739065285/ xm=0.5*(b+a) xr=0.5*(b-a) ss=0 Will be twice the average value of the function, since the ten do 11 j=1,5 weights (five numbers above each used twice) sum to 2. dx=xr*x(j) ss=ss+w(j)*(func(xm+dx)+func(xm-dx)) enddo 11 ss=xr*ss Scale the answer to the range of integration. return END
4.5 Gaussian Quadratures and Orthogonal Polynomials
143
p (x ) 0 1 p1 (x1 ) .. . pN−1 (x1 )
... ...
Rb w1 a W (x)p0 (x)dx w2 0 . = . .. ..
p0 (xN ) p1 (xN ) .. .
. . . pN−1 (xN )
wN
(4.5.8)
0
Equation (4.5.8) simply solves for those weights such that the quadrature (4.5.1) gives the correct answer for the integral of the first N orthogonal polynomials. Note that the zeros on the right-hand side of (4.5.8) appear because p1 (x), . . . , pN−1 (x) are all orthogonal to p0 (x), which is a constant. It can be shown that, with those weights, the integral of the next N − 1 polynomials is also exact, so that the quadrature is exact for all polynomials of degree 2N − 1 or less. Another way to evaluate the weights (though one whose proof is beyond our scope) is by the formula wj =
hpN−1 |pN−1 i pN−1 (xj )p0N (xj )
(4.5.9)
where p0N (xj ) is the derivative of the orthogonal polynomial at its zero xj . The computation of Gaussian quadrature rules thus involves two distinct phases: (i) the generation of the orthogonal polynomials p0 , . . . , pN , i.e., the computation of the coefficients aj , bj in (4.5.6); (ii) the determination of the zeros of pN (x), and the computation of the associated weights. For the case of the “classical” orthogonal polynomials, the coefficients aj and bj are explicitly known (equations 4.5.10 –
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The coefficient b0 is arbitrary; we can take it to be zero. The polynomials defined by (4.5.6) are monic, i.e., the coefficient of their leading term [xj for pj (x)] is unity. If we divide each pj (x) by the constant [hpj |pj i]1/2 we can render the set of polynomials orthonormal. One also encounters orthogonal polynomials with various other normalizations. You can convert from a given normalization to monic polynomials if you know that the coefficient of xj in pj is λj , say; then the monic polynomials are obtained by dividing each pj by λj . Note that the coefficients in the recurrence relation (4.5.6) depend on the adopted normalization. The polynomial pj (x) can be shown to have exactly j distinct roots in the interval (a, b). Moreover, it can be shown that the roots of pj (x) “interleave” the j − 1 roots of pj−1 (x), i.e., there is exactly one root of the former in between each two adjacent roots of the latter. This fact comes in handy if you need to find all the roots: You can start with the one root of p1 (x) and then, in turn, bracket the roots of each higher j, pinning them down at each stage more precisely by Newton’s rule or some other root-finding scheme (see Chapter 9). Why would you ever want to find all the roots of an orthogonal polynomial pj (x)? Because the abscissas of the N -point Gaussian quadrature formulas (4.5.1) and (4.5.4) with weighting function W (x) in the interval (a, b) are precisely the roots of the orthogonal polynomial pN (x) for the same interval and weighting function. This is the fundamental theorem of Gaussian quadratures, and lets you find the abscissas for any particular case. Once you know the abscissas x1 , . . . , xN , you need to find the weights wj , j = 1, . . . , N . One way to do this (not the most efficient) is to solve the set of linear equations
144
Chapter 4.
Integration of Functions
4.5.14 below) and phase (i) can be omitted. However, if you are confronted with a “nonclassical” weight function W (x), and you don’t know the coefficients aj and bj , the construction of the associated set of orthogonal polynomials is not trivial. We discuss it at the end of this section.
This task can range from easy to difficult, depending on how much you already know about your weight function and its associated polynomials. In the case of classical, well-studied, orthogonal polynomials, practically everything is known, including good approximations for their zeros. These can be used as starting guesses, enabling Newton’s method (to be discussed in §9.4) to converge very rapidly. Newton’s method requires the derivative p0N (x), which is evaluated by standard relations in terms of pN and pN−1 . The weights are then conveniently evaluated by equation (4.5.9). For the following named cases, this direct root-finding is faster, by a factor of 3 to 5, than any other method. Here are the weight functions, intervals, and recurrence relations that generate the most commonly used orthogonal polynomials and their corresponding Gaussian quadrature formulas. Gauss-Legendre: W (x) = 1
−1 (a + 1)/(a + b + 2) we can just use the symmetry relation (6.4.3) to obtain an equivalent computation where the continued fraction will also converge rapidly. Hence we have
C
*
FUNCTION betai(a,b,x) REAL betai,a,b,x USES betacf,gammln Returns the incomplete beta function Ix (a, b). REAL bt,betacf,gammln if(x.lt.0..or.x.gt.1.)pause ’bad argument x in betai’ if(x.eq.0..or.x.eq.1.)then bt=0. else Factors in front of the continued fraction. bt=exp(gammln(a+b)-gammln(a)-gammln(b) +a*log(x)+b*log(1.-x)) endif if(x.lt.(a+1.)/(a+b+2.))then Use continued fraction directly.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
.8
6.4 Incomplete Beta Function, Student’s Distribution, F-Distribution, Cumulative Binomial Distribution 221
betai=bt*betacf(a,b,x)/a return else betai=1.-bt*betacf(b,a,1.-x)/b return endif END
Use continued fraction after making the symmetry transformation.
1
FUNCTION betacf(a,b,x) INTEGER MAXIT REAL betacf,a,b,x,EPS,FPMIN PARAMETER (MAXIT=100,EPS=3.e-7,FPMIN=1.e-30) Used by betai: Evaluates continued fraction for incomplete beta function by modified Lentz’s method (§5.2). INTEGER m,m2 REAL aa,c,d,del,h,qab,qam,qap qab=a+b These q’s will be used in factors that occur in the qap=a+1. coefficients (6.4.6). qam=a-1. c=1. First step of Lentz’s method. d=1.-qab*x/qap if(abs(d).lt.FPMIN)d=FPMIN d=1./d h=d do 11 m=1,MAXIT m2=2*m aa=m*(b-m)*x/((qam+m2)*(a+m2)) d=1.+aa*d One step (the even one) of the recurrence. if(abs(d).lt.FPMIN)d=FPMIN c=1.+aa/c if(abs(c).lt.FPMIN)c=FPMIN d=1./d h=h*d*c aa=-(a+m)*(qab+m)*x/((a+m2)*(qap+m2)) d=1.+aa*d Next step of the recurrence (the odd one). if(abs(d).lt.FPMIN)d=FPMIN c=1.+aa/c if(abs(c).lt.FPMIN)c=FPMIN d=1./d del=d*c h=h*del if(abs(del-1.).lt.EPS)goto 1 Are we done? enddo 11 pause ’a or b too big, or MAXIT too small in betacf’ betacf=h return END
Student’s Distribution Probability Function Student’s distribution, denoted A(t|ν), is useful in several statistical contexts, notably in the test of whether two observed distributions have the same mean. A(t|ν) is the probability, for ν degrees of freedom, that a certain statistic t (measuring the observed difference of means) would be smaller than the observed value if the means were in fact the same. (See Chapter 14 for further details.) Two means are
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
which utilizes the continued fraction evaluation routine
222
Chapter 6.
Special Functions
significantly different if, e.g., A(t|ν) > 0.99. In other words, 1 − A(t|ν) is the significance level at which the hypothesis that the means are equal is disproved. The mathematical definition of the function is Z t −t
x2 1+ ν
− ν+1 2 dx
(6.4.7)
Limiting values are A(0|ν) = 0
A(∞|ν) = 1
(6.4.8)
A(t|ν) is related to the incomplete beta function Ix (a, b) by A(t|ν) = 1 − I
ν ν+t2
ν 1 , 2 2
(6.4.9)
So, you can use (6.4.9) and the above routine betai to evaluate the function.
F-Distribution Probability Function This function occurs in the statistical test of whether two observed samples have the same variance. A certain statistic F , essentially the ratio of the observed dispersion of the first sample to that of the second one, is calculated. (For further details, see Chapter 14.) The probability that F would be as large as it is if the first sample’s underlying distribution actually has smaller variance than the second’s is denoted Q(F |ν1, ν2 ), where ν1 and ν2 are the number of degrees of freedom in the first and second samples, respectively. In other words, Q(F |ν1, ν2) is the significance level at which the hypothesis “1 has smaller variance than 2” can be rejected. A small numerical value implies a very significant rejection, in turn implying high confidence in the hypothesis “1 has variance greater or equal to 2.” Q(F |ν1, ν2 ) has the limiting values Q(0|ν1 , ν2) = 1
Q(∞|ν1, ν2) = 0
(6.4.10)
Its relation to the incomplete beta function Ix (a, b) as evaluated by betai above is Q(F |ν1, ν2 ) = I
ν2 ν2 +ν1 F
ν2 ν1 , 2 2
(6.4.11)
Cumulative Binomial Probability Distribution Suppose an event occurs with probability p per trial. Then the probability P of its occurring k or more times in n trials is termed a cumulative binomial probability, and is related to the incomplete beta function Ix (a, b) as follows: P ≡
n X n j=k
j
pj (1 − p)n−j = Ip (k, n − k + 1)
(6.4.12)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1 A(t|ν) = 1/2 1 ν ν B( 2 , 2 )
6.5 Bessel Functions of Integer Order
223
For n larger than a dozen or so, betai is a much better way to evaluate the sum in (6.4.12) than would be the straightforward sum with concurrent computation of the binomial coefficients. (For n smaller than a dozen, either method is acceptable.)
CITED REFERENCES AND FURTHER READING:
Pearson, E., and Johnson, N. 1968, Tables of the Incomplete Beta Function (Cambridge: Cambridge University Press).
6.5 Bessel Functions of Integer Order This section and the next one present practical algorithms for computing various kinds of Bessel functions of integer order. In §6.7 we deal with fractional order. In fact, the more complicated routines for fractional order work fine for integer order too. For integer order, however, the routines in this section (and §6.6) are simpler and faster. Their only drawback is that they are limited by the precision of the underlying rational approximations. For full double precision, it is best to work with the routines for fractional order in §6.7. For any real ν, the Bessel function Jν (x) can be defined by the series representation Jν (x) =
1 x 2
ν X ∞ k=0
(− 14 x2 )k k!Γ(ν + k + 1)
(6.5.1)
The series converges for all x, but it is not computationally very useful for x 1. For ν not an integer the Bessel function Yν (x) is given by Yν (x) =
Jν (x) cos(νπ) − J−ν (x) sin(νπ)
(6.5.2)
The right-hand side goes to the correct limiting value Yn (x) as ν goes to some integer n, but this is also not computationally useful. For arguments x < ν, both Bessel functions look qualitatively like simple power laws, with the asymptotic forms for 0 < x ν ν 1 1 x Jν (x) ∼ Γ(ν + 1) 2 2 Y0 (x) ∼ ln(x) π −ν Γ(ν) 1 Yν (x) ∼ − x π 2
ν ≥0 (6.5.3) ν>0
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), Chapters 6 and 26.
224
Chapter 6.
Special Functions
1 J0 J1
J2
J3
0 − .5
Y0
Y1
Y2
−1 − 1.5 −2
0
Figure 6.5.1.
2
4
x
6
8
10
Bessel functions J0 (x) through J3 (x) and Y0 (x) through Y2 (x).
For x > ν, both Bessel functions look qualitatively like sine or cosine waves whose amplitude decays as x−1/2 . The asymptotic forms for x ν are r 1 1 2 cos x − νπ − π Jν (x) ∼ πx 2 4 (6.5.4) r 1 1 2 Yν (x) ∼ sin x − νπ − π πx 2 4 In the transition region where x ∼ ν, the typical amplitudes of the Bessel functions are on the order Jν (ν) ∼
21/3 0.4473 1 ∼ 1/3 ν 32/3 Γ( 23 ) ν 1/3
21/3 0.7748 1 Yν (ν) ∼ − 1/6 2 1/3 ∼ − 1/3 ν 3 Γ( 3 ) ν
(6.5.5)
which holds asymptotically for large ν. Figure 6.5.1 plots the first few Bessel functions of each kind. The Bessel functions satisfy the recurrence relations Jn+1 (x) =
2n Jn (x) − Jn−1 (x) x
(6.5.6)
Yn+1 (x) =
2n Yn (x) − Yn−1 (x) x
(6.5.7)
and
As already mentioned in §5.5, only the second of these (6.5.7) is stable in the direction of increasing n for x < n. The reason that (6.5.6) is unstable in the
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Bessel functions
.5
225
6.5 Bessel Functions of Integer Order
Y0 (x) −
2 J0 (x) ln(x) π
and
Y1 (x) −
2 1 J1 (x) ln(x) − π x
(6.5.8)
For 8 < x < ∞, use the approximating forms (n = 0, 1) r
2 8 8 Pn cos(Xn ) − Qn sin(Xn ) Jn (x) = πx x x r 2 8 8 Pn sin(Xn ) + Qn cos(Xn ) Yn (x) = πx x x
(6.5.9) (6.5.10)
where Xn ≡ x −
2n + 1 π 4
(6.5.11)
and where P0 , P1 , Q0 , and Q1 are each polynomials in their arguments, for 0 < 8/x < 1. The P ’s are even polynomials, the Q’s odd. Coefficients of the various rational functions and polynomials are given by Hart [1], for various levels of desired accuracy. A straightforward implementation is
* * * * * * *
*
FUNCTION bessj0(x) REAL bessj0,x Returns the Bessel function J0 (x) for any real x. REAL ax,xx,z DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4, r5,r6,s1,s2,s3,s4,s5,s6,y We’ll accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6, s1,s2,s3,s4,s5,s6 DATA p1,p2,p3,p4,p5/1.d0,-.1098628627d-2,.2734510407d-4, -.2073370639d-5,.2093887211d-6/, q1,q2,q3,q4,q5/-.1562499995d-1, .1430488765d-3,-.6911147651d-5,.7621095161d-6,-.934945152d-7/ DATA r1,r2,r3,r4,r5,r6/57568490574.d0,-13362590354.d0,651619640.7d0, -11214424.18d0,77392.33017d0,-184.9052456d0/, s1,s2,s3,s4,s5,s6/57568490411.d0,1029532985.d0, 9494680.718d0,59272.64853d0,267.8532712d0,1.d0/ if(abs(x).lt.8.)then Direct rational function fit. y=x**2 bessj0=(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6))))) /(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6))))) else Fitting function (6.5.9). ax=abs(x) z=8./ax y=z**2 xx=ax-.785398164 bessj0=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
direction of increasing n is simply that it is the same recurrence as (6.5.7): A small amount of “polluting” Yn introduced by roundoff error will quickly come to swamp the desired Jn , according to equation (6.5.3). A practical strategy for computing the Bessel functions of integer order divides into two tasks: first, how to compute J0 , J1 , Y0 , and Y1 , and second, how to use the recurrence relations stably to find other J’s and Y ’s. We treat the first task first: For x between zero and some arbitrary value (we will use the value 8), approximate J0 (x) and J1 (x) by rational functions in x. Likewise approximate by rational functions the “regular part” of Y0 (x) and Y1 (x), defined as
226 *
Chapter 6.
Special Functions
*p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5))))) endif return END
* * * * * * * *
*
*
* * * * * * *
*
FUNCTION bessj1(x) REAL bessj1,x Returns the Bessel function J1 (x) for any real x. REAL ax,xx,z DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4, r5,r6,s1,s2,s3,s4,s5,s6,y We’ll accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4,r5,r6, s1,s2,s3,s4,s5,s6 DATA r1,r2,r3,r4,r5,r6/72362614232.d0,-7895059235.d0,242396853.1d0, -2972611.439d0,15704.48260d0,-30.16036606d0/, s1,s2,s3,s4,s5,s6/144725228442.d0,2300535178.d0, 18583304.74d0,99447.43394d0,376.9991397d0,1.d0/ DATA p1,p2,p3,p4,p5/1.d0,.183105d-2,-.3516396496d-4,.2457520174d-5, -.240337019d-6/, q1,q2,q3,q4,q5/.04687499995d0,-.2002690873d-3, .8449199096d-5,-.88228987d-6,.105787412d-6/ if(abs(x).lt.8.)then Direct rational approximation. y=x**2 bessj1=x*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6))))) /(s1+y*(s2+y*(s3+y*(s4+y*(s5+y*s6))))) else Fitting function (6.5.9). ax=abs(x) z=8./ax y=z**2 xx=ax-2.356194491
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C
FUNCTION bessy0(x) REAL bessy0,x USES bessj0 Returns the Bessel function Y0 (x) for positive x. REAL xx,z,bessj0 DOUBLE PRECISION p1,p2,p3,p4,p5,q1, q2,q3,q4,q5,r1,r2,r3,r4, r5,r6,s1,s2,s3,s4,s5,s6,y We’ll accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4, r5,r6,s1,s2,s3,s4,s5,s6 DATA p1,p2,p3,p4,p5/1.d0,-.1098628627d-2,.2734510407d-4, -.2073370639d-5,.2093887211d-6/, q1,q2,q3,q4,q5/-.1562499995d-1, .1430488765d-3,-.6911147651d-5,.7621095161d-6,-.934945152d-7/ DATA r1,r2,r3,r4,r5,r6/-2957821389.d0,7062834065.d0,-512359803.6d0, 10879881.29d0,-86327.92757d0,228.4622733d0/, s1,s2,s3,s4,s5,s6/40076544269.d0,745249964.8d0, 7189466.438d0,47447.26470d0,226.1030244d0,1.d0/ if(x.lt.8.)then Rational function approximation of (6.5.8). y=x**2 bessy0=(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y *(s3+y*(s4+y*(s5+y*s6)))))+.636619772*bessj0(x)*log(x) else Fitting function (6.5.10). z=8./x y=z**2 xx=x-.785398164 bessy0=sqrt(.636619772/x)*(sin(xx)*(p1+y*(p2+y*(p3+y*(p4+y* p5))))+z*cos(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5))))) endif return END
6.5 Bessel Functions of Integer Order
* *
* * * * * * *
* *
*
bessj1=sqrt(.636619772/ax)*(cos(xx)*(p1+y*(p2+y*(p3+y*(p4+y *p5))))-z*sin(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5))))) *sign(1.,x) endif return END FUNCTION bessy1(x) REAL bessy1,x USES bessj1 Returns the Bessel function Y1 (x) for positive x. REAL xx,z,bessj1 DOUBLE PRECISION p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4, r5,r6,s1,s2,s3,s4,s5,s6,s7,y We’ll accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,q1,q2,q3,q4,q5,r1,r2,r3,r4, r5,r6,s1,s2,s3,s4,s5,s6,s7 DATA p1,p2,p3,p4,p5/1.d0,.183105d-2,-.3516396496d-4,.2457520174d-5, -.240337019d-6/, q1,q2,q3,q4,q5/.04687499995d0,-.2002690873d-3, .8449199096d-5,-.88228987d-6,.105787412d-6/ DATA r1,r2,r3,r4,r5,r6/-.4900604943d13,.1275274390d13,-.5153438139d11, .7349264551d9,-.4237922726d7,.8511937935d4/, s1,s2,s3,s4,s5,s6,s7/.2499580570d14,.4244419664d12, .3733650367d10,.2245904002d8,.1020426050d6,.3549632885d3,1.d0/ if(x.lt.8.)then Rational function approximation of (6.5.8). y=x**2 bessy1=x*(r1+y*(r2+y*(r3+y*(r4+y*(r5+y*r6)))))/(s1+y*(s2+y* (s3+y*(s4+y*(s5+y*(s6+y*s7))))))+.636619772 *(bessj1(x)*log(x)-1./x) else Fitting function (6.5.10). z=8./x y=z**2 xx=x-2.356194491 bessy1=sqrt(.636619772/x)*(sin(xx)*(p1+y*(p2+y*(p3+y*(p4+y *p5))))+z*cos(xx)*(q1+y*(q2+y*(q3+y*(q4+y*q5))))) endif return END
We now turn to the second task, namely how to use the recurrence formulas (6.5.6) and (6.5.7) to get the Bessel functions Jn (x) and Yn (x) for n ≥ 2. The latter of these is straightforward, since its upward recurrence is always stable:
C
FUNCTION bessy(n,x) INTEGER n REAL bessy,x USES bessy0,bessy1 Returns the Bessel function Yn (x) for positive x and n ≥ 2. INTEGER j REAL by,bym,byp,tox,bessy0,bessy1 if(n.lt.2)pause ’bad argument n in bessy’ tox=2./x by=bessy1(x) Starting values for the recurrence. bym=bessy0(x) do 11 j=1,n-1 Recurrence (6.5.7). byp=j*tox*by-bym bym=by by=byp enddo 11 bessy=by return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C
227
228
Chapter 6.
Special Functions
C
FUNCTION bessj(n,x) INTEGER n,IACC REAL bessj,x,BIGNO,BIGNI PARAMETER (IACC=40,BIGNO=1.e10,BIGNI=1.e-10) USES bessj0,bessj1 Returns the Bessel function Jn (x) for any real x and n ≥ 2. INTEGER j,jsum,m REAL ax,bj,bjm,bjp,sum,tox,bessj0,bessj1 if(n.lt.2)pause ’bad argument n in bessj’ ax=abs(x) if(ax.eq.0.)then bessj=0. else if(ax.gt.float(n))then Upwards recurrence from J0 and J1 . tox=2./ax bjm=bessj0(ax) bj=bessj1(ax) do 11 j=1,n-1 bjp=j*tox*bj-bjm bjm=bj bj=bjp enddo 11 bessj=bj else Downwards recurrence from an even m here comtox=2./ax puted. Make IACC larger to increase accuracy. m=2*((n+int(sqrt(float(IACC*n))))/2) bessj=0. jsum=0 jsum will alternate between 0 and 1; when it is 1, we sum=0. accumulate in sum the even terms in (5.5.16). bjp=0. bj=1. do 12 j=m,1,-1 The downward recurrence. bjm=j*tox*bj-bjp bjp=bj bj=bjm if(abs(bj).gt.BIGNO)then Renormalize to prevent overflows. bj=bj*BIGNI
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The cost of this algorithm is the call to bessy1 and bessy0 (which generate a call to each of bessj1 and bessj0), plus O(n) operations in the recurrence. As for Jn (x), things are a bit more complicated. We can start the recurrence upward on n from J0 and J1 , but it will remain stable only while n does not exceed x. This is, however, just fine for calls with large x and small n, a case which occurs frequently in practice. The harder case to provide for is that with x < n. The best thing to do here is to use Miller’s algorithm (see discussion preceding equation 5.5.16), applying the recurrence downward from some arbitrary starting value and making use of the upward-unstable nature of the recurrence to put us onto the correct solution. When we finally arrive at J0 or J1 we are able to normalize the solution with the sum (5.5.16) accumulated along the way. The only subtlety is in deciding at how large an n we need start the downward recurrence so as to obtain a desired accuracy by the time we reach the n that we really want. If you play with the asymptotic forms (6.5.3) and (6.5.5), you should be able to convince yourself that the answer is to start larger than the desired n by an additive amount of order [constant × n]1/2 , where the square root of the constant is, very roughly, the number of significant figures of accuracy. The above considerations lead to the following function.
6.6 Modified Bessel Functions of Integer Order
229
CITED REFERENCES AND FURTHER READING: Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), Chapter 9. Hart, J.F., et al. 1968, Computer Approximations (New York: Wiley), §6.8, p. 141. [1]
6.6 Modified Bessel Functions of Integer Order The modified Bessel functions In (x) and Kn (x) are equivalent to the usual Bessel functions Jn and Yn evaluated for purely imaginary arguments. In detail, the relationship is In (x) = (−i)n Jn (ix) Kn (x) =
π n+1 i [Jn (ix) + iYn (ix)] 2
(6.6.1)
The particular choice of prefactor and of the linear combination of Jn and Yn to form Kn are simply choices that make the functions real-valued for real arguments x. For small arguments x n, both In (x) and Kn (x) become, asymptotically, simple powers of their argument In (x) ≈
1 x n n! 2
n≥0
K0 (x) ≈ − ln(x) Kn (x) ≈
(n − 1)! x −n 2 2
(6.6.2) n>0
These expressions are virtually identical to those for Jn (x) and Yn (x) in this region, except for the factor of −2/π difference between Yn (x) and Kn (x). In the region
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
bjp=bjp*BIGNI bessj=bessj*BIGNI sum=sum*BIGNI endif if(jsum.ne.0)sum=sum+bj Accumulate the sum. jsum=1-jsum Change 0 to 1 or vice versa. if(j.eq.n)bessj=bjp Save the unnormalized answer. enddo 12 sum=2.*sum-bj Compute (5.5.16) bessj=bessj/sum and use it to normalize the answer. endif if(x.lt.0..and.mod(n,2).eq.1)bessj=-bessj return END
230
Chapter 6.
Special Functions
4
I0
2 K0
K1
K2
I1 I2
1
I3
0 0
1
2 x
3
4
Figure 6.6.1. Modified Bessel functions I0 (x) through I3 (x), K0 (x) through K2 (x).
x n, however, the modified functions have quite different behavior than the Bessel functions, 1 exp(x) In (x) ≈ √ 2πx π Kn (x) ≈ √ exp(−x) 2πx
(6.6.3)
The modified functions evidently have exponential rather than sinusoidal behavior for large arguments (see Figure 6.6.1). The smoothness of the modified Bessel functions, once the exponential factor is removed, makes a simple polynomial approximation of a few terms quite suitable for the functions I0 , I1 , K0 , and K1 . The following routines, based on polynomial coefficients given by Abramowitz and Stegun [1], evaluate these four functions, and will provide the basis for upward recursion for n > 1 when x > n.
*
* * *
FUNCTION bessi0(x) REAL bessi0,x Returns the modified Bessel function I0 (x) for any real x. REAL ax DOUBLE PRECISION p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7, q8,q9,y Accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7,q8,q9 DATA p1,p2,p3,p4,p5,p6,p7/1.0d0,3.5156229d0,3.0899424d0,1.2067492d0, 0.2659732d0,0.360768d-1,0.45813d-2/ DATA q1,q2,q3,q4,q5,q6,q7,q8,q9/0.39894228d0,0.1328592d-1, 0.225319d-2,-0.157565d-2,0.916281d-2,-0.2057706d-1, 0.2635537d-1,-0.1647633d-1,0.392377d-2/
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
modified Bessel functions
3
6.6 Modified Bessel Functions of Integer Order
C
*
* *
*
*
*
* * *
*
if (abs(x).lt.3.75) then y=(x/3.75)**2 bessi0=p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7))))) else ax=abs(x) y=3.75/ax bessi0=(exp(ax)/sqrt(ax))*(q1+y*(q2+y*(q3+y*(q4 +y*(q5+y*(q6+y*(q7+y*(q8+y*q9)))))))) endif return END
FUNCTION bessk0(x) REAL bessk0,x USES bessi0 Returns the modified Bessel function K0 (x) for positive real x. REAL bessi0 DOUBLE PRECISION p1,p2,p3,p4,p5,p6,p7,q1, q2,q3,q4,q5,q6,q7,y Accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7 DATA p1,p2,p3,p4,p5,p6,p7/-0.57721566d0,0.42278420d0,0.23069756d0, 0.3488590d-1,0.262698d-2,0.10750d-3,0.74d-5/ DATA q1,q2,q3,q4,q5,q6,q7/1.25331414d0,-0.7832358d-1,0.2189568d-1, -0.1062446d-1,0.587872d-2,-0.251540d-2,0.53208d-3/ if (x.le.2.0) then Polynomial fit. y=x*x/4.0 bessk0=(-log(x/2.0)*bessi0(x))+(p1+y*(p2+y*(p3+ y*(p4+y*(p5+y*(p6+y*p7)))))) else y=(2.0/x) bessk0=(exp(-x)/sqrt(x))*(q1+y*(q2+y*(q3+ y*(q4+y*(q5+y*(q6+y*q7)))))) endif return END
FUNCTION bessi1(x) REAL bessi1,x Returns the modified Bessel function I1 (x) for any real x. REAL ax DOUBLE PRECISION p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7, q8,q9,y Accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7,q8,q9 DATA p1,p2,p3,p4,p5,p6,p7/0.5d0,0.87890594d0,0.51498869d0, 0.15084934d0,0.2658733d-1,0.301532d-2,0.32411d-3/ DATA q1,q2,q3,q4,q5,q6,q7,q8,q9/0.39894228d0,-0.3988024d-1, -0.362018d-2,0.163801d-2,-0.1031555d-1,0.2282967d-1, -0.2895312d-1,0.1787654d-1,-0.420059d-2/ if (abs(x).lt.3.75) then Polynomial fit. y=(x/3.75)**2 bessi1=x*(p1+y*(p2+y*(p3+y*(p4+y*(p5+y*(p6+y*p7)))))) else ax=abs(x) y=3.75/ax bessi1=(exp(ax)/sqrt(ax))*(q1+y*(q2+y*(q3+y*(q4+ y*(q5+y*(q6+y*(q7+y*(q8+y*q9)))))))) if(x.lt.0.)bessi1=-bessi1 endif return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
231
232
C
*
*
*
*
Special Functions
FUNCTION bessk1(x) REAL bessk1,x USES bessi1 Returns the modified Bessel function K1 (x) for positive real x. REAL bessi1 DOUBLE PRECISION p1,p2,p3,p4,p5,p6,p7,q1, q2,q3,q4,q5,q6,q7,y Accumulate polynomials in double precision. SAVE p1,p2,p3,p4,p5,p6,p7,q1,q2,q3,q4,q5,q6,q7 DATA p1,p2,p3,p4,p5,p6,p7/1.0d0,0.15443144d0,-0.67278579d0, -0.18156897d0,-0.1919402d-1,-0.110404d-2,-0.4686d-4/ DATA q1,q2,q3,q4,q5,q6,q7/1.25331414d0,0.23498619d0,-0.3655620d-1, 0.1504268d-1,-0.780353d-2,0.325614d-2,-0.68245d-3/ if (x.le.2.0) then Polynomial fit. y=x*x/4.0 bessk1=(log(x/2.0)*bessi1(x))+(1.0/x)*(p1+y*(p2+ y*(p3+y*(p4+y*(p5+y*(p6+y*p7)))))) else y=2.0/x bessk1=(exp(-x)/sqrt(x))*(q1+y*(q2+y*(q3+ y*(q4+y*(q5+y*(q6+y*q7)))))) endif return END
The recurrence relation for In (x) and Kn (x) is the same as that for Jn (x) and Yn (x) provided that ix is substituted for x. This has the effect of changing a sign in the relation, 2n In (x) + In−1 (x) In+1 (x) = − x (6.6.4) 2n Kn (x) + Kn−1 (x) Kn+1 (x) = + x These relations are always unstable for upward recurrence. For Kn , itself growing, this presents no problem. For In , however, the strategy of downward recursion is therefore required once again, and the starting point for the recursion may be chosen in the same manner as for the routine bessj. The only fundamental difference is that the normalization formula for In (x) has an alternating minus sign in successive terms, which again arises from the substitution of ix for x in the formula used previously for Jn 1 = I0 (x) − 2I2 (x) + 2I4 (x) − 2I6 (x) + · · ·
(6.6.5)
In fact, we prefer simply to normalize with a call to bessi0. With this simple modification, the recursion routines bessj and bessy become the new routines bessi and bessk:
C
FUNCTION bessk(n,x) INTEGER n REAL bessk,x USES bessk0,bessk1 Returns the modified Bessel function Kn (x) for positive x and n ≥ 2. INTEGER j REAL bk,bkm,bkp,tox,bessk0,bessk1 if (n.lt.2) pause ’bad argument n in bessk’ tox=2.0/x
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
Chapter 6.
6.6 Modified Bessel Functions of Integer Order
C
Upward recurrence for all x... ...and here it is.
FUNCTION bessi(n,x) INTEGER n,IACC REAL bessi,x,BIGNO,BIGNI PARAMETER (IACC=40,BIGNO=1.0e10,BIGNI=1.0e-10) USES bessi0 Returns the modified Bessel function In (x) for any real x and n ≥ 2. INTEGER j,m REAL bi,bim,bip,tox,bessi0 if (n.lt.2) pause ’bad argument n in bessi’ if (x.eq.0.) then bessi=0. else tox=2.0/abs(x) bip=0.0 bi=1.0 bessi=0. m=2*((n+int(sqrt(float(IACC*n))))) Downward recurrence from even m. do 11 j=m,1,-1 Make IACC larger to increase accuracy. bim=bip+float(j)*tox*bi The downward recurrence. bip=bi bi=bim if (abs(bi).gt.BIGNO) then Renormalize to prevent overflows. bessi=bessi*BIGNI bi=bi*BIGNI bip=bip*BIGNI endif if (j.eq.n) bessi=bip enddo 11 bessi=bessi*bessi0(x)/bi Normalize with bessi0. if (x.lt.0..and.mod(n,2).eq.1) bessi=-bessi endif return END
CITED REFERENCES AND FURTHER READING: Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), §9.8. [1] Carrier, G.F., Krook, M. and Pearson, C.E. 1966, Functions of a Complex Variable (New York: McGraw-Hill), pp. 220ff.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
bkm=bessk0(x) bk=bessk1(x) do 11 j=1,n-1 bkp=bkm+j*tox*bk bkm=bk bk=bkp enddo 11 bessk=bk return END
233
234
Chapter 6.
Special Functions
6.7 Bessel Functions of Fractional Order, Airy Functions, Spherical Bessel Functions
Ordinary Bessel Functions The basic idea is Steed’s method, which was originally developed [1] for Coulomb wave functions. The method calculates Jν , Jν0 , Yν , and Yν0 simultaneously, and so involves four relations among these functions. Three of the relations come from two continued fractions, one of which is complex. The fourth is provided by the Wronskian relation W ≡ Jν Yν0 − Yν Jν0 =
2 πx
(6.7.1)
The first continued fraction, CF1, is defined by fν ≡
Jν0 ν Jν+1 = − Jν x Jν 1 1 ν ··· = − x 2(ν + 1)/x − 2(ν + 2)/x −
(6.7.2)
You can easily derive it from the three-term recurrence relation for Bessel functions: Start with equation (6.5.6) and use equation (5.5.18). Forward evaluation of the continued fraction by one of the methods of §5.2 is essentially equivalent to backward recurrence of the recurrence relation.pThe rate of convergence of CF1 is determined by the position of the turning point xtp = ν(ν + 1) ≈ ν, beyond which the Bessel functions become oscillatory. If x < ∼ xtp, convergence is very rapid. If x > ∼ xtp , then each iteration of the continued fraction effectively increases ν by one until x < ∼ xtp ; thereafter rapid convergence sets in. Thus the number of iterations of CF1 is of order x for large x. In the routine bessjy we set the maximum allowed number of iterations to 10,000. For larger x, you can use the usual asymptotic expressions for Bessel functions. One can show that the sign of Jν is the same as the sign of the denominator of CF1 once it has converged. The complex continued fraction CF2 is defined by p + iq ≡
Jν0 + iYν0 1 i (1/2)2 − ν 2 (3/2)2 − ν 2 =− +i+ ··· Jν + iYν 2x x 2(x + i) + 2(x + 2i) +
(6.7.3)
(We sketch the derivation of CF2 in the analogous case of modified Bessel functions in the next subsection.) This continued fraction converges rapidly for x > ∼ xtp, while convergence fails as x → 0. We have to adopt a special method for small x, which we describe below. For 0 x not too small, we can ensure that x > ∼ xtp by a stable recurrence of Jν and Jν downwards < to a value ν = µ ∼ x, thus yielding the ratio fµ at this lower value of ν. This is the stable direction for the recurrence relation. The initial values for the recurrence are Jν = arbitrary,
Jν0 = fν Jν ,
(6.7.4)
with the sign of the arbitrary initial value of Jν chosen to be the sign of the denominator of CF1. Choosing the initial value of Jν very small minimizes the possibility of overflow during the recurrence. The recurrence relations are ν Jν−1 = Jν + Jν0 x (6.7.5) ν−1 0 Jν−1 − Jν Jν−1 = x
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Many algorithms have been proposed for computing Bessel functions of fractional order numerically. Most of them are, in fact, not very good in practice. The routines given here are rather complicated, but they can be recommended wholeheartedly.
6.7 Bessel Functions of Fractional Order
235
Once CF2 has been evaluated at ν = µ, then with the Wronskian (6.7.1) we have enough relations to solve for all four quantities. The formulas are simplified by introducing the quantity γ≡ Then Jµ = ±
W q + γ(p − fµ )
(6.7.6) 1/2 (6.7.7)
Jµ0 = fµ Jµ Yµ = γJµ q 0 Yµ = Yµ p + γ
(6.7.8) (6.7.9) (6.7.10)
The sign of Jµ in (6.7.7) is chosen to be the same as the sign of the initial Jν in (6.7.4). Once all four functions have been determined at the value ν = µ, we can find them at the original value of ν. For Jν and Jν0 , simply scale the values in (6.7.4) by the ratio of (6.7.7) to the value found after applying the recurrence (6.7.5). The quantities Yν and Yν0 can be found by starting with the values in (6.7.9) and (6.7.10) and using the stable upwards recurrence Yν+1 =
2ν Yν − Yν−1 x
(6.7.11)
ν Yν − Yν+1 x
(6.7.12)
together with the relation Yν0 =
Now turn to the case of small x, when CF2 is not suitable. Temme [2] has given a good method of evaluating Yν and Yν+1 , and hence Yν0 from (6.7.12), by series expansions that accurately handle the singularity as x → 0. The expansions work only for |ν| ≤ 1/2, and so now the recurrence (6.7.5) is used to evaluate fν at a value ν = µ in this interval. Then one calculates Jµ from Jµ =
W Yµ0 − Yµ fµ
(6.7.13)
and Jµ0 from (6.7.8). The values at the original value of ν are determined by scaling as before, and the Y ’s are recurred up as before. Temme’s series are Yν = −
∞ X
ck gk
Yν+1 = −
k=0
∞ 2X ck hk x
(6.7.14)
k=0
Here ck =
(−x2 /4)k k!
(6.7.15)
while the coefficients gk and hk are defined in terms of quantities pk , qk , and fk that can be found by recursion: νπ 2 gk = fk + sin2 qk ν 2 hk = −kgk + pk pk−1 k−ν qk−1 qk = k+ν kfk−1 + pk−1 + qk−1 fk = k2 − ν 2
pk =
(6.7.16)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
p − fµ q
236
Chapter 6.
Special Functions
(6.7.17)
(6.7.18)
The whole point of writing the formulas in this way is that the potential problems as ν → 0 can be controlled by evaluating νπ/ sin νπ, sinh σ/σ, and Γ1 carefully. In particular, Temme gives Chebyshev expansions for Γ1 (ν) and Γ2 (ν). We have rearranged his expansion for Γ1 to be explicitly an even series in ν so that we can use our routine chebev as explained in §5.8. The routine assumes ν ≥ 0. For negative ν you can use the reflection formulas J−ν = cos νπ Jν − sin νπ Yν Y−ν = sin νπ Jν + cos νπ Yν
(6.7.19)
The routine also assumes x > 0. For x < 0 the functions are in general complex, but expressible in terms of functions with x > 0. For x = 0, Yν is singular. Internal arithmetic in the routine is carried out in double precision. To maintain portability, complex arithmetic has been recoded with real variables.
* C
* * *
SUBROUTINE bessjy(x,xnu,rj,ry,rjp,ryp) INTEGER MAXIT REAL rj,rjp,ry,ryp,x,xnu,XMIN DOUBLE PRECISION EPS,FPMIN,PI PARAMETER (EPS=1.e-10,FPMIN=1.e-30,MAXIT=10000,XMIN=2., PI=3.141592653589793d0) USES beschb Returns the Bessel functions rj = Jν , ry = Yν and their derivatives rjp = Jν0 , ryp = Yν0 , for positive x and for xnu = ν ≥ 0. The relative accuracy is within one or two significant digits of EPS, except near a zero of one of the functions, where EPS controls its absolute accuracy. FPMIN is a number close to the machine’s smallest floating-point number. All internal arithmetic is in double precision. To convert the entire routine to double precision, change the REAL declaration above and decrease EPS to 10−16. Also convert the subroutine beschb. INTEGER i,isign,l,nl DOUBLE PRECISION a,b,br,bi,c,cr,ci,d,del,del1,den,di,dlr,dli, dr,e,f,fact,fact2,fact3,ff,gam,gam1,gam2,gammi,gampl,h, p,pimu,pimu2,q,r,rjl,rjl1,rjmu,rjp1,rjpl,rjtemp,ry1, rymu,rymup,rytemp,sum,sum1,temp,w,x2,xi,xi2,xmu,xmu2 if(x.le.0..or.xnu.lt.0.) pause ’bad arguments in bessjy’ if(x.lt.XMIN)then nl is the number of downward recurrences of the J’s and nl=int(xnu+.5d0) upward recurrences of Y ’s. xmu lies between −1/2 and else 1/2 for x < XMIN, while it is chosen so that x is greater nl=max(0,int(xnu-x+1.5d0)) than the turning point for x ≥ XMIN. endif xmu=xnu-nl xmu2=xmu*xmu xi=1.d0/x xi2=2.d0*xi w=xi2/PI The Wronskian.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The initial values for the recurrences are 1 x −ν p0 = Γ(1 + ν) π 2 1 x ν q0 = Γ(1 − ν) π 2 2 2 νπ sinh σ cosh σΓ1 (ν) + ln Γ2 (ν) f0 = π sin νπ σ x with 2 σ = ν ln x 1 1 1 Γ1 (ν) = − 2ν Γ(1 − ν) Γ(1 + ν) 1 1 1 Γ2 (ν) = + 2 Γ(1 − ν) Γ(1 + ν)
6.7 Bessel Functions of Fractional Order
isign=1 Evaluate CF1 by modified Lentz’s method (§5.2). isign keeps h=xnu*xi track of sign changes in the denominator. if(h.lt.FPMIN)h=FPMIN b=xi2*xnu d=0.d0 c=h do 11 i=1,MAXIT b=b+xi2 d=b-d if(abs(d).lt.FPMIN)d=FPMIN c=b-1.d0/c if(abs(c).lt.FPMIN)c=FPMIN d=1.d0/d del=c*d h=del*h if(d.lt.0.d0)isign=-isign if(abs(del-1.d0).lt.EPS)goto 1 enddo 11 pause ’x too large in bessjy; try asymptotic expansion’ continue rjl=isign*FPMIN Initialize Jν and Jν0 for downward recurrence. rjpl=h*rjl rjl1=rjl Store values for later rescaling. rjp1=rjpl fact=xnu*xi do 12 l=nl,1,-1 rjtemp=fact*rjl+rjpl fact=fact-xi rjpl=fact*rjtemp-rjl rjl=rjtemp enddo 12 if(rjl.eq.0.d0)rjl=EPS f=rjpl/rjl Now have unnormalized Jµ and Jµ0 . if(x.lt.XMIN) then Use series. x2=.5d0*x pimu=PI*xmu if(abs(pimu).lt.EPS)then fact=1.d0 else fact=pimu/sin(pimu) endif d=-log(x2) e=xmu*d if(abs(e).lt.EPS)then fact2=1.d0 else fact2=sinh(e)/e endif call beschb(xmu,gam1,gam2,gampl,gammi) Chebyshev evaluation of Γ1 and Γ2 . ff=2.d0/PI*fact*(gam1*cosh(e)+gam2*fact2*d) f0 . e=exp(e) p=e/(gampl*PI) p0 . q=1.d0/(e*PI*gammi) q0 . pimu2=0.5d0*pimu if(abs(pimu2).lt.EPS)then fact3=1.d0 else fact3=sin(pimu2)/pimu2 endif r=PI*pimu2*fact3*fact3 c=1.d0 d=-x2*x2 sum=ff+r*q sum1=p
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
237
238
3
Special Functions
do 13 i=1,MAXIT ff=(i*ff+p+q)/(i*i-xmu2) c=c*d/i p=p/(i-xmu) q=q/(i+xmu) del=c*(ff+r*q) sum=sum+del del1=c*p-i*del sum1=sum1+del1 if(abs(del).lt.(1.d0+abs(sum))*EPS)goto 2 enddo 13 pause ’bessy series failed to converge’ continue rymu=-sum ry1=-sum1*xi2 rymup=xmu*xi*rymu-ry1 rjmu=w/(rymup-f*rymu) Equation (6.7.13). else Evaluate CF2 by modified Lentz’s method a=.25d0-xmu2 (§5.2). p=-.5d0*xi q=1.d0 br=2.d0*x bi=2.d0 fact=a*xi/(p*p+q*q) cr=br+q*fact ci=bi+p*fact den=br*br+bi*bi dr=br/den di=-bi/den dlr=cr*dr-ci*di dli=cr*di+ci*dr temp=p*dlr-q*dli q=p*dli+q*dlr p=temp do 14 i=2,MAXIT a=a+2*(i-1) bi=bi+2.d0 dr=a*dr+br di=a*di+bi if(abs(dr)+abs(di).lt.FPMIN)dr=FPMIN fact=a/(cr*cr+ci*ci) cr=br+cr*fact ci=bi-ci*fact if(abs(cr)+abs(ci).lt.FPMIN)cr=FPMIN den=dr*dr+di*di dr=dr/den di=-di/den dlr=cr*dr-ci*di dli=cr*di+ci*dr temp=p*dlr-q*dli q=p*dli+q*dlr p=temp if(abs(dlr-1.d0)+abs(dli).lt.EPS)goto 3 enddo 14 pause ’cf2 failed in bessjy’ continue gam=(p-f)/q Equations (6.7.6) – (6.7.10). rjmu=sqrt(w/((p-f)*gam+q)) rjmu=sign(rjmu,rjl) rymu=rjmu*gam rymup=rymu*(p+q/gam) ry1=xmu*xi*rymu-rymup endif fact=rjmu/rjl
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
Chapter 6.
6.7 Bessel Functions of Fractional Order
239
C
* * * *
SUBROUTINE beschb(x,gam1,gam2,gampl,gammi) INTEGER NUSE1,NUSE2 DOUBLE PRECISION gam1,gam2,gammi,gampl,x PARAMETER (NUSE1=5,NUSE2=5) USES chebev Evaluates Γ1 and Γ2 by Chebyshev expansion for |x| ≤ 1/2. Also returns 1/Γ(1 + x) and 1/Γ(1 − x). If converting to double precision, set NUSE1 = 7, NUSE2 = 8. REAL xx,c1(7),c2(8),chebev SAVE c1,c2 DATA c1/-1.142022680371168d0,6.5165112670737d-3, 3.087090173086d-4,-3.4706269649d-6,6.9437664d-9, 3.67795d-11,-1.356d-13/ DATA c2/1.843740587300905d0,-7.68528408447867d-2, 1.2719271366546d-3,-4.9717367042d-6,-3.31261198d-8, 2.423096d-10,-1.702d-13,-1.49d-15/ xx=8.d0*x*x-1.d0 Multiply x by 2 to make range be −1 to 1, and then gam1=chebev(-1.,1.,c1,NUSE1,xx) apply transformation for evaluating even Chebygam2=chebev(-1.,1.,c2,NUSE2,xx) shev series. gampl=gam2-x*gam1 gammi=gam2+x*gam1 return END
Modified Bessel Functions Steed’s method does not work for modified Bessel functions because in this case CF2 is purely imaginary and we have only three relations among the four functions. Temme [3] has given a normalization condition that provides the fourth relation. The Wronskian relation is 1 W ≡ Iν Kν0 − Kν Iν0 = − (6.7.20) x The continued fraction CF1 becomes fν ≡
1 1 Iν0 ν ··· = + Iν x 2(ν + 1)/x + 2(ν + 2)/x +
(6.7.21)
To get CF2 and the normalization condition in a convenient form, consider the sequence of confluent hypergeometric functions
for fixed ν.
zn (x) = U (ν + 1/2 + n, 2ν + 1, 2x)
(6.7.22)
Kν (x) = π1/2 (2x)ν e−x z0 (x) Kν+1 (x) 1 1 1 z1 = ν + + x + ν2 − Kν (x) x 2 4 z0
(6.7.23)
Then
(6.7.24)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
rj=rjl1*fact Scale original Jν and Jν0 . rjp=rjp1*fact do 15 i=1,nl Upward recurrence of Yν . rytemp=(xmu+i)*xi2*ry1-rymu rymu=ry1 ry1=rytemp enddo 15 ry=rymu ryp=xnu*xi*rymu-ry1 return END
240
Chapter 6.
Special Functions
Equation (6.7.23) is the standard expression for Kν in terms of a confluent hypergeometric function, while equation (6.7.24) follows from relations between contiguous confluent hypergeometric functions (equations 13.4.16 and 13.4.18 in Abramowitz and Stegun). Now the functions zn satisfy the three-term recurrence relation (equation 13.4.15 in Abramowitz and Stegun) zn−1 (x) = bn zn (x) + an+1 zn+1
(6.7.25)
bn = 2(n + x) an+1 = −[(n + 1/2)2 − ν 2 ]
(6.7.26)
Following the steps leading to equation (5.5.18), we get the continued fraction CF2 z1 a2 1 ··· = z0 b1 + b2 + from which (6.7.24) gives Kν+1 /Kν and thus Kν0 /Kν . Temme’s normalization condition is that ν+1/2 ∞ X 1 Cn zn = 2x n=0 where (−1)n Γ(ν + 1/2 + n) Cn = n! Γ(ν + 1/2 − n)
(6.7.27)
(6.7.28)
(6.7.29)
Note that the Cn ’s can be determined by recursion: Cn+1 = −
C0 = 1,
an+1 Cn n+1
(6.7.30)
We use the condition (6.7.28) by finding S=
∞ X
Cn
n=1
Then
z0 =
1 2x
zn z0
ν+1/2
1 1+S
(6.7.31)
(6.7.32)
and (6.7.23) gives Kν . Thompson and Barnett [4] have given a clever method of doing the sum (6.7.31) simultaneously with the forward evaluation of the continued fraction CF2. Suppose the continued fraction is being evaluated as ∞ X z1 = ∆hn z0 n=0
(6.7.33)
where the increments ∆hn are being found by, e.g., Steed’s algorithm or the modified Lentz’s algorithm of §5.2. Then the approximation to S keeping the first N terms can be found as SN =
N X
Qn ∆hn
(6.7.34)
n=1
Here Qn =
n X
Ck qk
(6.7.35)
k=1
and qk is found by recursion from qk+1 = (qk−1 − bk qk )/ak+1
(6.7.36)
starting with q0 = 0, q1 = 1. For the case at hand, approximately three times as many terms are needed to get S to converge as are needed simply for CF2 to converge.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
with
6.7 Bessel Functions of Fractional Order
241
To find Kν and Kν+1 for small x we use series analogous to (6.7.14): Kν =
∞ X
ck fk
Kν+1 =
k=0
∞ 2X ck hk x
(6.7.37)
k=0
Here
pk−1 k−ν qk−1 qk = k+ν kfk−1 + pk−1 + qk−1 fk = k2 − ν 2
pk =
The initial values for the recurrences are 1 x −ν Γ(1 + ν) p0 = 2 2 ν 1 x q0 = Γ(1 − ν) 2 2 2 νπ sinh σ ln cosh σΓ1 (ν) + Γ2 (ν) f0 = sin νπ σ x
(6.7.38)
(6.7.39)
Both the series for small x, and CF2 and the normalization relation (6.7.28) require |ν| ≤ 1/2. In both cases, therefore, we recurse Iν down to a value ν = µ in this interval, find Kµ there, and recurse Kν back up to the original value of ν. The routine assumes ν ≥ 0. For negative ν use the reflection formulas I−ν = Iν +
2 sin(νπ) Kν π
(6.7.40)
K−ν = Kν Note that for large x, Iν ∼ ex , Kν ∼ e−x , and so these functions will overflow or underflow. It is often desirable to be able to compute the scaled quantities e−x Iν and ex Kν . Simply omitting the factor e−x in equation (6.7.23) will ensure that all four quantities will have the appropriate scaling. If you also want to scale the four quantities for small x when the series in equation (6.7.37) are used, you must multiply each series by ex .
* C
* * *
SUBROUTINE bessik(x,xnu,ri,rk,rip,rkp) INTEGER MAXIT REAL ri,rip,rk,rkp,x,xnu,XMIN DOUBLE PRECISION EPS,FPMIN,PI PARAMETER (EPS=1.e-10,FPMIN=1.e-30,MAXIT=10000,XMIN=2., PI=3.141592653589793d0) USES beschb Returns the modified Bessel functions ri = Iν , rk = Kν and their derivatives rip = Iν0 , rkp = Kν0 , for positive x and for xnu = ν ≥ 0. The relative accuracy is within one or two significant digits of EPS. FPMIN is a number close to the machine’s smallest floatingpoint number. All internal arithmetic is in double precision. To convert the entire routine to double precision, change the REAL declaration above and decrease EPS to 10−16 . Also convert the subroutine beschb. INTEGER i,l,nl DOUBLE PRECISION a,a1,b,c,d,del,del1,delh,dels,e,f,fact, fact2,ff,gam1,gam2,gammi,gampl,h,p,pimu,q,q1,q2, qnew,ril,ril1,rimu,rip1,ripl,ritemp,rk1,rkmu,rkmup, rktemp,s,sum,sum1,x2,xi,xi2,xmu,xmu2 if(x.le.0..or.xnu.lt.0.) pause ’bad arguments in bessik’
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
(x2 /4)k k! hk = −kfk + pk ck =
242
Special Functions
nl=int(xnu+.5d0) xmu=xnu-nl xmu2=xmu*xmu xi=1.d0/x xi2=2.d0*xi h=xnu*xi if(h.lt.FPMIN)h=FPMIN b=xi2*xnu d=0.d0 c=h do 11 i=1,MAXIT b=b+xi2 d=1.d0/(b+d) c=b+1.d0/c del=c*d h=del*h if(abs(del-1.d0).lt.EPS)goto 1 enddo 11 pause ’x too large in bessik; try asymptotic continue ril=FPMIN ripl=h*ril ril1=ril rip1=ripl fact=xnu*xi do 12 l=nl,1,-1 ritemp=fact*ril+ripl fact=fact-xi ripl=fact*ritemp+ril ril=ritemp enddo 12 f=ripl/ril if(x.lt.XMIN) then x2=.5d0*x pimu=PI*xmu if(abs(pimu).lt.EPS)then fact=1.d0 else fact=pimu/sin(pimu) endif d=-log(x2) e=xmu*d if(abs(e).lt.EPS)then fact2=1.d0 else fact2=sinh(e)/e endif call beschb(xmu,gam1,gam2,gampl,gammi) ff=fact*(gam1*cosh(e)+gam2*fact2*d) sum=ff e=exp(e) p=0.5d0*e/gampl q=0.5d0/(e*gammi) c=1.d0 d=x2*x2 sum1=p do 13 i=1,MAXIT ff=(i*ff+p+q)/(i*i-xmu2) c=c*d/i p=p/(i-xmu) q=q/(i+xmu) del=c*ff sum=sum+del del1=c*(p-i*ff)
nl is the number of downward recurrences of the I’s and upward recurrences of K’s. xmu lies between −1/2 and 1/2. Evaluate CF1 by modified Lentz’s method (§5.2).
Denominators cannot be zero here, so no need for special precautions.
expansion’ Initialize Iν and Iν0 for downward recurrence. Store values for later rescaling.
Now have unnormalized Iµ and Iµ0 . Use series.
Chebyshev evaluation of Γ1 and Γ2 . f0 . p0 . q0 .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 6.
6.7 Bessel Functions of Fractional Order
2
sum1=sum1+del1 if(abs(del).lt.abs(sum)*EPS)goto 2 enddo 13 pause ’bessk series failed to converge’ continue rkmu=sum rk1=sum1*xi2 else Evaluate CF2 by Steed’s algorithm (§5.2), b=2.d0*(1.d0+x) which is OK because there can be no d=1.d0/b zero denominators. delh=d h=delh q1=0.d0 Initializations for recurrence (6.7.35). q2=1.d0 a1=.25d0-xmu2 c=a1 q=c First term in equation (6.7.34). a=-a1 s=1.d0+q*delh do 14 i=2,MAXIT a=a-2*(i-1) c=-a*c/i qnew=(q1-b*q2)/a q1=q2 q2=qnew q=q+c*qnew b=b+2.d0 d=1.d0/(b+a*d) delh=(b*d-1.d0)*delh h=h+delh dels=q*delh s=s+dels if(abs(dels/s).lt.EPS)goto 3 Need only test convergence of sum since enddo 14 CF2 itself converges more quickly. pause ’bessik: failure to converge in cf2’ continue h=a1*h rkmu=sqrt(PI/(2.d0*x))*exp(-x)/s Omit the factor exp(−x) to scale all the rk1=rkmu*(xmu+x+.5d0-h)*xi returned functions by exp(x) for x ≥ endif XMIN. rkmup=xmu*xi*rkmu-rk1 rimu=xi/(f*rkmu-rkmup) Get Iµ from Wronskian. ri=(rimu*ril1)/ril Scale original Iν and Iν0 . rip=(rimu*rip1)/ril do 15 i=1,nl Upward recurrence of Kν . rktemp=(xmu+i)*xi2*rk1+rkmu rkmu=rk1 rk1=rktemp enddo 15 rk=rkmu rkp=xnu*xi*rkmu-rk1 return END
Airy Functions For positive x, the Airy functions are defined by r 1 x Ai(x) = K1/3 (z) π 3
(6.7.41)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
3
243
244
Chapter 6. r Bi(x) =
Special Functions
x [I1/3 (z) + I−1/3 (z)] 3
(6.7.42)
where
2 3/2 x (6.7.43) 3 By using the reflection formula (6.7.40), we can convert (6.7.42) into the computationally more useful form √ 2 1 Bi(x) = x √ I1/3 (z) + K1/3 (z) (6.7.44) π 3 z=
The corresponding formulas for negative arguments are √ x 1 Ai(−x) = J1/3 (z) − √ Y1/3 (z) 2 3 √ x 1 √ J1/3 (z) + Y1/3 (z) Bi(−x) = − 2 3 x 1 Ai0 (−x) = J2/3 (z) + √ Y2/3 (z) 2 3 x 1 0 √ J2/3 (z) − Y2/3 (z) Bi (−x) = 2 3
C
* *
SUBROUTINE airy(x,ai,bi,aip,bip) REAL ai,aip,bi,bip,x USES bessik,bessjy Returns Airy functions Ai(x), Bi(x), and their derivatives Ai0 (x), Bi0 (x). REAL absx,ri,rip,rj,rjp,rk,rkp,rootx,ry,ryp,z, PI,THIRD,TWOTHR,ONOVRT PARAMETER (PI=3.1415927,THIRD=1./3.,TWOTHR=2.*THIRD, ONOVRT=.57735027) absx=abs(x) rootx=sqrt(absx) z=TWOTHR*absx*rootx if(x.gt.0.)then call bessik(z,THIRD,ri,rk,rip,rkp) ai=rootx*ONOVRT*rk/PI bi=rootx*(rk/PI+2.*ONOVRT*ri) call bessik(z,TWOTHR,ri,rk,rip,rkp) aip=-x*ONOVRT*rk/PI bip=x*(rk/PI+2.*ONOVRT*ri) else if(x.lt.0.)then call bessjy(z,THIRD,rj,ry,rjp,ryp) ai=.5*rootx*(rj-ONOVRT*ry) bi=-.5*rootx*(ry+ONOVRT*rj) call bessjy(z,TWOTHR,rj,ry,rjp,ryp) aip=.5*absx*(ONOVRT*ry+rj) bip=.5*absx*(ONOVRT*rj-ry) else Case x = 0. ai=.35502805 bi=ai/ONOVRT
(6.7.46)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
so that Ai and Bi can be evaluated with a single call to bessik. The derivatives should not be evaluated by simply differentiating the above expressions because of possible subtraction errors near x = 0. Instead, use the equivalent expressions x Ai0 (x) = − √ K2/3 (z) π 3 (6.7.45) 2 1 0 Bi (x) = x √ I2/3 (z) + K2/3 (z) π 3
6.7 Bessel Functions of Fractional Order
245
aip=-.25881940 bip=-aip/ONOVRT endif return END
For integer n, spherical Bessel functions are defined by r π jn (x) = Jn+(1/2) (x) 2x r π yn (x) = Yn+(1/2) (x) 2x
(6.7.47)
They can be evaluated by a call to bessjy, and the derivatives can safely be found from the derivatives of equation (6.7.47). Note that in the continued fraction CF2 in (6.7.3) just the first term survives for ν = 1/2. Thus one can make a very simple algorithm for spherical Bessel functions along the lines of bessjy by always recursing jn down to n = 0, setting p and q from the first term in CF2, and then recursing yn up. No special series is required near x = 0. However, bessjy is already so efficient that we have not bothered to provide an independent routine for spherical Bessels.
C
SUBROUTINE sphbes(n,x,sj,sy,sjp,syp) INTEGER n REAL sj,sjp,sy,syp,x USES bessjy 0 (x), y0 (x) for Returns spherical Bessel functions jn (x), yn (x), and their derivatives jn n integer n. REAL factor,order,rj,rjp,ry,ryp,RTPIO2 PARAMETER (RTPIO2=1.2533141) if(n.lt.0.or.x.le.0.)pause ’bad arguments in sphbes’ order=n+0.5 call bessjy(x,order,rj,ry,rjp,ryp) factor=RTPIO2/sqrt(x) sj=factor*rj sy=factor*ry sjp=factor*rjp-sj/(2.*x) syp=factor*ryp-sy/(2.*x) return END
CITED REFERENCES AND FURTHER READING: Barnett, A.R., Feng, D.H., Steed, J.W., and Goldfarb, L.J.B. 1974, Computer Physics Communications, vol. 8, pp. 377–395. [1] Temme, N.M. 1976, Journal of Computational Physics, vol. 21, pp. 343–350 [2]; 1975, op. cit., vol. 19, pp. 324–337. [3] Thompson, I.J., and Barnett, A.R. 1987, Computer Physics Communications, vol. 47, pp. 245– 257. [4] Barnett, A.R. 1981, Computer Physics Communications, vol. 21, pp. 297–314. Thompson, I.J., and Barnett, A.R. 1986, Journal of Computational Physics, vol. 64, pp. 490–509. Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), Chapter 10.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Spherical Bessel Functions
246
Chapter 6.
Special Functions
6.8 Spherical Harmonics
−1
0
Here asterisk denotes complex conjugation. Mathematically, the spherical harmonics are related to associated Legendre polynomials by the equation s 2l + 1 (l − m)! m P (cos θ)eimφ Ylm (θ, φ) = (6.8.2) 4π (l + m)! l By using the relation Yl,−m (θ, φ) = (−1)m Ylm *(θ, φ)
(6.8.3)
we can always relate a spherical harmonic to an associated Legendre polynomial with m ≥ 0. With x ≡ cos θ, these are defined in terms of the ordinary Legendre polynomials (cf. §4.5 and §5.5) by dm Pl (x) (6.8.4) dxm The first few associated Legendre polynomials, and their corresponding normalized spherical harmonics, are q 1 P00 (x) = 1 Y00 = 4π q 3 P11 (x) = − (1 − x2 )1/2 Y11 = − 8π sin θeiφ q 3 P10 (x) = x Y10 = cos θ 4π q 15 P22 (x) = 3 (1 − x2 ) Y22 = 14 2π sin2 θe2iφ q 15 P21 (x) = −3 (1 − x2 )1/2 x Y21 = − 8π sin θ cos θeiφ q 5 3 1 2 P20 (x) = 12 (3x2 − 1) Y20 = 4π ( 2 cos θ − 2 ) (6.8.5) Plm (x) = (−1)m (1 − x2 )m/2
There are many bad ways to evaluate associated Legendre polynomials numerically. For example, there are explicit expressions, such as (l − m)(m + l + 1) 1 − x (−1)m (l + m)! m 2 m/2 (1 − x ) 1− Pl (x) = m 2 m!(l − m)! 1!(m + 1) 2 # 2 (l − m)(l − m − 1)(m + l + 1)(m + l + 2) 1 − x −··· + 2!(m + 1)(m + 2) 2 (6.8.6)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Spherical harmonics occur in a large variety of physical problems, for example, whenever a wave equation, or Laplace’s equation, is solved by separation of variables in spherical coordinates. The spherical harmonic Ylm (θ, φ), −l ≤ m ≤ l, is a function of the two coordinates θ, φ on the surface of a sphere. The spherical harmonics are orthogonal for different l and m, and they are normalized so that their integrated square over the sphere is unity: Z 2π Z 1 dφ d(cos θ)Yl0 m0 *(θ, φ)Ylm (θ, φ) = δl0 l δm0 m (6.8.1)
6.8 Spherical Harmonics
247
m m − (l + m − 1)Pl−2 (l − m)Plm = x(2l − 1)Pl−1
(6.8.7)
It is useful because there is a closed-form expression for the starting value, m = (−1)m (2m − 1)!!(1 − x2 )m/2 Pm
(6.8.8)
(The notation n!! denotes the product of all odd integers less than or equal to n.) m Using (6.8.7) with l = m + 1, and setting Pm−1 = 0, we find m m = x(2m + 1)Pm Pm+1
(6.8.9)
Equations (6.8.8) and (6.8.9) provide the two starting values required for (6.8.7) for general l. The function that implements this is FUNCTION plgndr(l,m,x) INTEGER l,m REAL plgndr,x Computes the associated Legendre polynomial Plm (x). Here m and l are integers satisfying 0 ≤ m ≤ l, while x lies in the range −1 ≤ x ≤ 1. INTEGER i,ll REAL fact,pll,pmm,pmmp1,somx2 if(m.lt.0.or.m.gt.l.or.abs(x).gt.1.)pause ’bad arguments in plgndr’ m. pmm=1. Compute Pm if(m.gt.0) then somx2=sqrt((1.-x)*(1.+x)) fact=1. do 11 i=1,m pmm=-pmm*fact*somx2 fact=fact+2. enddo 11 endif if(l.eq.m) then plgndr=pmm else m . pmmp1=x*(2*m+1)*pmm Compute Pm+1 if(l.eq.m+1) then plgndr=pmmp1 else Compute Plm , l > m + 1. do 12 ll=m+2,l
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where the polynomial continues up through the term in (1 − x)l−m . (See [1] for this and related formulas.) This is not a satisfactory method because evaluation of the polynomial involves delicate cancellations between successive terms, which alternate in sign. For large l, the individual terms in the polynomial become very much larger than their sum, and all accuracy is lost. In practice, (6.8.6) can be used only in single precision (32-bit) for l up to 6 or 8, and in double precision (64-bit) for l up to 15 or 18, depending on the precision required for the answer. A more robust computational procedure is therefore desirable, as follows: The associated Legendre functions satisfy numerous recurrence relations, tabulated in [1-2] . These are recurrences on l alone, on m alone, and on both l and m simultaneously. Most of the recurrences involving m are unstable, and so dangerous for numerical work. The following recurrence on l is, however, stable (compare 5.5.1):
248
Chapter 6.
Special Functions
CITED REFERENCES AND FURTHER READING: Magnus, W., and Oberhettinger, F. 1949, Formulas and Theorems for the Functions of Mathematical Physics (New York: Chelsea), pp. 54ff. [1] Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), Chapter 8. [2]
6.9 Fresnel Integrals, Cosine and Sine Integrals Fresnel Integrals The two Fresnel integrals are defined by Z
x
cos
C(x) = 0
π t2 dt, 2
Z S(x) =
x
sin 0
π t2 dt 2
(6.9.1)
The most convenient way of evaluating these functions to arbitrary precision is to use power series for small x and a continued fraction for large x. The series are π 4 x 9 π 2 x 5 + −··· C(x) = x − 2 5 · 2! 2 9 · 4! π 3 x 7 π 5 x11 π x3 − + −··· S(x) = 2 3 · 1! 2 7 · 3! 2 11 · 5!
(6.9.2)
There is a complex continued fraction that yields both S(x) and C(x) simultaneously: √ π z= (1 − i)x 2
(6.9.3)
2 1 1 1/2 1 3/2 2 ··· ez erfc z = √ π z+ z+ z+ z+ z+ 2z 1·2 3·4 1 · · · = √ π 2z 2 + 1 − 2z 2 + 5 − 2z 2 + 9 −
(6.9.4)
1+i erf z, C(x) + iS(x) = 2 where
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
pll=(x*(2*ll-1)*pmmp1-(ll+m-1)*pmm)/(ll-m) pmm=pmmp1 pmmp1=pll enddo 12 plgndr=pll endif endif return END
6.9 Fresnel Integrals, Cosine and Sine Integrals
249
In the last line we have converted the “standard” form of the continued fraction to its “even” form (see §5.2), which converges twice as fast. We must be careful not to evaluate the alternating series (6.9.2) at too large a value of x; inspection of the terms shows that x = 1.5 is a good point to switch over to the continued fraction. Note that for large x π 1 1 + sin x2 , 2 πx 2
S(x) ∼
π 1 1 − cos x2 2 πx 2
(6.9.5)
Thus the precision of the routine frenel may be limited by the precision of the library routines for sine and cosine for large x.
*
1
SUBROUTINE frenel(x,s,c) INTEGER MAXIT REAL c,s,x,EPS,FPMIN,PI,PIBY2,XMIN PARAMETER (EPS=6.e-8,MAXIT=100,FPMIN=1.e-30,XMIN=1.5, PI=3.1415927,PIBY2=1.5707963) Computes the Fresnel integrals S(x) and C(x) for all real x. Parameters: EPS is the relative error; MAXIT is the maximum number of iterations allowed; FPMIN is a number near the smallest representable floating-point number; XMIN is the dividing line between using the series and continued fraction; PI = π; PIBY2 = π/2. INTEGER k,n REAL a,absc,ax,fact,pix2,sign,sum,sumc,sums,term,test COMPLEX b,cc,d,h,del,cs LOGICAL odd absc(h)=abs(real(h))+abs(aimag(h)) Statement function. ax=abs(x) if(ax.lt.sqrt(FPMIN))then Special case: avoid failure of convergence test s=0. because of underflow. c=ax else if(ax.le.XMIN)then Evaluate both series simultaneously. sum=0. sums=0. sumc=ax sign=1. fact=PIBY2*ax*ax odd=.true. term=ax n=3 do 11 k=1,MAXIT term=term*fact/k sum=sum+sign*term/n test=abs(sum)*EPS if(odd)then sign=-sign sums=sum sum=sumc else sumc=sum sum=sums endif if(term.lt.test)goto 1 odd=.not.odd n=n+2 enddo 11 pause ’series failed in frenel’ s=sums c=sumc else Evaluate continued fraction by modified Lentz’s pix2=PI*ax*ax method (§5.2). b=cmplx(1.,-pix2)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C(x) ∼
250
Special Functions
cc=1./FPMIN d=1./b h=d n=-1 do 12 k=2,MAXIT n=n+2 a=-n*(n+1) b=b+4. d=1./(a*d+b) Denominators cannot be zero. cc=b+a/cc del=cc*d h=h*del if(absc(del-1.).lt.EPS)goto 2 enddo 12 pause ’cf failed in frenel’ h=h*cmplx(ax,-ax) cs=cmplx(.5,.5)*(1.-cmplx(cos(.5*pix2),sin(.5*pix2))*h) c=real(cs) s=aimag(cs) endif if(x.lt.0.)then Use antisymmetry. c=-c s=-s endif return END
Cosine and Sine Integrals The cosine and sine integrals are defined by Z x cos t − 1 dt Ci(x) = γ + ln x + t 0 Z x sin t Si(x) = dt t 0
(6.9.6)
Here γ ≈ 0.5772 . . . is Euler’s constant. We only need a way to calculate the functions for x > 0, because Si(−x) = − Si(x),
Ci(−x) = Ci(x) − iπ
(6.9.7)
Once again we can evaluate these functions by a judicious combination of power series and complex continued fraction. The series are x5 x3 + −··· 3 · 3! 5 · 5! x4 x2 + −··· Ci(x) = γ + ln x + − 2 · 2! 4 · 4! Si(x) = x −
(6.9.8)
The continued fraction for the exponential integral E1 (ix) is E1 (ix) = − Ci(x) + i[Si(x) − π/2] 1 1 2 2 1 −ix ··· =e ix + 1 + ix + 1 + ix + 12 22 1 ··· = e−ix 1 + ix − 3 + ix − 5 + ix −
(6.9.9)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
Chapter 6.
6.9 Fresnel Integrals, Cosine and Sine Integrals
251
The “even” form of the continued fraction is given in the last line and converges twice as fast for about the same amount of computation. A good crossover point from the alternating series to the continued fraction is x = 2 in this case. As for the Fresnel integrals, for large x the precision may be limited by the precision of the sine and cosine routines.
1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
SUBROUTINE cisi(x,ci,si) INTEGER MAXIT REAL ci,si,x,EPS,EULER,PIBY2,FPMIN,TMIN PARAMETER (EPS=6.e-8,EULER=.57721566,MAXIT=100,PIBY2=1.5707963, FPMIN=1.e-30,TMIN=2.) Computes the cosine and sine integrals Ci(x) and Si(x). Ci(0) is returned as a large negative number and no error message is generated. For x < 0 the routine returns Ci(−x) and you must supply the −iπ yourself. Parameters: EPS is the relative error, or absolute error near a zero of Ci(x); EULER = γ; MAXIT is the maximum number of iterations allowed; PIBY2 = π/2; FPMIN is a number near the smallest representable floating-point number; TMIN is the dividing line between using the series and continued fraction. INTEGER i,k REAL a,err,fact,sign,sum,sumc,sums,t,term,absc COMPLEX h,b,c,d,del LOGICAL odd absc(h)=abs(real(h))+abs(aimag(h)) Statement function. t=abs(x) if(t.eq.0.)then Special case. si=0. ci=-1./FPMIN return endif if(t.gt.TMIN)then Evaluate continued fraction by modified Lentz’s b=cmplx(1.,t) method (§5.2). c=1./FPMIN d=1./b h=d do 11 i=2,MAXIT a=-(i-1)**2 b=b+2. d=1./(a*d+b) Denominators cannot be zero. c=b+a/c del=c*d h=h*del if(absc(del-1.).lt.EPS)goto 1 enddo 11 pause ’cf failed in cisi’ continue h=cmplx(cos(t),-sin(t))*h ci=-real(h) si=PIBY2+aimag(h) else Evaluate both series simultaneously. if(t.lt.sqrt(FPMIN))then Special case: avoid failure of convergence test sumc=0. because of underflow. sums=t else sum=0. sums=0. sumc=0. sign=1. fact=1. odd=.true. do 12 k=1,MAXIT fact=fact*t/k term=fact/k
252
Special Functions
sum=sum+sign*term err=term/abs(sum) if(odd)then sign=-sign sums=sum sum=sumc else sumc=sum sum=sums endif if(err.lt.EPS)goto 2 odd=.not.odd enddo 12 pause ’maxits exceeded in cisi’ endif si=sums ci=sumc+log(t)+EULER endif if(x.lt.0.)si=-si return END
CITED REFERENCES AND FURTHER READING: Stegun, I.A., and Zucker, R. 1976, Journal of Research of the National Bureau of Standards, vol. 80B, pp. 291–311; 1981, op. cit., vol. 86, pp. 661–686. Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), Chapters 5 and 7.
6.10 Dawson’s Integral Dawson’s Integral F (x) is defined by −x2
Z
x
2
et dt
F (x) = e
(6.10.1)
0
The function can also be related to the complex error function by F (z) =
√ i π −z2 e [1 − erfc(−iz)] . 2
(6.10.2)
A remarkable approximation for F (x), due to Rybicki [1], is 2 1 X e−(z−nh) F (z) = lim √ h→0 π n
(6.10.3)
n odd
What makes equation (6.10.3) unusual is that its accuracy increases exponentially as h gets small, so that quite moderate values of h (and correspondingly quite rapid convergence of the series) give very accurate approximations.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
Chapter 6.
253
6.10 Dawson’s Integral
0 0 2 N 1 X e−(x −n h) F (x) ≈ √ , π n0 =−N n0 + n0
(6.10.4)
n0 odd
where the approximate equality is accurate when h is sufficiently small and N is sufficiently large. The computation of this formula can be greatly speeded up if we note that 0
0
02
0
e−(x −n h) = e−x e−(n h) 2
2
0
e2x h
n0 .
(6.10.5)
The first factor is computed once, the second is an array of constants to be stored, and the third can be computed recursively, so that only two exponentials need be 0 2 evaluated. Advantage is also taken of the symmetry of the coefficients e−(n h) by breaking the summation up into positive and negative values of n0 separately. In the following routine, the choices h = 0.4 and N = 11 are made. Because of the symmetry of the summations and the restriction to odd values of n, the limits on the do loops are 1 to 6. The accuracy of the result in this REAL version is about 2 × 10−7 . In order to maintain relative accuracy near x = 0, where F (x) vanishes, the program branches to the evaluation of the power series [2] for F (x), for |x| < 0.2. FUNCTION dawson(x) INTEGER NMAX REAL dawson,x,H,A1,A2,A3 PARAMETER (NMAX=6,H=0.4,A1=2./3.,A2=0.4,A3=2./7.) Rx Returns Dawson’s integral F (x) = exp(−x2 ) 0 exp(t2 )dt for any real x. INTEGER i,init,n0 REAL d1,d2,e1,e2,sum,x2,xp,xx,c(NMAX) SAVE init,c DATA init/0/ Flag is 0 if we need to initialize, else 1. if(init.eq.0)then init=1 do 11 i=1,NMAX c(i)=exp(-((2.*float(i)-1.)*H)**2) enddo 11 endif if(abs(x).lt.0.2)then Use series expansion. x2=x**2 dawson=x*(1.-A1*x2*(1.-A2*x2*(1.-A3*x2))) else Use sampling theorem representation. xx=abs(x) n0=2*nint(0.5*xx/H) xp=xx-float(n0)*H e1=exp(2.*xp*H) e2=e1**2 d1=float(n0+1) d2=d1-2. sum=0. do 12 i=1,NMAX
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
We will discuss the theory that leads to equation (6.10.3) later, in §13.11, as an interesting application of Fourier methods. Here we simply implement a routine based on the formula. It is first convenient to shift the summation index to center it approximately on the maximum of the exponential term. Define n0 to be the even integer nearest to x/h, and x0 ≡ n0 h, x0 ≡ x − x0 , and n0 ≡ n − n0 , so that
254
Chapter 6.
Special Functions
√ Constant is 1/ π.
Other methods for computing Dawson’s integral are also known [2,3] . CITED REFERENCES AND FURTHER READING: Rybicki, G.B. 1989, Computers in Physics, vol. 3, no. 2, pp. 85–87. [1] Cody, W.J., Pociorek, K.A., and Thatcher, H.C. 1970, Mathematics of Computation, vol. 24, pp. 171–178. [2] McCabe, J.H. 1974, Mathematics of Computation, vol. 28, pp. 811–816. [3]
6.11 Elliptic Integrals and Jacobian Elliptic Functions Elliptic integrals occur in many applications, because any integral of the form Z R(t, s) dt (6.11.1) where R is a rational function of t and s, and s is the square root of a cubic or quartic polynomial in t, can be evaluated in terms of elliptic integrals. Standard references [1] describe how to carry out the reduction, which was originally done by Legendre. Legendre showed that only three basic elliptic integrals are required. The simplest of these is Z x dt p (6.11.2) I1 = (a1 + b1 t)(a2 + b2 t)(a3 + b3 t)(a4 + b4 t) y where we have written the quartic s2 in factored form. In standard integral tables [2], one of the limits of integration is always a zero of the quartic, while the other limit lies closer than the next zero, so that there is no singularity within the interval. To evaluate I1 , we simply break the interval [y, x] into subintervals, each of which either begins or ends on a singularity. The tables, therefore, need only distinguish the eight cases in which each of the four zeros (ordered according to size) appears as the upper or lower limit of integration. In addition, when one of the b’s in (6.11.2) tends to zero, the quartic reduces to a cubic, with the largest or smallest singularity moving to ±∞; this leads to eight more cases (actually just special cases of the first eight). The sixteen cases in total are then usually tabulated in terms of Legendre’s standard elliptic integral of the 1st kind, which we will define below. By a change of the variable of integration t, the zeros of the quartic are mapped to standard locations
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
sum=sum+c(i)*(e1/d1+1./(d2*e1)) d1=d1+2. d2=d2-2. e1=e2*e1 enddo 12 dawson=0.5641895835*sign(exp(-xp**2),x)*sum endif return END
6.11 Elliptic Integrals and Jacobian Elliptic Functions
255
on the real axis. Then only two dimensionless parameters are needed to tabulate Legendre’s integral. However, the symmetry of the original integral (6.11.2) under permutation of the roots is concealed in Legendre’s notation. We will get back to Legendre’s notation below. But first, here is a better way:
where x, y, and z are nonnegative and at most one is zero. By standardizing the range of integration, he retains permutation symmetry for the zeros. (Weierstrass’ canonical form also has this property.) Carlson first shows that when x or y is a zero of the quartic in (6.11.2), the integral I1 can be written in terms of RF in a form that is symmetric under permutation of the remaining three zeros. In the general case when neither x nor y is a zero, two such RF functions can be combined into a single one by an addition theorem, leading to the fundamental formula 2 2 2 I1 = 2RF (U12 , U13 , U14 )
(6.11.4)
where Uij = (Xi Xj Yk Ym + Yi Yj Xk Xm )/(x − y) Xi = (ai + bi x)
1/2
,
Yi = (ai + bi y)
1/2
(6.11.5) (6.11.6)
and i, j, k, m is any permutation of 1, 2, 3, 4. A short-cut in evaluating these expressions is 2 2 U13 = U12 − (a1 b4 − a4 b1 )(a2 b3 − a3 b2 ) 2 2 U14 = U12 − (a1 b3 − a3 b1 )(a2 b4 − a4 b2 )
(6.11.7)
The U ’s correspond to the three ways of pairing the four zeros, and I1 is thus manifestly symmetric under permutation of the zeros. Equation (6.11.4) therefore reproduces all sixteen cases when one limit is a zero, and also includes the cases when neither limit is a zero. Thus Carlson’s function allows arbitrary ranges of integration and arbitrary positions of the branch points of the integrand relative to the interval of integration. To handle elliptic integrals of the second and third kind, Carlson defines the standard integral of the third kind as Z dt 3 ∞ p RJ (x, y, z, p) = (6.11.8) 2 0 (t + p) (t + x)(t + y)(t + z) which is symmetric in x, y, and z. The degenerate case when two arguments are equal is denoted RD (x, y, z) = RJ (x, y, z, z)
(6.11.9)
and is symmetric in x and y. The function RD replaces Legendre’s integral of the second kind. The degenerate form of RF is denoted RC (x, y) = RF (x, y, y)
(6.11.10)
It embraces logarithmic, inverse circular, and inverse hyperbolic functions. Carlson [4-7] gives integral tables in terms of the exponents of the linear factors of the quartic in (6.11.1). For example, the integral where the exponents are ( 12 , 12 ,− 12 ,− 32 ) can be expressed as a single integral in terms of RD ; it accounts for 144 separate cases in Gradshteyn and Ryzhik [2]! Refer to Carlson’s papers [3-7] for some of the practical details in reducing elliptic integrals to his standard forms, such as handling complex conjugate zeros.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Carlson [3] has given a new definition of a standard elliptic integral of the first kind, Z dt 1 ∞ p (6.11.3) RF (x, y, z) = 2 0 (t + x)(t + y)(t + z)
256
Chapter 6.
Special Functions
RF (x, y, z) = 2RF (x + λ, y + λ, z + λ) x+λ y+λ z+λ , , = RF 4 4 4
(6.11.11)
λ = (xy)1/2 + (xz)1/2 + (yz)1/2
(6.11.12)
where This theorem can be proved by a simple change of variable of integration [11]. Equation (6.11.11) is iterated until the arguments of RF are nearly equal. For equal arguments we have RF (x, x, x) = x−1/2
(6.11.13)
When the arguments are close enough, the function is evaluated from a fixed Taylor expansion about (6.11.13) through fifth-order terms. While the iterative part of the algorithm is only linearly convergent, the error ultimately decreases by a factor of 46 = 4096 for each iteration. Typically only two or three iterations are required, perhaps six or seven if the initial values of the arguments have huge ratios. We list the algorithm for RF here, and refer you to Carlson’s paper [9] for the other cases. Stage 1: For n = 0, 1, 2, . . . compute µn = (xn + yn + zn )/3 Xn = 1 − (xn /µn ),
Yn = 1 − (yn /µn ),
Zn = 1 − (zn /µn )
n = max(|Xn |, |Yn |, |Zn |) If n < tol go to Stage 2; else compute λn = (xn yn )1/2 + (xn zn )1/2 + (yn zn )1/2 xn+1 = (xn + λn )/4,
yn+1 = (yn + λn )/4,
zn+1 = (zn + λn )/4
and repeat this stage. Stage 2: Compute E2 = Xn Yn − Zn2 , RF = (1 −
1 E 10 2
+
E3 = Xn Yn Zn 1 E 14 3
+
1 E2 24 2
−
3 E E )/(µn )1/2 44 2 3
In some applications the argument p in RJ or the argument y in RC is negative, and the Cauchy principal value of the integral is required. This is easily handled by using the formulas RJ (x, y,z, p) = [(γ − y)RJ (x, y, z, γ) − 3RF (x, y, z) + 3RC (xz/y, pγ/y)] /(y − p) (6.11.14) where γ≡y+
(z − y)(y − x) y−p
(6.11.15)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Turn now to the numerical evaluation of elliptic integrals. The traditional methods [8] are Gauss or Landen transformations. Descending transformations decrease the modulus k of the Legendre integrals towards zero, increasing transformations increase it towards unity. In these limits the functions have simple analytic expressions. While these methods converge quadratically and are quite satisfactory for integrals of the first and second kinds, they generally lead to loss of significant figures in certain regimes for integrals of the third kind. Carlson’s algorithms [9,10] , by contrast, provide a unified method for all three kinds with no significant cancellations. The key ingredient in these algorithms is the duplication theorem:
6.11 Elliptic Integrals and Jacobian Elliptic Functions
is positive if p is negative, and RC (x, y) =
x x−y
257
1/2 RC (x − y, −y)
(6.11.16)
The Cauchy principal value of RJ has a zero at some value of p < 0, so (6.11.14) will give some loss of significant figures near the zero.
*
1
A value of 0.08 for the error tolerance parameter is adequate for single precision (7 significant digits). Since the error scales as 6n , we see that 0.0025 will yield double precision (16 significant digits) and require at most two or three more iterations. Since the coefficients of the sixth-order truncation error are different for the other elliptic functions, these values for the error tolerance should be changed to 0.04 and 0.0012 in the algorithm for RC , and 0.05 and 0.0015 for RJ and RD . As well as being an algorithm in its own right for certain combinations of elementary functions, the algorithm for RC is used repeatedly in the computation of RJ . The Fortran implementations test the input arguments against two machine-dependent constants, TINY and BIG, to ensure that there will be no underflow or overflow during the computation. We have chosen conservative values, corresponding to a machine minimum of 3 × 10−39 and a machine maximum of 1.7 × 1038 . You can always extend the range of admissible argument values by using the homogeneity relations (6.11.22), below.
*
FUNCTION rd(x,y,z) REAL rd,x,y,z,ERRTOL,TINY,BIG,C1,C2,C3,C4,C5,C6 PARAMETER (ERRTOL=.05,TINY=1.e-25,BIG=4.5E21,C1=3./14.,C2=1./6., C3=9./22.,C4=3./26.,C5=.25*C3,C6=1.5*C4) Computes Carlson’s elliptic integral of the second kind, RD (x, y, z). x and y must be nonnegative, and at most one can be zero. z must be positive. TINY must be at least twice the negative 2/3 power of the machine overflow limit. BIG must be at most 0.1 × ERRTOL times the negative 2/3 power of the machine underflow limit. REAL alamb,ave,delx,dely,delz,ea,eb,ec,ed,ee,fac,sqrtx,sqrty,
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
FUNCTION rf(x,y,z) REAL rf,x,y,z,ERRTOL,TINY,BIG,THIRD,C1,C2,C3,C4 PARAMETER (ERRTOL=.08,TINY=1.5e-38,BIG=3.E37,THIRD=1./3., C1=1./24.,C2=.1,C3=3./44.,C4=1./14.) Computes Carlson’s elliptic integral of the first kind, RF (x, y, z). x, y, and z must be nonnegative, and at most one can be zero. TINY must be at least 5 times the machine underflow limit, BIG at most one fifth the machine overflow limit. REAL alamb,ave,delx,dely,delz,e2,e3,sqrtx,sqrty,sqrtz,xt,yt,zt if(min(x,y,z).lt.0..or.min(x+y,x+z,y+z).lt.TINY.or. max(x,y,z).gt.BIG)pause ’invalid arguments in rf’ xt=x yt=y zt=z continue sqrtx=sqrt(xt) sqrty=sqrt(yt) sqrtz=sqrt(zt) alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz xt=.25*(xt+alamb) yt=.25*(yt+alamb) zt=.25*(zt+alamb) ave=THIRD*(xt+yt+zt) delx=(ave-xt)/ave dely=(ave-yt)/ave delz=(ave-zt)/ave if(max(abs(delx),abs(dely),abs(delz)).gt.ERRTOL)goto 1 e2=delx*dely-delz**2 e3=delx*dely*delz rf=(1.+(C1*e2-C2-C3*e3)*e2+C4*e3)/sqrt(ave) return END
258 * *
*
* C
* * *
Special Functions
sqrtz,sum,xt,yt,zt if(min(x,y).lt.0..or.min(x+y,z).lt.TINY.or. max(x,y,z).gt.BIG)pause ’invalid arguments in rd’ xt=x yt=y zt=z sum=0. fac=1. continue sqrtx=sqrt(xt) sqrty=sqrt(yt) sqrtz=sqrt(zt) alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz sum=sum+fac/(sqrtz*(zt+alamb)) fac=.25*fac xt=.25*(xt+alamb) yt=.25*(yt+alamb) zt=.25*(zt+alamb) ave=.2*(xt+yt+3.*zt) delx=(ave-xt)/ave dely=(ave-yt)/ave delz=(ave-zt)/ave if(max(abs(delx),abs(dely),abs(delz)).gt.ERRTOL)goto 1 ea=delx*dely eb=delz*delz ec=ea-eb ed=ea-6.*eb ee=ed+ec+ec rd=3.*sum+fac*(1.+ed*(-C1+C5*ed-C6*delz*ee) +delz*(C2*ee+delz*(-C3*ec+delz*C4*ea)))/(ave*sqrt(ave)) return END
FUNCTION rj(x,y,z,p) REAL rj,p,x,y,z,ERRTOL,TINY,BIG,C1,C2,C3,C4,C5,C6,C7,C8 PARAMETER (ERRTOL=.05,TINY=2.5e-13,BIG=9.E11,C1=3./14.,C2=1./3., C3=3./22.,C4=3./26.,C5=.75*C3,C6=1.5*C4,C7=.5*C2,C8=C3+C3) USES rc,rf Computes Carlson’s elliptic integral of the third kind, RJ (x, y, z, p). x, y, and z must be nonnegative, and at most one can be zero. p must be nonzero. If p < 0, the Cauchy principal value is returned. TINY must be at least twice the cube root of the machine underflow limit, BIG at most one fifth the cube root of the machine overflow limit. REAL a,alamb,alpha,ave,b,beta,delp,delx,dely,delz,ea,eb,ec, ed,ee,fac,pt,rcx,rho,sqrtx,sqrty,sqrtz,sum,tau,xt, yt,zt,rc,rf if(min(x,y,z).lt.0..or.min(x+y,x+z,y+z,abs(p)).lt.TINY.or. max(x,y,z,abs(p)).gt.BIG)pause ’invalid arguments in rj’ sum=0. fac=1. if(p.gt.0.)then xt=x yt=y zt=z pt=p else xt=min(x,y,z) zt=max(x,y,z) yt=x+y+z-xt-zt a=1./(yt-p) b=a*(zt-yt)*(yt-xt) pt=yt+b rho=xt*zt/yt
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 6.
6.11 Elliptic Integrals and Jacobian Elliptic Functions
1
* * *
* *
1
tau=p*pt/yt rcx=rc(rho,tau) endif continue sqrtx=sqrt(xt) sqrty=sqrt(yt) sqrtz=sqrt(zt) alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz alpha=(pt*(sqrtx+sqrty+sqrtz)+sqrtx*sqrty*sqrtz)**2 beta=pt*(pt+alamb)**2 sum=sum+fac*rc(alpha,beta) fac=.25*fac xt=.25*(xt+alamb) yt=.25*(yt+alamb) zt=.25*(zt+alamb) pt=.25*(pt+alamb) ave=.2*(xt+yt+zt+pt+pt) delx=(ave-xt)/ave dely=(ave-yt)/ave delz=(ave-zt)/ave delp=(ave-pt)/ave if(max(abs(delx),abs(dely),abs(delz),abs(delp)).gt.ERRTOL)goto 1 ea=delx*(dely+delz)+dely*delz eb=delx*dely*delz ec=delp**2 ed=ea-3.*ec ee=eb+2.*delp*(ea-ec) rj=3.*sum+fac*(1.+ed*(-C1+C5*ed-C6*ee)+eb*(C7+delp*(-C8+delp*C4)) +delp*ea*(C2-delp*C3)-C2*delp*ec)/(ave*sqrt(ave)) if (p.le.0.) rj=a*(b*rj+3.*(rcx-rf(xt,yt,zt))) return END
FUNCTION rc(x,y) REAL rc,x,y,ERRTOL,TINY,SQRTNY,BIG,TNBG,COMP1,COMP2,THIRD, C1,C2,C3,C4 PARAMETER (ERRTOL=.04,TINY=1.69e-38,SQRTNY=1.3e-19,BIG=3.E37, TNBG=TINY*BIG,COMP1=2.236/SQRTNY,COMP2=TNBG*TNBG/25., THIRD=1./3.,C1=.3,C2=1./7.,C3=.375,C4=9./22.) Computes Carlson’s degenerate elliptic integral, RC (x, y). x must be nonnegative and y must be nonzero. If y < 0, the Cauchy principal value is returned. TINY must be at least 5 times the machine underflow limit, BIG at most one fifth the machine maximum overflow limit. REAL alamb,ave,s,w,xt,yt if(x.lt.0..or.y.eq.0..or.(x+abs(y)).lt.TINY.or.(x+abs(y)).gt.BIG .or.(y.lt.-COMP1.and.x.gt.0..and.x.lt.COMP2)) pause ’invalid arguments in rc’ if(y.gt.0.)then xt=x yt=y w=1. else xt=x-y yt=-y w=sqrt(x)/sqrt(xt) endif continue alamb=2.*sqrt(xt)*sqrt(yt)+yt xt=.25*(xt+alamb) yt=.25*(yt+alamb) ave=THIRD*(xt+yt+yt) s=(yt-ave)/ave
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
259
260
Chapter 6.
Special Functions
if(abs(s).gt.ERRTOL)goto 1 rc=w*(1.+s*s*(C1+s*(C2+s*(C3+s*C4))))/sqrt(ave) return END
K(k) ≡ F (π/2, k)
(6.11.18)
In terms of RF , F (φ, k) = sin φRF (cos2 φ, 1 − k 2 sin2 φ, 1) K(k) = RF (0, 1 − k 2 , 1)
(6.11.19)
The Legendre elliptic integral of the 2nd kind and the complete elliptic integral of the 2nd kind are given by Z φp E(φ, k) ≡ 1 − k 2 sin2 θ dθ 0
= sin φRF (cos2 φ, 1 − k 2 sin2 φ, 1)
(6.11.20)
− 13 k 2 sin3 φRD (cos2 φ, 1 − k 2 sin2 φ, 1) E(k) ≡ E(π/2, k) = RF (0, 1 − k 2 , 1) − 13 k 2 RD (0, 1 − k 2 , 1) Finally, the Legendre elliptic integral of the 3rd kind is Z
φ
Π(φ, n, k) ≡ 0
dθ p (1 + n sin θ) 1 − k 2 sin2 θ 2
= sin φRF (cos2 φ, 1 − k 2 sin2 φ, 1)
(6.11.21)
− 13 n sin3 φRJ (cos2 φ, 1 − k 2 sin2 φ, 1, 1 + n sin2 φ) (Note that this sign convention for n is opposite that of Abramowitz and Stegun [12], and that their sin α is our k.)
C
FUNCTION ellf(phi,ak) REAL ellf,ak,phi USES rf Legendre elliptic integral of the 1st kind F (φ, k), evaluated using Carlson’s function RF . The argument ranges are 0 ≤ φ ≤ π/2, 0 ≤ k sin φ ≤ 1. REAL s,rf s=sin(phi) ellf=s*rf(cos(phi)**2,(1.-s*ak)*(1.+s*ak),1.) return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
At times you may want to express your answer in Legendre’s notation. Alternatively, you may be given results in that notation and need to compute their values with the programs given above. It is a simple matter to transform back and forth. The Legendre elliptic integral of the 1st kind is defined as Z φ dθ p (6.11.17) F (φ, k) ≡ 0 1 − k 2 sin2 θ The complete elliptic integral of the 1st kind is given by
6.11 Elliptic Integrals and Jacobian Elliptic Functions
C
FUNCTION elle(phi,ak) REAL elle,ak,phi USES rd,rf Legendre elliptic integral of the 2nd kind E(φ, k), evaluated using Carlson’s functions RD and RF . The argument ranges are 0 ≤ φ ≤ π/2, 0 ≤ k sin φ ≤ 1. REAL cc,q,s,rd,rf s=sin(phi) cc=cos(phi)**2 q=(1.-s*ak)*(1.+s*ak) elle=s*(rf(cc,q,1.)-((s*ak)**2)*rd(cc,q,1.)/3.) return END FUNCTION ellpi(phi,en,ak) REAL ellpi,ak,en,phi USES rf,rj Legendre elliptic integral of the 3rd kind Π(φ, n, k), evaluated using Carlson’s functions RJ and RF . (Note that the sign convention on n is opposite that of Abramowitz and Stegun.) The ranges of φ and k are 0 ≤ φ ≤ π/2, 0 ≤ k sin φ ≤ 1. REAL cc,enss,q,s,rf,rj s=sin(phi) enss=en*s*s cc=cos(phi)**2 q=(1.-s*ak)*(1.+s*ak) ellpi=s*(rf(cc,q,1.)-enss*rj(cc,q,1.,1.+enss)/3.) return END
Carlson’s functions are homogeneous of degree − 12 and − 32 , so RF (λx, λy, λz) = λ−1/2 RF (x, y, z) RJ (λx, λy, λz, λp) = λ−3/2 RJ (x, y, z, p)
(6.11.22)
Thus to express a Carlson function in Legendre’s notation, permute the first three arguments into ascending order, use homogeneity to scale the third argument to be 1, and then use equations (6.11.19)–(6.11.21).
Jacobian Elliptic Functions The Jacobian elliptic function sn is defined as follows: instead of considering the elliptic integral u(y, k) ≡ u = F (φ, k)
(6.11.23)
consider the inverse function y = sin φ = sn(u, k) Equivalently,
Z sn u= 0
dy p 2 (1 − y )(1 − k 2 y2 )
(6.11.24)
(6.11.25)
When k = 0, sn is just sin. The functions cn and dn are defined by the relations sn2 + cn2 = 1,
k 2 sn2 + dn2 = 1
(6.11.26)
The routine given below actually takes mc ≡ kc2 = 1 − k 2 as an input parameter. It also computes all three functions sn, cn, and dn since computing all three is no harder than computing any one of them. For a description of the method, see [8].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C
261
262
2
Special Functions
SUBROUTINE sncndn(uu,emmc,sn,cn,dn) REAL cn,dn,emmc,sn,uu,CA PARAMETER (CA=.0003) The accuracy is the square of CA. Returns the Jacobian elliptic functions sn(u, kc ), cn(u, kc ), and dn(u, kc ). Here uu = u, while emmc = kc2 . INTEGER i,ii,l REAL a,b,c,d,emc,u,em(13),en(13) LOGICAL bo emc=emmc u=uu if(emc.ne.0.)then bo=(emc.lt.0.) if(bo)then d=1.-emc emc=-emc/d d=sqrt(d) u=d*u endif a=1. dn=1. do 11 i=1,13 l=i em(i)=a emc=sqrt(emc) en(i)=emc c=0.5*(a+emc) if(abs(a-emc).le.CA*a)goto 1 emc=a*emc a=c enddo 11 u=c*u sn=sin(u) cn=cos(u) if(sn.eq.0.)goto 2 a=cn/sn c=a*c do 12 ii=l,1,-1 b=em(ii) a=c*a c=dn*c dn=(en(ii)+a)/(b+a) a=c/b enddo 12 a=1./sqrt(c**2+1.) if(sn.lt.0.)then sn=-a else sn=a endif cn=c*sn if(bo)then a=dn dn=cn cn=a sn=sn/d endif else cn=1./cosh(u) dn=cn sn=tanh(u) endif return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 6.
6.12 Hypergeometric Functions
263
CITED REFERENCES AND FURTHER READING: Erdelyi, ´ A., Magnus, W., Oberhettinger, F., and Tricomi, F.G. 1953, Higher Transcendental Functions, Vol. II, (New York: McGraw-Hill). [1] Gradshteyn, I.S., and Ryzhik, I.W. 1980, Table of Integrals, Series, and Products (New York: Academic Press). [2] Carlson, B.C. 1977, SIAM Journal on Mathematical Analysis, vol. 8, pp. 231–242. [3]
Bulirsch, R. 1965, Numerische Mathematik, vol. 7, pp. 78–90; 1965, op. cit., vol. 7, pp. 353–354; 1969, op. cit., vol. 13, pp. 305–315. [8] Carlson, B.C. 1979, Numerische Mathematik, vol. 33, pp. 1–16. [9] Carlson, B.C., and Notis, E.M. 1981, ACM Transactions on Mathematical Software, vol. 7, pp. 398–403. [10] Carlson, B.C. 1978, SIAM Journal on Mathematical Analysis, vol. 9, p. 524–528. [11] Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), Chapter 17. [12] Mathews, J., and Walker, R.L. 1970, Mathematical Methods of Physics, 2nd ed. (Reading, MA: W.A. Benjamin/Addison-Wesley), pp. 78–79.
6.12 Hypergeometric Functions As was discussed in §5.14, a fast, general routine for the the complex hypergeometric function 2 F1 (a, b, c; z), is difficult or impossible. The function is defined as the analytic continuation of the hypergeometric series, a(a + 1)b(b + 1) z 2 ab z + +··· c 1! c(c + 1) 2! a(a + 1) . . . (a + j − 1)b(b + 1) . . . (b + j − 1) z j +··· + c(c + 1) . . . (c + j − 1) j! (6.12.1) This series converges only within the unit circle |z| < 1 (see [1]), but one’s interest in the function is not confined to this region. Section 5.14 discussed the method of evaluating this function by direct path integration in the complex plane. We here merely list the routines that result. Implementation of the function hypgeo is straightforward, and is described by comments in the program. The machinery associated with Chapter 16’s routine for integrating differential equations, odeint, is only minimally intrusive, and need not even be completely understood: use of odeint requires a common block with one zeroed variable, one subroutine call, and a prescribed format for the derivative routine hypdrv. The subroutine hypgeo will fail, of course, for values of z too close to the singularity at 1. (If you need to approach this singularity, or the one at ∞, use the “linear transformation formulas” in §15.3 of [1].) Away from z = 1, and for moderate values of a, b, c, it is often remarkable how few steps are required to integrate the equations. A half-dozen is typical. 2 F1 (a, b, c; z)
=1+
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Carlson, B.C. 1987, Mathematics of Computation, vol. 49, pp. 595–606 [4]; 1988, op. cit., vol. 51, pp. 267–280 [5]; 1989, op. cit., vol. 53, pp. 327–333 [6]; 1991, op. cit., vol. 56, pp. 267–280. [7]
264
C
Chapter 6.
Special Functions
SUBROUTINE hypser(a,b,c,z,series,deriv) INTEGER n COMPLEX a,b,c,z,series,deriv,aa,bb,cc,fac,temp Returns the hypergeometric series 2 F1 and its derivative, iterating to machine accuracy. For cabs(z) ≤ 1/2 convergence is quite rapid. deriv=cmplx(0.,0.) fac=cmplx(1.,0.) temp=fac aa=a bb=b cc=c do 11 n=1,1000 fac=((aa*bb)/cc)*fac deriv=deriv+fac fac=fac*z/n series=temp+fac if (series.eq.temp) return temp=series aa=aa+1. bb=bb+1. cc=cc+1. enddo 11 pause ’convergence failure in hypser’ END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
FUNCTION hypgeo(a,b,c,z) COMPLEX hypgeo,a,b,c,z REAL EPS PARAMETER (EPS=1.e-6) Accuracy parameter. USES bsstep,hypdrv,hypser,odeint Complex hypergeometric function 2 F1 for complex a, b, c, and z, by direct integration of the hypergeometric equation in the complex plane. The branch cut is taken to lie along the real axis, Re z > 1. INTEGER kmax,nbad,nok EXTERNAL bsstep,hypdrv COMPLEX z0,dz,aa,bb,cc,y(2) COMMON /hypg/ aa,bb,cc,z0,dz COMMON /path/ kmax Used by odeint. kmax=0 if (real(z)**2+aimag(z)**2.le.0.25) then Use series... call hypser(a,b,c,z,hypgeo,y(2)) return else if (real(z).lt.0.) then ...or pick a starting point for the path intez0=cmplx(-0.5,0.) gration. else if (real(z).le.1.0) then z0=cmplx(0.5,0.) else z0=cmplx(0.,sign(0.5,aimag(z))) endif aa=a Load the common block, used to pass pabb=b rameters “over the head” of odeint to cc=c hypdrv. dz=z-z0 call hypser(aa,bb,cc,z0,y(1),y(2)) Get starting function and derivative. call odeint(y,4,0.,1.,EPS,.1,.0001,nok,nbad,hypdrv,bsstep) The arguments to odeint are the vector of independent variables, its length, the starting and ending values of the dependent variable, the accuracy parameter, an initial guess for stepsize, a minimum stepsize, the (returned) number of good and bad steps taken, and the names of the derivative routine and the (here Bulirsch-Stoer) stepping routine. hypgeo=y(1) return END
265 6.12 Hypergeometric Functions
CITED REFERENCES AND FURTHER READING:
Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York). [1]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE hypdrv(s,y,dyds) REAL s COMPLEX y(2),dyds(2),aa,bb,cc,z0,dz,z Derivative subroutine for the hypergeometric equation, see text equation (5.14.4). COMMON /hypg/ aa,bb,cc,z0,dz z=z0+s*dz dyds(1)=y(2)*dz dyds(2)=((aa*bb)*y(1)-(cc-((aa+bb)+1.)*z)*y(2))*dz/(z*(1.-z)) return END
Random Numbers
7.0 Introduction It may seem perverse to use a computer, that most precise and deterministic of all machines conceived by the human mind, to produce “random” numbers. More than perverse, it may seem to be a conceptual impossibility. Any program, after all, will produce output that is entirely predictable, hence not truly “random.” Nevertheless, practical computer “random number generators” are in common use. We will leave it to philosophers of the computer age to resolve the paradox in a deep way (see, e.g., Knuth [1] §3.5 for discussion and references). One sometimes hears computer-generated sequences termed pseudo-random, while the word random is reserved for the output of an intrinsically random physical process, like the elapsed time between clicks of a Geiger counter placed next to a sample of some radioactive element. We will not try to make such fine distinctions. A working, though imprecise, definition of randomness in the context of computer-generated sequences, is to say that the deterministic program that produces a random sequence should be different from, and — in all measurable respects — statistically uncorrelated with, the computer program that uses its output. In other words, any two different random number generators ought to produce statistically the same results when coupled to your particular applications program. If they don’t, then at least one of them is not (from your point of view) a good generator. The above definition may seem circular, comparing, as it does, one generator to another. However, there exists a body of random number generators which mutually do satisfy the definition over a very, very broad class of applications programs. And it is also found empirically that statistically identical results are obtained from random numbers produced by physical processes. So, because such generators are known to exist, we can leave to the philosophers the problem of defining them. A pragmatic point of view, then, is that randomness is in the eye of the beholder (or programmer). What is random enough for one application may not be random enough for another. Still, one is not entirely adrift in a sea of incommensurable applications programs: There is a certain list of statistical tests, some sensible and some merely enshrined by history, which on the whole will do a very good job of ferreting out any correlations that are likely to be detected by an applications program (in this case, yours). Good random number generators ought to pass all of these tests; or at least the user had better be aware of any that they fail, so that he or she will be able to judge whether they are relevant to the case at hand. 266
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Chapter 7.
7.1 Uniform Deviates
267
As for references on this subject, the one to turn to first is Knuth [1]. Then try [2]. Only a few of the standard books on numerical methods [3-4] treat topics relating to random numbers.
CITED REFERENCES AND FURTHER READING:
Dahlquist, G., and Bjorck, A. 1974, Numerical Methods (Englewood Cliffs, NJ: Prentice-Hall), Chapter 11. [3] Forsythe, G.E., Malcolm, M.A., and Moler, C.B. 1977, Computer Methods for Mathematical Computations (Englewood Cliffs, NJ: Prentice-Hall), Chapter 10. [4]
7.1 Uniform Deviates Uniform deviates are just random numbers that lie within a specified range (typically 0 to 1), with any one number in the range just as likely as any other. They are, in other words, what you probably think “random numbers” are. However, we want to distinguish uniform deviates from other sorts of random numbers, for example numbers drawn from a normal (Gaussian) distribution of specified mean and standard deviation. These other sorts of deviates are almost always generated by performing appropriate operations on one or more uniform deviates, as we will see in subsequent sections. So, a reliable source of random uniform deviates, the subject of this section, is an essential building block for any sort of stochastic modeling or Monte Carlo computer work.
System-Supplied Random Number Generators Your computer very likely has lurking within it a library routine which is called a “random number generator.” That routine typically has an unforgettable name like “ran,” and a calling sequence like x=ran(iseed)
sets x to the next random number and updates iseed
You initialize iseed to a (usually) arbitrary value before the first call to ran. Each initializing value will typically return a different subsequent random sequence, or at least a different subsequence of some one enormously long sequence. The same initializing value of iseed will always return the same random sequence, however. Now our first, and perhaps most important, lesson in this chapter is: Be very, very suspicious of a system-supplied ran that resembles the one just described. If all scientific papers whose results are in doubt because of bad rans were to disappear from library shelves, there would be a gap on each shelf about as big as your fist. System-supplied rans are almost always linear congruential generators, which
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), Chapter 3, especially §3.5. [1] Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: SpringerVerlag). [2]
268
Chapter 7.
Random Numbers
generate a sequence of integers I1 , I2 , I3 , . . . , each between 0 and m − 1 (a large number) by the recurrence relation Ij+1 = aIj + c
(mod m)
(7.1.1)
Correlation in k-space is not the only weakness of linear congruential generators. Such generators often have their low-order (least significant) bits much less random than their high-order bits. If you want to generate a random integer between 1 and 10, you should always do it using high-order bits, as in j=1+int(10.*ran(iseed))
and never by anything resembling j=1+mod(int(1000000.*ran(iseed)),10)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Here m is called the modulus, and a and c are positive integers called the multiplier and the increment, respectively. The recurrence (7.1.1) will eventually repeat itself, with a period that is obviously no greater than m. If m, a, and c are properly chosen, then the period will be of maximal length, i.e., of length m. In that case, all possible integers between 0 and m − 1 occur at some point, so any initial “seed” choice of I0 is as good as any other: The sequence just takes off from that point. The real number between 0 and 1 which is returned is generally Ij+1 /m, so that it is strictly less than 1, but occasionally (once in m calls) exactly equal to zero. iseed is set to Ij+1 (or some encoding of it), so that it can be used on the next call to generate Ij+2 , and so on. The linear congruential method has the advantage of being very fast, requiring only a few operations per call, hence its almost universal use. It has the disadvantage that it is not free of sequential correlation on successive calls. If k random numbers at a time are used to plot points in k dimensional space (with each coordinate between 0 and 1), then the points will not tend to “fill up” the k-dimensional space, but rather will lie on (k − 1)-dimensional “planes.” There will be at most about m1/k such planes. If the constants m, a, and c are not very carefully chosen, there will be many fewer than that. The number m is usually close to the machine’s largest representable integer, e.g., ∼ 232. So, for example, the number of planes on which triples of points lie in three-dimensional space is usually no greater than about the cube root of 232 , about 1600. You might well be focusing attention on a physical process that occurs in a small fraction of the total volume, so that the discreteness of the planes can be very pronounced. Even worse, you might be using a ran whose choices of m, a, and c have been botched. One infamous such routine, RANDU, with a = 65539 and m = 231 , was widespread on IBM mainframe computers for many years, and widely copied onto other systems [1]. One of us recalls producing a “random” plot with only 11 planes, and being told by his computer center’s programming consultant that he had misused the random number generator: “We guarantee that each number is random individually, but we don’t guarantee that more than one of them is random.” Figure that out.
7.1 Uniform Deviates
269
(which uses lower-order bits). Similarly you should never try to take apart a “ran” number into several supposedly random pieces. Instead use separate calls for every piece.
Portable Random Number Generators
Ij+1 = aIj
(mod m)
(7.1.2)
can be as good as any of the more general linear congruential generators that have c 6= 0 (equation 7.1.1) — if the multiplier a and modulus m are chosen exquisitely carefully. Park and Miller propose a “Minimal Standard” generator based on the choices m = 231 − 1 = 2147483647
a = 75 = 16807
(7.1.3)
First proposed by Lewis, Goodman, and Miller in 1969, this generator has in subsequent years passed all new theoretical tests, and (perhaps more importantly) has accumulated a large amount of successful use. Park and Miller do not claim that the generator is “perfect” (we will see below that it is not), but only that it is a good minimal standard against which other generators should be judged. It is not possible to implement equations (7.1.2) and (7.1.3) directly in a high-level language, since the product of a and m − 1 exceeds the maximum value for a 32-bit integer. Assembly language implementation using a 64-bit product register is straightforward, but not portable from machine to machine. A trick due to Schrage [2,3] for multiplying two 32-bit integers modulo a 32-bit constant, without using any intermediates larger than 32 bits (including a sign bit) is therefore extremely interesting: It allows the Minimal Standard generator to be implemented in essentially any programming language on essentially any machine. Schrage’s algorithm is based on an approximate factorization of m, m = aq + r,
i.e.,
q = [m/a], r = m mod a
(7.1.4)
with square brackets denoting integer part. If r is small, specifically r < q, and 0 < z < m − 1, it can be shown that both a(z mod q) and r[z/q] lie in the range 0, . . . , m − 1, and that a(z mod q) − r[z/q] if it is ≥ 0, (7.1.5) az mod m = a(z mod q) − r[z/q] + m otherwise The application of Schrage’s algorithm to the constants (7.1.3) uses the values q = 127773 and r = 2836. Here is an implementation of the Minimal Standard generator:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Park and Miller [1] have surveyed a large number of random number generators that have been used over the last 30 years or more. Along with a good theoretical review, they present an anecdotal sampling of a number of inadequate generators that have come into widespread use. The historical record is nothing if not appalling. There is good evidence, both theoretical and empirical, that the simple multiplicative congruential algorithm
270
*
Chapter 7.
Random Numbers
The period of ran0 is 231 − 2 ≈ 2.1 × 109 . A peculiarity of generators of the form (7.1.2) is that the value 0 must never be allowed as the initial seed — it perpetuates itself — and it never occurs for any nonzero initial seed. Experience has shown that users always manage to call random number generators with the seed idum=0. That is why ran0 performs its exclusive-or with an arbitrary constant both on entry and exit. If you are the first user in history to be proof against human error, you can remove the two lines with the ieor function. Park and Miller discuss two other multipliers a that can be used with the same m = 231 − 1. These are a = 48271 (with q = 44488 and r = 3399) and a = 69621 (with q = 30845 and r = 23902). These can be substituted in the routine ran0 if desired; they may be slightly superior to Lewis et al.’s longer-tested values. No values other than these should be used. The routine ran0 is a Minimal Standard, satisfactory for the majority of applications, but we do not recommend it as the final word on random number generators. Our reason is precisely the simplicity of the Minimal Standard. It is not hard to think of situations where successive random numbers might be used in a way that accidentally conflicts with the generation algorithm. For example, since successive numbers differ by a multiple of only 1.6×104 out of a modulus of more than 2×109 , very small random numbers will tend to be followed by smaller than average values. One time in 106 , for example, there will be a value < 10−6 returned (as there should be), but this will always be followed by a value less than about 0.0168. One can easily think of applications involving rare events where this property would lead to wrong results. There are other, more subtle, serial correlations present in ran0. For example, if successive points (Ii , Ii+1 ) are binned into a two-dimensional plane for i = 1, 2, . . . , N , then the resulting distribution fails the χ2 test when N is greater than a few ×107 , much less than the period m − 2. Since low-order serial correlations have historically been such a bugaboo, and since there is a very simple way to remove them, we think that it is prudent to do so. The following routine, ran1, uses the Minimal Standard for its random value, but it shuffles the output to remove low-order serial correlations. A random deviate derived from the jth value in the sequence, Ij , is output not on the jth call, but rather on a randomized later call, j + 32 on average. The shuffling algorithm is due to Bays and Durham as described in Knuth [4], and is illustrated in Figure 7.1.1.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
FUNCTION ran0(idum) INTEGER idum,IA,IM,IQ,IR,MASK REAL ran0,AM PARAMETER (IA=16807,IM=2147483647,AM=1./IM, IQ=127773,IR=2836,MASK=123459876) “Minimal” random number generator of Park and Miller. Returns a uniform random deviate between 0.0 and 1.0. Set or reset idum to any integer value (except the unlikely value MASK) to initialize the sequence; idum must not be altered between calls for successive deviates in a sequence. INTEGER k idum=ieor(idum,MASK) XORing with MASK allows use of zero and other simple k=idum/IQ bit patterns for idum. idum=IA*(idum-k*IQ)-IR*k Compute idum=mod(IA*idum,IM) without overflows by if (idum.lt.0) idum=idum+IM Schrage’s method. ran0=AM*idum Convert idum to a floating result. idum=ieor(idum,MASK) Unmask before return. return END
7.1 Uniform Deviates
*
271
The routine ran1 passes those statistical tests that ran0 is known to fail. In fact, we do not know of any statistical test that ran1 fails to pass, except when the number of calls starts to become on the order of the period m, say > 108 ≈ m/20. For situations when even longer random sequences are needed, L’Ecuyer [6] has given a good way of combining two different sequences with different periods so as to obtain a new sequence whose period is the least common multiple of the two periods. The basic idea is simply to add the two sequences, modulo the modulus of either of them (call it m). A trick to avoid an intermediate value that overflows the integer wordsize is to subtract rather than add, and then add back the constant m − 1 if the result is ≤ 0, so as to wrap around into the desired interval 0, . . . , m − 1. Notice that it is not necessary that this wrapped subtraction be able to reach all values 0, . . . , m − 1 from every value of the first sequence. Consider the absurd extreme case where the value subtracted was only between 1 and 10: The resulting sequence would still be no less random than the first sequence by itself. As a practical matter it is only necessary that the second sequence have a range covering substantially all of the range of the first. L’Ecuyer recommends the use of the two generators m1 = 2147483563 (with a1 = 40014, q1 = 53668, r1 = 12211) and m2 = 2147483399 (with a2 = 40692, q2 = 52774, r2 = 3791). Both moduli are slightly less than 231 . The periods m1 − 1 = 2 × 3 × 7 × 631 × 81031 and m2 − 1 = 2 × 19 × 31 × 1019 × 1789 share only the factor 2, so the period of the combined generator is ≈ 2.3 × 1018 . For present computers, period exhaustion is a practical impossibility.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
FUNCTION ran1(idum) INTEGER idum,IA,IM,IQ,IR,NTAB,NDIV REAL ran1,AM,EPS,RNMX PARAMETER (IA=16807,IM=2147483647,AM=1./IM,IQ=127773,IR=2836, NTAB=32,NDIV=1+(IM-1)/NTAB,EPS=1.2e-7,RNMX=1.-EPS) “Minimal” random number generator of Park and Miller with Bays-Durham shuffle and added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call with idum a negative integer to initialize; thereafter, do not alter idum between successive deviates in a sequence. RNMX should approximate the largest floating value that is less than 1. INTEGER j,k,iv(NTAB),iy SAVE iv,iy DATA iv /NTAB*0/, iy /0/ if (idum.le.0.or.iy.eq.0) then Initialize. idum=max(-idum,1) Be sure to prevent idum = 0. do 11 j=NTAB+8,1,-1 Load the shuffle table (after 8 warm-ups). k=idum/IQ idum=IA*(idum-k*IQ)-IR*k if (idum.lt.0) idum=idum+IM if (j.le.NTAB) iv(j)=idum enddo 11 iy=iv(1) endif k=idum/IQ Start here when not initializing. idum=IA*(idum-k*IQ)-IR*k Compute idum=mod(IA*idum,IM) without overflows by if (idum.lt.0) idum=idum+IM Schrage’s method. j=1+iy/NDIV Will be in the range 1:NTAB. iy=iv(j) Output previously stored value and refill the shuffle taiv(j)=idum ble. ran1=min(AM*iy,RNMX) Because users don’t expect endpoint values. return END
272
Chapter 7.
Random Numbers
iy
1
RAN
3
OUTPUT
2
iv32
Figure 7.1.1. Shuffling procedure used in ran1 to break up sequential correlations in the Minimal Standard generator. Circled numbers indicate the sequence of events: On each call, the random number in iy is used to choose a random element in the array iv. That element becomes the output random number, and also is the next iy. Its spot in iv is refilled from the Minimal Standard routine.
Combining the two generators breaks up serial correlations to a considerable extent. We nevertheless recommend the additional shuffle that is implemented in the following routine, ran2. We think that, within the limits of its floating-point precision, ran2 provides perfect random numbers; a practical definition of “perfect” is that we will pay $1000 to the first reader who convinces us otherwise (by finding a statistical test that ran2 fails in a nontrivial way, excluding the ordinary limitations of a machine’s floating-point representation).
* *
FUNCTION ran2(idum) INTEGER idum,IM1,IM2,IMM1,IA1,IA2,IQ1,IQ2,IR1,IR2,NTAB,NDIV REAL ran2,AM,EPS,RNMX PARAMETER (IM1=2147483563,IM2=2147483399,AM=1./IM1,IMM1=IM1-1, IA1=40014,IA2=40692,IQ1=53668,IQ2=52774,IR1=12211, IR2=3791,NTAB=32,NDIV=1+IMM1/NTAB,EPS=1.2e-7,RNMX=1.-EPS) Long period (> 2 × 1018 ) random number generator of L’Ecuyer with Bays-Durham shuffle and added safeguards. Returns a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call with idum a negative integer to initialize; thereafter, do not alter idum between successive deviates in a sequence. RNMX should approximate the largest floating value that is less than 1. INTEGER idum2,j,k,iv(NTAB),iy SAVE iv,iy,idum2 DATA idum2/123456789/, iv/NTAB*0/, iy/0/ if (idum.le.0) then Initialize. idum=max(-idum,1) Be sure to prevent idum = 0. idum2=idum do 11 j=NTAB+8,1,-1 Load the shuffle table (after 8 warm-ups). k=idum/IQ1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
iv1
7.1 Uniform Deviates
Start here when not initializing. Compute idum=mod(IA1*idum,IM1) without overflows by Schrage’s method. Compute idum2=mod(IA2*idum2,IM2) likewise. Will be in the range 1:NTAB. Here idum is shuffled, idum and idum2 are combined to generate output. Because users don’t expect endpoint values.
L’Ecuyer [6] lists additional short generators that can be combined into longer ones, including generators that can be implemented in 16-bit integer arithmetic. Finally, we give you Knuth’s suggestion [4] for a portable routine, which we have translated to the present conventions as ran3. This is not based on the linear congruential method at all, but rather on a subtractive method (see also [5]). One might hope that its weaknesses, if any, are therefore of a highly different character from the weaknesses, if any, of ran1 above. If you ever suspect trouble with one routine, it is a good idea to try the other in the same application. ran3 has one nice feature: if your machine is poor on integer arithmetic (i.e., is limited to 16-bit integers), substitution of the three “commented” lines for the ones directly preceding them will render the routine entirely floating-point.
C
C
C
FUNCTION ran3(idum) Returns a uniform random deviate between 0.0 and 1.0. Set idum to any negative value to initialize or reinitialize the sequence. INTEGER idum INTEGER MBIG,MSEED,MZ REAL MBIG,MSEED,MZ REAL ran3,FAC PARAMETER (MBIG=1000000000,MSEED=161803398,MZ=0,FAC=1./MBIG) PARAMETER (MBIG=4000000.,MSEED=1618033.,MZ=0.,FAC=1./MBIG) According to Knuth, any large mbig, and any smaller (but still large) mseed can be substituted for the above values. INTEGER i,iff,ii,inext,inextp,k INTEGER mj,mk,ma(55) The value 55 is special and should not be modified; see REAL mj,mk,ma(55) Knuth. SAVE iff,inext,inextp,ma DATA iff /0/ if(idum.lt.0.or.iff.eq.0)then Initialization. iff=1 mj=abs(MSEED-abs(idum)) Initialize ma(55) using the seed idum and the large nummj=mod(mj,MBIG) ber mseed. ma(55)=mj mk=1 do 11 i=1,54 Now initialize the rest of the table, ii=mod(21*i,55) in a slightly random order, ma(ii)=mk with numbers that are not especially random. mk=mj-mk if(mk.lt.MZ)mk=mk+MBIG
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
idum=IA1*(idum-k*IQ1)-k*IR1 if (idum.lt.0) idum=idum+IM1 if (j.le.NTAB) iv(j)=idum enddo 11 iy=iv(1) endif k=idum/IQ1 idum=IA1*(idum-k*IQ1)-k*IR1 if (idum.lt.0) idum=idum+IM1 k=idum2/IQ2 idum2=IA2*(idum2-k*IQ2)-k*IR2 if (idum2.lt.0) idum2=idum2+IM2 j=1+iy/NDIV iy=iv(j)-idum2 iv(j)=idum if(iy.lt.1)iy=iy+IMM1 ran2=min(AM*iy,RNMX) return END
273
274
Chapter 7.
Random Numbers
Quick and Dirty Generators One sometimes would like a “quick and dirty” generator to embed in a program, perhaps taking only one or two lines of code, just to somewhat randomize things. One might wish to process data from an experiment not always in exactly the same order, for example, so that the first output is more “typical” than might otherwise be the case. For this kind of application, all we really need is a list of “good” choices for m, a, and c in equation (7.1.1). If we don’t need a period longer than 104 to 106 , say, we can keep the value of (m − 1)a + c small enough to avoid overflows that would otherwise mandate the extra complexity of Schrage’s method (above). We can thus easily embed in our programs jran=mod(jran*ia+ic,im) ran=float(jran)/float(im)
whenever we want a quick and dirty uniform deviate, or jran=mod(jran*ia+ic,im) j=jlo+((jhi-jlo+1)*jran)/im
whenever we want an integer between jlo and jhi, inclusive. (In both cases jran was once initialized to any seed value between 0 and im-1.) Be sure to remember, however, that when im is small, the kth root of it, which is the number of planes in k-space, is even smaller! So a quick and dirty generator should never be used to select points in k-space with k > 1. With these caveats, some “good” choices for the constants are given in the accompanying table. These constants (i) give a period of maximal length im, and, more important, (ii) pass Knuth’s “spectral√test” for dimensions 2, 3, 4, 5, and 6. The increment ic is a prime, close to the value ( 12 − 16 3)im; actually almost any value of ic that is relatively prime to im will do just as well, but there is some “lore” favoring this choice (see [4], p. 84).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
mj=ma(ii) enddo 11 do 13 k=1,4 We randomize them by “warming up the generator.” do 12 i=1,55 ma(i)=ma(i)-ma(1+mod(i+30,55)) if(ma(i).lt.MZ)ma(i)=ma(i)+MBIG enddo 12 enddo 13 inext=0 Prepare indices for our first generated number. inextp=31 The constant 31 is special; see Knuth. idum=1 endif inext=inext+1 Here is where we start, except on initialization. Increment if(inext.eq.56)inext=1 inext, wrapping around 56 to 1. inextp=inextp+1 Ditto for inextp. if(inextp.eq.56)inextp=1 mj=ma(inext)-ma(inextp) Now generate a new random number subtractively. if(mj.lt.MZ)mj=mj+MBIG Be sure that it is in range. ma(inext)=mj Store it, ran3=mj*FAC and output the derived uniform deviate. return END
275
7.1 Uniform Deviates
Constants for Quick and Dirty Random Number Generators overflow at
ia
ic
6075
106
1283
7875
211
1663
7875
421
1663
6075 1366 6655 936 11979 430
1283 1399 2531
overflow at
220 221
ia
ic
86436 1093 121500 1021 259200 421
18257 25673 54773
117128 1277 121500 2041 312500 741
24749 25673 66037
145800 175000 233280 244944
30809 36979 49297 51749
227
222 228
223 14406 29282 53125
967 3041 419 6173 171 11213
2
12960 1741 2731 14000 1541 2957 21870 1291 4621 31104 625 6571 139968 205 29573 225
3661 2661 1861 1597
229
24
226
im
139968 3877 29573 214326 3613 45289 714025 1366 150889 230 134456 8121 259200 7141
28411 54773
231 29282 1255 6173 81000 421 17117 134456 281 28411
233280 9301 49297 714025 4096 150889 232
An Even Quicker and Dirtier Generator Many FORTRAN compilers can be abused in such a way that they will multiply two 32-bit integers ignoring any resulting overflow. In such cases, on many machines, the value returned is predictably the low-order 32 bits of the true 64-bit product. (C compilers, incidentally, can do this without the requirement of abuse — it is guaranteed behavior for so-called unsigned long int integers. On VMS VAXes, the necessary FORTRAN command is FORTRAN/CHECK=NOOVERFLOW.) If we now choose m = 232 , the “mod” in equation (7.1.1) is free, and we have simply Ij+1 = aIj + c
(7.1.6)
Knuth suggests a = 1664525 as a suitable multiplier for this value of m. H.W. Lewis has √ conducted extensive tests of this value of a with c = 1013904223, which is a prime close to ( 5 − 2)m. The resulting in-line generator (we will call it ranqd1) is simply idum=1664525*idum+1013904223
This is about as good as any 32-bit linear congruential generator, entirely adequate for many uses. And, with only a single multiply and add, it is very fast. To check whether your compiler and machine have the desired overflow properties, see if you can generate the following sequence of 32-bit values (given here in hex): 00000000, 3C6EF35F, 47502932, D1CCF6E9, AAF95334, 6252E503, 9F2EC686, 57FE6C2D, A3D95FA8, 81FDBEE7, 94F0AF1A, CBF633B1. If you need floating-point values instead of 32-bit integers, and want to avoid a divide by floating-point 232 , a dirty trick is to mask in an exponent that makes the value lie between 1 and 2, then subtract 1.0. The resulting in-line generator (call it ranqd2) will look something like
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
im
276
C
Chapter 7.
Random Numbers
The hex constants 3F800000 and 007FFFFF are the appropriate ones for computers using the IEEE representation for 32-bit floating-point numbers (e.g., IBM PCs and most UNIX workstations). For DEC VAXes, the correct hex constants are, respectively, 00004080 and FFFF007F. Notice that the IEEE mask results in the floating-point number being constructed out of the 23 low-order bits of the integer, which is not ideal. Also notice that your compiler may require a different notation for hex constants, e.g., x’3f800000’, ’3F800000’X, or even 16#3F800000. (Your authors have tried very hard to make almost all of the material in this book machine and compiler independent — indeed, even programming language independent. This subsection is a rare aberration. Forgive us. Once in a great while the temptation to be really dirty is just irresistible.)
Relative Timings and Recommendations Timings are inevitably machine dependent. Nevertheless the following table is indicative of the relative timings, for typical machines, of the various uniform generators discussed in this section, plus ran4 from §7.5. Smaller values in the table indicate faster generators. The generators ranqd1 and ranqd2 refer to the “quick and dirty” generators immediately above.
Generator
Relative Execution Time
ran0
≡ 1.0
ran1 ran2 ran3 ranqd1
≈ 1.3 ≈ 2.0 ≈ 0.6 ≈ 0.10
ranqd2 ran4
≈ 0.25 ≈ 4.0
On balance, we recommend ran1 for general use. It is portable, based on Park and Miller’s Minimal Standard generator with an additional shuffle, and has no known (to us) flaws other than period exhaustion. If you are generating more than 100,000,000 random numbers in a single calculation (that is, more than about 5% of ran1’s period), we recommend the use of ran2, with its much longer period. Knuth’s subtractive routine ran3 seems to be the timing winner among portable routines. Unfortunately the subtractive method is not so well studied, and not a standard. We like to keep ran3 in reserve for a “second opinion,” substituting it when we suspect another generator of introducing unwanted correlations into a calculation. The routine ran4 generates extremely good random deviates, and has some other nice properties, but it is slow. See §7.5 for discussion.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
INTEGER idum,itemp,jflone,jflmsk REAL ftemp EQUIVALENCE (itemp,ftemp) DATA jflone /Z’3F800000’/, jflmsk /Z’007FFFFF’/ ... idum=1664525*idum+1013904223 itemp=ior(jflone,iand(jflmsk,idum)) ran=ftemp-1.0
277
7.2 Transformation Method: Exponential and Normal Deviates
Finally, the quick and dirty in-line generators ranqd1 and ranqd2 are very fast, but they are machine dependent, nonportable, and at best only as good as a 32-bit linear congruential generator ever is — in our view not good enough in many situations. We would use these only in very special cases, where speed is critical.
Park, S.K., and Miller, K.W. 1988, Communications of the ACM, vol. 31, pp. 1192–1201. [1] Schrage, L. 1979, ACM Transactions on Mathematical Software, vol. 5, pp. 132–138. [2] Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: SpringerVerlag). [3] Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §§3.2–3.3. [4] Kahaner, D., Moler, C., and Nash, S. 1989, Numerical Methods and Software (Englewood Cliffs, NJ: Prentice Hall), Chapter 10. [5] L’Ecuyer, P. 1988, Communications of the ACM, vol. 31, pp. 742–774. [6] Forsythe, G.E., Malcolm, M.A., and Moler, C.B. 1977, Computer Methods for Mathematical Computations (Englewood Cliffs, NJ: Prentice-Hall), Chapter 10.
7.2 Transformation Method: Exponential and Normal Deviates In the previous section, we learned how to generate random deviates with a uniform probability distribution, so that the probability of generating a number between x and x + dx, denoted p(x)dx, is given by n p(x)dx =
dx 0 < x < 1 0 otherwise
(7.2.1)
The probability distribution p(x) is of course normalized, so that Z
∞
p(x)dx = 1
(7.2.2)
−∞
Now suppose that we generate a uniform deviate x and then take some prescribed function of it, y(x). The probability distribution of y, denoted p(y)dy, is determined by the fundamental transformation law of probabilities, which is simply
or
|p(y)dy| = |p(x)dx| dx p(y) = p(x) dy
(7.2.3) (7.2.4)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
CITED REFERENCES AND FURTHER READING:
278
Chapter 7.
Random Numbers
1 uniform deviate in
F(y) =⌡⌠0 p(y)dy y
x
0
y transformed deviate out
Figure 7.2.1. Transformation method for generating a random deviate y from a known probability distribution p(y). The indefinite integral of p(y) must be known and invertible. A uniform deviate x is chosen between 0 and 1. Its corresponding y on the definite-integral curve is the desired deviate.
Exponential Deviates As an example, suppose that y(x) ≡ − ln(x), and that p(x) is as given by equation (7.2.1) for a uniform deviate. Then dx p(y)dy = dy = e−y dy dy
(7.2.5)
which is distributed exponentially. This exponential distribution occurs frequently in real problems, usually as the distribution of waiting times between independent Poisson-random events, for example the radioactive decay of nuclei. You can also easily see (from 7.2.4) that the quantity y/λ has the probability distribution λe−λy . So we have
C
1
FUNCTION expdev(idum) INTEGER idum REAL expdev USES ran1 Returns an exponentially distributed, positive, random deviate of unit mean, using ran1(idum) as the source of uniform deviates. REAL dum,ran1 dum=ran1(idum) if(dum.eq.0.)goto 1 expdev=-log(dum) return END
Let’s see what is involved in using the above transformation method to generate some arbitrary desired distribution of y’s, say one with p(y) = f(y) for some positive function f whose integral is 1. (See Figure 7.2.1.) According to (7.2.4), we need to solve the differential equation dx = f(y) dy
(7.2.6)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
p(y)
7.2 Transformation Method: Exponential and Normal Deviates
279
But the solution of this is just x = F (y), where F (y) is the indefinite integral of f(y). The desired transformation which takes a uniform deviate into one distributed as f(y) is therefore y(x) = F −1 (x)
(7.2.7)
Normal (Gaussian) Deviates Transformation methods generalize to more than one dimension. If x1 , x2 , . . . are random deviates with a joint probability distribution p(x1 , x2 , . . .) dx1 dx2 . . . , and if y1 , y2 , . . . are each functions of all the x’s (same number of y’s as x’s), then the joint probability distribution of the y’s is ∂(x1 , x2 , . . .) dy1 dy2 . . . p(y1 , y2 , . . .)dy1 dy2 . . . = p(x1 , x2 , . . .) (7.2.8) ∂(y1 , y2 , . . .) where |∂( )/∂( )| is the Jacobian determinant of the x’s with respect to the y’s (or reciprocal of the Jacobian determinant of the y’s with respect to the x’s). An important example of the use of (7.2.8) is the Box-Muller method for generating random deviates with a normal (Gaussian) distribution, 2 1 p(y)dy = √ e−y /2 dy 2π
(7.2.9)
Consider the transformation between two uniform deviates on (0,1), x1 , x2 , and two quantities y1 , y2 , p y1 = −2 ln x1 cos 2πx2 (7.2.10) p y2 = −2 ln x1 sin 2πx2 Equivalently we can write 1 x1 = exp − (y12 + y22 ) 2 y2 1 arctan x2 = 2π y1 Now the Jacobian determinant can readily be calculated (try it!): ∂x ∂x1 1 2 2 1 1 ∂(x1 , x2) ∂y1 ∂y2 = ∂x2 ∂x2 = − √ e−y1 /2 √ e−y2 /2 ∂(y1 , y2 ) 2π 2π ∂y1
∂y2
(7.2.11)
(7.2.12)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where F −1 is the inverse function to F . Whether (7.2.7) is feasible to implement depends on whether the inverse function of the integral of f(y) is itself feasible to compute, either analytically or numerically. Sometimes it is, and sometimes it isn’t. Incidentally, (7.2.7) has an immediate geometric interpretation: Since F (y) is the area under the probability curve to the left of y, (7.2.7) is just the prescription: choose a uniform random x, then find the value y that has that fraction x of probability area to its left, and return the value y.
280
Chapter 7.
Random Numbers
C
1
FUNCTION gasdev(idum) INTEGER idum REAL gasdev USES ran1 Returns a normally distributed deviate with zero mean and unit variance, using ran1(idum) as the source of uniform deviates. INTEGER iset REAL fac,gset,rsq,v1,v2,ran1 SAVE iset,gset DATA iset/0/ if (idum.lt.0) iset=0 Reinitialize. if (iset.eq.0) then We don’t have an extra deviate handy, so v1=2.*ran1(idum)-1. pick two uniform numbers in the square extendv2=2.*ran1(idum)-1. ing from -1 to +1 in each direction, rsq=v1**2+v2**2 see if they are in the unit circle, if(rsq.ge.1..or.rsq.eq.0.)goto 1 and if they are not, try again. fac=sqrt(-2.*log(rsq)/rsq) Now make the Box-Muller transformation to get gset=v1*fac two normal deviates. Return one and save gasdev=v2*fac the other for next time. iset=1 Set flag. else We have an extra deviate handy, gasdev=gset so return it, iset=0 and unset the flag. endif return END
See Devroye [1] and Bratley [2] for many additional algorithms.
CITED REFERENCES AND FURTHER READING: Devroye, L. 1986, Non-Uniform Random Variate Generation (New York: Springer-Verlag), §9.1. [1] Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: SpringerVerlag). [2] Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), pp. 116ff.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Since this is the product of a function of y2 alone and a function of y1 alone, we see that each y is independently distributed according to the normal distribution (7.2.9). One further trick is useful in applying (7.2.10). Suppose that, instead of picking uniform deviates x1 and x2 in the unit square, we instead pick v1 and v2 as the ordinate and abscissa of a random point inside the unit circle around the origin. Then the sum of their squares, R2 ≡ v12 +v22 is a uniform deviate, which can be used for x1 , while the angle that (v1 , v2 ) defines with respect to the v1 axis can serve as the random angle 2πx2 . What’s √ the advantage? √ It’s that the cosine and sine in (7.2.10) can now be written as v1 / R2 and v2 / R2 , obviating the trigonometric function calls! We thus have
7.3 Rejection Method: Gamma, Poisson, Binomial Deviates
281
7.3 Rejection Method: Gamma, Poisson, Binomial Deviates
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The rejection method is a powerful, general technique for generating random deviates whose distribution function p(x)dx (probability of a value occurring between x and x + dx) is known and computable. The rejection method does not require that the cumulative distribution function [indefinite integral of p(x)] be readily computable, much less the inverse of that function — which was required for the transformation method in the previous section. The rejection method is based on a simple geometrical argument: Draw a graph of the probability distribution p(x) that you wish to generate, so that the area under the curve in any range of x corresponds to the desired probability of generating an x in that range. If we had some way of choosing a random point in two dimensions, with uniform probability in the area under your curve, then the x value of that random point would have the desired distribution. Now, on the same graph, draw any other curve f(x) which has finite (not infinite) area and lies everywhere above your original probability distribution. (This is always possible, because your original curve encloses only unit area, by definition of probability.) We will call this f(x) the comparison function. Imagine now that you have some way of choosing a random point in two dimensions that is uniform in the area under the comparison function. Whenever that point lies outside the area under the original probability distribution, we will reject it and choose another random point. Whenever it lies inside the area under the original probability distribution, we will accept it. It should be obvious that the accepted points are uniform in the accepted area, so that their x values have the desired distribution. It should also be obvious that the fraction of points rejected just depends on the ratio of the area of the comparison function to the area of the probability distribution function, not on the details of shape of either function. For example, a comparison function whose area is less than 2 will reject fewer than half the points, even if it approximates the probability function very badly at some values of x, e.g., remains finite in some region where x is zero. It remains only to suggest how to choose a uniform random point in two dimensions under the comparison function f(x). A variant of the transformation method (§7.2) does nicely: Be sure to have chosen a comparison function whose indefinite integral is known analytically, and is also analytically invertible to give x as a function of “area under the comparison function to the left of x.” Now pick a uniform deviate between 0 and A, where A is the total area under f(x), and use it to get a corresponding x. Then pick a uniform deviate between 0 and f(x) as the y value for the two-dimensional point. You should be able to convince yourself that the point (x, y) is uniformly distributed in the area under the comparison function f(x). An equivalent procedure is to pick the second uniform deviate between zero and one, and accept or reject according to whether it is respectively less than or greater than the ratio p(x)/f(x). So, to summarize, the rejection method for some given p(x) requires that one find, once and for all, some reasonably good comparison function f(x). Thereafter, each deviate generated requires two uniform random deviates, one evaluation of f (to get the coordinate y), and one evaluation of p (to decide whether to accept or reject
282
Chapter 7.
Random Numbers
A first random deviate in ⌠x ⌡0
f (x)dx f (x0 )
accept x0
second random deviate in
p(x) 0
0
x0
Figure 7.3.1. Rejection method for generating a random deviate x from a known probability distribution p(x) that is everywhere less than some other function f (x). The transformation method is first used to generate a random deviate x of the distribution f (compare Figure 7.2.1). A second uniform deviate is used to decide whether to accept or reject that x. If it is rejected, a new deviate of f is found; and so on. The ratio of accepted to rejected points is the ratio of the area under p to the area between p and f .
the point x, y). Figure 7.3.1 illustrates the procedure. Then, of course, this procedure must be repeated, on the average, A times before the final deviate is obtained.
Gamma Distribution The gamma distribution of integer order a > 0 is the waiting time to the ath event in a Poisson random process of unit mean. For example, when a = 1, it is just the exponential distribution of §7.2, the waiting time to the first event. A gamma deviate has probability pa (x)dx of occurring with a value between x and x + dx, where pa (x)dx =
xa−1 e−x dx Γ(a)
x>0
(7.3.1)
To generate deviates of (7.3.1) for small values of a, it is best to add up a exponentially distributed waiting times, i.e., logarithms of uniform deviates. Since the sum of logarithms is the logarithm of the product, one really has only to generate the product of a uniform deviates, then take the log. For larger values of a, the distribution (7.3.1) has √ a typically “bell-shaped” form, with a peak at x = a and a half-width of about a. We will be interested in several probability distributions with this same qualitative form. A useful comparison function in such cases is derived from the Lorentzian distribution 1 1 dy (7.3.2) p(y)dy = π 1 + y2 whose inverse indefinite integral is just the tangent function. It follows that the x-coordinate of an area-uniform random point under the comparison function f(x) =
c0 1 + (x − x0 )2 /a20
(7.3.3)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
reject x0 f (x)
7.3 Rejection Method: Gamma, Poisson, Binomial Deviates
283
for any constants a0 , c0 , and x0 , can be generated by the prescription x = a0 tan(πU ) + x0
(7.3.4)
C
1
FUNCTION gamdev(ia,idum) INTEGER ia,idum REAL gamdev USES ran1 Returns a deviate distributed as a gamma distribution of integer order ia, i.e., a waiting time to the iath event in a Poisson process of unit mean, using ran1(idum) as the source of uniform deviates. INTEGER j REAL am,e,s,v1,v2,x,y,ran1 if(ia.lt.1)pause ’bad argument in gamdev’ if(ia.lt.6)then Use direct method, adding waiting times. x=1. do 11 j=1,ia x=x*ran1(idum) enddo 11 x=-log(x) else Use rejection method. v1=ran1(idum) These four lines generate the tangent of a random angle, i.e., v2=2.*ran1(idum)-1. are equivalent to y = tan(3.14159265 * ran1(idum)). if(v1**2+v2**2.gt.1.)goto 1 y=v2/v1 am=ia-1 s=sqrt(2.*am+1.) x=s*y+am We decide whether to reject x: if(x.le.0.)goto 1 Reject in region of zero probability. e=(1.+y**2)*exp(am*log(x/am)-s*y) Ratio of prob. fn. to comparison fn. if(ran1(idum).gt.e)goto 1 Reject on basis of a second uniform deendif viate. gamdev=x return END
Poisson Deviates The Poisson distribution is conceptually related to the gamma distribution. It gives the probability of a certain integer number m of unit rate Poisson random events occurring in a given interval of time x, while the gamma distribution was the probability of waiting time between x and x + dx to the mth event. Note that m takes on only integer values ≥ 0, so that the Poisson distribution, viewed as a continuous distribution function px(m)dm, is zero everywhere except where m is an integer ≥ 0. At such places, it is infinite, such that the integrated probability over a region containing the integer is some finite number. The total probability at an integer j is Z j+ xj e−x (7.3.5) px (m)dm = Prob(j) = j! j−
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where U is a uniform deviate between 0 and 1. Thus, for some specific “bell-shaped” p(x) probability distribution, we need only find constants a0 , c0 , x0 , with the product a0 c0 (which determines the area) as small as possible, such that (7.3.3) is everywhere greater than p(x). Ahrens has done this for the gamma distribution, yielding the following algorithm (as described in Knuth [1]):
284
Chapter 7.
Random Numbers
1 in
accept
0
1
2
3
4
5
Figure 7.3.2. Rejection method as applied to an integer-valued distribution. The method is performed on the step function shown as a dashed line, yielding a real-valued deviate. This deviate is rounded down to the next lower integer, which is output.
At first sight this might seem an unlikely candidate distribution for the rejection method, since no continuous comparison function can be larger than the infinitely tall, but infinitely narrow, Dirac delta functions in px (m). However, there is a trick that we can do: Spread the finite area in the spike at j uniformly into the interval between j and j + 1. This defines a continuous distribution qx (m)dm given by qx (m)dm =
x[m] e−x dm [m]!
(7.3.6)
where [m] represents the largest integer less than m. If we now use the rejection method to generate a (noninteger) deviate from (7.3.6), and then take the integer part of that deviate, it will be as if drawn from the desired distribution (7.3.5). (See Figure 7.3.2.) This trick is general for any integer-valued probability distribution. For x large enough, the distribution (7.3.6) is qualitatively bell-shaped (albeit with a bell made out of small, square steps), and we can use the same kind of Lorentzian comparison function as was already used above. For small x, we can generate independent exponential deviates (waiting times between events); when the sum of these first exceeds x, then the number of events that would have occurred in waiting time x becomes known and is one less than the number of terms in the sum. These ideas produce the following routine:
C
FUNCTION poidev(xm,idum) INTEGER idum REAL poidev,xm,PI PARAMETER (PI=3.141592654) USES gammln,ran1 Returns as a floating-point number an integer value that is a random deviate drawn from a Poisson distribution of mean xm, using ran1(idum) as a source of uniform random deviates.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
reject
7.3 Rejection Method: Gamma, Poisson, Binomial Deviates
1
REAL alxm,em,g,oldm,sq,t,y,gammln,ran1 SAVE alxm,g,oldm,sq DATA oldm /-1./ Flag for whether xm has changed since last call. if (xm.lt.12.)then Use direct method. if (xm.ne.oldm) then oldm=xm g=exp(-xm) If xm is new, compute the exponential. endif em=-1 t=1. em=em+1. Instead of adding exponential deviates it is equivalent to mult=t*ran1(idum) tiply uniform deviates. We never actually have to take the if (t.gt.g) goto 2 log, merely compare to the pre-computed exponential. else Use rejection method. if (xm.ne.oldm) then If xm has changed since the last call, then precompute some oldm=xm functions that occur below. sq=sqrt(2.*xm) alxm=log(xm) g=xm*alxm-gammln(xm+1.) The function gammln is the natural log of the gamma endif function, as given in §6.1. y=tan(PI*ran1(idum)) y is a deviate from a Lorentzian comparison function. em=sq*y+xm em is y, shifted and scaled. if (em.lt.0.) goto 1 Reject if in regime of zero probability. em=int(em) The trick for integer-valued distributions. t=0.9*(1.+y**2)*exp(em*alxm-gammln(em+1.)-g) The ratio of the desired distribuif (ran1(idum).gt.t) goto 1 tion to the comparison function; we accept or reendif ject by comparing it to another uniform deviate. poidev=em The factor 0.9 is chosen so that t never exceeds return 1. END
Binomial Deviates If an event occurs with probability q, and we make n trials, then the number of times m that it occurs has the binomial distribution, Z
j+
pn,q (m)dm = j−
n j q (1 − q)n−j j
(7.3.7)
The binomial distribution is integer valued, with m taking on possible values from 0 to n. It depends on two parameters, n and q, so is correspondingly a bit harder to implement than our previous examples. Nevertheless, the techniques already illustrated are sufficiently powerful to do the job:
C
FUNCTION bnldev(pp,n,idum) INTEGER idum,n REAL bnldev,pp,PI USES gammln,ran1 PARAMETER (PI=3.141592654) Returns as a floating-point number an integer value that is a random deviate drawn from a binomial distribution of n trials each of probability pp, using ran1(idum) as a source of uniform random deviates. INTEGER j,nold REAL am,em,en,g,oldg,p,pc,pclog,plog,pold,sq,t,y,gammln,ran1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
285
286
2
*
Random Numbers
SAVE nold,pold,pc,plog,pclog,en,oldg DATA nold /-1/, pold /-1./ Arguments from previous calls. if(pp.le.0.5)then The binomial distribution is invariant under changing pp to p=pp 1.-pp, if we also change the answer to n minus itself; else we’ll remember to do this below. p=1.-pp endif am=n*p This is the mean of the deviate to be produced. if (n.lt.25)then Use the direct method while n is not too large. This can bnldev=0. require up to 25 calls to ran1. do 11 j=1,n if(ran1(idum).lt.p)bnldev=bnldev+1. enddo 11 else if (am.lt.1.) then If fewer than one event is expected out of 25 or more trig=exp(-am) als, then the distribution is quite accurately Poisson. Use t=1. direct Poisson method. do 12 j=0,n t=t*ran1(idum) if (t.lt.g) goto 1 enddo 12 j=n bnldev=j else Use the rejection method. if (n.ne.nold) then If n has changed, then compute useful quantities. en=n oldg=gammln(en+1.) nold=n endif if (p.ne.pold) then If p has changed, then compute useful quantities. pc=1.-p plog=log(p) pclog=log(pc) pold=p endif sq=sqrt(2.*am*pc) The following code should by now seem familiar: rejection y=tan(PI*ran1(idum)) method with a Lorentzian comparison function. em=sq*y+am if (em.lt.0..or.em.ge.en+1.) goto 2 Reject. em=int(em) Trick for integer-valued distribution. t=1.2*sq*(1.+y**2)*exp(oldg-gammln(em+1.) -gammln(en-em+1.)+em*plog+(en-em)*pclog) if (ran1(idum).gt.t) goto 2 Reject. This happens about 1.5 times per deviate, on bnldev=em average. endif if (p.ne.pp) bnldev=n-bnldev Remember to undo the symmetry transformation. return END
See Devroye [2] and Bratley [3] for many additional algorithms. CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), pp. 120ff. [1] Devroye, L. 1986, Non-Uniform Random Variate Generation (New York: Springer-Verlag), §X.4. [2] Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: SpringerVerlag). [3].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 7.
7.4 Generation of Random Bits
287
7.4 Generation of Random Bits
x18 + x5 + x2 + x1 + x0
(7.4.1)
which we can abbreviate by just writing the nonzero powers of x, e.g., (18, 5, 2, 1, 0) Every primitive polynomial modulo 2 of order n (=18 above) defines a recurrence relation for obtaining a new random bit from the n preceding ones. The recurrence relation is guaranteed to produce a sequence of maximal length, i.e., cycle through all possible sequences of n bits (except all zeros) before it repeats. Therefore one can seed the sequence with any initial bit pattern (except all zeros), and get 2n − 1 random bits before the sequence repeats. Let the bits be numbered from 1 (most recently generated) through n (generated n steps ago), and denoted a1 , a2 , . . . , an . We want to give a formula for a new bit a0 . After generating a0 we will shift all the bits by one, so that the old an is finally lost, and the new a0 becomes a1 . We then apply the formula again, and so on. “Method I” is the easiest to implement in hardware, requiring only a single shift register n bits long and a few XOR (“exclusive or” or bit addition mod 2) gates. For the primitive polynomial given above, the recurrence formula is a0 = a18 XOR a5 XOR a2 XOR a1
(7.4.2)
The terms that are XOR’d together can be thought of as “taps” on the shift register, XOR’d into the register’s input. More generally, there is precisely one term for each nonzero coefficient in the primitive polynomial except the constant (zero bit) term. So the first term will always be an for a primitive polynomial of degree n, while the last term might or might not be a1 , depending on whether the primitive polynomial has a term in x1 . It is rather cumbersome to illustrate the method in FORTRAN. Assume that iand is a bitwise AND function, not is bitwise complement, ishft( ,1) is leftshift by one bit, ior is bitwise OR. (These are available in many FORTRAN implementations.) Then we have the following routine.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
This topic is not very useful for programming in high-level languages, but it can be quite useful when you have access to the machine-language level of a machine or when you are in a position to build special-purpose hardware out of readily available chips. The problem is how to generate single random bits, with 0 and 1 equally probable. Of course you can just generate uniform random deviates between zero and one and use their high-order bit (i.e., test if they are greater than or less than 0.5). However this takes a lot of arithmetic; there are special-purpose applications, such as real-time signal processing, where you want to generate bits very much faster than that. One method for generating random bits, with two variant implementations, is based on “primitive polynomials modulo 2.” The theory of these polynomials is beyond our scope (although §7.7 and §20.3 will give you small tastes of it). Here, suffice it to say that there are special polynomials among those whose coefficients are zero or one. An example is
288
Chapter 7.
18
17
5
4
Random Numbers
3
2
1
0 shift left
18
17
5
4
3
2
1
0 shift left
(b) Figure 7.4.1. Two related methods for obtaining random bits from a shift register and a primitive polynomial modulo 2. (a) The contents of selected taps are combined by exclusive-or (addition modulo 2), and the result is shifted in from the right. This method is easiest to implement in hardware. (b) Selected bits are modified by exclusive-or with the leftmost bit, which is then shifted in from the right. This method is easiest to implement in software. FUNCTION irbit1(iseed) INTEGER irbit1,iseed,IB1,IB2,IB5,IB18 PARAMETER (IB1=1,IB2=2,IB5=16,IB18=131072) Powers of 2. Returns as an integer a random bit, based on the 18 low-significance bits in iseed (which is modified for the next call). LOGICAL newbit The accumulated XOR’s. newbit=iand(iseed,IB18).ne.0 Get bit 18. if(iand(iseed,IB5).ne.0)newbit=.not.newbit XOR with bit 5. if(iand(iseed,IB2).ne.0)newbit=.not.newbit XOR with bit 2. if(iand(iseed,IB1).ne.0)newbit=.not.newbit XOR with bit 1. irbit1=0 iseed=iand(ishft(iseed,1),not(IB1)) Leftshift the seed and put a zero in its bit 1. if(newbit)then But if the XOR calculation gave a 1, irbit1=1 iseed=ior(iseed,IB1) then put that in bit 1 instead. endif return END
“Method II” is less suited to direct hardware implementation (though still possible), but is more suited to machine-language implementation. It modifies more than one bit among the saved n bits as each new bit is generated (Figure 7.4.1). It generates the maximal length sequence, but not in the same order as Method I. The prescription for the primitive polynomial (7.4.1) is: a0 = a18 a5 = a5 XOR a0 a2 = a2 XOR a0 a1 = a1 XOR a0
(7.4.3)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
(a)
289
7.4 Generation of Random Bits
Some Primitive Polynomials Modulo 2 (after Watson) 0) 1, 1, 1, 2, 1, 1, 4, 4, 3, 2, 6, 4, 5, 1, 5, 3, 5, 5, 3, 2, 1, 5, 4, 3, 6, 5, 3, 2, 6, 3, 7, 6, 7, 2, 6, 5, 6, 4, 5, 3, 5, 6, 6, 4, 8, 5, 7, 6, 4,
0) 0) 0) 0) 0) 0) 3, 0) 0) 0) 4, 3, 3, 0) 3, 0) 2, 2, 0) 0) 0) 0) 3, 0) 2, 2, 0) 0) 4, 0) 5, 4, 6, 0) 5, 4, 5, 0) 4 0) 4, 4, 5, 3, 5, 0) 5, 5, 3,
2, 0)
1, 0) 1, 0) 1, 0) 2, 0) 1, 0) 1, 0)
1, 0) 1, 0) 1, 0) 1, 0) 3, 2, 1, 0) 1, 0) 5, 2, 1, 0) 4, 2, 1, 0) 3, 2, 1, 0) 1, 0) 3, 0) 3, 3, 2, 1, 3,
2, 1, 0) 0) 0) 0) 2, 1, 0)
4, 2, 1, 0) 4, 0) 2, 0)
(51, (52, (53, (54, (55, (56, (57, (58, (59, (60, (61, (62, (63, (64, (65, (66, (67, (68, (69, (70, (71, (72, (73, (74, (75, (76, (77, (78, (79, (80, (81, (82, (83, (84, (85, (86, (87, (88, (89, (90, (91, (92, (93, (94, (95, (96, (97, (98, (99, (100,
6, 3, 3, 0) 6, 2, 6, 5, 6, 2, 7, 4, 5, 3, 6, 5, 6, 5, 1, 0) 5, 2, 6, 5, 1, 0) 4, 3, 4, 3, 8, 6, 5, 2, 7, 5, 6, 5, 5, 3, 5, 3, 6, 4, 4, 3, 7, 4, 6, 3, 5, 4, 6, 5, 7, 2, 4, 3, 7, 5, 4 0) 8, 7, 7, 4, 8, 7, 8, 2, 6, 5, 7, 5, 8, 5, 6, 5, 5, 3, 7, 6, 6, 5, 2, 0) 6, 5, 6, 5, 7, 6, 6, 0) 7, 4, 7, 5, 8, 7,
1, 0) 1, 4, 1, 2, 2, 1, 4,
0) 3, 2, 0) 0) 0) 0) 0) 3, 1, 0)
1, 0) 3, 0) 1, 1, 5, 1, 1, 2, 1, 1, 3, 2, 3, 1, 2, 2, 1, 2, 3,
0) 0) 3, 2, 0) 0) 0) 0) 0) 0) 2, 1, 0) 0) 0) 0) 0) 0) 0) 0) 2, 1, 0)
6, 2, 5, 1, 2, 1, 4, 3, 2, 5, 2,
4, 0) 3, 0) 0) 0) 3, 0) 0) 3, 0)
1, 0) 1, 0)
1, 0) 2, 0)
1, 0) 4, 2, 1, 0) 4, 3, 2, 0) 3, 2, 1, 0) 4, 0) 2, 0)
In general there will be an exclusive-or for each nonzero term in the primitive polynomial except 0 and n. The nice feature about Method II is that all the exclusive-or’s can usually be done as a single masked word XOR (here assumed to be the FORTRAN function ieor):
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
(1, (2, (3, (4, (5, (6, (7, (8, (9, (10, (11, (12, (13, (14, (15, (16, (17, (18, (19, (20, (21, (22, (23, (24, (25, (26, (27, (28, (29, (30, (31, (32, (33, (34, (35, (36, (37, (38, (39, (40, (41, (42, (43, (44, (45, (46, (47, (48, (49, (50,
290
Chapter 7.
Random Numbers
A word of caution is: Don’t use sequential bits from these routines as the bits of a large, supposedly random, integer, or as the bits in the mantissa of a supposedly random floating-point number. They are not very random for that purpose; see Knuth [1]. Examples of acceptable uses of these random bits are: (i) multiplying a signal randomly by ±1 at a rapid “chip rate,” so as to spread its spectrum uniformly (but recoverably) across some desired bandpass, or (ii) Monte Carlo exploration of a binary tree, where decisions as to whether to branch left or right are to be made randomly. Now we do not want you to go through life thinking that there is something special about the primitive polynomial of degree 18 used in the above examples. (We chose 18 because 218 is small enough for you to verify our claims directly by numerical experiment.) The accompanying table [2] lists one primitive polynomial for each degree up to 100. (In fact there exist many such for each degree. For example, see §7.7 for a complete table up to degree 10.) CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), pp. 29ff. [1] Horowitz, P., and Hill, W. 1989, The Art of Electronics, 2nd ed. (Cambridge: Cambridge University Press), §§9.32–9.37. Tausworthe, R.C. 1965, Mathematics of Computation, vol. 19, pp. 201–209. Watson, E.J. 1962, Mathematics of Computation, vol. 16, pp. 368–369. [2]
7.5 Random Sequences Based on Data Encryption In Numerical Recipes’ first edition,we described how to use the Data Encryption Standard (DES) [1-3] for the generation of random numbers. Unfortunately, when implemented in software in a high-level language like FORTRAN, DES is very slow, so excruciatingly slow, in fact, that our previous implementation can be viewed as more mischievous than useful. Here we give a much faster and simpler algorithm which, though it may not be secure in the cryptographic sense, generates about equally good random numbers. DES, like its progenitor cryptographic system LUCIFER, is a so-called “block product cipher” [4]. It acts on 64 bits of input by iteratively applying (16 times, in fact) a kind of highly
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
FUNCTION irbit2(iseed) INTEGER irbit2,iseed,IB1,IB2,IB5,IB18,MASK PARAMETER (IB1=1,IB2=2,IB5=16,IB18=131072,MASK=IB1+IB2+IB5) Returns as an integer a random bit, based on the 18 low-significance bits in iseed (which is modified for the next call). if(iand(iseed,IB18).ne.0)then Change all masked bits, shift, and put 1 into bit 1. iseed=ior(ishft(ieor(iseed,MASK),1),IB1) irbit2=1 else Shift and put 0 into bit 1. iseed=iand(ishft(iseed,1),not(IB1)) irbit2=0 endif return END
7.5 Random Sequences Based on Data Encryption
left 32-bit word
291
right 32-bit word
g
left 32-bit word
right 32-bit word
g
32-bit XOR
left 32-bit word
right 32-bit word
Figure 7.5.1. The Data Encryption Standard (DES) iterates a nonlinear function g on two 32-bit words, in the manner shown here (after Meyer and Matyas [4]).
nonlinear bit-mixing function. Figure 7.5.1 shows the flow of information in DES during this mixing. The function g, which takes 32-bits into 32-bits, is called the “cipher function.” Meyer and Matyas [4] discuss the importance of the cipher function being nonlinear, as well as other design criteria. DES constructs its cipher function g from an intricate set of bit permutations and table lookups acting on short sequences of consecutive bits. Apparently, this function was chosen to be particularly strong cryptographically (or conceivably as some critics contend, to have an exquisitely subtle cryptographic flaw!). For our purposes, a different function g that can be rapidly computed in a high-level computer language is preferable. Such a function may weaken the algorithm cryptographically. Our purposes are not, however, cryptographic: We want to find the fastest g, and smallest number of iterations of the mixing procedure in Figure 7.5.1, such that our output random sequence passes the standard tests that are customarily applied to random number generators. The resulting algorithm will not be DES, but rather a kind of “pseudo-DES,” better suited to the purpose at hand. Following the criterion, mentioned above, that g should be nonlinear, we must give the integer multiply operation a prominent place in g. Because 64-bit registers are not generally accessible in high-level languages, we must confine ourselves to multiplying 16-bit operands into a 32-bit result. So, the general idea of g, almost forced, is to calculate the three distinct 32-bit products of the high and low 16-bit input half-words, and then to combine these, and perhaps additional fixed constants, by fast operations (e.g., add or exclusive-or) into a single 32-bit result. There are only a limited number of ways of effecting this general scheme, allowing systematic exploration of the alternatives. Experimentation, and tests of the randomness of the output, lead to the sequence of operations shown in Figure 7.5.2. The few new elements in the figure need explanation: The values C1 and C2 are fixed constants, chosen randomly with the constraint that they have exactly 16 1-bits and 16 0-bits; combining these constants
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
32-bit XOR
292
Chapter 7.
C1
Random Numbers
XOR
lo 2
hi • lo
NOT
+ reverse half-words
C2
XOR
+ Figure 7.5.2.
The nonlinear function g used by the routine psdes.
via exclusive-or ensures that the overall g has no bias towards 0 or 1 bits. The “reverse half-words” operation in Figure 7.5.2 turns out to be essential; otherwise, the very lowest and very highest bits are not properly mixed by the three multiplications. The nonobvious choices in g are therefore: where along the vertical “pipeline” to do the reverse; in what order to combine the three products and C2 ; and with which operation (add or exclusive-or) should each combining be done? We tested these choices exhaustively before settling on the algorithm shown in the figure. It remains to determine the smallest number of iterations Nit that we can get away with. The minimum meaningful Nit is evidently two, since a single iteration simply moves one 32-bit word without altering it. One can use the constants C1 and C2 to help determine an appropriate Nit : When Nit = 2 and C1 = C2 = 0 (an intentionally very poor choice), the generator fails several tests of randomness by easily measurable, though not overwhelming, amounts. When Nit = 4, on the other hand, or with Nit = 2 but with the constants C1 , C2 nonsparse, we have been unable to find any statistical deviation from randomness in sequences of up to 109 floating numbers ri derived from this scheme. The combined strength of Nit = 4 and nonsparse C1 , C2 should therefore give sequences that are random to tests even far beyond those that we have actually tried. These are our recommended conservative parameter values, notwithstanding the fact that Nit = 2 (which is, of course, twice as fast) has no nonrandomness discernible (by us). We turn now to implementation. The nonlinear function shown in Figure 7.5.2 is not implementable in strictly portable FORTRAN, for at least three reasons: (1) The addition of two 32-bit integers may overflow, and the multiplication of two 16-bit integers may not produce the correct 32-bit product because of sign-bit conventions. We intend that the overflow be ignored, and that the 16-bit integers be multiplied as if they are positive. It is possible to force this behavior on most machines. (2) We assume 32-bit integers; however, there
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
hi 2
7.5 Random Sequences Based on Data Encryption
293
* *
SUBROUTINE psdes(lword,irword) INTEGER irword,lword,NITER PARAMETER (NITER=4) “Pseudo-DES” hashing of the 64-bit word (lword,irword). Both 32-bit arguments are returned hashed on all bits. NOTE: This routine assumes that arbitrary 32-bit integers can be added without overflow. To accomplish this, you may need to compile with a special directive (e.g., /check=nooverflow for VMS). In other languages, such as C, one can instead type the integers as “unsigned.” INTEGER i,ia,ib,iswap,itmph,itmpl,c1(4),c2(4) SAVE c1,c2 DATA c1 /Z’BAA96887’,Z’1E17D32C’,Z’03BCDC3C’, Your compiler may use a differZ’0F33D1B2’/, c2 /Z’4B0F3B58’,Z’E874F0C3’, ent notation for hex constants! Z’6955C5A6’, Z’55A7CA46’/ do 11 i=1,NITER Perform niter iterations of DES logic, using a simpler (noniswap=irword cryptographic) nonlinear function instead of DES’s. ia=ieor(irword,c1(i)) The bit-rich constants c1 and (below) c2 guarantee lots of itmpl=iand(ia,65535) nonlinear mixing. itmph=iand(ishft(ia,-16),65535) ib=itmpl**2+not(itmph**2) ia=ior(ishft(ib,16),iand(ishft(ib,-16),65535)) irword=ieor(lword,ieor(c2(i),ia)+itmpl*itmph) lword=iswap enddo 11 return END
The routine ran4, listed below, uses psdes to generate uniform random deviates. We adopt the convention that a negative value of the argument idum sets the left 32-bit word, while a positive value i sets the right 32-bit word, returns the ith random deviate, and increments idum to i + 1. This is no more than a convenient way of defining many different sequences (negative values of idum), but still with random access to each sequence (positive values of idum). For getting a floating-point number from the 32-bit integer, we like to do it by the masking trick described at the end of §7.1, above. The hex constants 3F800000 and 007FFFFF are the appropriate ones for computers using the IEEE representation for 32-bit floating-point numbers (e.g., IBM PCs and most UNIX workstations). For DEC VAXes, the correct hex constants are, respectively, 00004080 and FFFF007F. Note that your compiler may require a different notation for hex constants, e.g., x’3f800000’, ’3F800000’X, or even 16#3F800000. For greater portability, you can instead construct a floating number by making the (signed) 32-bit integer nonnegative (typically, you add exactly 231 if it is negative) and then multiplying it by a floating constant (typically 2.−31 ). An interesting, and sometimes useful, feature of the routine ran4, below, is that it allows random access to the nth random value in a sequence, without the necessity of first generating values 1 · · · n − 1. This property is shared by any random number generator based on hashing (the technique of mapping data keys, which may be highly clustered in value, approximately uniformly into a storage address space) [5,6] . One might have a simulation problem in which some certain rare situation becomes recognizable by its consequences only considerably after it has occurred. One may wish to restart the simulation back at that occurrence, using identical random values but, say, varying some other control parameters. The relevant question might then be something like “what random numbers were used in cycle number 337098901?” It might already be cycle number 395100273 before the question comes up. Random generators based on recursion, rather than hashing, cannot easily answer such a question.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
is no reason to believe that longer integers would be in any way inferior (with suitable extensions of the constants C1 , C2 ). (3) Your compiler may require a different notation for hex constants (see below). We have been able to run the following routine, psdes, successfully on machines ranging from PCs to VAXes and both “big-endian” and “little-endian” UNIX workstations. (Big- and little-endian refer to the order in which the bytes are stored in a word.) A strictly portable implementation is possible in C. If all else fails, you can make a FORTRAN-callable version of the C routine, found in Numerical Recipes in C.
294
Chapter 7.
Random Numbers
Values for Verifying the Implementation of psdes idum
before psdes call
after psdes call (hex)
ran4(idum)
irword
lword
irword
VAX
PC
–1
1
1
604D1DCE
509C0C23
0.275898
0.219120
99
1
99
D97F8571
A66CB41A
0.208204
0.849246
–99
99
1
7822309D
64300984
0.034307
0.375290
99
99
99
D7F376F0
59BA89EB
0.838676
0.457334
Successive calls to psdes with arguments −1, 99, −99, and 1, should produce exactly the lword and irword values shown. Masking conversion to a returned floating random value is allowed to be machine dependent; values for VAX and PC are shown.
C
FUNCTION ran4(idum) INTEGER idum REAL ran4 USES psdes Returns a uniform random deviate in the range 0.0 to 1.0, generated by pseudo-DES (DESlike) hashing of the 64-bit word (idums,idum), where idums was set by a previous call with negative idum. Also increments idum. Routine can be used to generate a random sequence by successive calls, leaving idum unaltered between calls; or it can randomly access the nth deviate in a sequence by calling with idum = n. Different sequences are initialized by calls with differing negative values of idum. INTEGER idums,irword,itemp,jflmsk,jflone,lword REAL ftemp EQUIVALENCE (itemp,ftemp) SAVE idums,jflone,jflmsk DATA idums /0/, jflone /Z’3F800000’/, jflmsk /Z’007FFFFF’/ The hexadecimal constants jflone and jflmsk are used to produce a floating number between 1. and 2. by bitwise masking. They are machine-dependent. See text. if(idum.lt.0)then Reset idums and prepare to return the first deviidums=-idum ate in its sequence. idum=1 endif irword=idum lword=idums call psdes(lword,irword) “Pseudo-DES” encode the words. itemp=ior(jflone,iand(jflmsk,irword)) Mask to a floating number between 1 and 2. ran4=ftemp-1.0 Subtraction moves range to 0. to 1. idum=idum+1 return END
The accompanying table gives data for verifying that ran4 and psdes work correctly on your machine. We do not advise the use of ran4 unless you are able to reproduce the hex values shown. Typically, ran4 is about 4 times slower than ran0 (§7.1), or about 3 times slower than ran1.
CITED REFERENCES AND FURTHER READING: Data Encryption Standard, 1977 January 15, Federal Information Processing Standards Publication, number 46 (Washington: U.S. Department of Commerce, National Bureau of Standards). [1]
Guidelines for Implementing and Using the NBS Data Encryption Standard, 1981 April 1, Federal Information Processing Standards Publication, number 74 (Washington: U.S. Department of Commerce, National Bureau of Standards). [2]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
lword
295
7.6 Simple Monte Carlo Integration
7.6 Simple Monte Carlo Integration Inspirations for numerical methods can spring from unlikely sources. “Splines” first were flexible strips of wood used by draftsmen. “Simulated annealing” (we shall see in §10.9) is rooted in a thermodynamic analogy. And who does not feel at least a faint echo of glamor in the name “Monte Carlo method”? Suppose that we pick N random points, uniformly distributed in a multidimensional volume V . Call them x1 , . . . , xN . Then the basic theorem of Monte Carlo integration estimates the integral of a function f over the multidimensional volume, s
Z f dV ≈ V hfi ± V
hf 2 i − hfi N
2
(7.6.1)
Here the angle brackets denote taking the arithmetic mean over the N sample points, hfi ≡
N 1 X f(xi ) N i=1
N 1 X 2 f2 ≡ f (xi ) N
(7.6.2)
i=1
The “plus-or-minus” term in (7.6.1) is a one standard deviation error estimate for the integral, not a rigorous bound; further, there is no guarantee that the error is distributed as a Gaussian, so the error term should be taken only as a rough indication of probable error. Suppose that you want to integrate a function g over a region W that is not easy to sample randomly. For example, W might have a very complicated shape. No problem. Just find a region V that includes W and that can easily be sampled (Figure 7.6.1), and then define f to be equal to g for points in W and equal to zero for points outside of W (but still inside the sampled V ). You want to try to make V enclose W as closely as possible, because the zero values of f will increase the error estimate term of (7.6.1). And well they should: points chosen outside of W have no information content, so the effective value of N , the number of points, is reduced. The error estimate in (7.6.1) takes this into account. General purpose routines for Monte Carlo integration are quite complicated (see §7.8), but a worked example will show the underlying simplicity of the method. Suppose that we want to find the weight and the position of the center of mass of an
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Validating the Correctness of Hardware Implementations of the NBS Data Encryption Standard, 1980, NBS Special Publication 500–20 (Washington: U.S. Department of Commerce, National Bureau of Standards). [3] Meyer, C.H. and Matyas, S.M. 1982, Cryptography: A New Dimension in Computer Data Security (New York: Wiley). [4] Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading, MA: Addison-Wesley), Chapter 6. [5] Vitter, J.S., and Chen, W-C. 1987, Design and Analysis of Coalesced Hashing (New York: Oxford University Press). [6]
Random Numbers Chapter 7.
296
area A
Figure 7.6.1. Monte Carlo integration. Random points are chosen within the area A. The integral of the function f is estimated as the area of A multiplied by the fraction of random points that fall below the curve f . Refinements on this procedure can improve the accuracy of the method; see text.
y
4
2
0
1
2
4
x
Figure 7.6.2. Example of Monte Carlo integration (see text). The region of interest is a piece of a torus, bounded by the intersection of two planes. The limits of integration of the region cannot easily be written in analytically closed form, so Monte Carlo is a useful technique.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
∫fdx
7.6 Simple Monte Carlo Integration
297
object of complicated shape, namely the intersection of a torus with the edge of a large box. In particular let the object be defined by the three simultaneous conditions z2 +
p
x2 + y 2 − 3
2
≤1
(7.6.3)
x≥1
y ≥ −3
(7.6.4)
(two faces of the box, see Figure 7.6.2). Suppose for the moment that the object has a constant density ρ. We want to estimate the following integrals over the interior of the complicated object: Z Z Z Z ρ dx dy dz xρ dx dy dz yρ dx dy dz zρ dx dy dz (7.6.5) The coordinates of the center of mass will be the ratio of the latter three integrals (linear moments) to the first one (the weight). In the following fragment, the region V , enclosing the piece-of-torus W , is the rectangular box extending from 1 to 4 in x, −3 to 4 in y, and −1 to 1 in z. n= Set to the number of sample points desired. den= Set to the constant value of the density. sw=0. Zero the various sums to be accumulated. swx=0. swy=0. swz=0. varw=0. varx=0. vary=0. varz=0. vol=3.*7.*2. Volume of the sampled region. do 11 j=1,n x=1.+3.*ran2(idum) Pick a point randomly in the sampled region. y=-3.+7.*ran2(idum) z=-1.+2.*ran2(idum) if (z**2+(sqrt(x**2+y**2)-3.)**2.le.1.)then Is it in the torus? sw=sw+den If so, add to the various cumulants. swx=swx+x*den swy=swy+y*den swz=swz+z*den varw=varw+den**2 varx=varx+(x*den)**2 vary=vary+(y*den)**2 varz=varz+(z*den)**2 endif enddo 11 w=vol*sw/n The values of the integrals (7.6.5), x=vol*swx/n y=vol*swy/n z=vol*swz/n dw=vol*sqrt((varw/n-(sw/n)**2)/n) and their corresponding error estimates. dx=vol*sqrt((varx/n-(swx/n)**2)/n) dy=vol*sqrt((vary/n-(swy/n)**2)/n) dz=vol*sqrt((varz/n-(swz/n)**2)/n)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
(torus centered on the origin with major radius = 4, minor radius = 2)
298
Chapter 7.
Random Numbers
A change of variable can often be extremely worthwhile in Monte Carlo integration. Suppose, for example, that we want to evaluate the same integrals, but for a piece-of-torus whose density is a strong function of z, in fact varying according to ρ(x, y, z) = e5z
(7.6.6)
den=exp(5.*z)
inside the if...then block, just before den is first used. This will work, but it is a poor way to proceed. Since (7.6.6) falls so rapidly to zero as z decreases (down to its lower limit −1), most sampled points contribute almost nothing to the sum of the weight or moments. These points are effectively wasted, almost as badly as those that fall outside of the region W . A change of variable, exactly as in the transformation methods of §7.2, solves this problem. Let 1 1 (7.6.7) ds = e5z dz so that s = e5z , z = ln(5s) 5 5 Then ρdz = ds, and the limits −1 < z < 1 become .00135 < s < 29.682. The program fragment now looks like this n= Set to the number of sample points desired. sw=0. swx=0. swy=0. swz=0. varw=0. varx=0. vary=0. varz=0. ss=(0.2*(exp(5.)-exp(-5.))) Interval of s to be random sampled. vol=3.*7.*ss Volume in x,y,s-space. do 11 j=1,n x=1.+3.*ran2(idum) y=-3.+7.*ran2(idum) s=.00135+ss*ran2(idum) Pick a point in s. z=0.2*log(5.*s) Equation (7.6.7). if (z**2+(sqrt(x**2+y**2)-3.)**2.lt.1.)then sw=sw+1. Density is 1, since absorbed into definition of s. swx=swx+x swy=swy+y swz=swz+z varw=varw+1. varx=varx+x**2 vary=vary+y**2 varz=varz+z**2 endif enddo 11 w=vol*sw/n The values of the integrals (7.6.5), x=vol*swx/n y=vol*swy/n z=vol*swz/n dw=vol*sqrt((varw/n-(sw/n)**2)/n) and their corresponding error estimates. dx=vol*sqrt((varx/n-(swx/n)**2)/n) dy=vol*sqrt((vary/n-(swy/n)**2)/n) dz=vol*sqrt((varz/n-(swz/n)**2)/n)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
One way to do this is to put the statement
7.7 Quasi- (that is, Sub-) Random Sequences
299
CITED REFERENCES AND FURTHER READING: Hammersley, J.M., and Handscomb, D.C. 1964, Monte Carlo Methods (London: Methuen). Shreider, Yu. A. (ed.) 1966, The Monte Carlo Method (Oxford: Pergamon). Sobol’, I.M. 1974, The Monte Carlo Method (Chicago: University of Chicago Press). Kalos, M.H., and Whitlock, P.A. 1986, Monte Carlo Methods (New York: Wiley).
7.7 Quasi- (that is, Sub-) Random Sequences We have just seen that choosing N points uniformly randomly in an ndimensional space leads to an error term in Monte Carlo integration that decreases √ as 1/ N . In essence, each new point sampled adds linearly to an accumulated sum that will become the function average, and also linearly to an accumulated sum of squares that will become the variance (equation 7.6.2). The estimated error comes from the square root of this variance, hence the power N −1/2 . Just because this square root convergence is familiar does not, however, mean that it is inevitable. A simple counterexample is to choose sample points that lie on a Cartesian grid, and to sample each grid point exactly once (in whatever order). The Monte Carlo method thus becomes a deterministic quadrature scheme — albeit a simple one — whose fractional error decreases at least as fast as N −1 (even faster if the function goes to zero smoothly at the boundaries of the sampled region, or is periodic in the region).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
If you think for a minute, you will realize that equation (7.6.7) was useful only because the part of the integrand that we wanted to eliminate (e5z ) was both integrable analytically, and had an integral that could be analytically inverted. (Compare §7.2.) In general these properties will not hold. Question: What then? Answer: Pull out of the integrand the “best” factor that can be integrated and inverted. The criterion for “best” is to try to reduce the remaining integrand to a function that is as close as possible to constant. The limiting case is instructive: If you manage to make the integrand f exactly constant, and if the region V , of known volume, exactly encloses the desired region W , then the average of f that you compute will be exactly its constant value, and the error estimate in equation (7.6.1) will exactly vanish. You will, in fact, have done the integral exactly, and the Monte Carlo numerical evaluations are superfluous. So, backing off from the extreme limiting case, to the extent that you are able to make f approximately constant by change of variable, and to the extent that you can sample a region only slightly larger than W , you will increase the accuracy of the Monte Carlo integral. This technique is generically called reduction of variance in the literature. The fundamental disadvantage of simple Monte Carlo integration is that its accuracy increases only as the square root of N , the number of sampled points. If your accuracy requirements are modest, or if your computer budget is large, then the technique is highly recommended as one of great generality. In the next two sections we will see that there are techniques available for “breaking the square root of N barrier” and achieving, at least in some cases, higher accuracy with fewer function evaluations.
300
Chapter 7.
Random Numbers
The Sobol’ sequencegenerates numbers between zero and one directly as binary fractions of length w bits, from a set of w special binary fractions, Vi , i = 1, 2, . . . , w, called direction numbers. In Sobol’s original method, the jth number Xj is generated by XORing (bitwise exclusive or) together the set of Vi ’s satisfying the criterion on i, “the ith bit of j is nonzero.” As j increments, in other words, different ones of the Vi ’s flash in and out of Xj on different time scales. V1 alternates between being present and absent most quickly, while Vk goes from present to absent (or vice versa) only every 2k−1 steps. Antonov and Saleev’s contribution was to show that instead of using the bits of the integer j to select direction numbers, one could just as well use the bits of the Gray code of j, G(j). (For a quick review of Gray codes, look at §20.2.) Now G(j) and G(j + 1) differ in exactly one bit position, namely in the position of the
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The trouble with a grid is that one has to decide in advance how fine it should be. One is then committed to completing all of its sample points. With a grid, it is not convenient to “sample until” some convergence or termination criterion is met. One might ask if there is not some intermediate scheme, some way to pick sample points “at random,” yet spread out in some self-avoiding way, avoiding the chance clustering that occurs with uniformly random points. A similar question arises for tasks other than Monte Carlo integration. We might want to search an n-dimensional space for a point where some (locally computable) condition holds. Of course, for the task to be computationally meaningful, there had better be continuity, so that the desired condition will hold in some finite ndimensional neighborhood. We may not know a priori how large that neighborhood is, however. We want to “sample until” the desired point is found, moving smoothly to finer scales with increasing samples. Is there any way to do this that is better than uncorrelated, random samples? The answer to the above question is “yes.” Sequences of n-tuples that fill n-space more uniformly than uncorrelated random points are called quasi-random sequences. That term is somewhat of a misnomer, since there is nothing “random” about quasi-random sequences: They are cleverly crafted to be, in fact, sub-random. The sample points in a quasi-random sequence are, in a precise sense, “maximally avoiding” of each other. A conceptually simple example is Halton’s sequence [1]. In one dimension, the jth number Hj in the sequence is obtained by the following steps: (i) Write j as a number in base b, where b is some prime. (For example j = 17 in base b = 3 is 122.) (ii) Reverse the digits and put a radix point (i.e., a decimal point base b) in front of the sequence. (In the example, we get 0.221 base 3.) The result is Hj . To get a sequence of n-tuples in n-space, you make each component a Halton sequence with a different prime base b. Typically, the first n primes are used. It is not hard to see how Halton’s sequence works: Every time the number of digits in j increases by one place, j’s digit-reversed fraction becomes a factor of b finer-meshed. Thus the process is one of filling in all the points on a sequence of finer and finer Cartesian grids — and in a kind of maximally spread-out order on each grid (since, e.g., the most rapidly changing digit in j controls the most significant digit of the fraction). Other ways of generating quasi-random sequences have been suggested by Faure, Sobol’, Niederreiter, and others. Bratley and Fox [2] provide a good review and references, and discuss a particularly efficient variant of the Sobol’ [3] sequence suggested by Antonov and Saleev [4]. It is this Antonov-Saleev variant whose implementation we now discuss.
7.7 Quasi- (that is, Sub-) Random Sequences
0
.2
.4 .6 points 1 to 128
.8
...... ...... . ................ ....... ........... . . ...... .. ... . . ................ .... . .8 .. .. .... ......... ..... . .. . . . . . . . . . ... .... ......... ...... . ..... ............ . . ... ... . . . .6 ............................................................. . . . .... . . .4 ....... ... .... ........ ... . ...................................... ...................... .2 .. . ... ... . . .. .. . .. . .. ... ......... ................. ... ....... ............. . . .. . 0 . . .. .2
.4 .6 .8 points 513 to 1024
0
1
1
0
. . . . . .. . . . ..... . ....... ............ ..... ....... ............ . . . . .8 ...... ... . ... .................... ........... .... .... .. .... ........ .... .... .6 ...... .. . ... ... .. ......... . .... ... .......... . .... . ........ . ... ...... .......... .4 .. . .......... .............. ... .. .... .. . . . .. . .. . . . .2 . .. . ... . . . . . .. .... . . .... ...... .... . ... ....... .... .. . . 0 . .. .. . . . . .. . ....... 1
1
.2
.4 .6 .8 points 129 to 512
1
.. . ... .. . . . .. ...................................................................................................... . . . . . . . . . ... .. .8 ............ ....................... ............. ............. ................................. ............. . ... ........... ........ .................. .6 ............................. ....... ........... . ...................................................................................... . . . . ............ ...................... .4 .. ..... .. ...... . ................................................................................................ . ........... ...... ........... ...... .............. .2 . .. ............................................................................................... 0 ... ...... . . . . .. . ..... ... .. 1
0
.2
.4 .6 .8 points 1 to 1024
1
Figure 7.7.1. First 1024 points of a two-dimensional Sobol’ sequence. The sequence is generated number-theoretically, rather than randomly, so successive points at any stage “know” how to fill in the gaps in the previously generated distribution.
rightmost zero bit in the binary representation of j (adding a leading zero to j if necessary). A consequence is that the j + 1st Sobol’-Antonov-Saleev number can be obtained from the jth by XORing it with a single Vi , namely with i the position of the rightmost zero bit in j. This makes the calculation of the sequence very efficient, as we shall see. Figure 7.7.1 plots the first 1024 points generated by a two-dimensional Sobol’ sequence. One sees that successive points do “know” about the gaps left previously, and keep filling them in, hierarchically. We have deferred to this point a discussion of how the direction numbers Vi are generated. Some nontrivial mathematics is involved in that, so we will content ourself with a cookbook summary only: Each different Sobol’ sequence (or component of an n-dimensional sequence) is based on a different primitive polynomial over the integers modulo 2, that is, a polynomial whose coefficients are either 0 or 1, and which generates a maximal length shift register sequence. (Primitive polynomials modulo 2 were used in §7.4, and are further discussed in §20.3.) Suppose P is such a polynomial, of degree q, P = xq + a1 xq−1 + a2 xq−2 + · · · + aq−1 + 1
(7.7.1)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
. . .. . .. . .. ... ... .. ..... . . . .8 .. . . .. . . .. . . . . . . .. . .. . .. .. .. . .6 . . . . .. . . . . . . . . . . . . .. . . . . .4 . . . . . . . . . . . .. . . . . . . . .. . .. . . .2 . . .. .. . . . . . . . . .. . . . . . 0 1
301
302
Chapter 7.
Degree
Random Numbers
Primitive Polynomials Modulo 2* 0 (i.e., x + 1)
2
1 (i.e., x2 + x + 1)
3
1, 2 (i.e., x3 + x + 1 and x3 + x2 + 1)
4
1, 4 (i.e., x4 + x + 1 and x4 + x3 + 1)
5
2, 4, 7, 11, 13, 14
6
1, 13, 16, 19, 22, 25
7
1, 4, 7, 8, 14, 19, 21, 28, 31, 32, 37, 41, 42, 50, 55, 56, 59, 62
8
14, 21, 22, 38, 47, 49, 50, 52, 56, 67, 70, 84, 97, 103, 115, 122
9
8, 13, 16, 22, 25, 44, 47, 52, 55, 59, 62, 67, 74, 81, 82, 87, 91, 94, 103, 104, 109, 122, 124, 137, 138, 143, 145, 152, 157, 167, 173, 176, 181, 182, 185, 191, 194, 199, 218, 220, 227, 229, 230, 234, 236, 241, 244, 253
10
4, 13, 19, 22, 50, 55, 64, 69, 98, 107, 115, 121, 127, 134, 140, 145, 152, 158, 161, 171, 181, 194, 199, 203, 208, 227, 242, 251, 253, 265, 266, 274, 283, 289, 295, 301, 316, 319, 324, 346, 352, 361, 367, 382, 395, 398, 400, 412, 419, 422, 426, 428, 433, 446, 454, 457, 472, 493, 505, 508
*Expressed as a decimal integer representing the interior bits (that is, omitting the high-order bit and the unit bit).
Define a sequence of integers Mi by the q-term recurrence relation, Mi = 2a1 Mi−1 ⊕ 22 a2 Mi−2 ⊕ · · · ⊕ 2q−1 Mi−q+1 aq−1 ⊕ (2q Mi−q ⊕ Mi−q ) (7.7.2) Here bitwise XOR is denoted by ⊕. The starting values for this recurrence are that M1, . . . , Mq can be arbitrary odd integers less than 2, . . . , 2q , respectively. Then, the direction numbers Vi are given by Vi = Mi /2i
i = 1, . . . , w
(7.7.3)
The accompanying table lists all primitive polynomials modulo 2 with degree q ≤ 10. Since the coefficients are either 0 or 1, and since the coefficients of xq and of 1 are predictably 1, it is convenient to denote a polynomial by its middle coefficients taken as the bits of a binary number (higher powers of x being more significant bits). The table uses this convention. Turn now to the implementation of the Sobol’ sequence. Successive calls to the function sobseq (after a preliminary initializing call) return successive points in an n-dimensional Sobol’ sequence based on the first n primitive polynomials in the table. As given, the routine is initialized for maximum n of 6 dimensions, and for a word length w of 30 bits. These parameters can be altered by changing MAXBIT (≡ w) and MAXDIM, and by adding more initializing data to the arrays ip (the primitive polynomials from the table), mdeg (their degrees), and iv (the starting values for the recurrence, equation 7.7.2). A second table, below, elucidates the initializing data in the routine. SUBROUTINE sobseq(n,x) INTEGER n,MAXBIT,MAXDIM REAL x(*) PARAMETER (MAXBIT=30,MAXDIM=6) When n is negative, internally initializes a set of MAXBIT direction numbers for each of MAXDIM different Sobol’ sequences. When n is positive (but ≤MAXDIM), returns as the
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
7.7 Quasi- (that is, Sub-) Random Sequences
303
Initializing Values Used in sobseq Polynomial
Starting Values
1
0
1
(3)
(5)
(15) . . .
2
1
1
1
(7)
(11) . . .
3
1
1
3
7
(5) . . .
3
2
1
3
3
(15) . . .
4
1
1
1
3
13 . . .
4
4
1
1
5
9 ...
Parenthesized values are not freely specifiable, but are forced by the required recurrence for this degree.
*
1
vector x(1..n) the next values from n of these sequences. (n must not be changed between initializations.) INTEGER i,im,in,ipp,j,k,l,ip(MAXDIM),iu(MAXDIM,MAXBIT), iv(MAXBIT*MAXDIM),ix(MAXDIM),mdeg(MAXDIM) REAL fac SAVE ip,mdeg,ix,iv,in,fac EQUIVALENCE (iv,iu) To allow both 1D and 2D addressing. DATA ip /0,1,1,2,1,4/, mdeg /1,2,3,3,4,4/, ix /6*0/ DATA iv /6*1,3,1,3,3,1,1,5,7,7,3,3,5,15,11,5,15,13,9,156*0/ if (n.lt.0) then Initialize, don’t return a vector. do 11 k=1,MAXDIM ix(k)=0 enddo 11 in=0 if(iv(1).ne.1)return fac=1./2.**MAXBIT do 15 k=1,MAXDIM do 12 j=1,mdeg(k) Stored values only require normalization. iu(k,j)=iu(k,j)*2**(MAXBIT-j) enddo 12 do 14 j=mdeg(k)+1,MAXBIT Use the recurrence to get other values. ipp=ip(k) i=iu(k,j-mdeg(k)) i=ieor(i,i/2**mdeg(k)) do 13 l=mdeg(k)-1,1,-1 if(iand(ipp,1).ne.0)i=ieor(i,iu(k,j-l)) ipp=ipp/2 enddo 13 iu(k,j)=i enddo 14 enddo 15 else Calculate the next vector in the sequence. im=in do 16 j=1,MAXBIT Find the rightmost zero bit. if(iand(im,1).eq.0)goto 1 im=im/2 enddo 16 pause ’MAXBIT too small in sobseq’ im=(j-1)*MAXDIM do 17 k=1,min(n,MAXDIM) XOR the appropriate direction number into each comix(k)=ieor(ix(k),iv(im+k)) ponent of the vector and convert to a floating x(k)=ix(k)*fac number. enddo 17 in=in+1 Increment the counter.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Degree
304
Chapter 7.
Random Numbers
endif return END
Let us try the function f (x, y, z) =
1 + cos 0
πr2 a2
r < r0
(7.7.5)
r ≥ r0
which can be integrated analytically in cylindrical coordinates, giving Z Z Z dx dy dz f (x, y, z) = 2π2 a2 R0
(7.7.6)
With parameters R0 = 0.6, r0 = 0.3, we did 100 successive Monte Carlo integrations of equation (7.7.4), sampling uniformly in the region −1 < x, y, z < 1, for the two cases of uncorrelated random points and the Sobol’ sequence generated by the routine sobseq. Figure 7.7.2 shows the results, plotting the r.m.s. average error of the 100 integrations as a function of the number of points sampled. (For any single integration, the error of course wanders from positive to negative, or vice versa, so a logarithmic plot of fractional error is not very informative.) The thin, dashed curve corresponds to uncorrelated random points and shows the familiar N −1/2 asymptotics. The thin, solid gray curve shows the result for the Sobol’ sequence. The logarithmic term in the expected (ln N )3 /N is readily apparent as curvature in the curve, but the asymptotic N −1 is unmistakable. To understand the importance of Figure 7.7.2, suppose that a Monte Carlo integration of f with 1% accuracy is desired. The Sobol’ sequence achieves this accuracy in a few thousand samples, while pseudorandom sampling requires nearly 100,000 samples. The ratio would be even greater for higher desired accuracies. A different, not quite so favorable, case occurs when the function being integrated has hard (discontinuous) boundaries inside the sampling region, for example the function that is one inside the torus, zero outside, n 1 r < r0 f (x, y, z) = (7.7.7) 0 r ≥ r0 where r is defined in equation (7.7.4). Not by coincidence, this function has the same analytic integral as the function of equation (7.7.5), namely 2π2 a2 R0 . The carefully hierarchical Sobol’ sequence is based on a set of Cartesian grids, but the boundary of the torus has no particular relation to those grids. The result is that it is essentially random whether sampled points in a thin layer at the surface of the torus, containing on the order of N 2/3 points, come out to be inside, or outside, the torus. The square root law, applied to this thin layer, gives N 1/3 fluctuations in the sum, or N −2/3 fractional error in the Monte Carlo integral. One sees this behavior verified in Figure 7.7.2 by the thicker gray curve. The thicker dashed curve in Figure 7.7.2 is the result of integrating the function of equation (7.7.7) using independent random points. While the advantage of the Sobol’ sequence is not quite so dramatic as in the case of a smooth function, it can nonetheless be a significant factor (∼5) even at modest accuracies like 1%, and greater at higher accuracies. Note that we have not provided the routine sobseq with a means of starting the sequence at a point other than the beginning, but this feature would be easy to add. Once the initialization of the direction numbers iv has been done, the jth point can be obtained directly by XORing together those direction numbers corresponding to nonzero bits in the Gray code of j, as described above.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
How good is a Sobol’ sequence, anyway? For Monte Carlo integration of a smooth function in n dimensions, the answer is that the fractional error will decrease with N , the number of samples, as (ln N )n /N , i.e., almost as fast as 1/N . As an example, let us integrate a function that is nonzero inside a torus (doughnut) in three-dimensional space. If the major radius of the torus is R0 , the minor radial coordinate r is defined by 1/2 r = [(x2 + y2 )1/2 − R0 ]2 + z 2 (7.7.4)
305
7.7 Quasi- (that is, Sub-) Random Sequences
∝ N −1/2 .01
.001
100
∝ N −2/3
pseudo-random, hard boundary pseudo-random, soft boundary quasi-random, hard boundary quasi-random, soft boundary
1000
∝ N −1
10000
10 5
number of points N Figure 7.7.2. Fractional accuracy of Monte Carlo integrations as a function of number of points sampled, for two different integrands and two different methods of choosing random points. The quasi-random Sobol’ sequence converges much more rapidly than a conventional pseudo-random sequence. Quasirandom sampling does better when the integrand is smooth (“soft boundary”) than when it has step discontinuities (“hard boundary”). The curves shown are the r.m.s. average of 100 trials.
The Latin Hypercube We might here give passing mention the unrelated technique of Latin square or Latin hypercube sampling, which is useful when you must sample an N -dimensional space exceedingly sparsely, at M points. For example, you may want to test the crashworthiness of cars as a simultaneous function of 4 different design parameters, but with a budget of only three expendable cars. (The issue is not whether this is a good plan — it isn’t — but rather how to make the best of the situation!) The idea is to partition each design parameter (dimension) into M segments, so that the whole space is partitioned into M N cells. (You can choose the segments in each dimension to be equal or unequal, according to taste.) With 4 parameters and 3 cars, for example, you end up with 3 × 3 × 3 × 3 = 81 cells. Next, choose M cells to contain the sample points by the following algorithm: Randomly choose one of the M N cells for the first point. Now eliminate all cells that agree with this point on any of its parameters (that is, cross out all cells in the same row, column, etc.), leaving (M − 1)N candidates. Randomly choose one of these, eliminate new rows and columns, and continue the process until there is only one cell left, which then contains the final sample point. The result of this construction is that each design parameter will have been tested in every one of its subranges. If the response of the system under test is
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
fractional accuracy of integral
.1
306
Chapter 7.
Random Numbers
dominated by one of the design parameters, that parameter will be found with this sampling technique. On the other hand, if there is an important interaction among different design parameters, then the Latin hypercube gives no particular advantage. Use with care.
Bratley P., and Fox, B.L. 1988, ACM Transactions on Mathematical Software, vol. 14, pp. 88– 100. [2] Lambert, J.P. 1988, in Numerical Mathematics – Singapore 1988, ISNM vol. 86, R.P. Agarwal, Y.M. Chow, and S.J. Wilson, eds. (Basel: Birkhauser), ¨ pp. 273–284. ¨ Niederreiter, H. 1988, in Numerical Integration III, ISNM vol. 85, H. Brass and G. Hammerlin, eds. (Basel: Birkhauser), ¨ pp. 157–171. Sobol’, I.M. 1967, USSR Computational Mathematics and Mathematical Physics, vol. 7, no. 4, pp. 86–112. [3] Antonov, I.A., and Saleev, V.M 1979, USSR Computational Mathematics and Mathematical Physics, vol. 19, no. 1, pp. 252–256. [4] Dunn, O.J., and Clark, V.A. 1974, Applied Statistics: Analysis of Variance and Regression (New York, Wiley) [discusses Latin Square].
7.8 Adaptive and Recursive Monte Carlo Methods This section discusses more advanced techniques of Monte Carlo integration. As examples of the use of these techniques, we include two rather different, fairly sophisticated, multidimensional Monte Carlo codes: vegas [1,2] , and miser [3]. The techniques that we discuss all fall under the general rubric of reduction of variance (§7.6), but are otherwise quite distinct.
Importance Sampling The use of importance sampling was already implicit in equations (7.6.6) and (7.6.7). We now return to it in a slightly more formal way. Suppose that an integrand f can be written as the product of a function h that is almost constant times another, positive, function g. Then its integral over a multidimensional volume V is Z Z Z f dV = (f /g) gdV = h gdV (7.8.1) In equation (7.6.7) we interpreted equation (7.8.1) as suggesting a change of variable to G, the indefinite integral of g. That made gdV a perfect differential. We then proceeded to use the basic theorem of Monte Carlo integration, equation (7.6.1). A more general interpretation of equation (7.8.1) is that we can integrate f by instead sampling h — not, however, with uniform probability density dV , but rather with nonuniform density gdV . In this second interpretation, the first interpretation follows as the special case, where the means of generating the nonuniform sampling of gdV is via the transformation method, using the indefinite integral G (see §7.2). More directly, one can go back and generalize the basic theorem (7.6.1) to the case of nonuniform sampling: Suppose that points xi are chosen within the volume V with a probability density p satisfying Z p dV = 1 (7.8.2)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
CITED REFERENCES AND FURTHER READING: Halton, J.H. 1960, Numerische Mathematik, vol. 2, pp. 84–90. [1]
7.8 Adaptive and Recursive Monte Carlo Methods
307
The generalized fundamental theorem is that the integral of any function f is estimated, using N sample points xi , . . . , xN , by Z I≡
Z f dV =
f pdV ≈ p
f ± p
s hf 2 /p2 i − hf /pi2 N
(7.8.3)
S≡
f2 p2
−
Z 2 Z 2 Z 2 2 Z 2 f f f f ≈ pdV − = (7.8.4) pdV dV − f dV p p2 p p
We now find the optimal p subject to the constraint equation (7.8.2) by the functional variation δ 0= δp
Z
f2 dV − p
Z
2 +λ
f dV
!
Z p dV
(7.8.5)
with λ a Lagrange multiplier. Note that the middle term does not depend on p. The variation (which comes inside the integrals) gives 0 = −f 2 /p2 + λ or |f | |f | p= √ = R |f | dV λ
(7.8.6)
where λ has been chosen to enforce the constraint (7.8.2). If f has one sign in the region of integration, then we get the obvious result that the optimal choice of p — if one can figure out a practical way of effecting the sampling — is that it be proportional to |f |. Then the variance is reduced to zero. Not so obvious, but seen to be true, is the fact that p ∝ |f | is optimal even if f takes on both signs. In that case the variance per sample point (from equations 7.8.4 and 7.8.6) is Z S = Soptimal =
2 |f | dV
Z −
2 f dV
(7.8.7)
One curiosity is that one can add a constant to the integrand to make it all of one sign, since this changes the integral by a known amount, constant × V . Then, the optimal choice of p always gives zero variance, that is, a perfectly accurate integral! The resolution of this seeming paradox (already mentioned at the end R of §7.6) is that perfect knowledge of p in equation (7.8.6) requires perfect knowledge of |f |dV , which is tantamount to already knowing the integral you are trying to compute! If your function f takes on a known constant value in most of the volume V , it is certainly a good idea to add a constant so as to make that value zero. Having done that, the accuracy attainable by importance sampling depends in practice not on how small equation (7.8.7) is, but rather on how small is equation (7.8.4) for an implementable p, likely only a crude approximation to the ideal.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where angle brackets denote arithmetic means over the N points, exactly as in equation (7.6.2). As in equation (7.6.1), the “plus-or-minus” term is a one standard deviation error estimate. Notice that equation (7.6.1) is in fact the special case of equation (7.8.3), with p = constant = 1/V . What is the best choice for the sampling density p? Intuitively, we have already seen that the idea is to make h = f /p as close to constant as possible. We can be more rigorous by focusing on the numerator inside the square root in equation (7.8.3), which is the variance per sample point. Both angle brackets are themselves Monte Carlo estimators of integrals, so we can write
308
Chapter 7.
Random Numbers
Stratified Sampling
The variance of the estimator, Var (hf i), which measures the square of the error of the Monte Carlo integration, is asymptotically related to the variance of the function, Var (f ) ≡ hhf 2ii − hhf ii2, by the relation Var (hf i) =
Var (f ) N
(7.8.9)
(compare equation 7.6.1). Suppose we divide the volume V into two equal, disjoint subvolumes, denoted a and b, and sample N/2 points in each subvolume. Then another estimator for hhf ii, different from equation (7.8.8), which we denote hf i0 , is 1 (7.8.10) hf ia + hf ib 2 in other words, the mean of the sample averages in the two half-regions. The variance of estimator (7.8.10) is given by 1 Var hf i0 = Var hf ia + Var hf ib 4 Varb (f ) 1 Vara (f ) (7.8.11) + = 4 N/2 N/2 1 = [Vara (f ) + Varb (f )] 2N hf i0 ≡
Here Vara (f ) denotes the variance of f in subregion a, that is, hhf 2iia − hhf ii2a , and correspondingly for b. From the definitions already given, it is not difficult to prove the relation 1 1 (7.8.12) [Vara (f ) + Varb (f )] + (hhf iia − hhf iib)2 2 4 (In physics, this formula for combining second moments is the “parallel axis theorem.”) Comparing equations (7.8.9), (7.8.11), and (7.8.12), one sees that the stratified (into two subvolumes) sampling gives a variance that is never larger than the simple Monte Carlo case — and smaller whenever the means of the stratified samples, hhf iia and hhf iib, are different. We have not yet exploited the possibility of sampling the two subvolumes with different numbers of points, say Na in subregion a and Nb ≡ N − Na in subregion b. Let us do so now. Then the variance of the estimator is 1 Vara (f ) Varb (f ) Var hf i0 = + (7.8.13) 4 Na N − Na Var (f ) =
which is minimized (one can easily verify) when Na σa = N σa + σb
(7.8.14)
Here we have adopted the shorthand notation σa ≡ [Vara (f )]1/2 , and correspondingly for b. If Na satisfies equation (7.8.14), then equation (7.8.13) reduces to (σa + σb )2 Var hf i0 = 4N
(7.8.15)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The idea of stratified sampling is quite different from importance sampling. Let us expand our notation slightly and let hhf ii denote the true average of the function f over the volume V (namely the integral divided by V ), while hf i denotes as before the simplest (uniformly sampled) Monte Carlo estimator of that average: Z 1 1 X hhf ii ≡ f dV hf i ≡ f (xi) (7.8.8) V N i
7.8 Adaptive and Recursive Monte Carlo Methods
309
Mixed Strategies Importance sampling and stratified sampling seem, at first sight, inconsistent with each other. The former concentrates sample points where the magnitude of the integrand |f | is largest, that latter where the variance of f is largest. How can both be right? The answer is that (like so much else in life) it all depends on what you know and how well you know it. Importance sampling depends on already knowing some approximation to your integral, so that you are able to generate random points xi with the desired probability density p. To the extent that your p is not ideal, you are left with an error that decreases only as N −1/2 . Things are particularly bad if your p is far from ideal in a region where the integrand f is changing rapidly, since then the sampled function h = f /p will have a large variance. Importance sampling works by smoothing the values of the sampled function h, and is effective only to the extent that you succeed in this. Stratified sampling, by contrast, does not necessarily require that you know anything about f . Stratified sampling works by smoothing out the fluctuations of the number of points in subregions, not by smoothing the values of the points. The simplest stratified strategy, dividing V into N equal subregions and choosing one point randomly in each subregion, already gives a method whose error decreases asymptotically as N −1 , much faster than N −1/2 . (Note that quasi-random numbers, §7.7, are another way of smoothing fluctuations in the density of points, giving nearly as good a result as the “blind” stratification strategy.) However, “asymptotically” is an important caveat: For example, if the integrand is negligible in all but a single subregion, then the resulting one-sample integration is all but useless. Information, even very crude, allowing importance sampling to put many points in the active subregion would be much better than blind stratified sampling. Stratified sampling really comes into its own if you have some way of estimating the variances, so that you can put unequal numbers of points in different subregions, according to (7.8.14) or its generalizations, and if you can find a way of dividing a region into a practical number of subregions (notably not K d with large dimension d), while yet significantly reducing the variance of the function in each subregion compared to its variance in the full volume. Doing this requires a lot of knowledge about f , though different knowledge from what is required for importance sampling. In practice, importance sampling and stratified sampling are not incompatible. In many, if not most, cases of interest, the integrand f is small everywhere in V except for a small fractional volume of “active regions.” In these regions the magnitude of |f | and the standard deviation σ = [Var (f )]1/2 are comparable in size, so both techniques will give about the same concentration of points. In more sophisticated implementations, it is also possible to “nest” the two techniques, so that (e.g.) importance sampling on a crude grid is followed by stratification within each grid cell.
Adaptive Monte Carlo: VEGAS The VEGAS algorithm, invented by Peter Lepage [1,2] , is widely used for multidimensional integrals that occur in elementary particle physics. VEGAS is primarily based on importance sampling, but it also does some stratified sampling if the dimension d is small enough to avoid K d explosion (specifically, if (K/2)d < N/2, with N the number of sample
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Equation (7.8.15) reduces to equation (7.8.9) if Var (f ) = Vara (f ) = Varb (f ), in which case stratifying the sample makes no difference. A standard way to generalize the above result is to consider the volume V divided into more than two equal subregions. One can readily obtain the result that the optimal allocation of sample points among the regions is to have the number of points in each region j proportional to σj (that is, the square root of the variance of the function f in that subregion). In spaces of high dimensionality (say d > ∼ 4) this is not in practice very useful, however. Dividing a volume into K segments along each dimension implies K d subvolumes, typically much too large a number when one contemplates estimating all the corresponding σj ’s.
310
Chapter 7.
Random Numbers
points). The basic technique for importance sampling in VEGAS is to construct, adaptively, a multidimensional weight function g that is separable, p ∝ g(x, y, z, . . .) = gx (x)gy (y)gz (z) . . .
(7.8.16)
d
(and correspondingly for y, z, . . .). Notice that this reduces to g ∝ |f | (7.8.6) in one dimension. Equation (7.8.17) immediately suggests VEGAS’ adaptive strategy: Given a set of g-functions (initially all constant, say), one samples the function f , accumulating not only the overall estimator of the integral, but also the Kd estimators (K subdivisions of the independent variable in each of d dimensions) of the right-hand side of equation (7.8.17). These then determine improved g functions for the next iteration. When the integrand f is concentrated in one, or at most a few, regions in d-space, then the weight function g’s quickly become large at coordinate values that are the projections of these regions onto the coordinate axes. The accuracy of the Monte Carlo integration is then enormously enhanced over what simple Monte Carlo would give. The weakness of VEGAS is the obvious one: To the extent that the projection of the function f onto individual coordinate directions is uniform, VEGAS gives no concentration of sample points in those dimensions. The worst case for VEGAS, e.g., is an integrand that is concentrated close to a body diagonal line, e.g., one from (0, 0, 0, . . .) to (1, 1, 1, . . .). Since this geometry is completely nonseparable, VEGAS can give no advantage at all. More generally, VEGAS may not do well when the integrand is concentrated in one-dimensional (or higher) curved trajectories (or hypersurfaces), unless these happen to be oriented close to the coordinate directions. The routine vegas that follows is essentially Lepage’s standard version, minimally modified to conform to our conventions. (We thank Lepage for permission to reproduce the program here.) For consistency with other versions of the VEGAS algorithm in circulation, we have preserved original variable names. The parameter NDMX is what we have called K, the maximum number of increments along each axis; MXDIM is the maximum value of d; some other parameters are explained in the comments. The vegas routine performs m = itmx statistically independent evaluations of the desired integral, each with N = ncall function evaluations. While statistically independent, these iterations do assist each other, since each one is used to refine the sampling grid for the next one. The results of all iterations are combined into a single best answer, and its estimated error, by the relations , m !−1/2 m m X X 1 X Ii 1 Ibest = σ = (7.8.18) best σ2 σ2 σ2 i=1 i i=1 i i=1 i Also returned is the quantity χ2 /m ≡
m 1 X (Ii − Ibest)2 m − 1 i=1 σi2
(7.8.19)
If this is significantly larger than 1, then the results of the iterations are statistically inconsistent, and the answers are suspect. The input flag init can be used to advantage. One might have a call with init=0, ncall=1000, itmx=5 immediately followed by a call with init=1, ncall=100000, itmx=1. The effect would be to develop a sampling grid over 5 iterations of a small number of samples, then to do a single high accuracy integration on the optimized grid.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Such a function avoids the K explosion in two ways: (i) It can be stored in the computer as d separate one-dimensional functions, each defined by K tabulated values, say — so that K × d replaces K d. (ii) It can be sampled as a probability density by consecutively sampling the d one-dimensional functions to obtain coordinate vector components (x, y, z, . . .). The optimal separable weight function can be shown to be [1] Z 1/2 Z f 2 (x, y, z, . . .) gx (x) ∝ dy dz . . . (7.8.17) gy (y)gz (z) . . .
7.8 Adaptive and Recursive Monte Carlo Methods
311
Note that the user-supplied integrand function, fxn, has an argument wgt in addition to the expected evaluation point x. In most applications you ignore wgt inside the function. Occasionally, however, you may want to integrate some additional function or functions along with the principal function f . The integral of any such function g can be estimated by X Ig = wi g(x) (7.8.20) i
*
C
* *
SUBROUTINE vegas(region,ndim,fxn,init,ncall,itmx,nprn, tgral,sd,chi2a) INTEGER init,itmx,ncall,ndim,nprn,NDMX,MXDIM REAL tgral,chi2a,sd,region(2*ndim),fxn,ALPH,TINY PARAMETER (ALPH=1.5,NDMX=50,MXDIM=10,TINY=1.e-30) EXTERNAL fxn USES fxn,ran2,rebin Performs Monte Carlo integration of a user-supplied ndim-dimensional function fxn over a rectangular volume specified by region, a 2×ndim vector consisting of ndim “lower left” coordinates of the region followed by ndim “upper right” coordinates. The integration consists of itmx iterations, each with approximately ncall calls to the function. After each iteration the grid is refined; more than 5 or 10 iterations are rarely useful. The input flag init signals whether this call is a new start, or a subsequent call for additional iterations (see comments below). The input flag nprn (normally 0) controls the amount of diagnostic output. Returned answers are tgral (the best estimate of the integral), sd (its standard deviation), and chi2a (χ2 per degree of freedom, an indicator of whether consistent results are being obtained). See text for further details. INTEGER i,idum,it,j,k,mds,nd,ndo,ng,npg,ia(MXDIM),kg(MXDIM) REAL calls,dv2g,dxg,f,f2,f2b,fb,rc,ti,tsi,wgt,xjac,xn,xnd,xo, d(NDMX,MXDIM),di(NDMX,MXDIM),dt(MXDIM),dx(MXDIM), r(NDMX),x(MXDIM),xi(NDMX,MXDIM),xin(NDMX),ran2 DOUBLE PRECISION schi,si,swgt COMMON /ranno/ idum Means for random number initialization. SAVE Best make everything static, allowing restarts. if(init.le.0)then Normal entry. Enter here on a cold start. mds=1 Change to mds=0 to disable stratified sampling, i.e., use imndo=1 portance sampling only. do 11 j=1,ndim xi(1,j)=1. enddo 11 endif if (init.le.1)then Enter here to inherit the grid from a previous call, but not its si=0.d0 answers. swgt=0.d0 schi=0.d0 endif if (init.le.2)then Enter here to inherit the previous grid and its answers. nd=NDMX ng=1 if(mds.ne.0)then Set up for stratification. ng=(ncall/2.+0.25)**(1./ndim) mds=1 if((2*ng-NDMX).ge.0)then mds=-1 npg=ng/NDMX+1 nd=ng/npg ng=npg*nd endif endif k=ng**ndim
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where the wi ’s and x’s are the arguments wgt and x, respectively. It is straightforward to accumulate this sum inside your function fxn, and to pass the answer back to your main program via a common block. Of course, g(x) had better resemble the principal function f to some degree, since the sampling will be optimized for f .
312
Chapter 7.
Random Numbers
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
npg=max(ncall/k,2) calls=float(npg)*float(k) dxg=1./ng dv2g=(calls*dxg**ndim)**2/npg/npg/(npg-1.) xnd=nd dxg=dxg*xnd xjac=1./calls do 12 j=1,ndim dx(j)=region(j+ndim)-region(j) xjac=xjac*dx(j) enddo 12 if(nd.ne.ndo)then Do binning if necessary. do 13 i=1,max(nd,ndo) r(i)=1. enddo 13 do 14 j=1,ndim call rebin(ndo/xnd,nd,r,xin,xi(1,j)) enddo 14 ndo=nd endif if(nprn.ge.0) write(*,200) ndim,calls,it,itmx,nprn, * ALPH,mds,nd,(j,region(j),j,region(j+ndim),j=1,ndim) endif do 28 it=1,itmx Main iteration loop. Can enter here (init ≥ 3) to do an additional itmx iterations with all other parameters unchanged. ti=0. tsi=0. do 16 j=1,ndim kg(j)=1 do 15 i=1,nd d(i,j)=0. di(i,j)=0. enddo 15 enddo 16 10 continue fb=0. f2b=0. do 19 k=1,npg wgt=xjac do 17 j=1,ndim xn=(kg(j)-ran2(idum))*dxg+1. ia(j)=max(min(int(xn),NDMX),1) if(ia(j).gt.1)then xo=xi(ia(j),j)-xi(ia(j)-1,j) rc=xi(ia(j)-1,j)+(xn-ia(j))*xo else xo=xi(ia(j),j) rc=(xn-ia(j))*xo endif x(j)=region(j)+rc*dx(j) wgt=wgt*xo*xnd enddo 17 f=wgt*fxn(x,wgt) f2=f*f fb=fb+f f2b=f2b+f2 do 18 j=1,ndim di(ia(j),j)=di(ia(j),j)+f if(mds.ge.0) d(ia(j),j)=d(ia(j),j)+f2 enddo 18 enddo 19 f2b=sqrt(f2b*npg) f2b=(f2b-fb)*(f2b+fb)
7.8 Adaptive and Recursive Monte Carlo Methods
313
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
if (f2b.le.0.) f2b=TINY ti=ti+fb tsi=tsi+f2b if(mds.lt.0)then Use stratified sampling. do 21 j=1,ndim d(ia(j),j)=d(ia(j),j)+f2b enddo 21 endif do 22 k=ndim,1,-1 kg(k)=mod(kg(k),ng)+1 if(kg(k).ne.1) goto 10 enddo 22 tsi=tsi*dv2g Compute final results for this iteration. wgt=1./tsi si=si+dble(wgt)*dble(ti) schi=schi+dble(wgt)*dble(ti)**2 swgt=swgt+dble(wgt) tgral=si/swgt chi2a=max((schi-si*tgral)/(it-.99d0),0.d0) sd=sqrt(1./swgt) tsi=sqrt(tsi) if(nprn.ge.0)then write(*,201) it,ti,tsi,tgral,sd,chi2a if(nprn.ne.0)then do 23 j=1,ndim write(*,202) j,(xi(i,j),di(i,j), * i=1+nprn/2,nd,nprn) enddo 23 endif endif do 25 j=1,ndim Refine the grid. Consult references to understand the subtlety xo=d(1,j) of this procedure. The refinement is damped, to avoid xn=d(2,j) rapid, destabilizing changes, and also compressed in range d(1,j)=(xo+xn)/2. by the exponent ALPH. dt(j)=d(1,j) do 24 i=2,nd-1 rc=xo+xn xo=xn xn=d(i+1,j) d(i,j)=(rc+xn)/3. dt(j)=dt(j)+d(i,j) enddo 24 d(nd,j)=(xo+xn)/2. dt(j)=dt(j)+d(nd,j) enddo 25 do 27 j=1,ndim rc=0. do 26 i=1,nd if(d(i,j).lt.TINY) d(i,j)=TINY r(i)=((1.-d(i,j)/dt(j))/(log(dt(j))-log(d(i,j))))**ALPH rc=rc+r(i) enddo 26 call rebin(rc/xnd,nd,r,xin,xi(1,j)) enddo 27 enddo 28 return 200 FORMAT(/’ input parameters for vegas: ndim=’,i3,’ ncall=’,f8.0 * /28x,’ it=’,i5,’ itmx=’,i5 * /28x,’ nprn=’,i3,’ alph=’,f5.2/28x,’ mds=’,i3,’ nd=’,i4 * /(30x,’xl(’,i2,’)= ’,g11.4,’ xu(’,i2,’)= ’,g11.4)) 201 FORMAT(/’ iteration no.’,I3,’: ’,’integral =’,g14.7,’+/- ’,g9.2 * /’ all iterations: integral =’,g14.7,’+/- ’,g9.2, * ’ chi**2/it’’n =’,g9.2) 202 FORMAT(/’ data for axis ’,I2/’ X delta i ’,
314 * *
Chapter 7.
Random Numbers
’ x delta i ’,’ x delta i ’, /(1x,f7.5,1x,g11.4,5x,f7.5,1x,g11.4,5x,f7.5,1x,g11.4)) END
Recursive Stratified Sampling The problem with stratified sampling, we have seen, is that it may not avoid the K d explosion inherent in the obvious, Cartesian, tesselation of a d-dimensional volume. A technique called recursive stratified sampling [3] attempts to do this by successive bisections of a volume, not along all d dimensions, but rather along only one dimension at a time. The starting points are equations (7.8.10) and (7.8.13), applied to bisections of successively smaller subregions. Suppose that we have a quota of N evaluations of the function f , and want to evaluate hf i0 in the rectangular parallelepiped region R = (xa , xb ). (We denote such a region by the two coordinate vectors of its diagonally opposite corners.) First, we allocate a fraction p of N towards exploring the variance of f in R: We sample pN function values uniformly in R and accumulate the sums that will give the d different pairs of variances corresponding to the d different coordinate directions along which R can be bisected. In other words, in pN samples, we estimate Var (f ) in each of the regions resulting from a possible bisection of R, Rai ≡(xa , xb −
1 ei · (xb − xa )ei ) 2
(7.8.21) 1 Rbi ≡(xa + ei · (xb − xa )ei , xb ) 2 Here ei is the unit vector in the ith coordinate direction, i = 1, 2, . . . , d. Second, we inspect the variances to find the most favorable dimension i to bisect. By equation (7.8.15), we could, for example, choose that i for which the sum of the square roots of the variance estimators in regions Rai and Rbi is minimized. (Actually, as we will explain, we do something slightly different.)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
SUBROUTINE rebin(rc,nd,r,xin,xi) INTEGER nd REAL rc,r(*),xi(*),xin(*) Utility routine used by vegas, to rebin a vector of densities xi into new bins defined by a vector r. INTEGER i,k REAL dr,xn,xo k=0 xo=0. dr=0. do 11 i=1,nd-1 if(rc.gt.dr)then k=k+1 dr=dr+r(k) goto 1 endif if(k.gt.1) xo=xi(k-1) xn=xi(k) dr=dr-rc xin(i)=xn-(xn-xo)*dr/r(k) enddo 11 do 12 i=1,nd-1 xi(i)=xin(i) enddo 12 xi(nd)=1. return END
7.8 Adaptive and Recursive Monte Carlo Methods
315
where α is an unknown constant ≥ 1 (the case of equality corresponding to simple Monte Carlo). In that case, a short calculation shows that Var hf i0 is minimized when Na Vara (f )1/(1+α) = N Vara (f )1/(1+α) + Varb (f )1/(1+α) and that its minimum value is i1+α h Var hf i0 ∝ Vara (f )1/(1+α) + Varb (f )1/(1+α)
(7.8.23)
(7.8.24)
Equations (7.8.22)–(7.8.24) reduce to equations (7.8.13)–(7.8.15) when α = 1. Numerical experiments to find a self-consistent value for α find that α ≈ 2. That is, when equation (7.8.23) with α = 2 is used recursively to allocate sample opportunities, the observed variance of the RSS algorithm goes approximately as N −2 , while any other value of α in equation (7.8.23) gives a poorer fall-off. (The sensitivity to α is, however, not very great; it is not known whether α = 2 is an analytically justifiable result, or only a useful heuristic.) Turn now to the routine, miser, which implements the RSS method. A bit of FORTRAN wizardry is its implementation of the required recursion. This is done by dimensioning an array stack, and a shorter “stack frame” stf; the latter has components that are equivalenced to variables that need to be preserved during the recursion, including a flag indicating where program control should return. A recursive call then consists of copying the stack frame onto the stack, incrementing the stack pointer jstack, and transferring control. A recursive return analogously pops the stack and transfers control to the saved location. Stack growth in miser is only logarithmic in N , since at each bifurcation one of the subvolumes can be processed immediately. The principal difference between miser’s implementation and the algorithm as described thus far lies in how the variances on the right-hand side of equation (7.8.23) are estimated. We find empirically that it is somewhat more robust to use the square of the difference of maximum and minimum sampled function values, instead of the genuine second moment of the samples. This estimator is of course increasingly biased with increasing sample size; however, equation (7.8.23) uses it only to compare two subvolumes (a and b) having approximately equal numbers of samples. The “max minus min” estimator proves its worth when the preliminary sampling yields only a single point, or small number of points, in active regions of the integrand. In many realistic cases, these are indicators of nearby regions of even greater importance, and it is useful to let them attract the greater sampling weight that “max minus min” provides.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Third, we allocate the remaining (1 − p)N function evaluations between the regions Rai and Rbi . If we used equation (7.8.15) to choose i, we should do this allocation according to equation (7.8.14). We now have two parallelepipeds each with its own allocation of function evaluations for estimating the mean of f . Our “RSS” algorithm now shows itself to be recursive: To evaluate the mean in each region, we go back to the sentence beginning “First,...” in the paragraph above equation (7.8.21). (Of course, when the allocation of points to a region falls below some number, we resort to simple Monte Carlo rather than continue with the recursion.) Finally, we combine the means, and also estimated variances of the two subvolumes, using equation (7.8.10) and the first line of equation (7.8.11). This completes the RSS algorithm in its simplest form. Before we describe some additional tricks under the general rubric of “implementation details,” we need to return briefly to equations (7.8.13)–(7.8.15) and derive the equations that we actually use instead of these. The right-hand side of equation (7.8.13) applies the familiar scaling law of equation (7.8.9) twice, once to a and again to b. This would be correct if the estimates hf ia and hf ib were each made by simple Monte Carlo, with uniformly random sample points. However, the two estimates of the mean are in fact made recursively. Thus, there is no reason to expect equation (7.8.9) to hold. Rather, we might substitute for equation (7.8.13) the relation, 1 Vara (f ) Varb (f ) Var hf i0 = + (7.8.22) 4 Naα (N − Na )α
316
Chapter 7.
Random Numbers
*
C
* * * *
1
SUBROUTINE miser(func,region,ndim,npts,dith,ave,var) INTEGER ndim,npts,MNPT,MNBS,MAXD,NSTACK REAL ave,dith,var,region(2*ndim),func,TINY,BIG,PFAC PARAMETER (MNPT=15,MNBS=4*MNPT,MAXD=10,TINY=1.e-30,BIG=1.e30, NSTACK=1000,PFAC=0.1) EXTERNAL func USES func,ranpt Monte Carlo samples a user-supplied ndim-dimensional function func in a rectangular volume specified by region, a 2×ndim vector consisting of ndim “lower-left” coordinates of the region followed by ndim “upper-right” coordinates. The function is sampled a total of npts times, at locations determined by the method of recursive stratified sampling. The mean value of the function in the region is returned as ave; an estimate of the statistical uncertainty of ave (square of standard deviation) is returned as var. The input parameter dith should normally be set to zero, but can be set to (e.g.) 0.1 if func’s active region falls on the boundary of a power-of-two subdivision of region. Parameters: PFAC is the fraction of remaining function evaluations used at each stage to explore the variance of func. At least MNPT function evaluations are performed in any terminal subregion; a subregion is further bisected only if at least MNBS function evaluations are available. MAXD is the largest value of ndim. NSTACK is the total size of the stack. INTEGER iran,j,jb,jstack,n,naddr,np,npre,nptl,nptr,nptt REAL avel,fracl,fval,rgl,rgm,rgr,s,sigl,siglb,sigr,sigrb,sum, sumb,summ,summ2,varl,fmaxl(MAXD),fmaxr(MAXD),fminl(MAXD), fminr(MAXD),pt(MAXD),rmid(MAXD),stack(NSTACK),stf(9) EQUIVALENCE (stf(1),avel),(stf(2),varl),(stf(3),jb), (stf(4),nptr),(stf(5),naddr),(stf(6),rgl),(stf(7),rgm), (stf(8),rgr),(stf(9),fracl) SAVE iran DATA iran /0/ jstack=0 nptt=npts continue if (nptt.lt.MNBS) then Too few points to bisect; do straight Monte Carlo. np=abs(nptt) summ=0. summ2=0. do 11 n=1,np call ranpt(pt,region,ndim)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
A second modification embodied in the code is the introduction of a “dithering parameter,” dith, whose nonzero value causes subvolumes to be divided not exactly down the middle, but rather into fractions 0.5±dith, with the sign of the ± randomly chosen by a built-in random number routine. Normally dith can be set to zero. However, there is a large advantage in taking dith to be nonzero if some special symmetry of the integrand puts the active region exactly at the midpoint of the region, or at the center of some power-of-two submultiple of the region. One wants to avoid the extreme case of the active region being evenly divided into 2d abutting corners of a d-dimensional space. A typical nonzero value of dith, on those occasions when it is useful, might be 0.1. Of course, when the dithering parameter is nonzero, we must take the differing sizes of the subvolumes into account; the code does this through the variable fracl. One final feature in the code deserves mention. The RSS algorithm uses a single set of sample points to evaluate equation (7.8.23) in all d directions. At bottom levels of the recursion, the number of sample points can be quite small. Although rare, it can happen that in one direction all the samples are in one half of the volume; in that case, that direction is ignored as a candidate for bifurcation. Even more rare is the possibility that all of the samples are in one half of the volume in all directions. In this case, a random direction is chosen. If this happens too often in your application, then you should increase MNPT (see line if (jb.eq.0). . . in the code). Note that miser, as given, returns as ave an estimate of the average function value hhf ii, not the integral of f over the region. The routine vegas, adopting the other convention, returns as tgral the integral. The two conventions are of course trivially related, by equation (7.8.8), since the volume V of the rectangular region is known.
7.8 Adaptive and Recursive Monte Carlo Methods
317
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
fval=func(pt) summ=summ+fval summ2=summ2+fval**2 enddo 11 ave=summ/np var=max(TINY,(summ2-summ**2/np)/np**2) else Do the preliminary (uniform) sampling. npre=max(int(nptt*PFAC),MNPT) do 12 j=1,ndim Initialize the left and right bounds for each dimension. iran=mod(iran*2661+36979,175000) s=sign(dith,float(iran-87500)) rmid(j)=(0.5+s)*region(j)+(0.5-s)*region(j+ndim) fminl(j)=BIG fminr(j)=BIG fmaxl(j)=-BIG fmaxr(j)=-BIG enddo 12 do 14 n=1,npre Loop over the points in the sample. call ranpt(pt,region,ndim) fval=func(pt) do 13 j=1,ndim Find the left and right bounds for each dimension. if(pt(j).le.rmid(j))then fminl(j)=min(fminl(j),fval) fmaxl(j)=max(fmaxl(j),fval) else fminr(j)=min(fminr(j),fval) fmaxr(j)=max(fmaxr(j),fval) endif enddo 13 enddo 14 sumb=BIG Choose which dimension jb to bisect. jb=0 siglb=1. sigrb=1. do 15 j=1,ndim if(fmaxl(j).gt.fminl(j).and.fmaxr(j).gt.fminr(j))then sigl=max(TINY,(fmaxl(j)-fminl(j))**(2./3.)) sigr=max(TINY,(fmaxr(j)-fminr(j))**(2./3.)) sum=sigl+sigr Equation (7.8.24), see text. if (sum.le.sumb) then sumb=sum jb=j siglb=sigl sigrb=sigr endif endif enddo 15 if (jb.eq.0) jb=1+(ndim*iran)/175000 MNPT may be too small. rgl=region(jb) Apportion the remaining points between left and right. rgm=rmid(jb) rgr=region(jb+ndim) fracl=abs((rgm-rgl)/(rgr-rgl)) nptl=MNPT+(nptt-npre-2*MNPT) * *fracl*siglb/(fracl*siglb+(1.-fracl)*sigrb) Equation (7.8.23). nptr=nptt-npre-nptl region(jb+ndim)=rgm Set region to left. naddr=1 Push the stack. do 16 j=1,9 stack(jstack+j)=stf(j) enddo 16 jstack=jstack+9 nptt=nptl goto 1 Dispatch recursive call; will return back here eventually. 10 continue
318
Chapter 7.
Random Numbers
The miser routine calls a short subroutine ranpt to get a random point within a specified d-dimensional region. The following version of ranpt makes consecutive calls to a uniform random number generator and does the obvious scaling. One can easily modify ranpt to generate its points via the quasi-random routine sobseq (§7.7). We find that miser with sobseq can be considerably more accurate than miser with uniform random deviates. Since the use of RSS and the use of quasi-random numbers are completely separable, however, we have not made the code given here dependent on sobseq. A similar remark might be made regarding importance sampling, which could in principle be combined with RSS. (One could in principle combine vegas and miser, although the programming would be intricate.)
C
SUBROUTINE ranpt(pt,region,n) INTEGER n,idum REAL pt(n),region(2*n) COMMON /ranno/ idum SAVE /ranno/ USES ran1 Returns a uniformly random point pt in an n-dimensional rectangular region. Used by miser; calls ran1 for uniform deviates. Your main program should initialize idum, through the COMMON block /ranno/, to a negative seed integer. INTEGER j REAL ran1 do 11 j=1,n pt(j)=region(j)+(region(j+n)-region(j))*ran1(idum) enddo 11 return END
CITED REFERENCES AND FURTHER READING: Hammersley, J.M. and Handscomb, D.C. 1964, Monte Carlo Methods (London: Methuen). Kalos, M.H. and Whitlock, P.A. 1986, Monte Carlo Methods (New York: Wiley). Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: Springer-Verlag).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
avel=ave Save left estimates on stack variable. varl=var region(jb)=rgm Set region to right. region(jb+ndim)=rgr naddr=2 Push the stack. do 17 j=1,9 stack(jstack+j)=stf(j) enddo 17 jstack=jstack+9 nptt=nptr goto 1 Dispatch recursive call; will return back here eventually. 20 continue region(jb)=rgl Restore region to original value (so that we don’t ave=fracl*avel+(1.-fracl)*ave need to include it on the stack). var=fracl**2*varl+(1.-fracl)**2*var Combine left and right regions by equaendif tion (7.8.11) (1st line). if (jstack.ne.0) then Pop the stack. jstack=jstack-9 do 18 j=1,9 stf(j)=stack(jstack+j) enddo 18 goto (10,20),naddr pause ’miser: never get here’ endif return END
319 7.8 Adaptive and Recursive Monte Carlo Methods
Lepage, G.P. 1978, Journal of Computational Physics, vol. 27, pp. 192–203. [1]
Lepage, G.P. 1980, “VEGAS: An Adaptive Multidimensional Integration Program,” Publication CLNS-80/447, Cornell University. [2] Press, W.H., and Farrar, G.R. 1990, Computers in Physics, vol. 4, pp. 190–195. [3]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Sorting
8.0 Introduction This chapter almost doesn’t belong in a book on numerical methods. However, some practical knowledge of techniques for sorting is an indispensable part of any good programmer’s expertise. We would not want you to consider yourself expert in numerical techniques while remaining ignorant of so basic a subject. In conjunction with numerical work, sorting is frequently necessary when data (either experimental or numerically generated) are being handled. One has tables or lists of numbers, representing one or more independent (or “control”) variables, and one or more dependent (or “measured”) variables. One may wish to arrange these data, in various circumstances, in order by one or another of these variables. Alternatively, one may simply wish to identify the “median” value, or the “upper quartile” value of one of the lists of values. This task, closely related to sorting, is called selection. Here, more specifically, are the tasks that this chapter will deal with: • Sort, i.e., rearrange, an array of numbers into numerical order. • Rearrange an array into numerical order while performing the corresponding rearrangement of one or more additional arrays, so that the correspondence between elements in all arrays is maintained. • Given an array, prepare an index table for it, i.e., a table of pointers telling which number array element comes first in numerical order, which second, and so on. • Given an array, prepare a rank table for it, i.e., a table telling what is the numerical rank of the first array element, the second array element, and so on. • Select the M th largest element from an array. For the basic task of sorting N elements, the best algorithms require on the order of several times N log2 N operations. The algorithm inventor tries to reduce the constant in front of this estimate to as small a value as possible. Two of the best algorithms are Quicksort (§8.2), invented by the inimitable C.A.R. Hoare, and Heapsort (§8.3), invented by J.W.J. Williams. For large N (say > 1000), Quicksort is faster, on most machines, by a factor of 1.5 or 2; it requires a bit of extra memory, however, and is a moderately complicated program. Heapsort is a true “sort in place,” and is somewhat more compact to program and therefore a bit easier to modify for special purposes. On balance, we recommend Quicksort because of its speed, but we implement both routines. 320
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Chapter 8.
8.1 Straight Insertion and Shell’s Method
321
CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading, MA: Addison-Wesley). [1] Sedgewick, R. 1988, Algorithms, 2nd ed. (Reading, MA: Addison-Wesley), Chapters 8–13. [2]
8.1 Straight Insertion and Shell’s Method Straight insertion is an N 2 routine, and should be used only for small N , say < 20. The technique is exactly the one used by experienced card players to sort their cards: Pick out the second card and put it in order with respect to the first; then pick out the third card and insert it into the sequence among the first two; and so on until the last card has been picked out and inserted. SUBROUTINE piksrt(n,arr) INTEGER n REAL arr(n) Sorts an array arr(1:n) into ascending numerical order, by straight insertion. n is input; arr is replaced on output by its sorted rearrangement. INTEGER i,j REAL a do 12 j=2,n Pick out each element in turn. a=arr(j) do 11 i=j-1,1,-1 Look for the place to insert it. if(arr(i).le.a)goto 10 arr(i+1)=arr(i) enddo 11 i=0 10 arr(i+1)=a Insert it. enddo 12 return END
What if you also want to rearrange an array brr at the same time as you sort arr? Simply move an element of brr whenever you move an element of arr:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
For small N one does better to use an algorithm whose operation count goes as a higher, i.e., poorer, power of N , if the constant in front is small enough. For N < 20, roughly, the method of straight insertion (§8.1) is concise and fast enough. We include it with some trepidation: It is an N 2 algorithm, whose potential for misuse (by using it for too large an N ) is great. The resultant waste of computer time is so awesome, that we were tempted not to include any N 2 routine at all. We will draw the line, however, at the inefficient N 2 algorithm, beloved of elementary computer science texts, called bubble sort. If you know what bubble sort is, wipe it from your mind; if you don’t know, make a point of never finding out! For N < 50, roughly, Shell’s method (§8.1), only slightly more complicated to program than straight insertion, is competitive with the more complicated Quicksort on many machines. This method goes as N 3/2 in the worst case, but is usually faster. See references [1,2] for further information on the subject of sorting, and for detailed references to the literature.
322
Chapter 8.
Sorting
For the case of rearranging a larger number of arrays by sorting on one of them, see §8.4.
Shell’s Method This is actually a variant on straight insertion, but a very powerful variant indeed. The rough idea, e.g., for the case of sorting 16 numbers n1 . . . n16 , is this: First sort, by straight insertion, each of the 8 groups of 2 (n1 , n9 ), (n2 , n10), . . . , (n8 , n16). Next, sort each of the 4 groups of 4 (n1 , n5 , n9 , n13 ), . . . , (n4 , n8 , n12 , n16). Next sort the 2 groups of 8 records, beginning with (n1 , n3 , n5 , n7 , n9 , n11, n13 , n15). Finally, sort the whole list of 16 numbers. Of course, only the last sort is necessary for putting the numbers into order. So what is the purpose of the previous partial sorts? The answer is that the previous sorts allow numbers efficiently to filter up or down to positions close to their final resting places. Therefore, the straight insertion passes on the final sort rarely have to go past more than a “few” elements before finding the right place. (Think of sorting a hand of cards that are already almost in order.) The spacings between the numbers sorted on each pass through the data (8,4,2,1 in the above example) are called the increments, and a Shell sort is sometimes called a diminishing increment sort. There has been a lot of research into how to choose a good set of increments, but the optimum choice is not known. The set . . . , 8, 4, 2, 1 is in fact not a good choice, especially for N a power of 2. A much better choice is the sequence (3k − 1)/2, . . . , 40, 13, 4, 1
(8.1.1)
which can be generated by the recurrence i1 = 1,
ik+1 = 3ik + 1,
k = 1, 2, . . .
(8.1.2)
It can be shown (see [1]) that for this sequence of increments the number of operations required in all is of order N 3/2 for the worst possible ordering of the original data.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE piksr2(n,arr,brr) INTEGER n REAL arr(n),brr(n) Sorts an array arr(1:n) into ascending numerical order, by straight insertion, while making the corresponding rearrangement of the array brr(1:n). INTEGER i,j REAL a,b do 12 j=2,n Pick out each element in turn. a=arr(j) b=brr(j) do 11 i=j-1,1,-1 Look for the place to insert it. if(arr(i).le.a)goto 10 arr(i+1)=arr(i) brr(i+1)=brr(i) enddo 11 i=0 10 arr(i+1)=a Insert it. brr(i+1)=b enddo 12 return END
8.2 Quicksort
323
For “randomly” ordered data, the operations count goes approximately as N 1.25, at least for N < 60000. For N > 50, however, Quicksort is generally faster. The program follows:
2
3
4
CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §5.2.1. [1] Sedgewick, R. 1988, Algorithms, 2nd ed. (Reading, MA: Addison-Wesley), Chapter 8.
8.2 Quicksort Quicksort is, on most machines, on average, for large N , the fastest known sorting algorithm. It is a “partition-exchange” sorting method: A “partitioning element” a is selected from the array. Then by pairwise exchanges of elements, the original array is partitioned into two subarrays. At the end of a round of partitioning, the element a is in its final place in the array. All elements in the left subarray are ≤ a, while all elements in the right subarray are ≥ a. The process is then repeated on the left and right subarrays independently, and so on. The partitioning process is carried out by selecting some element, say the leftmost, as the partitioning element a. Scan a pointer up the array until you find an element > a, and then scan another pointer down from the end of the array until you find an element < a. These two elements are clearly out of place for the final partitioned array, so exchange them. Continue this process until the pointers
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
SUBROUTINE shell(n,a) INTEGER n REAL a(n) Sorts an array a(1:n) into ascending numerical order by Shell’s method (diminishing increment sort). n is input; a is replaced on output by its sorted rearrangement. INTEGER i,j,inc REAL v inc=1 Determine the starting increment. inc=3*inc+1 if(inc.le.n)goto 1 continue Loop over the partial sorts. inc=inc/3 do 11 i=inc+1,n Outer loop of straight insertion. v=a(i) j=i if(a(j-inc).gt.v)then Inner loop of straight insertion. a(j)=a(j-inc) j=j-inc if(j.le.inc)goto 4 goto 3 endif a(j)=v enddo 11 if(inc.gt.1)goto 2 return END
324
Chapter 8.
Sorting
1
2
SUBROUTINE sort(n,arr) INTEGER n,M,NSTACK REAL arr(n) PARAMETER (M=7,NSTACK=50) Sorts an array arr(1:n) into ascending numerical order using the Quicksort algorithm. n is input; arr is replaced on output by its sorted rearrangement. Parameters: M is the size of subarrays sorted by straight insertion and NSTACK is the required auxiliary storage. INTEGER i,ir,j,jstack,k,l,istack(NSTACK) REAL a,temp jstack=0 l=1 ir=n if(ir-l.lt.M)then Insertion sort when subarray small enough. do 12 j=l+1,ir a=arr(j) do 11 i=j-1,l,-1 if(arr(i).le.a)goto 2 arr(i+1)=arr(i) enddo 11 i=l-1 arr(i+1)=a enddo 12
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
cross. This is the right place to insert a, and that round of partitioning is done. The question of the best strategy when an element is equal to the partitioning element is subtle; we refer you to Sedgewick [1] for a discussion. (Answer: You should stop and do an exchange.) Quicksort requires an auxiliary array of storage, of length 2 log2 N , which it uses as a push-down stack for keeping track of the pending subarrays. When a subarray has gotten down to some size M , it becomes faster to sort it by straight insertion (§8.1), so we will do this. The optimal setting of M is machine dependent, but M = 7 is not too far wrong. Some people advocate leaving the short subarrays unsorted until the end, and then doing one giant insertion sort at the end. Since each element moves at most 7 places, this is just as efficient as doing the sorts immediately, and saves on the overhead. However, on modern machines with paged memory, there is increased overhead when dealing with a large array all at once. We have not found any advantage in saving the insertion sorts till the end. As already mentioned, Quicksort’s average running time is fast, but its worst case running time can be very slow: For the worst case it is, in fact, an N 2 method! And for the most straightforward implementation of Quicksort it turns out that the worst case is achieved for an input array that is already in order! This ordering of the input array might easily occur in practice. One way to avoid this is to use a little random number generator to choose a random element as the partitioning element. Another is to use instead the median of the first, middle, and last elements of the current subarray. The great speed of Quicksort comes from the simplicity and efficiency of its inner loop. Simply adding one unnecessary test (for example, a test that your pointer has not moved off the end of the array) can almost double the running time! One avoids such unnecessary tests by placing “sentinels” at either end of the subarray being partitioned. The leftmost sentinel is ≤ a, the rightmost ≥ a. With the “median-of-three” selection of a partitioning element, we can use the two elements that were not the median to be the sentinels for that subarray. Our implementation closely follows [1]:
8.2 Quicksort
4
5
if(jstack.eq.0)return ir=istack(jstack) Pop stack and begin a new round of partitioning. l=istack(jstack-1) jstack=jstack-2 else k=(l+ir)/2 Choose median of left, center, and right elements as partemp=arr(k) titioning element a. Also rearrange so that a(l) ≤ arr(k)=arr(l+1) a(l+1) ≤ a(ir). arr(l+1)=temp if(arr(l).gt.arr(ir))then temp=arr(l) arr(l)=arr(ir) arr(ir)=temp endif if(arr(l+1).gt.arr(ir))then temp=arr(l+1) arr(l+1)=arr(ir) arr(ir)=temp endif if(arr(l).gt.arr(l+1))then temp=arr(l) arr(l)=arr(l+1) arr(l+1)=temp endif i=l+1 Initialize pointers for partitioning. j=ir a=arr(l+1) Partitioning element. continue Beginning of innermost loop. i=i+1 Scan up to find element > a. if(arr(i).lt.a)goto 3 continue j=j-1 Scan down to find element < a. if(arr(j).gt.a)goto 4 if(j.lt.i)goto 5 Pointers crossed. Exit with partitioning complete. temp=arr(i) Exchange elements. arr(i)=arr(j) arr(j)=temp goto 3 End of innermost loop. arr(l+1)=arr(j) Insert partitioning element. arr(j)=a jstack=jstack+2 Push pointers to larger subarray on stack, process smaller subarray immediately. if(jstack.gt.NSTACK)pause ’NSTACK too small in sort’ if(ir-i+1.ge.j-l)then istack(jstack)=ir istack(jstack-1)=i ir=j-1 else istack(jstack)=j-1 istack(jstack-1)=l l=i endif endif goto 1 END
As usual you can move any other arrays around at the same time as you sort arr. At the risk of being repetitious:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
3
325
326
2
Sorting
SUBROUTINE sort2(n,arr,brr) INTEGER n,M,NSTACK REAL arr(n),brr(n) PARAMETER (M=7,NSTACK=50) Sorts an array arr(1:n) into ascending order using Quicksort, while making the corresponding rearrangement of the array brr(1:n). INTEGER i,ir,j,jstack,k,l,istack(NSTACK) REAL a,b,temp jstack=0 l=1 ir=n if(ir-l.lt.M)then Insertion sort when subarray small enough. do 12 j=l+1,ir a=arr(j) b=brr(j) do 11 i=j-1,l,-1 if(arr(i).le.a)goto 2 arr(i+1)=arr(i) brr(i+1)=brr(i) enddo 11 i=l-1 arr(i+1)=a brr(i+1)=b enddo 12 if(jstack.eq.0)return ir=istack(jstack) Pop stack and begin a new round of partitioning. l=istack(jstack-1) jstack=jstack-2 else k=(l+ir)/2 Choose median of left, center and right elements as partemp=arr(k) titioning element a. Also rearrange so that a(l) ≤ arr(k)=arr(l+1) a(l+1) ≤ a(ir). arr(l+1)=temp temp=brr(k) brr(k)=brr(l+1) brr(l+1)=temp if(arr(l).gt.arr(ir))then temp=arr(l) arr(l)=arr(ir) arr(ir)=temp temp=brr(l) brr(l)=brr(ir) brr(ir)=temp endif if(arr(l+1).gt.arr(ir))then temp=arr(l+1) arr(l+1)=arr(ir) arr(ir)=temp temp=brr(l+1) brr(l+1)=brr(ir) brr(ir)=temp endif if(arr(l).gt.arr(l+1))then temp=arr(l) arr(l)=arr(l+1) arr(l+1)=temp temp=brr(l) brr(l)=brr(l+1) brr(l+1)=temp endif i=l+1 Initialize pointers for partitioning. j=ir a=arr(l+1) Partitioning element. b=brr(l+1)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 8.
327
8.3 Heapsort
3
4
You could, in principle, rearrange any number of additional arrays along with brr, but this becomes wasteful as the number of such arrays becomes large. The preferred technique is to make use of an index table, as described in §8.4. CITED REFERENCES AND FURTHER READING: Sedgewick, R. 1978, Communications of the ACM, vol. 21, pp. 847–857. [1]
8.3 Heapsort While usually not quite as fast as Quicksort, Heapsort is one of our favorite sorting routines. It is a true “in-place” sort, requiring no auxiliary storage. It is an N log2 N process, not only on average, but also for the worst-case order of input data. In fact, its worst case is only 20 percent or so worse than its average running time. It is beyond our scope to give a complete exposition on the theory of Heapsort. We will mention the general principles, then let you refer to the references [1,2] , or analyze the program yourself, if you want to understand the details. A set of N numbers ai , i = 1, . . . , N , is said to form a “heap” if it satisfies the relation aj/2 ≥ aj
for 1 ≤ j/2 < j ≤ N
(8.3.1)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
5
continue Beginning of innermost loop. i=i+1 Scan up to find element > a. if(arr(i).lt.a)goto 3 continue j=j-1 Scan down to find element < a. if(arr(j).gt.a)goto 4 if(j.lt.i)goto 5 Pointers crossed. Exit with partitioning complete. temp=arr(i) Exchange elements of both arrays. arr(i)=arr(j) arr(j)=temp temp=brr(i) brr(i)=brr(j) brr(j)=temp goto 3 End of innermost loop. arr(l+1)=arr(j) Insert partitioning element in both arrays. arr(j)=a brr(l+1)=brr(j) brr(j)=b jstack=jstack+2 Push pointers to larger subarray on stack, process smaller subarray immediately. if(jstack.gt.NSTACK)pause ’NSTACK too small in sort2’ if(ir-i+1.ge.j-l)then istack(jstack)=ir istack(jstack-1)=i ir=j-1 else istack(jstack)=j-1 istack(jstack-1)=l l=i endif endif goto 1 END
328
Chapter 8.
Sorting
a1
a2
a3
a8
a5 a9
a10
a6 a11
a7
a12
Figure 8.3.1. Ordering implied by a “heap,” here of 12 elements. Elements connected by an upward path are sorted with respect to one another, but there is not necessarily any ordering among elements related only “laterally.”
Here the division in j/2 means “integer divide,” i.e., is an exact integer or else is rounded down to the closest integer. Definition (8.3.1) will make sense if you think of the numbers ai as being arranged in a binary tree, with the top, “boss,” node being a1 , the two “underling” nodes being a2 and a3 , their four underling nodes being a4 through a7 , etc. (See Figure 8.3.1.) In this form, a heap has every “supervisor” greater than or equal to its two “supervisees,” down through the levels of the hierarchy. If you have managed to rearrange your array into an order that forms a heap, then sorting it is very easy: You pull off the “top of the heap,” which will be the largest element yet unsorted. Then you “promote” to the top of the heap its largest underling. Then you promote its largest underling, and so on. The process is like what happens (or is supposed to happen) in a large corporation when the chairman of the board retires. You then repeat the whole process by retiring the new chairman of the board. Evidently the whole thing is an N log2 N process, since each retiring chairman leads to log2 N promotions of underlings. Well, how do you arrange the array into a heap in the first place? The answer is again a “sift-up” process like corporate promotion. Imagine that the corporation starts out with N/2 employees on the production line, but with no supervisors. Now a supervisor is hired to supervise two workers. If he is less capable than one of his workers, that one is promoted in his place, and he joins the production line. After supervisors are hired, then supervisors of supervisors are hired, and so on up the corporate ladder. Each employee is brought in at the top of the tree, but then immediately sifted down, with more capable workers promoted until their proper corporate level has been reached. In the Heapsort implementation, the same “sift-up” code can be used for the initial creation of the heap and for the subsequent retirement-and-promotion phase. One execution of the Heapsort subroutine represents the entire life-cycle of a giant corporation: N/2 workers are hired; N/2 potential supervisors are hired; there is a sifting up in the ranks, a sort of super Peter Principle: in due course, each of the original employees gets promoted to chairman of the board.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
a4
8.4 Indexing and Ranking
329
CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §5.2.3. [1] Sedgewick, R. 1988, Algorithms, 2nd ed. (Reading, MA: Addison-Wesley), Chapter 11. [2]
8.4 Indexing and Ranking The concept of keys plays a prominent role in the management of data files. A data record in such a file may contain several items, or fields. For example, a record in a file of weather observations may have fields recording time, temperature, and
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE hpsort(n,ra) INTEGER n REAL ra(n) Sorts an array ra(1:n) into ascending numerical order using the Heapsort algorithm. n is input; ra is replaced on output by its sorted rearrangement. INTEGER i,ir,j,l REAL rra if (n.lt.2) return The index l will be decremented from its initial value down to 1 during the “hiring” (heap creation) phase. Once it reaches 1, the index ir will be decremented from its initial value down to 1 during the “retirement-and-promotion” (heap selection) phase. l=n/2+1 ir=n 10 continue if(l.gt.1)then Still in hiring phase. l=l-1 rra=ra(l) else In retirement-and-promotion phase. rra=ra(ir) Clear a space at end of array. ra(ir)=ra(1) Retire the top of the heap into it. ir=ir-1 Decrease the size of the corporation. if(ir.eq.1)then Done with the last promotion. ra(1)=rra The least competent worker of all! return endif endif i=l Whether in the hiring phase or promotion phase, we here j=l+l set up to sift down element rra to its proper level. 20 if(j.le.ir)then “Do while j.le.ir:” if(j.lt.ir)then if(ra(j).lt.ra(j+1))j=j+1 Compare to the better underling. endif if(rra.lt.ra(j))then Demote rra. ra(i)=ra(j) i=j j=j+j else This is rra’s level. Set j to terminate the sift-down. j=ir+1 endif goto 20 endif ra(i)=rra Put rra into its slot. goto 10 END
330
Chapter 8.
original array
index table
rank table
sorted array
5
4
3
14 1
1 8
1 4
2 32
3
1 5 5 6
(b)
15 5
3 6
14 4
6
15
(a)
2 4
5
8 3
1
3
6
6 3
4
7 2
2
7
5
3 2
3
4
1
32 6
(c)
(d)
Figure 8.4.1. (a) An unsorted array of six numbers. (b) Index table, whose entries are pointers to the elements of (a) in ascending order. (c) Rank table, whose entries are the ranks of the corresponding elements of (a). (d) Sorted array of the elements in (a).
wind velocity. When we sort the records, we must decide which of these fields we want to be brought into sorted order. The other fields in a record just come along for the ride, and will not, in general, end up in any particular order. The field on which the sort is performed is called the key field. For a data file with many records and many fields, the actual movement of N records into the sorted order of their keys Ki , i = 1, . . . , N , can be a daunting task. Instead, one can construct an index table Ij , j = 1, . . . , N , such that the smallest Ki has i = I1 , the second smallest has i = I2 , and so on up to the largest Ki with i = IN . In other words, the array KIj
j = 1, 2, . . . , N
(8.4.1)
is in sorted order when indexed by j. When an index table is available, one need not move records from their original order. Further, different index tables can be made from the same set of records, indexing them to different keys. The algorithm for constructing an index table is straightforward: Initialize the index array with the integers from 1 to N , then perform the Quicksort algorithm, moving the elements around as if one were sorting the keys. The integer that initially numbered the smallest key thus ends up in the number one position, and so on. SUBROUTINE indexx(n,arr,indx) INTEGER n,indx(n),M,NSTACK REAL arr(n) PARAMETER (M=7,NSTACK=50) Indexes an array arr(1:n), i.e., outputs the array indx(1:n) such that arr(indx(j)) is in ascending order for j = 1, 2, . . . , N . The input quantities n and arr are not changed.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
Sorting
8.4 Indexing and Ranking
2
3
4
5
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
INTEGER i,indxt,ir,itemp,j,jstack,k,l,istack(NSTACK) REAL a do 11 j=1,n indx(j)=j enddo 11 jstack=0 l=1 ir=n if(ir-l.lt.M)then do 13 j=l+1,ir indxt=indx(j) a=arr(indxt) do 12 i=j-1,l,-1 if(arr(indx(i)).le.a)goto 2 indx(i+1)=indx(i) enddo 12 i=l-1 indx(i+1)=indxt enddo 13 if(jstack.eq.0)return ir=istack(jstack) l=istack(jstack-1) jstack=jstack-2 else k=(l+ir)/2 itemp=indx(k) indx(k)=indx(l+1) indx(l+1)=itemp if(arr(indx(l)).gt.arr(indx(ir)))then itemp=indx(l) indx(l)=indx(ir) indx(ir)=itemp endif if(arr(indx(l+1)).gt.arr(indx(ir)))then itemp=indx(l+1) indx(l+1)=indx(ir) indx(ir)=itemp endif if(arr(indx(l)).gt.arr(indx(l+1)))then itemp=indx(l) indx(l)=indx(l+1) indx(l+1)=itemp endif i=l+1 j=ir indxt=indx(l+1) a=arr(indxt) continue i=i+1 if(arr(indx(i)).lt.a)goto 3 continue j=j-1 if(arr(indx(j)).gt.a)goto 4 if(j.lt.i)goto 5 itemp=indx(i) indx(i)=indx(j) indx(j)=itemp goto 3 indx(l+1)=indx(j) indx(j)=indxt jstack=jstack+2 if(jstack.gt.NSTACK)pause ’NSTACK too small in indexx’ if(ir-i+1.ge.j-l)then istack(jstack)=ir
331
332
Chapter 8.
Sorting
If you want to sort an array while making the corresponding rearrangement of several or many other arrays, you should first make an index table, then use it to rearrange each array in turn. This requires two arrays of working space: one to hold the index, and another into which an array is temporarily moved, and from which it is redeposited back on itself in the rearranged order. For 3 arrays, the procedure looks like this:
C
SUBROUTINE sort3(n,ra,rb,rc,wksp,iwksp) INTEGER n,iwksp(n) REAL ra(n),rb(n),rc(n),wksp(n) USES indexx Sorts an array ra(1:n) into ascending numerical order while making the corresponding rearrangements of the arrays rb(1:n) and rc(1:n). An index table is constructed via the routine indexx. INTEGER j call indexx(n,ra,iwksp) Make the index table. do 11 j=1,n Save the array ra. wksp(j)=ra(j) enddo 11 do 12 j=1,n Copy it back in the rearranged order. ra(j)=wksp(iwksp(j)) enddo 12 do 13 j=1,n Ditto rb. wksp(j)=rb(j) enddo 13 do 14 j=1,n rb(j)=wksp(iwksp(j)) enddo 14 do 15 j=1,n Ditto rc. wksp(j)=rc(j) enddo 15 do 16 j=1,n rc(j)=wksp(iwksp(j)) enddo 16 return END
The generalization to any other number of arrays is obviously straightforward. A rank table is different from an index table. A rank table’s jth entry gives the rank of the jth element of the original array of keys, ranging from 1 (if that element was the smallest) to N (if that element was the largest). One can easily construct a rank table from an index table, however:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
istack(jstack-1)=i ir=j-1 else istack(jstack)=j-1 istack(jstack-1)=l l=i endif endif goto 1 END
8.5 Selecting the Mth Largest
333
Figure 8.4.1 summarizes the concepts discussed in this section.
8.5 Selecting the Mth Largest Selection is sorting’s austere sister. (Say that five times quickly!) Where sorting demands the rearrangement of an entire data array, selection politely asks for a single returned value: What is the kth smallest (or, equivalently, the m = N +1−kth largest) element out of N elements? The fastest methods for selection do, unfortunately, rearrange the array for their own computational purposes, typically putting all smaller elements to the left of the kth, all larger elements to the right, and scrambling the order within each subset. This side effect is at best innocuous, at worst downright inconvenient. When the array is very long, so that making a scratch copy of it is taxing on memory, or when the computational burden of the selection is a negligible part of a larger calculation, one turns to selection algorithms without side effects, which leave the original array undisturbed. Such in place selection is slower than the faster selection methods by a factor of about 10. We give routines of both types, below. The most common use of selection is in the statistical characterization of a set of data. One often wants to know the median element in an array, or the top and bottom quartile elements. When N is odd, the median is the kth element, with k = (N + 1)/2. When N is even, statistics books define the median as the arithmetic mean of the elements k = N/2 and k = N/2 + 1 (that is, N/2 from the bottom and N/2 from the top). If you accept such pedantry, you must perform two separate selections to find these elements. For N > 100 we usually define k = N/2 to be the median element, pedants be damned. The fastest general method for selection, allowing rearrangement, is partitioning, exactly as was done in the Quicksort algorithm (§8.2). Selecting a “random” partition element, one marches through the array, forcing smaller elements to the left, larger elements to the right. As in Quicksort, it is important to optimize the inner loop, using “sentinels” (§8.2) to minimize the number of comparisons. For sorting, one would then proceed to further partition both subsets. For selection, we can ignore one subset and attend only to the one that contains our desired kth element. Selection by partitioning thus does not need a stack of pending operations, and its operations count scales as N rather than as N log N (see [1]). Comparison with sort in §8.2 should make the following routine obvious:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE rank(n,indx,irank) INTEGER n,indx(n),irank(n) Given indx(1:n) as output from the routine indexx, this routine returns an array irank(1:n), the corresponding table of ranks. INTEGER j do 11 j=1,n irank(indx(j))=j enddo 11 return END
334
3
4
5
Sorting
FUNCTION select(k,n,arr) INTEGER k,n REAL select,arr(n) Returns the kth smallest value in the array arr(1:n). The input array will be rearranged to have this value in location arr(k), with all smaller elements moved to arr(1:k-1) (in arbitrary order) and all larger elements in arr[k+1..n] (also in arbitrary order). INTEGER i,ir,j,l,mid REAL a,temp l=1 ir=n if(ir-l.le.1)then Active partition contains 1 or 2 elements. if(ir-l.eq.1)then Active partition contains 2 elements. if(arr(ir).lt.arr(l))then temp=arr(l) arr(l)=arr(ir) arr(ir)=temp endif endif select=arr(k) return else mid=(l+ir)/2 Choose median of left, center, and right elements as partemp=arr(mid) titioning element a. Also rearrange so that arr(l) ≤ arr(mid)=arr(l+1) arr(l+1), arr(ir) ≥ arr(l+1). arr(l+1)=temp if(arr(l).gt.arr(ir))then temp=arr(l) arr(l)=arr(ir) arr(ir)=temp endif if(arr(l+1).gt.arr(ir))then temp=arr(l+1) arr(l+1)=arr(ir) arr(ir)=temp endif if(arr(l).gt.arr(l+1))then temp=arr(l) arr(l)=arr(l+1) arr(l+1)=temp endif i=l+1 Initialize pointers for partitioning. j=ir a=arr(l+1) Partitioning element. continue Beginning of innermost loop. i=i+1 Scan up to find element > a. if(arr(i).lt.a)goto 3 continue j=j-1 Scan down to find element < a. if(arr(j).gt.a)goto 4 if(j.lt.i)goto 5 Pointers crossed. Exit with partitioning complete. temp=arr(i) Exchange elements. arr(i)=arr(j) arr(j)=temp goto 3 End of innermost loop. arr(l+1)=arr(j) Insert partitioning element. arr(j)=a if(j.ge.k)ir=j-1 Keep active the partition that contains the kth element. if(j.le.k)l=i endif goto 1 END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 8.
8.5 Selecting the Mth Largest
335
M ∼2
√
log2 N
(8.5.1)
The square root of the logarithm is so slowly varying that secondary considerations of machine timing become important. We use M = 64 as a convenient constant value. Two minor additional tricks in the following routine, selip, are (i) augmenting the set of M random values by an M + 1st, the arithmetic mean, and (ii) choosing the M random values “on the fly” in a pass through the data, by a method that makes later values no less likely to be chosen than earlier ones. (The underlying idea is to give element m > M an M/m chance of being brought into the set. You can prove by induction that this yields the desired result.)
C
1
FUNCTION selip(k,n,arr) INTEGER k,n,M REAL selip,arr(n),BIG PARAMETER (M=64,BIG=1.E30) Returns the kth smallest value in the array arr(1:n). The input array is not altered. USES shell INTEGER i,j,jl,jm,ju,kk,mm,nlo,nxtmm,isel(M+2) REAL ahi,alo,sum,sel(M+2) if(k.lt.1.or.k.gt.n.or.n.le.0) pause ’bad input to selip’ kk=k ahi=BIG alo=-BIG continue Main iteration loop, until desired element is isolated. mm=0 nlo=0 sum=0. nxtmm=M+1 do 11 i=1,n Make a pass through the whole array. if(arr(i).ge.alo.and.arr(i).le.ahi)then Consider only elements in the curmm=mm+1 rent brackets. if(arr(i).eq.alo) nlo=nlo+1 In case of ties for low bracket. if(mm.le.M)then Statistical procedure for selecting m in-range elements sel(mm)=arr(i) with equal probability, even without knowing in else if(mm.eq.nxtmm)then advance how many there are! nxtmm=mm+mm/M sel(1+mod(i+mm+kk,M))=arr(i) The mod function provides a someendif what random number. sum=sum+arr(i)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
In-place, nondestructive, selection is conceptually simple, but it requires a lot of bookkeeping, and it is correspondingly slower. The general idea is to pick some number M of elements at random, to sort them, and then to make a pass through the array counting how many elements fall in each of the M + 1 intervals defined by these elements. The kth largest will fall in one such interval — call it the “live” interval. One then does a second round, first picking M random elements in the live interval, and then determining which of the new, finer, M + 1 intervals all presently live elements fall into. And so on, until the kth element is finally localized within a single array of size M , at which point direct selection is possible. How shall we pick M ? The number of rounds, logM N = log2 N/ log2 M , will be smaller if M is larger; but the work to locate each element among M + 1 subintervals will be larger, scaling as log2 M for bisection, say. Each round requires looking at all N elements, if only to find those that are still alive, while the bisections are dominated by the N that occur in the first round. Minimizing O(N logM N ) + O(N log2 M ) thus yields the result
336
3
Sorting
endif enddo 11 if(kk.le.nlo)then Desired element is tied for lower bound; return it. selip=alo return else if(mm.le.M)then All in-range elements were kept. So return answer by call shell(mm,sel) direct method. selip=sel(kk) return endif Augment selected set by mean value (fixes degenerasel(M+1)=sum/mm cies), and sort it. call shell(M+1,sel) sel(M+2)=ahi do 12 j=1,M+2 Zero the count array. isel(j)=0 enddo 12 do 13 i=1,n Make another pass through the whole array. if(arr(i).ge.alo.and.arr(i).le.ahi)then For each in-range element.. jl=0 ju=M+2 if(ju-jl.gt.1)then ...find its position among the select by bisection... jm=(ju+jl)/2 if(arr(i).ge.sel(jm))then jl=jm else ju=jm endif goto 2 endif isel(ju)=isel(ju)+1 ...and increment the counter. endif enddo 13 j=1 Now we can narrow the bounds to just one bin, that if(kk.gt.isel(j))then is, by a factor of order m. alo=sel(j) kk=kk-isel(j) j=j+1 goto 3 endif ahi=sel(j) goto 1 END
Approximate timings: selip is about 10 times slower than select. Indeed, for N in the range of ∼ 105 , selip is about 1.5 times slower than a full sort with sort, while select is about 6 times faster than sort. You should weigh time against memory and convenience carefully. Of course neither of the above routines should be used for the trivial cases of finding the largest, or smallest, element in an array. Those cases, you code by hand as simple do loops. There are also good ways to code the case where k is modest in comparison to N , so that extra memory of order k is not burdensome. An example is to use the method of Heapsort (§8.3) to make a single pass through an array of length N while saving the m largest elements. The advantage of the heap structure is that only log m, rather than m, comparisons are required every time a new √element is added to the candidate list. This becomes a real savings when m > O( N), but it never hurts otherwise and is easy to code. The following program gives the idea. SUBROUTINE hpsel(m,n,arr,heap) INTEGER m,n
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
Chapter 8.
8.6 Determination of Equivalence Classes
C
2
REAL arr(n),heap(m) USES sort Returns in heap(1:m) the largest m elements of the array arr(1:n), with heap(1) guaranteed to be the the mth largest element. The array arr is not altered. For efficiency, this routine should be used only when m n. INTEGER i,j,k REAL swap if (m.gt.n/2.or.m.lt.1) pause ’probable misuse of hpsel’ do 11 i=1,m heap(i)=arr(i) enddo 11 call sort(m,heap) Create initial heap by overkill! We assume m n. do 12 i=m+1,n For each remaining element... if(arr(i).gt.heap(1))then Put it on the heap? heap(1)=arr(i) j=1 continue Sift down. k=2*j if(k.gt.m)goto 2 if(k.ne.m)then if(heap(k).gt.heap(k+1))k=k+1 endif if(heap(j).le.heap(k))goto 2 swap=heap(k) heap(k)=heap(j) heap(j)=swap j=k goto 1 continue endif enddo 12 return end
CITED REFERENCES AND FURTHER READING: Sedgewick, R. 1988, Algorithms, 2nd ed. (Reading, MA: Addison-Wesley), pp. 126ff. [1] Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading, MA: Addison-Wesley).
8.6 Determination of Equivalence Classes A number of techniques for sorting and searching relate to data structures whose details are beyond the scope of this book, for example, trees, linked lists, etc. These structures and their manipulations are the bread and butter of computer science, as distinct from numerical analysis, and there is no shortage of books on the subject. In working with experimental data, we have found that one particular such manipulation, namely the determination of equivalence classes, arises sufficiently often to justify inclusion here. The problem is this: There are N “elements” (or “data points” or whatever), numbered 1, . . . , N . You are given pairwise information about whether elements are in the same equivalence class of “sameness,” by whatever criterion happens to be of interest. For example, you may have a list of facts like: “Element 3 and element 7 are in the same class; element 19 and element 4 are in the same class; element 7 and element 12 are in the same class, . . . .” Alternatively, you may have a procedure, given the numbers of two elements
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
337
338
Chapter 8.
Sorting
1
2
3
SUBROUTINE eclass(nf,n,lista,listb,m) INTEGER m,n,lista(m),listb(m),nf(n) Given m equivalences between pairs of n individual elements in the form of the input arrays lista(1:m) and listb(1:m), this routine returns in nf(1:n) the number of the equivalence class of each of the n elements, integers between 1 and n (not all such integers used). INTEGER j,k,l do 11 k=1,n Initialize each element its own class. nf(k)=k enddo 11 do 12 l=1,m For each piece of input information... j=lista(l) if(nf(j).ne.j)then Track first element up to its ancestor. j=nf(j) goto 1 endif k=listb(l) if(nf(k).ne.k)then Track second element up to its ancestor. k=nf(k) goto 2 endif if(j.ne.k)nf(j)=k If they are not already related, make them so. enddo 12 do 13 j=1,n Final sweep up to highest ancestors. if(nf(j).ne.nf(nf(j)))then nf(j)=nf(nf(j)) goto 3 endif enddo 13 return END
Alternatively, we may be able to construct a procedure equiv(j,k) that returns a value .true. if elements j and k are related, or .false. if they are not. Then we want to loop over all pairs of elements to get the complete picture. D. Eardley has devised a clever way of doing this while simultaneously sweeping the tree up to high ancestors in a manner that keeps it current and obviates most of the final sweep phase:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
j and k, for deciding whether they are in the same class or different classes. (Recall that an equivalence relation can be anything satisfying the RST properties: reflexive, symmetric, transitive. This is compatible with any intuitive definition of “sameness.”) The desired output is an assignment to each of the N elements of an equivalence class number, such that two elements are in the same class if and only if they are assigned the same class number. Efficient algorithms work like this: Let F (j) be the class or “family” number of element j. Start off with each element in its own family, so that F (j) = j. The array F (j) can be interpreted as a tree structure, where F (j) denotes the parent of j. If we arrange for each family to be its own tree, disjoint from all the other “family trees,” then we can label each family (equivalence class) by its most senior great-great-. . .grandparent. The detailed topology of the tree doesn’t matter at all, as long as we graft each related element onto it somewhere. Therefore, we process each elemental datum “j is equivalent to k” by (i) tracking j up to its highest ancestor, (ii) tracking k up to its highest ancestor, (iii) giving j to k as a new parent, or vice versa (it makes no difference). After processing all the relations, we go through all the elements j and reset their F (j)’s to their highest possible ancestors, which then label the equivalence classes. The following routine, based on Knuth [1], assumes that there are m elemental pieces of information, stored in two arrays of length m, lista,listb, the interpretation being that lista(j) and listb(j), j=1...m, are the numbers of two elements which (we are thus told) are related.
8.6 Determination of Equivalence Classes
339
CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1968, Fundamental Algorithms, vol. 1 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §2.3.3. [1] Sedgewick, R. 1988, Algorithms, 2nd ed. (Reading, MA: Addison-Wesley), Chapter 30.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE eclazz(nf,n,equiv) INTEGER n,nf(n) LOGICAL equiv EXTERNAL equiv Given a user-supplied logical function equiv which tells whether a pair of elements, each in the range 1...n, are related, return in nf equivalence class numbers for each element. INTEGER jj,kk nf(1)=1 do 12 jj=2,n Loop over first element of all pairs. nf(jj)=jj do 11 kk=1,jj-1 Loop over second element of all pairs. nf(kk)=nf(nf(kk)) Sweep it up this much. if (equiv(jj,kk)) nf(nf(nf(kk)))=jj Good exercise for the reader to figure enddo 11 out why this much ancestry is necessary! enddo 12 do 13 jj=1,n Only this much sweeping is needed finally. nf(jj)=nf(nf(jj)) enddo 13 return END
9.0 Introduction We now consider that most basic of tasks, solving equations numerically. While most equations are born with both a right-hand side and a left-hand side, one traditionally moves all terms to the left, leaving f(x) = 0
(9.0.1)
whose solution or solutions are desired. When there is only one independent variable, the problem is one-dimensional, namely to find the root or roots of a function. With more than one independent variable, more than one equation can be satisfied simultaneously. You likely once learned the implicit function theorem which (in this context) gives us the hope of satisfying N equations in N unknowns simultaneously. Note that we have only hope, not certainty. A nonlinear set of equations may have no (real) solutions at all. Contrariwise, it may have more than one solution. The implicit function theorem tells us that “generically” the solutions will be distinct, pointlike, and separated from each other. If, however, life is so unkind as to present you with a nongeneric, i.e., degenerate, case, then you can get a continuous family of solutions. In vector notation, we want to find one or more N -dimensional solution vectors x such that f(x) = 0
(9.0.2)
where f is the N -dimensional vector-valued function whose components are the individual equations to be satisfied simultaneously. Don’t be fooled by the apparent notational similarity of equations (9.0.2) and (9.0.1). Simultaneous solution of equations in N dimensions is much more difficult than finding roots in the one-dimensional case. The principal difference between one and many dimensions is that, in one dimension, it is possible to bracket or “trap” a root between bracketing values, and then hunt it down like a rabbit. In multidimensions, you can never be sure that the root is there at all until you have found it. Except in linear problems, root finding invariably proceeds by iteration, and this is equally true in one or in many dimensions. Starting from some approximate trial solution, a useful algorithm will improve the solution until some predetermined convergence criterion is satisfied. For smoothly varying functions, good algorithms 340
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Chapter 9. Root Finding and Nonlinear Sets of Equations
9.0 Introduction
341
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
will always converge, provided that the initial guess is good enough. Indeed one can even determine in advance the rate of convergence of most algorithms. It cannot be overemphasized, however, how crucially success depends on having a good first guess for the solution, especially for multidimensional problems. This crucial beginning usually depends on analysis rather than numerics. Carefully crafted initial estimates reward you not only with reduced computational effort, but also with understanding and increased self-esteem. Hamming’s motto, “the purpose of computing is insight, not numbers,” is particularly apt in the area of finding roots. You should repeat this motto aloud whenever your program converges, with ten-digit accuracy, to the wrong root of a problem, or whenever it fails to converge because there is actually no root, or because there is a root but your initial estimate was not sufficiently close to it. “This talk of insight is all very well, but what do I actually do?” For onedimensional root finding, it is possible to give some straightforward answers: You should try to get some idea of what your function looks like before trying to find its roots. If you need to mass-produce roots for many different functions, then you should at least know what some typical members of the ensemble look like. Next, you should always bracket a root, that is, know that the function changes sign in an identified interval, before trying to converge to the root’s value. Finally (this is advice with which some daring souls might disagree, but we give it nonetheless) never let your iteration method get outside of the best bracketing bounds obtained at any stage. We will see below that some pedagogically important algorithms, such as secant method or Newton-Raphson, can violate this last constraint, and are thus not recommended unless certain fixups are implemented. Multiple roots, or very close roots, are a real problem, especially if the multiplicity is an even number. In that case, there may be no readily apparent sign change in the function, so the notion of bracketing a root — and maintaining the bracket — becomes difficult. We are hard-liners: we nevertheless insist on bracketing a root, even if it takes the minimum-searching techniques of Chapter 10 to determine whether a tantalizing dip in the function really does cross zero or not. (You can easily modify the simple golden section routine of §10.1 to return early if it detects a sign change in the function. And, if the minimum of the function is exactly zero, then you have found a double root.) As usual, we want to discourage you from using routines as black boxes without understanding them. However, as a guide to beginners, here are some reasonable starting points: • Brent’s algorithm in §9.3 is the method of choice to find a bracketed root of a general one-dimensional function, when you cannot easily compute the function’s derivative. Ridders’ method (§9.2) is concise, and a close competitor. • When you can compute the function’s derivative, the routine rtsafe in §9.4, which combines the Newton-Raphson method with some bookkeeping on bounds, is recommended. Again, you must first bracket your root. • Roots of polynomials are a special case. Laguerre’s method, in §9.5, is recommended as a starting point. Beware: Some polynomials are ill-conditioned! • Finally, for multidimensional problems, the only elementary method is Newton-Raphson (§9.6), which works very well if you can supply a
342
Chapter 9.
Root Finding and Nonlinear Sets of Equations
1
SUBROUTINE scrsho(fx) INTEGER ISCR,JSCR REAL fx EXTERNAL fx PARAMETER (ISCR=60,JSCR=21) Number of horizontal and vertical positions in display. For interactive CRT terminal use. Produce a crude graph of the function fx over the prompted-for interval x1,x2. Query for another plot until the user signals satisfaction. INTEGER i,j,jz REAL dx,dyj,x,x1,x2,ybig,ysml,y(ISCR) CHARACTER*1 scr(ISCR,JSCR),blank,zero,yy,xx,ff SAVE blank,zero,yy,xx,ff DATA blank,zero,yy,xx,ff/’ ’,’-’,’l’,’-’,’x’/ continue write (*,*) ’ Enter x1,x2 (= to stop)’ Query for another plot, quit if x1=x2. read (*,*) x1,x2 if(x1.eq.x2) return do 11 j=1,JSCR Fill vertical sides with character ’l’. scr(1,j)=yy scr(ISCR,j)=yy enddo 11 do 13 i=2,ISCR-1 scr(i,1)=xx Fill top, bottom with character ’-’. scr(i,JSCR)=xx do 12 j=2,JSCR-1 Fill interior with blanks. scr(i,j)=blank enddo 12 enddo 13 dx=(x2-x1)/(ISCR-1) x=x1 ybig=0. Limits will include 0. ysml=ybig do 14 i=1,ISCR Evaluate the function at equal intervals. Find the y(i)=fx(x) largest and smallest values. if(y(i).lt.ysml) ysml=y(i) if(y(i).gt.ybig) ybig=y(i) x=x+dx enddo 14 if(ybig.eq.ysml) ybig=ysml+1. Be sure to separate top and bottom. dyj=(JSCR-1)/(ybig-ysml) jz=1-ysml*dyj Note which row corresponds to 0. do 15 i=1,ISCR Place an indicator at function height and 0. scr(i,jz)=zero j=1+(y(i)-ysml)*dyj scr(i,j)=ff enddo 15 write (*,’(1x,1pe10.3,1x,80a1)’) ybig,(scr(i,JSCR),i=1,ISCR) do 16 j=JSCR-1,2,-1 Display. write (*,’(12x,80a1)’) (scr(i,j),i=1,ISCR) enddo 16 write (*,’(1x,1pe10.3,1x,80a1)’) ysml,(scr(i,1),i=1,ISCR) write (*,’(12x,1pe10.3,40x,e10.3)’) x1,x2 goto 1 END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
good first guess of the solution. Try it. Then read the more advanced material in §9.7 for some more complicated, but globally more convergent, alternatives. Avoiding implementations for specific computers, this book must generally steer clear of interactive or graphics-related routines. We make an exception right now. The following routine, which produces a crude function plot with interactively scaled axes, can save you a lot of grief as you enter the world of root finding.
9.1 Bracketing and Bisection
343
CITED REFERENCES AND FURTHER READING: Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag), Chapter 5. Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathematical Association of America), Chapters 2, 7, and 14.
Householder, A.S. 1970, The Numerical Treatment of a Single Nonlinear Equation (New York: McGraw-Hill).
9.1 Bracketing and Bisection We will say that a root is bracketed in the interval (a, b) if f(a) and f(b) have opposite signs. If the function is continuous, then at least one root must lie in that interval (the intermediate value theorem). If the function is discontinuous, but bounded, then instead of a root there might be a step discontinuity which crosses zero (see Figure 9.1.1). For numerical purposes, that might as well be a root, since the behavior is indistinguishable from the case of a continuous function whose zero crossing occurs in between two “adjacent” floating-point numbers in a machine’s finite-precision representation. Only for functions with singularities is there the possibility that a bracketed root is not really there, as for example f(x) =
1 x−c
(9.1.1)
Some root-finding algorithms (e.g., bisection in this section) will readily converge to c in (9.1.1). Luckily there is not much possibility of your mistaking c, or any number x close to it, for a root, since mere evaluation of |f(x)| will give a very large, rather than a very small, result. If you are given a function in a black box, there is no sure way of bracketing its roots, or of even determining that it has roots. If you like pathological examples, think about the problem of locating the two real roots of equation (3.0.1), which dips below zero only in the ridiculously small interval of about x = π ± 10−667. In the next chapter we will deal with the related problem of bracketing a function’s minimum. There it is possible to give a procedure that always succeeds; in essence, “Go downhill, taking steps of increasing size, until your function starts back uphill.” There is no analogous procedure for roots. The procedure “go downhill until your function changes sign,” can be foiled by a function that has a simple extremum. Nevertheless, if you are prepared to deal with a “failure” outcome, this procedure is often a good first start; success is usual if your function has opposite signs in the limit x → ±∞.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), Chapter 8.
b a
x1
(a)
(b)
(c)
a
b
(d)
Figure 9.1.1. Some situations encountered while root finding: (a) shows an isolated root x1 bracketed by two points a and b at which the function has opposite signs; (b) illustrates that there is not necessarily a sign change in the function near a double root (in fact, there is not necessarily a root!); (c) is a pathological function with many roots; in (d) the function has opposite signs at points a and b, but the points bracket a singularity, not a root.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
a x2 x3 b x1 d c f e
Root Finding and Nonlinear Sets of Equations Chapter 9.
344
9.1 Bracketing and Bisection
345
Alternatively, you might want to “look inward” on an initial interval, rather than “look outward” from it, asking if there are any roots of the function f(x) in the interval from x1 to x2 when a search is carried out by subdivision into n equal intervals. The following subroutine returns brackets for up to nb distinct intervals which each contain one or more roots. SUBROUTINE zbrak(fx,x1,x2,n,xb1,xb2,nb) INTEGER n,nb REAL x1,x2,xb1(nb),xb2(nb),fx EXTERNAL fx Given a function fx defined on the interval from x1-x2 subdivide the interval into n equally spaced segments, and search for zero crossings of the function. nb is input as the maximum number of roots sought, and is reset to the number of bracketing pairs xb1(1:nb), xb2(1:nb) that are found. INTEGER i,nbb REAL dx,fc,fp,x nbb=0 x=x1 dx=(x2-x1)/n Determine the spacing appropriate to the mesh. fp=fx(x) do 11 i=1,n Loop over all intervals x=x+dx fc=fx(x) if(fc*fp.le.0.) then If a sign change occurs then record values for the bounds. nbb=nbb+1 xb1(nbb)=x-dx xb2(nbb)=x if(nbb.eq.nb)goto 1 endif fp=fc enddo 11
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE zbrac(func,x1,x2,succes) INTEGER NTRY REAL x1,x2,func,FACTOR EXTERNAL func PARAMETER (FACTOR=1.6,NTRY=50) Given a function func and an initial guessed range x1 to x2, the routine expands the range geometrically until a root is bracketed by the returned values x1 and x2 (in which case succes returns as .true.) or until the range becomes unacceptably large (in which case succes returns as .false.). INTEGER j REAL f1,f2 LOGICAL succes if(x1.eq.x2)pause ’you have to guess an initial range in zbrac’ f1=func(x1) f2=func(x2) succes=.true. do 11 j=1,NTRY if(f1*f2.lt.0.)return if(abs(f1).lt.abs(f2))then x1=x1+FACTOR*(x1-x2) f1=func(x1) else x2=x2+FACTOR*(x2-x1) f2=func(x2) endif enddo 11 succes=.false. return END
346 1
Chapter 9.
Root Finding and Nonlinear Sets of Equations
continue nb=nbb return END
Once we know that an interval contains a root, several classical procedures are available to refine it. These proceed with varying degrees of speed and sureness towards the answer. Unfortunately, the methods that are guaranteed to converge plod along most slowly, while those that rush to the solution in the best cases can also dash rapidly to infinity without warning if measures are not taken to avoid such behavior. The bisection method is one that cannot fail. It is thus not to be sneered at as a method for otherwise badly behaved problems. The idea is simple. Over some interval the function is known to pass through zero because it changes sign. Evaluate the function at the interval’s midpoint and examine its sign. Use the midpoint to replace whichever limit has the same sign. After each iteration the bounds containing the root decrease by a factor of two. If after n iterations the root is known to be within an interval of size n , then after the next iteration it will be bracketed within an interval of size n+1 = n /2
(9.1.2)
neither more nor less. Thus, we know in advance the number of iterations required to achieve a given tolerance in the solution, n = log2
0
(9.1.3)
where 0 is the size of the initially bracketing interval, is the desired ending tolerance. Bisection must succeed. If the interval happens to contain two or more roots, bisection will find one of them. If the interval contains no roots and merely straddles a singularity, it will converge on the singularity. When a method converges as a factor (less than 1) times the previous uncertainty to the first power (as is the case for bisection), it is said to converge linearly. Methods that converge as a higher power, n+1 = constant × (n )m
m>1
(9.1.4)
are said to converge superlinearly. In other contexts “linear” convergence would be termed “exponential,” or “geometrical.” That is not too bad at all: Linear convergence means that successive significant figures are won linearly with computational effort. It remains to discuss practical criteria for convergence. It is crucial to keep in mind that computers use a fixed number of binary digits to represent floating-point numbers. While your function might analytically pass through zero, it is possible that its computed value is never zero, for any floating-point argument. One must decide what accuracy on the root is attainable: Convergence to within 10−6 in absolute value is reasonable when the root lies near 1, but certainly unachievable if
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Bisection Method
9.2 Secant Method, False Position Method, and Ridders’ Method
347
FUNCTION rtbis(func,x1,x2,xacc) INTEGER JMAX REAL rtbis,x1,x2,xacc,func EXTERNAL func PARAMETER (JMAX=40) Maximum allowed number of bisections. Using bisection, find the root of a function func known to lie between x1 and x2. The root, returned as rtbis, will be refined until its accuracy is ±xacc. INTEGER j REAL dx,f,fmid,xmid fmid=func(x2) f=func(x1) if(f*fmid.ge.0.) pause ’root must be bracketed in rtbis’ if(f.lt.0.)then Orient the search so that f>0 lies at x+dx. rtbis=x1 dx=x2-x1 else rtbis=x2 dx=x1-x2 endif do 11 j=1,JMAX Bisection loop. dx=dx*.5 xmid=rtbis+dx fmid=func(xmid) if(fmid.le.0.)rtbis=xmid if(abs(dx).lt.xacc .or. fmid.eq.0.) return enddo 11 pause ’too many bisections in rtbis’ END
9.2 Secant Method, False Position Method, and Ridders’ Method For functions that are smooth near a root, the methods known respectively as false position (or regula falsi) and secant method generally converge faster than bisection. In both of these methods the function is assumed to be approximately linear in the local region of interest, and the next improvement in the root is taken as the point where the approximating line crosses the axis. After each iteration one of the previous boundary points is discarded in favor of the latest estimate of the root. The only difference between the methods is that secant retains the most recent of the prior estimates (Figure 9.2.1; this requires an arbitrary choice on the first iteration), while false position retains that prior estimate for which the function value
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
the root lies near 1026 . One might thus think to specify convergence by a relative (fractional) criterion, but this becomes unworkable for roots near zero. To be most general, the routines below will require you to specify an absolute tolerance, such that iterations continue until the interval becomes smaller than this tolerance in absolute units. Usually you may wish to take the tolerance to be (|x1| +|x2 |)/2 where is the machine precision and x1 and x2 are the initial brackets. When the root lies near zero you ought to consider carefully what reasonable tolerance means for your function. The following routine quits after 40 bisections in any event, with 2−40 ≈ 10−12.
Root Finding and Nonlinear Sets of Equations Chapter 9.
348
2 f (x)
x 4
1
Figure 9.2.1. Secant method. Extrapolation or interpolation lines (dashed) are drawn through the two most recently evaluated points, whether or not they bracket the function. The points are numbered in the order that they are used.
f (x)
2
3 4
x
1
Figure 9.2.2. False position method. Interpolation lines (dashed) are drawn through the most recent points that bracket the root. In this example, point 1 thus remains “active” for many steps. False position converges less rapidly than the secant method, but it is more certain.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
3
9.2 Secant Method, False Position Method, and Ridders’ Method
349
2
f (x)
1 3 4 Figure 9.2.3. Example where both the secant and false position methods will take many iterations to arrive at the true root. This function would be difficult for many other root-finding methods.
has opposite sign from the function value at the current best estimate of the root, so that the two points continue to bracket the root (Figure 9.2.2). Mathematically, the secant method converges more rapidly near a root of a sufficiently continuous function. Its order of convergence can be shown to be the “golden ratio” 1.618 . . ., so that 1.618
lim |k+1 | ≈ const × |k |
k→∞
(9.2.1)
The secant method has, however, the disadvantage that the root does not necessarily remain bracketed. For functions that are not sufficiently continuous, the algorithm can therefore not be guaranteed to converge: Local behavior might send it off towards infinity. False position, since it sometimes keeps an older rather than newer function evaluation, has a lower order of convergence. Since the newer function value will sometimes be kept, the method is often superlinear, but estimation of its exact order is not so easy. Here are sample implementations of these two related methods. While these methods are standard textbook fare, Ridders’ method, described below, or Brent’s method, in the next section, are almost always better choices. Figure 9.2.3 shows the behavior of secant and false-position methods in a difficult situation. FUNCTION rtflsp(func,x1,x2,xacc) INTEGER MAXIT REAL rtflsp,x1,x2,xacc,func EXTERNAL func PARAMETER (MAXIT=30) Set to the maximum allowed number of iterations.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
x
350
Chapter 9.
Root Finding and Nonlinear Sets of Equations
FUNCTION rtsec(func,x1,x2,xacc) INTEGER MAXIT REAL rtsec,x1,x2,xacc,func EXTERNAL func PARAMETER (MAXIT=30) Maximum allowed number of iterations. Using the secant method, find the root of a function func thought to lie between x1 and x2. The root, returned as rtsec, is refined until its accuracy is ±xacc. INTEGER j REAL dx,f,fl,swap,xl fl=func(x1) f=func(x2) if(abs(fl).lt.abs(f))then Pick the bound with the smaller function value as the most rtsec=x1 recent guess. xl=x2 swap=fl fl=f f=swap else xl=x1 rtsec=x2 endif do 11 j=1,MAXIT Secant loop. dx=(xl-rtsec)*f/(f-fl) Increment with respect to latest value. xl=rtsec fl=f rtsec=rtsec+dx
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Using the false position method, find the root of a function func known to lie between x1 and x2. The root, returned as rtflsp, is refined until its accuracy is ±xacc. INTEGER j REAL del,dx,f,fh,fl,swap,xh,xl fl=func(x1) fh=func(x2) Be sure the interval brackets a root. if(fl*fh.gt.0.) pause ’root must be bracketed in rtflsp’ if(fl.lt.0.)then Identify the limits so that xl corresponds to the low side. xl=x1 xh=x2 else xl=x2 xh=x1 swap=fl fl=fh fh=swap endif dx=xh-xl do 11 j=1,MAXIT False position loop. rtflsp=xl+dx*fl/(fl-fh) Increment with respect to latest value. f=func(rtflsp) if(f.lt.0.) then Replace appropriate limit. del=xl-rtflsp xl=rtflsp fl=f else del=xh-rtflsp xh=rtflsp fh=f endif dx=xh-xl if(abs(del).lt.xacc.or.f.eq.0.)return Convergence. enddo 11 pause ’rtflsp exceed maximum iterations’ END
9.2 Secant Method, False Position Method, and Ridders’ Method
f=func(rtsec) if(abs(dx).lt.xacc.or.f.eq.0.)return enddo 11 pause ’rtsec exceed maximum iterations’ END
351
Convergence.
A powerful variant on false position is due to Ridders [1]. When a root is bracketed between x1 and x2 , Ridders’ method first evaluates the function at the midpoint x3 = (x1 + x2 )/2. It then factors out that unique exponential function which turns the residual function into a straight line. Specifically, it solves for a factor eQ that gives f(x1 ) − 2f(x3 )eQ + f(x2 )e2Q = 0
(9.2.2)
This is a quadratic equation in eQ , which can be solved to give p f(x3 ) + sign[f(x2 )] f(x3 )2 − f(x1 )f(x2 ) e = f(x2 ) Q
(9.2.3)
Now the false position method is applied, not to the values f(x1 ), f(x3 ), f(x2 ), but to the values f(x1 ), f(x3 )eQ , f(x2 )e2Q , yielding a new guess for the root, x4 . The overall updating formula (incorporating the solution 9.2.3) is sign[f(x1 ) − f(x2 )]f(x3 ) x4 = x3 + (x3 − x1 ) p f(x3 )2 − f(x1 )f(x2 )
(9.2.4)
Equation (9.2.4) has some very nice properties. First, x4 is guaranteed to lie in the interval (x1 , x2 ), so the method never jumps out of its brackets. Second, the convergence of successive applications of equation (9.2.4) is quadratic, that is, m = 2 in equation (9.1.4). Since each application √ of (9.2.4) requires two function evaluations, the actual order of the method is 2, not 2; but this is still quite respectably superlinear: the number of significant digits in the answer approximately doubles with each two function evaluations. Third, taking out the function’s “bend” via exponential (that is, ratio) factors, rather than via a polynomial technique (e.g., fitting a parabola), turns out to give an extraordinarily robust algorithm. In both reliability and speed, Ridders’ method is generally competitive with the more highly developed and better established (but more complicated) method of Van Wijngaarden, Dekker, and Brent, which we next discuss.
C
FUNCTION zriddr(func,x1,x2,xacc) INTEGER MAXIT REAL zriddr,x1,x2,xacc,func,UNUSED PARAMETER (MAXIT=60,UNUSED=-1.11E30) EXTERNAL func USES func Using Ridders’ method, return the root of a function func known to lie between x1 and x2. The root, returned as zriddr, will be refined to an approximate accuracy xacc. INTEGER j REAL fh,fl,fm,fnew,s,xh,xl,xm,xnew
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Ridders’ Method
352
Chapter 9.
Root Finding and Nonlinear Sets of Equations
CITED REFERENCES AND FURTHER READING: Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §8.3. Ostrowski, A.M. 1966, Solutions of Equations and Systems of Equations, 2nd ed. (New York: Academic Press), Chapter 12. Ridders, C.J.F. 1979, IEEE Transactions on Circuits and Systems, vol. CAS-26, pp. 979–980. [1]
9.3 Van Wijngaarden–Dekker–Brent Method While secant and false position formally converge faster than bisection, one finds in practice pathological functions for which bisection converges more rapidly.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
fl=func(x1) fh=func(x2) if((fl.gt.0..and.fh.lt.0.).or.(fl.lt.0..and.fh.gt.0.))then xl=x1 xh=x2 zriddr=UNUSED Any highly unlikely value, to simplify logic do 11 j=1,MAXIT below. xm=0.5*(xl+xh) fm=func(xm) First of two function evaluations per its=sqrt(fm**2-fl*fh) eration. if(s.eq.0.)return xnew=xm+(xm-xl)*(sign(1.,fl-fh)*fm/s) Updating formula. if (abs(xnew-zriddr).le.xacc) return zriddr=xnew fnew=func(zriddr) Second of two function evaluations per if (fnew.eq.0.) return iteration. if(sign(fm,fnew).ne.fm) then Bookkeeping to keep the root bracketed xl=xm on next iteration. fl=fm xh=zriddr fh=fnew else if(sign(fl,fnew).ne.fl) then xh=zriddr fh=fnew else if(sign(fh,fnew).ne.fh) then xl=zriddr fl=fnew else pause ’never get here in zriddr’ endif if(abs(xh-xl).le.xacc) return enddo 11 pause ’zriddr exceed maximum iterations’ else if (fl.eq.0.) then zriddr=x1 else if (fh.eq.0.) then zriddr=x2 else pause ’root must be bracketed in zriddr’ endif return END
9.3 Van Wijngaarden–Dekker–Brent Method
353
x=
[y − f(a)][y − f(b)]c [y − f(b)][y − f(c)]a + [f(c) − f(a)][f(c) − f(b)] [f(a) − f(b)][f(a) − f(c)] [y − f(c)][y − f(a)]b + [f(b) − f(c)][f(b) − f(a)]
(9.3.1)
Setting y to zero gives a result for the next root estimate, which can be written as x = b + P/Q
(9.3.2)
where, in terms of R ≡ f(b)/f(c),
S ≡ f(b)/f(a),
T ≡ f(a)/f(c)
(9.3.3)
we have P = S [T (R − T )(c − b) − (1 − R)(b − a)] Q = (T − 1)(R − 1)(S − 1)
(9.3.4) (9.3.5)
In practice b is the current best estimate of the root and P/Q ought to be a “small” correction. Quadratic methods work well only when the function behaves smoothly;
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
These can be choppy, discontinuous functions, or even smooth functions if the second derivative changes sharply near the root. Bisection always halves the interval, while secant and false position can sometimes spend many cycles slowly pulling distant bounds closer to a root. Ridders’ method does a much better job, but it too can sometimes be fooled. Is there a way to combine superlinear convergence with the sureness of bisection? Yes. We can keep track of whether a supposedly superlinear method is actually converging the way it is supposed to, and, if it is not, we can intersperse bisection steps so as to guarantee at least linear convergence. This kind of super-strategy requires attention to bookkeeping detail, and also careful consideration of how roundoff errors can affect the guiding strategy. Also, we must be able to determine reliably when convergence has been achieved. An excellent algorithm that pays close attention to these matters was developed in the 1960s by van Wijngaarden, Dekker, and others at the Mathematical Center in Amsterdam, and later improved by Brent [1]. For brevity, we refer to the final form of the algorithm as Brent’s method. The method is guaranteed (by Brent) to converge, so long as the function can be evaluated within the initial interval known to contain a root. Brent’s method combines root bracketing, bisection, and inverse quadratic interpolation to converge from the neighborhood of a zero crossing. While the false position and secant methods assume approximately linear behavior between two prior root estimates, inverse quadratic interpolation uses three prior points to fit an inverse quadratic function (x as a quadratic function of y) whose value at y = 0 is taken as the next estimate of the root x. Of course one must have contingency plans for what to do if the root falls outside of the brackets. Brent’s method takes care of all that. If the three point pairs are [a, f(a)], [b, f(b)], [c, f(c)] then the interpolation formula (cf. equation 3.1.1) is
354
Chapter 9.
Root Finding and Nonlinear Sets of Equations
*
*
FUNCTION zbrent(func,x1,x2,tol) INTEGER ITMAX REAL zbrent,tol,x1,x2,func,EPS EXTERNAL func PARAMETER (ITMAX=100,EPS=3.e-8) Using Brent’s method, find the root of a function func known to lie between x1 and x2. The root, returned as zbrent, will be refined until its accuracy is tol. Parameters: Maximum allowed number of iterations, and machine floating-point precision. INTEGER iter REAL a,b,c,d,e,fa,fb,fc,p,q,r, s,tol1,xm a=x1 b=x2 fa=func(a) fb=func(b) if((fa.gt.0..and.fb.gt.0.).or.(fa.lt.0..and.fb.lt.0.)) pause ’root must be bracketed for zbrent’ c=b fc=fb do 11 iter=1,ITMAX if((fb.gt.0..and.fc.gt.0.).or.(fb.lt.0..and.fc.lt.0.))then c=a Rename a, b, c and adjust bounding interval d. fc=fa d=b-a e=d endif if(abs(fc).lt.abs(fb)) then a=b b=c c=a fa=fb fb=fc fc=fa endif tol1=2.*EPS*abs(b)+0.5*tol Convergence check. xm=.5*(c-b) if(abs(xm).le.tol1 .or. fb.eq.0.)then zbrent=b return endif if(abs(e).ge.tol1 .and. abs(fa).gt.abs(fb)) then s=fb/fa Attempt inverse quadratic interpolation. if(a.eq.c) then p=2.*xm*s q=1.-s else q=fa/fc r=fb/fc p=s*(2.*xm*q*(q-r)-(b-a)*(r-1.)) q=(q-1.)*(r-1.)*(s-1.)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
they run the serious risk of giving very bad estimates of the next root or causing machine failure by an inappropriate division by a very small number (Q ≈ 0). Brent’s method guards against this problem by maintaining brackets on the root and checking where the interpolation would land before carrying out the division. When the correction P/Q would not land within the bounds, or when the bounds are not collapsing rapidly enough, the algorithm takes a bisection step. Thus, Brent’s method combines the sureness of bisection with the speed of a higher-order method when appropriate. We recommend it as the method of choice for general one-dimensional root finding where a function’s values only (and not its derivative or functional form) are available.
9.4 Newton-Raphson Method Using Derivative
355
CITED REFERENCES AND FURTHER READING: Brent, R.P. 1973, Algorithms for Minimization without Derivatives (Englewood Cliffs, NJ: PrenticeHall), Chapters 3, 4. [1] Forsythe, G.E., Malcolm, M.A., and Moler, C.B. 1977, Computer Methods for Mathematical Computations (Englewood Cliffs, NJ: Prentice-Hall), §7.2.
9.4 Newton-Raphson Method Using Derivative Perhaps the most celebrated of all one-dimensional root-finding routines is Newton’s method, also called the Newton-Raphson method. This method is distinguished from the methods of previous sections by the fact that it requires the evaluation of both the function f(x), and the derivative f 0 (x), at arbitrary points x. The Newton-Raphson formula consists geometrically of extending the tangent line at a current point xi until it crosses zero, then setting the next guess xi+1 to the abscissa of that zero-crossing (see Figure 9.4.1). Algebraically, the method derives from the familiar Taylor series expansion of a function in the neighborhood of a point, f(x + δ) ≈ f(x) + f 0 (x)δ +
f 00 (x) 2 δ + .... 2
(9.4.1)
For small enough values of δ, and for well-behaved functions, the terms beyond linear are unimportant, hence f(x + δ) = 0 implies δ=−
f(x) . f 0 (x)
(9.4.2)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
endif if(p.gt.0.) q=-q Check whether in bounds. p=abs(p) if(2.*p .lt. min(3.*xm*q-abs(tol1*q),abs(e*q))) then e=d Accept interpolation. d=p/q else d=xm Interpolation failed, use bisection. e=d endif else Bounds decreasing too slowly, use bisection. d=xm e=d endif a=b Move last best guess to a. fa=fb if(abs(d) .gt. tol1) then Evaluate new trial root. b=b+d else b=b+sign(tol1,xm) endif fb=func(b) enddo 11 pause ’zbrent exceeding maximum iterations’ zbrent=b return END
356
Chapter 9.
Root Finding and Nonlinear Sets of Equations
f 00 (x) + · · ·, 2 f 0 (x + ) = f 0 (x) + f 00 (x) + · · · f(x + ) = f(x) + f 0 (x) + 2
(9.4.3)
By the Newton-Raphson formula, xi+1 = xi −
f(xi ) , f 0 (xi )
(9.4.4)
i+1 = i −
f(xi ) . f 0 (xi )
(9.4.5)
so that
When a trial solution xi differs from the true root by i , we can use (9.4.3) to express f(xi ), f 0 (xi ) in (9.4.4) in terms of i and derivatives at the root itself. The result is a recurrence relation for the deviations of the trial solutions i+1 = −2i
f 00 (x) . 2f 0 (x)
(9.4.6)
Equation (9.4.6) says that Newton-Raphson converges quadratically (cf. equation 9.2.3). Near a root, the number of significant digits approximately doubles with each step. This very strong convergence property makes Newton-Raphson the method of choice for any function whose derivative can be evaluated efficiently, and whose derivative is continuous and nonzero in the neighborhood of a root. Even where Newton-Raphson is rejected for the early stages of convergence (because of its poor global convergence properties), it is very common to “polish up” a root with one or two steps of Newton-Raphson, which can multiply by two or four its number of significant figures! For an efficient realization of Newton-Raphson the user provides a routine that evaluates both f(x) and its first derivative f 0 (x) at the point x. The Newton-Raphson formula can also be applied using a numerical difference to approximate the true local derivative, f 0 (x) ≈
f(x + dx) − f(x) . dx
(9.4.7)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Newton-Raphson is not restricted to one dimension. The method readily generalizes to multiple dimensions, as we shall see in §9.6 and §9.7, below. Far from a root, where the higher-order terms in the series are important, the Newton-Raphson formula can give grossly inaccurate, meaningless corrections. For instance, the initial guess for the root might be so far from the true root as to let the search interval include a local maximum or minimum of the function. This can be death to the method (see Figure 9.4.2). If an iteration places a trial guess near such a local extremum, so that the first derivative nearly vanishes, then NewtonRaphson sends its solution off to limbo, with vanishingly small hope of recovery. Like most powerful tools, Newton-Raphson can be destructive used in inappropriate circumstances. Figure 9.4.3 demonstrates another possible pathology. Why do we call Newton-Raphson powerful? The answer lies in its rate of convergence: Within a small distance of x the function and its derivative are approximately:
2 3
x
Figure 9.4.1. Newton’s method extrapolates the local derivative to find the next estimate of the root. In this example it works well and converges quadratically.
f (x)
1
x
Figure 9.4.2. Unfortunate case where Newton’s method encounters a local extremum and shoots off to outer space. Here bracketing bounds, as in rtsafe, would save the day.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2 3
357 9.4 Newton-Raphson Method Using Derivative
f (x)
1
358
Chapter 9.
Root Finding and Nonlinear Sets of Equations
f (x)
1
2
Figure 9.4.3. Unfortunate case where Newton’s method enters a nonconvergent cycle. This behavior is often encountered when the function f is obtained, in whole or in part, by table interpolation. With a better initial guess, the method would have succeeded.
This is not, however, a recommended procedure for the following reasons: (i) You are doing two function evaluations per step, so at best the superlinear order of √ convergence will be only 2. (ii) If you take dx too small you will be wiped out by roundoff, while if you take it too large your order of convergence will be only linear, no better than using the initial evaluation f 0 (x0 ) for all subsequent steps. Therefore, Newton-Raphson with numerical derivatives is (in one dimension) always dominated by the secant method of §9.2. (In multidimensions, where there is a paucity of available methods, Newton-Raphson with numerical derivatives must be taken more seriously. See §§9.6–9.7.) The following subroutine calls a user supplied subroutine funcd(x,fn,df) which returns the function value as fn and the derivative as df. We have included input bounds on the root simply to be consistent with previous root-finding routines: Newton does not adjust bounds, and works only on local information at the point x. The bounds are used only to pick the midpoint as the first guess, and to reject the solution if it wanders outside of the bounds. FUNCTION rtnewt(funcd,x1,x2,xacc) INTEGER JMAX REAL rtnewt,x1,x2,xacc EXTERNAL funcd PARAMETER (JMAX=20) Set to maximum number of iterations. Using the Newton-Raphson method, find the root of a function known to lie in the interval [x1, x2]. The root rtnewt will be refined until its accuracy is known within ±xacc. funcd is a user-supplied subroutine that returns both the function value and the first derivative of the function at the point x. INTEGER j REAL df,dx,f
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
x
9.4 Newton-Raphson Method Using Derivative
*
359
While Newton-Raphson’s global convergence properties are poor, it is fairly easy to design a fail-safe routine that utilizes a combination of bisection and NewtonRaphson. The hybrid algorithm takes a bisection step whenever Newton-Raphson would take the solution out of bounds, or whenever Newton-Raphson is not reducing the size of the brackets rapidly enough.
*
*
FUNCTION rtsafe(funcd,x1,x2,xacc) INTEGER MAXIT REAL rtsafe,x1,x2,xacc EXTERNAL funcd PARAMETER (MAXIT=100) Maximum allowed number of iterations. Using a combination of Newton-Raphson and bisection, find the root of a function bracketed between x1 and x2. The root, returned as the function value rtsafe, will be refined until its accuracy is known within ±xacc. funcd is a user-supplied subroutine which returns both the function value and the first derivative of the function. INTEGER j REAL df,dx,dxold,f,fh,fl,temp,xh,xl call funcd(x1,fl,df) call funcd(x2,fh,df) if((fl.gt.0..and.fh.gt.0.).or.(fl.lt.0..and.fh.lt.0.)) pause ’root must be bracketed in rtsafe’ if(fl.eq.0.)then rtsafe=x1 return else if(fh.eq.0.)then rtsafe=x2 return else if(fl.lt.0.)then Orient the search so that f (xl) < 0. xl=x1 xh=x2 else xh=x1 xl=x2 endif rtsafe=.5*(x1+x2) Initialize the guess for root, dxold=abs(x2-x1) the “stepsize before last,” dx=dxold and the last step. call funcd(rtsafe,f,df) do 11 j=1,MAXIT Loop over allowed iterations. if(((rtsafe-xh)*df-f)*((rtsafe-xl)*df-f).gt.0. Bisect if Newton out of range, .or. abs(2.*f).gt.abs(dxold*df) ) then or not decreasing fast enough. dxold=dx dx=0.5*(xh-xl) rtsafe=xl+dx if(xl.eq.rtsafe)return Change in root is negligible. else Newton step acceptable. Take it. dxold=dx dx=f/df temp=rtsafe
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
rtnewt=.5*(x1+x2) Initial guess. do 11 j=1,JMAX call funcd(rtnewt,f,df) dx=f/df rtnewt=rtnewt-dx if((x1-rtnewt)*(rtnewt-x2).lt.0.) pause ’rtnewt jumped out of brackets’ if(abs(dx).lt.xacc) return Convergence. enddo 11 pause ’rtnewt exceeded maximum iterations’ END
360
Chapter 9.
Root Finding and Nonlinear Sets of Equations
For many functions the derivative f 0 (x) often converges to machine accuracy before the function f(x) itself does. When that is the case one need not subsequently update f 0 (x). This shortcut is recommended only when you confidently understand the generic behavior of your function, but it speeds computations when the derivative calculation is laborious. (Formally this makes the convergence only linear, but if the derivative isn’t changing anyway, you can do no better.)
Newton-Raphson and Fractals An interesting sidelight to our repeated warnings about Newton-Raphson’s unpredictable global convergence properties — its very rapid local convergence notwithstanding — is to investigate, for some particular equation, the set of starting values from which the method does, or doesn’t converge to a root. Consider the simple equation z3 − 1 = 0
(9.4.8)
whose single real root is z = 1, but which also has complex roots at the other two cube roots of unity, exp(±2πi/3). Newton’s method gives the iteration zj+1 = zj −
zj3 − 1 3zj2
(9.4.9)
Up to now, we have applied an iteration like equation (9.4.9) only for real starting values z0 , but in fact all of the equations in this section also apply in the complex plane. We can therefore map out the complex plane into regions from which a starting value z0 , iterated in equation (9.4.9), will, or won’t, converge to z = 1. Naively, we might expect to find a “basin of convergence” somehow surrounding the root z = 1. We surely do not expect the basin of convergence to fill the whole plane, because the plane must also contain regions that converge to each of the two complex roots. In fact, by symmetry, the three regions must have identical shapes. Perhaps they will be three symmetric 120◦ wedges, with one root centered in each? Now take a look at Figure 9.4.4, which shows the result of a numerical exploration. The basin of convergence does indeed cover 1/3 the area of the complex plane, but its boundary is highly irregular — in fact, fractal. (A fractal, so called, has self-similar structure that repeats on all scales of magnification.) How
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
rtsafe=rtsafe-dx if(temp.eq.rtsafe)return endif if(abs(dx).lt.xacc) return Convergence criterion. call funcd(rtsafe,f,df) The one new function evaluation per iteration. if(f.lt.0.) then Maintain the bracket on the root. xl=rtsafe else xh=rtsafe endif enddo 11 pause ’rtsafe exceeding maximum iterations’ return END
9.4 Newton-Raphson Method Using Derivative
361
does this fractal emerge from something as simple as Newton’s method, and an equation as simple as (9.4.8)? The answer is already implicit in Figure 9.4.2, which showed how, on the real line, a local extremum causes Newton’s method to shoot off to infinity. Suppose one is slightly removed from such a point. Then one might be shot off not to infinity, but — by luck — right into the basin of convergence of the desired root. But that means that in the neighborhood of an extremum there must be a tiny, perhaps distorted, copy of the basin of convergence — a kind of “one-bounce away” copy. Similar logic shows that there can be “two-bounce” copies, “three-bounce” copies, and so on. A fractal thus emerges. Notice that, for equation (9.4.8), almost the whole real axis is in the domain of convergence for the root z = 1. We say “almost” because of the peculiar discrete points on the negative real axis whose convergence is indeterminate (see figure). What happens if you start Newton’s method from one of these points? (Try it.)
CITED REFERENCES AND FURTHER READING: Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathematical Association of America), Chapter 2. Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §8.4. Ortega, J., and Rheinboldt, W. 1970, Iterative Solution of Nonlinear Equations in Several Variables (New York: Academic Press). Mandelbrot, B.B. 1983, The Fractal Geometry of Nature (San Francisco: W.H. Freeman).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Figure 9.4.4. The complex z plane with real and imaginary components in the range (−2, 2). The black region is the set of points from which Newton’s method converges to the root z = 1 of the equation z3 − 1 = 0. Its shape is fractal.
362
Chapter 9.
Root Finding and Nonlinear Sets of Equations
Peitgen, H.-O., and Saupe, D. (eds.) 1988, The Science of Fractal Images (New York: SpringerVerlag).
Here we present a few methods for finding roots of polynomials. These will serve for most practical problems involving polynomials of low-to-moderate degree or for well-conditioned polynomials of higher degree. Not as well appreciated as it ought to be is the fact that some polynomials are exceedingly ill-conditioned. The tiniest changes in a polynomial’s coefficients can, in the worst case, send its roots sprawling all over the complex plane. (An infamous example due to Wilkinson is detailed by Acton [1].) Recall that a polynomial of degree n will have n roots. The roots can be real or complex, and they might not be distinct. If the coefficients of the polynomial are real, then complex roots will occur in pairs that are conjugate, i.e., if x1 = a + bi is a root then x2 = a − bi will also be a root. When the coefficients are complex, the complex roots need not be related. Multiple roots, or closely spaced roots, produce the most difficulty for numerical algorithms (see Figure 9.5.1). For example, P (x) = (x − a)2 has a double real root at x = a. However, we cannot bracket the root by the usual technique of identifying neighborhoods where the function changes sign, nor will slope-following methods such as Newton-Raphson work well, because both the function and its derivative vanish at a multiple root. Newton-Raphson may work, but slowly, since large roundoff errors can occur. When a root is known in advance to be multiple, then special methods of attack are readily devised. Problems arise when (as is generally the case) we do not know in advance what pathology a root will display.
Deflation of Polynomials When seeking several or all roots of a polynomial, the total effort can be significantly reduced by the use of deflation. As each root r is found, the polynomial is factored into a product involving the root and a reduced polynomial of degree one less than the original, i.e., P (x) = (x − r)Q(x). Since the roots of Q are exactly the remaining roots of P , the effort of finding additional roots decreases, because we work with polynomials of lower and lower degree as we find successive roots. Even more important, with deflation we can avoid the blunder of having our iterative method converge twice to the same (nonmultiple) root instead of separately to two different roots. Deflation, which amounts to synthetic division, is a simple operation that acts on the array of polynomial coefficients. The concise code for synthetic division by a monomial factor was given in §5.3 above. You can deflate complex roots either by converting that code to complex data type, or else — in the case of a polynomial with real coefficients but possibly complex roots — by deflating by a quadratic factor, [x − (a + ib)] [x − (a − ib)] = x2 − 2ax + (a2 + b2 )
(9.5.1)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
9.5 Roots of Polynomials
9.5 Roots of Polynomials
f (x)
f (x)
x
( b)
Figure 9.5.1. (a) Linear, quadratic, and cubic behavior at the roots of polynomials. Only under high magnification (b) does it become apparent that the cubic has one, not three, roots, and that the quadratic has two roots rather than none.
The routine poldiv in §5.3 can be used to divide the polynomial by this factor. Deflation must, however, be utilized with care. Because each new root is known with only finite accuracy, errors creep into the determination of the coefficients of the successively deflated polynomial. Consequently, the roots can become more and more inaccurate. It matters a lot whether the inaccuracy creeps in stably (plus or minus a few multiples of the machine precision at each stage) or unstably (erosion of successive significant figures until the results become meaningless). Which behavior occurs depends on just how the root is divided out. Forward deflation, where the new polynomial coefficients are computed in the order from the highest power of x down to the constant term, was illustrated in §5.3. This turns out to be stable if the root of smallest absolute value is divided out at each stage. Alternatively, one can do backward deflation, where new coefficients are computed in order from the constant term up to the coefficient of the highest power of x. This is stable if the remaining root of largest absolute value is divided out at each stage. A polynomial whose coefficients are interchanged “end-to-end,” so that the constant becomes the highest coefficient, etc., has its roots mapped into their reciprocals. (Proof: Divide the whole polynomial by its highest power xn and rewrite it as a polynomial in 1/x.) The algorithm for backward deflation is therefore virtually identical to that of forward deflation, except that the original coefficients are taken in reverse order and the reciprocal of the deflating root is used. Since we will use forward deflation below, we leave to you the exercise of writing a concise coding for backward deflation (as in §5.3). For more on the stability of deflation, consult [2]. To minimize the impact of increasing errors (even stable ones) when using deflation, it is advisable to treat roots of the successively deflated polynomials as only tentative roots of the original polynomial. One then polishes these tentative roots by taking them as initial guesses that are to be re-solved for, using the nondeflated original polynomial P . Again you must beware lest two deflated roots are inaccurate enough that, under polishing, they both converge to the same undeflated root; in that case you gain a spurious root-multiplicity and lose a distinct root. This is detectable,
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
x
(a)
363
364
Chapter 9.
Root Finding and Nonlinear Sets of Equations
Muller’s Method Muller’s method generalizes the secant method, but uses quadratic interpolation among three points instead of linear interpolation between two. Solving for the zeros of the quadratic allows the method to find complex pairs of roots. Given three previous guesses for the root xi−2 , xi−1 , xi , and the values of the polynomial P (x) at those points, the next approximation xi+1 is produced by the following formulas, xi − xi−1 xi−1 − xi−2 A ≡ qP (xi ) − q(1 + q)P (xi−1 ) + q 2 P (xi−2 ) q≡
(9.5.2)
B ≡ (2q + 1)P (xi ) − (1 + q)2 P (xi−1 ) + q 2 P (xi−2 ) C ≡ (1 + q)P (xi ) followed by
xi+1
2C √ = xi − (xi − xi−1 ) B ± B 2 − 4AC
(9.5.3)
where the sign in the denominator is chosen to make its absolute value or modulus as large as possible. You can start the iterations with any three values of x that you like, e.g., three equally spaced values on the real axis. Note that you must allow for the possibility of a complex denominator, and subsequent complex arithmetic, in implementing the method. Muller’s method is sometimes also used for finding complex zeros of analytic functions (not just polynomials) in the complex plane, for example in the IMSL routine ZANLY [3].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
since you can compare each polished root for equality to previous ones from distinct tentative roots. When it happens, you are advised to deflate the polynomial just once (and for this root only), then again polish the tentative root, or to use Maehly’s procedure (see equation 9.5.29 below). Below we say more about techniques for polishing real and complex-conjugate tentative roots. First, let’s get back to overall strategy. There are two schools of thought about how to proceed when faced with a polynomial of real coefficients. One school says to go after the easiest quarry, the real, distinct roots, by the same kinds of methods that we have discussed in previous sections for general functions, i.e., trial-and-error bracketing followed by a safe Newton-Raphson as in rtsafe. Sometimes you are only interested in real roots, in which case the strategy is complete. Otherwise, you then go after quadratic factors of the form (9.5.1) by any of a variety of methods. One such is Bairstow’s method, which we will discuss below in the context of root polishing. Another is Muller’s method, which we here briefly discuss.
9.5 Roots of Polynomials
365
Laguerre’s Method
Pn (x) = (x − x1 )(x − x2 ) . . . (x − xn ) ln |Pn (x)| = ln |x − x1 | + ln |x − x2 | + . . . + ln |x − xn |
(9.5.4) (9.5.5)
1 1 1 P0 d ln |Pn (x)| =+ + +...+ = n ≡ G (9.5.6) dx x − x1 x − x2 x − xn Pn 2 d ln |Pn (x)| 1 1 1 − =+ + +...+ dx2 (x − x1 )2 (x − x2 )2 (x − xn )2 0 2 P 00 Pn = − n ≡H (9.5.7) Pn Pn Starting from these relations, the Laguerre formulas make what Acton [1] nicely calls “a rather drastic set of assumptions”: The root x1 that we seek is assumed to be located some distance a from our current guess x, while all other roots are assumed to be located at a distance b x − x1 = a ;
x − xi = b i = 2, 3, . . . , n
(9.5.8)
Then we can express (9.5.6), (9.5.7) as 1 n−1 + =G a b 1 n−1 + 2 =H a2 b
(9.5.9) (9.5.10)
which yields as the solution for a a=
G±
p
n (n − 1)(nH − G2 )
(9.5.11)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The second school regarding overall strategy happens to be the one to which we belong. That school advises you to use one of a very small number of methods that will converge (though with greater or lesser efficiency) to all types of roots: real, complex, single, or multiple. Use such a method to get tentative values for all n roots of your nth degree polynomial. Then go back and polish them as you desire. Laguerre’s method is by far the most straightforward of these general, complex methods. It does require complex arithmetic, even while converging to real roots; however, for polynomials with all real roots, it is guaranteed to converge to a root from any starting point. For polynomials with some complex roots, little is theoretically proved about the method’s convergence. Much empirical experience, however, suggests that nonconvergence is extremely unusual, and, further, can almost always be fixed by a simple scheme to break a nonconverging limit cycle. (This is implemented in our routine, below.) An example of a polynomial that requires this cycle-breaking scheme is one of high degree (> ∼ 20), with all its roots just outside of the complex unit circle, approximately equally spaced around it. When the method converges on a simple complex zero, it is known that its convergence is third order. In some instances the complex arithmetic in the Laguerre method is no disadvantage, since the polynomial itself may have complex coefficients. To motivate (although not rigorously derive) the Laguerre formulas we can note the following relations between the polynomial and its roots and derivatives
366
Chapter 9.
Root Finding and Nonlinear Sets of Equations
SUBROUTINE laguer(a,m,x,its) INTEGER m,its,MAXIT,MR,MT REAL EPSS COMPLEX a(m+1),x PARAMETER (EPSS=2.e-7,MR=8,MT=10,MAXIT=MT*MR) P i−1 , Given the degree m and the complex coefficients a(1:m+1) of the polynomial m+1 i=1 a(i)x and given a complex value x, this routine improves x by Laguerre’s method until it converges, within the achievable roundoff limit, to a root of the given polynomial. The number of iterations taken is returned as its. Parameters: EPSS is the estimated fractional roundoff error. We try to break (rare) limit cycles with MR different fractional values, once every MT steps, for MAXIT total allowed iterations. INTEGER iter,j REAL abx,abp,abm,err,frac(MR) COMPLEX dx,x1,b,d,f,g,h,sq,gp,gm,g2 SAVE frac DATA frac /.5,.25,.75,.13,.38,.62,.88,1./ Fractions used to break a limit cycle. do 12 iter=1,MAXIT Loop over iterations up to allowed maximum. its=iter b=a(m+1) err=abs(b) d=cmplx(0.,0.) f=cmplx(0.,0.) abx=abs(x) do 11 j=m,1,-1 Efficient computation of the polynomial and its first f=x*f+d two derivatives. d=x*d+b b=x*b+a(j) err=abs(b)+abx*err enddo 11 err=EPSS*err Estimate of roundoff error in evaluating polynomial. if(abs(b).le.err) then We are on the root. return else The generic case: use Laguerre’s formula. g=d/b g2=g*g h=g2-2.*f/b sq=sqrt((m-1)*(m*h-g2)) gp=g+sq gm=g-sq abp=abs(gp) abm=abs(gm) if(abp.lt.abm) gp=gm if (max(abp,abm).gt.0.) then dx=m/gp
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where the sign should be taken to yield the largest magnitude for the denominator. Since the factor inside the square root can be negative, a can be complex. (A more rigorous justification of equation 9.5.11 is in [4].) The method operates iteratively: For a trial value x, a is calculated by equation (9.5.11). Then x − a becomes the next trial value. This continues until a is sufficiently small. The following routine implements the Laguerre method to find one root of a given polynomial of degree m, whose coefficients can be complex. As usual, the first coefficient a(1) is the constant term, while a(m+1) is the coefficient of the highest power of x. The routine implements a simplified version of an elegant stopping criterion due to Adams [5], which neatly balances the desire to achieve full machine accuracy, on the one hand, with the danger of iterating forever in the presence of roundoff error, on the other.
9.5 Roots of Polynomials
367
Here is a driver routine that calls laguer in succession for each root, performs the deflation, optionally polishes the roots by the same Laguerre method — if you are not going to polish in some other way — and finally sorts the roots by their real parts. (We will use this routine in Chapter 13.)
C
SUBROUTINE zroots(a,m,roots,polish) INTEGER m,MAXM REAL EPS COMPLEX a(m+1),roots(m) LOGICAL polish PARAMETER (EPS=1.e-6,MAXM=101) A small number and maximum anticipated value of m+1. USES laguer P i−1 , Given the degree m and the complex coefficients a(1:m+1) of the polynomial m+1 i=1 a(i)x this routine successively calls laguer and finds all m complex roots. The logical variable polish should be input as .true. if polishing (also by Laguerre’s method) is desired, .false. if the roots will be subsequently polished by other means. INTEGER i,j,jj,its COMPLEX ad(MAXM),x,b,c do 11 j=1,m+1 Copy of coefficients for successive deflation. ad(j)=a(j) enddo 11 do 13 j=m,1,-1 Loop over each root to be found. x=cmplx(0.,0.) Start at zero to favor convergence to smallest remaining root. call laguer(ad,j,x,its) Find the root. if(abs(aimag(x)).le.2.*EPS**2*abs(real(x))) x=cmplx(real(x),0.) roots(j)=x b=ad(j+1) Forward deflation. do 12 jj=j,1,-1 c=ad(jj) ad(jj)=b b=x*b+c enddo 12 enddo 13 if (polish) then do 14 j=1,m Polish the roots using the undeflated coefficients. call laguer(a,m,roots(j),its) enddo 14 endif do 16 j=2,m Sort roots by their real parts by straight insertion. x=roots(j) do 15 i=j-1,1,-1 if(real(roots(i)).le.real(x))goto 10 roots(i+1)=roots(i) enddo 15 i=0
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
else dx=exp(cmplx(log(1.+abx),float(iter))) endif endif x1=x-dx if(x.eq.x1)return Converged. if (mod(iter,MT).ne.0) then x=x1 else Every so often we take a fractional step, to break any x=x-dx*frac(iter/MT) limit cycle (itself a rare occurrence). endif enddo 12 pause ’too many iterations in laguer’ Very unusual — can occur only for complex roots. return Try a different starting guess for the root. END
368 10
Chapter 9.
Root Finding and Nonlinear Sets of Equations
roots(i+1)=x enddo 16 return END
The eigenvalues of a matrix A are the roots of the “characteristic polynomial” P (x) = det[A − xI]. However, as we will see in Chapter 11, root-finding is not generally an efficient way to find eigenvalues. Turning matters around, we can use the more efficient eigenvalue methods that are discussed in Chapter 11 to find the roots of arbitrary polynomials. You can easily verify (see, e.g., [6]) that the characteristic polynomial of the special m × m companion matrix A=
− aam
a − am−1
1 0 .. .
0 1
· · · − a a2 m+1 ··· 0 ··· 0
0
0
···
m+1
m+1
1
− a a1
m+1
0 0 .. .
(9.5.12)
0
is equivalent to the general polynomial P (x) =
m+1 X
ai xi−1
(9.5.13)
i=1
If the coefficients ai are real, rather than complex, then the eigenvalues of A can be found using the routines balanc and hqr in §§11.5–11.6 (see discussion there). This method, implemented in the routine zrhqr following, is typically about a factor 2 slower than zroots (above). However, for some classes of polynomials, it is a more robust technique, largely because of the fairly sophisticated convergence methods embodied in hqr. If your polynomial has real coefficients, and you are having trouble with zroots, then zrhqr is a recommended alternative.
C
SUBROUTINE zrhqr(a,m,rtr,rti) INTEGER m,MAXM REAL a(m+1),rtr(m),rti(m) PARAMETER (MAXM=50) USES balanc,hqr P i−1 , given the degree Find all the roots of a polynomial with real coefficients, m+1 i=1 a(i)x m and the coefficients a(1:m+1). The method is to construct an upper Hessenberg matrix whose eigenvalues are the desired roots, and then use the routines balanc and hqr. The real and imaginary parts of the roots are returned in rtr(1:m) and rti(1:m), respectively. INTEGER j,k REAL hess(MAXM,MAXM),xr,xi if (m.gt.MAXM.or.a(m+1).eq.0.) pause ’bad args in zrhqr’ do 12 k=1,m Construct the matrix. hess(1,k)=-a(m+1-k)/a(m+1) do 11 j=2,m hess(j,k)=0. enddo 11 if (k.ne.m) hess(k+1,k)=1.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Eigenvalue Methods
9.5 Roots of Polynomials
enddo 12 call balanc(hess,m,MAXM) Find its eigenvalues. call hqr(hess,m,MAXM,rtr,rti) do 14 j=2,m Sort roots by their real parts by straight insertion. xr=rtr(j) xi=rti(j) do 13 k=j-1,1,-1 if(rtr(k).le.xr)goto 1 rtr(k+1)=rtr(k) rti(k+1)=rti(k) enddo 13 k=0 rtr(k+1)=xr rti(k+1)=xi enddo 14 return END
Other Sure-Fire Techniques The Jenkins-Traub method has become practically a standard in black-box polynomial root-finders, e.g., in the IMSL library [3]. The method is too complicated to discuss here, but is detailed, with references to the primary literature, in [4]. The Lehmer-Schur algorithm is one of a class of methods that isolate roots in the complex plane by generalizing the notion of one-dimensional bracketing. It is possible to determine efficiently whether there are any polynomial roots within a circle of given center and radius. From then on it is a matter of bookkeeping to hunt down all the roots by a series of decisions regarding where to place new trial circles. Consult [1] for an introduction.
Techniques for Root-Polishing Newton-Raphson works very well for real roots once the neighborhood of a root has been identified. The polynomial and its derivative can be efficiently simultaneously evaluated as in §5.3. For a polynomial of degree n-1 with coefficients c(1)...c(n), the following segment of code embodies one cycle of NewtonRaphson: p=c(n)*x+c(n-1) p1=c(n) do 11 i=n-2,1,-1 p1=p+p1*x p=c(i)+p*x enddo 11 if (p1.eq.0.) pause ’derivative should not vanish’ x=x-p/p1
Once all real roots of a polynomial have been polished, one must polish the complex roots, either directly, or by looking for quadratic factors. Direct polishing by Newton-Raphson is straightforward for complex roots if the above code is converted to complex data types. With real polynomial coefficients, note that your starting guess (tentative root) must be off the real axis, otherwise you will never get off that axis — and may get shot off to infinity by a minimum or maximum of the polynomial.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
369
370
Chapter 9.
Root Finding and Nonlinear Sets of Equations
For real polynomials, the alternative means of polishing complex roots (or, for that matter, double real roots) is Bairstow’s method, which seeks quadratic factors. The advantage of going after quadratic factors is that it avoids all complex arithmetic. Bairstow’s method seeks a quadratic factor that embodies the two roots x = a ± ib, namely x2 − 2ax + (a2 + b2 ) ≡ x2 + Bx + C
(9.5.14)
In general if we divide a polynomial by a quadratic factor, there will be a linear remainder (9.5.15)
Given B and C, R and S can be readily found, by polynomial division (§5.3). We can consider R and S to be adjustable functions of B and C, and they will be zero if the quadratic factor is zero. In the neighborhood of a root a first-order Taylor series expansion approximates the variation of R, S with respect to small changes in B, C ∂R ∂R R(B + δB, C + δC) ≈ R(B, C) + δB + δC (9.5.16) ∂B ∂C ∂S ∂S δB + δC (9.5.17) ∂B ∂C To evaluate the partial derivatives, consider the derivative of (9.5.15) with respect to C. Since P (x) is a fixed polynomial, it is independent of C, hence S(B + δB, C + δC) ≈ S(B, C) +
0 = (x2 + Bx + C)
∂Q ∂R ∂S + Q(x) + x+ ∂C ∂C ∂C
(9.5.18)
which can be rewritten as ∂Q ∂R ∂S + x+ (9.5.19) ∂C ∂C ∂C Similarly, P (x) is independent of B, so differentiating (9.5.15) with respect to B gives ∂Q ∂R ∂S −xQ(x) = (x2 + Bx + C) + x+ (9.5.20) ∂B ∂B ∂B Now note that equation (9.5.19) matches equation (9.5.15) in form. Thus if we perform a second synthetic division of P (x), i.e., a division of Q(x), yielding a remainder R1 x+S1 , then ∂R ∂S = −R1 = −S1 (9.5.21) ∂C ∂C To get the remaining partial derivatives, evaluate equation (9.5.20) at the two roots of the quadratic, x+ and x− . Since −Q(x) = (x2 + Bx + C)
Q(x± ) = R1 x± + S1
(9.5.22)
∂R ∂S x+ + = −x+ (R1 x+ + S1 ) ∂B ∂B
(9.5.23)
we get
∂R ∂S x− + = −x− (R1 x− + S1 ) ∂B ∂B Solve these two equations for the partial derivatives, using x+ + x− = −B
x+ x− = C
(9.5.24)
(9.5.25)
and find ∂R ∂S = BR1 − S1 = CR1 (9.5.26) ∂B ∂B Bairstow’s method now consists of using Newton-Raphson in two dimensions (which is actually the subject of the next section) to find a simultaneous zero of R and S. Synthetic division is used twice per cycle to evaluate R, S and their partial derivatives with respect to B, C. Like one-dimensional Newton-Raphson, the method works well in the vicinity of a root pair (real or complex), but it can fail miserably when started at a random point. We therefore recommend it only in the context of polishing tentative complex roots.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
P (x) = (x2 + Bx + C)Q(x) + Rx + S.
371
9.5 Roots of Polynomials
C
We have already remarked on the annoyance of having two tentative roots collapse to one value under polishing. You are left not knowing whether your polishing procedure has lost a root, or whether there is actually a double root, which was split only by roundoff errors in your previous deflation. One solution is deflate-and-repolish; but deflation is what we are trying to avoid at the polishing stage. An alternative is Maehly’s procedure. Maehly pointed out that the derivative of the reduced polynomial Pj (x) ≡
P (x) (x − x1 ) · · · (x − xj )
(9.5.27)
can be written as X P (x) P 0 (x) − (x − xi )−1 (9.5.28) (x − x1 ) · · · (x − xj ) (x − x1 ) · · · (x − xj ) j
Pj0 (x) =
i=1
Hence one step of Newton-Raphson, taking a guess xk into a new guess xk+1 , can be written as xk+1 = xk −
P 0 (x
P (xk ) Pj −1 k ) − P (xk ) i=1 (xk − xi )
(9.5.29)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
* *
SUBROUTINE qroot(p,n,b,c,eps) INTEGER n,NMAX,ITMAX REAL b,c,eps,p(n),TINY PARAMETER (NMAX=20,ITMAX=20,TINY=1.0e-6) USES poldiv Given coefficients p(1:n) of a polynomial of degree n-1, and trial values for the coefficients of a quadratic factor x*x+b*x+c, improve the solution until the coefficients b,c change by less than eps. The routine poldiv §5.3 is used. Parameters: At most NMAX coefficients, ITMAX iterations. INTEGER iter REAL delb,delc,div,r,rb,rc,s,sb,sc,d(3),q(NMAX),qq(NMAX),rem(NMAX) d(3)=1. do 11 iter=1,ITMAX d(2)=b d(1)=c call poldiv(p,n,d,3,q,rem) s=rem(1) First division r,s. r=rem(2) call poldiv(q,n-1,d,3,qq,rem) sc=-rem(1) Second division partial r,s with respect to c. rc=-rem(2) sb=-c*rc rb=sc-b*rc div=1./(sb*rc-sc*rb) Solve 2x2 equation. delb=(r*sc-s*rc)*div delc=(-r*sb+s*rb)*div b=b+delb c=c+delc if((abs(delb).le.eps*abs(b).or.abs(b).lt.TINY) .and.(abs(delc).le.eps*abs(c) .or.abs(c).lt.TINY)) return Coefficients converged. enddo 11 pause ’too many iterations in qroot’ END
372
Chapter 9.
Root Finding and Nonlinear Sets of Equations
This equation, if used with i ranging over the roots already polished, will prevent a tentative root from spuriously hopping to another one’s true root. It is an example of so-called zero suppression as an alternative to true deflation. Muller’s method, which was described above, can also be useful at the polishing stage.
Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathematical Association of America), Chapter 7. [1] Peters G., and Wilkinson, J.H. 1971, Journal of the Institute of Mathematics and its Applications, vol. 8, pp. 16–35. [2]
IMSL Math/Library Users Manual (IMSL Inc., 2500 CityWest Boulevard, Houston TX 77042). [3] Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill), §8.9–8.13. [4] Adams, D.A. 1967, Communications of the ACM, vol. 10, pp. 655–658. [5] Johnson, L.W., and Riess, R.D. 1982, Numerical Analysis, 2nd ed. (Reading, MA: AddisonWesley), §4.4.3. [6] Henrici, P. 1974, Applied and Computational Complex Analysis, vol. 1 (New York: Wiley). Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag), §§5.5–5.9.
9.6 Newton-Raphson Method for Nonlinear Systems of Equations We make an extreme, but wholly defensible, statement: There are no good, general methods for solving systems of more than one nonlinear equation. Furthermore, it is not hard to see why (very likely) there never will be any good, general methods: Consider the case of two dimensions, where we want to solve simultaneously f(x, y) = 0 g(x, y) = 0
(9.6.1)
The functions f and g are two arbitrary functions, each of which has zero contour lines that divide the (x, y) plane into regions where their respective function is positive or negative. These zero contour boundaries are of interest to us. The solutions that we seek are those points (if any) that are common to the zero contours of f and g (see Figure 9.6.1). Unfortunately, the functions f and g have, in general, no relation to each other at all! There is nothing special about a common point from either f’s point of view, or from g’s. In order to find all common points, which are the solutions of our nonlinear equations, we will (in general) have to do neither more nor less than map out the full zero contours of both functions. Note further that the zero contours will (in general) consist of an unknown number of disjoint closed curves. How can we ever hope to know when we have found all such disjoint pieces? For problems in more than two dimensions, we need to find points mutually common to N unrelated zero-contour hypersurfaces, each of dimension N − 1.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
CITED REFERENCES AND FURTHER READING:
373
9.6 Newton-Raphson Method for Nonlinear Systems of Equations
no root here! two roots here
f pos
g pos
M
f=0 g pos
g=
g neg
f pos
f neg
g neg
=
0 g
g=
0
g pos x
Figure 9.6.1. Solution of two nonlinear equations in two unknowns. Solid curves refer to f (x, y), dashed curves to g(x, y). Each equation divides the (x, y) plane into positive and negative regions, bounded by zero curves. The desired solutions are the intersections of these unrelated zero curves. The number of solutions is a priori unknown.
You see that root finding becomes virtually impossible without insight! You will almost always have to use additional information, specific to your particular problem, to answer such basic questions as, “Do I expect a unique solution?” and “Approximately where?” Acton [1] has a good discussion of some of the particular strategies that can be tried. In this section we will discuss the simplest multidimensional root finding method, Newton-Raphson. This method gives you a very efficient means of converging to a root, if you have a sufficiently good initial guess. It can also spectacularly fail to converge, indicating (though not proving) that your putative root does not exist nearby. In §9.7 we discuss more sophisticated implementations of the Newton-Raphson method, which try to improve on Newton-Raphson’s poor global convergence. A multidimensional generalization of the secant method, called Broyden’s method, is also discussed in §9.7. A typical problem gives N functional relations to be zeroed, involving variables xi , i = 1, 2, . . . , N : Fi (x1 , x2, . . . , xN ) = 0
i = 1, 2, . . . , N.
(9.6.2)
We let x denote the entire vector of values xi and F denote the entire vector of functions Fi . In the neighborhood of x, each of the functions Fi can be expanded in Taylor series
Fi (x + δx) = Fi (x) +
N X ∂Fi
∂xj j=1
δxj + O(δx2 ).
(9.6.3)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
f=0
y
0
f pos
374
Chapter 9.
Root Finding and Nonlinear Sets of Equations
The matrix of partial derivatives appearing in equation (9.6.3) is the Jacobian matrix J: Jij ≡
∂Fi . ∂xj
(9.6.4)
F(x + δx) = F(x) + J · δx + O(δx2 ).
(9.6.5)
By neglecting terms of order δx2 and higher and by setting F(x + δx) = 0, we obtain a set of linear equations for the corrections δx that move each function closer to zero simultaneously, namely J · δx = −F.
(9.6.6)
Matrix equation (9.6.6) can be solved by LU decomposition as described in §2.3. The corrections are then added to the solution vector, xnew = xold + δx
(9.6.7)
and the process is iterated to convergence. In general it is a good idea to check the degree to which both functions and variables have converged. Once either reaches machine accuracy, the other won’t change. The following routine mnewt performs ntrial iterations starting from an initial guess at the solution vector x of length n variables. Iteration stops if either the sum of the magnitudes of the functions Fi is less than some tolerance tolf, or the sum of the absolute values of the corrections to δxi is less than some tolerance tolx. mnewt calls a user supplied subroutine usrfun which must return the function values F and the Jacobian matrix J. If J is difficult to compute analytically, you can try having usrfun call the routine fdjac of §9.7 to compute the partial derivatives by finite differences. You should not make ntrial too big; rather inspect to see what is happening before continuing for some further iterations.
C
SUBROUTINE mnewt(ntrial,x,n,tolx,tolf) INTEGER n,ntrial,NP REAL tolf,tolx,x(n) PARAMETER (NP=15) Up to NP variables. USES lubksb,ludcmp,usrfun Given an initial guess x for a root in n dimensions, take ntrial Newton-Raphson steps to improve the root. Stop if the root converges in either summed absolute variable increments tolx or summed absolute function values tolf. INTEGER i,k,indx(NP) REAL d,errf,errx,fjac(NP,NP),fvec(NP),p(NP) do 14 k=1,ntrial call usrfun(x,n,NP,fvec,fjac) User subroutine supplies function values at x in fvec errf=0. and Jacobian matrix in fjac. do 11 i=1,n Check function convergence. errf=errf+abs(fvec(i)) enddo 11 if(errf.le.tolf)return do 12 i=1,n Right-hand side of linear equations. p(i)=-fvec(i) enddo 12
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
In matrix notation equation (9.6.3) is
9.6 Newton-Raphson Method for Nonlinear Systems of Equations
Solve linear equations using LU decomposition. Check root convergence. Update solution.
Newton’s Method versus Minimization In the next chapter, we will find that there are efficient general techniques for finding a minimum of a function of many variables. Why is that task (relatively) easy, while multidimensional root finding is often quite hard? Isn’t minimization equivalent to finding a zero of an N -dimensional gradient vector, not so different from zeroing an N -dimensional function? No! The components of a gradient vector are not independent, arbitrary functions. Rather, they obey so-called integrability conditions that are highly restrictive. Put crudely, you can always find a minimum by sliding downhill on a single surface. The test of “downhillness” is thus one-dimensional. There is no analogous conceptual procedure for finding a multidimensional root, where “downhill” must mean simultaneously downhill in N separate function spaces, thus allowing a multitude of trade-offs, as to how much progress in one dimension is worth compared with progress in another. It might occur to you to carry out multidimensional root finding by collapsing all these dimensions into one: Add up the sums of squares of the individual functions Fi to get a master function F which (i) is positive definite, and (ii) has a global minimum of zero exactly at all solutions of the original set of nonlinear equations. Unfortunately, as you will see in the next chapter, the efficient algorithms for finding minima come to rest on global and local minima indiscriminately. You will often find, to your great dissatisfaction, that your function F has a great number of local minima. In Figure 9.6.1, for example, there is likely to be a local minimum wherever the zero contours of f and g make a close approach to each other. The point labeled M is such a point, and one sees that there are no nearby roots. However, we will now see that sophisticated strategies for multidimensional root finding can in fact make use of the idea of minimizing a master function F , by combining it with Newton’s method applied to the full set of functions Fi . While such methods can still occasionally fail by coming to rest on a local minimum of F , they often succeed where a direct attack via Newton’s method alone fails. The next section deals with these methods. CITED REFERENCES AND FURTHER READING: Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathematical Association of America), Chapter 14. [1] Ostrowski, A.M. 1966, Solutions of Equations and Systems of Equations, 2nd ed. (New York: Academic Press). Ortega, J., and Rheinboldt, W. 1970, Iterative Solution of Nonlinear Equations in Several Variables (New York: Academic Press).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
call ludcmp(fjac,n,NP,indx,d) call lubksb(fjac,n,NP,indx,p) errx=0. do 13 i=1,n errx=errx+abs(p(i)) x(i)=x(i)+p(i) enddo 13 if(errx.le.tolx)return enddo 14 return END
375
376
Chapter 9.
Root Finding and Nonlinear Sets of Equations
9.7 Globally Convergent Methods for Nonlinear Systems of Equations
F(x) = 0
(9.7.1)
xnew = xold + δx
(9.7.2)
δx = −J−1 · F
(9.7.3)
is where Here J is the Jacobian matrix. How do we decide whether to accept the Newton step δx? A reasonable strategy is to require that the step decrease |F|2 = F · F. This is the same requirement we would impose if we were trying to minimize f=
1 F·F 2
(9.7.4)
(The 12 is for later convenience.) Every solution to (9.7.1) minimizes (9.7.4), but there may be local minima of (9.7.4) that are not solutions to (9.7.1). Thus, as already mentioned, simply applying one of our minimum finding algorithms from Chapter 10 to (9.7.4) is not a good idea. To develop a better strategy, note that the Newton step (9.7.3) is a descent direction for f: ∇f · δx = (F · J) · (−J −1 · F) = −F · F < 0
(9.7.5)
Thus our strategy is quite simple: We always first try the full Newton step, because once we are close enough to the solution we will get quadratic convergence. However, we check at each iteration that the proposed step reduces f. If not, we backtrack along the Newton direction until we have an acceptable step. Because the Newton step is a descent direction for f, we are guaranteed to find an acceptable step by backtracking. We will discuss the backtracking algorithm in more detail below. Note that this method essentially minimizes f by taking Newton steps designed to bring F to zero. This is not equivalent to minimizing f directly by taking Newton steps designed to bring ∇f to zero. While the method can still occasionally fail by landing on a local minimum of f, this is quite rare in practice. The routine newt below will warn you if this happens. The remedy is to try a new starting point.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
We have seen that Newton’s method for solving nonlinear equations has an unfortunate tendency to wander off into the wild blue yonder if the initial guess is not sufficiently close to the root. A global method is one that converges to a solution from almost any starting point. In this section we will develop an algorithm that combines the rapid local convergence of Newton’s method with a globally convergent strategy that will guarantee some progress towards the solution at each iteration. The algorithm is closely related to the quasi-Newton method of minimization which we will describe in §10.7. Recall our discussion of §9.6: the Newton step for the set of equations
9.7 Globally Convergent Methods for Nonlinear Systems of Equations
377
Line Searches and Backtracking
xnew = xold + λp,
0slowc only a fraction of the corrections are used, but when err≤slowc the entire correction gets applied. The call statement also supplies solvde with the array y(1:nyj,1:nyk) containing the initial trial solution, and workspace arrays c(1:nci,1:ncj,1:nck), s(1:nsi,1:nsj). The array c is the blockbuster: It stores the unreduced elements of the matrix built up for the backsubstitution step. If there are m mesh points, then there will be nck=m+1 blocks, each requiring nci=ne rows and ncj=ne-nb+1 columns. Although large, this is small compared with (ne×m)2 elements required for the whole matrix if we did not break it into blocks. We now describe the workings of the user-supplied subroutine difeq. The parameters of the subroutine are given by SUBROUTINE difeq(k,k1,k2,jsf,is1,isf,indexv,ne,s,nsi,nsj,y,nyj,nyk)
The only information returned from difeq to solvde is the matrix of derivatives s(i,j); all other arguments are input to difeq and should not be altered. k indicates the current mesh point, or block number. k1,k2 label the first and last point in the mesh. If k=k1 or k>k2, the block involves the boundary conditions at the first or final points; otherwise the block acts on FDEs coupling variables at points k-1, k. The convention on storing information into the array s(i,j) follows that used in equations (17.3.8), (17.3.10), and (17.3.12): Rows i label equations, columns j refer to derivatives with respect to dependent variables in the solution. Recall that each equation will depend on the ne dependent variables at either one or two points. Thus, j runs from 1 to either ne or 2*ne. The column ordering for dependent variables at each point must agree with the list supplied in indexv(j). Thus, for a block not at a boundary, the first column multiplies ∆Y (l=indexv(1),k-1), and the column ne+1 multiplies ∆Y (l=indexv(1),k). is1,isf give the numbers of the starting and final rows that need to be filled in the s matrix for this block. jsf labels the column in which the difference equations Ej,k of equations (17.3.3)–(17.3.5) are stored. Thus, −s(i,jsf) is the vector on the right-hand side of the matrix. The reason for the minus sign is that difeq supplies the actual difference equation, Ej,k , not its negative. Note that solvde supplies a value for jsf such that the difference equation is put in the column just after all derivatives in the s matrix. Thus, difeq expects to find values entered into s(i,j) for rows is1 ≤ i ≤ isf and 1 ≤ j ≤ jsf.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
and (17.3.12), and the dependent variables. As described above it is essential that the nb boundary conditions at k=1 involve the dependent variables referenced by the first nb columns of the s matrix. Thus, columns j of the s matrix can be ordered by the user in difeq to refer to derivatives with respect to the dependent variable indexv(j). The subroutine only attempts itmax correction cycles before returning, even if the solution has not converged. The parameters conv, slowc, scalv relate to convergence. Each inversion of the matrix produces corrections for ne variables at m mesh points. We want these to become vanishingly small as the iterations proceed, but we must define a measure for the size of corrections. This error “norm” is very problem specific, so the user might wish to rewrite this section of the code as appropriate. In the program below we compute a value for the average correction err by summing the absolute value of all corrections, weighted by a scale factor appropriate to each type of variable:
760
Chapter 17.
Two Point Boundary Value Problems
Finally, s(1:nsi,1:nsj) and y(1:nyj,1:nyk) supply difeq with storage for s and the solution variables y for this iteration. An example of how to use this routine is given in the next section. Many ideas in the following code are due to Eggleton [1].
* *
C
*
*
*
SUBROUTINE solvde(itmax,conv,slowc,scalv,indexv,ne,nb,m, y,nyj,nyk,c,nci,ncj,nck,s,nsi,nsj) INTEGER itmax,m,nb,nci,ncj,nck,ne,nsi,nsj, nyj,nyk,indexv(nyj),NMAX REAL conv,slowc,c(nci,ncj,nck),s(nsi,nsj), scalv(nyj),y(nyj,nyk) PARAMETER (NMAX=10) Largest expected value of ne. USES bksub,difeq,pinvs,red Driver routine for solution of two point boundary value problems by relaxation. itmax is the maximum number of iterations. conv is the convergence criterion (see text). slowc controls the fraction of corrections actually used after each iteration. scalv(1:nyj) contains typical sizes for each dependent variable, used to weight errors. indexv(1:nyj) lists the column ordering of variables used to construct the matrix s of derivatives. (The nb boundary conditions at the first mesh point must contain some dependence on the first nb variables listed in indexv.) The problem involves ne equations for ne adjustable dependent variables at each point. At the first mesh point there are nb boundary conditions. There are a total of m mesh points. y(1:nyj,1:nyk) is the two-dimensional array that contains the initial guess for all the dependent variables at each mesh point. On each iteration, it is updated by the calculated correction. The arrays c(1:nci,1:ncj,1:nck), s(1:nsi,1:nsj) supply dummy storage used by the relaxation code; the minimum dimensions must satisfy: nci=ne, ncj=ne-nb+1, nck=m+1, nsi=ne, nsj=2*ne+1. INTEGER ic1,ic2,ic3,ic4,it,j,j1,j2,j3,j4,j5,j6,j7,j8, j9,jc1,jcf,jv,k,k1,k2,km,kp,nvars,kmax(NMAX) REAL err,errj,fac,vmax,vz,ermax(NMAX) k1=1 Set up row and column markers. k2=m nvars=ne*m j1=1 j2=nb j3=nb+1 j4=ne j5=j4+j1 j6=j4+j2 j7=j4+j3 j8=j4+j4 j9=j8+j1 ic1=1 ic2=ne-nb ic3=ic2+1 ic4=ne jc1=1 jcf=ic3 do 16 it=1,itmax Primary iteration loop. k=k1 Boundary conditions at first point. call difeq(k,k1,k2,j9,ic3,ic4,indexv,ne,s,nsi,nsj,y,nyj,nyk) call pinvs(ic3,ic4,j5,j9,jc1,k1,c,nci,ncj,nck,s,nsi,nsj) do 11 k=k1+1,k2 Finite difference equations at all point pairs. kp=k-1 call difeq(k,k1,k2,j9,ic1,ic4,indexv,ne,s,nsi,nsj,y,nyj,nyk) call red(ic1,ic4,j1,j2,j3,j4,j9,ic3,jc1,jcf,kp, c,nci,ncj,nck,s,nsi,nsj) call pinvs(ic1,ic4,j3,j9,jc1,k,c,nci,ncj,nck,s,nsi,nsj) enddo 11 k=k2+1 Final boundary conditions. call difeq(k,k1,k2,j9,ic1,ic2,indexv,ne,s,nsi,nsj,y,nyj,nyk) call red(ic1,ic2,j5,j6,j7,j8,j9,ic3,jc1,jcf,k2, c,nci,ncj,nck,s,nsi,nsj) call pinvs(ic1,ic2,j7,j9,jcf,k2+1,c,nci,ncj,nck,s,nsi,nsj)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
17.3 Relaxation Methods
761
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
call bksub(ne,nb,jcf,k1,k2,c,nci,ncj,nck) Backsubstitution. err=0. do 13 j=1,ne Convergence check, accumulate average error. jv=indexv(j) errj=0. km=0 vmax=0. do 12 k=k1,k2 Find point with largest error, for each dependent variable. vz=abs(c(jv,1,k)) if(vz.gt.vmax) then vmax=vz km=k endif errj=errj+vz enddo 12 err=err+errj/scalv(j) Note weighting for each dependent variable. ermax(j)=c(jv,1,km)/scalv(j) kmax(j)=km enddo 13 err=err/nvars fac=slowc/max(slowc,err) Reduce correction applied when error is large. do 15 j=1,ne Apply corrections. jv=indexv(j) do 14 k=k1,k2 y(j,k)=y(j,k)-fac*c(jv,1,k) enddo 14 enddo 15 write(*,100) it,err,fac Summary of corrections for this step. Point with largest if(err.lt.conv) return error for each variable can be monitored by writenddo 16 ing out kmax and ermax. pause ’itmax exceeded in solvde’ Convergence failed. 100 format(1x,i4,2f12.6) return END SUBROUTINE bksub(ne,nb,jf,k1,k2,c,nci,ncj,nck) INTEGER jf,k1,k2,nb,nci,ncj,nck,ne REAL c(nci,ncj,nck) Backsubstitution, used internally by solvde. INTEGER i,im,j,k,kp,nbf REAL xx nbf=ne-nb im=1 do 13 k=k2,k1,-1 Use recurrence relations to eliminate remaining dependences. if (k.eq.k1) im=nbf+1 Special handling of first point. kp=k+1 do 12 j=1,nbf xx=c(j,jf,kp) do 11 i=im,ne c(i,jf,k)=c(i,jf,k)-c(i,j,k)*xx enddo 11 enddo 12 enddo 13 do 16 k=k1,k2 Reorder corrections to be in column 1. kp=k+1 do 14 i=1,nb c(i,1,k)=c(i+nbf,jf,k) enddo 14 do 15 i=1,nbf c(i+nb,1,k)=c(i,jf,kp) enddo 15 enddo 16 return END
762
Chapter 17.
Two Point Boundary Value Problems
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE pinvs(ie1,ie2,je1,jsf,jc1,k,c,nci,ncj,nck,s,nsi,nsj) INTEGER ie1,ie2,jc1,je1,jsf,k,nci,ncj,nck,nsi,nsj,NMAX REAL c(nci,ncj,nck),s(nsi,nsj) PARAMETER (NMAX=10) Diagonalize the square subsection of the s matrix, and store the recursion coefficients in c; used internally by solvde. INTEGER i,icoff,id,ipiv,irow,j,jcoff,je2,jp,jpiv,js1,indxr(NMAX) REAL big,dum,piv,pivinv,pscl(NMAX) je2=je1+ie2-ie1 js1=je2+1 do 12 i=ie1,ie2 Implicit pivoting, as in §2.1. big=0. do 11 j=je1,je2 if(abs(s(i,j)).gt.big) big=abs(s(i,j)) enddo 11 if(big.eq.0.) pause ’singular matrix, row all 0 in pinvs’ pscl(i)=1./big indxr(i)=0 enddo 12 do 18 id=ie1,ie2 piv=0. do 14 i=ie1,ie2 Find pivot element. if(indxr(i).eq.0) then big=0. do 13 j=je1,je2 if(abs(s(i,j)).gt.big) then jp=j big=abs(s(i,j)) endif enddo 13 if(big*pscl(i).gt.piv) then ipiv=i jpiv=jp piv=big*pscl(i) endif endif enddo 14 if(s(ipiv,jpiv).eq.0.) pause ’singular matrix in pinvs’ indxr(ipiv)=jpiv In place reduction. Save column ordering. pivinv=1./s(ipiv,jpiv) do 15 j=je1,jsf Normalize pivot row. s(ipiv,j)=s(ipiv,j)*pivinv enddo 15 s(ipiv,jpiv)=1. do 17 i=ie1,ie2 Reduce nonpivot elements in column. if(indxr(i).ne.jpiv) then if(s(i,jpiv).ne.0.) then dum=s(i,jpiv) do 16 j=je1,jsf s(i,j)=s(i,j)-dum*s(ipiv,j) enddo 16 s(i,jpiv)=0. endif endif enddo 17 enddo 18 jcoff=jc1-js1 Sort and store unreduced coefficients. icoff=ie1-je1 do 21 i=ie1,ie2 irow=indxr(i)+icoff do 19 j=js1,jsf c(irow,j+jcoff,k)=s(i,j) enddo 19 enddo 21
17.3 Relaxation Methods
763
return END
*
“Algebraically Difficult” Sets of Differential Equations Relaxation methods allow you to take advantage of an additional opportunity that, while not obvious, can speed up some calculations enormously. It is not necessary that the set of variables yj,k correspond exactly with the dependent variables of the original differential equations. They can be related to those variables through algebraic equations. Obviously, it is necessary only that the solution variables allow us to evaluate the functions y, g, B, C that are used to construct the FDEs from the ODEs. In some problems g depends on functions of y that are known only implicitly, so that iterative solutions are necessary to evaluate functions in the ODEs. Often one can dispense with this “internal” nonlinear problem by defining a new set of variables from which both y, g and the boundary conditions can be obtained directly. A typical example occurs in physical problems where the equations require solution of a complex equation of state that can be expressed in more convenient terms using variables other than the original dependent variables in the ODE. While this approach is analogous to performing an analytic change of variables directly on the original ODEs, such an analytic transformation might be prohibitively complicated. The change of variables in the relaxation method is easy and requires no analytic manipulations. CITED REFERENCES AND FURTHER READING: Eggleton, P.P. 1971, Monthly Notices of the Royal Astronomical Society, vol. 151, pp. 351–364. [1] Keller, H.B. 1968, Numerical Methods for Two-Point Boundary-Value Problems (Waltham, MA: Blaisdell). Kippenhan, R., Weigert, A., and Hofmeister, E. 1968, in Methods in Computational Physics, vol. 7 (New York: Academic Press), pp. 129ff.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
SUBROUTINE red(iz1,iz2,jz1,jz2,jm1,jm2,jmf,ic1,jc1,jcf,kc, c,nci,ncj,nck,s,nsi,nsj) INTEGER ic1,iz1,iz2,jc1,jcf,jm1,jm2,jmf,jz1,jz2,kc,nci,ncj, nck,nsi,nsj REAL c(nci,ncj,nck),s(nsi,nsj) Reduce columns jz1-jz2 of the s matrix, using previous results as stored in the c matrix. Only columns jm1-jm2,jmf are affected by the prior results. red is used internally by solvde. INTEGER i,ic,j,l,loff REAL vx loff=jc1-jm1 ic=ic1 do 14 j=jz1,jz2 Loop over columns to be zeroed. do 12 l=jm1,jm2 Loop over columns altered. vx=c(ic,l+loff,kc) do 11 i=iz1,iz2 Loop over rows. s(i,l)=s(i,l)-s(i,j)*vx enddo 11 enddo 12 vx=c(ic,jcf,kc) do 13 i=iz1,iz2 Plus final element. s(i,jmf)=s(i,jmf)-s(i,j)*vx enddo 13 ic=ic+1 enddo 14 return END
764
Chapter 17.
Two Point Boundary Value Problems
17.4 A Worked Example: Spheroidal Harmonics
Here m is an integer, c is the “oblateness parameter,” and λ is the eigenvalue. Despite the notation, c2 can be positive or negative. For c2 > 0 the functions are called “prolate,” while if c2 < 0 they are called “oblate.” The equation has singular points at x = ±1 and is to be solved subject to the boundary conditions that the solution be regular at x = ±1. Only for certain values of λ, the eigenvalues, will this be possible. If we consider first the spherical case, where c = 0, we recognize the differential equation for Legendre functions Pnm (x). In this case the eigenvalues are λmn = n(n + 1), n = m, m + 1, . . . . The integer n labels successive eigenvalues for fixed m: When n = m we have the lowest eigenvalue, and the corresponding eigenfunction has no nodes in the interval −1 < x < 1; when n = m + 1 we have the next eigenvalue, and the eigenfunction has one node inside (−1, 1); and so on. A similar situation holds for the general case c2 6= 0. We write the eigenvalues of (17.4.1) as λmn (c) and the eigenfunctions as Smn (x; c). For fixed m, n = m, m + 1, . . . labels the successive eigenvalues. The computation of λmn (c) and Smn (x; c) traditionally has been quite difficult. Complicated recurrence relations, power series expansions, etc., can be found in references [1-3]. Cheap computing makes evaluation by direct solution of the differential equation quite feasible. The first step is to investigate the behavior of the solution near the singular points x = ±1. Substituting a power series expansion of the form S = (1 ± x)α
∞ X
ak (1 ± x)k
(17.4.2)
k=0
in equation (17.4.1), we find that the regular solution has α = m/2. (Without loss of generality we can take m ≥ 0 since m → −m is a symmetry of the equation.) We get an equation that is numerically more tractable if we factor out this behavior. Accordingly we set S = (1 − x2 )m/2 y
(17.4.3)
We then find from (17.4.1) that y satisfies the equation (1 − x2 )
d2 y dy + (µ − c2 x2 )y = 0 − 2(m + 1)x dx2 dx
(17.4.4)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The best way to understand the algorithms of the previous sections is to see them employed to solve an actual problem. As a sample problem, we have selected the computation of spheroidal harmonics. (The more common name is spheroidal angle functions, but we prefer the explicit reminder of the kinship with spherical harmonics.) We will show how to find spheroidal harmonics, first by the method of relaxation (§17.3), and then by the methods of shooting (§17.1) and shooting to a fitting point (§17.2). Spheroidal harmonics typically arise when certain partial differential equations are solved by separation of variables in spheroidal coordinates. They satisfy the following differential equation on the interval −1 ≤ x ≤ 1: d m2 2 dS 2 2 (1 − x ) + λ−c x − S=0 (17.4.1) dx dx 1 − x2
765
17.4 A Worked Example: Spheroidal Harmonics
where µ ≡ λ − m(m + 1)
(17.4.5)
ymn (−x) = (−1)n−m ymn (x)
(17.4.6)
and similarly for Smn . The boundary conditions on (17.4.4) require that y be regular at x = ±1. In other words, near the endpoints the solution takes the form y = a0 + a1 (1 − x2 ) + a2 (1 − x2 )2 + . . .
(17.4.7)
Substituting this expansion in equation (17.4.4) and letting x → 1, we find that µ − c2 a0 4(m + 1)
(17.4.8)
µ − c2 y(1) 2(m + 1)
(17.4.9)
a1 = − Equivalently, y0 (1) =
A similar equation holds at x = −1 with a minus sign on the right-hand side. The irregular solution has a different relation between function and derivative at the endpoints. Instead of integrating the equation from −1 to 1, we can exploit the symmetry (17.4.6) to integrate from 0 to 1. The boundary condition at x = 0 is y(0) = 0,
n − m odd
y0 (0) = 0,
n − m even
(17.4.10)
A third boundary condition comes from the fact that any constant multiple of a solution y is a solution. We can thus normalize the solution. We adopt the normalization that the function Smn has the same limiting behavior as Pnm at x = 1: lim (1 − x2 )−m/2 Smn (x; c) = lim (1 − x2 )−m/2 Pnm (x)
x→1
x→1
(17.4.11)
Various normalization conventions in the literature are tabulated by Flammer [1].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Both equations (17.4.1) and (17.4.4) are invariant under the replacement x → −x. Thus the functions S and y must also be invariant, except possibly for an overall scale factor. (Since the equations are linear, a constant multiple of a solution is also a solution.) Because the solutions will be normalized, the scale factor can only be ±1. If n − m is odd, there are an odd number of zeros in the interval (−1, 1). Thus we must choose the antisymmetric solution y(−x) = −y(x) which has a zero at x = 0. Conversely, if n − m is even we must have the symmetric solution. Thus
766
Chapter 17.
Two Point Boundary Value Problems
Imposing three boundary conditions for the second-order equation (17.4.4) turns it into an eigenvalue problem for λ or equivalently for µ. We write it in the standard form by setting (17.4.12) (17.4.13)
y3 = µ
(17.4.14)
Then y10 = y2
(17.4.15)
1 y20 = 2x(m + 1)y2 − (y3 − c2 x2 )y1 2 1−x y30 = 0
(17.4.16) (17.4.17)
The boundary condition at x = 0 in this notation is y1 = 0,
n − m odd
y2 = 0,
n − m even
(17.4.18)
At x = 1 we have two conditions: y2 =
y3 − c2 y1 2(m + 1)
(17.4.19)
y1 = lim (1 − x2 )−m/2 Pnm (x) = x→1
(−1)m (n + m)! ≡γ 2m m!(n − m)!
(17.4.20)
We are now ready to illustrate the use of the methods of previous sections on this problem.
Relaxation If we just want a few isolated values of λ or S, shooting is probably the quickest method. However, if we want values for a large sequence of values of c, relaxation is better. Relaxation rewards a good initial guess with rapid convergence, and the previous solution should be a good initial guess if c is changed only slightly. For simplicity, we choose a uniform grid on the interval 0 ≤ x ≤ 1. For a total of M mesh points, we have h=
1 M −1
xk = (k − 1)h,
(17.4.21) k = 1, 2, . . . , M
(17.4.22)
At interior points k = 2, 3, . . ., M , equation (17.4.15) gives E1,k = y1,k − y1,k−1 −
h (y2,k + y2,k−1 ) 2
(17.4.23)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
y1 = y y2 = y0
17.4 A Worked Example: Spheroidal Harmonics
767
Equation (17.4.16) gives E2,k = y2,k − y2,k−1 − βk (y1,k + y1,k−1 ) (xk + xk−1 )(m + 1)(y2,k + y2,k−1 ) − αk × 2 2
(17.4.24)
y3,k + y3,k−1 c2 (xk + xk−1)2 − 2 4 h βk = 1 − 14 (xk + xk−1 )2
αk =
(17.4.25) (17.4.26)
Finally, equation (17.4.17) gives E3,k = y3,k − y3,k−1
(17.4.27)
Now recall that the matrix of partial derivatives Si,j of equation (17.3.8) is defined so that i labels the equation and j the variable. In our case, j runs from 1 to 3 for yj at k − 1 and from 4 to 6 for yj at k. Thus equation (17.4.23) gives S1,1 = −1, S1,4 = 1,
h S1,2 = − , 2 h S1,5 = − , 2
S1,3 = 0 (17.4.28) S1,6 = 0
Similarly equation (17.4.24) yields S2,2 = −1 − βk (xk + xk−1)(m + 1)/2, S2,4 = S2,1 , S2,6 = S2,3 (17.4.29) while from equation (17.4.27) we find S2,1 = αk βk /2, S2,3 = βk (y1,k + y1,k−1)/4 S2,5 = 2 + S2,2 ,
S3,1 = 0, S3,4 = 0,
S3,2 = 0, S3,5 = 0,
S3,3 = −1 S3,6 = 1
At x = 0 we have the boundary condition y1,1 , n − m odd E3,1 = y2,1 , n − m even
(17.4.30)
(17.4.31)
Recall the convention adopted in the solvde routine that for one boundary condition at k = 1 only S3,j can be nonzero. Also, j takes on the values 4 to 6 since the boundary condition involves only yk , not yk−1 . Accordingly, the only nonzero values of S3,j at x = 0 are S3,4 = 1,
n − m odd
S3,5 = 1,
n − m even
(17.4.32)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where
768
Chapter 17.
Two Point Boundary Value Problems
At x = 1 we have y3,M − c2 y1,M 2(m + 1) −γ
E1,M +1 = y2,M −
(17.4.33)
E2,M +1 = y1,M
(17.4.34)
S1,4 = − S2,4 = 1,
y3,M − c2 , 2(m + 1)
S1,5 = 1,
S1,6 = −
S2,5 = 0,
S2,6 = 0
y1,M 2(m + 1)
(17.4.35) (17.4.36)
Here now is the sample program that implements the above algorithm. We need a main program, sfroid, that calls the routine solvde, and we must supply the subroutine difeq called by solvde. For simplicity we choose an equally spaced mesh of m = 41 points, that is, h = .025. As we shall see, this gives good accuracy for the eigenvalues up to moderate values of n − m. Since the boundary condition at x = 0 does not involve y1 if n − m is even, we have to use the indexv feature of solvde. Recall that the value of indexv(j) describes which column of s(i,j) the variable y(j) has been put in. If n − m is even, we need to interchange the columns for y1 and y2 so that there is not a zero pivot element in s(i,j). The program prompts for values of m and n. It then computes an initial guess for y based on the Legendre function Pnm . It next prompts for c2 , solves for y, prompts for c2 , solves for y using the previous values as an initial guess, and so on.
* C
*
PROGRAM sfroid INTEGER NE,M,NB,NCI,NCJ,NCK,NSI,NSJ,NYJ,NYK COMMON /sfrcom/ x,h,mm,n,c2,anorm Communicates with difeq. PARAMETER (NE=3,M=41,NB=1,NCI=NE,NCJ=NE-NB+1,NCK=M+1,NSI=NE, NSJ=2*NE+1,NYJ=NE,NYK=M) USES plgndr,solvde Sample program using solvde. Computes eigenvalues of spheroidal harmonics Smn (x; c) for m ≥ 0 and n ≥ m. In the program, m is mm, c2 is c2, and γ of equation (17.4.20) is anorm. INTEGER i,itmax,k,mm,n,indexv(NE) REAL anorm,c2,conv,deriv,fac1,fac2,h,q1,slowc, c(NCI,NCJ,NCK),s(NSI,NSJ),scalv(NE),x(M),y(NE,M),plgndr itmax=100 conv=5.e-6 slowc=1. h=1./(M-1) c2=0. write(*,*)’ENTER M,N’ read(*,*)mm,n if(mod(n+mm,2).eq.1)then No interchanges necessary. indexv(1)=1 indexv(2)=2 indexv(3)=3 else Interchange y1 and y2 . indexv(1)=2 indexv(2)=1 indexv(3)=3 endif anorm=1. Compute γ.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Thus
17.4 A Worked Example: Spheroidal Harmonics
1
* *
if(mm.NE.0)then q1=n do 11 i=1,mm anorm=-.5*anorm*(n+i)*(q1/i) q1=q1-1. enddo 11 endif do 12 k=1,M-1 Initial guess. x(k)=(k-1)*h fac1=1.-x(k)**2 fac2=fac1**(-mm/2.) y(1,k)=plgndr(n,mm,x(k))*fac2 Pnm from §6.8. deriv=-((n-mm+1)*plgndr(n+1,mm,x(k))-(n+1)* x(k)*plgndr(n,mm,x(k)))/fac1 Derivative of Pnm from a recurrence rey(2,k)=mm*x(k)*y(1,k)/fac1+deriv*fac2 lation. y(3,k)=n*(n+1)-mm*(mm+1) enddo 12 x(M)=1. Initial guess at x = 1 done separately. y(1,M)=anorm y(3,M)=n*(n+1)-mm*(mm+1) y(2,M)=(y(3,M)-c2)*y(1,M)/(2.*(mm+1.)) scalv(1)=abs(anorm) scalv(2)=max(abs(anorm),y(2,M)) scalv(3)=max(1.,y(3,M)) continue write (*,*) ’ENTER C**2 OR 999 TO END’ read (*,*) c2 if (c2.eq.999.) stop call solvde(itmax,conv,slowc,scalv,indexv,NE,NB,M,y,NYJ,NYK, c,NCI,NCJ,NCK,s,NSI,NSJ) write (*,*) ’ M = ’,mm,’ N = ’,n, ’ C**2 = ’,c2,’ LAMBDA = ’,y(3,1)+mm*(mm+1) goto 1 for another value of c2 . END
SUBROUTINE difeq(k,k1,k2,jsf,is1,isf,indexv,ne,s,nsi,nsj,y,nyj,nyk) INTEGER is1,isf,jsf,k,k1,k2,ne,nsi,nsj,nyj,nyk,indexv(nyj),M REAL s(nsi,nsj),y(nyj,nyk) COMMON /sfrcom/ x,h,mm,n,c2,anorm PARAMETER (M=41) Returns matrix s(i,j) for solvde. INTEGER mm,n REAL anorm,c2,h,temp,temp2,x(M) if(k.eq.k1) then Boundary condition at first point. if(mod(n+mm,2).eq.1)then s(3,3+indexv(1))=1. Equation (17.4.32). s(3,3+indexv(2))=0. s(3,3+indexv(3))=0. s(3,jsf)=y(1,1) Equation (17.4.31). else s(3,3+indexv(1))=0. Equation (17.4.32). s(3,3+indexv(2))=1. s(3,3+indexv(3))=0. s(3,jsf)=y(2,1) Equation (17.4.31). endif else if(k.gt.k2) then Boundary conditions at last point. s(1,3+indexv(1))=-(y(3,M)-c2)/(2.*(mm+1.)) Equation (17.4.35). s(1,3+indexv(2))=1. s(1,3+indexv(3))=-y(1,M)/(2.*(mm+1.)) s(1,jsf)=y(2,M)-(y(3,M)-c2)*y(1,M)/(2.*(mm+1.)) Equation (17.4.33). s(2,3+indexv(1))=1. Equation (17.4.36). s(2,3+indexv(2))=0.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
769
770
Two Point Boundary Value Problems
s(2,3+indexv(3))=0. s(2,jsf)=y(1,M)-anorm Equation (17.4.34). else Interior point. s(1,indexv(1))=-1. Equation (17.4.28). s(1,indexv(2))=-.5*h s(1,indexv(3))=0. s(1,3+indexv(1))=1. s(1,3+indexv(2))=-.5*h s(1,3+indexv(3))=0. temp=h/(1.-(x(k)+x(k-1))**2*.25) temp2=.5*(y(3,k)+y(3,k-1))-c2*.25*(x(k)+x(k-1))**2 s(2,indexv(1))=temp*temp2*.5 s(2,indexv(2))=-1.-.5*temp*(mm+1.)*(x(k)+x(k-1)) s(2,indexv(3))=.25*temp*(y(1,k)+y(1,k-1)) s(2,3+indexv(1))=s(2,indexv(1)) s(2,3+indexv(2))=2.+s(2,indexv(2)) s(2,3+indexv(3))=s(2,indexv(3)) s(3,indexv(1))=0. s(3,indexv(2))=0. s(3,indexv(3))=-1. s(3,3+indexv(1))=0. s(3,3+indexv(2))=0. s(3,3+indexv(3))=1. s(1,jsf)=y(1,k)-y(1,k-1)-.5*h*(y(2,k)+y(2,k-1)) s(2,jsf)=y(2,k)-y(2,k-1)-temp*((x(k)+x(k-1)) *.5*(mm+1.)*(y(2,k)+y(2,k-1))-temp2* .5*(y(1,k)+y(1,k-1))) s(3,jsf)=y(3,k)-y(3,k-1) endif return END
Equation (17.4.29).
Equation (17.4.30).
Equation (17.4.23). Equation (17.4.24). Equation (17.4.27).
You can run the program and check it against values of λmn (c) given in the tables at the back of Flammer’s book [1] or in Table 21.1 of Abramowitz and Stegun [2]. Typically it converges in about 3 iterations. The table below gives a few comparisons.
m
Selected Output of sfroid n c2 λexact λsfroid
2
2
2
5
4
11
0.1 1.0 4.0 1.0 16.0 −1.0
6.01427 6.14095 6.54250 30.4361 36.9963 131.560
6.01427 6.14095 6.54253 30.4372 37.0135 131.554
Shooting To solve the same problem via shooting (§17.1), we supply a subroutine derivs that implements equations (17.4.15)–(17.4.17). We will integrate the equations over the range −1 ≤ x ≤ 0. We provide the subroutine load which sets the eigenvalue y3 to its current best estimate, v(1). It also sets the boundary values of y1 and
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
* *
Chapter 17.
17.4 A Worked Example: Spheroidal Harmonics
771
y2 using equations (17.4.20) and (17.4.19) (with a minus sign corresponding to x = −1). Note that the boundary condition is actually applied a distance dx from the boundary to avoid having to evaluate y20 right on the boundary. The subroutine score follows from equation (17.4.18).
1
SUBROUTINE load(x1,v,y) INTEGER m,n REAL c2,dx,gamma,x1,y1,v(1),y(3) COMMON /sphcom/ c2,gamma,dx,m,n Supplies starting values for integration at x = −1 + dx. y(3)=v(1) if(mod(n-m,2).eq.0)then y1=gamma else y1=-gamma endif y(2)=-(y(3)-c2)*y1/(2*(m+1)) y(1)=y1+y(2)*dx return END SUBROUTINE score(x2,y,f) INTEGER m,n REAL c2,dx,gamma,x2,f(1),y(3) COMMON /sphcom/ c2,gamma,dx,m,n Tests whether boundary condition at x = 0 is satisfied. if (mod(n-m,2).eq.0) then f(1)=y(2)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C
PROGRAM sphoot Sample program using shoot. Computes eigenvalues of spheroidal harmonics Smn (x; c) for m ≥ 0 and n ≥ m. Be sure that routine funcv for newt is provided by shoot (§17.1). INTEGER i,m,n,nvar,N2 PARAMETER (N2=1) REAL c2,dx,gamma,q1,x1,x2,v(N2) LOGICAL check COMMON /sphcom/ c2,gamma,dx,m,n Communicates with load, score, and derivs. COMMON /caller/ x1,x2,nvar Communicates with shoot. USES newt dx=1.e-4 Avoid evaluating derivatives exactly at x = −1. nvar=3 Number of equations. write(*,*) ’input m,n,c-squared (999 to end)’ read(*,*) m,n,c2 if (c2.eq.999.) stop if ((n.lt.m).or.(m.lt.0)) goto 1 gamma=1.0 Compute γ of equation (17.4.20). q1=n do 11 i=1,m gamma=-0.5*gamma*(n+i)*(q1/i) q1=q1-1.0 enddo 11 v(1)=n*(n+1)-m*(m+1)+c2/2.0 Initial guess for eigenvalue. x1=-1.0+dx Set range of integration. x2=0.0 call newt(v,N2,check) Find v that zeros function f in score. if(check)then write(*,*)’shoot failed; bad initial guess’ else write(*,’(1x,t6,a)’) ’mu(m,n)’ write(*,’(1x,f12.6)’) v(1) goto 1 endif END
772
Chapter 17.
Two Point Boundary Value Problems
else f(1)=y(1) endif return END
Shooting to a Fitting Point For variety we illustrate shootf from §17.2 by integrating over the whole range −1 + dx ≤ x ≤ 1 − dx, with the fitting point chosen to be at x = 0. The routine derivs is identical to the one for shoot. Now, however, there are two load routines. The routine load1 for x = −1 is essentially identical to load above. At x = 1, load2 sets the function value y1 and the eigenvalue y3 to their best current estimates, v2(1) and v2(2), respectively. If you quite sensibly make your initial guess of the eigenvalue the same in the two intervals, then v1(1) will stay equal to v2(2) during the iteration. The subroutine score simply checks whether all three function values match at the fitting point.
C
1
PROGRAM sphfpt Sample program using shootf. Computes eigenvalues of spheroidal harmonics Smn (x; c) for m ≥ 0 and n ≥ m. Be sure that routine funcv for newt is provided by shootf (§17.2). The routine derivs is the same as for sphoot. INTEGER i,m,n,nvar,nn2,N1,N2,NTOT REAL DXX PARAMETER (N1=2,N2=1,NTOT=N1+N2,DXX=1.e-4) REAL c2,dx,gamma,q1,x1,x2,xf,v1(N2),v2(N1),v(NTOT) LOGICAL check COMMON /sphcom/ c2,gamma,dx,m,n Communicates with load1, load2, score, and derivs. COMMON /caller/ x1,x2,xf,nvar,nn2 Communicates with shootf. EQUIVALENCE (v1(1),v(1)),(v2(1),v(N2+1)) USES newt nvar=NTOT Number of equations. nn2=N2 dx=DXX Avoid evaluating derivatives exactly at x = ±1. write(*,*) ’input m,n,c-squared (999 to end)’ read(*,*) m,n,c2 if (c2.eq.999.) stop if ((n.lt.m).or.(m.lt.0)) goto 1 gamma=1.0 Compute γ of equation (17.4.20). q1=n do 11 i=1,m gamma=-0.5*gamma*(n+i)*(q1/i) q1=q1-1.0 enddo 11 v1(1)=n*(n+1)-m*(m+1)+c2/2.0 Initial guess for eigenvalue and function value. v2(2)=v1(1)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE derivs(x,y,dydx) INTEGER m,n REAL c2,dx,gamma,x,dydx(3),y(3) COMMON /sphcom/ c2,gamma,dx,m,n Evaluates derivatives for odeint. dydx(1)=y(2) dydx(2)=(2.0*x*(m+1.0)*y(2)-(y(3)-c2*x*x)*y(1))/(1.0-x*x) dydx(3)=0.0 return END
17.4 A Worked Example: Spheroidal Harmonics
773
SUBROUTINE load1(x1,v1,y) INTEGER m,n REAL c2,dx,gamma,x1,y1,v1(1),y(3) COMMON /sphcom/ c2,gamma,dx,m,n Supplies starting values for integration at x = −1 + dx. y(3)=v1(1) if(mod(n-m,2).eq.0)then y1=gamma else y1=-gamma endif y(2)=-(y(3)-c2)*y1/(2*(m+1)) y(1)=y1+y(2)*dx return END SUBROUTINE load2(x2,v2,y) INTEGER m,n REAL c2,dx,gamma,x2,v2(2),y(3) COMMON /sphcom/ c2,gamma,dx,m,n Supplies starting values for integration at x = 1 − dx. y(3)=v2(2) y(1)=v2(1) y(2)=(y(3)-c2)*y(1)/(2*(m+1)) return END SUBROUTINE score(xf,y,f) INTEGER i,m,n REAL c2,gamma,dx,xf,f(3),y(3) COMMON /sphcom/ c2,gamma,dx,m,n Tests whether solutions match at fitting point x = 0. do 12 i=1,3 f(i)=y(i) enddo 12 return END
CITED REFERENCES AND FURTHER READING: Flammer, C. 1957, Spheroidal Wave Functions (Stanford, CA: Stanford University Press). [1] Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York), §21. [2] Morse, P.M., and Feshbach, H. 1953, Methods of Theoretical Physics, Part II (New York: McGrawHill), pp. 1502ff. [3]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
v2(1)=gamma*(1.-(v2(2)-c2)*dx/(2*(m+1))) x1=-1.0+dx Set range of integration. x2=1.0-dx xf=0. Fitting point. call newt(v,NTOT,check) Find v that zeros function f in score. if(check)then write(*,*)’shootf failed; bad initial guess’ else write(*,’(1x,t6,a)’) ’mu(m,n)’ write(*,’(1x,f12.6)’) v1(1) goto 1 endif END
774
Chapter 17.
Two Point Boundary Value Problems
17.5 Automated Allocation of Mesh Points
dy =g dx
(17.5.1)
becomes dy dx =g dq dq In terms of q, equation (17.5.2) as an FDE might be written " ! ! dx dx 1 yk − yk−1 − 2 g + g dq dq k
(17.5.2) # =0
(17.5.3)
k−1
or some related version. Note that dx/dq should accompany g. The transformation between x and q depends only on the Jacobian dx/dq. Its reciprocal dq/dx is proportional to the density of mesh points. Now, given the function y(x), or its approximation at the current stage of relaxation, we are supposed to have some idea of how we want to specify the density of mesh points. For example, we might want dq/dx to be larger where y is changing rapidly, or near to the boundaries, or both. In fact, we can probably make up a formula for what we would like dq/dx to be proportional to. The problem is that we do not know the proportionality constant. That is, the formula that we might invent would not have the correct integral over the whole range of x so as to make q vary from 1 to M , according to its definition. To solve this problem we introduce a second reparametrization Q(q), where Q is a new independent variable. The relation between Q and q is taken to be linear, so that a mesh spacing formula for dQ/dx differs only in its unknown proportionality constant. A linear relation implies d2 Q =0 dq2 or, expressed in the usual manner as coupled first-order equations,
(17.5.4)
dQ(x) dψ =ψ =0 (17.5.5) dq dq where ψ is a new intermediate variable. We add these two equations to the set of ODEs being solved. Completing the prescription, we add a third ODE that is just our desired mesh-density function, namely φ(x) =
dQ dQ dq = dx dq dx
(17.5.6)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
In relaxation problems, you have to choose values for the independent variable at the mesh points. This is called allocating the grid or mesh. The usual procedure is to pick a plausible set of values and, if it works, to be content. If it doesn’t work, increasing the number of points usually cures the problem. If we know ahead of time where our solutions will be rapidly varying, we can put more grid points there and less elsewhere. Alternatively, we can solve the problem first on a uniform mesh and then examine the solution to see where we should add more points. We then repeat the solution with the improved grid. The object of the exercise is to allocate points in such a way as to represent the solution accurately. It is also possible to automate the allocation of mesh points, so that it is done “dynamically” during the relaxation process. This powerful technique not only improves the accuracy of the relaxation method, but also (as we will see in the next section) allows internal singularities to be handled in quite a neat way. Here we learn how to accomplish the automatic allocation. We want to focus attention on the independent variable x, and consider two alternative reparametrizations of it. The first, we term q; this is just the coordinate corresponding to the mesh points themselves, so that q = 1 at k = 1, q = 2 at k = 2, and so on. Between any two mesh points we have ∆q = 1. In the change of independent variable in the ODEs from x to q,
775
17.6 Handling Internal Boundary Conditions or Singular Points
where φ(x) is chosen by us. Written in terms of the mesh variable q, this equation is dx ψ = dq φ(x)
(17.5.7)
dx |d ln y| + ∆ δ dy/dx dQ 1 φ(x) = = + dx ∆ yδ dQ =
or
(17.5.8) (17.5.9)
where ∆ and δ are constants that we choose. The first term would give a uniform spacing in x if it alone were present. The second term forces more grid points to be used where y is changing rapidly. The constants act to make every logarithmic change in y of an amount δ about as “attractive” to a grid point as a change in x of amount ∆. You adjust the constants according to taste. Other strategies are possible, such as a logarithmic spacing in x, replacing dx in the first term with d ln x. CITED REFERENCES AND FURTHER READING: Eggleton, P. P. 1971, Monthly Notices of the Royal Astronomical Society, vol. 151, pp. 351–364. Kippenhan, R., Weigert, A., and Hofmeister, E. 1968, in Methods in Computational Physics, vol. 7 (New York: Academic Press), pp. 129ff.
17.6 Handling Internal Boundary Conditions or Singular Points Singularities can occur in the interiors of two point boundary value problems. Typically, there is a point xs at which a derivative must be evaluated by an expression of the form S(xs ) =
N (xs , y) D(xs , y)
(17.6.1)
where the denominator D(xs , y) = 0. In physical problems with finite answers, singular points usually come with their own cure: Where D → 0, there the physical solution y must be such as to make N → 0 simultaneously, in such a way that the ratio takes on a meaningful value. This constraint on the solution y is often called a regularity condition. The condition that D(xs , y) satisfy some special constraint at xs is entirely analogous to an extra boundary condition, an algebraic relation among the dependent variables that must hold at a point. We discussed a related situation earlier, in §17.2, when we described the “fitting point method” to handle the task of integrating equations with singular behavior at the boundaries. In those problems you are unable to integrate from one side of the domain to the other.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Notice that φ(x) should be chosen to be positive definite, so that the density of mesh points is everywhere positive. Otherwise (17.5.7) can have a zero in its denominator. To use automated mesh spacing, you add the three ODEs (17.5.5) and (17.5.7) to your set of equations, i.e., to the array y(j,k). Now x becomes a dependent variable! Q and ψ also become new dependent variables. Normally, evaluating φ requires little extra work since it will be composed from pieces of the g’s that exist anyway. The automated procedure allows one to investigate quickly how the numerical results might be affected by various strategies for mesh spacing. (A special case occurs if the desired mesh spacing function Q can be found analytically, i.e., dQ/dx is directly integrable. Then, you need to add only two equations, those in 17.5.5, and two new variables x, ψ.) As an example of a typical strategy for implementing this scheme, consider a system with one dependent variable y(x). We could set
X X X X X X X X X X X
X X X 1 X 1 X 1
X X X X X X X X X X X
X X X X X X 1
X X X X X X X X X X X
X X X X X X 1
X X X X X
X X X X X
V V V V V V V V V V V V V
B B B B B B B B B B B B B
lo ck
X X X X X X X X X X X
B B B B B B B B B B B B B
X X X X X
X X X X X
X X X X X
X X X X X
ck
X X X 1 X X X 1 1 1
X X X X X X X X X X X
V V V V V V V V V V V V V
ec ia lb
X X X X X X X
lo
1
X X X X X X X
lb
(b)
X X X X X X X
X X 1 X 1 X 1 X
Figure 17.6.1. FDE matrix structure with an internal boundary condition. The internal condition introduces a special block. (a) Original form, compare with Figure 17.3.1; (b) final form, compare with Figure 17.3.2.
However, the ODEs do have well-behaved derivatives and solutions in the neighborhood of the singularity, so it is readily possible to integrate away from the point. Both the relaxation method and the method of “shooting” to a fitting point handle such problems easily. Also, in those problems the presence of singular behavior served to isolate some special boundary values that had to be satisfied to solve the equations. The difference here is that we are concerned with singularities arising at intermediate points, where the location of the singular point depends on the solution, so is not known a priori. Consequently, we face a circular task: The singularity prevents us from finding a numerical solution, but we need a numerical solution to find its location. Such singularities are also associated with selecting a special value for some variable which allows the solution to satisfy the regularity condition at the singular point. Thus, internal singularities take on aspects of being internal boundary conditions. One way of handling internal singularities is to treat the problem as a free boundary problem, as discussed at the end of §17.0. Suppose, as a simple example, we consider the equation dy N (x, y) = dx D(x, y)
(17.6.2)
where N and D are required to pass through zero at some unknown point xs . We add the equation z ≡ xs − x1
dz =0 dx
(17.6.3)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
X X X X X
1 X X X X X
sp
1
ia
(a)
Two Point Boundary Value Problems
ec
Chapter 17.
sp
776
17.6 Handling Internal Boundary Conditions or Singular Points
777
where xs is the unknown location of the singularity, and change the independent variable to t by setting x − x1 = tz,
0≤t≤1
(17.6.4)
D(x, y) = 0
(17.6.5)
The boundary conditions at t = 1 become N (x, y) = 0,
dQ =ψ dq dψ =0 dq
(17.6.6) (17.6.7)
with a simple mesh spacing function that maps x uniformly into q, where q runs from 1 to M , the number of mesh points: dQ =1 (17.6.8) dx Having added three first-order differential equations, we must also add their corresponding boundary conditions. If there were no singularity, these could simply be Q(x) = x − x1 ,
at at
q=1: q=M :
x = x1 , x = x2
Q=0
(17.6.9) (17.6.10)
and a total of N values yi specified at q = 1. In this case the problem is essentially an initial value problem with all boundary conditions specified at x1 and the mesh spacing function is superfluous. However, in the actual case at hand we impose the conditions at q = 1 : at q = M :
x = x1 , Q = 0 N (x, y) = 0, D(x, y) = 0
(17.6.11) (17.6.12)
and N − 1 values yi at q = 1. The “missing” yi is to be adjusted, in other words, so as to make the solution go through the singular point in a regular (zero-over-zero) rather than irregular (finite-over-zero) manner. Notice also that these boundary conditions do not directly impose a value for x2 , which becomes an adjustable parameter that the code varies in an attempt to match the regularity condition. In this example the singularity occurred at a boundary, and the complication arose because the location of the boundary was unknown. In other problems we might wish to continue the integration beyond the internal singularity. For the example given above, we could simply integrate the ODEs to the singular point, then as a separate problem recommence the integration from the singular point on as far we care to go. However, in other cases the singularity occurs internally, but does not completely determine the problem: There are still some more boundary conditions to be satisfied further along in the mesh. Such cases present no difficulty in principle, but do require some adaptation of the relaxation code given in §17.3. In effect all you need to do is to add a “special” block of equations at the mesh point where the internal boundary conditions occur, and do the proper bookkeeping. Figure 17.6.1 illustrates a concrete example where the overall problem contains 5 equations with 2 boundary conditions at the first point, one “internal” boundary condition, and two final boundary conditions. The figure shows the structure of the overall matrix equations along the diagonal in the vicinity of the special block. In the middle of the domain, blocks typically involve 5 equations (rows) in 10 unknowns (columns). For each block prior to the special block, the initial boundary conditions provided enough information to zero the first two columns of the blocks. The five FDEs eliminate five more columns, and the final three columns need to be stored for the backsubstitution step (as described in §17.3). To handle the extra condition we break the normal cycle and add a special block with only one
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Use of an adaptive mesh as discussed in the previous section is another way to overcome the difficulties of an internal singularity. For the problem (17.6.2), we add the mesh spacing equations
Two Point Boundary Value Problems Chapter 17.
778
equation: the internal boundary condition. This effectively reduces the required storage of unreduced coefficients by one column for the rest of the grid, and allows us to reduce to zero the first three columns of subsequent blocks. The subroutines red, pinvs, bksub can readily handle these cases with minor recoding, but each problem makes for a special case, and you will have to make the modifications as required.
London, R.A., and Flannery, B.P. 1982, Astrophysical Journal, vol. 258, pp. 260–269.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
CITED REFERENCES AND FURTHER READING:
18.0 Introduction Many people, otherwise numerically knowledgable, imagine that the numerical solution of integral equations must be an extremely arcane topic, since, until recently, it was almost never treated in numerical analysis textbooks. Actually there is a large and growing literature on the numerical solution of integral equations; several monographs have by now appeared [1-3]. One reason for the sheer volume of this activity is that there are many different kinds of equations, each with many different possible pitfalls; often many different algorithms have been proposed to deal with a single case. There is a close correspondence between linear integral equations, which specify linear, integral relations among functions in an infinite-dimensional function space, and plain old linear equations, which specify analogous relations among vectors in a finite-dimensional vector space. Because this correspondence lies at the heart of most computational algorithms, it is worth making it explicit as we recall how integral equations are classified. Fredholm equations involve definite integrals with fixed upper and lower limits. An inhomogeneous Fredholm equation of the first kind has the form Z b g(t) = K(t, s)f(s) ds (18.0.1) a
Here f(t) is the unknown function to be solved for, while g(t) is a known “right-hand side.” (In integral equations, for some odd reason, the familiar “right-hand side” is conventionally written on the left!) The function of two variables, K(t, s) is called the kernel. Equation (18.0.1) is analogous to the matrix equation K·f=g
(18.0.2)
whose solution is f = K−1 · g, where K−1 is the matrix inverse. Like equation (18.0.2), equation (18.0.1) has a unique solution whenever g is nonzero (the homogeneous case with g = 0 is almost never useful) and K is invertible. However, as we shall see, this latter condition is as often the exception as the rule. The analog of the finite-dimensional eigenvalue problem (K − σ1) · f = g 779
(18.0.3)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Chapter 18. Integral Equations and Inverse Theory
780
Chapter 18.
Integral Equations and Inverse Theory
is called a Fredholm equation of the second kind, usually written Z
b
K(t, s)f(s) ds + g(t)
f(t) = λ
(18.0.4)
a
Z
b
[K(t, s) − σδ(t − s)]f(s) ds = −σg(t)
(18.0.5)
a
where δ(t − s) is a Dirac delta function (and where we have changed from λ to its reciprocal σ for clarity). If σ is large enough in magnitude, then equation (18.0.5) is, in effect, diagonally dominant and thus well-conditioned. Only if σ is small do we go back to the ill-conditioned case. Homogeneous Fredholm equations of the second kind are likewise not particularly ill-posed. If K is a smoothing operator, then it will map many f’s to zero, or near-zero; there will thus be a large number of degenerate or nearly degenerate eigenvalues around σ = 0 (λ → ∞), but this will cause no particular computational difficulties. In fact, we can now see that the magnitude of σ needed to rescue the inhomogeneous equation (18.0.5) from an ill-conditioned fate is generally much less than that required for diagonal dominance. Since the σ term shifts all eigenvalues, it is enough that it be large enough to shift a smoothing operator’s forest of nearzero eigenvalues away from zero, so that the resulting operator becomes invertible (except, of course, at the discrete eigenvalues). Volterra equations are a special case of Fredholm equations with K(t, s) = 0 for s > t. Chopping off the unnecessary part of the integration, Volterra equations are written in a form where the upper limit of integration is the independent variable t.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Again, the notational conventions do not exactly correspond: λ in equation (18.0.4) is 1/σ in (18.0.3), while g is −g/λ. If g (or g) is zero, then the equation is said to be homogeneous. If the kernel K(t, s) is bounded, then, like equation (18.0.3), equation (18.0.4) has the property that its homogeneous form has solutions for at most a denumerably infinite set λ = λn , n = 1, 2, . . . , the eigenvalues. The corresponding solutions fn (t) are the eigenfunctions. The eigenvalues are real if the kernel is symmetric. In the inhomogeneous case of nonzero g (or g), equations (18.0.3) and (18.0.4) are soluble except when λ (or σ) is an eigenvalue — because the integral operator (or matrix) is singular then. In integral equations this dichotomy is called the Fredholm alternative. Fredholm equations of the first kind are often extremely ill-conditioned. Applying the kernel to a function is generally a smoothing operation, so the solution, which requires inverting the operator, will be extremely sensitive to small changes or errors in the input. Smoothing often actually loses information, and there is no way to get it back in an inverse operation. Specialized methods have been developed for such equations, which are often called inverse problems. In general, a method must augment the information given with some prior knowledge of the nature of the solution. This prior knowledge is then used, in one way or another, to restore lost information. We will introduce such techniques in §18.4. Inhomogeneous Fredholm equations of the second kind are much less often ill-conditioned. Equation (18.0.4) can be rewritten as
18.0 Introduction
781
The Volterra equation of the first kind Z
t
K(t, s)f(s) ds
g(t) =
(18.0.6)
a
has as its analog the matrix equation (now written out in components) Kkj fj = gk
(18.0.7)
j=1
Comparing with equation (18.0.2), we see that the Volterra equation corresponds to a matrix K that is lower (i.e., left) triangular, with zero entries above the diagonal. As we know from Chapter 2, such matrix equations are trivially soluble by forward substitution. Techniques for solving Volterra equations are similarly straightforward. When experimental measurement noise does not dominate, Volterra equations of the first kind tend not to be ill-conditioned; the upper limit to the integral introduces a sharp step that conveniently spoils any smoothing properties of the kernel. The Volterra equation of the second kind is written Z
t
f(t) =
K(t, s)f(s) ds + g(t)
(18.0.8)
a
whose matrix analog is the equation (K − 1) · f = g
(18.0.9)
with K lower triangular. The reason there is no λ in these equations is that (i) in the inhomogeneous case (nonzero g) it can be absorbed into K, while (ii) in the homogeneous case (g = 0), it is a theorem that Volterra equations of the second kind with bounded kernels have no eigenvalues with square-integrable eigenfunctions. We have specialized our definitions to the case of linear integral equations. The integrand in a nonlinear version of equation (18.0.1) or (18.0.6) would be K(t, s, f(s)) instead of K(t, s)f(s); a nonlinear version of equation (18.0.4) or (18.0.8) would have an integrand K(t, s, f(t), f(s)). Nonlinear Fredholm equations are considerably more complicated than their linear counterparts. Fortunately, they do not occur as frequently in practice and we shall by and large ignore them in this chapter. By contrast, solving nonlinear Volterra equations usually involves only a slight modification of the algorithm for linear equations, as we shall see. Almost all methods for solving integral equations numerically make use of quadrature rules, frequently Gaussian quadratures. This would be a good time for you to go back and review §4.5, especially the advanced material towards the end of that section. In the sections that follow, we first discuss Fredholm equations of the second kind with smooth kernels (§18.1). Nontrivial quadrature rules come into the discussion, but we will be dealing with well-conditioned systems of equations. We then return to Volterra equations (§18.2), and find that simple and straightforward methods are generally satisfactory for these equations. In §18.3 we discuss how to proceed in the case of singular kernels, focusing largely on Fredholm equations (both first and second kinds). Singularities require
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
k X
782
Chapter 18.
Integral Equations and Inverse Theory
CITED REFERENCES AND FURTHER READING: Delves, L.M., and Mohamed, J.L. 1985, Computational Methods for Integral Equations (Cambridge, U.K.: Cambridge University Press). [1] Linz, P. 1985, Analytical and Numerical Methods for Volterra Equations (Philadelphia: S.I.A.M.). [2] Atkinson, K.E. 1976, A Survey of Numerical Methods for the Solution of Fredholm Integral Equations of the Second Kind (Philadelphia: S.I.A.M.). [3] Brunner, H. 1988, in Numerical Analysis 1987, Pitman Research Notes in Mathematics vol. 170, D.F. Griffiths and G.A. Watson, eds. (Harlow, Essex, U.K.: Longman Scientific and Technical), pp. 18–38. [4] Smithies, F. 1958, Integral Equations (Cambridge, U.K.: Cambridge University Press). Kanwal, R.P. 1971, Linear Integral Equations (New York: Academic Press). Green, C.D. 1969, Integral Equation Methods (New York: Barnes & Noble).
18.1 Fredholm Equations of the Second Kind We desire a numerical solution for f(t) in the equation Z
b
K(t, s)f(s) ds + g(t)
f(t) = λ
(18.1.1)
a
The method we describe, a very basic one, is called the Nystrom method. It requires the choice of some approximate quadrature rule: Z
b
y(s) ds = a
N X
wj y(sj )
(18.1.2)
j=1
Here the set {wj } are the weights of the quadrature rule, while the N points {sj } are the abscissas. What quadrature rule should we use? It is certainly possible to solve integral equations with low-order quadrature rules like the repeated trapezoidal or Simpson’s
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
special quadrature rules, but they are also sometimes blessings in disguise, since they can spoil a kernel’s smoothing and make problems well-conditioned. In §§18.4–18.7 we face up to the issues of inverse problems. §18.4 is an introduction to this large subject. We should note here that wavelet transforms, already discussed in §13.10, are applicable not only to data compression and signal processing, but can also be used to transform some classes of integral equations into sparse linear problems that allow fast solution. You may wish to review §13.10 as part of reading this chapter. Some subjects, such as integro-differential equations, we must simply declare to be beyond our scope. For a review of methods for integro-differential equations, see Brunner [4]. It should go without saying that this one short chapter can only barely touch on a few of the most basic methods involved in this complicated subject.
18.1 Fredholm Equations of the Second Kind
783
f(t) = λ
N X
wj K(t, sj )f(sj ) + g(t)
(18.1.3)
j=1
Evaluate equation (18.1.3) at the quadrature points:
f(ti ) = λ
N X
wj K(ti , sj )f(sj ) + g(ti )
(18.1.4)
j=1
Let fi be the vector f(ti ), gi the vector g(ti ), Kij the matrix K(ti , sj ), and define e ij = Kij wj K
(18.1.5)
Then in matrix notation equation (18.1.4) becomes e ·f=g (1 − λK)
(18.1.6)
This is a set of N linear algebraic equations in N unknowns that can be solved by standard triangular decomposition techniques (§2.3) — that is where the O(N 3 ) operations count comes in. The solution is usually well-conditioned, unless λ is very close to an eigenvalue. Having obtained the solution at the quadrature points {ti }, how do you get the solution at some other point t? You do not simply use polynomial interpolation. This destroys all the accuracy you have worked so hard to achieve. Nystrom’s key observation was that you should use equation (18.1.3) as an interpolatory formula, maintaining the accuracy of the solution. We here give two subroutines for use with linear Fredholm equations of the second kind. The routine fred2 sets up equation (18.1.6) and then solves it by LU decomposition with calls to the routines ludcmp and lubksb. The Gauss-Legendre quadrature is implemented by first getting the weights and abscissas with a call to gauleg. Routine fred2 requires that you provide an external function that returns g(t) and another that returns λKij . It then returns the solution f at the quadrature points. It also returns the quadrature points and weights. These are used by the second routine fredin to carry out the Nystrom interpolation of equation (18.1.3) and return the value of f at any point in the interval [a, b].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
rules. We will see, however, that the solution method involves O(N 3 ) operations, and so the most efficient methods tend to use high-order quadrature rules to keep N as small as possible. For smooth, nonsingular problems, nothing beats Gaussian quadrature (e.g., Gauss-Legendre quadrature, §4.5). (For non-smooth or singular kernels, see §18.3.) Delves and Mohamed [1] investigated methods more complicated than the Nystrom method. For straightforward Fredholm equations of the second kind, they concluded “. . . the clear winner of this contest has been the Nystrom routine . . . with the N -point Gauss-Legendre rule. This routine is extremely simple. . . . Such results are enough to make a numerical analyst weep.” If we apply the quadrature rule (18.1.2) to equation (18.1.1), we get
784
C
Integral Equations and Inverse Theory
SUBROUTINE fred2(n,a,b,t,f,w,g,ak) INTEGER n,NMAX REAL a,b,f(n),t(n),w(n),g,ak EXTERNAL ak,g PARAMETER (NMAX=200) USES ak,g,gauleg,lubksb,ludcmp Solves a linear Fredholm equation of the second kind. On input, a and b are the limits of integration, and n is the number of points to use in the Gaussian quadrature. g and ak are user-supplied external functions that respectively return g(t) and λK(t, s). The routine returns arrays t(1:n) and f(1:n) containing the abscissas ti of the Gaussian quadrature and the solution f at these abscissas. Also returned is the array w(1:n) of Gaussian weights for use with the Nystrom interpolation routine fredin. INTEGER i,j,indx(NMAX) REAL d,omk(NMAX,NMAX) if(n.gt.NMAX) pause ’increase NMAX in fred2’ call gauleg(a,b,t,w,n) Replace gauleg with another routine if not using do 12 i=1,n Gauss-Legendre quadrature. e do 11 j=1,n Form 1 − λK. if(i.eq.j)then omk(i,j)=1. else omk(i,j)=0. endif omk(i,j)=omk(i,j)-ak(t(i),t(j))*w(j) enddo 11 f(i)=g(t(i)) enddo 12 call ludcmp(omk,n,NMAX,indx,d) Solve linear equations. call lubksb(omk,n,NMAX,indx,f) return END
FUNCTION fredin(x,n,a,b,t,f,w,g,ak) INTEGER n REAL fredin,a,b,x,f(n),t(n),w(n),g,ak EXTERNAL ak,g USES ak,g Given arrays t(1:n) and w(1:n) containing the abscissas and weights of the Gaussian quadrature, and given the solution array f(1:n) from fred2, this function returns the value of f at x using the Nystrom interpolation formula. On input, a and b are the limits of integration, and n is the number of points used in the Gaussian quadrature. g and ak are user-supplied external functions that respectively return g(t) and λK(t, s). INTEGER i REAL sum sum=0. do 11 i=1,n sum=sum+ak(x,t(i))*w(i)*f(i) enddo 11 fredin=g(x)+sum return END
One disadvantage of a method based on Gaussian quadrature is that there is no simple way to obtain an estimate of the error in the result. The best practical method is to increase N by 50%, say, and treat the difference between the two estimates as a conservative estimate of the error in the result obtained with the larger value of N .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C
Chapter 18.
18.1 Fredholm Equations of the Second Kind
785
Turn now to solutions of the homogeneous equation. If we set λ = 1/σ and g = 0, then equation (18.1.6) becomes a standard eigenvalue equation e · f = σf K
(18.1.7)
K · D · f = σf Multiplying by D1/2 , we get
D1/2 · K · D1/2 · h = σh
(18.1.8)
where h = D1/2 · f. Equation (18.1.8) is now in the form of a symmetric eigenvalue problem. Solution of equations (18.1.7) or (18.1.8) will in general give N eigenvalues, where N is the number of quadrature points used. For square-integrable kernels, these will provide good approximations to the lowest N eigenvalues of the integral equation. Kernels of finite rank (also called degenerate or separable kernels) have only a finite number of nonzero eigenvalues (possibly none). You can diagnose this situation by a cluster of eigenvalues σ that are zero to machine precision. The number of nonzero eigenvalues will stay constant as you increase N to improve their accuracy. Some care is required here: A nondegenerate kernel can have an infinite number of eigenvalues that have an accumulation point at σ = 0. You distinguish the two cases by the behavior of the solution as you increase N . If you suspect a degenerate kernel, you will usually be able to solve the problem by analytic techniques described in all the textbooks.
CITED REFERENCES AND FURTHER READING: Delves, L.M., and Mohamed, J.L. 1985, Computational Methods for Integral Equations (Cambridge, U.K.: Cambridge University Press). [1] Atkinson, K.E. 1976, A Survey of Numerical Methods for the Solution of Fredholm Integral Equations of the Second Kind (Philadelphia: S.I.A.M.).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
which we can solve with any convenient matrix eigenvalue routine (see Chapter 11). Note that if our original problem had a symmetric kernel, then the matrix K is symmetric. However, since the weights wj are not equal for most quadrature e (equation 18.1.5) is not symmetric. The matrix eigenvalue rules, the matrix K problem is much easier for symmetric matrices, and so we should restore the symmetry if possible. Provided the weights are positive (which they are for Gaussian quadrature), we can define the diagonal matrix D = diag(wj ) and its square root, √ D1/2 = diag( wj ). Then equation (18.1.7) becomes
786
Chapter 18.
Integral Equations and Inverse Theory
18.2 Volterra Equations
a
Most algorithms for Volterra equations march out from t = a, building up the solution as they go. In this sense they resemble not only forward substitution (as discussed in §18.0), but also initial-value problems for ordinary differential equations. In fact, many algorithms for ODEs have counterparts for Volterra equations. The simplest way to proceed is to solve the equation on a mesh with uniform spacing: ti = a + ih,
i = 0, 1, . . . , N,
h≡
b−a N
(18.2.2)
To do so, we must choose a quadrature rule. For a uniform mesh, the simplest scheme is the trapezoidal rule, equation (4.1.11): Z ti i−1 X K(ti , s)f(s) ds = h 12 Ki0 f0 + Kij fj + 12 Kii fi (18.2.3) a
j=1
Thus the trapezoidal method for equation (18.2.1) is: f0 = g0 (1 − 12 hKii )fi = h 12 Ki0 f0 +
i−1 X
Kij fj + gi ,
(18.2.4) i = 1, . . . , N
j=1
(For a Volterra equation of the first kind, the leading 1 on the left would be absent, and g would have opposite sign, with corresponding straightforward changes in the rest of the discussion.) Equation (18.2.4) is an explicit prescription that gives the solution in O(N 2 ) operations. Unlike Fredholm equations, it is not necessary to solve a system of linear equations. Volterra equations thus usually involve less work than the corresponding Fredholm equations which, as we have seen, do involve the inversion of, sometimes large, linear systems. The efficiency of solving Volterra equations is somewhat counterbalanced by the fact that systems of these equations occur more frequently in practice. If we interpret equation (18.2.1) as a vector equation for the vector of m functions f(t), then the kernel K(t, s) is an m × m matrix. Equation (18.2.4) must now also be understood as a vector equation. For each i, we have to solve the m × m set of linear algebraic equations by Gaussian elimination. The routine voltra below implements this algorithm. You must supply an external function that returns the kth function of the vector g(t) at the point t, and another that returns the (k, l) element of the matrix K(t, s) at (t, s). The routine voltra then returns the vector f(t) at the regularly spaced points ti .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Let us now turn to Volterra equations, of which our prototype is the Volterra equation of the second kind, Z t f(t) = K(t, s)f(s) ds + g(t) (18.2.1)
18.2 Volterra Equations
C
787
For nonlinear Volterra equations, equation (18.2.4) holds with the product Kii fi replaced by Kii (fi ), and similarly for the other two products of K’s and f’s. Thus for each i we solve a nonlinear equation for fi with a known right-hand side. Newton’s method (§9.4 or §9.6) with an initial guess of fi−1 usually works very well provided the stepsize is not too big. Higher-order methods for solving Volterra equations are, in our opinion, not as important as for Fredholm equations, since Volterra equations are relatively easy to solve. However, there is an extensive literature on the subject. Several difficulties arise. First, any method that achieves higher order by operating on several quadrature points simultaneously will need a special method to get started, when values at the first few points are not yet known. Second, stable quadrature rules can give rise to unexpected instabilities in integral equations. For example, suppose we try to replace the trapezoidal rule in
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE voltra(n,m,t0,h,t,f,g,ak) INTEGER m,n,MMAX REAL h,t0,f(m,n),t(n),g,ak EXTERNAL ak,g PARAMETER (MMAX=5) USES ak,g,lubksb,ludcmp Solves a set of m linear Volterra equations of the second kind using the extended trapezoidal rule. On input, t0 is the starting point of the integration and n-1 is the number of steps of size h to be taken. g(k,t) is a user-supplied external function that returns gk (t), while ak(k,l,t,s) is another user-supplied external function that returns the (k, l) element of the matrix K(t, s). The solution is returned in f(1:m,1:n), with the corresponding abscissas in t(1:n). INTEGER i,j,k,l,indx(MMAX) REAL d,sum,a(MMAX,MMAX),b(MMAX) t(1)=t0 do 11 k=1,m Initialize. f(k,1)=g(k,t(1)) enddo 11 do 16 i=2,n Take a step h. t(i)=t(i-1)+h do 14 k=1,m sum=g(k,t(i)) Accumulate right-hand side of linear equations in do 13 l=1,m sum. sum=sum+0.5*h*ak(k,l,t(i),t(1))*f(l,1) do 12 j=2,i-1 sum=sum+h*ak(k,l,t(i),t(j))*f(l,j) enddo 12 if(k.eq.l)then Left-hand side goes in matrix a. a(k,l)=1. else a(k,l)=0. endif a(k,l)=a(k,l)-0.5*h*ak(k,l,t(i),t(i)) enddo 13 b(k)=sum enddo 14 call ludcmp(a,m,MMAX,indx,d) Solve linear equations. call lubksb(a,m,MMAX,indx,b) do 15 k=1,m f(k,i)=b(k) enddo 15 enddo 16 return END
788
Chapter 18.
Integral Equations and Inverse Theory
fE =
4f(h/2) − f(h) 3
(18.2.5)
This procedure can be repeated as with Romberg integration. The general consensus is that the best of the higher order methods is the block-by-block method (see [1]). Another important topic is the use of variable stepsize methods, which are much more efficient if there are sharp features in K or f. Variable stepsize methods are quite a bit more complicated than their counterparts for differential equations; we refer you to the literature [1,2] for a discussion. You should also be on the lookout for singularities in the integrand. If you find them, then look to §18.3 for additional ideas. CITED REFERENCES AND FURTHER READING: Linz, P. 1985, Analytical and Numerical Methods for Volterra Equations (Philadelphia: S.I.A.M.). [1] Delves, L.M., and Mohamed, J.L. 1985, Computational Methods for Integral Equations (Cambridge, U.K.: Cambridge University Press). [2]
18.3 Integral Equations with Singular Kernels Many integral equations have singularities in either the kernel or the solution or both. A simple quadrature method will show poor convergence with N if such singularities are ignored. There is sometimes art in how singularities are best handled. We start with a few straightforward suggestions: 1. Integrable singularities can often be removed by a change of variable. For example, the singular behavior K(t, s) ∼ s1/2 or s−1/2 near s = 0 can be removed by the transformation z = s1/2 . Note that we are assuming that the singular behavior is confined to K, whereas the quadrature actually involves the product K(t, s)f (s), and it is this product that must be “fixed.” Ideally, you must deduce the singular nature of the product before you try a numerical solution, and take the appropriate action. Commonly, however, a singular kernel does not produce a singular solution f (t). (The highly singular kernel K(t, s) = δ(t − s) is simply the identity operator, for example.) 2. If K(t, s) can be factored as w(s)K(t, s), where w(s) is singular and K(t, s) is smooth, then a Gaussian quadrature based on w(s) as a weight function will work well. Even if the factorization is only approximate, the convergence is often improved dramatically. All you have to do is replace gauleg in the routine fred2 by another quadrature routine. Section 4.5 explained how to construct such quadratures; or you can find tabulated abscissas and weights in the standard references [1,2] . You must of course supply K instead of K.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
the algorithm above with Simpson’s rule. Simpson’s rule naturally integrates over an interval 2h, so we easily get the function values at the even mesh points. For the odd mesh points, we could try appending one panel of trapezoidal rule. But to which end of the integration should we append it? We could do one step of trapezoidal rule followed by all Simpson’s rule, or Simpson’s rule with one step of trapezoidal rule at the end. Surprisingly, the former scheme is unstable, while the latter is fine! A simple approach that can be used with the trapezoidal method given above is Richardson extrapolation: Compute the solution with stepsize h and h/2. Then, assuming the error scales with h2 , compute
18.3 Integral Equations with Singular Kernels
789
s=
2α −α z+1
(18.3.1)
which maps 0 < s < ∞ to 1 > z > −1 so that Gauss-Legendre integration can be used. Here α > 0 is a constant that you adjust to improve the convergence. 5. A common situation in practice is that K(t, s) is singular along the diagonal line t = s. Here the Nystrom method fails completely because the kernel gets evaluated at (ti , si ). Subtraction of the singularity is one possible cure: Z b Z b Z b K(t, s)f (s) ds = K(t, s)[f (s) − f (t)] ds + K(t, s)f (t) ds a a a (18.3.2) Z b K(t, s)[f (s) − f (t)] ds + r(t)f (t)
= a
Rb where r(t) = a K(t, s) ds is computed analytically or numerically. If the first term on the right-hand side is now regular, we can use the Nystrom method. Instead of equation (18.1.4), we get fi = λ
N X
wj Kij [fj − fi] + λri fi + gi
(18.3.3)
j=1 j6=i
Sometimes the subtraction process must be repeated before the kernel is completely regularized. See [3] for details. (And read on for a different, we think better, way to handle diagonal singularities.)
Quadrature on a Uniform Mesh with Arbitrary Weight It is possible in general to find n-point linear quadrature rules that approximate the integral of a function f (x), times an arbitrary weight function w(x), over an arbitrary range of integration (a, b), as the sum of weights times n evenly spaced values of the function f (x), say at x = kh, (k + 1)h, . . . , (k + n − 1)h. The general scheme for deriving such quadrature rules is to write down the n linear equations that must be satisfied if the quadrature rule is to be exact for the n functions f (x) = const, x, x2 , . . . , xn−1 , and then solve these for the coefficients. This can be done analytically, once and for all, if the moments of the weight function over the same range of integration, Z b 1 Wn ≡ n xn w(x)dx (18.3.4) h a
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
This method is a special case of the product Nystrom method [3,4], where one factors out a singular term p(t, s) depending on both t and s from K and constructs suitable weights for its Gaussian quadrature. The calculations in the general case are quite cumbersome, because the weights depend on the chosen {ti } as well as the form of p(t, s). We prefer to implement the product Nystrom method on a uniform grid, with a quadrature scheme that generalizes the extended Simpson’s 3/8 rule (equation 4.1.5) to arbitrary weight functions. We discuss this in the subsections below. 3. Special quadrature formulas are also useful when the kernel is not strictly singular, but is “almost” so. One example is when the kernel is concentrated near t = s on a scale much smaller than the scale on which the solution f (t) varies. In that case, a quadrature formula can be based on locally approximating f (s) by a polynomial or spline, while calculating the first few moments of the kernel K(t, s) at the tabulation points ti . In such a scheme the narrow width of the kernel becomes an asset, rather than a liability: The quadrature becomes exact as the width of the kernel goes to zero. 4. An infinite range of integration is also a form of singularity. Truncating the range at a large finite value should be used only as a last resort. If the kernel goes rapidly to zero, then a Gauss-Laguerre [w ∼ exp(−αs)] or Gauss-Hermite [w ∼ exp(−s2 )] quadrature should work well. Long-tailed functions often succumb to the transformation
790
Chapter 18.
Integral Equations and Inverse Theory
are assumed to be known. Here the prefactor h−n is chosen to make Wn scale as h if (as in the usual case) b − a is proportional to h. Carrying out this prescription for the four-point case gives the result Z
b
a
While the terms in brackets superficially appear to scale as k2 , there is typically cancellation at both O(k2 ) and O(k). Equation (18.3.5) can be specialized to various choices of (a, b). The obvious choice is a = kh, b = (k + 3)h, in which case we get a four-point quadrature rule that generalizes Simpson’s 3/8 rule (equation 4.1.5). In fact, we can recover this special case by setting w(x) = 1, in which case (18.3.4) becomes h [(k + 3)n+1 − kn+1 ] (18.3.6) n+1 The four terms in square brackets equation (18.3.5) each become independent of k, and (18.3.5) in fact reduces to Z (k+3)h 3h 9h 9h 3h f (x)dx = f (kh)+ f ([k +1]h)+ f ([k +2]h)+ f ([k +3]h) (18.3.7) 8 8 8 8 kh Wn =
Back to the case of general w(x), some other choices for a and b are also useful. For example, we may want to choose (a, b) to be ([k + 1]h, [k + 3]h) or ([k + 2]h, [k + 3]h), allowing us to finish off an extended rule whose number of intervals is not a multiple of three, without loss of accuracy: The integral will be estimated using the four values f (kh), . . . , f ([k + 3]h). Even more useful is to choose (a, b) to be ([k + 1]h, [k + 2]h), thus using four points to integrate a centered single interval. These weights, when sewed together into an extended formula, give quadrature schemes that have smooth coefficients, i.e., without the Simpson-like 2, 4, 2, 4, 2 alternation. (In fact, this was the technique that we used to derive equation 4.1.14, which you may now wish to reexamine.) All these rules are of the same order as the extended Simpson’s rule, that is, exact for f (x) a cubic polynomial. Rules of lower order, if desired, are similarly obtained. The three point formula is Z b 1 w(x)f (x)dx = f (kh) (k + 1)(k + 2)W0 − (2k + 3)W1 + W2 2 a (18.3.8) + f ([k + 1]h) − k(k + 2)W0 + 2(k + 1)W1 − W2 1 + f ([k + 2]h) k(k + 1)W0 − (2k + 1)W1 + W2 2 Here the simple special case is to take, w(x) = 1, so that h [(k + 2)n+1 − kn+1 ] n+1 Then equation (18.3.8) becomes Simpson’s rule, Z (k+2)h h 4h h f (x)dx = f (kh) + f ([k + 1]h) + f ([k + 2]h) 3 3 3 kh Wn =
(18.3.9)
(18.3.10)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
w(x)f (x)dx = 1 f (kh) (k + 1)(k + 2)(k + 3)W0 − (3k2 + 12k + 11)W1 + 3(k + 2)W2 − W3 6 1 + f ([k + 1]h) − k(k + 2)(k + 3)W0 + (3k2 + 10k + 6)W1 − (3k + 5)W2 + W3 2 1 + f ([k + 2]h) k(k + 1)(k + 3)W0 − (3k2 + 8k + 3)W1 + (3k + 4)W2 − W3 2 1 + f ([k + 3]h) − k(k + 1)(k + 2)W0 + (3k2 + 6k + 2)W1 − 3(k + 1)W2 + W3 6 (18.3.5)
18.3 Integral Equations with Singular Kernels
791
For nonconstant weight functions w(x), however, equation (18.3.8) gives rules of one order less than Simpson, since they do not benefit from the extra symmetry of the constant case. The two point formula is simply Z (k+1)h w(x)f (x)dx = f (kh)[(k + 1)W0 − W1 ] + f ([k + 1]h)[−kW0 + W1 ] (18.3.11) kh
(The lower limit is arbitrary and can be chosen for convenience.) Cautionary note: When called with N < 4, wwghts returns a rule of lower order than Simpson; you should structure your problem to avoid this.
C
* * * * * * * * * * * *
SUBROUTINE wwghts(wghts,n,h,kermom) INTEGER n REAL wghts(n),h EXTERNAL kermom USES kermom Constructs in wghts(1:n) weights for the n-point equal-interval quadrature from 0 to (n −1)h of a function f (x) times an arbitrary (possibly singular) weight function w(x) whose indefinite-integral moments Fn (y) are provided by the user-supplied subroutine kermom. INTEGER j,k DOUBLE PRECISION wold(4),wnew(4),w(4),hh,hi,c,fac,a,b hh=h Double precision on internal calculations even though hi=1.d0/hh the interface is in single precision. do 11 j=1,n Zero all the weights so we can sum into them. wghts(j)=0. enddo 11 call kermom(wold,0.d0,4) Evaluate indefinite integrals at lower end. if (n.ge.4) then Use highest available order. b=0.d0 For another problem, you might change this lower do 14 j=1,n-3 limit. c=j-1 This is called k in equation (18.3.5). a=b Set upper and lower limits for this step. b=a+hh if (j.eq.n-3) b=(n-1)*hh Last interval: go all the way to end. call kermom(wnew,b,4) fac=1.d0 do 12 k=1,4 Equation (18.3.4). w(k)=(wnew(k)-wold(k))*fac fac=fac*hi enddo 12 wghts(j)=wghts(j)+ Equation (18.3.5). ((c+1.d0)*(c+2.d0)*(c+3.d0)*w(1) -(11.d0+c*(12.d0+c*3.d0))*w(2) +3.d0*(c+2.d0)*w(3)-w(4))/6.d0 wghts(j+1)=wghts(j+1)+ (-c*(c+2.d0)*(c+3.d0)*w(1) +(6.d0+c*(10.d0+c*3.d0))*w(2) -(3.d0*c+5.d0)*w(3)+w(4))*.5d0 wghts(j+2)=wghts(j+2)+ (c*(c+1.d0)*(c+3.d0)*w(1) -(3.d0+c*(8.d0+c*3.d0))*w(2) +(3.d0*c+4.d0)*w(3)-w(4))*.5d0 wghts(j+3)=wghts(j+3)+ (-c*(c+1.d0)*(c+2.d0)*w(1) +(2.d0+c*(6.d0+c*3.d0))*w(2) -3.d0*(c+1.d0)*w(3)+w(4))/6.d0 do 13 k=1,4 Reset lower limits for moments.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Here is a routine wwghts that uses the above formulas to return an extended N -point quadrature rule for the interval (a, b) = (0, [N − 1]h). Input to wwghts is a user-supplied routine, kermom, that is called to get the first four indefinite-integral moments of w(x), namely Z y Fm (y) ≡ sm w(s)ds m = 0, 1, 2, 3 (18.3.12)
792
Chapter 18.
Integral Equations and Inverse Theory
We will now give an example of how to apply wwghts to a singular integral equation.
Worked Example: A Diagonally Singular Kernel As a particular example, consider the integral equation Z π K(x, y)f (y)dy = sin x f (x) +
(18.3.13)
0
with the (arbitrarily chosen) nasty kernel
K(x, y) = cos x cos y ×
ln(x √ − y) y−x
y x (18.3.15) x
or
Z
y
Fm (y; x) = x
Z
0 x−y
sm ln(x − s)ds =
(x − t)m ln t dt
if y < x
(18.3.16)
0
(where a change of variable has been made in the second equality in each case). Doing these integrals analytically (actually, we used a symbolic integration package!), we package the resulting formulas in the following routine. Note that w(j + 1) returns Fj (y; x). SUBROUTINE kermom(w,y,m) Returns in w(1:m) the first m indefinite-integral moments of one row of the singular part of the kernel. (For this example, m is hard-wired to be 4.) The input variable y labels the column, while x (in COMMON) is the row. INTEGER m DOUBLE PRECISION w(m),y,x,d,df,clog,x2,x3,x4 COMMON /momcom/ x We can take x as the lower limit of integration. Thus, we return the moment integrals either purely to the left or purely to the right of the diagonal. if (y.ge.x) then d=y-x df=2.d0*sqrt(d)*d w(1)=df/3.d0
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
wold(k)=wnew(k) enddo 13 enddo 14 else if (n.eq.3) then Lower-order cases; not recommended. call kermom(wnew,hh+hh,3) w(1)=wnew(1)-wold(1) w(2)=hi*(wnew(2)-wold(2)) w(3)=hi**2*(wnew(3)-wold(3)) wghts(1)=w(1)-1.5d0*w(2)+0.5d0*w(3) wghts(2)=2.d0*w(2)-w(3) wghts(3)=0.5d0*(w(3)-w(2)) else if (n.eq.2) then call kermom(wnew,hh,2) wghts(2)=hi*(wnew(2)-wold(2)) wghts(1)=wnew(1)-wold(1)-wghts(2) endif END
18.3 Integral Equations with Singular Kernels
*
*
w(2)=df*(x/3.d0+d/5.d0) w(3)=df*((x/3.d0 + 0.4d0*d)*x + d**2/7.d0) w(4)=df*(((x/3.d0 + 0.6d0*d)*x + 3.d0*d**2/7.d0)*x + d**3/9.d0) else x2=x**2 x3=x2*x x4=x2*x2 d=x-y clog=log(d) w(1)=d*(clog-1.d0) w(2)=-0.25d0*(3.d0*x+y-2.d0*clog*(x+y))*d w(3)=(-11.d0*x3+y*(6.d0*x2+y*(3.d0*x+2.d0*y)) +6.d0*clog*(x3-y**3))/18.d0 w(4)=(-25.d0*x4+y*(12.d0*x3+y*(6.d0*x2+y* (4.d0*x+3.d0*y)))+12.d0*clog*(x4-y**4))/48.d0 endif return END
Next, we write a routine that constructs the quadrature matrix.
C
SUBROUTINE quadmx(a,n,np) INTEGER n,np,NMAX REAL a(np,np),PI DOUBLE PRECISION xx PARAMETER (PI=3.14159265,NMAX=257) COMMON /momcom/ xx EXTERNAL kermom USES wwghts,kermom Constructs in a(1:n,1:n) the quadrature matrix for an example Fredholm equation of the second kind. The nonsingular part of the kernel is computed within this routine, while the quadrature weights which integrate the singular part of the kernel are obtained via calls to wwghts. An external routine kermom, which supplies indefinite-integral moments of the singular part of the kernel, is passed to wwghts. INTEGER j,k REAL h,wt(NMAX),x,cx,y h=PI/(n-1) do 12 j=1,n x=(j-1)*h xx=x Put x in COMMON for use by kermom. call wwghts(wt,n,h,kermom) cx=cos(x) Part of nonsingular kernel. do 11 k=1,n y=(k-1)*h a(j,k)=wt(k)*cx*cos(y) Put together all the pieces of the kernel. enddo 11 a(j,j)=a(j,j)+1. Since equation of the second kind, there is diagonal enddo 12 piece independent of h. return END
Finally, we solve the linear system for any particular right-hand side, here sin x.
C
PROGRAM fredex INTEGER NMAX REAL PI PARAMETER (NMAX=100,PI=3.14159265) INTEGER indx(NMAX),j,n REAL a(NMAX,NMAX),g(NMAX),x,d USES quadmx,ludcmp,lubksb
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
793
794
Chapter 18.
Integral Equations and Inverse Theory
1
0
− .5
n = 10 n = 20 n = 40
0
.5
1
1.5
2
2.5
3
x Figure 18.3.1. Solution of the example integral equation (18.3.14) with grid sizes N = 10, 20, and 40. The tabulated solution values have been connected by straight lines; in practice one would interpolate a small N solution more smoothly. This sample program shows how to solve a Fredholm equation of the second kind using the product Nystrom method and a quadrature rule especially constructed for a particular, singular, kernel. n=40 Here the size of the grid is specified. call quadmx(a,n,NMAX) Make the quadrature matrix; all the action is here. call ludcmp(a,n,NMAX,indx,d) Decompose the matrix. do 11 j=1,n Construct the right hand side, here sin x. x=(j-1)*PI/(n-1) g(j)=sin(x) enddo 11 call lubksb(a,n,NMAX,indx,g) Backsubstitute. do 12 j=1,n Write out the solution. x=(j-1)*PI/(n-1) write (*,*) j,x,g(j) enddo 12 write (*,*) ’normal completion’ END
With N = 40, this program gives accuracy at about the 10−5 level. The accuracy increases as N 4 (as it should for our Simpson-order quadrature scheme) despite the highly singular kernel. Figure 18.3.1 shows the solution obtained, also plotting the solution for smaller values of N , which are themselves seen to be remarkably faithful. Notice that the solution is smooth, even though the kernel is singular, a common occurrence. CITED REFERENCES AND FURTHER READING: Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York). [1] Stroud, A.H., and Secrest, D. 1966, Gaussian Quadrature Formulas (Englewood Cliffs, NJ: Prentice-Hall). [2] Delves, L.M., and Mohamed, J.L. 1985, Computational Methods for Integral Equations (Cambridge, U.K.: Cambridge University Press). [3] Atkinson, K.E. 1976, A Survey of Numerical Methods for the Solution of Fredholm Integral Equations of the Second Kind (Philadelphia: S.I.A.M.). [4]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
f (x)
.5
18.4 Inverse Problems and the Use of A Priori Information
795
18.4 Inverse Problems and the Use of A Priori Information
minimize:
A[u]
or
minimize:
B[u]
(18.4.1)
(Of course these will generally give different answers for u.) As another possibility, now suppose that we want to minimize A[u] subject to the constraint that B[u] have some particular value, say b. The method of Lagrange multipliers gives the variation δ δ {A[u] + λ1 (B[u] − b)} = (A[u] + λ1 B[u]) = 0 δu δu
(18.4.2)
where λ1 is a Lagrange multiplier. Notice that b is absent in the second equality, since it doesn’t depend on u. Next, suppose that we change our minds and decide to minimize B[u] subject to the constraint that A[u] have a particular value, a. Instead of equation (18.4.2) we have δ δ {B[u] + λ2 (A[u] − a)} = (B[u] + λ2 A[u]) = 0 δu δu
(18.4.3)
with, this time, λ2 the Lagrange multiplier. Multiplying equation (18.4.3) by the constant 1/λ2 , and identifying 1/λ2 with λ1 , we see that the actual variations are exactly the same in the two cases. Both cases will yield the same one-parameter family of solutions, say, u(λ1 ). As λ1 varies from 0 to ∞, the solution u(λ1 ) varies along a so-called trade-off curve between the problem of minimizing A and the problem of minimizing B. Any solution along this curve can equally well be thought of as either (i) a minimization of A for some constrained value of B, or (ii) a minimization of B for some constrained value of A, or (iii) a weighted minimization of the sum A + λ1 B. The second preliminary point has to do with degenerate minimization principles. In the example above, now suppose that A[u] has the particular form A[u] = |A · u − c|2
(18.4.4)
for some matrix A and vector c. If A has fewer rows than columns, or if A is square but degenerate (has a nontrivial nullspace, see §2.6, especially Figure 2.6.1), then minimizing A[u] will not give a unique solution for u. (To see why, review §15.4, and note that for a “design matrix” A with fewer rows than columns, the matrix AT · A in the normal equations 15.4.10 is degenerate.) However, if we add any multiple λ times a nondegenerate quadratic form B[u], for example u · H · u with H a positive definite matrix, then minimization of A[u] + λB[u] will lead to a unique solution for u. (The sum of two quadratic forms is itself a quadratic form, with the second piece guaranteeing nondegeneracy.)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Later discussion will be facilitated by some preliminary mention of a couple of mathematical points. Suppose that u is an “unknown” vector that we plan to determine by some minimization principle. Let A[u] > 0 and B[u] > 0 be two positive functionals of u, so that we can try to determine u by either
796
Chapter 18.
Integral Equations and Inverse Theory
We can combine these two points, for this conclusion: When a quadratic minimization principle is combined with a quadratic constraint, and both are positive, only one of the two need be nondegenerate for the overall problem to be well-posed. We are now equipped to face the subject of inverse problems.
Suppose that u(x) is some unknown or underlying (u stands for both unknown and underlying!) physical process, which we hope to determine by a set of N measurements ci , i = 1, 2, . . . , N . The relation between u(x) and the ci ’s is that each ci measures a (hopefully distinct) aspect of u(x) through its own linear response kernel ri , and with its own measurement error ni . In other words, Z ci ≡ si + ni = ri (x)u(x)dx + ni (18.4.5) (compare this to equations 13.3.1 and 13.3.2). Within the assumption of linearity, this is quite a general formulation. The ci ’s might approximate values of u(x) at certain locations xi , in which case ri (x) would have the form of a more or less narrow instrumental response centered around x = xi . Or, the ci ’s might “live” in an entirely different function space from u(x), measuring different Fourier components of u(x) for example. The inverse problem is, given the ci ’s, the ri (x)’s, and perhaps some information about the errors ni such as their covariance matrix Sij ≡ Covar[ni , nj ]
(18.4.6)
how do we find a good statistical estimator of u(x), call it u b(x)? It should be obvious that this is an ill-posed problem. After all, how can we reconstruct a whole function u b(x) from only a finite number of discrete values ci ? Yet, whether formally or informally, we do this all the time in science. We routinely measure “enough points” and then “draw a curve through them.” In doing so, we are making some assumptions, either about the underlying function u(x), or about the nature of the response functions ri (x), or both. Our purpose now is to formalize these assumptions, and to extend our abilities to cases where the measurements and underlying function live in quite different function spaces. (How do you “draw a curve” through a scattering of Fourier coefficients?) We can’t really want every point x of the function u b(x). We do want some large number M of discrete points xµ , µ = 1, 2, . . ., M , where M is sufficiently large, and the xµ ’s are sufficiently evenly spaced, that neither u(x) nor ri (x) varies much between any xµ and xµ+1 . (Here and following we will use Greek letters like µ to denote values in the space of the underlying process, and Roman letters like i to denote values of immediate observables.) For such a dense set of xµ ’s, we can replace equation (18.4.5) by a quadrature like X Riµu(xµ ) + ni (18.4.7) ci = µ
where the N × M matrix R has components Riµ ≡ ri (xµ )(xµ+1 − xµ−1 )/2
(18.4.8)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The Inverse Problem with Zeroth-Order Regularization
797
18.4 Inverse Problems and the Use of A Priori Information
(or any other simple quadrature — it rarely matters which). We will view equations (18.4.5) and (18.4.7) as being equivalent for practical purposes. How do you solve a set of equations like equation (18.4.7) for the unknown u(xµ )’s? Here is a bad way, but one that contains the germ of some correct ideas: Form a χ2 measure of how well a model u b(x) agrees with the measured data, χ =
N N X X i=1 j=1
" ci −
M X
"
# Riµ u b(xµ )
µ=1
" #2 PM N X ci − µ=1 Riµ u b(xµ ) ≈ σi
−1 Sij
cj −
M X µ=1
# Rjµu b(xµ ) (18.4.9)
i=1
(compare with equation 15.1.5). Here S−1 is the inverse of the covariance matrix, and the approximate equality holds if you can neglect the off-diagonal covariances, with σi ≡ (Covar[i, i])1/2 . Now you can use the method of singular value decomposition (SVD) in §15.4 to find the vector b u that minimizes equation (18.4.9). Don’t try to use the method of normal equations; since M is greater than N they will be singular, as we already discussed. The SVD process will thus surely find a large number of zero singular values, indicative of a highly non-unique solution. Among the infinity of degenerate solutions (most of them badly behaved with arbitrarily large u b(xµ )’s) SVD will select the one with smallest |b u| in the sense of X
[b u(xµ )]2
a minimum
(18.4.10)
µ
(look at Figure 2.6.1). This solution is often called the principal solution. It is a limiting case of what is called zeroth-order regularization, corresponding to minimizing the sum of the two positive functionals minimize:
u] + λ(b u·b u) χ2 [b
(18.4.11)
in the limit of small λ. Below, we will learn how to do such minimizations, as well as more general ones, without the ad hoc use of SVD. What happens if we determine b u by equation (18.4.11) with a non-infinitesimal value of λ? First, note that if M N (many more unknowns than equations), then u will often have enough freedom to be able to make χ2 (equation 18.4.9) quite unrealistically small, if not zero. In the language of §15.1, the number of degrees of freedom ν = N − M , which is approximately the expected value of χ2 when ν is large, is being driven down to zero (and, not meaningfully, beyond). Yet, we know that for the true underlying function u(x), which has no adjustable parameters, the number of degrees of freedom and the expected value of χ2 should be about ν ≈ N . Increasing λ pulls the solution away from minimizing χ2 in favor of minimizing b u·b u. From the preliminary discussion above, we can view this as minimizing b u·b u subject to the constraint that χ2 have some constant nonzero value. A popular choice, in fact, is to find that value of λ which yields χ2 = N , that is, to get about as much extra regularization as a plausible value of χ2 dictates. The resulting u b(x) is called the solution of the inverse problem with zeroth-order regularization.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
798
best smoothness (independent of agreement)
Integral Equations and Inverse Theory
achievable solutions
st
be ns
tio
lu
so
best agreement (independent of smoothness) Better Smoothness
Figure 18.4.1. Almost all inverse problem methods involve a trade-off between two optimizations: agreement between data and solution, or “sharpness”of mapping between true and estimated solution (here denoted A), and smoothness or stability of the solution (here denoted B). Among all possible solutions, shown here schematically as the shaded region, those on the boundary connecting the unconstrained minimum of A and the unconstrained minimum of B are the “best” solutions, in the sense that every other solution is dominated by at least one solution on the curve.
The value N is actually a surrogate for any value drawn from a Gaussian distribution with mean N and standard deviation (2N )1/2 (the asymptotic χ2 distribution). One might equally plausibly try two values of λ, one giving χ2 = N + (2N )1/2 , the other N − (2N )1/2 . Zeroth-order regularization, though dominated by better methods, demonstrates most of the basic ideas that are used in inverse problem theory. In general, there are two positive functionals, call them A and B. The first, A, measures something like the agreement of a model to the data (e.g., χ2 ), or sometimes a related quantity like the “sharpness” of the mapping between the solution and the underlying function. When A by itself is minimized, the agreement or sharpness becomes very good (often impossibly good), but the solution becomes unstable, wildly oscillating, or in other ways unrealistic, reflecting that A alone typically defines a highly degenerate minimization problem. That is where B comes in. It measures something like the “smoothness” of the desired solution, or sometimes a related quantity that parametrizes the stability of the solution with respect to variations in the data, or sometimes a quantity reflecting a priori judgments about the likelihood of a solution. B is called the stabilizing functional or regularizing operator. In any case, minimizing B by itself is supposed to give a solution that is “smooth” or “stable” or “likely” — and that has nothing at all to do with the measured data.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Better Agreement
Chapter 18.
799
18.5 Linear Regularization Methods
The single central idea in inverse theory is the prescription minimize:
A + λB
(18.4.12)
CITED REFERENCES AND FURTHER READING: Craig, I.J.D., and Brown, J.C. 1986, Inverse Problems in Astronomy (Bristol, U.K.: Adam Hilger). Twomey, S. 1977, Introduction to the Mathematics of Inversion in Remote Sensing and Indirect Measurements (Amsterdam: Elsevier). Tikhonov, A.N., and Arsenin, V.Y. 1977, Solutions of Ill-Posed Problems (New York: Wiley). Tikhonov, A.N., and Goncharsky, A.V. (eds.) 1987, Ill-Posed Problems in the Natural Sciences (Moscow: MIR). Parker, R.L. 1977, Annual Review of Earth and Planetary Science, vol. 5, pp. 35–64. Frieden, B.R. 1975, in Picture Processing and Digital Filtering, T.S. Huang, ed. (New York: Springer-Verlag). Tarantola, A. 1987, Inverse Problem Theory (Amsterdam: Elsevier). Baumeister, J. 1987, Stable Solution of Inverse Problems (Braunschweig, Germany: Friedr. Vieweg & Sohn) [mathematically oriented]. Titterington, D.M. 1985, Astronomy and Astrophysics, vol. 144, pp. 381–387. Jeffrey, W., and Rosner, R. 1986, Astrophysical Journal, vol. 310, pp. 463–472.
18.5 Linear Regularization Methods What we will call linear regularization is also called the Phillips-Twomey method [1,2] , the constrained linear inversion method [3], the method of regularization [4], and Tikhonov-Miller regularization [5-7]. (It probably has other names also,
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
for various values of 0 < λ < ∞ along the so-called trade-off curve (see Figure 18.4.1), and then to settle on a “best” value of λ by one or another criterion, ranging from fairly objective (e.g., making χ2 = N ) to entirely subjective. Successful methods, several of which we will now describe, differ as to their choices of A and B, as to whether the prescription (18.4.12) yields linear or nonlinear equations, as to their recommended method for selecting a final λ, and as to their practicality for computer-intensive two-dimensional problems like image processing. They also differ as to the philosophical baggage that they (or rather, their proponents) carry. We have thus far avoided the word “Bayesian.” (Courts have consistently held that academic license does not extend to shouting “Bayesian” in a crowded lecture hall.) But it is hard, nor have we any wish, to disguise the fact that B has something to do with a priori expectation, or knowledge, of a solution, while A has something to do with a posteriori knowledge. The constant λ adjudicates a delicate compromise between the two. Some inverse methods have acquired a more Bayesian stamp than others, but we think that this is purely an accident of history. An outsider looking only at the equations that are actually solved, and not at the accompanying philosophical justifications, would have a difficult time separating the so-called Bayesian methods from the so-called empirical ones, we think. The next three sections discuss three different approaches to the problem of inversion, which have had considerable success in different fields. All three fit within the general framework that we have outlined, but they are quite different in detail and in implementation.
800
Chapter 18.
Integral Equations and Inverse Theory
µ=1
since it is nonnegative and equal to zero only when u b(x) is constant. Here b(xµ ), and the second equality (proportionality) assumes that the xµ ’s are u bµ ≡ u uniformly spaced. We can write the second form of B as B = |B · b u|2 = b u · (BT · B) · b u≡b u·H·b u
(18.5.2)
b is the vector of components u where u bµ , µ = 1, . . . , M , B is the (M − 1) × M first difference matrix −1 1 0 0 0 0 0 ··· 0 1 0 0 0 0 ··· 0 0 −1 . .. .. .. (18.5.3) B= . . 0 ··· 0 0 0 0 −1 1 0 0 ··· 0 0 0 0 0 −1 1 and H is the M × M matrix 1 −1 0 2 −1 −1 2 0 −1 . .. H = BT · B = 0 0 ··· 0 ··· 0 0 ··· 0
0 0 −1
0 0 0 ..
0 0 0
. 0 0 0
0 0 0
0 0 0
−1 2 0 −1 0 0
0 0 0 .. . −1 0 2 −1 −1 1 ··· ··· ···
(18.5.4)
Note that B has one fewer row than column. It follows that the symmetric H is degenerate; it has exactly one zero eigenvalue corresponding to the value of a constant function, any one of which makes B exactly zero. If, just as in §15.4, we write Aiµ ≡ Riµ/σi
bi ≡ ci /σi
(18.5.5)
then, using equation (18.4.9), the minimization principle (18.4.12) is minimize:
A + λB = |A · b u − b|2 + λb u·H·b u
(18.5.6)
This can readily be reduced to a linear set of normal equations, just as in §15.4: The components u bµ of the solution satisfy the set of M equations in M unknowns, " # X X X bρ = Aiµ Aiρ + λHµρ u Aiµ bi µ = 1, 2, . . . , M (18.5.7) ρ
i
i
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
since it is so obviously a good idea.) In its simplest form, the method is an immediate generalization of zeroth-order regularization (equation 18.4.11, above). As before, the functional A is taken to be the χ2 deviation, equation (18.4.9), but the functional B is replaced by more sophisticated measures of smoothness that derive from first or higher derivatives. For example, suppose that your a priori belief is that a credible u(x) is not too different from a constant. Then a reasonable functional to minimize is Z M −1 X 0 2 [b uµ − u bµ+1 ]2 (18.5.1) B ∝ [b u (x)] dx ∝
801
18.5 Linear Regularization Methods
or, in vector notation, u = AT · b (AT · A + λH) · b
(18.5.8)
where the identity matrix in the form A · A−1 has been inserted. This is schematic not only because the matrix inverse is fancifully written as a denominator, but also because, in general, the inverse matrix A−1 does not exist. However, it is illuminating to compare equation (18.5.9) with equation (13.3.6) for optimal or Wiener filtering, or with equation (13.6.6) for general linear prediction. One sees that AT · A plays the role of S 2 , the signal power or autocorrelation, while λH plays the role of N 2 , the noise power or autocorrelation. The term in parentheses in equation (18.5.9) is something like an optimal filter, whose effect is to pass the ill-posed inverse A−1 · b through unmodified when AT · A is sufficiently large, but to suppress it when AT · A is small. The above choices of B and H are only the simplest in an obvious sequence of derivatives. If your a priori belief is that a linear function is a good approximation to u(x), then minimize Z M −2 X B ∝ [b u00 (x)]2 dx ∝ [−b uµ + 2b uµ+1 − u bµ+2 ]2 (18.5.10) µ=1
implying
−1 2 −1 0 2 −1 0 −1 . . B= . 0 ··· 0 0 0 ··· 0 0 and
1 −2 −2 5 1 −4 1 0 . .. H = BT · B = 0 ··· 0 ··· 0 ··· 0 ···
0 0 ..
. 0 0
1 0 −4 1 6 −4 −4 6 0 0 0 0
1 0 0 0
0 0
0 0
−1 2 0 −1 0 0 1 −4 .. .
0 0 0 1
··· ···
0 0 .. . 0
(18.5.11)
−1 2 −1 0 0 0 0
−4 6 −4 1 −4 6 0 1 −4 0 0 1
0 0 0 0 .. . 1 0 −4 1 5 −2 −2 1 ··· ··· ··· ···
(18.5.12)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Equations (18.5.7) or (18.5.8) can be solved by the standard techniques of Chapter 2, e.g., LU decomposition. The usual warnings about normal equations being ill-conditioned do not apply, since the whole purpose of the λ term is to cure that same ill-conditioning. Note, however, that the λ term by itself is ill-conditioned, since it does not select a preferred constant value. You hope your data can at least do that! Although inversion of the matrix (AT · A + λH) is not generally the best way to solve for b u, let us digress to write the solution to equation (18.5.8) schematically as 1 T b u= · A · A A−1 · b (schematic only!) (18.5.9) AT · A + λH
802
Chapter 18.
Integral Equations and Inverse Theory
This H has two zero eigenvalues, corresponding to the two undetermined parameters of a linear function. If your a priori belief is that a quadratic function is preferable, then minimize Z B∝
[b u000(x)]2 dx ∝
M −3 X
[−b uµ + 3b uµ+1 − 3b uµ+2 + u bµ+3 ]2
(18.5.13)
with
−1 3 −3 1 0 3 −3 1 0 −1 . . .. B = .. 0 ··· 0 0 −1 0 ··· 0 0 0 and now
0 0
0 0
3 −3 −1 3
··· ··· 1 −3
0 0 .. . 0
(18.5.14)
1
0 0 0 0 0 .. . 20 −15 6 −1 0 −15 20 −15 6 −1 6 −15 19 −12 3 −1 6 −12 10 −3 0 −1 3 −3 1 (18.5.15) (We’ll leave the calculation of cubics and above to the compulsive reader.) Notice that you can regularize with “closeness to a differential equation,” if you want. Just pick B to be the appropriate sum of finite-difference operators (the coefficients can depend on x), and calculate H = BT · B. You don’t need to know the values of your boundary conditions, since B can have fewer rows than columns, as above; hopefully, your data will determine them. Of course, if you do know some boundary conditions, you can build these into B too. With all the proportionality signs above, you may have lost track of what actual value of λ to try first. A simple trick for at least getting “on the map” is to first try 1 −3 3 −1 0 0 −3 10 −12 6 −1 0 19 −15 6 −1 3 −12 6 −15 20 −15 6 −1 6 −15 20 −15 0 −1 . .. . H= . . 0 −1 6 −15 0 ··· 0 0 −1 6 0 ··· 0 ··· 0 0 0 −1 0 ··· 0 0 0 0 0 ··· 0 0 0 0
0 0 0 −1 6
λ = Tr(AT · A)/Tr(H)
0 0 0 0 −1
0 0 0 0 0
··· ··· ··· ··· ···
(18.5.16)
where Tr is the trace of the matrix (sum of diagonal components). This choice will tend to make the two parts of the minimization have comparable weights, and you can adjust from there. As for what is the “correct” value of λ, an objective criterion, if you know u − b|2 ) equal your errors σi with reasonable accuracy, is to make χ2 (that is, |A · b to N , the number of measurements. We remarked above on the twin acceptable choices N ± (2N )1/2 . A subjective criterion is to pick any value that you like in the
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
µ=1
18.5 Linear Regularization Methods
803
range 0 < λ < ∞, depending on your relative degree of belief in the a priori and a posteriori evidence. (Yes, people actually do that. Don’t blame us.)
Two-Dimensional Problems and Iterative Methods
e A(i − µ, j − ν) ⇐⇒ A(k)
b(i,j) ⇐⇒ eb(k)
u b(i,j) ⇐⇒ u e(k) (18.5.17)
We also need a regularization or smoothing operator B and the derived H = BT · B. One popular choice for B is the five-point finite-difference approximation of the Laplacian operator, that is, the difference between the value of each point and the average of its four Cartesian neighbors. In Fourier space, this choice implies, e B(k) ∝ sin2 (πk1 /M ) sin2 (πk2 /K) e H(k) ∝ sin4 (πk1 /M ) sin4 (πk2 /K)
(18.5.18)
In Fourier space, equation (18.5.7) is merely algebraic, with solution u e(k) =
e eb(k) A*(k) 2 e e |A(k)| + λH(k)
(18.5.19)
where asterisk denotes complex conjugation. You can make use of the FFT routines for real data in §12.5. Turn now to the case where A is not translationally invariant. Direct solution of (18.5.8) is now hopeless, since the matrix A is just too large. We need some kind of iterative scheme. One way to proceed is to use the full machinery of the conjugate gradient method in §10.6 to find the minimum of A + λB, equation (18.5.6). Of the various methods in Chapter 10, conjugate gradient is the unique best choice because (i) it does not require storage of a Hessian matrix, which would be infeasible here,
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Up to now our notation has been indicative of a one-dimensional problem, b(xµ ). However, all of the discussion easily generalizes to the finding u b(x) or u bµ = u problem of estimating a two-dimensional set of unknowns u bµκ , µ = 1, . . . , M, κ = 1, . . . , K, corresponding, say, to the pixel intensities of a measured image. In this case, equation (18.5.8) is still the one we want to solve. In image processing, it is usual to have the same number of input pixels in a measured “raw” or “dirty” image as desired “clean” pixels in the processed output image, so the matrices R and A (equation 18.5.5) are square and of size M K × M K. A is typically much too large to represent as a full matrix, but often it is either (i) sparse, with coefficients blurring an underlying pixel (i, j) only into measurements (i±few, j±few), or (ii) translationally invariant, so that A(i,j)(µ,ν) = A(i−µ, j−ν). Both of these situations lead to tractable problems. In the case of translational invariance, fast Fourier transforms (FFTs) are the obvious method of choice. The general linear relation between underlying function and measured values (18.4.7) now becomes a discrete convolution like equation (13.1.1). If k denotes a two-dimensional wave-vector, then the two-dimensional FFT takes us back and forth between the transform pairs
804
Chapter 18.
Integral Equations and Inverse Theory
and (ii) it does exploit gradient information, which we can readily compute: The gradient of equation (18.5.6) is ∇(A + λB) = 2[(AT · A + λH) · b u − AT · b]
(18.5.20)
b u(k) + AT · b u(k+1) = [1 − (AT · A + λH)] · b
(18.5.21)
Here is a parameter that dictates how far to move in the downhill gradient direction. The method converges when is small enough, in particular satisfying 0 1 for some k. The number ξ is called the amplification factor at a given wave number k. To find ξ(k), we simply substitute (19.1.12) back into (19.1.11). Dividing by ξ n , we get ξ(k) = 1 − i
v∆t sin k∆x ∆x
(19.1.13)
whose modulus is > 1 for all k; so the FTCS scheme is unconditionally unstable. If the velocity v were a function of t and x, then we would write vjn in equation (19.1.11). In the von Neumann stability analysis we would still treat v as a constant, the idea being that for v slowly varying the analysis is local. In fact, even in the case of strictly constant v, the von Neumann analysis does not rigorously treat the end effects at j = 0 and j = N . More generally, if the equation’s right-hand side were nonlinear in u, then a von Neumann analysis would linearize by writing u = u0 + δu, expanding to linear order in δu. Assuming that the u0 quantities already satisfy the difference equation exactly, the analysis would look for an unstable eigenmode of δu. Despite its lack of rigor, the von Neumann method generally gives valid answers and is much easier to apply than more careful methods. We accordingly adopt it exclusively. (See, for example, [1] for a discussion of other methods of stability analysis.)
Lax Method The instability in the FTCS method can be cured by a simple change due to Lax. One replaces the term unj in the time derivative term by its average (Figure 19.1.2): unj →
1 n uj+1 + unj−1 2
(19.1.14)
This turns (19.1.11) into un+1 = j
v∆t n 1 n u u + unj−1 − − unj−1 2 j+1 2∆x j+1
(19.1.15)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
x or j
829
19.1 Flux-Conservative Initial Value Problems
stable
unstable ∆t ∆t
∆x
∆x
x or j (a)
( b)
Figure 19.1.3. Courant condition for stability of a differencing scheme. The solution of a hyperbolic problem at a point depends on information within some domain of dependency to the past, shown here shaded. The differencing scheme (19.1.15) has its own domain of dependency determined by the choice of points on one time slice (shown as connected solid dots) whose values are used in determining a new point (shown connected by dashed lines). A differencing scheme is Courant stable if the differencing domain of dependency is larger than that of the PDEs, as in (a), and unstable if the relationship is the reverse, as in (b). For more complicated differencing schemes, the domain of dependency might not be determined simply by the outermost points.
Substituting equation (19.1.12), we find for the amplification factor ξ = cos k∆x − i
v∆t sin k∆x ∆x
(19.1.16)
The stability condition |ξ|2 ≤ 1 leads to the requirement |v|∆t ≤1 ∆x
(19.1.17)
This is the famous Courant-Friedrichs-Lewy stability criterion, often called simply the Courant condition. Intuitively, the stability condition can be in equation (19.1.15) is understood as follows (Figure 19.1.3): The quantity un+1 j computed from information at points j − 1 and j + 1 at time n. In other words, xj−1 and xj+1 are the boundaries of the spatial region that is allowed to communicate . Now recall that in the continuum wave equation, information information to un+1 j actually propagates with a maximum velocity v. If the point un+1 is outside of j the shaded region in Figure 19.1.3, then it requires information from points more distant than the differencing scheme allows. Lack of that information gives rise to an instability. Therefore, ∆t cannot be made too large. The surprising result, that the simple replacement (19.1.14) stabilizes the FTCS scheme, is our first encounter with the fact that differencing PDEs is an art as much as a science. To see if we can demystify the art somewhat, let us compare the FTCS and Lax schemes by rewriting equation (19.1.15) so that it is in the form of equation (19.1.11) with a remainder term: n − unj un+1 uj+1 − unj−1 1 unj+1 − 2unj + unj−1 j = −v + (19.1.18) ∆t 2∆x 2 ∆t But this is exactly the FTCS representation of the equation ∂u (∆x)2 2 ∂u = −v + ∇ u ∂t ∂x 2∆t
(19.1.19)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
t or n
830
Chapter 19.
Partial Differential Equations
The Lax method for this equation is 1 n n (r + rj−1 )+ 2 j+1 1 = (snj+1 + snj−1 ) + 2
rjn+1 = sn+1 j
v∆t n (s − snj−1 ) 2∆x j+1 v∆t n n (r − rj−1 ) 2∆x j+1
(19.1.21)
The von Neumann stability analysis now proceeds by assuming that the eigenmode is of the following (vector) form, 0 n rj n ikj∆x r = ξ (19.1.22) e snj s0 Here the vector on the right-hand side is a constant (both in space and in time) eigenvector, and ξ is a complex number, as before. Substituting (19.1.22) into (19.1.21), and dividing by the power ξ n , gives the homogeneous vector equation
(cos k∆x) − ξ v∆t sin k∆x i ∆x
v∆t r0 sin k∆x 0 ∆x · = (cos k∆x) − ξ s0 0 i
(19.1.23)
This admits a solution only if the determinant of the matrix on the left vanishes, a condition easily shown to yield the two roots ξ ξ = cos k∆x ± i
v∆t sin k∆x ∆x
(19.1.24)
The stability condition is that both roots satisfy |ξ| ≤ 1. This again turns out to be simply the Courant condition (19.1.17).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where ∇2 = ∂ 2 /∂x2 in one dimension. We have, in effect, added a diffusion term to the equation, or, if you recall the form of the Navier-Stokes equation for viscous fluid flow, a dissipative term. The Lax scheme is thus said to have numerical dissipation, or numerical viscosity. We can see this also in the amplification factor. Unless |v|∆t is exactly equal to ∆x, |ξ| < 1 and the amplitude of the wave decreases spuriously. Isn’t a spurious decrease as bad as a spurious increase? No. The scales that we hope to study accurately are those that encompass many grid points, so that they have k∆x 1. (The spatial wave number k is defined by equation 19.1.12.) For these scales, the amplification factor can be seen to be very close to one, in both the stable and unstable schemes. The stable and unstable schemes are therefore about equally accurate. For the unstable scheme, however, short scales with k∆x ∼ 1, which we are not interested in, will blow up and swamp the interesting part of the solution. Much better to have a stable scheme in which these short wavelengths die away innocuously. Both the stable and the unstable schemes are inaccurate for these short wavelengths, but the inaccuracy is of a tolerable character when the scheme is stable. When the independent variable u is a vector, then the von Neumann analysis is slightly more complicated. For example, we can consider equation (19.1.3), rewritten as ∂ vs ∂ r = (19.1.20) ∂t s ∂x vr
19.1 Flux-Conservative Initial Value Problems
831
Other Varieties of Error
v∆t ξ = e−ik∆x + i 1 − sin k∆x ∆x
(19.1.25)
An arbitrary initial wave packet is a superposition of modes with different k’s. At each timestep the modes get multiplied by different phase factors (19.1.25), depending on their value of k. If ∆t = ∆x/v, then the exact solution for each mode of a wave packet f(x − vt) is obtained if each mode gets multiplied by exp(−ik∆x). For this value of ∆t, equation (19.1.25) shows that the finite-difference solution gives the exact analytic result. However, if v∆t/∆x is not exactly 1, the phase relations of the modes can become hopelessly garbled and the wave packet disperses. Note from (19.1.25) that the dispersion becomes large as soon as the wavelength becomes comparable to the grid spacing ∆x. A third type of error is one associated with nonlinear hyperbolic equations and is therefore sometimes called nonlinear instability. For example, a piece of the Euler or Navier-Stokes equations for fluid flow looks like ∂v ∂v = −v +... ∂t ∂x
(19.1.26)
The nonlinear term in v can cause a transfer of energy in Fourier space from long wavelengths to short wavelengths. This results in a wave profile steepening until a vertical profile or “shock” develops. Since the von Neumann analysis suggests that the stability can depend on k∆x, a scheme that was stable for shallow profiles can become unstable for steep profiles. This kind of difficulty arises in a differencing scheme where the cascade in Fourier space is halted at the shortest wavelength representable on the grid, that is, at k ∼ 1/∆x. If energy simply accumulates in these modes, it eventually swamps the energy in the long wavelength modes of interest. Nonlinear instability and shock formation is thus somewhat controlled by numerical viscosity such as that discussed in connection with equation (19.1.18) above. In some fluid problems, however, shock formation is not merely an annoyance, but an actual physical behavior of the fluid whose detailed study is a goal. Then, numerical viscosity alone may not be adequate or sufficiently controllable. This is a complicated subject which we discuss further in the subsection on fluid dynamics, below. For wave equations, propagation errors (amplitude or phase) are usually most worrisome. For advective equations, on the other hand, transport errors are usually of greater concern. In the Lax scheme, equation (19.1.15), a disturbance in the advected quantity u at mesh point j propagates to mesh points j + 1 and j − 1 at the next timestep. In reality, however, if the velocity v is positive then only mesh point j + 1 should be affected.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Thus far we have been concerned with amplitude error, because of its intimate connection with the stability or instability of a differencing scheme. Other varieties of error are relevant when we shift our concern to accuracy, rather than stability. Finite-difference schemes for hyperbolic equations can exhibit dispersion, or phase errors. For example, equation (19.1.16) can be rewritten as
832
Chapter 19.
Partial Differential Equations
v
t or n v
x or j Figure 19.1.4. Representation of upwind differencing schemes. The upper scheme is stable when the advection constant v is negative, as shown; the lower scheme is stable when the advection constant v is positive, also as shown. The Courant condition must, of course, also be satisfied.
The simplest way to model the transport properties “better” is to use upwind differencing (see Figure 19.1.4): n uj − unj−1 n+1 , n uj − uj ∆x = −vjn un − un ∆t j+1 j , ∆x
vjn > 0 (19.1.27) vjn < 0
Note that this scheme is only first-order, not second-order, accurate in the calculation of the spatial derivatives. How can it be “better”? The answer is one that annoys the mathematicians: The goal of numerical simulations is not always “accuracy” in a strictly mathematical sense, but sometimes “fidelity” to the underlying physics in a sense that is looser and more pragmatic. In such contexts, some kinds of error are much more tolerable than others. Upwind differencing generally adds fidelity to problems where the advected variables are liable to undergo sudden changes of state, e.g., as they pass through shocks or other discontinuities. You will have to be guided by the specific nature of your own problem. For the differencing scheme (19.1.27), the amplification factor (for constant v) is v∆t (1 − cos k∆x) − i v∆t sin k∆x ξ =1− ∆x ∆x v∆t v∆t 1 − (1 − cos k∆x) |ξ|2 = 1 − 2 ∆x ∆x
(19.1.28) (19.1.29)
So the stability criterion |ξ|2 ≤ 1 is (again) simply the Courant condition (19.1.17). There are various ways of improving the accuracy of first-order upwind differencing. In the continuum equation, material originally a distance v∆t away
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
upwind
19.1 Flux-Conservative Initial Value Problems
833
staggered leapfrog
x or j Figure 19.1.5. Representation of the staggered leapfrog differencing scheme. Note that information from two previous time slices is used in obtaining the desired point. This scheme is second-order accurate in both space and time.
arrives at a given point after a time interval ∆t. In the first-order method, the material always arrives from ∆x away. If v∆t ∆x (to insure accuracy), this can cause a large error. One way of reducing this error is to interpolate u between j − 1 and j before transporting it. This gives effectively a second-order method. Various schemes for second-order upwind differencing are discussed and compared in [2-3].
Second-Order Accuracy in Time When using a method that is first-order accurate in time but second-order accurate in space, one generally has to take v∆t significantly smaller than ∆x to achieve desired accuracy, say, by at least a factor of 5. Thus the Courant condition is not actually the limiting factor with such schemes in practice. However, there are schemes that are second-order accurate in both space and time, and these can often be pushed right to their stability limit, with correspondingly smaller computation times. For example, the staggered leapfrog method for the conservation equation (19.1.1) is defined as follows (Figure 19.1.5): Using the values of un at time tn , compute the fluxes Fjn . Then compute new values un+1 using the time-centered values of the fluxes: − un−1 =− un+1 j j
∆t n n (F − Fj−1 ) ∆x j+1
(19.1.30)
The name comes from the fact that the time levels in the time derivative term “leapfrog” over the time levels in the space derivative term. The method requires that un−1 and un be stored to compute un+1 . For our simple model equation (19.1.6), staggered leapfrog takes the form un+1 − un−1 =− j j
v∆t n (u − unj−1 ) ∆x j+1
(19.1.31)
The von Neumann stability analysis now gives a quadratic equation for ξ, rather than a linear one, because of the occurrence of three consecutive powers of ξ when the
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
t or n
834
Chapter 19.
Partial Differential Equations
form (19.1.12) for an eigenmode is substituted into equation (19.1.31), ξ 2 − 1 = −2iξ
v∆t sin k∆x ∆x
(19.1.32)
whose solution is
1−
v∆t sin k∆x ∆x
2 (19.1.33)
Thus the Courant condition is again required for stability. In fact, in equation (19.1.33), |ξ|2 = 1 for any v∆t ≤ ∆x. This is the great advantage of the staggered leapfrog method: There is no amplitude dissipation. Staggered leapfrog differencing of equations like (19.1.20) is most transparent if the variables are centered on appropriate half-mesh points: n rj+1/2
n+1/2 sj
n unj+1 − unj ∂u ≡v = v ∂x j+1/2 ∆x n+1/2 n+1 uj − unj ∂u ≡ = ∂t j ∆t
(19.1.34)
This is purely a notational convenience: we can think of the mesh on which r and s are defined as being twice as fine as the mesh on which the original variable u is defined. The leapfrog differencing of equation (19.1.20) is n+1 n − rj+1/2 rj+1/2 n+1/2 sj
n+1/2
=
∆t n−1/2 − sj =v ∆t
n+1/2
− sj ∆x n n rj+1/2 − rj−1/2
sj+1
(19.1.35)
∆x
If you substitute equation (19.1.22) in equation (19.1.35), you will find that once again the Courant condition is required for stability, and that there is no amplitude dissipation when it is satisfied. If we substitute equation (19.1.34) in equation (19.1.35), we find that equation (19.1.35) is equivalent to − 2unj + un−1 un+1 un − 2unj + unj−1 j j 2 j+1 = v (∆t)2 (∆x)2
(19.1.36)
This is just the “usual” second-order differencing of the wave equation (19.1.2). We see that it is a two-level scheme, requiring both un and un−1 to obtain un+1 . In equation (19.1.35) this shows up as both sn−1/2 and r n being needed to advance the solution. For equations more complicated than our simple model equation, especially nonlinear equations, the leapfrog method usually becomes unstable when the gradients get large. The instability is related to the fact that odd and even mesh points are completely decoupled, like the black and white squares of a chess board, as shown
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
s v∆t ξ = −i sin k∆x ± ∆x
19.1 Flux-Conservative Initial Value Problems
835
in Figure 19.1.6. This mesh drifting instability is cured by coupling the two meshes through a numerical viscosity term, e.g., adding to the right side of (19.1.31) a small coefficient ( 1) times unj+1 − 2unj + unj−1. For more on stabilizing difference schemes by adding numerical dissipation, see, e.g., [4]. The Two-Step Lax-Wendroff scheme is a second-order in time method that avoids large numerical dissipation and mesh drifting. One defines intermediate values uj+1/2 at the half timesteps tn+1/2 and the half mesh points xj+1/2 . These are calculated by the Lax scheme: n+1/2
uj+1/2 =
1 n ∆t (u (F n − Fjn ) + unj ) − 2 j+1 2∆x j+1
(19.1.37)
n+1/2
Using these variables, one calculates the fluxes Fj+1/2 . Then the updated values are calculated by the properly centered expression un+1 j = unj − un+1 j
∆t n+1/2 n+1/2 Fj+1/2 − Fj−1/2 ∆x
(19.1.38)
n+1/2
The provisional values uj+1/2 are now discarded. (See Figure 19.1.7.) Let us investigate the stability of this method for our model advective equation, where F = vu. Substitute (19.1.37) in (19.1.38) to get = unj − α un+1 j
1 1 n (uj+1 + unj ) − α(unj+1 − unj ) 2 2 1 n 1 − (uj + unj−1 ) + α(unj − unj−1 ) 2 2
(19.1.39)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Figure 19.1.6. Origin of mesh-drift instabilities in a staggered leapfrog scheme. If the mesh points are imagined to lie in the squares of a chess board, then white squares couple to themselves, black to themselves, but there is no coupling between white and black. The fix is to introduce a small diffusive mesh-coupling piece.
836
Chapter 19.
Partial Differential Equations
two-step Lax Wendroff halfstep points
x or j Figure 19.1.7. Representation of the two-step Lax-Wendroff differencing scheme. Two halfstep points (⊗) are calculated by the Lax method. These, plus one of the original points, produce the new point via staggered leapfrog. Halfstep points are used only temporarily and do not require storage allocation on the grid. This scheme is second-order accurate in both space and time.
where α≡
v∆t ∆x
(19.1.40)
Then ξ = 1 − iα sin k∆x − α2 (1 − cos k∆x)
(19.1.41)
so |ξ|2 = 1 − α2 (1 − α2 )(1 − cos k∆x)2
(19.1.42)
The stability criterion |ξ|2 ≤ 1 is therefore α2 ≤ 1, or v∆t ≤ ∆x as usual. Incidentally, you should not think that the Courant condition is the only stability requirement that ever turns up in PDEs. It keeps doing so in our model examples just because those examples are so simple in form. The method of analysis is, however, general. Except when α = 1, |ξ|2 < 1 in (19.1.42), so some amplitude damping does occur. The effect is relatively small, however, for wavelengths large compared with the mesh size ∆x. If we expand (19.1.42) for small k∆x, we find |ξ|2 = 1 − α2 (1 − α2 )
(k∆x)4 +... 4
(19.1.43)
The departure from unity occurs only at fourth order in k. This should be contrasted with equation (19.1.16) for the Lax method, which shows that |ξ|2 = 1 − (1 − α2 )(k∆x)2 + . . . for small k∆x.
(19.1.44)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
t or n
19.1 Flux-Conservative Initial Value Problems
837
Fluid Dynamics with Shocks As we alluded to earlier, the treatment of fluid dynamics problems with shocks has become a very complicated and very sophisticated subject. All we can attempt to do here is to guide you to some starting points in the literature. There are basically three important general methods for handling shocks. The oldest and simplest method, invented by von Neumann and Richtmyer, is to add artificial viscosity to the equations, modeling the way Nature uses real viscosity to smooth discontinuities. A good starting point for trying out this method is the differencing scheme in §12.11 of [1]. This scheme is excellent for nearly all problems in one spatial dimension. The second method combines a high-order differencing scheme that is accurate for smooth flows with a low order scheme that is very dissipative and can smooth the shocks. Typically, various upwind differencing schemes are combined using weights chosen to zero the low order scheme unless steep gradients are present, and also chosen to enforce various “monotonicity” constraints that prevent nonphysical oscillations from appearing in the numerical solution. References [2-3,5] are a good place to start with these methods. The third, and potentially most powerful method, is Godunov’s approach. Here one gives up the simple linearization inherent in finite differencing based on Taylor series and includes the nonlinearity of the equations explicitly. There is an analytic solution for the evolution of two uniform states of a fluid separated by a discontinuity, the Riemann shock problem. Godunov’s idea was to approximate the fluid by a large number of cells of uniform states, and piece them together using the Riemann solution. There have been many generalizations of Godunov’s approach, of which the most powerful is probably the PPM method [6]. Readable reviews of all these methods, discussing the difficulties arising when one-dimensional methods are generalized to multidimensions, are given in [7-9] . CITED REFERENCES AND FURTHER READING: Ames, W.F. 1977, Numerical Methods for Partial Differential Equations, 2nd ed. (New York: Academic Press), Chapter 4. Richtmyer, R.D., and Morton, K.W. 1967, Difference Methods for Initial Value Problems, 2nd ed. (New York: Wiley-Interscience). [1] Centrella, J., and Wilson, J.R. 1984, Astrophysical Journal Supplement, vol. 54, pp. 229–249, Appendix B. [2] Hawley, J.F., Smarr, L.L., and Wilson, J.R. 1984, Astrophysical Journal Supplement, vol. 55, pp. 211–246, §2c. [3] Kreiss, H.-O. 1978, Numerical Methods for Solving Time-Dependent Problems for Partial Differential Equations (Montreal: University of Montreal Press), pp. 66ff. [4] Harten, A., Lax, P.D., and Van Leer, B. 1983, SIAM Review, vol. 25, pp. 36–61. [5] Woodward, P., and Colella, P. 1984, Journal of Computational Physics, vol. 54, pp. 174–201. [6]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
In summary, our recommendation for initial value problems that can be cast in flux-conservative form, and especially problems related to the wave equation, is to use the staggered leapfrog method when possible. We have personally had better success with it than with the Two-Step Lax-Wendroff method. For problems sensitive to transport errors, upwind differencing or one of its refinements should be considered.
838
Chapter 19.
Partial Differential Equations
Roache, P.J. 1976, Computational Fluid Dynamics (Albuquerque: Hermosa). [7] Woodward, P., and Colella, P. 1984, Journal of Computational Physics, vol. 54, pp. 115–173. [8] Rizzi, A., and Engquist, B. 1987, Journal of Computational Physics, vol. 72, pp. 1–69. [9]
Recall the model parabolic equation, the diffusion equation in one space dimension, ∂ ∂u ∂u = D (19.2.1) ∂t ∂x ∂x where D is the diffusion coefficient. Actually, this equation is a flux-conservative equation of the form considered in the previous section, with F = −D
∂u ∂x
(19.2.2)
the flux in the x-direction. We will assume D ≥ 0, otherwise equation (19.2.1) has physically unstable solutions: A small disturbance evolves to become more and more concentrated instead of dispersing. (Don’t make the mistake of trying to find a stable differencing scheme for a problem whose underlying PDEs are themselves unstable!) Even though (19.2.1) is of the form already considered, it is useful to consider it as a model in its own right. The particular form of flux (19.2.2), and its direct generalizations, occur quite frequently in practice. Moreover, we have already seen that numerical viscosity and artificial viscosity can introduce diffusive pieces like the right-hand side of (19.2.1) in many other situations. Consider first the case when D is a constant. Then the equation ∂u ∂2u =D 2 ∂t ∂x
(19.2.3)
can be differenced in the obvious way: n − unj un+1 uj+1 − 2unj + unj−1 j =D ∆t (∆x)2
(19.2.4)
This is the FTCS scheme again, except that it is a second derivative that has been differenced on the right-hand side. But this makes a world of difference! The FTCS scheme was unstable for the hyperbolic equation; however, a quick calculation shows that the amplification factor for equation (19.2.4) is k∆x 4D∆t 2 (19.2.5) sin ξ =1− (∆x)2 2 The requirement |ξ| ≤ 1 leads to the stability criterion 2D∆t ≤1 (∆x)2
(19.2.6)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
19.2 Diffusive Initial Value Problems
19.2 Diffusive Initial Value Problems
839
The physical interpretation of the restriction (19.2.6) is that the maximum allowed timestep is, up to a numerical factor, the diffusion time across a cell of width ∆x. More generally, the diffusion time τ across a spatial scale of size λ is of order λ2 D
(19.2.7)
Usually we are interested in modeling accurately the evolution of features with spatial scales λ ∆x. If we are limited to timesteps satisfying (19.2.6), we will need to evolve through of order λ2 /(∆x)2 steps before things start to happen on the scale of interest. This number of steps is usually prohibitive. We must therefore find a stable way of taking timesteps comparable to, or perhaps — for accuracy — somewhat smaller than, the time scale of (19.2.7). This goal poses an immediate “philosophical” question. Obviously the large timesteps that we propose to take are going to be woefully inaccurate for the small scales that we have decided not to be interested in. We want those scales to do something stable, “innocuous,” and perhaps not too physically unreasonable. We want to build this innocuous behavior into our differencing scheme. What should it be? There are two different answers, each of which has its pros and cons. The first answer is to seek a differencing scheme that drives small-scale features to their equilibrium forms, e.g., satisfying equation (19.2.3) with the left-hand side set to zero. This answer generally makes the best physical sense; but, as we will see, it leads to a differencing scheme (“fully implicit”) that is only first-order accurate in time for the scales that we are interested in. The second answer is to let small-scale features maintain their initial amplitudes, so that the evolution of the larger-scale features of interest takes place superposed with a kind of “frozen in” (though fluctuating) background of small-scale stuff. This answer gives a differencing scheme (“CrankNicholson”) that is second-order accurate in time. Toward the end of an evolution calculation, however, one might want to switch over to some steps of the other kind, to drive the small-scale stuff into equilibrium. Let us now see where these distinct differencing schemes come from: Consider the following differencing of (19.2.3), " n+1 # un+1 uj+1 − 2un+1 − unj + un+1 j j j−1 =D (19.2.8) ∆t (∆x)2 This is exactly like the FTCS scheme (19.2.4), except that the spatial derivatives on the right-hand side are evaluated at timestep n + 1. Schemes with this character are called fully implicit or backward time, by contrast with FTCS (which is called fully explicit). To solve equation (19.2.8) one has to solve a set of simultaneous linear . Fortunately, this is a simple problem because equations at each timestep for the un+1 j the system is tridiagonal: Just group the terms in equation (19.2.8) appropriately: n+1 n −αun+1 − αun+1 j−1 + (1 + 2α)uj j+1 = uj ,
j = 1, 2...J − 1
(19.2.9)
where α≡
D∆t (∆x)2
(19.2.10)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
τ∼
840
Chapter 19.
Partial Differential Equations
∂2u =0 ∂x2
(19.2.11)
What about stability? The amplification factor for equation (19.2.8) is 1
ξ=
2
1 + 4α sin
k∆x 2
(19.2.12)
Clearly |ξ| < 1 for any stepsize ∆t. The scheme is unconditionally stable. The details of the small-scale evolution from the initial conditions are obviously inaccurate for large ∆t. But, as advertised, the correct equilibrium solution is obtained. This is the characteristic feature of implicit methods. Here, on the other hand, is how one gets to the second of our above philosophical answers, combining the stability of an implicit method with the accuracy of a method that is second-order in both space and time. Simply form the average of the explicit and implicit FTCS schemes: un+1 − unj D j = ∆t 2
"
n+1 n n n (un+1 + un+1 j+1 − 2uj j−1 ) + (uj+1 − 2uj + uj−1 ) (∆x)2
#
(19.2.13) Here both the left- and right-hand sides are centered at timestep n + 12 , so the method is second-order accurate in time as claimed. The amplification factor is k∆x 1 − 2α sin 2 ξ= k∆x 1 + 2α sin2 2
2
(19.2.14)
so the method is stable for any size ∆t. This scheme is called the Crank-Nicholson scheme, and is our recommended method for any simple diffusion problem (perhaps supplemented by a few fully implicit steps at the end). (See Figure 19.2.1.) Now turn to some generalizations of the simple diffusion equation (19.2.3). Suppose first that the diffusion coefficient D is not constant, say D = D(x). We can adopt either of two strategies. First, we can make an analytic change of variable Z y=
dx D(x)
(19.2.15)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Supplemented by Dirichlet or Neumann boundary conditions at j = 0 and j = J, equation (19.2.9) is clearly a tridiagonal system, which can easily be solved at each timestep by the method of §2.4. What is the behavior of (19.2.8) for very large timesteps? The answer is seen most clearly in (19.2.9), in the limit α → ∞ (∆t → ∞). Dividing by α, we see that the difference equations are just the finite-difference form of the equilibrium equation
841
19.2 Diffusive Initial Value Problems
t or n
(a)
Fully Implicit
(b)
x or j
(c)
Crank-Nicholson
Figure 19.2.1. Three differencing schemes for diffusive problems (shown as in Figure 19.1.2). (a) Forward Time Center Space is first-order accurate, but stable only for sufficiently small timesteps. (b) Fully Implicit is stable for arbitrarily large timesteps, but is still only first-order accurate. (c) Crank-Nicholson is second-order accurate, and is usually stable for large timesteps.
Then ∂ ∂u ∂u = D(x) ∂t ∂x ∂x
(19.2.16)
1 ∂2u ∂u = ∂t D(y) ∂y2
(19.2.17)
becomes
and we evaluate D at the appropriate yj . Heuristically, the stability criterion (19.2.6) in an explicit scheme becomes " # (∆y)2 (19.2.18) ∆t ≤ min j 2Dj−1 Note that constant spacing ∆y in y does not imply constant spacing in x. An alternative method that does not require analytically tractable forms for D is simply to difference equation (19.2.16) as it stands, centering everything appropriately. Thus the FTCS method becomes − unj un+1 Dj+1/2 (unj+1 − unj ) − Dj−1/2 (unj − unj−1 ) j = ∆t (∆x)2
(19.2.19)
where Dj+1/2 ≡ D(xj+1/2 )
(19.2.20)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
FTCS
842
Chapter 19.
Partial Differential Equations
and the heuristic stability criterion is
(∆x)2 ∆t ≤ min j 2Dj+1/2
(19.2.21)
Dj+1/2 =
1 D(unj+1 ) + D(unj ) 2
(19.2.22)
Implicit schemes are not as easy. The replacement (19.2.22) with n → n + 1 leaves us with a nasty set of coupled nonlinear equations to solve at each timestep. Often there is an easier way: If the form of D(u) allows us to integrate dz = D(u)du
(19.2.23)
analytically for z(u), then the right-hand side of (19.2.1) becomes ∂ 2 z/∂x2 , which we difference implicitly as n+1 n+1 − 2zjn+1 + zj−1 zj+1 (∆x)2
(19.2.24)
Now linearize each term on the right-hand side of equation (19.2.24), for example zjn+1 ≡ z(un+1 ) = z(unj ) + (un+1 − unj ) j j
∂z ∂u j,n
(19.2.25)
= z(unj ) + (un+1 − unj )D(unj ) j This reduces the problem to tridiagonal form again and in practice usually retains the stability advantages of fully implicit differencing.
Schrodinger Equation ¨ Sometimes the physical problem being solved imposes constraints on the differencing scheme that we have not yet taken into account. For example, consider the time-dependent Schr¨odinger equation of quantum mechanics. This is basically a parabolic equation for the evolution of a complex quantity ψ. For the scattering of a wavepacket by a one-dimensional potential V (x), the equation has the form i
∂2 ψ ∂ψ = − 2 + V (x)ψ ∂t ∂x
(19.2.26)
(Here we have chosen units so that Planck’s constant ¯h = 1 and the particle mass m = 1/2.) One is given the initial wavepacket, ψ(x, t = 0), together with boundary
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The Crank-Nicholson method can be generalized similarly. The second complication one can consider is a nonlinear diffusion problem, for example where D = D(u). Explicit schemes can be generalized in the obvious way. For example, in equation (19.2.19) write
843
19.2 Diffusive Initial Value Problems
conditions that ψ → 0 at x → ±∞. Suppose we content ourselves with firstorder accuracy in time, but want to use an implicit scheme, for stability. A slight generalization of (19.2.8) leads to "
#
"
# n+1 n+1 ψj+1 − 2ψjn+1 + ψj−1 =− + Vj ψjn+1 (∆x)2
(19.2.27)
for which ξ=
1 k∆x 4∆t 2 + Vj ∆t 1+i sin (∆x)2 2
(19.2.28)
This is unconditionally stable, but unfortunately is not unitary. The underlying physical problem requires that the total probability of finding the particle somewhere remains unity. This is represented formally by the modulus-square norm of ψ remaining unity: Z
∞ −∞
|ψ|2 dx = 1
(19.2.29)
The initial wave function ψ(x, 0) is normalized to satisfy (19.2.29). The Schr¨odinger equation (19.2.26) then guarantees that this condition is satisfied at all later times. Let us write equation (19.2.26) in the form i
∂ψ = Hψ ∂t
(19.2.30)
where the operator H is H =−
∂2 + V (x) ∂x2
(19.2.31)
The formal solution of equation (19.2.30) is ψ(x, t) = e−iHt ψ(x, 0)
(19.2.32)
where the exponential of the operator is defined by its power series expansion. The unstable explicit FTCS scheme approximates (19.2.32) as ψjn+1 = (1 − iH∆t)ψjn
(19.2.33)
where H is represented by a centered finite-difference approximation in x. The stable implicit scheme (19.2.27) is, by contrast, ψjn+1 = (1 + iH∆t)−1 ψjn
(19.2.34)
These are both first-order accurate in time, as can be seen by expanding equation (19.2.32). However, neither operator in (19.2.33) or (19.2.34) is unitary.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
ψjn+1 − ψjn i ∆t
844
Chapter 19.
Partial Differential Equations
The correct way to difference Schr¨odinger’s equation [1,2] is to use Cayley’s form for the finite-difference representation of e−iHt , which is second-order accurate and unitary: e−iHt '
(19.2.35)
In other words, 1 + 12 iH∆t ψjn+1 = 1 − 12 iH∆t ψjn
(19.2.36)
On replacing H by its finite-difference approximation in x, we have a complex tridiagonal system to solve. The method is stable, unitary, and second-order accurate in space and time. In fact, it is simply the Crank-Nicholson method once again! CITED REFERENCES AND FURTHER READING: Ames, W.F. 1977, Numerical Methods for Partial Differential Equations, 2nd ed. (New York: Academic Press), Chapter 2. Goldberg, A., Schey, H.M., and Schwartz, J.L. 1967, American Journal of Physics, vol. 35, pp. 177–186. [1] Galbraith, I., Ching, Y.S., and Abraham, E. 1984, American Journal of Physics, vol. 52, pp. 60– 68. [2]
19.3 Initial Value Problems in Multidimensions The methods described in §19.1 and §19.2 for problems in 1 + 1 dimension (one space and one time dimension) can easily be generalized to N + 1 dimensions. However, the computing power necessary to solve the resulting equations is enormous. If you have solved a one-dimensional problem with 100 spatial grid points, solving the two-dimensional version with 100 × 100 mesh points requires at least 100 times as much computing. You generally have to be content with very modest spatial resolution in multidimensional problems. Indulge us in offering a bit of advice about the development and testing of multidimensional PDE codes: You should always first run your programs on very small grids, e.g., 8 × 8, even though the resulting accuracy is so poor as to be useless. When your program is all debugged and demonstrably stable, then you can increase the grid size to a reasonable one and start looking at the results. We have actually heard someone protest, “my program would be unstable for a crude grid, but I am sure the instability will go away on a larger grid.” That is nonsense of a most pernicious sort, evidencing total confusion between accuracy and stability. In fact, new instabilities sometimes do show up on larger grids; but old instabilities never (in our experience) just go away. Forced to live with modest grid sizes, some people recommend going to higherorder methods in an attempt to improve accuracy. This is very dangerous. Unless the solution you are looking for is known to be smooth, and the high-order method you
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1 − 12 iH∆t 1 + 12 iH∆t
19.3 Initial Value Problems in Multidimensions
845
Lax Method for a Flux-Conservative Equation As an example, we show how to generalize the Lax method (19.1.15) to two dimensions for the conservation equation ∂u = −∇ · F = − ∂t
∂Fx ∂Fy + ∂x ∂y
(19.3.1)
Use a spatial grid with xj = x0 + j∆ (19.3.2)
yl = y0 + l∆ We have chosen ∆x = ∆y ≡ ∆ for simplicity. Then the Lax scheme is un+1 j,l =
1 n (u + unj−1,l + unj,l+1 + unj,l−1 ) 4 j+1,l ∆t n n n n (F − − Fj−1,l + Fj,l+1 − Fj,l−1 ) 2∆ j+1,l
(19.3.3)
Note that as an abbreviated notation Fj+1 and Fj−1 refer to Fx, while Fl+1 and Fl−1 refer to Fy . Let us carry out a stability analysis for the model advective equation (analog of 19.1.6) with Fx = vx u,
Fy = vy u
(19.3.4)
This requires an eigenmode with two dimensions in space, though still only a simple dependence on powers of ξ in time, unj,l = ξ n eikx j∆ eiky l∆
(19.3.5)
Substituting in equation (19.3.3), we find ξ=
1 (cos kx∆ + cos ky ∆) − iαx sin kx∆ − iαy sin ky ∆ 2
where αx =
vx ∆t , ∆
αy =
vy ∆t ∆
(19.3.6) (19.3.7)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
are using is known to be extremely stable, we do not recommend anything higher than second-order in time (for sets of first-order equations). For spatial differencing, we recommend the order of the underlying PDEs, perhaps allowing second-order spatial differencing for first-order-in-space PDEs. When you increase the order of a differencing method to greater than the order of the original PDEs, you introduce spurious solutions to the difference equations. This does not create a problem if they all happen to decay exponentially; otherwise you are going to see all hell break loose!
846
Chapter 19.
Partial Differential Equations
The expression for |ξ|2 can be manipulated into the form |ξ|2 = 1 − (sin2 kx∆ + sin2 ky ∆)
1 − (α2x + α2y ) 2
(19.3.8)
The last two terms are negative, and so the stability requirement |ξ|2 ≤ 1 becomes 1 − (α2x + α2y ) ≥ 0 2 ∆ ∆t ≤ √ 2(vx2 + vy2 )1/2
or
(19.3.9) (19.3.10)
This is an example of the general result for the N -dimensional Courant condition: If |v| is the maximum propagation velocity in the problem, then ∆t ≤ √
∆ N |v|
(19.3.11)
is the Courant condition.
Diffusion Equation in Multidimensions Let us consider the two-dimensional diffusion equation, ∂u =D ∂t
∂2u ∂2u + 2 ∂x2 ∂y
(19.3.12)
An explicit method, such as FTCS, can be generalized from the one-dimensional case in the obvious way. However, we have seen that diffusive problems are usually best treated implicitly. Suppose we try to implement the Crank-Nicholson scheme in two dimensions. This would give us
Here
1 2 n+1 n 2 n 2 n+1 2 n un+1 j,l = uj,l + α δx uj,l + δx uj,l + δy uj,l + δy uj,l 2 α≡
D∆t ∆2
∆ ≡ ∆x = ∆y
δx2 unj,l ≡ unj+1,l − 2unj,l + unj−1,l
(19.3.13) (19.3.14) (19.3.15)
and similarly for δy2 unj,l . This is certainly a viable scheme; the problem arises in solving the coupled linear equations. Whereas in one space dimension the system was tridiagonal, that is no longer true, though the matrix is still very sparse. One possibility is to use a suitable sparse matrix technique (see §2.7 and §19.0). Another possibility, which we generally prefer, is a slightly different way of generalizing the Crank-Nicholson algorithm. It is still second-order accurate in time and space, and unconditionally stable, but the equations are easier to solve than
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1 − (cos kx∆ − cos ky ∆)2 − (αy sin kx ∆ − αx sin ky ∆)2 4
19.3 Initial Value Problems in Multidimensions
847
(19.3.13). Called the alternating-direction implicit method (ADI), this embodies the powerful concept of operator splitting or time splitting, about which we will say more below. Here, the idea is to divide each timestep into two steps of size ∆t/2. In each substep, a different dimension is treated implicitly:
un+1 j,l
1 n+1/2 = unj,l + α δx2 uj,l + δy2 unj,l 2 1 n+1/2 n+1/2 = uj,l + α δx2 uj,l + δy2 un+1 j,l 2
(19.3.16)
The advantage of this method is that each substep requires only the solution of a simple tridiagonal system.
Operator Splitting Methods Generally The basic idea of operator splitting, which is also called time splitting or the method of fractional steps, is this: Suppose you have an initial value equation of the form ∂u = Lu ∂t
(19.3.17)
where L is some operator. While L is not necessarily linear, suppose that it can at least be written as a linear sum of m pieces, which act additively on u, Lu = L1 u + L2 u + · · · + Lm u
(19.3.18)
Finally, suppose that for each of the pieces, you already know a differencing scheme for updating the variable u from timestep n to timestep n + 1, valid if that piece of the operator were the only one on the right-hand side. We will write these updatings symbolically as un+1 = U1 (un , ∆t) un+1 = U2 (un , ∆t) ···
(19.3.19)
un+1 = Um (un , ∆t) Now, one form of operator splitting would be to get from n to n + 1 by the following sequence of updatings: un+(1/m) = U1 (un , ∆t) un+(2/m) = U2 (un+(1/m) , ∆t) ··· un+1 = Um (un+(m−1)/m , ∆t)
(19.3.20)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
n+1/2
uj,l
848
Chapter 19.
Partial Differential Equations
For example, a combined advective-diffusion equation, such as ∂u ∂2 u ∂u = −v +D 2 ∂t ∂x ∂x
(19.3.21)
un+1/m = U1 (un , ∆t/m) un+2/m = U2 (un+1/m , ∆t/m) (19.3.22)
··· un+1 = Um (un+(m−1)/m , ∆t/m)
The timestep for each fractional step in (19.3.22) is now only 1/m of the full timestep, because each partial operation acts with all the terms of the original operator. Equation (19.3.22) is usually, though not always, stable as a differencing scheme for the operator L. In fact, as a rule of thumb, it is often sufficient to have stable Ui ’s only for the operator pieces having the highest number of spatial derivatives — the other Ui ’s can be unstable — to make the overall scheme stable! It is at this point that we turn our attention from initial value problems to boundary value problems. These will occupy us for the remainder of the chapter. CITED REFERENCES AND FURTHER READING: Ames, W.F. 1977, Numerical Methods for Partial Differential Equations, 2nd ed. (New York: Academic Press).
19.4 Fourier and Cyclic Reduction Methods for Boundary Value Problems As discussed in §19.0, most boundary value problems (elliptic equations, for example) reduce to solving large sparse linear systems of the form A·u=b
(19.4.1)
either once, for boundary value equations that are linear, or iteratively, for boundary value equations that are nonlinear.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
might profitably use an explicit scheme for the advective term combined with a Crank-Nicholson or other implicit scheme for the diffusion term. The alternating-direction implicit (ADI) method, equation (19.3.16), is an example of operator splitting with a slightly different twist. Let us reinterpret (19.3.19) to have a different meaning: Let U1 now denote an updating method that includes algebraically all the pieces of the total operator L, but which is desirably stable only for the L1 piece; likewise U2 , . . . Um . Then a method of getting from un to un+1 is
19.4 Fourier and Cyclic Reduction Methods
849
Fourier Transform Method The discrete inverse Fourier transform in both x and y is
ujl =
J−1 L−1 1 XX u bmn e−2πijm/J e−2πiln/L JL m=0 n=0
(19.4.2)
This can be computed using the FFT independently in each dimension, or else all at once via the routine fourn of §12.4 or the routine rlft3 of §12.5. Similarly,
ρjl =
J−1 L−1 1 XX ρbmn e−2πijm/J e−2πiln/L JL m=0 n=0
(19.4.3)
If we substitute expressions (19.4.2) and (19.4.3) in our model problem (19.0.6), we find u bmn e2πim/J + e−2πim/J + e2πin/L + e−2πin/L − 4 = ρbmn ∆2 or u bmn =
ρbmn ∆2 2πn 2πm + cos −2 2 cos J L
(19.4.4) (19.4.5)
Thus the strategy for solving equation (19.0.6) by FFT techniques is: • Compute ρbmn as the Fourier transform ρbmn =
J−1 X L−1 X
ρjl e2πimj/J e2πinl/L
j=0 l=0
• Compute u bmn from equation (19.4.5).
(19.4.6)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Two important techniques lead to “rapid” solution of equation (19.4.1) when the sparse matrix is of certain frequently occurring forms. The Fourier transform method is directly applicable when the equations have coefficients that are constant in space. The cyclic reduction method is somewhat more general; its applicability is related to the question of whether the equations are separable (in the sense of “separation of variables”). Both methods require the boundaries to coincide with the coordinate lines. Finally, for some problems, there is a powerful combination of these two methods called FACR (Fourier Analysis and Cyclic Reduction). We now consider each method in turn, using equation (19.0.3), with finite-difference representation (19.0.6), as a model example. Generally speaking, the methods in this section are faster, when they apply, than the simpler relaxation methods discussed in §19.5; but they are not necessarily faster than the more complicated multigrid methods discussed in §19.6.
850
Chapter 19.
Partial Differential Equations
• Compute ujl by the inverse Fourier transform (19.4.2). The above procedure is valid for periodic boundary conditions. In other words, the solution satisfies ujl = uj+J,l = uj,l+L
(19.4.7)
ujl =
J−1 L−1 πln 2 2 XX πjm sin u bmn sin J L m=1 n=1 J L
(19.4.8)
This satisfies the boundary conditions that u = 0 at j = 0, J and at l = 0, L. If we substitute this expansion and the analogous one for ρjl into equation (19.0.6), we find that the solution procedure parallels that for periodic boundary conditions: • Compute ρbmn by the sine transform ρbmn =
J−1 X L−1 X
ρjl sin
j=1 l=1
πln πjm sin J L
(19.4.9)
(A fast sine transform algorithm was given in §12.3.) • Compute u bmn from the expression analogous to (19.4.5), u bmn =
∆2 ρbmn πn πm + cos −2 2 cos J L
(19.4.10)
• Compute ujl by the inverse sine transform (19.4.8). If we have inhomogeneous boundary conditions, for example u = 0 on all boundaries except u = f(y) on the boundary x = J∆, we have to add to the above solution a solution uH of the homogeneous equation ∂2u ∂2u + 2 =0 ∂x2 ∂y
(19.4.11)
that satisfies the required boundary conditions. In the continuum case, this would be an expression of the form uH =
X n
An sinh
nπy nπx sin J∆ L∆
(19.4.12)
where An would be found by requiring that u = f(y) at x = J∆. In the discrete case, we have uH jl =
L−1 πnl 2 X πnj sin An sinh L n=1 J L
(19.4.13)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Next consider a Dirichlet boundary condition u = 0 on the rectangular boundary. Instead of the expansion (19.4.2), we now need an expansion in sine waves:
19.4 Fourier and Cyclic Reduction Methods
851
If f(y = l∆) ≡ fl , then we get An from the inverse formula An =
L−1 X 1 πnl fl sin sinh πn L
(19.4.14)
l=1
u = ujl + uH jl
(19.4.15)
By adding appropriate terms of the form (19.4.12), we can handle inhomogeneous terms on any boundary surface. A much simpler procedure for handling inhomogeneous terms is to note that whenever boundary terms appear on the left-hand side of (19.0.6), they can be taken over to the right-hand side since they are known. The effective source term is therefore ρjl plus a contribution from the boundary terms. To implement this idea formally, write the solution as u = u0 + uB
(19.4.16)
where u0 = 0 on the boundary, while uB vanishes everywhere except on the boundary. There it takes on the given boundary value. In the above example, the only nonzero values of uB would be uB J,l = fl
(19.4.17)
The model equation (19.0.3) becomes ∇2 u0 = −∇2 uB + ρ
(19.4.18)
or, in finite-difference form, u0j+1,l + u0j−1,l + u0j,l+1 + u0j,l−1 − 4u0j,l = B B B B 2 − (uB j+1,l + uj−1,l + uj,l+1 + uj,l−1 − 4uj,l ) + ∆ ρj,l
(19.4.19)
All the uB terms in equation (19.4.19) vanish except when the equation is evaluated at j = J − 1, where u0J,l + u0J−2,l + u0J−1,l+1 + u0J−1,l−1 − 4u0J−1,l = −fl + ∆2 ρJ−1,l
(19.4.20)
Thus the problem is now equivalent to the case of zero boundary conditions, except that one row of the source term is modified by the replacement ∆2 ρJ−1,l → ∆2 ρJ−1,l − fl
(19.4.21)
The case of Neumann boundary conditions ∇u = 0 is handled by the cosine expansion (12.3.17): ujl =
L J πln 2 2 X00 X00 πjm cos u bmn cos J L m=0 n=0 J L
(19.4.22)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The complete solution to the problem is
852
Chapter 19.
Partial Differential Equations
Here the double prime notation means that the terms for m = 0 and m = J should be multiplied by 12 , and similarly for n = 0 and n = L. Inhomogeneous terms ∇u = g can be again included by adding a suitable solution of the homogeneous equation, or more simply by taking boundary terms over to the right-hand side. For example, the condition
becomes
at x = 0
u1,l − u−1,l = gl 2∆
(19.4.23)
(19.4.24)
where gl ≡ g(y = l∆). Once again we write the solution in the form (19.4.16), where now ∇u0 = 0 on the boundary. This time ∇uB takes on the prescribed value on the boundary, but uB vanishes everywhere except just outside the boundary. Thus equation (19.4.24) gives uB −1,l = −2∆gl
(19.4.25)
All the uB terms in equation (19.4.19) vanish except when j = 0: u01,l + u0−1,l + u00,l+1 + u00,l−1 − 4u00,l = 2∆gl + ∆2 ρ0,l
(19.4.26)
Thus u0 is the solution of a zero-gradient problem, with the source term modified by the replacement ∆2 ρ0,l → ∆2 ρ0,l + 2∆gl
(19.4.27)
Sometimes Neumann boundary conditions are handled by using a staggered grid, with the u’s defined midway between zone boundaries so that first derivatives are centered on the mesh points. You can solve such problems using similar techniques to those described above if you use the alternative form of the cosine transform, equation (12.3.23).
Cyclic Reduction Evidently the FFT method works only when the original PDE has constant coefficients, and boundaries that coincide with the coordinate lines. An alternative algorithm, which can be used on somewhat more general equations, is called cyclic reduction (CR). We illustrate cyclic reduction on the equation ∂u ∂2u ∂2u + c(y)u = g(x, y) + 2 + b(y) 2 ∂x ∂y ∂y
(19.4.28)
This form arises very often in practice from the Helmholtz or Poisson equations in polar, cylindrical, or spherical coordinate systems. More general separable equations are treated in [1].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
∂u = g(y) ∂x
853
19.4 Fourier and Cyclic Reduction Methods
The finite-difference form of equation (19.4.28) can be written as a set of vector equations uj−1 + T · uj + uj+1 = gj ∆2
(19.4.29)
T = B − 21
(19.4.30)
where the 21 comes from the x-differencing and the matrix B from the y-differencing. The matrix B, and hence T, is tridiagonal with variable coefficients. The CR method is derived by writing down three successive equations like (19.4.29): uj−2 + T · uj−1 + uj = gj−1 ∆2 uj−1 + T · uj + uj+1 = gj ∆2 uj + T · uj+1 + uj+2 = gj+1 ∆
(19.4.31) 2
Matrix-multiplying the middle equation by −T and then adding the three equations, we get (1)
uj−2 + T(1) · uj + uj+2 = gj ∆2
(19.4.32)
This is an equation of the same form as (19.4.29), with T(1) = 21 − T2 (1)
gj
= ∆2 (gj−1 − T · gj + gj+1 )
(19.4.33)
After one level of CR, we have reduced the number of equations by a factor of two. Since the resulting equations are of the same form as the original equation, we can repeat the process. Taking the number of mesh points to be a power of 2 for simplicity, we finally end up with a single equation for the central line of variables: (f)
T(f) · uJ/2 = ∆2 gJ/2 − u0 − uJ
(19.4.34)
Here we have moved u0 and uJ to the right-hand side because they are known boundary values. Equation (19.4.34) can be solved for uJ/2 by the standard tridiagonal algorithm. The two equations at level f − 1 involve uJ/4 and u3J/4 . The equation for uJ/4 involves u0 and uJ/2 , both of which are known, and hence can be solved by the usual tridiagonal routine. A similar result holds true at every stage, so we end up solving J − 1 tridiagonal systems. In practice, equations (19.4.33) should be rewritten to avoid numerical instability. For these and other practical details, refer to [2].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Here the index j comes from differencing in the x-direction, while the y-differencing (denoted by the index l previously) has been left in vector form. The matrix T has the form
854
Chapter 19.
Partial Differential Equations
FACR Method
(r)
(r)k
bkj + u bkj+2r = ∆2 gj u bkj−2r + λk u
(19.4.35)
(r)
Here λk is the eigenvalue of T(r) corresponding to the kth Fourier mode. For (r) the equation (19.0.3), equation (19.4.5) shows that λk will involve terms like cos(2πk/L) − 2 raised to a power. Solve the tridiagonal systems for u bkj at the levels r r r r j = 2 , 2 × 2 , 4 × 2 , ..., J − 2 . Fourier synthesize to get the y-values on these x-lines. Then fill in the intermediate x-lines as in the original CR algorithm. The trick is to choose the number of levels of CR so as to minimize the total number of arithmetic operations. One can show that for a typical case of a 128×128 mesh, the optimal level is r = 2; asymptotically, r → log2 (log2 J). A rough estimate of running times for these algorithms for equation (19.0.3) is as follows: The FFT method (in both x and y) and the CR method are roughly comparable. FACR with r = 0 (that is, FFT in one dimension and solve the tridiagonal equations by the usual algorithm in the other dimension) gives about a factor of two gain in speed. The optimal FACR with r = 2 gives another factor of two gain in speed. CITED REFERENCES AND FURTHER READING: Swartzrauber, P.N. 1977, SIAM Review, vol. 19, pp. 490–501. [1] Buzbee, B.L, Golub, G.H., and Nielson, C.W. 1970, SIAM Journal on Numerical Analysis, vol. 7, pp. 627–656; see also op. cit. vol. 11, pp. 753–763. [2] Hockney, R.W. 1965, Journal of the Association for Computing Machinery, vol. 12, pp. 95–113. [3] Hockney, R.W. 1970, in Methods of Computational Physics, vol. 9 (New York: Academic Press), pp. 135–211. [4] Hockney, R.W., and Eastwood, J.W. 1981, Computer Simulation Using Particles (New York: McGraw-Hill), Chapter 6. [5] Temperton, C. 1980, Journal of Computational Physics, vol. 34, pp. 314–329. [6]
19.5 Relaxation Methods for Boundary Value Problems As we mentioned in §19.0, relaxation methods involve splitting the sparse matrix that arises from finite differencing and then iterating until a solution is found. There is another way of thinking about relaxation methods that is somewhat more physical. Suppose we wish to solve the elliptic equation Lu = ρ
(19.5.1)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The best way to solve equations of the form (19.4.28), including the constant coefficient problem (19.0.3), is a combination of Fourier analysis and cyclic reduction, the FACR method [3-6]. If at the rth stage of CR we Fourier analyze the equations of the form (19.4.32) along y, that is, with respect to the suppressed vector index, we will have a tridiagonal system in the x-direction for each y-Fourier mode:
19.5 Relaxation Methods for Boundary Value Problems
855
where L represents some elliptic operator and ρ is the source term. Rewrite the equation as a diffusion equation, ∂u = Lu − ρ ∂t
(19.5.2)
∂u ∂2u ∂2u = + 2 −ρ ∂t ∂x2 ∂y
(19.5.3)
If we use FTCS differencing (cf. equation 19.2.4), we get n un+1 j,l = uj,l +
∆t n uj+1,l + unj−1,l + unj,l+1 + unj,l−1 − 4unj,l − ρj,l ∆t (19.5.4) 2 ∆
Recall from (19.2.6) that FTCS differencing is stable in one spatial dimension only if ∆t/∆2 ≤ 12 . In two dimensions this becomes ∆t/∆2 ≤ 14 . Suppose we try to take the largest possible timestep, and set ∆t = ∆2 /4. Then equation (19.5.4) becomes un+1 j,l =
∆2 1 n uj+1,l + unj−1,l + unj,l+1 + unj,l−1 − ρj,l 4 4
(19.5.5)
Thus the algorithm consists of using the average of u at its four nearest-neighbor points on the grid (plus the contribution from the source). This procedure is then iterated until convergence. This method is in fact a classical method with origins dating back to the last century, called Jacobi’s method (not to be confused with the Jacobi method for eigenvalues). The method is not practical because it converges too slowly. However, it is the basis for understanding the modern methods, which are always compared with it. Another classical method is the Gauss-Seidel method, which turns out to be important in multigrid methods (§19.6). Here we make use of updated values of u on the right-hand side of (19.5.5) as soon as they become available. In other words, the averaging is done “in place” instead of being “copied” from an earlier timestep to a later one. If we are proceeding along the rows, incrementing j for fixed l, we have un+1 j,l =
∆2 1 n n n+1 ρj,l uj+1,l + un+1 + u + u j,l+1 j−1,l j,l−1 − 4 4
(19.5.6)
This method is also slowly converging and only of theoretical interest when used by itself, but some analysis of it will be instructive. Let us look at the Jacobi and Gauss-Seidel methods in terms of the matrix splitting concept. We change notation and call u “x,” to conform to standard matrix notation. To solve A·x=b
(19.5.7)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
An initial distribution u relaxes to an equilibrium solution as t → ∞. This equilibrium has all time derivatives vanishing. Therefore it is the solution of the original elliptic problem (19.5.1). We see that all the machinery of §19.2, on diffusive initial value equations, can be brought to bear on the solution of boundary value problems by relaxation methods. Let us apply this idea to our model problem (19.0.3). The diffusion equation is
856
Chapter 19.
Partial Differential Equations
we can consider splitting A as A =L+D+U
(19.5.8)
D · x(r) = −(L + U) · x(r−1) + b
(19.5.9)
For our model problem (19.5.5), D is simply the identity matrix. The Jacobi method converges for matrices A that are “diagonally dominant” in a sense that can be made mathematically precise. For matrices arising from finite differencing, this condition is usually met. What is the rate of convergence of the Jacobi method? A detailed analysis is beyond our scope, but here is some of the flavor: The matrix −D−1 · (L + U) is the iteration matrix which, apart from an additive term, maps one set of x’s into the next. The iteration matrix has eigenvalues, each one of which reflects the factor by which the amplitude of a particular eigenmode of undesired residual is suppressed during one iteration. Evidently those factors had better all have modulus < 1 for the relaxation to work at all! The rate of convergence of the method is set by the rate for the slowest-decaying eigenmode, i.e., the factor with largest modulus. The modulus of this largest factor, therefore lying between 0 and 1, is called the spectral radius of the relaxation operator, denoted ρs . The number of iterations r required to reduce the overall error by a factor 10−p is thus estimated by r≈
p ln 10 (− ln ρs )
(19.5.10)
In general, the spectral radius ρs goes asymptotically to the value 1 as the grid size J is increased, so that more iterations are required. For any given equation, grid geometry, and boundary condition, the spectral radius can, in principle, be computed analytically. For example, for equation (19.5.5) on a J × J grid with Dirichlet boundary conditions on all four sides, the asymptotic formula for large J turns out to be ρs ' 1 −
π2 2J 2
(19.5.11)
The number of iterations r required to reduce the error by a factor of 10−p is thus r'
1 2pJ 2 ln 10 ' pJ 2 π2 2
(19.5.12)
In other words, the number of iterations is proportional to the number of mesh points, J 2 . Since 100 × 100 and larger problems are common, it is clear that the Jacobi method is only of theoretical interest.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where D is the diagonal part of A, L is the lower triangle of A with zeros on the diagonal, and U is the upper triangle of A with zeros on the diagonal. In the Jacobi method we write for the rth step of iteration
19.5 Relaxation Methods for Boundary Value Problems
857
The Gauss-Seidel method, equation (19.5.6), corresponds to the matrix decomposition (L + D) · x(r) = −U · x(r−1) + b
(19.5.13)
ρs ' 1 − r'
π2 J2
1 pJ 2 ln 10 ' pJ 2 π2 4
(19.5.14)
(19.5.15)
The factor of two improvement in the number of iterations over the Jacobi method still leaves the method impractical.
Successive Overrelaxation (SOR) We get a better algorithm — one that was the standard algorithm until the 1970s — if we make an overcorrection to the value of x(r) at the rth stage of Gauss-Seidel iteration, thus anticipating future corrections. Solve (19.5.13) for x(r) , add and subtract x(r−1) on the right-hand side, and hence write the Gauss-Seidel method as x(r) = x(r−1) − (L + D)−1 · [(L + D + U) · x(r−1) − b]
(19.5.16)
The term in square brackets is just the residual vector ξ (r−1) , so x(r) = x(r−1) − (L + D)−1 · ξ (r−1)
(19.5.17)
Now overcorrect, defining x(r) = x(r−1) − ω(L + D)−1 · ξ (r−1)
(19.5.18)
Here ω is called the overrelaxation parameter, and the method is called successive overrelaxation (SOR). The following theorems can be proved [1-3] : • The method is convergent only for 0 < ω < 2. If 0 < ω < 1, we speak of underrelaxation. • Under certain mathematical restrictions generally satisfied by matrices arising from finite differencing, only overrelaxation (1 < ω < 2 ) can give faster convergence than the Gauss-Seidel method. • If ρJacobi is the spectral radius of the Jacobi iteration (so that the square of it is the spectral radius of the Gauss-Seidel iteration), then the optimal choice for ω is given by ω=
1+
p
2 1 − ρ2Jacobi
(19.5.19)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The fact that L is on the left-hand side of the equation follows from the updating in place, as you can easily check if you write out (19.5.13) in components. One can show [1-3] that the spectral radius is just the square of the spectral radius of the Jacobi method. For our model problem, therefore,
858
Chapter 19.
Partial Differential Equations
• For this optimal choice, the spectral radius for SOR is ρSOR =
ρJacobi p 1 + 1 − ρ2Jacobi
!2 (19.5.20)
2 1 + π/J 2π '1− J
ω' ρSOR
(19.5.21) for large J
(19.5.22)
Equation (19.5.10) gives for the number of iterations to reduce the initial error by a factor of 10−p , r'
1 pJ ln 10 ' pJ 2π 3
(19.5.23)
Comparing with equation (19.5.12) or (19.5.15), we see that optimal SOR requires of order J iterations, as opposed to of order J 2 . Since J is typically 100 or larger, this makes a tremendous difference! Equation (19.5.23) leads to the mnemonic that 3-figure accuracy (p = 3) requires a number of iterations equal to the number of mesh points along a side of the grid. For 6-figure accuracy, we require about twice as many iterations. How do we choose ω for a problem for which the answer is not known analytically? That is just the weak point of SOR! The advantages of SOR obtain only in a fairly narrow window around the correct value of ω. It is better to take ω slightly too large, rather than slightly too small, but best to get it right. One way to choose ω is to map your problem approximately onto a known problem, replacing the coefficients in the equation by average values. Note, however, that the known problem must have the same grid size and boundary conditions as the actual problem. We give for reference purposes the value of ρJacobi for our model problem on a rectangular J × L grid, allowing for the possibility that ∆x 6= ∆y: cos ρJacobi =
2 π ∆x cos ∆y L 2 ∆x 1+ ∆y
π + J
(19.5.24)
Equation (19.5.24) holds for homogeneous Dirichlet or Neumann boundary conditions. For periodic boundary conditions, make the replacement π → 2π. A second way, which is especially useful if you plan to solve many similar elliptic equations each time with slightly different coefficients, is to determine the optimum value ω empirically on the first equation and then use that value for the remaining equations. Various automated schemes for doing this and for “seeking out” the best values of ω are described in the literature. While the matrix notation introduced earlier is useful for theoretical analyses, for practical implementation of the SOR algorithm we need explicit formulas.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
As an application of the above results, consider our model problem for which ρJacobi is given by equation (19.5.11). Then equations (19.5.19) and (19.5.20) give
19.5 Relaxation Methods for Boundary Value Problems
859
Consider a general second-order elliptic equation in x and y, finite differenced on a square as for our model equation. Corresponding to each row of the matrix A is an equation of the form aj,l uj+1,l + bj,l uj−1,l + cj,l uj,l+1 + dj,l uj,l−1 + ej,l uj,l = fj,l
(19.5.25)
u*j,l =
1 (fj,l − aj,l uj+1,l − bj,l uj−1,l − cj,l uj,l+1 − dj,l uj,l−1 ) ej,l
(19.5.26)
Then unew j,l is a weighted average old unew j,l = ωu*j,l + (1 − ω)uj,l
(19.5.27)
We calculate it as follows: The residual at any stage is ξj,l = aj,l uj+1,l + bj,l uj−1,l + cj,l uj,l+1 + dj,l uj,l−1 + ej,l uj,l − fj,l (19.5.28) and the SOR algorithm (19.5.18) or (19.5.27) is old unew j,l = uj,l − ω
ξj,l ej,l
(19.5.29)
This formulation is very easy to program, and the norm of the residual vector ξj,l can be used as a criterion for terminating the iteration. Another practical point concerns the order in which mesh points are processed. The obvious strategy is simply to proceed in order down the rows (or columns). Alternatively, suppose we divide the mesh into “odd” and “even” meshes, like the red and black squares of a checkerboard. Then equation (19.5.26) shows that the odd points depend only on the even mesh values and vice versa. Accordingly, we can carry out one half-sweep updating the odd points, say, and then another half-sweep updating the even points with the new odd values. For the version of SOR implemented below, we shall adopt odd-even ordering. The last practical point is that in practice the asymptotic rate of convergence in SOR is not attained until of order J iterations. The error often grows by a factor of 20 before convergence sets in. A trivial modification to SOR resolves this problem. It is based on the observation that, while ω is the optimum asymptotic relaxation parameter, it is not necessarily a good initial choice. In SOR with Chebyshev acceleration, one uses odd-even ordering and changes ω at each halfsweep according to the following prescription: ω(0) = 1 ω(1/2) = 1/(1 − ρ2Jacobi /2) ω(n+1/2) = 1/(1 − ρ2Jacobi ω(n) /4), ω(∞) → ωoptimal
n = 1/2, 1, ..., ∞
(19.5.30)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
For our model equation, we had a = b = c = d = 1, e = −4. The quantity f is proportional to the source term. The iterative procedure is defined by solving (19.5.25) for uj,l :
860
Chapter 19.
Partial Differential Equations
The beauty of Chebyshev acceleration is that the norm of the error always decreases with each iteration. (This is the norm of the actual error in uj,l. The norm of the residual ξj,l need not decrease monotonically.) While the asymptotic rate of convergence is the same as ordinary SOR, there is never any excuse for not using Chebyshev acceleration to reduce the total number of iterations required.
* *
*
* *
SUBROUTINE sor(a,b,c,d,e,f,u,jmax,rjac) INTEGER jmax,MAXITS DOUBLE PRECISION rjac,a(jmax,jmax),b(jmax,jmax), c(jmax,jmax),d(jmax,jmax),e(jmax,jmax), f(jmax,jmax),u(jmax,jmax),EPS PARAMETER (MAXITS=1000,EPS=1.d-5) Successive overrelaxation solution of equation (19.5.25) with Chebyshev acceleration. a, b, c, d, e, and f are input as the coefficients of the equation, each dimensioned to the grid size JMAX × JMAX. u is input as the initial guess to the solution, usually zero, and returns with the final value. rjac is input as the spectral radius of the Jacobi iteration, or an estimate of it. INTEGER ipass,j,jsw,l,lsw,n DOUBLE PRECISION anorm,anormf, omega,resid Double precision is a good idea for JMAX bigger than about 25. anormf=0.d0 Compute initial norm of residual and terminate iteration when do 12 j=2,jmax-1 norm has been reduced by a factor EPS. do 11 l=2,jmax-1 anormf=anormf+abs(f(j,l)) Assumes initial u is zero. enddo 11 enddo 12 omega=1.d0 do 16 n=1,MAXITS anorm=0.d0 jsw=1 do 15 ipass=1,2 Odd-even ordering. lsw=jsw do 14 j=2,jmax-1 do 13 l=lsw+1,jmax-1,2 resid=a(j,l)*u(j+1,l)+b(j,l)*u(j-1,l)+ c(j,l)*u(j,l+1)+d(j,l)*u(j,l-1)+ e(j,l)*u(j,l)-f(j,l) anorm=anorm+abs(resid) u(j,l)=u(j,l)-omega*resid/e(j,l) enddo 13 lsw=3-lsw enddo 14 jsw=3-jsw if(n.eq.1.and.ipass.eq.1) then omega=1.d0/(1.d0-.5d0*rjac**2) else omega=1.d0/(1.d0-.25d0*rjac**2*omega) endif enddo 15 if(anorm.lt.EPS*anormf)return enddo 16 pause ’MAXITS exceeded in sor’ END
The main advantage of SOR is that it is very easy to program. Its main disadvantage is that it is still very inefficient on large problems.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Here we give a routine for SOR with Chebyshev acceleration.
19.5 Relaxation Methods for Boundary Value Problems
861
ADI (Alternating-Direction Implicit) Method The ADI method of §19.3 for diffusion equations can be turned into a relaxation method for elliptic equations [1-4] . In §19.3, we discussed ADI as a method for solving the time-dependent heat-flow equation (19.5.31)
By letting t → ∞ one also gets an iterative method for solving the elliptic equation ∇2 u = ρ
(19.5.32)
In either case, the operator splitting is of the form L = Lx + Ly
(19.5.33)
where Lx represents the differencing in x and Ly that in y. For example, in our model problem (19.0.6) with ∆x = ∆y = ∆, we have Lxu = 2uj,l − uj+1,l − uj−1,l (19.5.34)
Ly u = 2uj,l − uj,l+1 − uj,l−1
More complicated operators may be similarly split, but there is some art involved. A bad choice of splitting can lead to an algorithm that fails to converge. Usually one tries to base the splitting on the physical nature of the problem. We know for our model problem that an initial transient diffuses away, and we set up the x and y splitting to mimic diffusion in each dimension. Having chosen a splitting, we difference the time-dependent equation (19.5.31) implicitly in two half-steps: Lx un+1/2 + Ly un un+1/2 − un =− −ρ ∆t/2 ∆2 Lx un+1/2 + Ly un+1 un+1 − un+1/2 =− −ρ ∆t/2 ∆2
(19.5.35)
(cf. equation 19.3.16). Here we have suppressed the spatial indices (j, l). In matrix notation, equations (19.5.35) are (Lx + r1) · un+1/2 = (r1 − Ly ) · un − ∆2 ρ (Ly + r1) · u
n+1
= (r1 − Lx) · u
n+1/2
(19.5.36)
−∆ ρ 2
(19.5.37)
where r≡
2∆2 ∆t
(19.5.38)
The matrices on the left-hand sides of equations (19.5.36) and (19.5.37) are tridiagonal (and usually positive definite), so the equations can be solved by the
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
∂u = ∇2 u − ρ ∂t
862
Chapter 19.
Partial Differential Equations
CITED REFERENCES AND FURTHER READING: Hockney, R.W., and Eastwood, J.W. 1981, Computer Simulation Using Particles (New York: McGraw-Hill), Chapter 6. Young, D.M. 1971, Iterative Solution of Large Linear Systems (New York: Academic Press). [1] Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag), §§8.3–8.6. [2] Varga, R.S. 1962, Matrix Iterative Analysis (Englewood Cliffs, NJ: Prentice-Hall). [3] Spanier, J. 1967, in Mathematical Methods for Digital Computers, Volume 2 (New York: Wiley), Chapter 11. [4]
19.6 Multigrid Methods for Boundary Value Problems Practical multigrid methods were first introduced in the 1970s by Brandt. These methods can solve elliptic PDEs discretized on N grid points in O(N ) operations. The “rapid” direct elliptic solvers discussed in §19.4 solve special kinds of elliptic equations in O(N log N ) operations. The numerical coefficients in these estimates are such that multigrid methods are comparable to the rapid methods in execution speed. Unlike the rapid methods, however, the multigrid methods can solve general elliptic equations with nonconstant coefficients with hardly any loss in efficiency. Even nonlinear equations can be solved with comparable speed. Unfortunately there is not a single multigrid algorithm that solves all elliptic problems. Rather there is a multigrid technique that provides the framework for solving these problems. You have to adjust the various components of the algorithm within this framework to solve your specific problem. We can only give a brief
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
standard tridiagonal algorithm. Given un , one solves (19.5.36) for un+1/2 , substitutes on the right-hand side of (19.5.37), and then solves for un+1 . The key question is how to choose the iteration parameter r, the analog of a choice of timestep for an initial value problem. As usual, the goal is to minimize the spectral radius of the iteration matrix. Although it is beyond our scope to go into details here, it turns out that, for the optimal choice of r, the ADI method has the same rate of convergence as SOR. The individual iteration steps in the ADI method are much more complicated than in SOR, so the ADI method would appear to be inferior. This is in fact true if we choose the same parameter r for every iteration step. However, it is possible to choose a different r for each step. If this is done optimally, then ADI is generally more efficient than SOR. We refer you to the literature [1-4] for details. Our reason for not fully implementing ADI here is that, in most applications, it has been superseded by the multigrid methods described in the next section. Our advice is to use SOR for trivial problems (e.g., 20 × 20), or for solving a larger problem once only, where ease of programming outweighs expense of computer time. Occasionally, the sparse matrix methods of §2.7 are useful for solving a set of difference equations directly. For production solution of large elliptic problems, however, multigrid is now almost always the method of choice.
19.6 Multigrid Methods for Boundary Value Problems
863
From One-Grid, through Two-Grid, to Multigrid The key idea of the multigrid method can be understood by considering the simplest case of a two-grid method. Suppose we are trying to solve the linear elliptic problem Lu = f
(19.6.1)
where L is some linear elliptic operator and f is the source term. Discretize equation (19.6.1) on a uniform grid with mesh size h. Write the resulting set of linear algebraic equations as Lh uh = fh
(19.6.2)
Let u eh denote some approximate solution to equation (19.6.2). We will use the symbol uh to denote the exact solution to the difference equations (19.6.2). Then the error in u eh or the correction is vh = uh − u eh
(19.6.3)
dh = Lh u eh − fh
(19.6.4)
The residual or defect is
(Beware: some authors define residual as minus the defect, and there is not universal agreement about which of these two quantities 19.6.4 defines.) Since Lh is linear, the error satisfies Lh vh = −dh
(19.6.5)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
introduction to the subject here. In particular, we will give two sample multigrid routines, one linear and one nonlinear. By following these prototypes and by perusing the references [1-4] , you should be able to develop routines to solve your own problems. There are two related, but distinct, approaches to the use of multigrid techniques. The first, termed “the multigrid method,” is a means for speeding up the convergence of a traditional relaxation method, as defined by you on a grid of pre-specified fineness. In this case, you need define your problem (e.g., evaluate its source terms) only on this grid. Other, coarser, grids defined by the method can be viewed as temporary computational adjuncts. The second approach, termed (perhaps confusingly) “the full multigrid (FMG) method,” requires you to be able to define your problem on grids of various sizes (generally by discretizing the same underlying PDE into different-sized sets of finitedifference equations). In this approach, the method obtains successive solutions on finer and finer grids. You can stop the solution either at a pre-specified fineness, or you can monitor the truncation error due to the discretization, quitting only when it is tolerably small. In this section we will first discuss the “multigrid method,” then use the concepts developed to introduce the FMG method. The latter algorithm is the one that we implement in the accompanying programs.
864
Chapter 19.
Partial Differential Equations
At this point we need to make an approximation to Lh in order to find vh . The classical iteration methods, such as Jacobi or Gauss-Seidel, do this by finding, at each stage, an approximate solution of the equation Lbh vbh = −dh
(19.6.6)
=u eh + b vh u enew h
(19.6.7)
Now consider, as an alternative, a completely different type of approximation for Lh , one in which we “coarsify” rather than “simplify.” That is, we form some appropriate approximation LH of Lh on a coarser grid with mesh size H (we will always take H = 2h, but other choices are possible). The residual equation (19.6.5) is now approximated by LH vH = −dH
(19.6.8)
Since LH has smaller dimension, this equation will be easier to solve than equation (19.6.5). To define the defect dH on the coarse grid, we need a restriction operator R that restricts dh to the coarse grid: dH = Rdh
(19.6.9)
The restriction operator is also called the fine-to-coarse operator or the injection operator. Once we have a solution veH to equation (19.6.8), we need a prolongation operator P that prolongates or interpolates the correction to the fine grid: vh = P veH e
(19.6.10)
The prolongation operator is also called the coarse-to-fine operator or the interpolation operator. Both R and P are chosen to be linear operators. Finally the approximation u eh can be updated: u enew =u eh + e vh h One step of this coarse-grid correction scheme is thus: Coarse-Grid Correction • • • •
Compute the defect on the fine grid from (19.6.4). Restrict the defect by (19.6.9). Solve (19.6.8) exactly on the coarse grid for the correction. Interpolate the correction to the fine grid by (19.6.10).
(19.6.11)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where Lbh is a “simpler” operator than Lh . For example, Lbh is the diagonal part of Lh for Jacobi iteration, or the lower triangle for Gauss-Seidel iteration. The next approximation is generated by
19.6 Multigrid Methods for Boundary Value Problems
865
Smoothing, Restriction, and Prolongation Operators The most popular smoothing method, and the one you should try first, is Gauss-Seidel, since it usually leads to a good convergence rate. If we order the mesh points from 1 to N , then the Gauss-Seidel scheme is N 1 X Lij uj − fi ui = − Lii j=1 j6=i
i = 1, . . . , N
(19.6.12)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
• Compute the next approximation by (19.6.11). Let’s contrast the advantages and disadvantages of relaxation and the coarse-grid correction scheme. Consider the error vh expanded into a discrete Fourier series. Call the components in the lower half of the frequency spectrum the smooth components and the high-frequency components the nonsmooth components. We have seen that relaxation becomes very slowly convergent in the limit h → 0, i.e., when there are a large number of mesh points. The reason turns out to be that the smooth components are only slightly reduced in amplitude on each iteration. However, many relaxation methods reduce the amplitude of the nonsmooth components by large factors on each iteration: They are good smoothing operators. For the two-grid iteration, on the other hand, components of the error with wavelengths < ∼ 2H are not even representable on the coarse grid and so cannot be reduced to zero on this grid. But it is exactly these high-frequency components that can be reduced by relaxation on the fine grid! This leads us to combine the ideas of relaxation and coarse-grid correction: Two-Grid Iteration • Pre-smoothing: Compute u¯h by applying ν1 ≥ 0 steps of a relaxation method to u eh . • Coarse-grid correction: As above, using u ¯h to give u ¯new h . new • Post-smoothing: Compute u eh by applying ν2 ≥ 0 steps of the relaxation method to u¯new h . It is only a short step from the above two-grid method to a multigrid method. Instead of solving the coarse-grid defect equation (19.6.8) exactly, we can get an approximate solution of it by introducing an even coarser grid and using the two-grid iteration method. If the convergence factor of the two-grid method is small enough, we will need only a few steps of this iteration to get a good enough approximate solution. We denote the number of such iterations by γ. Obviously we can apply this idea recursively down to some coarsest grid. There the solution is found easily, for example by direct matrix inversion or by iterating the relaxation scheme to convergence. One iteration of a multigrid method, from finest grid to coarser grids and back to finest grid again, is called a cycle. The exact structure of a cycle depends on the value of γ, the number of two-grid iterations at each intermediate stage. The case γ = 1 is called a V-cycle, while γ = 2 is called a W-cycle (see Figure 19.6.1). These are the most important cases in practice. Note that once more than two grids are involved, the pre-smoothing steps after the first one on the finest grid need an initial approximation for the error v. This should be taken to be zero.
866
Chapter 19.
Partial Differential Equations
S
S
2-grid
S S
S
3-grid
S
S S
E
S
S S
S S
S E γ=1
S
S
E
E
S 4-grid
S S
S S
S E
S
S S
E
S E
S E
γ=2
Figure 19.6.1. Structure of multigrid cycles. S denotes smoothing, while E denotes exact solution on the coarsest grid. Each descending line \ denotes restriction (R) and each ascending line / denotes prolongation (P). The finest grid is at the top level of each diagram. For the V-cycles (γ = 1) the E step is replaced by one 2-grid iteration each time the number of grid levels is increased by one. For the W-cycles (γ = 2), each E step gets replaced by two 2-grid iterations.
where new values of u are used on the right-hand side as they become available. The exact form of the Gauss-Seidel method depends on the ordering chosen for the mesh points. For typical second-order elliptic equations like our model problem equation (19.0.3), as differenced in equation (19.0.8), it is usually best to use red-black ordering, making one pass through the mesh updating the “even” points (like the red squares of a checkerboard) and another pass updating the “odd” points (the black squares). When quantities are more strongly coupled along one dimension than another, one should relax a whole line along that dimension simultaneously. Line relaxation for nearest-neighbor coupling involves solving a tridiagonal system, and so is still efficient. Relaxing odd and even lines on successive passes is called zebra relaxation and is usually preferred over simple line relaxation. Note that SOR should not be used as a smoothing operator. The overrelaxation destroys the high-frequency smoothing that is so crucial for the multigrid method. A succint notation for the prolongation and restriction operators is to give their symbol. The symbol of P is found by considering vH to be 1 at some mesh point (x, y), zero elsewhere, and then asking for the values of PvH . The most popular prolongation operator is simple bilinear interpolation. It gives nonzero values at the 9 points (x, y), (x + h, y), . . . , (x − h, y − h), where the values are 1, 12 , . . . , 14 .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
S
E
19.6 Multigrid Methods for Boundary Value Problems
867
Its symbol is therefore 1
4 1 2 1 4
1 2
1 1 2
1 4 1 2 1 4
(19.6.13)
x,y
Then the adjoint of P, denoted P † , is defined by huH |P † vh iH = hPuH |vh ih
(19.6.15)
Now take P to be bilinear interpolation, and choose uH = 1 at (x, y), zero elsewhere. Set P † = R in (19.6.15) and H = 2h. You will find that (Rvh )(x,y) = 14 vh (x, y) + 18 vh (x + h, y) +
1 16 vh (x
+ h, y + h) + · · · (19.6.16)
so that the symbol of R is
1 16 1 8 1 16
1 8 1 4 1 8
1 16 1 8 1 16
(19.6.17)
Note the simple rule: The symbol of R is 14 the transpose of the matrix defining the symbol of P, equation (19.6.13). This rule is general whenever R = P † and H = 2h. The particular choice of R in (19.6.17) is called full weighting. Another popular choice for R is half weighting, “halfway” between full weighting and straight injection. Its symbol is 0 18 0 1 1 1 (19.6.18) 8 2 8 0
1 8
0
A similar notation can be used to describe the difference operator Lh . For example, the standard differencing of the model problem, equation (19.0.6), is represented by the five-point difference star 0 1 0 1 (19.6.19) Lh = 2 1 −4 1 h 0 1 0
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The symbol of R is defined by considering vh to be defined everywhere on the fine grid, and then asking what is Rvh at (x, y) as a linear combination of these values. The simplest possible choice for R is straight injection, which means simply filling each coarse-grid point with the value from the corresponding fine-grid point. Its symbol is “[1].” However, difficulties can arise in practice with this choice. It turns out that a safe choice for R is to make it the adjoint operator to P. To define the adjoint, define the scalar product of two grid functions uh and vh for mesh size h as X uh (x, y)vh (x, y) (19.6.14) huh |vh ih ≡ h2
868
Chapter 19.
Partial Differential Equations
Full Multigrid Algorithm So far we have described multigrid as an iterative scheme, where one starts with some initial guess on the finest grid and carries out enough cycles (V-cycles, W-cycles,. . . ) to achieve convergence. This is the simplest way to use multigrid: Simply apply enough cycles until some appropriate convergence criterion is met. However, efficiency can be improved by using the Full Multigrid Algorithm (FMG), also known as nested iteration. Instead of starting with an arbitrary approximation on the finest grid (e.g., uh = 0), the first approximation is obtained by interpolating from a coarse-grid solution: uh = PuH
(19.6.20)
The coarse-grid solution itself is found by a similar FMG process from even coarser grids. At the coarsest level, you start with the exact solution. Rather than proceed as in Figure 19.6.1, then, FMG gets to its solution by a series of increasingly tall “N’s,” each taller one probing a finer grid (see Figure 19.6.2). Note that P in (19.6.20) need not be the same P used in the multigrid cycles. It should be at least of the same order as the discretization Lh , but sometimes a higher-order operator leads to greater efficiency. It turns out that you usually need one or at most two multigrid cycles at each level before proceeding down to the next finer grid. While there is theoretical guidance on the required number of cycles (e.g., [2]), you can easily determine it empirically. Fix the finest level and study the solution values as you increase the number of cycles per level. The asymptotic value of the solution is the exact solution of the difference equations. The difference between this exact solution and the solution for a small number of cycles is the iteration error. Now fix the number of cycles to be large, and vary the number of levels, i.e., the smallest value of h used. In this way you can estimate the truncation error for a given h. In your final production code, there is no point in using more cycles than you need to get the iteration error down to the size of the truncation error. The simple multigrid iteration (cycle) needs the right-hand side f only at the finest level. FMG needs f at all levels. If the boundary conditions are homogeneous,
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
If you are confronted with a new problem and you are not sure what P and R choices are likely to work well, here is a safe rule: Suppose mp is the order of the interpolation P (i.e., it interpolates polynomials of degree mp − 1 exactly). Suppose mr is the order of R, and that R is the adjoint of some P (not necessarily the P you intend to use). Then if m is the order of the differential operator Lh , you should satisfy the inequality mp + mr > m. For example, bilinear interpolation and its adjoint, full weighting, for Poisson’s equation satisfy mp + mr = 4 > m = 2. Of course the P and R operators should enforce the boundary conditions for your problem. The easiest way to do this is to rewrite the difference equation to have homogeneous boundary conditions by modifying the source term if necessary (cf. §19.4). Enforcing homogeneous boundary conditions simply requires the P operator to produce zeros at the appropriate boundary points. The corresponding R is then found by R = P † .
869
19.6 Multigrid Methods for Boundary Value Problems
S S S
S
S S
E
S
S S
E
S E
S S S E
S E
S E
S S
S E
S S
S E
4-grid ncycle = 1
S S
S S
S E
S S
S S
S
4-grid ncycle = 2
E
Figure 19.6.2. Structure of cycles for the full multigrid (FMG) method. This method starts on the coarsest grid, interpolates, and then refines (by “V’s”), the solution onto grids of increasing fineness.
you can use fH = Rfh . This prescription is not always safe for inhomogeneous boundary conditions. In that case it is better to discretize f on each coarse grid. Note that the FMG algorithm produces the solution on all levels. It can therefore be combined with techniques like Richardson extrapolation. We now give a routine mglin that implements the Full Multigrid Algorithm for a linear equation, the model problem (19.0.6). It uses red-black Gauss-Seidel as the smoothing operator, bilinear interpolation for P, and half-weighting for R. To change the routine to handle another linear problem, all you need do is modify the subroutines relax, resid, and slvsml appropriately. A feature of the routine is the dynamical allocation of storage for variables defined on the various grids. The subroutine maloc emulates the C function malloc. It allows you to write subroutines that operate on two-dimensional arrays in the usual way, but to allocate storage for these arrays in the calling program “on the fly” out of a single long one-dimensional array.
C
*
SUBROUTINE mglin(u,n,ncycle) INTEGER n,ncycle,NPRE,NPOST,NG,MEMLEN DOUBLE PRECISION u(n,n) PARAMETER (NG=5,MEMLEN=13*2**(2*NG)/3+14*2**NG+8*NG-100/3) PARAMETER (NPRE=1,NPOST=1) USES addint,copy,fill0,interp,maloc,relax,resid,rstrct,slvsml Full Multigrid Algorithm for solution of linear elliptic equation, here the model problem (19.0.6). On input u(1:n,1:n) contains the right-hand side ρ, while on output it returns the solution. The dimension n is related to the number of grid levels used in the solution, NG below, by n = 2**NG + 1. ncycle is the number of V-cycles to be used at each level. Parameters: NG is the number of grid levels used; MEMLEN is the maximum amount of memory that can be allocated by calls to maloc; NPRE and NPOST are the number of relaxation sweeps before and after the coarse-grid correction is computed. INTEGER j,jcycle,jj,jpost,jpre,mem,nf,ngrid,nn,ires(NG), irho(NG),irhs(NG),iu(NG),maloc DOUBLE PRECISION z
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
E
S
S
870
1
Chapter 19.
Partial Differential Equations
SUBROUTINE rstrct(uc,uf,nc) INTEGER nc DOUBLE PRECISION uc(nc,nc),uf(2*nc-1,2*nc-1) Half-weighting restriction. nc is the coarse-grid dimension. The fine-grid solution is input in uf(1:2*nc-1,1:2*nc-1), the coarse-grid solution is returned in uc(1:nc,1:nc). INTEGER ic,if,jc,jf
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
COMMON /memory/ z(MEMLEN),mem Storage for grid functions is allocated by maloc mem=0 from array z. nn=n/2+1 ngrid=NG-1 irho(ngrid)=maloc(nn**2) Allocate storage for r.h.s. on grid NG − 1, call rstrct(z(irho(ngrid)),u,nn) and fill it by restricting from the fine grid. if (nn.gt.3) then Similarly allocate storage and fill r.h.s. on all nn=nn/2+1 coarse grids. ngrid=ngrid-1 irho(ngrid)=maloc(nn**2) call rstrct(z(irho(ngrid)),z(irho(ngrid+1)),nn) goto 1 endif nn=3 iu(1)=maloc(nn**2) irhs(1)=maloc(nn**2) call slvsml(z(iu(1)),z(irho(1))) Initial solution on coarsest grid. ngrid=NG do 16 j=2,ngrid Nested iteration loop. nn=2*nn-1 iu(j)=maloc(nn**2) irhs(j)=maloc(nn**2) ires(j)=maloc(nn**2) call interp(z(iu(j)),z(iu(j-1)),nn) Interpolate from coarse grid to next finer grid. if (j.ne.ngrid) then call copy(z(irhs(j)),z(irho(j)),nn) Set up r.h.s. else call copy(z(irhs(j)),u,nn) endif do 15 jcycle=1,ncycle V-cycle loop. nf=nn do 12 jj=j,2,-1 Downward stoke of the V. do 11 jpre=1,NPRE Pre-smoothing. call relax(z(iu(jj)),z(irhs(jj)),nf) enddo 11 call resid(z(ires(jj)),z(iu(jj)),z(irhs(jj)),nf) nf=nf/2+1 call rstrct(z(irhs(jj-1)),z(ires(jj)),nf) Restriction of the residual is the next r.h.s. call fill0(z(iu(jj-1)),nf) Zero for initial guess in next relaxation. enddo 12 call slvsml(z(iu(1)),z(irhs(1))) Bottom of V: solve on coarsest grid. nf=3 do 14 jj=2,j Upward stroke of V. nf=2*nf-1 call addint(z(iu(jj)),z(iu(jj-1)),z(ires(jj)),nf) Use res for temporary storage inside addint. do 13 jpost=1,NPOST Post-smoothing. call relax(z(iu(jj)),z(irhs(jj)),nf) enddo 13 enddo 14 enddo 15 enddo 16 call copy(u,z(iu(ngrid)),n) Return solution in u. return END
19.6 Multigrid Methods for Boundary Value Problems
*
871
SUBROUTINE interp(uf,uc,nf) INTEGER nf DOUBLE PRECISION uc(nf/2+1,nf/2+1),uf(nf,nf) INTEGER ic,if,jc,jf,nc Coarse-to-fine prolongation by bilinear interpolation. nf is the fine-grid dimension. The coarse-grid solution is input as uc(1:nc,1:nc), where nc = nf/2 + 1. The fine-grid solution is returned in uf(1:nf,1:nf). nc=nf/2+1 do 12 jc=1,nc Do elements that are copies. jf=2*jc-1 do 11 ic=1,nc uf(2*ic-1,jf)=uc(ic,jc) enddo 11 enddo 12 do 14 jf=1,nf,2 Do odd-numbered columns, interpolating verdo 13 if=2,nf-1,2 tically. uf(if,jf)=.5d0*(uf(if+1,jf)+uf(if-1,jf)) enddo 13 enddo 14 do 16 jf=2,nf-1,2 Do even-numbered columns, interpolating hordo 15 if=1,nf izontally. uf(if,jf)=.5d0*(uf(if,jf+1)+uf(if,jf-1)) enddo 15 enddo 16 return END
C
SUBROUTINE addint(uf,uc,res,nf) INTEGER nf DOUBLE PRECISION res(nf,nf),uc(nf/2+1,nf/2+1),uf(nf,nf) USES interp Does coarse-to-fine interpolation and adds result to uf. nf is the fine-grid dimension. The coarse-grid solution is input as uc(1:nc,1:nc), where nc = nf/2 + 1. The fine-grid solution is returned in uf(1:nf,1:nf). res(1:nf,1:nf) is used for temporary storage. INTEGER i,j call interp(res,uc,nf) do 12 j=1,nf do 11 i=1,nf uf(i,j)=uf(i,j)+res(i,j) enddo 11 enddo 12 return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
do 12 jc=2,nc-1 Interior points. jf=2*jc-1 do 11 ic=2,nc-1 if=2*ic-1 uc(ic,jc)=.5d0*uf(if,jf)+.125d0*(uf(if+1,jf)+ uf(if-1,jf)+uf(if,jf+1)+uf(if,jf-1)) enddo 11 enddo 12 do 13 ic=1,nc Boundary points. uc(ic,1)=uf(2*ic-1,1) uc(ic,nc)=uf(2*ic-1,2*nc-1) enddo 13 do 14 jc=1,nc uc(1,jc)=uf(1,2*jc-1) uc(nc,jc)=uf(2*nc-1,2*jc-1) enddo 14 return END
872
C
*
Partial Differential Equations
SUBROUTINE slvsml(u,rhs) DOUBLE PRECISION rhs(3,3),u(3,3) USES fill0 Solution of the model problem on the coarsest grid, where h = 12 . The right-hand side is input in rhs(1:3,1:3) and the solution is returned in u(1:3,1:3). DOUBLE PRECISION h call fill0(u,3) h=.5d0 u(2,2)=-h*h*rhs(2,2)/4.d0 return END
SUBROUTINE relax(u,rhs,n) INTEGER n DOUBLE PRECISION rhs(n,n),u(n,n) Red-black Gauss-Seidel relaxation for model problem. The current value of the solution u(1:n,1:n) is updated, using the right-hand side function rhs(1:n,1:n). INTEGER i,ipass,isw,j,jsw DOUBLE PRECISION h,h2 h=1.d0/(n-1) h2=h*h jsw=1 do 13 ipass=1,2 Red and black sweeps. isw=jsw do 12 j=2,n-1 do 11 i=isw+1,n-1,2 Gauss-Seidel formula. u(i,j)=0.25d0*(u(i+1,j)+u(i-1,j)+u(i,j+1) +u(i,j-1)-h2*rhs(i,j)) enddo 11 isw=3-isw enddo 12 jsw=3-jsw enddo 13 return END
SUBROUTINE resid(res,u,rhs,n) INTEGER n DOUBLE PRECISION res(n,n),rhs(n,n),u(n,n) Returns minus the residual for the model problem. Input quantities are u(1:n,1:n) and rhs(1:n,1:n), while res(1:n,1:n) is returned. INTEGER i,j DOUBLE PRECISION h,h2i h=1.d0/(n-1) h2i=1.d0/(h*h) do 12 j=2,n-1 Interior points. do 11 i=2,n-1 res(i,j)=-h2i*(u(i+1,j)+u(i-1,j)+u(i,j+1)+u(i,j-1)4.d0*u(i,j))+rhs(i,j) enddo 11 enddo 12 do 13 i=1,n Boundary points. res(i,1)=0.d0 res(i,n)=0.d0 res(1,i)=0.d0 res(n,i)=0.d0 enddo 13 return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
Chapter 19.
19.6 Multigrid Methods for Boundary Value Problems
873
SUBROUTINE fill0(u,n) INTEGER n DOUBLE PRECISION u(n,n) Fills u(1:n,1:n) with zeros. INTEGER i,j do 12 j=1,n do 11 i=1,n u(i,j)=0.d0 enddo 11 enddo 12 return END
C
FUNCTION maloc(len) INTEGER maloc,len,NG,MEMLEN PARAMETER (NG=5,MEMLEN=13*2**(2*NG)/3+14*2**NG+8*NG-100/3) for mglin PARAMETER (NG=5,MEMLEN=17*2**(2*NG)/3+18*2**NG+10*NG-86/3) for mgfas, N.B.! INTEGER mem DOUBLE PRECISION z COMMON /memory/ z(MEMLEN),mem Dynamical storage allocation. Returns integer pointer to the starting position for len array elements in the array z. The preceding array element is filled with the value of len, and the variable mem is updated to point to the last element of z that has been used. if (mem+len+1.gt.MEMLEN) pause ’insufficient memory in maloc’ z(mem+1)=len maloc=mem+2 mem=mem+len+1 return END
The routine mglin is written for clarity, not maximum efficiency, so that it is easy to modify. Several simple changes will speed up the execution time: • The defect dh vanishes identically at all black mesh points after a red-black Gauss-Seidel step. Thus dH = Rdh for half-weighting reduces to simply copying half the defect from the fine grid to the corresponding coarse-grid point. The calls to resid followed by rstrct in the first part of the V-cycle can be replaced by a routine that loops only over the coarse grid, filling it with half the defect. =u eh + Pe vH need not be computed at red • Similarly, the quantity u enew h mesh points, since they will immediately be redefined in the subsequent Gauss-Seidel sweep. This means that addint need only loop over black points.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE copy(aout,ain,n) INTEGER n DOUBLE PRECISION ain(n,n),aout(n,n) Copies ain(1:n,1:n) to aout(1:n,1:n). INTEGER i,j do 12 i=1,n do 11 j=1,n aout(j,i)=ain(j,i) enddo 11 enddo 12 return END
874
Chapter 19.
Partial Differential Equations
Nonlinear Multigrid: The FAS Algorithm Now turn to solving a nonlinear elliptic equation, which we write symbolically as L(u) = 0
(19.6.21)
Any explicit source term has been moved to the left-hand side. Suppose equation (19.6.21) is suitably discretized: Lh (uh ) = 0
(19.6.22)
We will see below that in the multigrid algorithm we will have to consider equations where a nonzero right-hand side is generated during the course of the solution: Lh (uh ) = fh
(19.6.23)
One way of solving nonlinear problems with multigrid is to use Newton’s method, which produces linear equations for the correction term at each iteration. We can then use linear multigrid to solve these equations. A great strength of the multigrid idea, however, is that it can be applied directly to nonlinear problems. All we need is a suitable nonlinear relaxation method to smooth the errors, plus a procedure for approximating corrections on coarser grids. This direct approach is Brandt’s Full Approximation Storage Algorithm (FAS). No nonlinear equations need be solved, except perhaps on the coarsest grid. To develop the nonlinear algorithm, suppose we have a relaxation procedure that can smooth the residual vector as we did in the linear case. Then we can seek a smooth correction vh to solve (19.6.23): Lh (e uh + vh ) = fh
(19.6.24)
To find vh, note that Lh (e uh + vh ) − Lh (e uh ) = fh − Lh (e uh ) = −dh
(19.6.25)
The right-hand side is smooth after a few nonlinear relaxation sweeps. Thus we can transfer the left-hand side to a coarse grid: LH (uH ) − LH (Re uh ) = −Rdh
(19.6.26)
LH (uH ) = LH (Re uh ) − Rdh
(19.6.27)
that is, we solve on the coarse grid. (This is how nonzero right-hand sides appear.) Suppose the approximate solution is u eH . Then the coarse-grid correction is vH = u e eH − Re uh
(19.6.28)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
• You can speed up relax in several ways. First, you can have a special form when the initial guess is zero, and omit the routine fill0. Next, you can store h2 fh on the various grids and save a multiplication. Finally, it is possible to save an addition in the Gauss-Seidel formula by rewriting it with intermediate variables. • On typical problems, mglin with ncycle = 1 will return a solution with the iteration error bigger than the truncation error for the given size of h. To knock the error down to the size of the truncation error, you have to set ncycle = 2 or, more cheaply, npre = 2. A more efficient way turns out to be to use a higher-order P in (19.6.20) than the linear interpolation used in the V-cycle. Implementing all the above features typically gives up to a factor of two improvement in execution time and is certainly worthwhile in a production code.
875
19.6 Multigrid Methods for Boundary Value Problems
and u enew =u eh + P(e uH − Re uh ) h
(19.6.29)
τ ≡ Lh (u) − fh
(19.6.30)
where u is the exact solution of the oiginal continuum equation. If we rewrite this as Lh (u) = fh + τ
(19.6.31)
we see that τ can be regarded as the correction to fh so that the solution of the fine-grid equation will be the exact solution u. Now consider the relative truncation error τh , which is defined on the H-grid relative to the h-grid: τh ≡ LH (Ruh) − RLh (uh )
(19.6.32)
Since Lh (uh ) = fh , this can be rewritten as LH (uH ) = fH + τh
(19.6.33)
In other words, we can think of τh as the correction to fH that makes the solution of the coarse-grid equation equal to the fine-grid solution. Of course we cannot compute τh , but we do have an approximation to it from using u eh in equation (19.6.32): τh ' τeh ≡ LH (Re uh ) − RLh (e uh )
(19.6.34)
Replacing τh by τeh in equation (19.6.33) gives LH (uH ) = LH (Re uh ) − Rdh
(19.6.35)
which is just the coarse-grid equation (19.6.27)! Thus we see that there are two complementary viewpoints for the relation between coarse and fine grids: • Coarse grids are used to accelerate the convergence of the smooth components of the fine-grid residuals. • Fine grids are used to compute correction terms to the coarse-grid equations, yielding fine-grid accuracy on the coarse grids. One benefit of this new viewpoint is that it allows us to derive a natural stopping criterion for a multigrid iteration. Normally the criterion would be kdh k ≤
(19.6.36)
and the question is how to choose . There is clearly no benefit in iterating beyond the point when the remaining error is dominated by the local truncation error τ . The computable quantity is τeh . What is the relation between τ and τeh ? For the typical case of a second-order accurate differencing scheme, τ = Lh (u) − Lh (uh ) = h2 τ2 (x, y) + · · ·
(19.6.37)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Note that PR = 6 1 in general, so u enew 6= P u eH . This is a key point: In equation (19.6.29) the h interpolation error comes only from the correction, not from the full solution u eH . Equation (19.6.27) shows that one is solving for the full approximation uH , not just the error as in the linear algorithm. This is the origin of the name FAS. The FAS multigrid algorithm thus looks very similar to the linear multigrid algorithm. The only differences are that both the defect dh and the relaxed approximation uh have to be restricted to the coarse grid, where now it is equation (19.6.27) that is solved by recursive invocation of the algorithm. However, instead of implementing the algorithm this way, we will first describe the so-called dual viewpoint, which leads to a powerful alternative way of looking at the multigrid idea. The dual viewpoint considers the local truncation error, defined as
876
Chapter 19.
Partial Differential Equations
Assume the solution satisfies uh = u + h2 u2 (x, y) + · · · . Then, assuming R is of high enough order that we can neglect its effect, equation (19.6.32) gives τh ' LH (u + h2 u2 ) − Lh (u + h2 u2 ) = LH (u) − Lh (u) + h2 [L0H (u2 ) − L0h (u2 )] + · · ·
(19.6.38)
2
4
For the usual case of H = 2h we therefore have τ ' 13 τh ' 13 τeh
(19.6.39)
The stopping criterion is thus equation (19.6.36) with = αke τh k,
α∼
1 3
(19.6.40)
We have one remaining task before implementing our nonlinear multigrid algorithm: choosing a nonlinear relaxation scheme. Once again, your first choice should probably be the nonlinear Gauss-Seidel scheme. If the discretized equation (19.6.23) is written with some choice of ordering as Li (u1 , . . . , uN ) = fi ,
i = 1, . . . , N
(19.6.41)
then the nonlinear Gauss-Seidel schemes solves , ui+1 , . . . , uN ) = fi Li (u1 , . . . , ui−1 , unew i
(19.6.42)
. As usual new u’s replace old u’s as soon as they have been computed. Often equation for unew i (19.6.42) is linear in unew , since the nonlinear terms are discretized by means of its neighbors. i If this is not the case, we replace equation (19.6.42) by one step of a Newton iteration: unew = uold − i i
Li (uold i ) − fi ∂Li (uold i )/∂ui
(19.6.43)
For example, consider the simple nonlinear equation ∇2 u + u2 = ρ
(19.6.44)
In two-dimensional notation, we have L(ui,j ) = (ui+1,j + ui−1,j + ui,j+1 + ui,j−1 − 4ui,j )/h2 + u2i,j − ρi,j = 0 (19.6.45) Since ∂L = −4/h2 + 2ui,j ∂ui,j
(19.6.46)
the Newton Gauss-Seidel iteration is unew i,j = ui,j −
L(ui,j ) −4/h2 + 2ui,j
(19.6.47)
Here is a routine mgfas that solves equation (19.6.44) using the Full Multigrid Algorithm and the FAS scheme. Restriction and prolongation are done as in mglin. We have included the convergence test based on equation (19.6.40). A successful multigrid solution of a problem should aim to satisfy this condition with the maximum number of V-cycles, maxcyc, equal to 1 or 2. The routine mgfas uses the same subroutines copy, interp, maloc, and rstrct as mglin, but with a larger storage requirement MEMLEN in maloc (be sure to change the PARAMETER statement in that routine, as indicated by the commented line).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
= (H − h )τ2 + O(h ) 2
19.6 Multigrid Methods for Boundary Value Problems
C
1
SUBROUTINE mgfas(u,n,maxcyc) INTEGER maxcyc,n,NPRE,NPOST,NG,MEMLEN DOUBLE PRECISION u(n,n),ALPHA PARAMETER (NG=5,MEMLEN=17*2**(2*NG)/3+18*2**NG+10*NG-86/3) PARAMETER (NPRE=1,NPOST=1,ALPHA=.33d0) USES anorm2,copy,interp,lop,maloc,matadd,matsub,relax2,rstrct,slvsm2 Full Multigrid Algorithm for FAS solution of nonlinear elliptic equation, here equation (19.6.44). On input u(1:n,1:n) contains the right-hand side ρ, while on output it returns the solution. The dimension n is related to the number of grid levels used in the solution, NG below, by n = 2**NG + 1. maxcyc is the maximum number of V-cycles to be used at each level. Parameters: NG is the number of grid levels used; MEMLEN is the maximum amount of memory that can be allocated by calls to maloc; NPRE and NPOST are the number of relaxation sweeps before and after the coarse-grid correction is computed; ALPHA relates the estimated truncation error to the norm of the residual. INTEGER j,jcycle,jj,jm1,jpost,jpre,mem,nf,ngrid,nn,irho(NG), irhs(NG),itau(NG),itemp(NG),iu(NG),maloc DOUBLE PRECISION res,trerr,z,anorm2 COMMON /memory/ z(MEMLEN),mem Storage for grid functions is allocated by maloc mem=0 from array z. nn=n/2+1 ngrid=NG-1 irho(ngrid)=maloc(nn**2) Allocate storage for r.h.s. on grid NG − 1, call rstrct(z(irho(ngrid)),u,nn) and fill it by restricting from the fine grid. if (nn.gt.3) then Similarly allocate storage and fill r.h.s. on all nn=nn/2+1 coarse grids. ngrid=ngrid-1 irho(ngrid)=maloc(nn**2) call rstrct(z(irho(ngrid)),z(irho(ngrid+1)),nn) goto 1 endif nn=3 iu(1)=maloc(nn**2) irhs(1)=maloc(nn**2) itau(1)=maloc(nn**2) itemp(1)=maloc(nn**2) call slvsm2(z(iu(1)),z(irho(1))) Initial solution on coarsest grid. ngrid=NG do 16 j=2,ngrid Nested iteration loop. nn=2*nn-1 iu(j)=maloc(nn**2) irhs(j)=maloc(nn**2) itau(j)=maloc(nn**2) itemp(j)=maloc(nn**2) call interp(z(iu(j)),z(iu(j-1)),nn) Interpolate from coarse grid to next finer grid. if (j.ne.ngrid) then call copy(z(irhs(j)),z(irho(j)),nn) Set up r.h.s. else call copy(z(irhs(j)),u,nn) endif do 15 jcycle=1,maxcyc V-cycle loop. nf=nn do 12 jj=j,2,-1 Downward stoke of the V. do 11 jpre=1,NPRE Pre-smoothing. call relax2(z(iu(jj)),z(irhs(jj)),nf) enddo 11 call lop(z(itemp(jj)),z(iu(jj)),nf) Lh (e uh ). nf=nf/2+1 jm1=jj-1 call rstrct(z(itemp(jm1)),z(itemp(jj)),nf) RLh (e uh ). call rstrct(z(iu(jm1)),z(iu(jj)),nf) Re uh . call lop(z(itau(jm1)),z(iu(jm1)),nf) LH (Re uh ) stored temporarily in τeh . call matsub(z(itau(jm1)),z(itemp(jm1)),z(itau(jm1)),nf) Form τeh . if(jj.eq.j)trerr=ALPHA*anorm2(z(itau(jm1)),nf) Estimate truncation error τ .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
877
878
*
C
Partial Differential Equations
call rstrct(z(irhs(jm1)),z(irhs(jj)),nf) fH . call matadd(z(irhs(jm1)),z(itau(jm1)),z(irhs(jm1)),nf) fH + τeh . enddo 12 call slvsm2(z(iu(1)),z(irhs(1))) Bottom of V: Solve on coarsest grid. nf=3 do 14 jj=2,j Upward stroke of V. jm1=jj-1 call rstrct(z(itemp(jm1)),z(iu(jj)),nf) Re uh . call matsub(z(iu(jm1)),z(itemp(jm1)),z(itemp(jm1)),nf) u eH − Re uh . nf=2*nf-1 call interp(z(itau(jj)),z(itemp(jm1)),nf) P (e uH −Re uh ) stored in τeh . call matadd(z(iu(jj)),z(itau(jj)),z(iu(jj)),nf) Form u enew h . do 13 jpost=1,NPOST Post-smoothing. call relax2(z(iu(jj)),z(irhs(jj)),nf) enddo 13 enddo 14 call lop(z(itemp(j)),z(iu(j)),nf) Form residual kdh k. call matsub(z(itemp(j)),z(irhs(j)),z(itemp(j)),nf) res=anorm2(z(itemp(j)),nf) if(res.lt.trerr)goto 2 No more V-cycles needed if residual small enddo 15 enough. continue enddo 16 call copy(u,z(iu(ngrid)),n) Return solution in u. return END SUBROUTINE relax2(u,rhs,n) INTEGER n DOUBLE PRECISION rhs(n,n),u(n,n) Red-black Gauss-Seidel relaxation for equation (19.6.44). The current value of the solution u(1:n,1:n) is updated, using the right-hand side function rhs(1:n,1:n). INTEGER i,ipass,isw,j,jsw DOUBLE PRECISION foh2,h,h2i,res h=1.d0/(n-1) h2i=1.d0/(h*h) foh2=-4.d0*h2i jsw=1 do 13 ipass=1,2 Red and black sweeps. isw=jsw do 12 j=2,n-1 do 11 i=isw+1,n-1,2 res=h2i*(u(i+1,j)+u(i-1,j)+u(i,j+1)+u(i,j-1)4.d0*u(i,j))+u(i,j)**2-rhs(i,j) u(i,j)=u(i,j)-res/(foh2+2.d0*u(i,j)) Newton Gauss-Seidel formula. enddo 11 isw=3-isw enddo 12 jsw=3-jsw enddo 13 return END SUBROUTINE slvsm2(u,rhs) DOUBLE PRECISION rhs(3,3),u(3,3) USES fill0 Solution of equation (19.6.44) on the coarsest grid, where h = 12 . The right-hand side is input in rhs(1:3,1:3) and the solution is returned in u(1:3,1:3). DOUBLE PRECISION disc,fact,h call fill0(u,3) h=.5d0 fact=2.d0/h**2 disc=sqrt(fact**2+rhs(2,2))
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
Chapter 19.
19.6 Multigrid Methods for Boundary Value Problems
879
u(2,2)=-rhs(2,2)/(fact+disc) return END
SUBROUTINE matadd(a,b,c,n) INTEGER n DOUBLE PRECISION a(n,n),b(n,n),c(n,n) Adds a(1:n,1:n) to b(1:n,1:n) and returns result in c(1:n,1:n). INTEGER i,j do 12 j=1,n do 11 i=1,n c(i,j)=a(i,j)+b(i,j) enddo 11 enddo 12 return END SUBROUTINE matsub(a,b,c,n) INTEGER n DOUBLE PRECISION a(n,n),b(n,n),c(n,n) Subtracts b(1:n,1:n) from a(1:n,1:n) and returns result in c(1:n,1:n). INTEGER i,j do 12 j=1,n do 11 i=1,n c(i,j)=a(i,j)-b(i,j) enddo 11 enddo 12 return END DOUBLE PRECISION FUNCTION anorm2(a,n) INTEGER n DOUBLE PRECISION a(n,n) Returns the Euclidean norm of the matrix a(1:n,1:n). INTEGER i,j DOUBLE PRECISION sum sum=0.d0 do 12 j=1,n do 11 i=1,n
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
SUBROUTINE lop(out,u,n) INTEGER n DOUBLE PRECISION out(n,n),u(n,n) Given u(1:n,1:n), returns Lh (e uh ) for equation (19.6.44) in out(1:n,1:n). INTEGER i,j DOUBLE PRECISION h,h2i h=1.d0/(n-1) h2i=1.d0/(h*h) do 12 j=2,n-1 Interior points. do 11 i=2,n-1 out(i,j)=h2i*(u(i+1,j)+u(i-1,j)+u(i,j+1)+u(i,j-1)4.d0*u(i,j))+u(i,j)**2 enddo 11 enddo 12 do 13 i=1,n Boundary points. out(i,1)=0.d0 out(i,n)=0.d0 out(1,i)=0.d0 out(n,i)=0.d0 enddo 13 return END
880
Chapter 19.
Partial Differential Equations
sum=sum+a(i,j)**2 enddo 11 enddo 12 anorm2=sqrt(sum)/n return END
Brandt, A. 1977, Mathematics of Computation, vol. 31, pp. 333–390. [1] Hackbusch, W. 1985, Multi-Grid Methods and Applications (New York: Springer-Verlag). [2] Stuben, K., and Trottenberg, U. 1982, in Multigrid Methods, W. Hackbusch and U. Trottenberg, eds. (Springer Lecture Notes in Mathematics No. 960) (New York: Springer-Verlag), pp. 1– 176. [3] Brandt, A. 1982, in Multigrid Methods, W. Hackbusch and U. Trottenberg, eds. (Springer Lecture Notes in Mathematics No. 960) (New York: Springer-Verlag). [4] Baker, L. 1991, More C Tools for Scientists and Engineers (New York: McGraw-Hill). Briggs, W.L. 1987, A Multigrid Tutorial (Philadelphia: S.I.A.M.). Jespersen, D. 1984, Multrigrid Methods for Partial Differential Equations (Washington: Mathematical Association of America). McCormick, S.F. (ed.) 1988, Multigrid Methods: Theory, Applications, and Supercomputing (New York: Marcel Dekker). Hackbusch, W., and Trottenberg, U. (eds.) 1991, Multigrid Methods III (Boston: Birkhauser). Wesseling, P. 1992, An Introduction to Multigrid Methods (New York: Wiley).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
CITED REFERENCES AND FURTHER READING:
20.0 Introduction You can stop reading now. You are done with Numerical Recipes, as such. This final chapter is an idiosyncratic collection of “less-numerical recipes” which, for one reason or another, we have decided to include between the covers of an otherwise more-numerically oriented book. Authors of computer science texts, we’ve noticed, like to throw in a token numerical subject (usually quite a dull one — quadrature, for example). We find that we are not free of the reverse tendency. Our selection of material is not completely arbitrary. One topic, Gray codes, was already used in the construction of quasi-random sequences (§7.7), and here needs only some additional explication. Two other topics, on diagnosing a computer’s floating-point parameters, and on arbitrary precision arithmetic, give additional insight into the machinery behind the casual assumption that computers are useful for doing things with numbers (as opposed to bits or characters). The latter of these topics also shows a very different use for Chapter 12’s fast Fourier transform. The three other topics (checksums, Huffman and arithmetic coding) involve different aspects of data coding, compression, and validation. If you handle a large amount of data — numerical data, even — then a passing familiarity with these subjects might at some point come in handy. In §13.6, for example, we already encountered a good use for Huffman coding. But again, you don’t have to read this chapter. (And you should learn about quadrature from Chapters 4 and 16, not from a computer science text!)
20.1 Diagnosing Machine Parameters A convenient fiction is that a computer’s floating-point arithmetic is “accurate enough.” If you believe this fiction, then numerical analysis becomes a very clean subject. Roundoff error disappears from view; many finite algorithms become “exact”; only docile truncation error (§1.2) stands between you and a perfect calculation. Sounds rather naive, doesn’t it? Yes, it is naive. Notwithstanding, it is a fiction necessarily adopted throughout most of this book. To do a good job of answering the question of how roundoff error 881
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Chapter 20. Less-Numerical Algorithms
882
Chapter 20.
Less-Numerical Algorithms
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
propagates, or can be bounded, for every algorithm that we have discussed would be impractical. In fact, it would not be possible: Rigorous analysis of many practical algorithms has never been made, by us or anyone. Proper numerical analysts cringe when they hear a user say, “I was getting roundoff errors with single precision, so I switched to double.” The actual meaning is, “for this particular algorithm, and my particular data, double precision seemed able to restore my erroneous belief in the ‘convenient fiction’.” We admit that most of the mentions of precision or roundoff in Numerical Recipes are only slightly more quantitative in character. That comes along with our trying to be “practical.” It is important to know what the limitations of your machine’s floating-point arithmetic actually are — the more so when your treatment of floating-point roundoff error is going to be intuitive, experimental, or casual. Methods for determining useful floating-point parameters experimentally have been developed by Cody [1], Malcolm [2], and others, and are embodied in the routine machar, below, which follows Cody’s implementation. All of machar’s arguments are returned values. Here is what they mean: • ibeta (called B in §1.2) is the radix in which numbers are represented, almost always 2, but occasionally 16, or even 10. • it is the number of base-ibeta digits in the floating-point mantissa M (see Figure 1.2.1). • machep is the exponent of the smallest (most negative) power of ibeta that, added to 1.0, gives something different from 1.0. • eps is the floating-point number ibetamachep, loosely referred to as the “floating-point precision.” • negep is the exponent of the smallest power of ibeta that, subtracted from 1.0, gives something different from 1.0. • epsneg is ibetanegep, another way of defining floating-point precision. Not infrequently epsneg is 0.5 times eps; occasionally eps and epsneg are equal. • iexp is the number of bits in the exponent (including its sign or bias). • minexp is the smallest (most negative) power of ibeta consistent with there being no leading zeros in the mantissa. • xmin is the floating-point number ibetaminexp, generally the smallest (in magnitude) useable floating value. • maxexp is the smallest (positive) power of ibeta that causes overflow. • xmax is (1−epsneg)×ibetamaxexp, generally the largest (in magnitude) useable floating value. • irnd returns a code in the range 0 . . . 5, giving information on what kind of rounding is done in addition, and on how underflow is handled. See below. • ngrd is the number of “guard digits” used when truncating the product of two mantissas to fit the representation. There is a lot of subtlety in a program like machar, whose purpose is to ferret out machine properties that are supposed to be transparent to the user. Further, it must do so avoiding error conditions, like overflow and underflow, that might interrupt its execution. In some cases the program is able to do this only by recognizing certain characteristics of “standard” representations. For example, it recognizes the IEEE standard representation [3] by its rounding behavior, and assumes certain features of its exponent representation as a consequence. We refer you to [1] and
883
20.1 Diagnosing Machine Parameters
Sample Results Returned by machar typical IEEE-compliant machine
DEC VAX
single
double
single
ibeta
2
2
2
it
24
53
24
machep
−23
−52
−24
eps
1.19 × 10−7
2.22 × 10−16
5.96 × 10−8
negep
−24
−53
−24
epsneg
5.96 × 10−8
1.11 × 10−16
5.96 × 10−8
iexp
8
11
8
minexp
−126
−1022
−128
xmin
1.18 × 10−38
2.23 × 10−308
2.94 × 10−39
maxexp
128
1024
127
xmax
3.40 × 1038
1.79 × 10308
1.70 × 1038
irnd
5
5
1
ngrd
0
0
0
references therein for details. Be aware that machar can give incorrect results on some nonstandard machines. The parameter irnd needs some additional explanation. In the IEEE standard, bit patterns correspond to exact, “representable” numbers. The specified method for rounding an addition is to add two representable numbers “exactly,” and then round the sum to the closest representable number. If the sum is precisely halfway between two representable numbers, it should be rounded to the even one (low-order bit zero). The same behavior should hold for all the other arithmetic operations, that is, they should be done in a manner equivalent to infinite precision, and then rounded to the closest representable number. If irnd returns 2 or 5, then your computer is compliant with this standard. If it returns 1 or 4, then it is doing some kind of rounding, but not the IEEE standard. If irnd returns 0 or 3, then it is truncating the result, not rounding it — not desirable. The other issue addressed by irnd concerns underflow. If a floating value is less than xmin, many computers underflow its value to zero. Values irnd = 0, 1, or 2 indicate this behavior. The IEEE standard specifies a more graceful kind of underflow: As a value becomes smaller than xmin, its exponent is frozen at the smallest allowed value, while its mantissa is decreased, acquiring leading zeros and “gracefully” losing precision. This is indicated by irnd = 3, 4, or 5.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
precision
884
*
1
2
3
4
5
6
Less-Numerical Algorithms
SUBROUTINE machar(ibeta,it,irnd,ngrd,machep,negep,iexp,minexp, maxexp,eps,epsneg,xmin,xmax) INTEGER ibeta,iexp,irnd,it,machep,maxexp,minexp,negep,ngrd REAL eps,epsneg,xmax,xmin Determines and returns machine-specific parameters affecting floating-point arithmetic. Returned values include ibeta, the floating-point radix; it, the number of base-ibeta digits in the floating-point mantissa; eps, the smallest positive number that, added to 1.0, is not equal to 1.0; epsneg, the smallest positive number that, subtracted from 1.0, is not equal to 1.0; xmin, the smallest representable positive number; and xmax, the largest representable positive number. See text for description of other returned parameters. INTEGER i,itemp,iz,j,k,mx,nxres REAL a,b,beta,betah,betain,one,t,temp,temp1,tempa,two,y,z ,zero,CONV CONV(i)=float(i) Change to dble(i), and change REAL declaration above to one=CONV(1) DOUBLE PRECISION to find double precision parameters. two=one+one zero=one-one a=one Determine ibeta and beta by the method of M. Malcolm. continue a=a+a temp=a+one temp1=temp-a if (temp1-one.eq.zero) goto 1 b=one continue b=b+b temp=a+b itemp=int(temp-a) if (itemp.eq.0) goto 2 ibeta=itemp beta=CONV(ibeta) it=0 Determine it and irnd. b=one continue it=it+1 b=b*beta temp=b+one temp1=temp-b if (temp1-one.eq.zero) goto 3 irnd=0 betah=beta/two temp=a+betah if (temp-a.ne.zero) irnd=1 tempa=a+beta temp=tempa+betah if ((irnd.eq.0).and.(temp-tempa.ne.zero)) irnd=2 negep=it+3 Determine negep and epsneg. betain=one/beta a=one do 11 i=1, negep a=a*betain enddo 11 b=a continue temp=one-a if (temp-one.ne.zero) goto 5 a=a*beta negep=negep-1 goto 4 negep=-negep epsneg=a machep=-it-3 Determine machep and eps. a=b continue
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
*
Chapter 20.
20.1 Diagnosing Machine Parameters
885
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
temp=one+a if (temp-one.ne.zero) goto 7 a=a*beta machep=machep+1 goto 6 7 eps=a ngrd=0 Determine ngrd. temp=one+eps if ((irnd.eq.0).and.(temp*one-one.ne.zero)) ngrd=1 i=0 Determine iexp. k=1 z=betain t=one+eps nxres=0 8 continue Loop until an underflow occurs, then exit. y=z z=y*y a=z*one Check here for the underflow. temp=z*t if ((a+a.eq.zero).or.(abs(z).ge.y)) goto 9 temp1=temp*betain if (temp1*beta.eq.z) goto 9 i=i+1 k=k+k goto 8 9 if (ibeta.ne.10) then iexp=i+1 mx=k+k else For decimal machines only. iexp=2 iz=ibeta 10 if (k.ge.iz) then iz=iz*ibeta iexp=iexp+1 goto 10 endif mx=iz+iz-1 endif 20 xmin=y To determine minexp and xmin, loop until an underflow ocy=y*betain curs, then exit. a=y*one Check here for the underflow. temp=y*t if (((a+a).ne.zero).and.(abs(y).lt.xmin)) then k=k+1 temp1=temp*betain if ((temp1*beta.ne.y).or.(temp.eq.y)) then goto 20 else nxres=3 xmin=y endif endif minexp=-k Determine maxexp, xmax. if ((mx.le.k+k-3).and.(ibeta.ne.10)) then mx=mx+mx iexp=iexp+1 endif maxexp=mx+minexp irnd=irnd+nxres Adjust irnd to reflect partial underflow. if (irnd.ge.2) maxexp=maxexp-2 Adjust for IEEE-style machines. i=maxexp+minexp Adjust for machines with implicit leading bit in binary mantissa, and machines with radix point at extreme right of mantissa. if ((ibeta.eq.2).and.(i.eq.0)) maxexp=maxexp-1
886
Chapter 20.
Less-Numerical Algorithms
Some typical values returned by machar are given in the table, above. IEEEcompliant machines referred to in the table include most UNIX workstations (SUN, DEC, MIPS), and Apple Macintosh IIs. IBM PCs with floating co-processors are generally IEEE-compliant, except that some compilers underflow intermediate results ungracefully, yielding irnd = 2 rather than 5. Notice, as in the case of a VAX (fourth column), that representations with a “phantom” leading 1 bit in the mantissa achieve a smaller eps for the same wordlength, but cannot underflow gracefully. CITED REFERENCES AND FURTHER READING: Goldberg, D. 1991, ACM Computing Surveys, vol. 23, pp. 5–48. Cody, W.J. 1988, ACM Transactions on Mathematical Software, vol. 14, pp. 303–311. [1] Malcolm, M.A. 1972, Communications of the ACM, vol. 15, pp. 949–951. [2] IEEE Standard for Binary Floating-Point Numbers, ANSI/IEEE Std 754–1985 (New York: IEEE, 1985). [3]
20.2 Gray Codes A Gray code is a function G(i) of the integers i, that for each integer N ≥ 0 is one-to-one for 0 ≤ i ≤ 2N − 1, and that has the following remarkable property: The binary representation of G(i) and G(i + 1) differ in exactly one bit. An example of a Gray code (in fact, the most commonly used one) is the sequence 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, and 1000, for i = 0, . . . , 15. The algorithm for generating this code is simply to form the bitwise exclusive-or (XOR) of i with i/2 (integer part). Think about how the carries work when you add one to a number in binary, and you will be able to see why this works. You will also see that G(i) and G(i + 1) differ in the bit position of the rightmost zero bit of i (prefixing a leading zero if necessary). The spelling is “Gray,” not “gray”: The codes are named after one Frank Gray, who first patented the idea for use in shaft encoders. A shaft encoder is a wheel with concentric coded stripes each of which is “read” by a fixed conducting brush. The idea is to generate a binary code describing the angle of the wheel. The obvious, but wrong, way to build a shaft encoder is to have one stripe (the innermost, say) conducting on half the wheel, but insulating on the other half; the next stripe is conducting in quadrants 1 and 3; the next stripe is conducting in octants 1, 3, 5, and 7; and so on. The brushes together then read a direct binary code for the position of the wheel.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
if (i.gt.20) maxexp=maxexp-1 if (a.ne.y) maxexp=maxexp-2 xmax=one-epsneg if (xmax*one.ne.xmax) xmax=one-beta*epsneg xmax=xmax/(beta*beta*beta*xmin) i=maxexp+minexp+3 do 12 j=1,i if (ibeta.eq.2) xmax=xmax+xmax if (ibeta.ne.2) xmax=xmax*beta enddo 12 return END
887
20.2 Gray Codes
MSB 4 3
XOR
3
2
XOR
2
1
XOR
1
0 LSB
XOR
0
G (i)
(a)
MSB 4 3 G (i)
2 1 0 LSB
4 3
XOR
2
XOR
i
1
XOR
XOR
0
(b) Figure 20.2.1. Single-bit operations for calculating the Gray code G(i) from i (a), or the inverse (b). LSB and MSB indicate the least and most significant bits, respectively. XOR denotes exclusive-or.
The reason this method is bad, is that there is no way to guarantee that all the brushes will make or break contact exactly simultaneously as the wheel turns. Going from position 7 (0111) to 8 (1000), one might pass spuriously and transiently through 6 (0110), 14 (1110), and 10 (1010), as the different brushes make or break contact. Use of a Gray code on the encoding stripes guarantees that there is no transient state between 7 (0100 in the sequence above) and 8 (1100). Of course we then need circuitry, or algorithmics, to translate from G(i) to i. Figure 20.2.1 (b) shows how this is done by a cascade of XOR gates. The idea is that each output bit should be the XOR of all more significant input bits. To do N bits of Gray code inversion requires N − 1 steps (or gate delays) in the circuit. (Nevertheless, this is typically very fast in circuitry.) In a register with word-wide binary operations, we don’t have to do N consecutive operations, but only ln2 N . The trick is to use the associativity of XOR and group the operations hierarchically. This involves sequential right-shifts by 1, 2, 4, 8, . . . bits until the wordlength is exhausted. Here is a piece of code for doing both G(i) and its inverse.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
i
4
888
Less-Numerical Algorithms
FUNCTION igray(n,is) INTEGER igray,is,n For zero or positive values of is, return the Gray code of n; if is is negative, return the inverse Gray code of n. INTEGER idiv,ish if (is.ge.0) then This is the easy direction! igray=ieor(n,n/2) else This is the more complicated direction: In hierarchical stages, ish=-1 starting with a one-bit right shift, cause each bit to be igray=n XORed with all more significant bits. continue idiv=ishft(igray,ish) igray=ieor(igray,idiv) if(idiv.le.1.or.ish.eq.-16)return ish=ish+ish Double the amount of shift on the next cycle. goto 1 endif return END
In numerical work, Gray codes can be useful when you need to do some task that depends intimately on the bits of i, looping over many values of i. Then, if there are economies in repeating the task for values differing by only one bit, it makes sense to do things in Gray code order rather than consecutive order. We saw an example of this in §7.7, for the generation of quasi-random sequences. CITED REFERENCES AND FURTHER READING: Horowitz, P., and Hill, W. 1989, The Art of Electronics, 2nd ed. (New York: Cambridge University Press), §8.02. Knuth, D.E. Combinatorial Algorithms, vol. 4 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §7.2.1. [Unpublished. Will it be always so?]
20.3 Cyclic Redundancy and Other Checksums When you send a sequence of bits from point A to point B, you want to know that it will arrive without error. A common form of insurance is the “parity bit,” attached to 7-bit ASCII characters to put them into 8-bit format. The parity bit is chosen so as to make the total number of one-bits (versus zero-bits) either always even (“even parity”) or always odd (“odd parity”). Any single bit error in a character will thereby be detected. When errors are sufficiently rare, and do not occur closely bunched in time, use of parity provides sufficient error detection. Unfortunately, in real situations, a single noise “event” is likely to disrupt more than one bit. Since the parity bit has two possible values (0 and 1), it gives, on average, only a 50% chance of detecting an erroneous character with more than one wrong bit. That probability, 50%, is not nearly good enough for most applications. Most communications protocols [1] use a multibit generalization of the parity bit called a “cyclic redundancy check” or CRC. In typical applications the CRC is 16 bits long (two bytes or two characters), so that the chance of a random error going undetected is 1 in 216 = 65536. Moreover, M -bit CRCs have the mathematical property of detecting all errors that occur in M or fewer consecutive bits, for any
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
Chapter 20.
20.3 Cyclic Redundancy and Other Checksums
889
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
length of message. (We prove this below.) Since noise in communication channels tends to be “bursty,” with short sequences of adjacent bits getting corrupted, this consecutive-bit property is highly desirable. Normally CRCs lie in the province of communications software experts and chip-level hardware designers — people with bits under their fingernails. However, there are at least two kinds of situations where some understanding of CRCs can be useful to the rest of us. First, we sometimes need to be able to communicate with a lower-level piece of hardware or software that expects a valid CRC as part of its input. For example, it can be convenient to have a program generate XMODEM or Kermit [2] packets directly into the communications line rather than having to store the data in a local file. Second, in the manipulation of large quantities of (e.g., experimental) data, it is useful to be able to tag aggregates of data (whether numbers, records, lines, or whole files) with a statistically unique “key,” its CRC. Aggregates of any size can then be compared for identity by comparing only their short CRC keys. Differing keys imply nonidentical records. Identical keys imply, to high statistical certainty, identical records. If you can’t tolerate the very small probability of being wrong, you can do a full comparison of the records when the keys are identical. When there is a possibility of files or data records being inadvertently or irresponsibly modified (for example, by a computer virus), it is useful to have their prior CRCs stored externally on a physically secure medium, like a floppy disk. Sometimes CRCs can be used to compress data as it is recorded. If identical data records occur frequently, one can keep sorted in memory the CRCs of previously encountered records. A new record is archived in full if its CRC is different, otherwise only a pointer to a previous record need be archived. In this application one might desire a 4- or 8-byte CRC, to make the odds of mistakenly discarding a different data record be tolerably small; or, if previous records can be randomly accessed, a full comparison can be made to decide whether records with identical CRCs are in fact identical. Now let us briefly discuss the theory of CRCs. After that, we will give implementations of various (related) CRCs that are used by the official or de facto standard protocols [1-3] listed in the accompanying table. The mathematics underlying CRCs is “polynomials over the integers modulo 2.” Any binary message can be thought of as a polynomial with coefficients 0 and 1. For example, the message “1100001101” is the polynomial x9 + x8 + x3 + x2 + 1. Since 0 and 1 are the only integers modulo 2, a power of x in the polynomial is either present (1) or absent (0). A polynomial over the integers modulo 2 may be irreducible, meaning that it can’t be factored. A subset of the irreducible polynomials are the “primitive” polynomials. These generate maximum length sequences when used in shift registers, as described in §7.4. The polynomial x2 + 1 is not irreducible: x2 +1 = (x+1)(x+1), so it is also not primitive. The polynomial x4 +x3 +x2 +x+1 is irreducible, but it turns out not to be primitive. The polynomial x4 + x + 1 is both irreducible and primitive. An M -bit long CRC is based on a particular primitive polynomial of degree M , called the generator polynomial. The choice of which primitive polynomial to use is only a matter of convention. For 16-bit CRC’s, the CCITT (Comit´e Consultatif International T´el´egraphique et T´el´ephonique) has anointed the “CCITT polynomial,” which is x16 + x12 + x5 + 1. This polynomial is used by all of the
890
Chapter 20.
Less-Numerical Algorithms
Conventions and Test Values for Various CRC Protocols icrc args Protocol
jinit jrev 1
Test Values (C2 C1 in hex)
Packet
T
CatMouse987654321
Format
CRC
1A71
E556
S1 S2 . . . SN C2 C1
0
0
X.25
255
−1 1B26
F56E
S1 S2 . . . SN C1 C2
F0B8
(no name)
255
−1 1B26
F56E
S1 S2 . . . SN C1 C2
0
SDLC (IBM)
same as X.25
HDLC (ISO)
same as X.25
CRC-CCITT
0
−1 14A1
C28D
S1 S2 . . . SN C1 C2
0
(no name)
0
−1 14A1
C28D
S1 S2 . . . SN C1 C2
F0B8
Kermit Notes:
same as CRC-CCITT
see Notes
Overbar denotes bit complement. S1 . . . SN are character data. C1 is CRC’s least significant 8 bits, C2 is its most significant 8 bits, so CRC = 256 C2 + C1 (shown in hex). Kermit (block check level 3) sends the CRC as 3 printable ASCII characters (sends value +32). These contain, respectively, 4 most significant bits, 6 middle bits, 6 least significant bits.
protocols listed in the table. Another common choice is the “CRC-16” polynomial x16 + x15 + x2 + 1, which is used for EBCDIC messages in IBM’s BISYNCH [1]. A common 12-bit choice, “CRC-12,” is x12 + x11 + x3 + x + 1. A common 32-bit choice, “AUTODIN-II,” is x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1. For a table of some other primitive polynomials, see §7.4. Given the generator polynomial G of degree M (which can be written either in polynomial form or as a bit-string, e.g., 10001000000100001 for CCITT), here is how you compute the CRC for a sequence of bits S: First, multiply S by xM , that is, append M zero bits to it. Second divide — by long division — G into SxM . Keep in mind that the subtractions in the long division are done modulo 2, so that there are never any “borrows”: Modulo 2 subtraction is the same as logical exclusive-or (XOR). Third, ignore the quotient you get. Fourth, when you eventually get to a remainder, it is the CRC, call it C. C will be a polynomial of degree M − 1 or less, otherwise you would not have finished the long division. Therefore, in bit string form, it has M bits, which may include leading zeros. (C might even be all zeros, see below.) See [3] for a worked example. If you work through the above steps in an example, you will see that most of what you write down in the long-division tableau is superfluous. You are actually just left-shifting sequential bits of S, from the right, into an M -bit register. Every time a 1 bit gets shifted off the left end of this register, you zap the register by an XOR with the M low order bits of G (that is, all the bits of G except its leading 1). When a 0 bit is shifted off the left end you don’t zap the register. When the last bit that was originally part of S gets shifted off the left end of the register, what remains is the CRC. You can immediately recognize how efficiently this procedure can be implemented in hardware. It requires only a shift register with a few hard-wired XOR taps into it. That is how CRCs are computed in communications devices, by a single chip (or small part of one). In software, the implementation is not so elegant, since
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
XMODEM
20.3 Cyclic Redundancy and Other Checksums
891
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
bit-shifting is not generally very efficient. One therefore typically finds (as in our implementation below) table-driven routines that pre-calculate the result of a bunch of shifts and XORs, say for each of 256 possible 8-bit inputs [4]. We can now see how the CRC gets its ability to detect all errors in M consecutive bits. Suppose two messages, S and T , differ only within a frame of M bits. Then their CRCs differ by an amount that is the remainder when G is divided into (S − T )xM ≡ D. Now D has the form of leading zeros (which can be ignored), followed by some 1’s in an M -bit frame, followed by trailing zeros (which are just multiplicative factors of x). Since factorization is unique, G cannot possibly divide D: G is primitive of degree M , while D is a power of x times a factor of (at most) degree M − 1. Therefore S and T have inevitably different CRCs. In most protocols, a transmitted block of data consists of some N data bits, directly followed by the M bits of their CRC (or the CRC XORed with a constant, see below). There are two equivalent ways of validating a block at the receiving end. Most obviously, the receiver can compute the CRC of the data bits, and compare it to the transmitted CRC bits. Less obviously, but more elegantly, the receiver can simply compute the CRC of the total block, with N + M bits, and verify that a result of zero is obtained. Proof: The total block is the polynomial SxM + C (data left-shifted to make room for the CRC bits). The definition of C is that Sxm = QG + C, where Q is the discarded quotient. But then SxM + C = QG + C + C = QG (remember modulo 2), which is a perfect multiple of G. It remains a multiple of G when it gets multiplied by an additional xM on the receiving end, so it has a zero CRC, q.e.d. A couple of small variations on the basic procedure need to be mentioned [1,3]: First, when the CRC is computed, the M -bit register need not be initialized to zero. Initializing it to some other M -bit value (e.g., all 1’s) in effect prefaces all blocks by a phantom message that would have given the initialization value as its remainder. It is advantageous to do this, since the CRC described thus far otherwise cannot detect the addition or removal of any number of initial zero bits. (Loss of an initial bit, or insertion of zero bits, are common “clocking errors.”) Second, one can add (XOR) any M -bit constant K to the CRC before it is transmitted. This constant can either be XORed away at the receiving end, or else it just changes the expected CRC of the whole block by a known amount, namely the remainder of dividing G into KxM . The constant K is frequently “all bits,” changing the CRC into its ones complement. This has the advantage of detecting another kind of error that the CRC would otherwise not find: deletion of an initial 1 bit in the message with spurious insertion of a 1 bit at the end of the block. The accompanying function icrc implements the above CRC calculation, including the possibility of the mentioned variations. Input to the function is the starting address of an array of characters, and the length of that array. (In practice, FORTRAN allows you to use the address of any data structure; icrc will treat it as a byte array.) Output is in both of two formats. The function value returns the CRC as a 4-byte integer in the range 0 to 65535. The character array crc, of length 2, returns the CRC as two 8-bit characters. icrc has two “switch” arguments that specify variations in the CRC calculation. A zero or positive value of jinit causes the 16-bit register to have each byte initialized with the value jinit. A negative value of jrev causes each input character to be interpreted as its bit-reverse image, and a similar bit reversal to be done on the output CRC. You do not have to understand this; just use the values of jinit and jrev specified in the table.
892
Chapter 20.
Less-Numerical Algorithms
FUNCTION icrc1(crc,onech,ib1,ib2,ib3) INTEGER icrc1,ib1,ib2,ib3 Given a remainder up to now, return the new CRC after one character is added. This routine is functionally equivalent to icrc(,,1,-1,1), but slower. It is used by icrc to initialize its table. INTEGER i,ichr,ireg CHARACTER*1 onech,crc(4),creg(4) EQUIVALENCE (creg,ireg) ireg=0 creg(ib1)=crc(ib1) Here is where the character is folded into the register. creg(ib2)=char(ieor(ichar(crc(ib2)),ichar(onech))) do 11 i=1,8 Here is where 8 one-bit shifts, and some XORs with the genichr=ichar(creg(ib2)) erator polynomial, are done. ireg=ireg+ireg creg(ib3)=char(0) if(ichr.gt.127)ireg=ieor(ireg,4129) enddo 11 icrc1=ireg return END
Now look at icrc. There are two parts to understand, how it builds a table when it initializes, and how it uses that table later on. Go back to thinking about a character’s bits being shifted into the CRC register from the least significant end. The key observation is that while 8 bits are being shifted into the register’s low end, all the generator zapping is being determined by the bits already in the high end. Since XOR is commutative and associative, all we need is a table of the result of all this zapping, for each of 256 possible high-bit configurations. Then we can play catch-up and XOR an input character into the result of a lookup into this table. The routine makes repeated use of an equivalenced 4-byte integer and 4-byte
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
(If you insist on knowing, the explanation is that serial data ports send characters least-significant bit first (!), and many protocols shift bits into the CRC register in exactly the order received.) The table shows how to construct a block of characters from the input array and output CRC of icrc. You should not need to do any additional bit-reversal outside of icrc. The switch jinit has one additional use: When negative it causes the input value of the array crc to be used as initialization of the register. If crc is unmodified since the last call to icrc, this in effect appends the current input array to that of the previous call or calls. Use this feature, for example, to build up the CRC of a whole file a line at a time, without keeping the whole file in memory. At initialization, the routine icrc figures out the order in which the bytes occur when a 4-byte character array is equivalenced to a 4-byte integer. This is not strictly portable FORTRAN, but it should work on all machines with 32-bit word lengths. icrc is loosely based on a more portable C function in [4], a good place to turn if you have trouble running the program here. Here is how to understand the operation of icrc: First look at the function icrc1. This incorporates one input character into a 16-bit CRC register. The only trick used is that character bits are XORed into the most significant bits, eight at a time, instead of being fed into the least significant bit, one bit at a time, at the time of the register shift. This works because XOR is associative and commutative — we can feed in character bits any time before they will determine whether to zap with the generator polynomial. (The decimal constant 4129 has the generator’s bits in it.)
20.3 Cyclic Redundancy and Other Checksums
893
character array to get at different 8-bit chunks.The only other content to icrc is the construction at initialization time of an 8-bit bit-reverse table from the 4-bit table stored in it, and the logic associated with doing the bit reversals. References [4-6] give further details on table-driven CRC computations. Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C
FUNCTION icrc(crc,bufptr,len,jinit,jrev) INTEGER icrc,jinit,jrev,len CHARACTER*1 bufptr(*),crc(2) USES icrc1 Computes a 16-bit Cyclic Redundancy Check for an array bufptr of length len bytes, using any of several conventions as determined by the settings of jinit and jrev (see accompanying table). The result is returned both as an integer icrc and as a 2-byte array crc. If jinit is negative, then crc is used on input to initialize the remainder register, in effect concatenating bufptr to the previous call. INTEGER ich,init,ireg,j,icrctb(0:255),it(0:15),icrc1,ib1,ib2,ib3 CHARACTER*1 creg(4),rchr(0:255) SAVE icrctb,rchr,init,it,ib1,ib2,ib3 EQUIVALENCE (creg,ireg) Used to get at the 4 bytes in an integer. DATA it/0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15/, init /0/ Table of 4-bit bit-reverses, and flag for initialization. if (init.eq.0) then Do we need to initialize tables? init=1 ireg=256*(256*ichar(’3’)+ichar(’2’))+ichar(’1’) do 11 j=1,4 Figure out which component of creg addresses which if (creg(j).eq.’1’) ib1=j byte of ireg. if (creg(j).eq.’2’) ib2=j if (creg(j).eq.’3’) ib3=j enddo 11 do 12 j=0,255 The two tables are: CRCs of all characters, and bit-reverses ireg=j*256 of all characters. icrctb(j)=icrc1(creg,char(0),ib1,ib2,ib3) ich=it(mod(j,16))*16+it(j/16) rchr(j)=char(ich) enddo 12 endif if (jinit.ge.0) then Initialize the remainder register. crc(1)=char(jinit) crc(2)=char(jinit) else if (jrev.lt.0) then If not initializing, do we reverse the register? ich=ichar(crc(1)) crc(1)=rchr(ichar(crc(2))) crc(2)=rchr(ich) endif do 13 j=1,len Main loop over the characters in the array. ich=ichar(bufptr(j)) if(jrev.lt.0)ich=ichar(rchr(ich)) ireg=icrctb(ieor(ich,ichar(crc(2)))) crc(2)=char(ieor(ichar(creg(ib2)),ichar(crc(1)))) crc(1)=creg(ib1) enddo 13 if (jrev.ge.0) then Do we need to reverse the output? creg(ib1)=crc(1) creg(ib2)=crc(2) else creg(ib2)=rchr(ichar(crc(1))) creg(ib1)=rchr(ichar(crc(2))) crc(1)=creg(ib1) crc(2)=creg(ib2) endif icrc=ireg return END
894
Chapter 20.
Less-Numerical Algorithms
Other Kinds of Checksums Quite different from CRCs are the various techniques used to append a decimal “check digit” to numbers that are handled by human beings (e.g., typed into a computer). Check digits need to be proof against the kinds of highly structured errors that humans tend to make, such as transposing consecutive digits. Wagner and Putter [7] give an interesting introduction to this subject, including specific algorithms. Checksums now in widespread use vary from fair to poor. The 10-digit ISBN (International Standard Book Number) that you find on most books, including this one, uses the check equation 10d1 + 9d2 + 8d3 + · · · + 2d9 + d10 = 0 (mod 11)
(20.3.1)
where d10 is the right-hand check digit. The character “X” is used to represent a check digit value of 10. Another popular scheme is the so-called “IBM check,” often used for account numbers (including, e.g., MasterCard). Here, the check equation is 2#d1 + d2 + 2#d3 + d4 + · · · = 0 (mod 10)
(20.3.2)
where 2#d means, “multiply d by two and add the resulting decimal digits.” United States banks code checks with a 9-digit processing number whose check equation is 3a1 + 7a2 + a3 + 3a4 + 7a5 + a6 + 3a7 + 7a8 + a9 = 0 (mod 10) (20.3.3) The bar code put on many envelopes by the U.S. Postal Service is decoded by removing the single tall marker bars at each end, and breaking the remaining bars into 6 or 10 groups of five. In each group the five bars signify (from left to right) the values 7,4,2,1,0. Exactly two of them will be tall. Their sum is the represented digit, except that zero is represented as 7 + 4. The 5- or 9-digit Zip Code is followed by a check digit, with the check equation X (20.3.4) di = 0 (mod 10) None of these schemes is close to optimal. An elegant scheme due to Verhoeff is described in [7]. The underlying idea is to use the ten-element dihedral group D5 ,
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
What if you need a 32-bit checksum? For a true 32-bit CRC, you will need to rewrite the routines given to work with a longer generating polynomial. For example, x32 + x7 + x5 + x3 + x2 + x + 1 is primitive modulo 2, and has nonleading, nonzero bits only in its least significant byte (which makes for some simplification). The idea of table lookup on only the most significant byte of the CRC register goes through unchanged. Pay attention to the fact that FORTRAN does not have unsigned integers, so half of your CRCs will appear to be negative in integer format. If you do not care about the M -consecutive bit property of the checksum, but rather only need a statistically random 32 bits, then you can use icrc as given here: Call it once with jrev = 1 to get 16 bits, and again with jrev = −1 to get another 16 bits. The internal bit reversals make these two 16-bit CRCs in effect totally independent of each other.
20.3 Cyclic Redundancy and Other Checksums
895
which corresponds to the symmetries of a pentagon, instead of the cyclic group of the integers modulo 10. The check equation is a1 *f(a2 )*f 2 (a3 )* · · · *f n−1 (an ) = 0
(20.3.5)
* * * * * *
1
LOGICAL FUNCTION decchk(string,n,ch) INTEGER n CHARACTER string*(*),ch*1 Decimal check digit computation or verification. Returns as ch a check digit for appending to string(1:n), that is, for storing into string(n+1:n+1). In this mode, ignore the returned logical value. If string(1:n) already ends with a check digit (string(n:n)), returns the function value .true. if the check digit is valid, otherwise .false. In this mode, ignore the returned value of ch. Note that string and ch contain ASCII characters corresponding to the digits 0-9, not byte values in that range. Other ASCII characters are allowed in string, and are ignored in calculating the check digit. INTEGER ij(10,10),ip(10,8),i,j,k,m SAVE ij,ip Group multiplication and permutation tables. DATA ip/0,1,2,3,4,5,6,7,8,9,1,5,7,6,2,8,3,0,9,4, 5,8,0,3,7,9,6,1,4,2,8,9,1,6,0,4,3,5,2,7,9,4,5,3,1,2,6,8,7,0, 4,2,8,6,5,7,3,9,0,1,2,7,9,3,8,0,6,4,1,5,7,0,4,6,9,1,3,2,5,8/, ij/0,1,2,3,4,5,6,7,8,9,1,2,3,4,0,9,5,6,7,8,2,3,4,0,1,8,9,5,6, 7,3,4,0,1,2,7,8,9,5,6,4,0,1,2,3,6,7,8,9,5,5,6,7,8,9,0,1,2,3, 4,6,7,8,9,5,4,0,1,2,3,7,8,9,5,6,3,4,0,1,2,8,9,5,6,7,2,3,4,0, 1,9,5,6,7,8,1,2,3,4,0/ k=0 m=0 do 11 j=1,n Look at successive characters. i=ichar(string(j:j)) if (i.ge.48.and.i.le.57)then Ignore everything except digits. k=ij(k+1,ip(mod(i+2,10)+1,mod(m,8)+1)+1) m=m+1 endif enddo 11 decchk=(k.eq.0) do 12 i=0,9 Find which appended digit will check properly. if (ij(k+1,ip(i+1,mod(m,8)+1)+1).eq.0) goto 1 enddo 12 ch=char(i+48) Convert to ASCII. return end
CITED REFERENCES AND FURTHER READING: McNamara, J.E. 1982, Technical Aspects of Data Communication, 2nd ed. (Bedford, MA: Digital Press). [1] da Cruz, F. 1987, Kermit, A File Transfer Protocol (Bedford, MA: Digital Press). [2] Morse, G. 1986, Byte, vol. 11, pp. 115–124 (September). [3] LeVan, J. 1987, Byte, vol. 12, pp. 339–341 (November). [4] Sarwate, D.V. 1988, Communications of the ACM, vol. 31, pp. 1008–1013. [5] Griffiths, G., and Stones, G.C. 1987, Communications of the ACM, vol. 30, pp. 617–620. [6] Wagner, N.R., and Putter, P.S. 1989, Communications of the ACM, vol. 32, pp. 106–110. [7]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
where * is (noncommutative) multiplication in D5 , and f i denotes the ith iteration of a certain fixed permutation. Verhoeff’s method finds all single errors in a string, and all adjacent transpositions. It also finds about 95% of twin errors (aa → bb), jump transpositions (acb → bca), and jump twin errors (aca → bcb). Here is an implementation:
896
Chapter 20.
Less-Numerical Algorithms
20.4 Huffman Coding and Compression of Data
bits per character. Here H is the entropy of the probability distribution. Moreover, coding schemes exist which approach the bound arbitrarily closely. For the case of equiprobable characters, with all pi = 1/Nch , one easily sees that H = log2 Nch , which is the case of no compression at all. Any other set of pi ’s gives a smaller entropy, allowing some useful compression. Notice that the bound of (20.4.1) would be achieved if we could encode character i with a P code of length Li = − log2 pi bits: Equation (20.4.1) would then be the average pi Li . The trouble with such a scheme is that − log2 pi is not generally an integer. How can we encode the letter “Q” in 5.32 bits? Huffman coding makes a stab at this by, in effect, approximating all the probabilities pi by integer powers of 1/2, so that all the Li ’s are integral. If all the pi ’s are in fact of this form, then a Huffman code does achieve the entropy bound H. The construction of a Huffman code is best illustrated by example. Imagine a language, Vowellish, with the Nch = 5 character alphabet A, E, I, O, and U, occurring with the respective probabilities 0.12, 0.42, 0.09, 0.30, and 0.07. Then the construction of a Huffman code for Vowellish is accomplished in the following table:
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
A lossless data compression algorithm takes a string of symbols (typically ASCII characters or bytes) and translates it reversibly into another string, one that is on the average of shorter length. The words “on the average” are crucial; it is obvious that no reversible algorithm can make all strings shorter — there just aren’t enough short strings to be in one-to-one correspondence with longer strings. Compression algorithms are possible only when, on the input side, some strings, or some input symbols, are more common than others. These can then be encoded in fewer bits than rarer input strings or symbols, giving a net average gain. There exist many, quite different, compression techniques, corresponding to different ways of detecting and using departures from equiprobability in input strings. In this section and the next we shall consider only variable length codes with defined word inputs. In these, the input is sliced into fixed units, for example ASCII characters, while the corresponding output comes in chunks of variable size. The simplest such method is Huffman coding [1], discussed in this section. Another example, arithmetic compression, is discussed in §20.5. At the opposite extreme from defined-word, variable length codes are schemes that divide up the input into units of variable length (words or phrases of English text, for example) and then transmit these, often with a fixed-length output code. The most widely used code of this type is the Ziv-Lempel code [2]. References [3-6] give the flavor of some other compression techniques, with references to the large literature. The idea behind Huffman coding is simply to use shorter bit patterns for more common characters. We can make this idea quantitative by considering the concept of entropy. Suppose the input alphabet has Nch characters, and that these Poccur in pi = 1. the input string with respective probabilities pi , i = 1, . . . , Nch , so that Then the fundamental theorem of information theory says that strings consisting of independently random sequences of these characters (a conservative, but not always realistic assumption) require, on the average, at least X H=− pi log2 pi (20.4.1)
897
20.4 Huffman Coding and Compression of Data
Node Stage:
1
2
3
4
0.42
A:
0.12
0.12
2
E:
0.42
0.42
0.42
3
I:
0.09
4
O:
0.30
0.30
0.30
5
U:
0.07
6 7 8 9
UI:
0.16 AUI:
0.28 AUIO:
0.58 EAUIO: 1.00
Here is how it works, proceeding in sequence through Nch stages, represented by the columns of the table. The first stage starts with Nch nodes, one for each letter of the alphabet, containing their respective relative frequencies. At each stage, the two smallest probabilities are found, summed to make a new node, and then dropped from the list of active nodes. (A “block” denotes the stage where a node is dropped.) All active nodes (including the new composite) are then carried over to the next stage (column). In the table, the names assigned to new nodes (e.g., AUI) are inconsequential. In the example shown, it happens that (after stage 1) the two smallest nodes are always an original node and a composite one; this need not be true in general: The two smallest probabilities might be both original nodes, or both composites, or one of each. At the last stage, all nodes will have been collected into one grand composite of total probability 1. Now, to see the code, you redraw the data in the above table as a tree (Figure 20.4.1). As shown, each node of the tree corresponds to a node (row) in the table, indicated by the integer to its left and probability value to its right. Terminal nodes, so called, are shown as circles; these are single alphabetic characters. The branches of the tree are labeled 0 and 1. The code for a character is the sequence of zeros and ones that lead to it, from the top down. For example, E is simply 0, while U is 1010. Any string of zeros and ones can now be decoded into an alphabetic sequence. Consider, for example, the string 1011111010. Starting at the top of the tree we descend through 1011 to I, the first character. Since we have reached a terminal node, we reset to the top of the tree, next descending through 11 to O. Finally 1010 gives U. The string thus decodes to IOU. These ideas are embodied in the following routines. Input to the first routine hufmak is an integer vector of the frequency of occurrence of the nchin ≡ Nch alphabetic characters, i.e., a set of integers proportional to the pi ’s. hufmak, along with hufapp, which it calls, performs the construction of the above table, and also the tree of Figure 20.4.1. The routine utilizes a heap structure (see §8.3) for efficiency; for a detailed description, see Sedgewick [7].
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
5
898
Chapter 20.
Less-Numerical Algorithms
9 EAUIO 1.00 0
1
2 E 0.42
8
AUIO
7
AUI
0
1 0.28
4 O 0.30
1
1 A 0.12 6 0 5 U 0.07
UI
0.16 1 3 I 0.09
Figure 20.4.1. Huffman code for the fictitious language Vowellish, in tree form. A letter (A, E, I, O, or U) is encoded or decoded by traversing the tree from the top down; the code is the sequence of 0’s and 1’s on the branches. The value to the right of each node is its probability; to the left, its node number in the accompanying table.
C
*
1
SUBROUTINE hufmak(nfreq,nchin,ilong,nlong) INTEGER ilong,nchin,nlong,nfreq(nchin),MC,MQ PARAMETER (MC=512,MQ=2*MC-1) USES hufapp Given the frequency of occurrence table nfreq(1:nchin) of nchin characters, construct in the common block /hufcom/ the Huffman code. Returned values ilong and nlong are the character number that produced the longest code symbol, and the length of that symbol. You should check that nlong is not larger than your machine’s word length. INTEGER ibit,j,k,n,nch,node,nodemx,nused,ibset,index(MQ), iup(MQ),icod(MQ),left(MQ),iright(MQ),ncod(MQ),nprob(MQ) COMMON /hufcom/ icod,ncod,nprob,left,iright,nch,nodemx SAVE /hufcom/ nch=nchin Initialization. nused=0 do 11 j=1,nch nprob(j)=nfreq(j) icod(j)=0 ncod(j)=0 if(nfreq(j).ne.0)then nused=nused+1 index(nused)=j endif enddo 11 do 12 j=nused,1,-1 Sort nprob into a heap structure in index. call hufapp(index,nprob,nused,j) enddo 12 k=nch if(nused.gt.1)then Combine heap nodes, remaking the heap at each stage. node=index(1) index(1)=index(nused) nused=nused-1 call hufapp(index,nprob,nused,1) k=k+1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
0
0.58
20.4 Huffman Coding and Compression of Data
2
3
nprob(k)=nprob(index(1))+nprob(node) left(k)=node Store left and right children of a node. iright(k)=index(1) iup(index(1)) = -k Indicate whether a node is a left or right child of its parent. iup(node)=k index(1)=k call hufapp(index,nprob,nused,1) goto 1 endif nodemx=k iup(nodemx)=0 do 13 j=1,nch Make the Huffman code from the tree. if(nprob(j).ne.0)then n=0 ibit=0 node=iup(j) if(node.ne.0)then if(node.lt.0)then n=ibset(n,ibit) node = -node endif node=iup(node) ibit=ibit+1 goto 2 endif icod(j)=n ncod(j)=ibit endif enddo 13 nlong=0 do 14 j=1,nch if(ncod(j).gt.nlong)then nlong=ncod(j) ilong=j-1 endif enddo 14 return END
SUBROUTINE hufapp(index,nprob,m,l) INTEGER m,l,MC,MQ PARAMETER (MC=512,MQ=2*MC-1) INTEGER index(MQ),nprob(MQ) Used by hufmak to maintain a heap structure in the array index(1:l). INTEGER i,j,k,n n=m i=l k=index(i) if(i.le.n/2)then j=i+i if (j.lt.n.and.nprob(index(j)).gt.nprob(index(j+1))) j=j+1 if (nprob(k).le.nprob(index(j))) goto 3 index(i)=index(j) i=j goto 2 endif index(i)=k return END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
2
899
900
Chapter 20.
Less-Numerical Algorithms
Once the code is constructed, one encodes a string of characters by repeated calls to hufenc, which simply does a table lookup of the code and appends it to the output message.
Decoding a Huffman-encoded message is slightly more complicated. The coding tree must be traversed from the top down, using up a variable number of bits:
1
SUBROUTINE hufdec(ich,code,lcode,nb) INTEGER ich,lcode,nb,MC,MQ PARAMETER (MC=512,MQ=2*MC-1) Starting at bit number nb in the character array code(1:lcode), use the Huffman code stored in common block /hufcom/ to decode a single character (returned as ich in the range 0..nch-1) and increment nb appropriately. Repeated calls, starting with nb = 0 will return successive characters in a compressed message. The returned value ich=nch indicates end-of-message. This routine must be preceded by a single initializing call to hufmak. Parameters: MC is the maximum value of nch, the input alphabet size. INTEGER l,nc,nch,node,nodemx INTEGER icod(MQ),left(MQ),iright(MQ),ncod(MQ),nprob(MQ) LOGICAL btest CHARACTER*1 code(lcode) COMMON /hufcom/ icod,ncod,nprob,left,iright,nch,nodemx SAVE /hufcom/ node=nodemx Set node to the top of the decoding tree. continue Loop until a valid character is obtained. nc=nb/8+1 if (nc.gt.lcode)then Ran out of input; with ich=nch indicating end of message. ich=nch return endif l=mod(nb,8) Now decoding this bit. nb=nb+1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE hufenc(ich,code,lcode,nb) INTEGER ich,lcode,nb,MC,MQ PARAMETER (MC=512,MQ=2*MC-1) Huffman encode the single character ich (in the range 0..nch-1), write the result to the character array code(1:lcode) starting at bit nb (whose smallest valid value is zero), and increment nb appropriately. This routine is called repeatedly to encode consecutive characters in a message, but must be preceded by a single initializing call to hufmak. INTEGER k,l,n,nc,nch,nodemx,ntmp,ibset INTEGER icod(MQ),left(MQ),iright(MQ),ncod(MQ),nprob(MQ) LOGICAL btest CHARACTER*1 code(*) COMMON /hufcom/ icod,ncod,nprob,left,iright,nch,nodemx SAVE /hufcom/ k=ich+1 Convert character range 0..nch-1 to array index range 1..nch. if(k.gt.nch.or.k.lt.1)pause ’ich out of range in hufenc.’ do 11 n=ncod(k),1,-1 Loop over the bits in the stored Huffman code for ich. nc=nb/8+1 if (nc.gt.lcode) pause ’lcode too small in hufenc.’ l=mod(nb,8) if (l.eq.0) code(nc)=char(0) if(btest(icod(k),n-1))then Set appropriate bits in code. ntmp=ibset(ichar(code(nc)),l) code(nc)=char(ntmp) endif nb=nb+1 enddo 11 return END
20.4 Huffman Coding and Compression of Data
901
For simplicity, hufdec quits when it runs out of code bytes; if your coded message is not an integral number of bytes, and if Nch is less than 256, hufdec can return a spurious final character or two, decoded from the spurious trailing bits in your last code byte. If you have independent knowledge of the number of characters sent, you can readily discard these. Otherwise, you can fix this behavior by providing a bit, not byte, count, and modifying the routine accordingly. (When Nch is 256 or larger, hufdec will normally run out of code in the middle of a spurious character, and it will be discarded.)
Run-Length Encoding For the compression of highly correlated bit-streams (for example the black or white values along a facsimile scan line), Huffman compression is often combined with run-length encoding: Instead of sending each bit, the input stream is converted to a series of integers indicating how many consecutive bits have the same value. These integers are then Huffman-compressed. The Group 3 CCITT facsimile standard functions in this manner, with a fixed, immutable, Huffman code, optimized for a set of eight standard documents [8,9] .
CITED REFERENCES AND FURTHER READING: Gallager, R.G. 1968, Information Theory and Reliable Communication (New York: Wiley). Hamming, R.W. 1980, Coding and Information Theory (Englewood Cliffs, NJ: Prentice-Hall). Storer, J.A. 1988, Data Compression: Methods and Theory (Rockville, MD: Computer Science Press). Nelson, M. 1991, The Data Compression Book (Redwood City, CA: M&T Books). Huffman, D.A. 1952, Proceedings of the Institute of Radio Engineers, vol. 40, pp. 1098–1101. [1] Ziv, J., and Lempel, A. 1978, IEEE Transactions on Information Theory, vol. IT-24, pp. 530–536. [2] Cleary, J.G., and Witten, I.H. 1984, IEEE Transactions on Communications, vol. COM-32, pp. 396–402. [3] Welch, T.A. 1984, Computer, vol. 17, no. 6, pp. 8–19. [4] Bentley, J.L., Sleator, D.D., Tarjan, R.E., and Wei, V.K. 1986, Communications of the ACM, vol. 29, pp. 320–330. [5] Jones, D.W. 1988, Communications of the ACM, vol. 31, pp. 996–1007. [6] Sedgewick, R. 1988, Algorithms, 2nd ed. (Reading, MA: Addison-Wesley), Chapter 22. [7] Hunter, R., and Robinson, A.H. 1980, Proceedings of the IEEE, vol. 68, pp. 854–867. [8] Marking, M.P. 1990, The C Users’ Journal, vol. 8, no. 6, pp. 45–54. [9]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
if(btest(ichar(code(nc)),l))then Branch left or right in tree, depending on its node=iright(node) value. else node=left(node) endif if(node.le.nch)then If we reach a terminal node, we have a complete character ich=node-1 and can return. return endif goto 1 END
902
Chapter 20.
Less-Numerical Algorithms
20.5 Arithmetic Coding
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
We saw in the previous section that a perfect (entropy-bounded) coding scheme would use Li = − log2 pi bits to encode character i (in the range 1 ≤ i ≤ Nch ), if pi is its probability of occurrence. Huffman coding gives a way of rounding the Li ’s to close integer values and constructing a code with those lengths. Arithmetic coding [1], which we now discuss, actually does manage to encode characters using noninteger numbers of bits! It also provides a convenient way to output the result not as a stream of bits, but as a stream of symbols in any desired radix. This latter property is particularly useful if you want, e.g., to convert data from bytes (radix 256) to printable ASCII characters (radix 94), or to case-independent alphanumeric sequences containing only A-Z and 0-9 (radix 36). In arithmetic coding, an input message of any length is represented as a real number R in the range 0 ≤ R < 1. The longer the message, the more precision required of R. This is best illustrated by an example, so let us return to the fictitious language, Vowellish, of the previous section. Recall that Vowellish has a 5 character alphabet (A, E, I, O, U), with occurrence probabilities 0.12, 0.42, 0.09, 0.30, and 0.07, respectively. Figure 20.5.1 shows how a message beginning “IOU” is encoded: The interval [0, 1) is divided into segments corresponding to the 5 alphabetical characters; the length of a segment is the corresponding pi . We see that the first message character, “I”, narrows the range of R to 0.37 ≤ R < 0.46. This interval is now subdivided into five subintervals, again with lengths proportional to the pi ’s. The second message character, “O”, narrows the range of R to 0.3763 ≤ R < 0.4033. The “U” character further narrows the range to 0.37630 ≤ R < 0.37819. Any value of R in this range can be sent as encoding “IOU”. In particular, the binary fraction .011000001 is in this range, so “IOU” can be sent in 9 bits. (Huffman coding took 10 bits for this example, see §20.4.) Of course there is the problem of knowing when to stop decoding. The fraction .011000001 represents not simply “IOU,” but “IOU. . . ,” where the ellipses represent an infinite string of successor characters. To resolve this ambiguity, arithmetic coding generally assumes the existence of a special Nch + 1th character, EOM (end of message), which occurs only once at the end of the input. Since EOM has a low probability of occurrence, it gets allocated only a very tiny piece of the number line. In the above example, we gave R as a binary fraction. We could just as well have output it in any other radix, e.g., base 94 or base 36, whatever is convenient for the anticipated storage or communication channel. You might wonder how one deals with the seemingly incredible precision required of R for a long message. The answer is that R is never actually represented all at once. At any give stage we have upper and lower bounds for R represented as a finite number of digits in the output radix. As digits of the upper and lower bounds become identical, we can left-shift them away and bring in new digits at the low-significance end. The routines below have a parameter NWK for the number of working digits to keep around. This must be large enough to make the chance of an accidental degeneracy vanishingly small. (The routines signal if a degeneracy ever occurs.) Since the process of discarding old digits and bringing in new ones is performed identically on encoding and decoding, everything stays synchronized.
903
20.5 Arithmetic Coding
0.4033
0.46
1.0 A 0.9
A
0.37819 A
A 0.3780
0.45 0.400
0.8 E
0.43
E
0.395
E
0.3776
E
0.3774
0.6 0.42
0.4
0.3772
0.390
0.5 I
0.41
I
0.40
0.385
0.3 O 0.2
0.39
I
O
0.3770
I
0.3768 O
O 0.3766
0.380 0.38
0.1 U 0.0
0.3764 U
0.37
U
U 0.3763
0.37630
Figure 20.5.1. Arithmetic coding of the message “IOU...” in the fictitious language Vowellish. Successive characters give successively finer subdivisions of the initial interval between 0 and 1. The final value can be output as the digits of a fraction in any desired radix. Note how the subinterval allocated to a character is proportional to its probability of occurrence.
The routine arcmak constructs the cumulative frequency distribution table used to partition the interval at each stage. In the principal routine arcode, when an interval of size jdif is to be partitioned in the proportions of some n to some ntot, say, then we must compute (n*jdif)/ntot. With integer arithmetic, the numerator is likely to overflow; and, unfortunately, an expression like jdif/(ntot/n) is not equivalent. In the implementation below, we resort to double precision floating arithmetic for this calculation. Not only is this inefficient, but different roundoff errors can (albeit very rarely) make different machines encode differently, though any one type of machine will decode exactly what it encoded, since identical roundoff errors occur in the two processes. For serious use, one needs to replace this floating calculation with an integer computation in a double register (not available to the FORTRAN programmer). The internally set variable minint, which is the minimum allowed number of discrete steps between the upper and lower bounds, determines when new lowsignificance digits are added. minint must be large enough to provide resolution of all the input characters. That is, we must have pi × minint > 1 for all i. A value of 100Nch, or 1.1/ min pi , whichever is larger, is generally adequate. However, for safety, the routine below takes minint to be as large as possible, with the product minint*nradd just smaller than overflow. This results in some time inefficiency, and in a few unnecessary characters being output at the end of a message. You can
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
0.7
0.3778
0.44
904
Chapter 20.
Less-Numerical Algorithms
*
SUBROUTINE arcmak(nfreq,nchh,nradd) INTEGER nchh,nradd,nfreq(nchh),MC,NWK,MAXINT PARAMETER (MC=512,NWK=20,MAXINT=2147483647) Given a table nfreq(1:nchh) of the frequency of occurrence of nchh symbols, and given a desired output radix nradd, initialize the cumulative frequency table and other variables for arithmetic compression. Parameters: MC is largest anticipated value of nchh; NWK is the number of working digits (see text); MAXINT is a large positive integer that does not overflow. INTEGER j,jdif,minint,nc,nch,nrad,ncum, ncumfq(MC+2),ilob(NWK),iupb(NWK) COMMON /arccom/ ncumfq,iupb,ilob,nch,nrad,minint,jdif,nc,ncum SAVE /arccom/ if(nchh.gt.MC)pause ’MC too small in arcmak’ if(nradd.gt.256)pause ’nradd may not exceed 256 in arcmak’ minint=MAXINT/nradd nch=nchh nrad=nradd ncumfq(1)=0 do 11 j=2,nch+1 ncumfq(j)=ncumfq(j-1)+max(nfreq(j-1),1) enddo 11 ncumfq(nch+2)=ncumfq(nch+1)+1 ncum=ncumfq(nch+2) return END
Individual characters in a message are coded or decoded by the routine arcode, which in turn uses the utility arcsum.
C
*
SUBROUTINE arcode(ich,code,lcode,lcd,isign) INTEGER ich,isign,lcd,lcode,MC,NWK CHARACTER*1 code(lcode) PARAMETER (MC=512,NWK=20) USES arcsum Compress (isign = 1) or decompress (isign = −1) the single character ich into or out of the character array code(1:lcode), starting with byte code(lcd) and (if necessary) incrementing lcd so that, on return, lcd points to the first unused byte in code. Note that this routine saves the result of previous calls until a new byte of code is produced, and only then increments lcd. An initializing call with isign=0 is required for each different array code. The routine arcmak must have previously been called to initialize the common block /arccom/. A call with ich=nch (as set in arcmak) has the reserved meaning “end of message.” INTEGER ihi,j,ja,jdif,jh,jl,k,m,minint,nc,nch,nrad,ilob(NWK), iupb(NWK),ncumfq(MC+2),ncum,JTRY COMMON /arccom/ ncumfq,iupb,ilob,nch,nrad,minint,jdif,nc,ncum SAVE /arccom/ The following statement function is used to calculate (k*j)/m without overflow. Program efficiency can be improved by substituting an assembly language routine that does integer multiply to a double register. JTRY(j,k,m)=int((dble(k)*dble(j))/dble(m)) if (isign.eq.0) then Initialize enough digits of the upper and lower bounds. jdif=nrad-1 do 11 j=NWK,1,-1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
decrease minint if you want to live closer to the edge. A final safety feature in arcmak is its refusal to believe zero values in the table nfreq; a 0 is treated as if it were a 1. If this were not done, the occurrence in a message of a single character whose nfreq entry is zero would result in scrambling the entire rest of the message. If you want to live dangerously, with a very slightly more efficient coding, you can delete the max( ,1) operation.
20.5 Arithmetic Coding
2 3
iupb(j)=nrad-1 ilob(j)=0 nc=j if(jdif.gt.minint)return Initialization complete. jdif=(jdif+1)*nrad-1 enddo 11 pause ’NWK too small in arcode’ else if (isign.gt.0) then If encoding, check for valid input character. if(ich.gt.nch.or.ich.lt.0)pause ’bad ich in arcode’ else If decoding, locate the character ich by bisection. ja=ichar(code(lcd))-ilob(nc) do 12 j=nc+1,NWK ja=ja*nrad+(ichar(code(j+lcd-nc))-ilob(j)) enddo 12 ich=0 ihi=nch+1 if(ihi-ich.gt.1) then m=(ich+ihi)/2 if (ja.ge.JTRY(jdif,ncumfq(m+1),ncum)) then ich=m else ihi=m endif goto 1 endif if(ich.eq.nch)return Detected end of message. endif Following code is common for encoding and decoding. Convert character ich to a new subrange [ilob,iupb). jh=JTRY(jdif,ncumfq(ich+2),ncum) jl=JTRY(jdif,ncumfq(ich+1),ncum) jdif=jh-jl call arcsum(ilob,iupb,jh,NWK,nrad,nc) call arcsum(ilob,ilob,jl,NWK,nrad,nc) How many leading digits to output do 13 j=nc,NWK (if encoding) or skip over? if(ich.ne.nch.and.iupb(j).ne.ilob(j))goto 2 if(lcd.gt.lcode)pause ’lcode too small in arcode’ if(isign.gt.0) code(lcd)=char(ilob(j)) lcd=lcd+1 enddo 13 return Ran out of message. Did someone forget to encode nc=j a terminating ncd? j=0 How many digits to shift? if (jdif.lt.minint) then j=j+1 jdif=jdif*nrad goto 3 endif if (nc-j.lt.1) pause ’NWK too small in arcode’ if(j.ne.0)then Shift them. do 14 k=nc,NWK iupb(k-j)=iupb(k) ilob(k-j)=ilob(k) enddo 14 endif nc=nc-j do 15 k=NWK-j+1,NWK iupb(k)=0 ilob(k)=0 enddo 15 endif return Normal return. END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
1
905
906
Chapter 20.
Less-Numerical Algorithms
If radix-changing, rather than compression, is your primary aim (for example to convert an arbitrary file into printable characters) then you are of course free to set all the components of nfreq equal, say, to 1. CITED REFERENCES AND FURTHER READING: Bell, T.C., Cleary, J.G., and Witten, I.H. 1990, Text Compression (Englewood Cliffs, NJ: PrenticeHall). Nelson, M. 1991, The Data Compression Book (Redwood City, CA: M&T Books). Witten, I.H., Neal, R.M., and Cleary, J.G. 1987, Communications of the ACM, vol. 30, pp. 520– 540. [1]
20.6 Arithmetic at Arbitrary Precision Let’s compute the number π to a couple of thousand decimal places. In doing so, we’ll learn some things about multiple precision arithmetic on computers and meet quite an unusual application of the fast Fourier transform (FFT). We’ll also develop a set of routines that you can use for other calculations at any desired level of arithmetic precision. To start with, we need an analytic algorithm for π. Useful algorithms are quadratically convergent, i.e., they double the number of significant digits at each iteration. Quadratically convergent algorithms for π are based on the AGM (arithmetic geometric mean) method, which also finds application to the calculation of elliptic integrals (cf. §6.11) and in advanced implementations of the ADI method for elliptic partial differential equations (§19.5). Borwein and Borwein [1] treat this subject, which is beyond our scope here. One of their algorithms for π starts with the initializations √ X0 = 2 √ π0 = 2 + 2 (20.6.1) √ 4 Y0 = 2
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
SUBROUTINE arcsum(iin,iout,ja,nwk,nrad,nc) INTEGER ja,nc,nrad,nwk,iin(*),iout(*) Used by arcode. Add the integer ja to the radix nrad multiple-precision integer iin(nc..nwk). Return the result in iout(nc..nwk). INTEGER j,jtmp,karry karry=0 do 11 j=nwk,nc+1,-1 jtmp=ja ja=ja/nrad iout(j)=iin(j)+(jtmp-ja*nrad)+karry if (iout(j).ge.nrad) then iout(j)=iout(j)-nrad karry=1 else karry=0 endif enddo 11 iout(nc)=iin(nc)+ja+karry return END
20.6 Arithmetic at Arbitrary Precision
(20.6.2)
The value π emerges as the limit π∞ . Now, to the question of how to do arithmetic to arbitrary precision: In a high-level language like FORTRAN, a natural choice is to work in radix (base) 256, so that character arrays can be directly interpreted as strings of digits. At the very end of our calculation, we will want to convert our answer to radix 10, but that is essentially a frill for the benefit of human ears, accustomed to the familiar chant, “three point one four one five nine. . . .” For any less frivolous calculation, we would likely never leave base 256 (or the thence trivially reachable hexadecimal, octal, or binary bases). We will adopt the convention of storing digit strings in the “human” ordering, that is, with the first stored digit in an array being most significant, the last stored digit being least significant. The opposite convention would, of course, also be possible. “Carries,” where we need to partition a number larger than 255 into a low-order byte and a high-order carry, present a minor programming annoyance, solved, in the routines below, by the use of FORTRAN’s EQUIVALENCE facility, and some initial testing of the order in which bytes are stored in a FORTRAN integer. It is easy at this point, following Knuth [2], to write a routine for the “fast” arithmetic operations: short addition (adding a single byte to a string), addition, subtraction, short multiplication (multiplying a string by a single byte), short division, ones-complement negation; and a couple of utility operations, copying and left-shifting strings. SUBROUTINE mpops(w,u,v) CHARACTER*1 w(*),u(*),v(*) Multiple precision arithmetic operations done on character strings, interpreted as radix 256 numbers. This routine collects the simpler operations. INTEGER i,ireg,j,n,ir,is,iv,ii1,ii2 CHARACTER*1 creg(4) SAVE ii1,ii2 EQUIVALENCE (ireg,creg) It is assumed that with the above equivalence, creg(ii1) addresses the low-order byte of ireg, and creg(ii2) addresses the next higher order byte. The values ii1 and ii2 are set by an initial call to mpinit. ENTRY mpinit ireg=256*ichar(’2’)+ichar(’1’) do 11 j=1,4 Figure out the byte ordering. if (creg(j).eq.’1’) ii1=j if (creg(j).eq.’2’) ii2=j enddo 11 return ENTRY mpadd(w,u,v,n) Adds the unsigned radix 256 integers u(1:n) and v(1:n) yielding the unsigned integer w(1:n+1). ireg=0 do 12 j=n,1,-1
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
and then, for i = 0, 1, . . . , repeats the iteration 1 p 1 Xi + √ Xi+1 = 2 Xi Xi+1 + 1 πi+1 = πi Yi + 1 p Yi Xi+1 + p 1 Xi+1 Yi+1 = Yi + 1
907
908
Chapter 20.
Less-Numerical Algorithms
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
ireg=ichar(u(j))+ichar(v(j))+ichar(creg(ii2)) w(j+1)=creg(ii1) enddo 12 w(1)=creg(ii2) return ENTRY mpsub(is,w,u,v,n) Subtracts the unsigned radix 256 integer v(1:n) from u(1:n) yielding the unsigned integer w(1:n). If the result is negative (wraps around), is is returned as −1; otherwise it is returned as 0. ireg=256 do 13 j=n,1,-1 ireg=255+ichar(u(j))-ichar(v(j))+ichar(creg(ii2)) w(j)=creg(ii1) enddo 13 is=ichar(creg(ii2))-1 return ENTRY mpsad(w,u,n,iv) Short addition: the integer iv (in the range 0 ≤ iv ≤ 255) is added to the unsigned radix 256 integer u(1:n), yielding w(1:n+1). ireg=256*iv do 14 j=n,1,-1 ireg=ichar(u(j))+ichar(creg(ii2)) w(j+1)=creg(ii1) enddo 14 w(1)=creg(ii2) return ENTRY mpsmu(w,u,n,iv) Short multiplication: the unsigned radix 256 integer u(1:n) is multiplied by the integer iv (in the range 0 ≤ iv ≤ 255), yielding w(1:n+1). ireg=0 do 15 j=n,1,-1 ireg=ichar(u(j))*iv+ichar(creg(ii2)) w(j+1)=creg(ii1) enddo 15 w(1)=creg(ii2) return ENTRY mpsdv(w,u,n,iv,ir) Short division: the unsigned radix 256 integer u(1:n) is divided by the integer iv (in the range 0 ≤ iv ≤ 255), yielding a quotient w(1:n) and a remainder ir (with 0 ≤ ir ≤ 255). ir=0 do 16 j=1,n i=256*ir+ichar(u(j)) w(j)=char(i/iv) ir=mod(i,iv) enddo 16 return ENTRY mpneg(u,n) Ones-complement negate the unsigned radix 256 integer u(1:n). ireg=256 do 17 j=n,1,-1 ireg=255-ichar(u(j))+ichar(creg(ii2)) u(j)=creg(ii1) enddo 17 return ENTRY mpmov(u,v,n) Move v(1:n) onto u(1:n). do 18 j=1,n u(j)=v(j) enddo 18 return ENTRY mplsh(u,n) Left shift u(2..n+1) onto u(1:n). do 19 j=1,n u(j)=u(j+1)
20.6 Arithmetic at Arbitrary Precision
909
enddo 19 return END
456 × 789 4104 3648 3192 359784
4 × 7 36 32 40 28 35 42 28 67 118 3 5 9 7
5 6 8 9 45 54 48 93 54 8 4
The tableau on the left shows the conventional method of multiplication, in which three separate short multiplications of the full multiplicand (by 9, 8, and 7) are added to obtain the final result. The tableau on the right shows a different method (sometimes taught for mental arithmetic), where the single-digit cross products are all computed (e.g. 8 × 6 = 48), then added in columns to obtain an incompletely carried result (here, the list 28, 67, 118, 93, 54). The final step is a single pass from right to left, recording the single least-significant digit and carrying the higher digit or digits into the total to the left (e.g. 93 + 5 = 98, record the 8, carry 9). You can see immediately that the column sums in the right-hand method are components of the convolution of the digit strings, for example 118 = 4 × 9 + 5 × 8 + 6 × 7. In §13.1 we learned how to compute the convolution of two vectors by the fast Fourier transform (FFT): Each vector is FFT’d, the two complex transforms are multiplied, and the result is inverse-FFT’d. Since the transforms are done with floating arithmetic, we need sufficient precision so that the exact integer value of each component of the result is discernible in the presence of roundoff error. We should therefore allow a (conservative) few times log2 (log2 N ) bits for roundoff in the FFT. A number of length N bytes in radix 256 can generate convolution components as large as the order of (256)2 N , thus requiring 16 + log2 N bits of precision for exact storage. If it is the number of bits in the floating mantissa (cf. §20.1), we obtain the condition 16 + log2 N + few × log2 log2 N < it
(20.6.3)
We see that single precision, say with it = 24, is inadequate for any interesting value of N , while double precision, say with it = 53, allows N to be greater than 106 , corresponding to some millions of decimal digits. The following routine
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Full multiplication of two digit strings, if done by the traditional hand method, is not a fast operation: In multiplying two strings of length N , the multiplicand would be short-multiplied in turn by each byte of the multiplier, requiring O(N 2 ) operations in all. We will see, however, that all the arithmetic operations on numbers of length N can in fact be done in O(N × log N × log log N ) operations. The trick is to recognize that multiplication is essentially a convolution (§13.1) of the digits of the multiplicand and multiplier, followed by some kind of carry operation. Consider, for example, two ways of writing the calculation 456 × 789:
910
Chapter 20.
Less-Numerical Algorithms
therefore presumes double precision versions of realft (§12.3) and four1 (§12.2), here called drealft and dfour1. (These routines are included on the Numerical Recipes diskettes.)
1
With multiplication thus a “fast” operation, division is best performed by multiplying the dividend by the reciprocal of the divisor. The reciprocal of a value
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
C
SUBROUTINE mpmul(w,u,v,n,m) INTEGER m,n,NMAX CHARACTER*1 w(n+m),u(n),v(m) DOUBLE PRECISION RX PARAMETER (NMAX=8192,RX=256.D0) USES drealft DOUBLE PRECISION version of realft. Uses Fast Fourier Transform to multiply the unsigned radix 256 integers u(1:n) and v(1:m), yielding a product w(1:n+m). INTEGER j,mn,nn DOUBLE PRECISION cy,t,a(NMAX),b(NMAX) mn=max(m,n) nn=1 Find the smallest useable power of two for the transform. if(nn.lt.mn) then nn=nn+nn goto 1 endif nn=nn+nn if(nn.gt.NMAX)pause ’NMAX too small in fftmul’ do 11 j=1,n Move U to a double precision floating array. a(j)=ichar(u(j)) enddo 11 do 12 j=n+1,nn a(j)=0.D0 enddo 12 do 13 j=1,m Move V to a double precision floating array. b(j)=ichar(v(j)) enddo 13 do 14 j=m+1,nn b(j)=0.D0 enddo 14 Perform the convolution: First, the two Fourier transforms. call drealft(a,nn,1) call drealft(b,nn,1) b(1)=b(1)*a(1) Then multiply the complex results (real and imaginary parts). b(2)=b(2)*a(2) do 15 j=3,nn,2 t=b(j) b(j)=t*a(j)-b(j+1)*a(j+1) b(j+1)=t*a(j+1)+b(j+1)*a(j) enddo 15 call drealft(b,nn,-1) Then do the inverse Fourier transform. cy=0. Make a final pass to do all the carries. do 16 j=nn,1,-1 t=b(j)/(nn/2)+cy+0.5D0 The 0.5 allows for roundoff error. b(j)=mod(t,RX) cy=int(t/RX) enddo 16 if (cy.ge.RX) pause ’cannot happen in fftmul’ w(1)=char(int(cy)) Copy answer to output. do 17 j=2,n+m w(j)=char(int(b(j-1))) enddo 17 return END
20.6 Arithmetic at Arbitrary Precision
911
V is calculated by iteration of Newton’s rule, Ui+1 = Ui (2 − V Ui )
(20.6.4)
C
1
SUBROUTINE mpinv(u,v,n,m) INTEGER m,n,MF,NMAX CHARACTER*1 u(n),v(m) REAL BI PARAMETER (MF=4,BI=1./256.,NMAX=8192) Character string v(1:m) is interpreted as a radix 256 number with the radix point after (nonzero) v(1); u(1:n) is set to the most significant digits of its reciprocal, with the radix point after u(1). USES mpmov,mpmul,mpneg INTEGER i,j,mm REAL fu,fv CHARACTER*1 rr(2*NMAX+1),s(NMAX) if(max(n,m).gt.NMAX)pause ’NMAX too small in mpinv’ mm=min(MF,m) fv=ichar(v(mm)) Use ordinary floating arithmetic to get an initial apdo 11 j=mm-1,1,-1 proximation. fv=fv*BI+ichar(v(j)) enddo 11 fu=1./fv do 12 j=1,n i=int(fu) u(j)=char(i) fu=256.*(fu-i) enddo 12 continue Iterate Newton’s rule to convergence. call mpmul(rr,u,v,n,m) Construct 2 − U V in S. call mpmov(s,rr(2),n) call mpneg(s,n) s(1)=char(ichar(s(1))-254) Multiply SU into U . call mpmul(rr,s,u,n,n) call mpmov(u,rr(2),n) do 13 j=2,n-1 If fractional part of S is not zero, it has not converged if(ichar(s(j)).ne.0)goto 1 to 1. enddo 13 continue return END
Division now follows as a simple corollary, with only the necessity of calculating the reciprocal to sufficient accuracy to get an exact quotient and remainder.
C
SUBROUTINE mpdiv(q,r,u,v,n,m) INTEGER m,n,NMAX,MACC CHARACTER*1 q(n-m+1),r(m),u(n),v(m) PARAMETER (NMAX=8192,MACC=6) Divides unsigned radix 256 integers u(1:n) by v(1:m) (with m ≤ n required), yielding a quotient q(1:n-m+1) and a remainder r(1:m). USES mpinv,mpmov,mpmul,mpsad,mpsub INTEGER is CHARACTER*1 rr(2*NMAX),s(2*NMAX) if(n+MACC.gt.NMAX)pause ’NMAX too small in mpdiv’
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
which results in the quadratic convergence of U∞ to 1/V , as you can easily prove. (Many supercomputers and RISC machines actually use this iteration to perform divisions.) We can now see where the operations count N log N log log N , mentioned above, originates: N log N is in the Fourier transform, with the iteration to converge Newton’s rule giving an additional factor of log log N .
912
Chapter 20.
Set S = 1/V . Set Q = SU . Multiply and subtract to get the remainder. in mpdiv’
Square roots are calculated by a Newton’s rule much like division. If 1 Ui (3 − V Ui2 ) (20.6.5) 2 √ √ converges quadratically to 1/ V . A final multiplication by V gives V . Ui+1 =
then U∞
C
1
2
SUBROUTINE mpsqrt(w,u,v,n,m) INTEGER m,n,NMAX,MF CHARACTER*1 w(*),u(*),v(*) REAL BI PARAMETER (NMAX=2048,MF=3,BI=1./256.) USES mplsh,mpmov,mpmul,mpneg,mpsdv Character string v(1:m) is interpreted as a radix 256 number with the radix point after v(1); w(1:n) is set to its square root (radix point after w(1)), and u(1:n) is set to the reciprocal thereof (radix point before u(1)). w and u need not be distinct, in which case they are set to the square root. INTEGER i,ir,j,mm REAL fu,fv CHARACTER*1 r(NMAX),s(NMAX) if(2*n+1.gt.NMAX)pause ’NMAX too small in mpsqrt’ mm=min(m,MF) fv=ichar(v(mm)) Use ordinary floating arithmetic to get an initial approxdo 11 j=mm-1,1,-1 imation. fv=BI*fv+ichar(v(j)) enddo 11 fu=1./sqrt(fv) do 12 j=1,n i=int(fu) u(j)=char(i) fu=256.*(fu-i) enddo 12 continue Iterate Newton’s rule to convergence. call mpmul(r,u,u,n,n) Construct S = (3 − V U 2 )/2. call mplsh(r,n) call mpmul(s,r,v,n,m) call mplsh(s,n) call mpneg(s,n) s(1)=char(ichar(s(1))-253) call mpsdv(s,s,n,2,ir) do 13 j=2,n-1 If fractional part of S is not zero, it has not converged if(ichar(s(j)).ne.0)goto 2 to 1. enddo 13 call mpmul(r,u,v,n,m) Get square root from reciprocal and return. call mpmov(w,r(2),n) return continue call mpmul(r,s,u,n,n) Replace U by SU . call mpmov(u,r(2),n) goto 1 END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
call mpinv(s,v,n+MACC,m) call mpmul(rr,s,u,n+MACC,n) call mpsad(s,rr,n+n+MACC/2,1) call mpmov(q,s(3),n-m+1) call mpmul(rr,q,v,n-m+1,m) call mpsub(is,rr(2),u,rr(2),n) if (is.ne.0) pause ’MACC too small call mpmov(r,rr(n-m+2),m) return END
Less-Numerical Algorithms
20.6 Arithmetic at Arbitrary Precision
913
C
SUBROUTINE mp2dfr(a,s,n,m) INTEGER m,n,IAZ CHARACTER*1 a(*),s(*) PARAMETER (IAZ=48) USES mplsh,mpsmu Converts a radix 256 fraction a(1:n) (radix point before a(1)) to a decimal fraction represented as an ascii string s(1:m), where m is a returned value. The input array a(1:n) is destroyed. NOTE: For simplicity, this routine implements a slow (∝ N 2) algorithm. Fast (∝ N ln N ), more complicated, radix conversion algorithms do exist. INTEGER j m=2.408*n do 11 j=1,m call mpsmu(a,a,n,10) s(j)=char(ichar(a(1))+IAZ) call mplsh(a,n) enddo 11 return END
Finally, then, we arrive at a routine implementing equations (20.6.1) and (20.6.2):
C
*
1
SUBROUTINE mppi(n) INTEGER n,IAOFF,NMAX PARAMETER (IAOFF=48,NMAX=8192) USES mpinit,mp2dfr,mpadd,mpinv,mplsh,mpmov,mpmul,mpsdv,mpsqrt Demonstrate multiple precision routines by calculating and printing the first n bytes of π. INTEGER ir,j,m CHARACTER*1 x(NMAX),y(NMAX),sx(NMAX),sxi(NMAX),t(NMAX),s(3*NMAX), pi(NMAX) call mpinit t(1)=char(2) Set T = 2. do 11 j=2,n t(j)=char(0) enddo 11 √ call mpsqrt(x,x,t,n,n) Set X0 = 2.√ call mpadd(pi,t,x,n) Set π0 = 2 + 2. call mplsh(pi,n) call mpsqrt(sx,sxi,x,n,n) Set Y0 = 21/4 . call mpmov(y,sx,n) continue 1/2 −1/2 call mpadd(x,sx,sxi,n) Set Xi+1 = (Xi + Xi )/2. call mpsdv(x,x(2),n,2,ir) 1/2 −1/2 call mpsqrt(sx,sxi,x,n,n) Form the temporary T = Yi Xi+1 + Xi+1 . call mpmul(t,y,sx,n,n) call mpadd(t(2),t(2),sxi,n)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
We already mentioned that radix conversion to decimal is a merely cosmetic operation that should normally be omitted. The simplest way to convert a fraction to decimal is to multiply it repeatedly by 10, picking off (and subtracting) the resulting integer part. This, has an operations count of O(N 2 ), however, since each liberated decimal digit takes an O(N ) operation. It is possible to do the radix conversion as a fast operation by a “divide and conquer” strategy, in which the fraction is (fast) multiplied by a large power of 10, enough to move about half the desired digits to the left of the radix point. The integer and fractional pieces are now processed independently, each further subdivided. If our goal were a few billion digits of π, instead of a few thousand, we would need to implement this scheme. For present purposes, the following lazy routine is adequate:
914
Chapter 20.
Less-Numerical Algorithms
Figure 20.6.1. The first 2398 decimal digits of π, computed by the routines in this section.
2
x(1)=char(ichar(x(1))+1) Increment Xi+1 and Yi by 1. y(1)=char(ichar(y(1))+1) call mpinv(s,y,n,n) Set Yi+1 = T /(Yi + 1). call mpmul(y,t(3),s,n,n) call mplsh(y,n) call mpmul(t,x,s,n,n) Form temporary T = (Xi+1 + 1)/(Yi + 1). continue If T = 1 then we have converged. m=mod(255+ichar(t(2)),256) do 12 j=3,n if(ichar(t(j)).ne.m)goto 2 enddo 12 if (abs(ichar(t(n+1))-m).gt.1)goto 2 write (*,*) ’pi=’ s(1)=char(ichar(pi(1))+IAOFF) s(2)=’.’ call mp2dfr(pi(2),s(3),n-1,m) Convert to decimal for printing. NOTE: The conversion routine, for this demonstration only, is a slow (∝ N 2) algorithm. Fast (∝ N ln N ), more complicated, radix conversion algorithms do exist. write (*,’(1x,64a1)’) (s(j),j=1,m+1) return continue call mpmul(s,pi,t(2),n,n) Set πi+1 = T πi . call mpmov(pi,s(2),n) goto 1 END
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
3.1415926535897932384626433832795028841971693993751058209749445923078164062 862089986280348253421170679821480865132823066470938446095505822317253594081 284811174502841027019385211055596446229489549303819644288109756659334461284 756482337867831652712019091456485669234603486104543266482133936072602491412 737245870066063155881748815209209628292540917153643678925903600113305305488 204665213841469519415116094330572703657595919530921861173819326117931051185 480744623799627495673518857527248912279381830119491298336733624406566430860 213949463952247371907021798609437027705392171762931767523846748184676694051 320005681271452635608277857713427577896091736371787214684409012249534301465 495853710507922796892589235420199561121290219608640344181598136297747713099 605187072113499999983729780499510597317328160963185950244594553469083026425 223082533446850352619311881710100031378387528865875332083814206171776691473 035982534904287554687311595628638823537875937519577818577805321712268066130 019278766111959092164201989380952572010654858632788659361533818279682303019 520353018529689957736225994138912497217752834791315155748572424541506959508 295331168617278558890750983817546374649393192550604009277016711390098488240 128583616035637076601047101819429555961989467678374494482553797747268471040 475346462080466842590694912933136770289891521047521620569660240580381501935 112533824300355876402474964732639141992726042699227967823547816360093417216 412199245863150302861829745557067498385054945885869269956909272107975093029 553211653449872027559602364806654991198818347977535663698074265425278625518 184175746728909777727938000816470600161452491921732172147723501414419735685 481613611573525521334757418494684385233239073941433345477624168625189835694 855620992192221842725502542568876717904946016534668049886272327917860857843 838279679766814541009538837863609506800642251252051173929848960841284886269 456042419652850222106611863067442786220391949450471237137869609563643719172 874677646575739624138908658326459958133904780275900994657640789512694683983 525957098258226205224894077267194782684826014769909026401363944374553050682 034962524517493996514314298091906592509372216964615157098583874105978859597 729754989301617539284681382686838689427741559918559252459539594310499725246 808459872736446958486538367362226260991246080512438843904512441365497627807 977156914359977001296160894416948685558484063534220722258284886481584560285
915
20.6 Arithmetic at Arbitrary Precision
Figure 20.6.1 gives the result, computed with n = 1000. As an exercise, you might enjoy checking the first hundred digits of the figure against the first 12 terms of Ramanujan’s celebrated identity [3] (20.6.6)
using the above routines. You might also use the routines to verify that the number 2512 + 1 is not a prime, but has factors 2,424,833 and 7,455,602,825,647,884,208,337,395,736,200,454,918,783,366,342,657 (which are in fact prime; the remaining prime factor being about 7.416 × 1098 ) [4]. CITED REFERENCES AND FURTHER READING: Borwein, J.M., and Borwein, P.B. 1987, Pi and the AGM: A Study in Analytic Number Theory and Computational Complexity (New York: Wiley). [1] Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §4.3. [2] Ramanujan, S. 1927, Collected Papers of Srinivasa Ramanujan, G.H. Hardy, P.V. Seshu Aiyar, and B.M. Wilson, eds. (Cambridge, U.K.: Cambridge University Press), pp. 23–39. [3] Kolata, G. 1990, June 20, The New York Times. [4] Kronsjo, ¨ L. 1987, Algorithms: Their Complexity and Efficiency, 2nd ed. (New York: Wiley).
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
√ ∞ 1 8 X (4n)! (1103 + 26390n) = π 9801 n=0 (n! 396n)4
The references collected here are those of general usefulness, usually cited in more than one section of this book. More specialized sources, usually cited in a single section, are not repeated here. We first list a small number of books that form the nucleus of a recommended personal reference collection on numerical methods, numerical analysis, and closely related subjects. These are the books that we like to have within easy reach. Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York) Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathematical Association of America) Ames, W.F. 1977, Numerical Methods for Partial Differential Equations, 2nd ed. (New York: Academic Press) Bratley, P., Fox, B.L., and Schrage, E.L. 1983, A Guide to Simulation (New York: Springer-Verlag) Dahlquist, G., and Bjorck, A. 1974, Numerical Methods (Englewood Cliffs, NJ: Prentice-Hall) Delves, L.M., and Mohamed, J.L. 1985, Computational Methods for Integral Equations (Cambridge, U.K.: Cambridge University Press) Dennis, J.E., and Schnabel, R.B. 1983, Numerical Methods for Unconstrained Optimization and Nonlinear Equations (Englewood Cliffs, NJ: Prentice-Hall) Gill, P.E., Murray, W., and Wright, M.H. 1991, Numerical Linear Algebra and Optimization, vol. 1 (Redwood City, CA: Addison-Wesley) Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press) Oppenheim, A.V., and Schafer, R.W. 1989, Discrete-Time Signal Processing (Englewood Cliffs, NJ: Prentice-Hall) Ralston, A., and Rabinowitz, P. 1978, A First Course in Numerical Analysis, 2nd ed. (New York: McGraw-Hill) Sedgewick, R. 1988, Algorithms, 2nd ed. (Reading, MA: Addison-Wesley) Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag) Wilkinson, J.H., and Reinsch, C. 1971, Linear Algebra, vol. II of Handbook for Automatic Computation (New York: Springer-Verlag) 916
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
References
References
917
We next list the larger collection of books, which, in our view, should be included in any serious research library on computing, numerical methods, or analysis.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Bevington, P.R. 1969, Data Reduction and Error Analysis for the Physical Sciences (New York: McGraw-Hill) Bloomfield, P. 1976, Fourier Analysis of Time Series – An Introduction (New York: Wiley) Bowers, R.L., and Wilson, J.R. 1991, Numerical Modeling in Applied Physics and Astrophysics (Boston: Jones & Bartlett) Brent, R.P. 1973, Algorithms for Minimization without Derivatives (Englewood Cliffs, NJ: Prentice-Hall) Brigham, E.O. 1974, The Fast Fourier Transform (Englewood Cliffs, NJ: PrenticeHall) Brownlee, K.A. 1965, Statistical Theory and Methodology, 2nd ed. (New York: Wiley) Bunch, J.R., and Rose, D.J. (eds.) 1976, Sparse Matrix Computations (New York: Academic Press) Canuto, C., Hussaini, M.Y., Quarteroni, A., and Zang, T.A. 1988, Spectral Methods in Fluid Dynamics (New York: Springer-Verlag) Carnahan, B., Luther, H.A., and Wilkes, J.O. 1969, Applied Numerical Methods (New York: Wiley) Champeney, D.C. 1973, Fourier Transforms and Their Physical Applications (New York: Academic Press) Childers, D.G. (ed.) 1978, Modern Spectrum Analysis (New York: IEEE Press) Cooper, L., and Steinberg, D. 1970, Introduction to Methods of Optimization (Philadelphia: Saunders) Dantzig, G.B. 1963, Linear Programming and Extensions (Princeton, NJ: Princeton University Press) Devroye, L. 1986, Non-Uniform Random Variate Generation (New York: SpringerVerlag) Dongarra, J.J., et al. 1979, LINPACK User’s Guide (Philadelphia: S.I.A.M.) Downie, N.M., and Heath, R.W. 1965, Basic Statistical Methods, 2nd ed. (New York: Harper & Row) Duff, I.S., and Stewart, G.W. (eds.) 1979, Sparse Matrix Proceedings 1978 (Philadelphia: S.I.A.M.) Elliott, D.F., and Rao, K.R. 1982, Fast Transforms: Algorithms, Analyses, Applications (New York: Academic Press) Fike, C.T. 1968, Computer Evaluation of Mathematical Functions (Englewood Cliffs, NJ: Prentice-Hall) Forsythe, G.E., Malcolm, M.A., and Moler, C.B. 1977, Computer Methods for Mathematical Computations (Englewood Cliffs, NJ: Prentice-Hall) Forsythe, G.E., and Moler, C.B. 1967, Computer Solution of Linear Algebraic Systems (Englewood Cliffs, NJ: Prentice-Hall) Gass, S.T. 1969, Linear Programming, 3rd ed. (New York: McGraw-Hill) Gear, C.W. 1971, Numerical Initial Value Problems in Ordinary Differential Equations (Englewood Cliffs, NJ: Prentice-Hall) Goodwin, E.T. (ed.) 1961, Modern Computing Methods, 2nd ed. (New York: Philosophical Library) Gottlieb, D. and Orszag, S.A. 1977, Numerical Analysis of Spectral Methods: Theory and Applications (Philadelphia: S.I.A.M.) Hackbusch, W. 1985, Multi-Grid Methods and Applications (New York: SpringerVerlag)
918
References
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Hamming, R.W. 1962, Numerical Methods for Engineers and Scientists; reprinted 1986 (New York: Dover) Hart, J.F., et al. 1968, Computer Approximations (New York: Wiley) Hastings, C. 1955, Approximations for Digital Computers (Princeton: Princeton University Press) Hildebrand, F.B. 1974, Introduction to Numerical Analysis, 2nd ed.; reprinted 1987 (New York: Dover) Hoel, P.G. 1971, Introduction to Mathematical Statistics, 4th ed. (New York: Wiley) Horn, R.A., and Johnson, C.R. 1985, Matrix Analysis (Cambridge: Cambridge University Press) Householder, A.S. 1970, The Numerical Treatment of a Single Nonlinear Equation (New York: McGraw-Hill) Huber, P.J. 1981, Robust Statistics (New York: Wiley) Isaacson, E., and Keller, H.B. 1966, Analysis of Numerical Methods (New York: Wiley) Jacobs, D.A.H. (ed.) 1977, The State of the Art in Numerical Analysis (London: Academic Press) Johnson, L.W., and Riess, R.D. 1982, Numerical Analysis, 2nd ed. (Reading, MA: Addison-Wesley) Kahaner, D., Moler, C., and Nash, S. 1989, Numerical Methods and Software (Englewood Cliffs, NJ: Prentice Hall) Keller, H.B. 1968, Numerical Methods for Two-Point Boundary-Value Problems (Waltham, MA: Blaisdell) Knuth, D.E. 1968, Fundamental Algorithms, vol. 1 of The Art of Computer Programming (Reading, MA: Addison-Wesley) Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley) Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading, MA: Addison-Wesley) Koonin, S.E., and Meredith, D.C. 1990, Computational Physics, Fortran Version (Redwood City, CA: Addison-Wesley) Kuenzi, H.P., Tzschach, H.G., and Zehnder, C.A. 1971, Numerical Methods of Mathematical Optimization (New York: Academic Press) Lanczos, C. 1956, Applied Analysis; reprinted 1988 (New York: Dover) Land, A.H., and Powell, S. 1973, Fortran Codes for Mathematical Programming (London: Wiley-Interscience) Lawson, C.L., and Hanson, R. 1974, Solving Least Squares Problems (Englewood Cliffs, NJ: Prentice-Hall) Lehmann, E.L. 1975, Nonparametrics: Statistical Methods Based on Ranks (San Francisco: Holden-Day) Luke, Y.L. 1975, Mathematical Functions and Their Approximations (New York: Academic Press) Magnus, W., and Oberhettinger, F. 1949, Formulas and Theorems for the Functions of Mathematical Physics (New York: Chelsea) Martin, B.R. 1971, Statistics for Physicists (New York: Academic Press) Mathews, J., and Walker, R.L. 1970, Mathematical Methods of Physics, 2nd ed. (Reading, MA: W.A. Benjamin/Addison-Wesley) von Mises, R. 1964, Mathematical Theory of Probability and Statistics (New York: Academic Press) Murty, K.G. 1976, Linear and Combinatorial Programming (New York: Wiley) Norusis, M.J. 1982, SPSS Introductory Guide: Basic Statistics and Operations; and 1985, SPSS-X Advanced Statistics Guide (New York: McGraw-Hill)
References
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
Nussbaumer, H.J. 1982, Fast Fourier Transform and Convolution Algorithms (New York: Springer-Verlag) Ortega, J., and Rheinboldt, W. 1970, Iterative Solution of Nonlinear Equations in Several Variables (New York: Academic Press) Ostrowski, A.M. 1966, Solutions of Equations and Systems of Equations, 2nd ed. (New York: Academic Press) Polak, E. 1971, Computational Methods in Optimization (New York: Academic Press) Rice, J.R. 1983, Numerical Methods, Software, and Analysis (New York: McGrawHill) Richtmyer, R.D., and Morton, K.W. 1967, Difference Methods for Initial Value Problems, 2nd ed. (New York: Wiley-Interscience) Roache, P.J. 1976, Computational Fluid Dynamics (Albuquerque: Hermosa) Robinson, E.A., and Treitel, S. 1980, Geophysical Signal Analysis (Englewood Cliffs, NJ: Prentice-Hall) Smith, B.T., et al. 1976, Matrix Eigensystem Routines — EISPACK Guide, 2nd ed., vol. 6 of Lecture Notes in Computer Science (New York: Springer-Verlag) Stuart, A., and Ord, J.K. 1987, Kendall’s Advanced Theory of Statistics, 5th ed. (London: Griffin and Co.) [previous eds. published as Kendall, M., and Stuart, A., The Advanced Theory of Statistics] Tewarson, R.P. 1973, Sparse Matrices (New York: Academic Press) Westlake, J.R. 1968, A Handbook of Numerical Matrix Inversion and Solution of Linear Equations (New York: Wiley) Wilkinson, J.H. 1965, The Algebraic Eigenvalue Problem (New York: Oxford University Press) Young, D.M., and Gregory, R.T. 1973, A Survey of Numerical Mathematics, 2 vols.; reprinted 1988 (New York: Dover)
919
Index of Programs and Dependencies
addint airy amebsa
interp bessik bessjy ran1 . amotsa
.
. . . . beschb . . . [funk]
. . . . . . chebev . . . .
. .
. .
. .
. .
. .
§19.6 (p. 871) §6.7 (p. 244)
.
.
.
.
.
§10.9 (p. 445)
.
.
.
.
.
.
.
.
.
§10.4 (p. 404)
. .
ran1
amoeba amotry amotsa anneal
anorm2 arcmak arcode arcsum avevar badluk balanc
[funk] amotry [funk] [funk] [funk] ran1 ran3 . irbit1 trncst metrop trnspt revcst revers . . . . . . arcsum . . . . . . julday flmoon . . .
[funk] . .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§10.4 (p. 405) §10.9 (p. 446)
.
.
.
.
.
.
.
.
.
.
.
.
§10.9 (p. 439)
ran3
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
§19.6 §20.5 §20.5 §20.5 §14.2 §1.1
.
.
.
.
.
.
.
.
.
.
.
.
§11.5 (p. 477)
920
(p. 879) (p. 904) (p. 904) (p. 906) (p. 611) (p. 14)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
The following table lists, in alphabetical order, all the routines in Numerical Recipes. When a routine requires subsidiary routines, either from this book or else user-supplied, the full dependency tree is shown: A routine calls directly all routines to which it is connected by a solid line in the column immediately to its right; it calls indirectly the connected routines in all columns to its right. Typographical conventions: Routines from this book are in typewriter font (e.g., eulsum, gammln). The smaller, slanted font is used for the second and subsequent occurences of a routine in a single dependency tree. (When you are getting routines from the Numerical Recipes diskettes, or their archive files, you need only specify names in the larger, upright font.) User-supplied routines are indicated by the use of text font and square brackets, e.g., [funcv]. Consult the text for individual specifications of these routines. The right-hand side of the table lists section and page numbers for each program.
921
Index of Programs and Dependencies
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§2.4 (p. 46) §2.4 (p. 45)
banmul bcucof bcuint
. . . . . . bcucof
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§2.4 (p. 44) §3.6 (p. 119) §3.6 (p. 120)
beschb bessi
chebev bessi0
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§6.7 (p. 239) §6.6 (p. 233)
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§6.6 (p. 230) §6.6 (p. 231)
chebev . . .
. .
. .
. .
. .
. .
. .
. .
. .
§6.7 (p. 241) §6.5 (p. 228)
.
.
.
.
.
.
.
.
.
§6.5 (p. 225)
. . . chebev
. .
. .
. .
. .
. .
. .
. .
. .
§6.5 (p. 226) §6.7 (p. 236)
bessi0 . bessi1
.
.
.
.
.
.
.
.
§6.6 (p. 232)
. .
. .
. .
. .
. .
. .
. .
. .
. .
§6.6 (p. 231) §6.6 (p. 232)
bessj1 . bessj0 . . . . . . . .
.
.
.
.
.
.
.
.
§6.5 (p. 227)
. .
. .
. .
. .
. .
. .
. .
. .
§6.5 (p. 226) §6.5 (p. 227)
bessi0 bessi1
. .
. .
. .
. .
bessj0
beschb bessj0 bessj1 . . .
bessj1 bessjy
. . . beschb
bessik bessj
bessk
. . .
bessk0 bessk1 . .
.
. .
.
. .
bessk0 bessk1
bessi0 bessi1
bessy bessy0 bessy1
bessy1 bessy0 bessj0 bessj1
beta betacf
gammln . . . .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§6.1 (p. 209) §6.4 (p. 221)
gammln betacf factln bico bksub . . . .
.
.
.
.
.
.
.
.
.
.
.
.
§6.4 (p. 220)
gammln . . .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§6.1 (p. 208) §17.3 (p. 761)
.
.
.
.
.
.
.
.
.
.
.
.
§7.3 (p. 285)
.
. . . . [funcv]
. .
. .
. .
. .
. .
. .
. .
. .
. .
§10.2 (p. 397) §9.7 (p. 383)
[derivs]
[funcv] . . .
.
.
.
.
.
.
§16.4 (p. 722)
.
.
.
.
.
.
.
.
§1.1 (p. 16)
betai
bnldev brent broydn
bsstep caldat
ran1 . gammln [func] . fmin fdjac qrdcmp qrupdt rsolv lnsrch mmid pzextr . . .
rotate fmin
.
.
.
.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
banbks bandec
922
Index of Programs and Dependencies
chder .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§5.9 (p. 189)
chebev
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§5.8 (p. 187)
[func]
.
.
.
.
.
.
.
.
.
.
.
.
§5.8 (p. 186)
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§5.10 (p. 191)
chint .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§5.9 (p. 189)
chixy .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§15.3 (p. 663)
choldc
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.9 (p. 90)
cholsl
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.9 (p. 90)
chsone
gammq
gser gcf
. . gammln
.
.
.
.
.
.
§14.3 (p. 615)
chstwo
gammq
gser gcf
. . gammln
.
.
.
.
.
.
§14.3 (p. 616)
cisi
.
.
.
.
cntab1
gammq
cntab2
.
convlv
twofft realft
copy
.
.
.
.
.
.
.
gser gcf .
.
.
.
.
.
.
.
.
.
.
§6.9 (p. 251)
. . gammln
.
.
.
.
.
.
§14.4 (p. 625)
.
.
.
.
.
.
.
.
.
.
.
§14.4 (p. 629)
. . four1
.
.
.
.
.
.
.
.
§13.1 (p. 536)
.
.
.
.
.
.
.
.
.
§19.6 (p. 873)
.
.
.
.
correl
twofft realft
. . four1
.
.
.
.
.
.
.
.
§13.2 (p. 539)
cosft1
realft
four1 .
.
.
.
.
.
.
.
.
§12.3 (p. 512)
cosft2
realft
four1 .
.
.
.
.
.
.
.
.
§12.3 (p. 514)
covsrt
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§15.4 (p. 669)
crank .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§14.6 (p. 636)
cyclic
tridag
.
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 68)
daub4 .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. §13.10 (p. 588)
dawson
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§6.10 (p. 253)
dbrent
[func] [dfunc]
.
.
.
.
.
.
.
.
.
.
.
.
§10.3 (p. 400)
ddpoly
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§5.3 (p. 168)
decchk
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§20.3 (p. 895)
df1dim
[dfunc]
.
.
.
.
.
.
.
.
.
.
.
.
§10.6 (p. 417)
dfour1
.
.
.
.
.
.
dfpmin
[func] [dfunc] lnsrch
.
.
.
.
.
.
.
.
.
.
.
.
§10.7 (p. 421)
dfridr
[func]
.
.
.
.
.
.
.
.
.
.
.
.
§5.7 (p. 182)
dftcor
.
.
.
.
.
.
.
.
.
.
.
.
.
§13.9 (p. 580)
.
.
.
DOUBLE PRECISION version of four1, q.v.
[func]
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
chebft chebpc
923
Index of Programs and Dependencies
dftint
ellf ellpi
.
. . . four1
.
.
.
.
.
.
.
.
§13.9 (p. 581)
. . . . . §17.4 (p. 769) PRECISION version of pythag, q.v. PRECISION version of realft, q.v. PRECISION version of sprsax, q.v. PRECISION version of sprstx, q.v. PRECISION version of svbksb, q.v. PRECISION version of svdcmp, q.v. . . . . . §8.6 (p. 338) . . . . . §8.6 (p. 339) . . . . . §6.3 (p. 218) . . . . . §11.1 (p. 462) . . . . . §6.11 (p. 261)
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . DOUBLE DOUBLE DOUBLE DOUBLE DOUBLE DOUBLE . . . . . . . . . . . . . . .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§6.11 (p. 260) §6.11 (p. 261)
. . . . . gser . . gcf gammln gser . . gcf gammln
. .
. .
. .
. .
. .
. .
. .
§11.5 (p. 479) §6.2 (p. 213)
.
.
.
.
.
.
.
§6.2 (p. 214)
rf
elmhes . . erf gammp erfc
.
gammp gammq
erfcc . . . . eulsum . . . evlmem . . . expdev ran1 . expint . . . f1dim [func] . factln gammln factrl gammln fasper avevar spread realft fdjac [funcv] fgauss . . . fill0 . . . .
gser gcf
gammln
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
§6.2 §5.1 §13.7 §7.2 §6.3 §10.5 §6.1 §6.1 §13.8
four1 . . . . . . . . . . . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§9.7 (p. 381) §15.5 (p. 683) §19.6 (p. 873)
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
(p. 214) (p. 161) (p. 567) (p. 278) (p. 217) (p. 413) (p. 208) (p. 207) (p. 575)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
difeq . dpythag drealft dsprsax dsprstx dsvbksb dsvdcmp eclass eclazz ei . . eigsrt elle
[func] realft polint dftcor . . . . . . . . . . . . . . . . . . . . . . . . [equiv] . . . . . . rf . . rd rf . . rf . . rj rc
924
fit
Index of Programs and Dependencies
. . gammln
.
.
.
.
.
.
.
§15.2 (p. 659)
avevar . . . . . fit gammq gser gcf chixy mnbrak brent
.
.
.
.
.
.
.
§15.3 (p. 662)
gammq
fitexy
gser gcf
zbrent fixrts fleg
gser gcf gammln chixy
zroots .
flmoon fmin
laguer
.
.
.
.
.
.
.
.
§13.6 (p. 562) §15.4 (p. 674)
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§1.0
[funcv] .
.
.
.
.
.
.
.
.
.
.
.
.
§9.7 (p. 381)
(p. 1)
four1 .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§12.2 (p. 501)
fourew
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§12.6 (p. 528)
fourfs
fourew
.
.
.
.
.
.
.
.
.
.
.
.
§12.6 (p. 525)
fourn .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§12.4 (p. 518)
fpoly .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§15.4 (p. 674)
.
.
.
.
.
.
.
.
.
.
.
.
§18.1 (p. 784)
kermom
.
.
.
.
.
§18.3 (p. 793)
fred2
gauleg [ak] [g] ludcmp lubksb
fredex
quadmx ludcmp lubksb
fredin
[ak] [g]
.
.
.
.
.
.
.
.
.
.
.
.
.
§18.1 (p. 784)
frenel
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§6.9 (p. 249)
frprmn
[func] [dfunc] linmin
.
.
.
.
.
.
.
.
.
.
.
.
§10.6 (p. 416)
ftest
.
avevar betai
wwghts
mnbrak brent
[func]
f1dim
. . . gammln betacf
.
.
.
.
.
.
.
.
.
§14.2 (p. 613)
gamdev
ran1 .
.
.
.
.
.
.
.
.
.
.
.
.
§7.3 (p. 283)
gammln
.
.
.
.
.
.
.
.
.
.
.
.
.
§6.1 (p. 207)
. . gammln
.
.
.
.
.
.
.
.
.
§6.2 (p. 211)
gammp
.
gser gcf
.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
gammq
gammln
925
Index of Programs and Dependencies
gammq gasdev gaucof
hypser . . . icrc icrc1 . icrc1 . . . . igray . . . . iindexx . . . indexx . . . interp . . . irbit1 . . . irbit2 . . . jacobi . . . jacobn . . . julday . . . kendl1 erfcc kendl2 erfcc kermom . . .
. . . . . . . . . . . . . . .
hypdrv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
§6.2 (p. 211)
. .
. .
. .
. .
. .
. .
. .
. .
. .
§7.2 (p. 280) §4.5 (p. 151)
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . mmid pzextr
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . . §6.12 (p. 264) . . . . §20.3 (p. 893) . . . . §20.3 (p. 892) . . . . §20.2 (p. 888) INTEGER version of indexx, q.v. . . . . . §8.4 (p. 330) . . . . . §19.6 (p. 871) . . . . . §7.4 (p. 288) . . . . . §7.4 (p. 290) . . . . . §11.1 (p. 460) . . . . . §16.6 (p. 734) . . . . . §1.1 (p. 13) . . . . . §14.6 (p. 638) . . . . . §14.6 (p. 639) . . . . . §18.3 (p. 792)
. . . . . . . . . . . . . . .
. . . . . . . . . . . . . . .
. . . .
§4.5 §4.5 §4.5 §4.5 §2.1 §6.2 §10.1 §6.2 §8.5 §8.3 §11.6 §20.4 §20.4 §20.4 §20.4 §3.4 §6.12 §6.12
(p. 147) (p. 148) (p. 146) (p. 145) (p. 30) (p. 212) (p. 394) (p. 212) (p. 336) (p. 329) (p. 484) (p. 899) (p. 900) (p. 900) (p. 898) (p. 112) (p. 265) (p. 264)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
gauher gaujac gaulag gauleg gaussj gcf golden gser hpsel hpsort hqr hufapp hufdec hufenc hufmak hunt hypdrv hypgeo
gser . . gcf gammln ran1 . . . . tqli pythag eigsrt . . . . . . gammln . . . gammln . . . . . . . . . . . . . . . gammln . . . . [func] . . . gammln . . . . sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . hufapp . . . . . . . . . . . . . . . . hypser . . . odeint bsstep
926
Index of Programs and Dependencies
ks2d1s
quadct quadvl pearsn
.
.
.
.
.
.
.
§14.7 (p. 642)
. . . . . . betai gammln betacf
.
.
.
.
.
§14.7 (p. 643)
.
.
.
betai
.
.
gammln betacf
probks .
probks ksone
sort . [func] probks
.
.
.
.
.
.
.
.
.
.
.
.
§14.3 (p. 619)
kstwo
sort . probks
.
.
.
.
.
.
.
.
.
.
.
.
§14.3 (p. 619)
.
.
.
.
.
.
.
.
.
.
.
.
.
§9.5 (p. 366)
[funcs] . gaussj covsrt
.
.
.
.
.
.
.
.
.
.
.
.
§15.4 (p. 668)
linbcg
atimes snrm asolve
.
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 79)
linmin
mnbrak brent
. f1dim
.
. . [func]
.
.
.
.
.
.
§10.5 (p. 412)
lnsrch
[func]
.
.
.
.
.
.
.
.
.
.
.
.
§9.7 (p. 378)
locate
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§3.4 (p. 111)
.
laguer lfit
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§19.6 (p. 879)
lubksb
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.3 (p. 39)
ludcmp
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.3 (p. 38)
machar
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§20.1 (p. 884)
maloc .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§19.6 (p. 873)
matadd
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§19.6 (p. 879)
matsub
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§19.6 (p. 879)
medfit
rofunc
.
.
.
.
.
.
.
.
§15.7 (p. 699)
memcof
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§13.6 (p. 561)
metrop
ran3 .
.
.
.
.
.
.
.
.
.
.
.
.
§10.9 (p. 443)
mgfas
maloc . rstrct slvsm2 interp copy relax2 lop matsub
.
.
.
.
.
.
.
.
.
.
.
.
§19.6 (p. 877)
lop
.
.
select
fill0
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
quadct pearsn
ks2d2s
927
Index of Programs and Dependencies
mglin
.
.
.
.
.
.
.
.
.
.
.
.
§19.6 (p. 869)
fill0
fill0
midinf midpnt miser mmid mnbrak
addint
interp
[func] [func] ranpt [func] [derivs] [func]
. . . . . . ran1 .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§4.4 (p. 138) §4.4 (p. 136) §7.8 (p. 316)
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§16.3 (p. 717) §10.1 (p. 393)
.
.
.
.
.
.
.
.
.
.
§9.6 (p. 374)
. . . . . . dfour1
. . .
. . .
§14.1 (p. 607) §20.6 (p. 913) §20.6 (p. 911)
.
.
.
.
§20.6 (p. 911)
. . . . . . dfour1
. . .
. . .
§20.6 (p. 910) §20.6 (p. 907) §20.6 (p. 913)
. .
. .
. .
§2.5 (p. 48) §20.6 (p. 912)
. .
. .
. .
§15.5 (p. 681) §15.5 (p. 680)
.
mnewt
[usrfun] ludcmp lubksb
.
moment mp2dfr mpdiv
. . . mpops mpinv
. . . . . . mpmul mpops
. . . . . . . . drealft
mpmul mpops
drealft
dfour1
mpinv
mpmul mpops drealft mpmul mpops . . . . mpsqrt mppi mpops mpmul
mprove mpsqrt mrqcof mrqmin
drealft
dfour1
dfour1 . . . . . . . . . . mpmul drealft mpops drealft
.
dfour1
mpinv mpmul drealft dfour1 mp2dfr mpops lubksb . . . . . . . . . mpmul drealft dfour1 . . mpops [funcs] . . . . . . . . . mrqcof [funcs] . . . . . . gaussj covsrt
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
anorm2 matadd maloc . rstrct slvsml interp copy relax resid
928
newt
orthog pade
pccheb
fmin . . . . . . fdjac [funcv] ludcmp lubksb lnsrch fmin [funcv] [derivs] . . . . rkqs [derivs] rkck [derivs] . . . . . . . . . ludcmp . . . . . . . lubksb mprove lubksb . . . . . . . . .
.
.
.
.
.
.
§9.7 (p. 379)
.
.
.
.
.
.
§16.2 (p. 714)
. .
. .
. .
. .
. .
. .
§4.5 (p. 153) §5.12 (p. 196)
.
.
.
.
.
.
§5.11 (p. 193)
. . gammln betacf . . . . . . . . .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§5.10 (p. 192) §14.5 (p. 632)
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§13.8 (p. 572) §8.1 (p. 322) §8.1 (p. 321)
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§17.3 (p. 762) §6.8 (p. 247) §7.3 (p. 284)
polcoe
. . . . . . ran1 . gammln . . .
.
.
.
.
.
.
.
.
.
.
.
.
§3.5 (p. 114)
polcof poldiv polin2 polint
polint . . . polint . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
§3.5 §5.3 §3.6 §3.1
powell
[func] linmin
.
. . . . . . mnbrak brent f1dim
.
.
.
.
.
pcshft pearsn
. . . betai
period piksr2 piksrt
avevar . . . . . .
pinvs . plgndr poidev
predic probks psdes . pwt . pwtset pythag pzextr qgaus qrdcmp
. . . .
. . . .
. . . . . . [func] .
.
.
(p. 115) (p. 169) (p. 118) (p. 103)
§10.5 (p. 411)
[func]
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. §13.6 . §14.3 . §7.5 . §13.10
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. §13.10 (p. 589) . §2.6 (p. 62) . §16.4 (p. 724) . §4.5 (p. 141)
.
.
.
.
.
.
.
.
.
.
.
.
.
(p. 562) (p. 620) (p. 293) (p. 589)
§2.10 (p. 92)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
odeint
Index of Programs and Dependencies
929
Index of Programs and Dependencies
qromb qromo qroot qrsolv qrupdt qsimp qtrap quad3d
quadct quadmx quadvl ran0 ran1 ran2 ran3 ran4 rank ranpt ratint ratlsq
ratval rc . rd . realft rebin red relax relax2 resid revcst revers rf .
. . . . .
. . . . . .
.
. . .
[func]
.
.
.
.
.
.
.
.
.
§4.3 (p. 134)
[func]
.
.
.
.
.
.
.
.
.
§4.4 (p. 137)
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
§9.5 §2.10 §2.10 §4.2 §4.2 §4.6
(p. 371) (p. 93) (p. 94) (p. 133) (p. 131) (p. 157)
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
§14.7 §18.3 §14.7 §7.1 §7.1 §7.1 §7.1 §7.5 §8.4 §7.8 §3.2 §5.13
(p. 642) (p. 793) (p. 643) (p. 270) (p. 271) (p. 272) (p. 273) (p. 294) (p. 333) (p. 318) (p. 106) (p. 200)
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
§5.3 §6.11 §6.11 §12.3 §7.8 §17.3 §19.6 §19.6 §19.6 §10.9 §10.9 §6.11
(p. 170) (p. 259) (p. 257) (p. 507) (p. 314) (p. 763) (p. 872) (p. 878) (p. 872) (p. 441) (p. 442) (p. 257)
. . . . . . . . . [func] . [func] . [func] . [y1] [y2] [z1] [z2] . . . . . . . wwghts kermom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . psdes . . . . . . . . . . . . ran1 . . . . . . . . . . . . [fn] . . . . . dsvdcmp dpythag dsvbksb ratval . . . . . . . . . . . . . . . . . . . . . four1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
trapzd polint midpnt polint poldiv rsolv rotate trapzd trapzd qgaus
930
Index of Programs and Dependencies
.
.
.
.
.
.
.
.
.
.
§6.11 (p. 258)
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§16.1 (p. 706) §16.2 (p. 713) §16.1 (p. 707)
.
.
.
.
.
.
.
.
§16.2 (p. 712)
fourn . rlft3 select rofunc rotate . . . rsolv . . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
§12.5 §15.7 §2.10 §2.10
rstrct rtbis rtflsp
. . . [func] . [func]
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§19.6 (p. 870) §9.1 (p. 347) §9.2 (p. 349)
rtnewt rtsafe rtsec rzextr
[funcd] [funcd] [func] . . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
§9.4 §9.4 §9.2 §16.4
savgol
ludcmp lubksb
.
.
.
.
.
.
.
.
.
.
.
.
§14.8 (p. 646)
scrsho select selip
[func] . . . shell .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
§9.0 (p. 342) §8.5 (p. 334) §8.5 (p. 335)
sfroid
plgndr solvde
.
. . . . . difeq pinvs red bksub . . . . . . . . . . . . [derivs] rkqs rkck
.
.
.
.
.
.
§17.4 (p. 768)
. .
. .
. .
. .
. .
. .
§8.1 (p. 323) §17.1 (p. 750)
[derivs]
.
.
.
.
.
§17.2 (p. 752)
. . .
. . .
. . .
§10.8 (p. 434) §10.8 (p. 434) §10.8 (p. 435)
shell . . . . [load] . shoot odeint
shootf
[score] [load1] odeint
. . . . . [derivs] rkqs rkck
.
.
(p. 522) (p. 700) (p. 95) (p. 93)
(p. 358) (p. 359) (p. 350) (p. 725)
[derivs]
[score] [load2] simp1 . simp2 . simp3 .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
. . .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
rc . . . . . rf [derivs] . . rk4 [derivs] . rkck [derivs] rkdumb rk4 [derivs] rkck [derivs] rkqs rj
931
Index of Programs and Dependencies
simplx
simpr
sor . sort . sort2 . sort3 spctrm spear
sphbes sphfpt
bessjy newt
sphoot
newt
splie2 splin2
spline splint spline . . . . . . . . .
spline splint spread
.
.
.
.
.
.
.
.
.
.
.
.
§10.8 (p. 432)
.
.
.
.
.
.
.
.
.
.
.
.
§16.6 (p. 736)
four1 . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
§12.3 §19.6 §19.6 §6.11 §2.7 §7.7 §17.3
(p. 511) (p. 878) (p. 872) (p. 262) (p. 81) (p. 302) (p. 760)
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
§19.5 §8.2 §8.2 §8.4 §13.4 §14.6
(p. 860) (p. 324) (p. 326) (p. 332) (p. 550) (p. 635)
.
. .
. .
. .
. .
§6.7 (p. 245) §17.4 (p. 772)
.
.
.
.
.
§17.4 (p. 771)
. .
. .
. .
. .
. .
§3.6 (p. 121) §3.6 (p. 121)
. . .
. . .
. . .
. . .
. . .
§3.3 (p. 109) §3.3 (p. 110) §13.8 (p. 576)
. . . . . .
. . . . . .
gammln betacf beschb chebev fdjac shootf (q.v.) lnsrch fmin shootf (q.v.) ludcmp lubksb fdjac shoot (q.v.) lnsrch fmin shoot (q.v.) ludcmp lubksb . . . . . . . . . . . . . . . . .
. . .
. . .
. . .
. . .
. . .
. . .
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
sinft slvsm2 slvsml sncndn snrm . sobseq solvde
simp1 simp2 simp3 ludcmp lubksb [derivs] realft fill0 fill0 . . . . . . . . . difeq pinvs red bksub . . . . . . . . . indexx four1 sort2 . crank erfcc betai
932
Index of Programs and Dependencies
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 72)
sprsin
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 72)
sprspm
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 75)
sprstm
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 76)
sprstp
iindexx .
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 73)
sprstx
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.7 (p. 73)
stifbs
jacobn simpr
.
. . . ludcmp lubksb [derivs]
.
.
.
.
.
.
.
.
§16.6 (p. 737)
.
pzextr stiff
jacobn ludcmp lubksb [derivs]
.
.
.
.
.
.
.
.
.
.
.
.
§16.6 (p. 732)
stoerm
[derivs]
.
.
.
.
.
.
.
.
.
.
§16.5 (p. 726)
svbksb
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.6 (p. 56)
svdcmp
pythag
.
.
.
.
.
.
.
.
.
.
.
.
§2.6 (p. 59)
svdfit
[funcs] svdcmp svbksb
.
. . . pythag
.
.
.
.
.
.
.
.
§15.4 (p. 672)
svdvar
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§15.4 (p. 673)
toeplz
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.8 (p. 88)
tptest
avevar betai
.
. . . gammln betacf
.
.
.
.
.
.
.
.
§14.2 (p. 612)
pythag .
.
.
.
.
.
.
.
.
.
.
.
.
§11.3 (p. 473)
trapzd
[func]
.
.
.
.
.
.
.
.
.
.
.
.
§4.2 (p. 131)
tred2 .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§11.2 (p. 467)
tridag
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.4 (p. 43)
trncst
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§10.9 (p. 442)
trnspt
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
§10.9 (p. 442)
. . . gammln betacf
.
.
.
.
.
.
.
.
.
§14.2 (p. 610)
. . . gammln betacf
.
.
.
.
.
.
.
.
§14.2 (p. 611)
tqli
ttest
.
avevar betai
tutest
avevar betai
.
twofft
four1
.
.
.
.
.
.
.
.
.
.
.
.
§12.3 (p. 505)
vander
.
.
.
.
.
.
.
.
.
.
.
.
.
§2.8 (p. 84)
.
.
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
sprsax
.
.
.
.
.
.
.
.
.
.
.
.
§7.8 (p. 311)
[g] . [ak] ludcmp lubksb daub4 .
.
.
.
.
.
.
.
.
.
.
.
.
§18.2 (p. 787)
.
.
.
.
.
.
.
.
.
.
.
. §13.10 (p. 587) daub4 . wtn kermom wwghts [func] . zbrac [func] . zbrak
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. . . .
. §13.10 (p. 595) . §18.3 (p. 791) . §9.1 (p. 345) . §9.1 (p. 345) [func] balanc hqr [func] laguer
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§9.3 (p. 354) §9.5 (p. 368) . .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. .
§9.2 (p. 351) §9.5 (p. 367)
Sample page from NUMERICAL RECIPES IN FORTRAN 77: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43064-X) Copyright (C) 1986-1992 by Cambridge University Press.Programs Copyright (C) 1986-1992 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to [email protected] (outside North America).
rebin . ran2 [fxn] vegas
voltra
wt1
zbrent zrhqr
zriddr zroots
933 Index of Programs and Dependencies
General Index to Volumes 1 and 2 In this index, page numbers 1 through 934 refer to Volume 1, Numerical Recipes in Fortran 77, while page numbers 935 through 1446 refer to Volume 2, Numerical Recipes in Fortran 90. Front matter in Volume 1 is indicated by page numbers in the range 1/i through 1/xxxi, while front matter in Volume 2 is indicated 2/i through 2/xx.
Abstract data types
2/xiii, 1030 Accelerated convergence of series 160ff., 1070 Accuracy 19f. achievable in minimization 392, 397, 404 achievable in root finding 346f. contrasted with fidelity 832, 840 CPU different from memory 181 vs. stability 704, 729, 830, 844 Accuracy parameters 1362f. Acknowledgments 1/xvi, 2/ix Ada 2/x Adams-Bashford-Moulton method 741 Adams’ stopping criterion 366 Adaptive integration 123, 135, 703, 708ff., 720, 726, 731f., 737, 742ff., 788, 1298ff., 1303, 1308f. Monte Carlo 306ff., 1161ff. Addition, multiple precision 907, 1353 Addition theorem, elliptic integrals 255 ADI (alternating direction implicit) method 847, 861f., 906 Adjoint operator 867 Adobe Illustrator 1/xvi, 2/xx Advective equation 826 AGM (arithmetic geometric mean) 906 Airy function 204, 234, 243f. routine for 244f., 1121 Aitken’s delta squared process 160 Aitken’s interpolation algorithm 102 Algol 2/x, 2/xiv Algorithms, non-numerical 881ff., 1343ff. Aliasing 495, 569 see also Fourier transform all() intrinsic function 945, 948 All-poles model 566 see also Maximum entropy method (MEM) All-zeros model 566 see also Periodogram Allocatable array 938, 941, 952ff., 1197, 1212, 1266, 1293, 1306, 1336 allocate statement 938f., 941, 953f., 1197, 1266, 1293, 1306, 1336 allocated() intrinsic function 938, 952ff., 1197, 1266, 1293 Allocation status 938, 952ff., 961, 1197, 1266, 1293
934
Alpha AXP 2/xix Alternating-direction implicit method (ADI) 847, 861f., 906 Alternating series 160f., 1070 Alternative extended Simpson’s rule 128 American National Standards Institute (ANSI) 2/x, 2/xiii Amoeba 403 see also Simplex, method of Nelder and Mead Amplification factor 828, 830, 832, 840, 845f. Amplitude error 831 Analog-to-digital converter 812, 886 Analyticity 195 Analyze/factorize/operate package 64, 824 Anderson-Darling statistic 621 Andrew’s sine 697 Annealing, method of simulated 387f., 436ff., 1219ff. assessment 447 for continuous variables 437, 443ff., 1222 schedule 438 thermodynamic analogy 437 traveling salesman problem 438ff., 1219ff. ANSI (American National Standards Institute) 2/x, 2/xiii Antonov-Saleev variant of Sobol’ sequence 300, 1160 any() intrinsic function 945, 948 APL (computer language) 2/xi Apple 1/xxiii Macintosh 2/xix, 4, 886 Approximate inverse of matrix 49 Approximation of functions 99, 1043 by Chebyshev polynomials 185f., 513, 1076ff. Pad´e approximant 194ff., 1080f. by rational functions 197ff., 1081f. by wavelets 594f., 782 see also Fitting Argument keyword 2/xiv, 947f., 1341 optional 2/xiv, 947f., 1092, 1228, 1230, 1256, 1272, 1275, 1340 Argument checking 994f., 1086, 1090, 1092, 1370f.
Index to Volumes 1 and 2
Arithmetic arbitrary precision 881, 906ff., 1352ff. floating point 881, 1343 IEEE standard 276, 882, 1343 rounding 882, 1343 Arithmetic coding 881, 902ff., 1349ff. Arithmetic-geometric mean (AGM) method 906 Arithmetic-if statement 2/xi Arithmetic progression 971f., 996, 1072, 1127, 1365, 1371f. Array 953ff. allocatable 938, 941, 952ff., 1197, 1212, 1266, 1293, 1306, 1336 allocated with pointer 941 allocation 953 array manipulation functions 950 array sections 939, 941, 943ff. of arrays 2/xii, 956, 1336 associated pointer 953f. assumed-shape 942 automatic 938, 954, 1197, 1212, 1336 centered subarray of 113 conformable to a scalar 942f., 965, 1094 constructor 2/xii, 968, 971, 1022, 1052, 1055, 1127 copying 991, 1034, 1327f., 1365f. cumulative product 997f., 1072, 1086, 1375 cumulative sum 997, 1280f., 1365, 1375 deallocation 938, 953f., 1197, 1266, 1293 disassociated pointer 953 extents 938, 949 in Fortran 90 941 increasing storage for 955, 1070, 1302 index loss 967f. index table 1173ff. indices 942 inquiry functions 948ff. intrinsic procedures 2/xiii, 948ff. of length 0 944 of length 1 949 location of first “true” 993, 1041, 1369 location of maximum value 993, 1015, 1017, 1365, 1369 location of minimum value 993, 1369f. manipulation functions 950, 1247 masked swapping of elements in two arrays 1368 operations on 942, 949, 964ff., 969, 1026, 1040, 1050, 1200, 1326 outer product 949, 1076 parallel features 941ff., 964ff., 985 passing variable number of arguments to function 1022 of pointers forbidden 956, 1337 rank 938, 949 reallocation 955, 992, 1070f., 1365, 1368f. reduction functions 948ff. shape 938, 944, 949 size 938 skew sections 945, 985 stride 944 subscript bounds 942 subscript triplet 944
935
swapping elements of two arrays 991, 1015, 1365ff. target 938 three-dimensional, in Fortran 90 1248 transformational functions 948ff. unary and binary functions 949 undefined status 952ff., 961, 1266, 1293 zero-length 944 Array section 2/xiii, 943ff., 960 matches by shape 944 pointer alias 939, 944f., 1286, 1333 skew 2/xii, 945, 960, 985, 1284 vs. eoshift 1078 array copy() utility function 988, 991, 1034, 1153, 1278, 1328 arth() utility function 972, 974, 988, 996, 1072, 1086, 1127 replaces do-list 968 Artificial viscosity 831, 837 Ascending transformation, elliptic integrals 256 ASCII character set 6, 888, 896, 902 Assembly language 269 assert() utility function 988, 994, 1086, 1090, 1249 assert eq() utility function 988, 995, 1022 associated() intrinsic function 952f. Associated Legendre polynomials 246ff., 764, 1122f., 1319 recurrence relation for 247 relation to Legendre polynomials 246 Association, measures of 604, 622ff., 1275 Assumed-shape array 942 Asymptotic series 161 exponential integral 218 Attenuation factors 583, 1261 Autocorrelation 492 in linear prediction 558 use of FFT 538f., 1254 Wiener-Khinchin theorem 492, 566f. AUTODIN-II polynomial 890 Automatic array 938, 954, 1197, 1212, 1336 specifying size of 938, 954 Automatic deallocation 2/xv, 961 Autonomous differential equations 729f. Autoregressive model (AR) see Maximum entropy method (MEM) Average deviation of distribution 605, 1269 Averaging kernel, in Backus-Gilbert method 807
B acksubstitution
33ff., 39, 42, 92, 1017 in band diagonal matrix 46, 1021 in Cholesky decomposition 90, 1039 complex equations 41 direct for computing A−1 · B 40 with QR decomposition 93, 1040 relaxation solution of boundary value problems 755, 1316 in singular value decomposition 56, 1022f. Backtracking 419 in quasi-Newton methods 376f., 1195 Backus-Gilbert method 806ff. Backus, John 2/x Backward deflation 363
936
Index to Volumes 1 and 2
Bader-Deuflhard method 730, 735, 1310f. Bairstow’s method 364, 370, 1193 Balancing 476f., 1230f. Band diagonal matrix 42ff., 1019 backsubstitution 46, 1021 LU decomposition 45, 1020 multiply by vector 44, 1019 storage 44, 1019 Band-pass filter 551, 554f. wavelets 584, 592f. Bandwidth limited function 495 Bank accounts, checksum for 894 Bar codes, checksum for 894 Bartlett window 547, 1254ff. Base case, of recursive procedure 958 Base of representation 19, 882, 1343 BASIC, Numerical Recipes in 1, 2/x, 2/xviii Basis functions in general linear least squares 665 Bayes’ Theorem 810 Bayesian approach to inverse problems 799, 810f., 816f. contrasted with frequentist 810 vs. historic maximum entropy method 816f. views on straight line fitting 664 Bays’ shuffle 270 Bernoulli number 132 Bessel functions 223ff., 234ff., 936, 1101ff. asymptotic form 223f., 229f. complex 204 continued fraction 234, 239 double precision 223 fractional order 223, 234ff., 1115ff. Miller’s algorithm 175, 228, 1106 modified 229ff. modified, fractional order 239ff. modified, normalization formula 232, 240 modified, routines for 230ff., 1109ff. normalization formula 175 parallel computation of 1107ff. recurrence relation 172, 224, 232, 234 reflection formulas 236 reflection formulas, modified functions 241 routines for 225ff., 236ff., 1101ff. routines for modified functions 241ff., 1118 series for 160, 223 series for Kν 241 series for Yν 235 spherical 234, 245, 1121f. turning point 234 Wronskian 234, 239 Best-fit parameters 650, 656, 660, 698, 1285ff. see also Fitting Beta function 206ff., 1089 incomplete see Incomplete beta function BFGS algorithm see Broyden-Fletcher-GoldfarbShanno algorithm Bias, of exponent 19 Bias, removal in linear prediction 563 Biconjugacy 77
Biconjugate gradient method elliptic partial differential equations 824 preconditioning 78f., 824, 1037 for sparse system 77, 599, 1034ff. Bicubic interpolation 118f., 1049f. Bicubic spline 120f., 1050f. Big-endian 293 Bilinear interpolation 117 Binary constant, initialization 959 Binomial coefficients 206ff., 1087f. recurrences for 209 Binomial probability function 208 cumulative 222f. deviates from 281, 285f., 1155 Binormal distribution 631, 690 Biorthogonality 77 Bisection 111, 359, 1045f. compared to minimum bracketing 390ff. minimum finding with derivatives 399 root finding 343, 346f., 352f., 390, 469, 1184f. BISYNCH 890 Bit 18 manipulation functions see Bitwise logical functions reversal in fast Fourier transform (FFT) 499f., 525 bit size() intrinsic function 951 Bitwise logical functions 2/xiii, 17, 287, 890f., 951 Block-by-block method 788 Block of statements 7 Bode’s rule 126 Boltzmann probability distribution 437 Boltzmann’s constant 437 Bootstrap method 686f. Bordering method for Toeplitz matrix 85f. Borwein and Borwein method for π 906, 1357 Boundary 155f., 425f., 745 Boundary conditions for differential equations 701f. initial value problems 702 in multigrid method 868f. partial differential equations 508, 819ff., 848ff. for spheroidal harmonics 764 two-point boundary value problems 702, 745ff., 1314ff. Boundary value problems see Differential equations; Elliptic partial differential equations; Two-point boundary value problems Box-Muller algorithm for normal deviate 279f., 1152 Bracketing of function minimum 343, 390ff., 402, 1201f. of roots 341, 343ff., 353f., 362, 364, 369, 390, 1183f. Branch cut, for hypergeometric function 203 Branching 9 Break iteration 14 Brenner, N.M. 500, 517
Index to Volumes 1 and 2
Brent’s method minimization 389, 395ff., 660f., 1204ff., 1286 minimization, using derivative 389, 399, 1205 root finding 341, 349, 660f., 1188f., 1286 Broadcast (parallel capability) 965ff. Broyden-Fletcher-Goldfarb-Shanno algorithm 390, 418ff., 1215 Broyden’s method 373, 382f., 386, 1199f. singular Jacobian 386 btest() intrinsic function 951 Bubble sort 321, 1168 Bugs 4 in compilers 1/xvii how to report 1/iv, 2/iv Bulirsch-Stoer algorithm for rational function interpolation 105f., 1043 method (differential equations) 202, 263, 702f., 706, 716, 718ff., 726, 740, 1138, 1303ff. method (differential equations), stepsize control 719, 726 for second order equations 726, 1307 Burg’s LP algorithm 561, 1256 Byte 18
C (programming language)
13, 2/viii and case construct 1010 Numerical Recipes in 1, 2/x, 2/xvii C++ 1/xiv, 2/viii, 2/xvi, 7f. class templates 1083, 1106 Calendar algorithms 1f., 13ff., 1010ff. Calibration 653 Capital letters in programs 3, 937 Cards, sorting a hand of 321 Carlson’s elliptic integrals 255f., 1128ff. case construct 2/xiv, 1010 trapping errors 1036 Cash-Karp parameters 710, 1299f. Cauchy probability distribution see Lorentzian probability distribution Cauchy problem for partial differential equations 818f. Cayley’s representation of exp(−iHt) 844 CCITT (Comit´e Consultatif International T´el´egraphique et T´el´ephonique) 889f., 901 CCITT polynomial 889f. ceiling() intrinsic function 947 Center of mass 295ff. Central limit theorem 652f. Central tendency, measures of 604ff., 1269 Change of variable in integration 137ff., 788, 1056ff. in Monte Carlo integration 298 in probability distribution 279 Character functions 952 Character variables, in Fortran 90 1183 Characteristic polynomial digital filter 554 eigensystems 449, 469 linear prediction 559 matrix with a specified 368, 1193 of recurrence relation 175
937
Characteristics of partial differential equations 818 Chebyshev acceleration in successive overrelaxation (SOR) 859f., 1332 Chebyshev approximation 84, 124, 183, 184ff., 1076ff. Clenshaw-Curtis quadrature 190 Clenshaw’s recurrence formula 187, 1076 coefficients for 185f., 1076 contrasted with Pad´e approximation 195 derivative of approximated function 183, 189, 1077f. economization of series 192f., 195, 1080 for error function 214, 1095 even function 188 and fast cosine transform 513 gamma functions 236, 1118 integral of approximated function 189, 1078 odd function 188 polynomial fits derived from 191, 1078 rational function 197ff., 1081f. Remes exchange algorithm for filter 553 Chebyshev polynomials 184ff., 1076ff. continuous orthonormality 184 discrete orthonormality 185 explicit formulas for 184 formula for xk in terms of 193, 1080 Check digit 894, 1345f. Checksum 881, 888 cyclic redundancy (CRC) 888ff., 1344f. Cherry, sundae without a 809 Chi-by-eye 651 Chi-square fitting see Fitting; Least squares fitting Chi-square probability function 209ff., 215, 615, 654, 798, 1272 as boundary of confidence region 688f. related to incomplete gamma function 215 Chi-square test 614f. for binned data 614f., 1272 chi-by-eye 651 and confidence limit estimation 688f. for contingency table 623ff., 1275 degrees of freedom 615f. for inverse problems 797 least squares fitting 653ff., 1285 nonlinear models 675ff., 1292 rule of thumb 655 for straight line fitting 655ff., 1285 for straight line fitting, errors in both coordinates 660, 1286ff. for two binned data sets 616, 1272 unequal size samples 617 Chip rate 290 Chirp signal 556 Cholesky decomposition 89f., 423, 455, 1038 backsubstitution 90, 1039 operation count 90 pivoting 90 solution of normal equations 668 Circulant 585 Class, data type 7 Clenshaw-Curtis quadrature 124, 190, 512f.
938
Index to Volumes 1 and 2
Clenshaw’s recurrence formula 176f., 191, 1078 for Chebyshev polynomials 187, 1076 stability 176f. Clocking errors 891 CM computers (Thinking Machines Inc.) 964 CM Fortran 2/xv cn function 261, 1137f. Coarse-grid correction 864f. Coarse-to-fine operator 864, 1337 Coding arithmetic 902ff., 1349ff. checksums 888, 1344 decoding a Huffman-encoded message 900, 1349 Huffman 896f., 1346ff. run-length 901 variable length code 896, 1346ff. Ziv-Lempel 896 see also Arithmetic coding; Huffman coding Coefficients binomial 208, 1087f. for Gaussian quadrature 140ff., 1059ff. for Gaussian quadrature, nonclassical weight function 151ff., 788f., 1064 for quadrature formulas 125ff., 789, 1328 Cohen, Malcolm 2/xiv Column degeneracy 22 Column operations on matrix 29, 31f. Column totals 624 Combinatorial minimization see Annealing Comit´e Consultatif International T´el´egraphique et T´el´ephonique (CCITT) 889f., 901 Common block obsolescent 2/xif. superseded by internal subprogram 957, 1067 superseded by module 940, 953, 1298, 1320, 1322, 1324, 1330 Communication costs, in parallel processing 969, 981, 1250 Communication theory, use in adaptive integration 721 Communications protocol 888 Comparison function for rejection method 281 Compilers 964, 1364 CM Fortran 968 DEC (Digital Equipment Corp.) 2/viii IBM (International Business Machines) 2/viii Microsoft Fortran PowerStation 2/viii NAG (Numerical Algorithms Group) 2/viii, 2/xiv for parallel supercomputers 2/viii Complementary error function 1094f. see Error function Complete elliptic integral see Elliptic integrals Complex arithmetic 171f. avoidance of in path integration 203 cubic equations 179f. for linear equations 41 quadratic equations 178 Complex error function 252
Complex plane fractal structure for Newton’s rule 360f. path integration for function evaluation 201ff., 263, 1138 poles in 105, 160, 202f., 206, 554, 566, 718f. Complex systems of linear equations 41f. Compression of data 596f. Concordant pair for Kendall’s tau 637, 1281 Condition number 53, 78 Confidence level 687, 691ff. Confidence limits bootstrap method 687f. and chi-square 688f. confidence region, confidence interval 687 on estimated model parameters 684ff. by Monte Carlo simulation 684ff. from singular value decomposition (SVD) 693f. Confluent hypergeometric function 204, 239 Conformable arrays 942f., 1094 Conjugate directions 408f., 414ff., 1210 Conjugate gradient method biconjugate 77, 1034 compared to variable metric method 418 elliptic partial differential equations 824 for minimization 390, 413ff., 804, 815, 1210, 1214 minimum residual method 78 preconditioner 78f., 1037 for sparse system 77ff., 599, 1034 and wavelets 599 Conservative differential equations 726, 1307 Constrained linear inversion method 799ff. Constrained linear optimization see Linear programming Constrained optimization 387 Constraints, deterministic 804ff. Constraints, linear 423 CONTAINS statement 954, 957, 1067, 1134, 1202 Contingency coefficient C 625, 1275 Contingency table 622ff., 638, 1275f. statistics based on chi-square 623ff., 1275 statistics based on entropy 626ff., 1275f. Continued fraction 163ff. Bessel functions 234 convergence criterion 165 equivalence transformation 166 evaluation 163ff. evaluation along with normalization condition 240 even and odd parts 166, 211, 216 even part 249, 251 exponential integral 216 Fresnel integral 248f. incomplete beta function 219f., 1099f. incomplete gamma function 211, 1092f. Lentz’s method 165, 212 modified Lentz’s method 165 Pincherle’s theorem 175 ratio of Bessel functions 239 rational function approximation 164, 211, 219f. recurrence for evaluating 164f.
Index to Volumes 1 and 2
and recurrence relation 175 sine and cosine integrals 250f. Steed’s method 164f. tangent function 164 typography for 163 Continuous variable (statistics) 623 Control structures 7ff., 2/xiv bad 15 named 959, 1219, 1305 Convergence accelerated, for series 160ff., 1070 of algorithm for pi 906 criteria for 347, 392, 404, 483, 488, 679, 759 eigenvalues accelerated by shifting 470f. golden ratio 349, 399 of golden section search 392f. of Levenberg-Marquardt method 679 linear 346, 393 of QL method 470f. quadratic 49, 351, 356, 409f., 419, 906 rate 346f., 353, 356 recurrence relation 175 of Ridders’ method 351 series vs. continued fraction 163f. and spectral radius 856ff., 862 Conversion intrinsic functions 946f. Convex sets, use in inverse problems 804 Convolution denoted by asterisk 492 finite impulse response (FIR) 531 of functions 492, 503f. of large data sets 536f. for multiple precision arithmetic 909, 1354 multiplication as 909, 1354 necessity for optimal filtering 535 overlap-add method 537 overlap-save method 536f. and polynomial interpolation 113 relation to wavelet transform 585 theorem 492, 531ff., 546 theorem, discrete 531ff. treatment of end effects 533 use of FFT 523, 531ff., 1253 wraparound problem 533 Cooley-Tukey FFT algorithm 503, 1250 parallel version 1239f. Co-processor, floating point 886 Copyright rules 1/xx, 2/xix Cornwell-Evans algorithm 816 Corporate promotion ladder 328 Corrected two-pass algorithm 607, 1269 Correction, in multigrid method 863 Correlation coefficient (linear) 630ff., 1276 Correlation function 492 autocorrelation 492, 539, 558 and Fourier transforms 492 theorem 492, 538 treatment of end effects 538f. using FFT 538f., 1254 Wiener-Khinchin theorem 492, 566f. Correlation, statistical 603f., 622 Kendall’s tau 634, 637ff., 1279
939
linear correlation coefficient 630ff., 658, 1276 linear related to least square fitting 630, 658 nonparametric or rank statistical 633ff., 1277 among parameters in a fit 657, 667, 670 in random number generators 268 Spearman rank-order coefficient 634f., 1277 sum squared difference of ranks 634, 1277 Cosine function, recurrence 172 Cosine integral 248, 250ff., 1125f. continued fraction 250 routine for 251f., 1125 series 250 Cosine transform see Fast Fourier transform (FFT); Fourier transform Coulomb wave function 204, 234 count() intrinsic function 948 Courant condition 829, 832ff., 836 multidimensional 846 Courant-Friedrichs-Lewy stability criterion see Courant condition Covariance a priori 700 in general linear least squares 667, 671, 1288ff. matrix, by Cholesky decomposition 91, 667 matrix, of errors 796, 808 matrix, is inverse of Hessian matrix 679 matrix, when it is meaningful 690ff. in nonlinear models 679, 681, 1292 relation to chi-square 690ff. from singular value decomposition (SVD) 693f. in straight line fitting 657 cpu time() intrinsic function (Fortran 95) 961 CR method see Cyclic reduction (CR) Cramer’s V 625, 1275 Crank-Nicholson method 840, 844, 846 Cray computers 964 CRC (cyclic redundancy check) 888ff., 1344f. CRC-12 890 CRC-16 polynomial 890 CRC-CCITT 890 Creativity, essay on 9 Critical (Nyquist) sampling 494, 543 Cross (denotes matrix outer product) 66 Crosstabulation analysis 623 see also Contingency table Crout’s algorithm 36ff., 45, 1017 cshift() intrinsic function 950 communication bottleneck 969 Cubic equations 178ff., 360 Cubic spline interpolation 107ff., 1044f. see also Spline cumprod() utility function 974, 988, 997, 1072, 1086 cumsum() utility function 974, 989, 997, 1280, 1305 Cumulant, of a polynomial 977, 999, 1071f., 1192
940
Index to Volumes 1 and 2
Cumulative binomial distribution 222f. Cumulative Poisson function 214 related to incomplete gamma function 214 Curvature matrix see Hessian matrix cycle statement 959, 1219 Cycle, in multigrid method 865 Cyclic Jacobi method 459, 1225 Cyclic reduction (CR) 848f., 852ff. linear recurrences 974 tridiagonal systems 976, 1018 Cyclic redundancy check (CRC) 888ff., 1344f. Cyclic tridiagonal systems 67, 1030
D .C. (direct current)
492 Danielson-Lanczos lemma 498f., 525, 1235ff. DAP Fortran 2/xi Data assigning keys to 889 continuous vs. binned 614 entropy 626ff., 896, 1275 essay on 603 fitting 650ff., 1285ff. fraudulent 655 glitches in 653 iid (independent and identically distributed) 686 modeling 650ff., 1285ff. serial port 892 smoothing 604, 644ff., 1283f. statistical tests 603ff., 1269ff. unevenly or irregularly sampled 569, 574, 648f., 1258ff. use of CRCs in manipulating 889 windowing 545ff., 1254 see also Statistical tests Data compression 596f., 881 arithmetic coding 902ff., 1349ff. cosine transform 513 Huffman coding 896f., 902, 1346ff. linear predictive coding (LPC) 563ff. lossless 896 Data Encryption Standard (DES) 290ff., 1144, 1147f., 1156ff. Data hiding 956ff., 1209, 1293, 1296 Data parallelism 941, 964ff., 985 DATA statement 959 for binary, octal, hexadecimal constants 959 repeat count feature 959 superseded by initialization expression 943, 959, 1127 Data type 18, 936 accuracy parameters 1362f. character 1183 derived 2/xiii, 937, 1030, 1336, 1346 derived, for array of arrays 956, 1336 derived, initialization 2/xv derived, for Numerical Recipes 1361 derived, storage allocation 955 DP (double precision) 1361f. DPC (double precision complex) 1361 I1B (1 byte integer) 1361 I2B (2 byte integer) 1361 I4B (4 byte integer) 1361
intrinsic 937 LGT (default logical type) 1361 nrtype.f90 1361f. passing complex as real 1140 SP (single precision) 1361f. SPC (single precision complex) 1361 user-defined 1346 DAUB4 584ff., 588, 590f., 594, 1264f. DAUB6 586 DAUB12 598 DAUB20 590f., 1265 Daubechies wavelet coefficients 584ff., 588, 590f., 594, 598, 1264ff. Davidon-Fletcher-Powell algorithm 390, 418ff., 1215 Dawson’s integral 252ff., 600, 1127f. approximation for 252f. routine for 253f., 1127 dble() intrinsic function (deprecated) 947 deallocate statement 938f., 953f., 1197, 1266, 1293 Deallocation, of allocatable array 938, 953f., 1197, 1266, 1293 Debugging 8 DEC (Digital Equipment Corp.) 1/xxiii, 2/xix, 886 Alpha AXP 2/viii Fortran 90 compiler 2/viii quadruple precision option 1362 VAX 4 Decomposition see Cholesky decomposition; LU decomposition; QR decomposition; Singular value decomposition (SVD) Deconvolution 535, 540, 1253 see also Convolution; Fast Fourier transform (FFT); Fourier transform Defect, in multigrid method 863 Deferred approach to the limit see Richardson’s deferred approach to the limit Deflation of matrix 471 of polynomials 362ff., 370f., 977 Degeneracy of linear algebraic equations 22, 53, 57, 670 Degenerate kernel 785 Degenerate minimization principle 795 Degrees of freedom 615f., 654, 691 Dekker, T.J. 353 Demonstration programs 3, 936 Deprecated features common block 2/xif., 940, 953, 957, 1067, 1298, 1320, 1322, 1324, 1330 dble() intrinsic function 947 EQUIVALENCE statement 2/xif., 1161, 1286 statement function 1057, 1256 Derivatives computation via Chebyshev approximation 183, 189, 1077f. computation via Savitzky-Golay filters 183, 645 matrix of first partial see Jacobian determinant matrix of second partial see Hessian matrix
Index to Volumes 1 and 2
numerical computation 180ff., 379, 645, 732, 750, 771, 1075, 1197, 1309 of polynomial 167, 978, 1071f. use in optimization 388f., 399, 1205ff. Derived data type see Data type, derived DES see Data Encryption Standard Descending transformation, elliptic integrals 256 Descent direction 376, 382, 419 Descriptive statistics 603ff., 1269ff. see also Statistical tests Design matrix 645, 665, 795, 801, 1082 Determinant 25, 41 Deviates, random see Random deviates DFP algorithm see Davidon-Fletcher-Powell algorithm diagadd() utility function 985, 989, 1004 diagmult() utility function 985, 989, 1004, 1294 Diagonal dominance 43, 679, 780, 856 Difference equations, finite see Finite difference equations (FDEs) Difference operator 161 Differential equations 701ff., 1297ff. accuracy vs. stability 704, 729 Adams-Bashforth-Moulton schemes 741 adaptive stepsize control 703, 708ff., 719, 726, 731, 737, 742f., 1298ff., 1303ff., 1308f., 1311ff. algebraically difficult sets 763 backward Euler’s method 729 Bader-Deuflhard method for stiff 730, 735, 1310f. boundary conditions 701f., 745ff., 749, 751f., 771, 1314ff. Bulirsch-Stoer method 202, 263, 702, 706, 716, 718ff., 740, 1138, 1303 Bulirsch-Stoer method for conservative equations 726, 1307 comparison of methods 702f., 739f., 743 conservative 726, 1307 danger of too small stepsize 714 eigenvalue problem 748, 764ff., 770ff., 1319ff. embedded Runge-Kutta method 709f., 731, 1298, 1308 equivalence of multistep and multivalue methods 743 Euler’s method 702, 704, 728f. forward Euler’s method 728 free boundary problem 748, 776 high-order implicit methods 730ff., 1308ff. implicit differencing 729, 740, 1308 initial value problems 702 internal boundary conditions 775ff. internal singular points 775ff. interpolation on right-hand sides 111 Kaps-Rentrop method for stiff 730, 1308 local extrapolation 709 modified midpoint method 716f., 719, 1302f. multistep methods 740ff. multivalue methods 740 order of method 704f., 719
941
path integration for function evaluation 201ff., 263, 1138 predictor-corrector methods 702, 730, 740ff. reduction to first-order sets 701, 745 relaxation method 746f., 753ff., 1316ff. relaxation method, example of 764ff., 1319ff. r.h.s. independent of x 729f. Rosenbrock methods for stiff 730, 1308f. Runge-Kutta method 702, 704ff., 708ff., 731, 740, 1297f., 1308 Runge-Kutta method, high-order 705, 1297 Runge-Kutta-Fehlberg method 709ff., 1298 scaling stepsize to required accuracy 709 second order 726, 1307 semi-implicit differencing 730 semi-implicit Euler method 730, 735f. semi-implicit extrapolation method 730, 735f., 1311ff. semi-implicit midpoint rule 735f., 1310f. shooting method 746, 749ff., 1314ff. shooting method, example 770ff., 1321ff. similarity to Volterra integral equations 786 singular points 718f., 751, 775ff., 1315f., 1323ff. step doubling 708f. stepsize control 703, 708ff., 719, 726, 731, 737, 742f., 1298, 1303ff., 1308f. stiff 703, 727ff., 1308ff. stiff methods compared 739 Stoermer’s rule 726, 1307 see also Partial differential equations; Twopoint boundary value problems Diffusion equation 818, 838ff., 855 Crank-Nicholson method 840, 844, 846 Forward Time Centered Space (FTCS) 839ff., 855 implicit differencing 840 multidimensional 846 Digamma function 216 Digital filtering see Filter Dihedral group D5 894 dim optional argument 948 Dimensional expansion 965ff. Dimensions (units) 678 Diminishing increment sort 322, 1168 Dirac delta function 284, 780 Direct method see Periodogram Direct methods for linear algebraic equations 26, 1014 Direct product see Outer product of matrices Direction of largest decrease 410f. Direction numbers, Sobol’s sequence 300 Direction-set methods for minimization 389, 406f., 1210ff. Dirichlet boundary conditions 820, 840, 850, 856, 858 Disclaimer of warranty 1/xx, 2/xvii Discordant pair for Kendall’s tau 637, 1281 Discrete convolution theorem 531ff.
942
Index to Volumes 1 and 2
Discrete Fourier transform (DFT) 495ff., 1235ff. as approximate continuous transform 497 see also Fast Fourier transform (FFT) Discrete optimization 436ff., 1219ff. Discriminant 178, 457 Diskettes are ANSI standard 3 how to order 1/xxi, 2/xvii Dispersion 831 DISPO see Savitzky-Golay filters Dissipation, numerical 830 Divergent series 161 Divide and conquer algorithm 1226, 1229 Division complex 171 multiple precision 910f., 1356 of polynomials 169, 362, 370, 1072 dn function 261, 1137f. Do-list, implied 968, 971, 1127 Do-loop 2/xiv Do-until iteration 14 Do-while iteration 13 Dogleg step methods 386 Domain of integration 155f. Dominant solution of recurrence relation 174 Dot (denotes matrix multiplication) 23 dot product() intrinsic function 945, 949, 969, 1216 Double exponential error distribution 696 Double precision converting to 1362 as refuge of scoundrels 882 use in iterative improvement 47, 1022 Double root 341 Downhill simplex method see Simplex, method of Nelder and Mead DP, defined 937 Driver programs 3 Dual viewpoint, in multigrid method 875 Duplication theorem, elliptic integrals 256 DWT (discrete wavelet transform) see Wavelet transform Dynamical allocation of storage 2/xiii, 869, 938, 941f., 953ff., 1327, 1336 garbage collection 956 increasing 955, 1070, 1302
E ardley, D.M.
338 EBCDIC 890 Economization of power series 192f., 195, 1080 Eigensystems 449ff., 1225ff. balancing matrix 476f., 1230f. bounds on eigenvalues 50 calculation of few eigenvalues 454, 488 canned routines 454f. characteristic polynomial 449, 469 completeness 450 defective 450, 476, 489 deflation 471 degenerate eigenvalues 449ff. elimination method 453, 478, 1231 factorization method 453
fast Givens reduction 463 generalized eigenproblem 455 Givens reduction 462f. Hermitian matrix 475 Hessenberg matrix 453, 470, 476ff., 488, 1232 Householder transformation 453, 462ff., 469, 473, 475, 478, 1227f., 1231 ill-conditioned eigenvalues 477 implicit shifts 472ff., 1228f. and integral equations 779, 785 invariance under similarity transform 452 inverse iteration 455, 469, 476, 487ff., 1230 Jacobi transformation 453, 456ff., 462, 475, 489, 1225f. left eigenvalues 451 list of tasks 454f. multiple eigenvalues 489 nonlinear 455 nonsymmetric matrix 476ff., 1230ff. operation count of balancing 476 operation count of Givens reduction 463 operation count of Householder reduction 467 operation count of inverse iteration 488 operation count of Jacobi method 460 operation count of QL method 470, 473 operation count of QR method for Hessenberg matrices 484 operation count of reduction to Hessenberg form 479 orthogonality 450 parallel algorithms 1226, 1229 polynomial roots and 368, 1193 QL method 469ff., 475, 488f. QL method with implicit shifts 472ff., 1228f. QR method 52, 453, 456, 469ff., 1228 QR method for Hessenberg matrices 480ff., 1232ff. real, symmetric matrix 150, 467, 785, 1225, 1228 reduction to Hessenberg form 478f., 1231 right eigenvalues 451 shifting eigenvalues 449, 470f., 480 special matrices 454 termination criterion 484, 488 tridiagonal matrix 453, 469ff., 488, 1228 Eigenvalue and eigenvector, defined 449 Eigenvalue problem for differential equations 748, 764ff., 770ff., 1319ff. Eigenvalues and polynomial root finding 368, 1193 EISPACK 454, 475 Electromagnetic potential 519 ELEMENTAL attribute (Fortran 95) 961, 1084 Elemental functions 2/xiii, 2/xv, 940, 942, 946f., 961, 986, 1015, 1083, 1097f. Elimination see Gaussian elimination Ellipse in confidence limit estimation 688 Elliptic integrals 254ff., 906 addition theorem 255
Index to Volumes 1 and 2
Carlson’s forms and algorithms 255f., 1128ff. Cauchy principal value 256f. duplication theorem 256 Legendre 254ff., 260f., 1135ff. routines for 257ff., 1128ff. symmetric form 255 Weierstrass 255 Elliptic partial differential equations 818, 1332ff. alternating-direction implicit method (ADI) 861f., 906 analyze/factorize/operate package 824 biconjugate gradient method 824 boundary conditions 820 comparison of rapid methods 854 conjugate gradient method 824 cyclic reduction 848f., 852ff. Fourier analysis and cyclic reduction (FACR) 848ff., 854 Gauss-Seidel method 855, 864ff., 876, 1338, 1341 incomplete Cholesky conjugate gradient method (ICCG) 824 Jacobi’s method 855f., 864 matrix methods 824 multigrid method 824, 862ff., 1009, 1334ff. rapid (Fourier) method 824, 848ff. relaxation method 823, 854ff., 1332 strongly implicit procedure 824 successive over-relaxation (SOR) 857ff., 862, 866, 1332 elsewhere construct 943 Emacs, GNU 1/xvi Embedded Runge-Kutta method 709f., 731, 1298, 1308 Encapsulation, in programs 7 Encryption 290, 1156 enddo statement 12, 17 Entropy 896 of data 626ff., 811, 1275 EOM (end of message) 902 eoshift() intrinsic function 950 communication bottleneck 969 vector shift argument 1019f. vs. array section 1078 epsilon() intrinsic function 951, 1189 Equality constraints 423 Equations cubic 178ff., 360 normal (fitting) 645, 666ff., 800, 1288 quadratic 20, 178 see also Differential equations; Partial differential equations; Root finding Equivalence classes 337f., 1180 EQUIVALENCE statement 2/xif., 1161, 1286 Equivalence transformation 166 Error checksums for preventing 891 clocking 891 double exponential distribution 696 local truncation 875 Lorentzian distribution 696f. in multigrid method 863 nonnormal 653, 690, 694ff.
943
relative truncation 875 roundoff 180f., 881, 1362 series, advantage of an even 132f., 717, 1362 systematic vs. statistical 653, 1362 truncation 20f., 180, 399, 709, 881, 1362 varieties found by check digits 895 varieties of, in PDEs 831ff. see also Roundoff error Error function 213f., 601, 1094f. approximation via sampling theorem 601 Chebyshev approximation 214, 1095 complex 252 for Fisher’s z-transformation 632, 1276 relation to Dawson’s integral 252, 1127 relation to Fresnel integrals 248 relation to incomplete gamma function 213 routine for 214, 1094 for significance of correlation 631, 1276 for sum squared difference of ranks 635, 1277 Error handling in programs 2/xii, 2/xvi, 3, 994f., 1036, 1370f. Estimation of parameters see Fitting; Maximum likelihood estimate Estimation of power spectrum 542ff., 565ff., 1254ff., 1258 Euler equation (fluid flow) 831 Euler-Maclaurin summation formula 132, 135 Euler’s constant 216ff., 250 Euler’s method for differential equations 702, 704, 728f. Euler’s transformation 160f., 1070 generalized form 162f. Evaluation of functions see Function Even and odd parts, of continued fraction 166, 211, 216 Even parity 888 Exception handling in programs see Error handling in programs exit statement 959, 1219 Explicit differencing 827 Exponent in floating point format 19, 882, 1343 exponent intrinsic function 1107 Exponential deviate 278, 1151f. Exponential integral 215ff., 1096f. asymptotic expansion 218 continued fraction 216 recurrence relation 172 related to incomplete gamma function 215 relation to cosine integral 250 routine for Ei(x) 218, 1097 routine for En (x) 217, 1096 series 216 Exponential probability distribution 570 Extended midpoint rule 124f., 129f., 135, 1054f. Extended Simpson’s rule 128, 788, 790 Extended Simpson’s three-eighths rule 789 Extended trapezoidal rule 125, 127, 130ff., 135, 786, 1052ff., 1326 roundoff error 132 Extirpolation (so-called) 574, 1261
944
Index to Volumes 1 and 2
Extrapolation 99ff. in Bulirsch-Stoer method 718ff., 726, 1305ff. differential equations 702 by linear prediction 557ff., 1256f. local 709 maximum entropy method as type of 567 polynomial 724, 726, 740, 1305f. rational function 718ff., 726, 1306f. relation to interpolation 101 for Romberg integration 134 see also Interpolation Extremization see Minimization
F -distribution probability function
222 F-test for differences of variances 611, 613, 1271 FACR see Fourier analysis and cyclic reduction (FACR) Facsimile standard 901 Factorial double (denoted “!!”) 247 evaluation of 159, 1072, 1086 relation to gamma function 206 routine for 207f., 1086ff. False position 347ff., 1185f. Family tree 338 FAS (full approximation storage algorithm) 874, 1339ff. Fast Fourier transform (FFT) 498ff., 881, 981, 1235f. alternative algorithms 503f. as approximation to continuous transform 497 Bartlett window 547, 1254 bit reversal 499f., 525 and Clenshaw-Curtis quadrature 190 column-parallel algorithm 981, 1237ff. communication bottleneck 969, 981, 1250 convolution 503f., 523, 531ff., 909, 1253, 1354 convolution of large data sets 536f. Cooley-Tukey algorithm 503, 1250 Cooley-Tukey algorithm, parallel 1239f. correlation 538f., 1254 cosine transform 190, 511ff., 851, 1245f. cosine transform, second form 513, 852, 1246 Danielson-Lanczos lemma 498f., 525 data sets not a power of 2 503 data smoothing 645 data windowing 545ff., 1254 decimation-in-frequency algorithm 503 decimation-in-time algorithm 503 discrete autocorrelation 539, 1254 discrete convolution theorem 531ff. discrete correlation theorem 538 at double frequency 575 effect of caching 982 endpoint corrections 578f., 1261ff. external storage 525 figures of merit for data windows 548 filtering 551ff. FIR filter 553 four-step framework 983, 1239
Fourier integrals 577ff., 1261 Fourier integrals, infinite range 583 Hamming window 547 Hann window 547 history 498 IIR filter 553ff. image processing 803, 805 integrals using 124 inverse of cosine transform 512ff. inverse of sine transform 511 large data sets 525 leakage 544 memory-local algorithm 528 multidimensional 515ff., 1236f., 1241, 1246, 1251 for multiple precision arithmetic 906 for multiple precision multiplication 909, 1354 number-theoretic transforms 503f. operation count 498 optimal (Wiener) filtering 539ff., 558 order of storage in 501 parallel algorithms 981ff., 1235ff. partial differential equations 824, 848ff. Parzen window 547 periodicity of 497 periodogram 543ff., 566 power spectrum estimation 542ff., 1254ff. for quadrature 124 of real data in 2D and 3D 519ff., 1248f. of real functions 504ff., 519ff., 1242f., 1248f. related algorithms 503f. row-parallel algorithm 981, 1235f. Sande-Tukey algorithm 503 sine transform 508ff., 850, 1245 Singleton’s algorithm 525 six-step framework 983, 1240 square window 546, 1254 timing 982 treatment of end effects in convolution 533 treatment of end effects in correlation 538f. Tukey’s trick for frequency doubling 575 use in smoothing data 645 used for Lomb periodogram 574, 1259 variance of power spectrum estimate 544f., 549 virtual memory machine 528 Welch window 547, 1254 Winograd algorithms 503 see also Discrete Fourier transform (DFT); Fourier transform; Spectral density Faure sequence 300 Fax (facsimile) Group 3 standard 901 Feasible vector 424 FFT see Fast Fourier transform (FFT) Field, in data record 329 Figure-of-merit function 650 Filon’s method 583 Filter 551ff. acausal 552 bilinear transformation method 554 causal 552, 644
Index to Volumes 1 and 2
characteristic polynomial 554 data smoothing 644f., 1283f. digital 551ff. DISPO 644 by fast Fourier transform (FFT) 523, 551ff. finite impulse response (FIR) 531, 552 homogeneous modes of 554 infinite impulse response (IIR) 552ff., 566 Kalman 700 linear 552ff. low-pass for smoothing 644ff., 1283f. nonrecursive 552 optimal (Wiener) 535, 539ff., 558, 644 quadrature mirror 585, 593 realizable 552, 554f. recursive 552ff., 566 Remes exchange algorithm 553 Savitzky-Golay 183, 644ff., 1283f. stability of 554f. in the time domain 551ff. Fine-to-coarse operator 864, 1337 Finite difference equations (FDEs) 753, 763, 774 alternating-direction implicit method (ADI) 847, 861f. art not science 829 Cayley’s form for unitary operator 844 Courant condition 829, 832ff., 836 Courant condition (multidimensional) 846 Crank-Nicholson method 840, 844, 846 eigenmodes of 827f. explicit vs. implicit schemes 827 forward Euler 826f. Forward Time Centered Space (FTCS) 827ff., 839ff., 843, 855 implicit scheme 840 Lax method 828ff., 836 Lax method (multidimensional) 845f. mesh drifting instability 834f. numerical derivatives 181 partial differential equations 821ff. in relaxation methods 753ff. staggered leapfrog method 833f. two-step Lax-Wendroff method 835ff. upwind differencing 832f., 837 see also Partial differential equations Finite element methods, partial differential equations 824 Finite impulse response (FIR) 531 Finkelstein, S. 1/xvi, 2/ix FIR (finite impulse response) filter 552 Fisher’s z-transformation 631f., 1276 Fitting 650ff., 1285ff. basis functions 665 by Chebyshev approximation 185f., 1076 chi-square 653ff., 1285ff. confidence levels related to chi-square values 691ff. confidence levels from singular value decomposition (SVD) 693f. confidence limits on fitted parameters 684ff. covariance matrix not always meaningful 651, 690 degeneracy of parameters 674
945
an exponential 674 freezing parameters in 668, 700 Gaussians, a sum of 682, 1294 general linear least squares 665ff., 1288, 1290f. Kalman filter 700 K–S test, caution regarding 621f. least squares 651ff., 1285 Legendre polynomials 674, 1291f. Levenberg-Marquardt method 678ff., 816, 1292f. linear regression 655ff., 1285ff. maximum likelihood estimation 652f., 694ff. Monte Carlo simulation 622, 654, 684ff. multidimensional 675 nonlinear models 675ff., 1292f. nonlinear models, advanced methods 683 nonlinear problems that are linear 674 nonnormal errors 656, 690, 694ff. polynomial 83, 114, 191, 645, 665, 674, 1078, 1291 by rational Chebyshev approximation 197ff., 1081f. robust methods 694ff., 1294 of sharp spectral features 566 standard (probable) errors on fitted parameters 657f., 661, 667, 671, 684ff., 1285f., 1288, 1290 straight line 655ff., 667f., 698, 1285ff., 1294ff. straight line, errors in both coordinates 660ff., 1286ff. see also Error; Least squares fitting; Maximum likelihood estimate; Robust estimation Five-point difference star 867 Fixed point format 18 Fletcher-Powell algorithm see Davidon-FletcherPowell algorithm Fletcher-Reeves algorithm 390, 414ff., 1214 Floating point co-processor 886 Floating point format 18ff., 882, 1343 care in numerical derivatives 181 IEEE 276, 882, 1343 floor() intrinsic function 948 Flux-conservative initial value problems 825ff. FMG (full multigrid method) 863, 868, 1334ff. FOR iteration 9f., 12 forall statement 2/xii, 2/xv, 960, 964, 986 access to associated index 968 skew array sections 985, 1007 Formats of numbers 18ff., 882, 1343 Fortran 9 arithmetic-if statement 2/xi COMMON block 2/xif., 953, 957 deprecated features 2/xif., 947, 1057, 1161, 1256, 1286 dynamical allocation of storage 869, 1336 EQUIVALENCE statement 2/xif., 1161, 1286 evolution of 2/xivff. exception handling 2/xii, 2/xvi filenames 935 Fortran 2000 (planned) 2/xvi
946
Index to Volumes 1 and 2
Fortran 95 2/xv, 945, 947, 1084, 1100, 1364 HPF (High-Performance Fortran) 2/xvf. Numerical Recipes in 2/x, 2/xvii, 1 obsolescent features 2/xif. side effects 960 see also Fortran 90 Fortran D 2/xv Fortran 77 1/xix bit manipulation functions 17 hexadecimal constants 17 Fortran 8x 2/xi, 2/xiii Fortran 90 3 abstract data types 2/xiii, 1030 all() intrinsic function 945, 948 allocatable array 938, 941, 953ff., 1197, 1212, 1266, 1293, 1306, 1336 allocate statement 938f., 941, 953f., 1197, 1266, 1293, 1306, 1336 allocated() intrinsic function 938, 952ff., 1197, 1266, 1293 any() intrinsic function 945, 948 array allocation and deallocation 953 array of arrays 2/xii, 956, 1336 array constructor 2/xii, 968, 971, 1022, 1052, 1055, 1127 array constructor with implied do-list 968, 971 array extents 938, 949 array features 941ff., 953ff. array intrinsic procedures 2/xiii, 948ff. array of length 0 944 array of length 1 949 array manipulation functions 950 array parallel operations 964f. array rank 938, 949 array reallocation 955 array section 2/xiif., 2/xiii, 939, 941ff., 960, 1078, 1284, 1286, 1333 array shape 938, 949 array size 938, 942 array transpose 981f. array unary and binary functions 949 associated() intrinsic function 952f. associated pointer 953f. assumed-shape array 942 automatic array 938, 954, 1197, 1212, 1336 backwards-compatibility 935, 946 bit manipulation functions 2/xiii, 951 bit size() intrinsic function 951 broadcasts 965f. btest() intrinsic function 951 case construct 1010, 1036 case insensitive 937 ceiling() intrinsic function 947 character functions 952 character variables 1183 cmplx function 1125 communication bottlenecks 969, 981, 1250 compatibility with Fortran 77 935, 946 compilers 2/viii, 2/xiv, 1364 compiling 936 conformable arrays 942f., 1094
CONTAINS statement 954, 957, 985, 1067, 1134, 1202 control structure 2/xiv, 959, 1219, 1305 conversion elemental functions 946 count() intrinsic function 948 cshift() intrinsic function 950, 969 cycle statement 959, 1219 data hiding 956ff., 1209 data parallelism 964 DATA statement 959 data types 937, 1336, 1346, 1361 deallocate statement 938f., 953f., 1197, 1266, 1293 deallocating array 938, 953f., 1197, 1266, 1293 defined types 956 deprecated features 947, 1057, 1161, 1256, 1286 derived types 937, 955 dimensional expansion 965ff. do-loop 2/xiv dot product() intrinsic function 945, 949, 969, 1216 dynamical allocation of storage 2/xiii, 938, 941f., 953ff., 1327, 1336 elemental functions 940, 942, 946f., 951, 1015, 1083, 1364 elsewhere construct 943 eoshift() intrinsic function 950, 969, 1019f., 1078 epsilon() intrinsic function 951, 1189 evolution 2/xivff., 959, 987f. example 936 exit statement 959, 1219 exponent() intrinsic function 1107 floor() intrinsic function 948 Fortran tip icon 1009 garbage collection 956 gather-scatter operations 2/xiif., 969, 981, 984, 1002, 1032, 1034, 1250 generic interface 2/xiii, 1083 generic procedures 939, 1015, 1083, 1094, 1096, 1364 global variables 955, 957, 1210 history 2/xff. huge() intrinsic function 951 iand() intrinsic function 951 ibclr() intrinsic function 951 ibits() intrinsic function 951 ibset() intrinsic function 951 ieor() intrinsic function 951 IMPLICIT NONE statement 2/xiv, 936 implied do-list 968, 971, 1127 index loss 967f. initialization expression 943, 959, 1012, 1127 inquiry functions 948 integer model 1144, 1149, 1156 INTENT attribute 1072, 1092 interface 939, 942, 1067, 1084, 1384 internal subprogram 2/xii, 2/xiv, 957, 1057, 1067, 1202f., 1256, 1302 interprocessor communication 969, 981, 1250 intrinsic data types 937
Index to Volumes 1 and 2
intrinsic procedures 939, 945ff., 987, 1016 ior() intrinsic function 951 ishft() intrinsic function 951 ishftc() intrinsic function 951 ISO (International Standards Organization) 2/xf., 2/xiiif. keyword argument 2/xiv, 947f., 1341 kind() intrinsic function 951 KIND parameter 937, 946, 1125, 1144, 1192, 1254, 1261, 1284, 1361 language features 935ff. lbound() intrinsic function 949 lexical comparison 952 linear algebra 969f., 1000ff., 1018f., 1026, 1040, 1200, 1326 linear recurrence 971, 988 linking 936 literal constant 937, 1361 logo for tips 2/viii, 1009 mask 948, 967f., 1006f., 1038, 1102, 1200, 1226, 1305, 1333f., 1368, 1378, 1382 matmul() intrinsic function 945, 949, 969, 1026, 1040, 1050, 1076, 1200, 1216, 1290, 1326 maxexponent() intrinsic function 1107 maxloc() intrinsic function 949, 961, 992f., 1015 maxval() intrinsic function 945, 948, 961, 1016, 1273 memory leaks 953, 956, 1327 memory management 938, 953ff. merge() intrinsic function 945, 950, 1010, 1094f., 1099f. Metcalf and Reid (M&R) 935 minloc() intrinsic function 949, 961, 992f. minval() intrinsic function 948, 961 missing language features 983ff., 987ff. modularization 956f. MODULE facility 2/xiii, 936f., 939f., 953f., 957, 1067, 1298, 1320, 1322, 1324, 1330, 1346 MODULE subprograms 940 modulo() intrinsic function 946, 1156 named constant 940, 1012, 1361 named control structure 959, 1219, 1305 nearest() intrinsic function 952, 1146 nested where construct forbidden 943 not() intrinsic function 951 nullify statement 953f., 1070, 1302 numerical representation functions 951 ONLY option 941, 957, 1067 operator overloading 2/xiif. operator, user-defined 2/xii optional argument 2/xiv, 947f., 1092, 1228, 1230, 1256, 1272, 1275, 1340 outer product 969f. overloading 940, 1083, 1102 pack() intrinsic function 945, 950, 964, 969, 991, 1170, 1176, 1178 pack, for selective evaluation 1087 parallel extensions 2/xv, 959ff., 964, 981, 984, 987, 1002, 1032 parallel programming 963ff. PARAMETER attribute 1012
947
pointer 2/xiiif., 938f., 941, 944f., 952ff., 1067, 1070, 1197, 1210, 1212, 1266, 1302, 1327, 1336 pointer to function (missing) 1067 portability 963 present() intrinsic function 952 PRIVATE attribute 957, 1067 product() intrinsic function 948 programming conventions 937 PUBLIC attribute 957, 1067 quick start 936 radix() intrinsic function 1231 random number() intrinsic function 1141, 1143 random seed() intrinsic function 1141 real() intrinsic function 947, 1125 RECURSIVE keyword 958, 1065, 1067 recursive procedure 2/xiv, 958, 1065, 1067, 1166 reduction functions 948 reshape() intrinsic function 950, 969, 1247 RESULT keyword 958, 1073 SAVE attribute 953f., 958f., 1052, 1070, 1266, 1293 scale() intrinsic function 1107 scatter-with-combine (missing function) 984 scope 956ff. scoping units 939 select case statement 2/xiv, 1010, 1036 shape() intrinsic function 938, 949 size() intrinsic function 938, 942, 945, 948 skew sections 985 sparse matrix representation 1030 specification statement 2/xiv spread() intrinsic function 945, 950, 966ff., 969, 1000, 1094, 1290f. statement functions deprecated 1057 stride (of an array) 944 structure constructor 2/xii subscript triplet 944 sum() intrinsic function 945, 948, 966 tiny() intrinsic function 952 transformational functions 948 transpose() intrinsic function 950, 960, 969, 981, 1247 tricks 1009, 1072, 1146, 1274, 1278, 1280 truncation elemental functions 946 type checking 1140 ubound() intrinsic function 949 undefined pointer 953 unpack() intrinsic function 950, 964, 969 USE statement 936, 939f., 954, 957, 1067, 1384 utility functions 987ff. vector subscripts 2/xiif., 969, 981, 984, 1002, 1032, 1034, 1250 visibility 956ff., 1209, 1293, 1296 WG5 technical committee 2/xi, 2/xiii, 2/xvf. where construct 943, 985, 1060, 1291 X3J3 Committee 2/viii, 2/xff., 2/xv, 947, 959, 964, 968, 990 zero-length array 944
948
Index to Volumes 1 and 2
see also Intrinsic procedures see also Fortran Fortran 95 947, 959ff. allocatable variables 961 blocks 960 cpu time() intrinsic function 961 elemental functions 2/xiii, 2/xv, 940, 961, 986, 1015, 1083f., 1097f. forall statement 2/xii, 2/xv, 960, 964, 968, 986, 1007 initialization of derived data type 2/xv initialization of pointer 2/xv, 961 minor changes from Fortran 90 961 modified intrinsic functions 961 nested where construct 2/xv, 960, 1100 pointer association status 961 pointers 961 PURE attribute 2/xv, 960f., 964, 986 SAVE attribute 961 side effects 960 and skew array section 945, 985 see also Fortran Fortran 2000 2/xvi Forward deflation 363 Forward difference operator 161 Forward Euler differencing 826f. Forward Time Centered Space see FTCS Four-step framework, for FFT 983, 1239 Fourier analysis and cyclic reduction (FACR) 848f., 854 Fourier integrals attenuation factors 583, 1261 endpoint corrections 578f., 1261 tail integration by parts 583 use of fast Fourier transform (FFT) 577ff., 1261ff. Fourier transform 99, 490ff., 1235ff. aliasing 495, 569 approximation of Dawson’s integral 253 autocorrelation 492 basis functions compared 508f. contrasted with wavelet transform 584, 594 convolution 492, 503f., 531ff., 909, 1253, 1354 correlation 492, 538f., 1254 cosine transform 190, 511ff., 851, 1245f. cosine transform, second form 513, 852, 1246 critical sampling 494, 543, 545 definition 490 discrete Fourier transform (DFT) 184, 495ff. Gaussian function 600 image processing 803, 805 infinite range 583 inverse of discrete Fourier transform 497 method for partial differential equations 848ff. missing data 569 missing data, fast algorithm 574f., 1259 Nyquist frequency 494ff., 520, 543, 545, 569, 571 optimal (Wiener) filtering 539ff., 558 Parseval’s theorem 492, 498, 544
power spectral density (PSD) 492f. power spectrum estimation by FFT 542ff., 1254ff. power spectrum estimation by maximum entropy method 565ff., 1258 properties of 491f. sampling theorem 495, 543, 545, 600 scalings of 491 significance of a peak in 570 sine transform 508ff., 850, 1245 symmetries of 491 uneven sampling, fast algorithm 574f., 1259 unevenly sampled data 569ff., 574, 1258 and wavelets 592f. Wiener-Khinchin theorem 492, 558, 566f. see also Fast Fourier transform (FFT); Spectral density Fractal region 360f. Fractional step methods 847f. Fredholm alternative 780 Fredholm equations 779f. eigenvalue problems 780, 785 error estimate in solution 784 first kind 779 Fredholm alternative 780 homogeneous, second kind 785, 1325 homogeneous vs. inhomogeneous 779f. ill-conditioned 780 infinite range 789 inverse problems 780, 795ff. kernel 779f. nonlinear 781 Nystrom method 782ff., 789, 1325 product Nystrom method 789, 1328ff. second kind 779f., 782ff., 1325, 1331 with singularities 788, 1328ff. with singularities, worked example 792, 1328ff. subtraction of singularity 789 symmetric kernel 785 see also Inverse problems Frequency domain 490 Frequency spectrum see Fast Fourier transform (FFT) Frequentist, contrasted with Bayesian 810 Fresnel integrals 248ff. asymptotic form 249 continued fraction 248f. routine for 249f., 1123 series 248 Friday the Thirteenth 14f., 1011f. FTCS (forward time centered space) 827ff., 839ff., 843 stability of 827ff., 839ff., 855 Full approximation storage (FAS) algorithm 874, 1339ff. Full moon 14f., 936, 1011f. Full multigrid method (FMG) 863, 868, 1334ff. Full Newton methods, nonlinear least squares 683 Full pivoting 29, 1014 Full weighting 867 Function Airy 204, 243f., 1121
Index to Volumes 1 and 2
approximation 99ff., 184ff., 1043, 1076ff. associated Legendre polynomial 246ff., 764, 1122f., 1319 autocorrelation of 492 bandwidth limited 495 Bessel 172, 204, 223ff., 234, 1101ff., 1115ff. beta 209, 1089 binomial coefficients 208f., 1087f. branch cuts of 202f. chi-square probability 215, 798 complex 202 confluent hypergeometric 204, 239 convolution of 492 correlation of 492 cosine integral 250f., 1123f. Coulomb wave 204, 234 cumulative binomial probability 222f. cumulative Poisson 209ff. Dawson’s integral 252ff., 600, 1127f. digamma 216 elliptic integrals 254ff., 906, 1128ff. error 213f., 248, 252, 601, 631, 635, 1094f., 1127, 1276f. evaluation 159ff., 1070ff. evaluation by path integration 201ff., 263, 1138 exponential integral 172, 215ff., 250, 1096f. F-distribution probability 222 Fresnel integral 248ff., 1123 gamma 206, 1085 hypergeometric 202f., 263ff., 1138ff. incomplete beta 219ff., 610, 1098ff., 1269 incomplete gamma 209ff., 615, 654, 657f., 1089ff., 1272, 1285 inverse hyperbolic 178, 255 inverse trigonometric 255 Jacobian elliptic 261, 1137f. Kolmogorov-Smirnov probability 618f., 640, 1274, 1281 Legendre polynomial 172, 246, 674, 1122, 1291 logarithm 255 modified Bessel 229ff., 1109ff. modified Bessel, fractional order 239ff., 1118ff. overloading 1083 parallel evaluation 986, 1009, 1084, 1087, 1090, 1102, 1128, 1134 path integration to evaluate 201ff. pathological 99f., 343 Poisson cumulant 214 representations of 490 routine for plotting a 342, 1182 sine and cosine integrals 248, 250ff., 1125f. sn, dn, cn 261, 1137f. spherical harmonics 246ff., 1122 spheroidal harmonic 764ff., 770ff., 1319ff., 1323ff. Student’s probability 221f. variable number of arguments 1022 Weber 204
949
Functional iteration, for implicit equations 740f. FWHM (full width at half maximum) 548f.
G amma deviate 282f., 1153f. Gamma function 206ff., 1085 incomplete see Incomplete gamma function Garbage collection 956 Gather-scatter operations 2/xiif., 984, 1002, 1032, 1034 communication bottleneck 969, 981, 1250 many-to-one 984, 1002, 1032, 1034 Gauss-Chebyshev integration 141, 144, 512f. Gauss-Hermite integration 144, 789 abscissas and weights 147, 1062 normalization 147 Gauss-Jacobi integration 144 abscissas and weights 148, 1063 Gauss-Jordan elimination 27ff., 33, 64, 1014f. operation count 34, 39 solution of normal equations 667, 1288 storage requirements 30 Gauss-Kronrod quadrature 154 Gauss-Laguerre integration 144, 789, 1060 Gauss-Legendre integration 145f., 1059 see also Gaussian integration Gauss-Lobatto quadrature 154, 190, 512 Gauss-Radau quadrature 154 Gauss-Seidel method (relaxation) 855, 857, 864ff., 1338 nonlinear 876, 1341 Gauss transformation 256 Gaussian (normal) distribution 267, 652, 798 central limit theorem 652f. deviates from 279f., 571, 1152 kurtosis of 606 multivariate 690 semi-invariants of 608 tails compared to Poisson 653 two-dimensional (binormal) 631 variance of skewness of 606 Gaussian elimination 33f., 51, 55, 1014f. fill-in 45, 64 integral equations 786, 1326 operation count 34 outer product variant 1017 in reduction to Hessenberg form 478, 1231 relaxation solution of boundary value problems 753ff., 777, 1316 Gaussian function Hardy’s theorem on Fourier transforms 600 see also Gaussian (normal) distribution Gaussian integration 127, 140ff., 789, 1059ff. calculation of abscissas and weights 142ff., 1009, 1059ff. error estimate in solution 784 extensions of 153f. Golub-Welsch algorithm for weights and abscissas 150, 1064 for integral equations 781, 783, 1325 from known recurrence relation 150, 1064
950
Index to Volumes 1 and 2
nonclassical weight function 151ff., 788f., 1064f., 1328f. and orthogonal polynomials 142, 1009, 1061 parallel calculation of formulas 1009, 1061 preassigned nodes 153f. weight function log x 153 weight functions 140ff., 788f., 1059ff., 1328f. Gear’s method (stiff ODEs) 730 Geiger counter 266 Generalized eigenvalue problems 455 Generalized minimum residual method (GMRES) 78 Generic interface see Interface, generic Generic procedures 939, 1083, 1094, 1096, 1364 elemental 940, 942, 946f., 1015, 1083 Geometric progression 972, 996f., 1365, 1372ff. geop() utility function 972, 974, 989, 996, 1127 Geophysics, use of Backus-Gilbert method 809 Gerchberg-Saxton algorithm 805 get diag() utility function 985, 989, 1005, 1226 Gilbert and Sullivan 714 Givens reduction 462f., 473 fast 463 operation count 463 Glassman, A.J. 180 Global optimization 387f., 436ff., 650, 1219ff. continuous variables 443f., 1222 Global variables 940, 953f., 1210 allocatable array method 954, 1197, 1212, 1266, 1287, 1298 communicated via internal subprogram 954, 957f., 1067, 1226 danger of 957, 1209, 1293, 1296 pointer method 954, 1197, 1212, 1266, 1287, 1302 Globally convergent minimization 418ff., 1215 root finding 373, 376ff., 382, 749f., 752, 1196, 1314f. GMRES (generalized minimum residual method) 78 GNU Emacs 1/xvi Godunov’s method 837 Golden mean (golden ratio) 21, 349, 392f., 399 Golden section search 341, 389ff., 395, 1202ff. Golub-Welsch algorithm, for Gaussian quadrature 150, 1064 Goodness-of-fit 650, 654, 657f., 662, 690, 1285 GOTO statements, danger of 9, 959 Gram-Schmidt biorthogonalization 415f. orthogonalization 94, 450f., 1039 SVD as alternative to 58 Graphics, function plotting 342, 1182f. Gravitational potential 519
Gray code 300, 881, 886ff., 1344 Greenbaum, A. 79 Gregorian calendar 13, 16, 1011, 1013 Grid square 116f. Group, dihedral 894, 1345 Guard digits 882, 1343
H alf weighting 867, 1337 Halton’s quasi-random sequence 300 Hamming window 547 Hamming’s motto 341 Hann window 547 Harmonic analysis see Fourier transform Hashing 293, 1144, 1148, 1156 for random number seeds 1147f. HDLC checksum 890 Heap (data structure) 327f., 336, 897, 1179 Heapsort 320, 327f., 336, 1171f., 1179 Helmholtz equation 852 Hermite polynomials 144, 147 approximation of roots 1062 Hermitian matrix 450ff., 475 Hertz (unit of frequency) 490 Hessenberg matrix 94, 453, 470, 476ff., 488, 1231 see also Matrix Hessian matrix 382, 408, 415f., 419f., 676ff., 803, 815 is inverse of covariance matrix 667, 679 second derivatives in 676 Hexadecimal constants 17f., 276, 293 initialization 959 Hierarchically band diagonal matrix 598 Hierarchy of program structure 6ff. High-order not same as high-accuracy 100f., 124, 389, 399, 705, 709, 741 High-pass filter 551 High-Performance Fortran (HPF) 2/xvf., 964, 981, 984 scatter-with-add 1032 Hilbert matrix 83 Home page, Numerical Recipes 1/xx, 2/xvii Homogeneous linear equations 53 Hook step methods 386 Hotelling’s method for matrix inverse 49, 598 Householder transformation 52, 453, 462ff., 469, 473, 475, 478, 481ff., 1227f. operation count 467 in QR decomposition 92, 1039 HPF see High-Performance Fortran Huffman coding 564, 881, 896f., 902, 1346ff. huge() intrinsic function 951 Hyperbolic functions, explicit formulas for inverse 178 Hyperbolic partial differential equations 818 advective equation 826 flux-conservative initial value problems 825ff. Hypergeometric function 202f., 263ff. routine for 264f., 1138 Hypothesis, null 603 I 2B, defined
937
Index to Volumes 1 and 2
I4B, defined 937 iand() intrinsic function 951 ibclr() intrinsic function 951 ibits() intrinsic function 951 IBM 1/xxiii, 2/xix bad random number generator 268 Fortran 90 compiler 2/viii PC 4, 276, 293, 886 PC-RT 4 radix base for floating point arithmetic 476 RS6000 2/viii, 4 IBM checksum 894 ibset() intrinsic function 951 ICCG (incomplete Cholesky conjugate gradient method) 824 ICF (intrinsic correlation function) model 817 Identity (unit) matrix 25 IEEE floating point format 276, 882f., 1343 ieor() intrinsic function 951 if statement, arithmetic 2/xi if structure 12f. ifirstloc() utility function 989, 993, 1041, 1346 IIR (infinite impulse response) filter 552ff., 566 Ill-conditioned integral equations 780 Image processing 519, 803 cosine transform 513 fast Fourier transform (FFT) 519, 523, 803 as an inverse problem 803 maximum entropy method (MEM) 809ff. from modulus of Fourier transform 805 wavelet transform 596f., 1267f. imaxloc() utility function 989, 993, 1017 iminloc() utility function 989, 993, 1046, 1076 Implicit function theorem 340 pivoting 30, 1014 shifts in QL method 472ff. Implicit differencing 827 for diffusion equation 840 for stiff equations 729, 740, 1308 IMPLICIT NONE statement 2/xiv, 936 Implied do-list 968, 971, 1127 Importance sampling, in Monte Carlo 306f. Improper integrals 135ff., 1055 Impulse response function 531, 540, 552 IMSL 1/xxiii, 2/xx, 26, 64, 205, 364, 369, 454 In-place selection 335, 1178f. Included file, superseded by module 940 Incomplete beta function 219ff., 1098ff. for F-test 613, 1271 routine for 220f., 1097 for Student’s t 610, 613, 1269 Incomplete Cholesky conjugate gradient method (ICCG) 824 Incomplete gamma function 209ff., 1089ff. for chi-square 615, 654, 657f., 1272, 1285 deviates from 282f., 1153 in mode estimation 610 routine for 211f., 1089
951
Increment of linear congruential generator 268 Indentation of blocks 9 Index 934ff., 1446ff. this entry 1464 Index loss 967f., 1038 Index table 320, 329f., 1173ff., 1176 Inequality constraints 423 Inheritance 8 Initial value problems 702, 818f. see also Differential equations; Partial differential equations Initialization of derived data type 2/xv Initialization expression 943, 959, 1012, 1127 Injection operator 864, 1337 Instability see Stability Integer model, in Fortran 90 1144, 1149, 1156 Integer programming 436 Integral equations 779ff. adaptive stepsize control 788 block-by-block method 788 correspondence with linear algebraic equations 779ff. degenerate kernel 785 eigenvalue problems 780, 785 error estimate in solution 784 Fredholm 779f., 782ff., 1325, 1331 Fredholm alternative 780 homogeneous, second kind 785, 1325 ill-conditioned 780 infinite range 789 inverse problems 780, 795ff. kernel 779 nonlinear 781, 787 Nystrom method 782ff., 789, 1325 product Nystrom method 789, 1328ff. with singularities 788ff., 1328ff. with singularities, worked example 792, 1328ff. subtraction of singularity 789 symmetric kernel 785 unstable quadrature 787f. Volterra 780f., 786ff., 1326f. wavelets 782 see also Inverse problems Integral operator, wavelet approximation of 597, 782 Integration of functions 123ff., 1052ff. cosine integrals 250, 1125 Fourier integrals 577ff., 1261 Fourier integrals, infinite range 583 Fresnel integrals 248, 1123 Gauss-Hermite 147f., 1062 Gauss-Jacobi 148, 1063 Gauss-Laguerre 146, 1060 Gauss-Legendre 145, 1059 integrals that are elliptic integrals 254 path integration 201ff. sine integrals 250, 1125 see also Quadrature Integro-differential equations 782 INTENT attribute 1072, 1092 Interface (Fortran 90) 939, 942, 1067
952
Index to Volumes 1 and 2
for communication between program parts 957, 1209, 1293, 1296 explicit 939, 942, 1067, 1384 generic 2/xiii, 940, 1015, 1083, 1094, 1096 implicit 939 for Numerical Recipes 1384ff. Interface block 939, 1084, 1384 Interface, in programs 2, 8 Intermediate value theorem 343 Internal subprogram (Fortran 90) 2/xiv, 954, 957, 1067, 1202f., 1226 nesting of 2/xii resembles C macro 1302 supersedes statement function 1057, 1256 International Standards Organization (ISO) 2/xf., 2/xiii Internet, availability of code over 1/xx, 2/xvii Interpolation 99ff. Aitken’s algorithm 102 avoid 2-stage method 100 avoid in Fourier analysis 569 bicubic 118f., 1049f. bilinear 117 caution on high-order 100 coefficients of polynomial 100, 113ff., 191, 575, 1047f., 1078 for computing Fourier integrals 578 error estimates for 100 of functions with poles 104ff., 1043f. inverse quadratic 353, 395ff., 1204 multidimensional 101f., 116ff., 1049ff. in multigrid method 866, 1337 Neville’s algorithm 102f., 182, 1043 Nystrom 783, 1326 offset arrays 104, 113 operation count for 100 operator 864, 1337 order of 100 and ordinary differential equations 101 oscillations of polynomial 100, 116, 389, 399 parabolic, for minimum finding 395, 1204 polynomial 99, 102ff., 182, 1043 rational Chebyshev approximation 197ff., 1081 rational function 99, 104ff., 194ff., 225, 718ff., 726, 1043f., 1080, 1306 reverse (extirpolation) 574, 1261 spline 100, 107ff., 120f., 1044f., 1050f. trigonometric 99 see also Fitting Interprocessor communication 969, 981 Interval variable (statistics) 623 Intrinsic correlation function (ICF) model 817 Intrinsic data types 937 Intrinsic procedures array inquiry 938, 942, 948ff. array manipulation 950 array reduction 948 array unary and binary functions 949 backwards-compatibility 946 bit manipulation 2/xiii, 951 character 952 cmplx 1254
conversion elemental 946 elemental 940, 942, 946f., 951, 1083, 1364 generic 939, 1083f., 1364 lexical comparison 952 numeric inquiry 2/xiv, 1107, 1231, 1343 numerical 946, 951f. numerical representation 951 pack used for sorting 1171 random number 1143 real 1254 top 10 945 truncation 946f. see also Fortran 90 Inverse hyperbolic function 178, 255 Inverse iteration see Eigensystems Inverse problems 779, 795ff. Backus-Gilbert method 806ff. Bayesian approach 799, 810f., 816f. central idea 799 constrained linear inversion method 799ff. data inversion 807 deterministic constraints 804ff. in geophysics 809 Gerchberg-Saxton algorithm 805 incomplete Fourier coefficients 813 and integral equations 780 linear regularization 799ff. maximum entropy method (MEM) 810, 815f. MEM demystified 814 Phillips-Twomey method 799ff. principal solution 797 regularization 796ff. regularizing operator 798 stabilizing functional 798 Tikhonov-Miller regularization 799ff. trade-off curve 795 trade-off curve, Backus-Gilbert method 809 two-dimensional regularization 803 use of conjugate gradient minimization 804, 815 use of convex sets 804 use of Fourier transform 803, 805 Van Cittert’s method 804 Inverse quadratic interpolation 353, 395ff., 1204 Inverse response kernel, in Backus-Gilbert method 807 Inverse trigonometric function 255 ior() intrinsic function 951 ISBN (International Standard Book Number) checksum 894 ishft() intrinsic function 951 ishftc() intrinsic function 951 ISO (International Standards Organization) 2/xf., 2/xiii Iterated integrals 155 Iteration 9f. functional 740f. to improve solution of linear algebraic equations 47ff., 195, 1022 for linear algebraic equations 26
Index to Volumes 1 and 2
required for two-point boundary value problems 745 in root finding 340f. Iteration matrix 856 ITPACK 71 Iverson, John 2/xi
J acobi matrix, for Gaussian quadrature
150, 1064 Jacobi polynomials, approximation of roots 1064 Jacobi transformation (or rotation) 94, 453, 456ff., 462, 475, 489, 1041, 1225 Jacobian determinant 279, 774 Jacobian elliptic functions 261, 1137f. Jacobian matrix 374, 376, 379, 382, 731, 1197f., 1309 singular in Newton’s rule 386 Jacobi’s method (relaxation) 855ff., 864 Jenkins-Traub method 369 Julian Day 1, 13, 16, 936, 1010ff. Jump transposition errors 895
K -S test see Kolmogorov-Smirnov test Kalman filter 700 Kanji 2/xii Kaps-Rentrop method 730, 1308 Kendall’s tau 634, 637ff., 1279 Kennedy, Ken 2/xv Kepler’s equation 1061 Kermit checksum 889 Kernel 779 averaging, in Backus-Gilbert method 807 degenerate 785 finite rank 785 inverse response 807 separable 785 singular 788f., 1328 symmetric 785 Keys used in sorting 329, 889 Keyword argument 2/xiv, 947f., 1341 kind() intrinsic function 951 KIND parameter 946, 1261, 1284 and cmplx() intrinsic function 1125, 1192, 1254 default 937 for Numerical Recipes 1361 for random numbers 1144 and real() intrinsic function 1125 Kolmogorov-Smirnov test 614, 617ff., 694, 1273f. two-dimensional 640, 1281ff. variants 620ff., 640, 1281 Kuiper’s statistic 621 Kurtosis 606, 608, 1269 L-estimate
694 Labels, statement 9 Lag 492, 538, 553 Lagged Fibonacci generator 1142, 1148ff. Lagrange multiplier 795 Lagrange’s formula for polynomial interpolation 84, 102f., 575, 578
953
Laguerre polynomials, approximation of roots 1061 Laguerre’s method 341, 365f., 1191f. Lanczos lemma 498f. Lanczos method for gamma function 206, 1085 Landen transformation 256 LAPACK 26, 1230 Laplace’s equation 246, 818 see also Poisson equation Las Vegas 625 Latin square or hypercube 305f. Laurent series 566 Lax method 828ff., 836, 845f. multidimensional 845f. Lax-Wendroff method 835ff. lbound() intrinsic function 949 Leakage in power spectrum estimation 544, 548 Leakage width 548f. Leapfrog method 833f. Least squares filters see Savitzky-Golay filters Least squares fitting 645, 651ff., 655ff., 660ff., 665ff., 1285f., 1288f. contrasted to general minimization problems 684ff. degeneracies in 671f., 674 Fourier components 570 as M-estimate for normal errors 696 as maximum likelihood estimator 652 as method for smoothing data 645, 1283 Fourier components 1258 freezing parameters in 668, 700 general linear case 665ff., 1288, 1290f. Levenberg-Marquardt method 678ff., 816, 1292f. Lomb periodogram 570, 1258 multidimensional 675 nonlinear 386, 675ff., 816, 1292 nonlinear, advanced methods 683 normal equations 645, 666f., 800, 1288 normal equations often singular 670, 674 optimal (Wiener) filtering 540f. QR method in 94, 668 for rational Chebyshev approximation 199f., 1081f. relation to linear correlation 630, 658 Savitzky-Golay filter as 645, 1283 singular value decomposition (SVD) 25f., 51ff., 199f., 670ff., 1081, 1290 skewed by outliers 653 for spectral analysis 570, 1258 standard (probable) errors on fitted parameters 667, 671 weighted 652 see also Fitting L’Ecuyer’s long period random generator 271, 273 Least squares fitting standard (probable) errors on fitted parameters 1288, 1290 weighted 1285 Left eigenvalues or eigenvectors 451 Legal matters 1/xx, 2/xvii Legendre elliptic integral see Elliptic integrals
954
Index to Volumes 1 and 2
Legendre polynomials 246, 1122 fitting data to 674, 1291f. recurrence relation 172 shifted monic 151 see also Associated Legendre polynomials; Spherical harmonics Lehmer-Schur algorithm 369 Lemarie’s wavelet 593 Lentz’s method for continued fraction 165, 212 Lepage, P. 309 Leptokurtic distribution 606 Levenberg-Marquardt algorithm 386, 678ff., 816, 1292 advanced implementation 683 Levinson’s method 86, 1038 Lewis, H.W. 275 Lexical comparison functions 952 LGT, defined 937 License information 1/xx, 2/xviiff. Limbo 356 Limit cycle, in Laguerre’s method 365 Line minimization see Minimization, along a ray Line search see Minimization, along a ray Linear algebra, intrinsic functions for parallelization 969f., 1026, 1040, 1200, 1326 Linear algebraic equations 22ff., 1014 band diagonal 43ff., 1019 biconjugate gradient method 77, 1034ff. Cholesky decomposition 89f., 423, 455, 668, 1038f. complex 41 computing A−1 · B 40 conjugate gradient method 77ff., 599, 1034 cyclic tridiagonal 67, 1030 direct methods 26, 64, 1014, 1030 Fortran 90 vs. library routines 1016 Gauss-Jordan elimination 27ff., 1014 Gaussian elimination 33f., 1014f. Hilbert matrix 83 Hotelling’s method 49, 598 and integral equations 779ff., 783, 1325 iterative improvement 47ff., 195, 1022 iterative methods 26, 77ff., 1034 large sets of 23 least squares solution 53ff., 57f., 199f., 671, 1081, 1290 LU decomposition 34ff., 195, 386, 732, 783, 786, 801, 1016, 1022, 1325f. nonsingular 23 overdetermined 25f., 199, 670, 797 partitioned 70 QR decomposition 91f., 382, 386, 668, 1039f., 1199 row vs. column elimination 31f. Schultz’s method 49, 598 Sherman-Morrison formula 65ff., 83 singular 22, 53, 58, 199, 670 singular value decomposition (SVD) 51ff., 199f., 670ff., 797, 1022, 1081, 1290 sparse 23, 43, 63ff., 732, 804, 1020f., 1030
summary of tasks 25f. Toeplitz 82, 85ff., 195, 1038 tridiagonal 26, 42f., 64, 109, 150, 453f., 462ff., 469ff., 488, 839f., 853, 861f., 1018f., 1227ff. Vandermonde 82ff., 114, 1037, 1047 wavelet solution 597ff., 782 Woodbury formula 68ff., 83 see also Eigensystems Linear congruential random number generator 267ff., 1142 choice of constants for 274ff. Linear constraints 423 Linear convergence 346, 393 Linear correlation (statistics) 630ff., 1276 Linear dependency constructing orthonormal basis 58, 94 of directions in N -dimensional space 409 in linear algebraic equations 22f. Linear equations see Differential equations; Integral equations; Linear algebraic equations Linear inversion method, constrained 799ff. Linear prediction 557ff. characteristic polynomial 559 coefficients 557ff., 1256 compared to maximum entropy method 558 compared with regularization 801 contrasted to polynomial extrapolation 560 related to optimal filtering 558 removal of bias in 563 stability 559f., 1257 Linear predictive coding (LPC) 563ff. Linear programming 387, 423ff., 1216ff. artificial variables 429 auxiliary objective function 430 basic variables 426 composite simplex algorithm 435 constraints 423 convergence criteria 432 degenerate feasible vector 429 dual problem 435 equality constraints 423 feasible basis vector 426 feasible vector 424 fundamental theorem 426 inequality constraints 423 left-hand variables 426 nonbasic variables 426 normal form 426 objective function 424 optimal feasible vector 424 pivot element 428f. primal-dual algorithm 435 primal problem 435 reduction to normal form 429ff. restricted normal form 426ff. revised simplex method 435 right-hand variables 426 simplex method 402, 423ff., 431ff., 1216ff. slack variables 429 tableau 427 vertex of simplex 426
Index to Volumes 1 and 2
Linear recurrence see Recurrence relation Linear regression 655ff., 660ff., 1285ff. see also Fitting Linear regularization 799ff. LINPACK 26 Literal constant 937, 1361 Little-endian 293 Local extrapolation 709 Local extremum 387f., 437 Localization of roots see Bracketing Logarithmic function 255 Lomb periodogram method of spectral analysis 569f., 1258f. fast algorithm 574f., 1259 Loops 9f. Lorentzian probability distribution 282, 696f. Low-pass filter 551, 644f., 1283f. Lower subscript 944 lower triangle() utility function 989, 1007, 1200 LP coefficients see Linear prediction LPC (linear predictive coding) 563ff. LU decomposition 34ff., 47f., 51, 55, 64, 97, 374, 667, 732, 1016, 1022 for A−1 · B 40 backsubstitution 39, 1017 band diagonal matrix 43ff., 1020 complex equations 41f. Crout’s algorithm 36ff., 45, 1017 for integral equations 783, 786, 1325f. for inverse iteration of eigenvectors 488 for inverse problems 801 for matrix determinant 41 for matrix inverse 40, 1016 for nonlinear sets of equations 374, 386, 1196 operation count 36, 39 outer product Gaussian elimination 1017 for Pad´e approximant 195, 1080 pivoting 37f., 1017 repeated backsubstitution 40, 46 solution of linear algebraic equations 40, 1017 solution of normal equations 667 for Toeplitz matrix 87 Lucifer 290
M &R (Metcalf and Reid)
935 M-estimates 694ff. how to compute 697f. local 695ff. see also Maximum likelihood estimate Machine accuracy 19f., 881f., 1189, 1343 Macintosh, see Apple Macintosh Maehly’s procedure 364, 371 Magic in MEM image restoration 814 in Pad´e approximation 195 Mantissa in floating point format 19, 882, 909, 1343 Marginals 624 Marquardt method (least squares fitting) 678ff., 816, 1292f. Marsaglia shift register 1142, 1148ff. Marsaglia, G. 1142, 1149
955
mask 1006f., 1102, 1200, 1226, 1305, 1333f., 1368, 1378, 1382 optional argument 948 optional argument, facilitates parallelism 967f., 1038 Mass, center of 295ff. MasterCard checksum 894 Mathematical Center (Amsterdam) 353 Mathematical intrinsic functions 946, 951f. matmul() intrinsic function 945, 949, 969, 1026, 1040, 1050, 1076, 1200, 1216, 1290, 1326 Matrix 23ff. add vector to diagonal 1004, 1234, 1366, 1381 approximation of 58f., 598f. band diagonal 42ff., 64, 1019 band triangular 64 banded 26, 454 bidiagonal 52 block diagonal 64, 754 block triangular 64 block tridiagonal 64 bordered 64 characteristic polynomial 449, 469 Cholesky decomposition 89f., 423, 455, 668, 1038f. column augmented 28, 1014 complex 41 condition number 53, 78 create unit matrix 1006, 1382 curvature 677 cyclic banded 64 cyclic tridiagonal 67, 1030 defective 450, 476, 489 of derivatives see Hessian matrix; Jacobian determinant design (fitting) 645, 665, 801, 1082 determinant of 25, 41 diagonal of sparse matrix 1033ff. diagonalization 452ff., 1225ff. elementary row and column operations 28f. finite differencing of partial differential equations 821ff. get diagonal 985, 1005, 1226f., 1366, 1381f. Hermitian 450, 454, 475 Hermitian conjugate 450 Hessenberg 94, 453, 470, 476ff., 488, 1231ff. Hessian see Hessian matrix hierarchically band diagonal 598 Hilbert 83 identity 25 ill-conditioned 53, 56, 114 indexed storage of 71f., 1030 and integral equations 779, 783, 1325 inverse 25, 27, 34, 40, 65ff., 70, 95ff., 1014, 1016f. inverse, approximate 49 inverse by Hotelling’s method 49, 598 inverse by Schultz’s method 49, 598 inverse multiplied by a matrix 40 iteration for inverse 49, 598
956
Index to Volumes 1 and 2
Jacobi transformation 453, 456ff., 462, 1225f. Jacobian 731, 1309 logical dimension 24 lower triangular 34f., 89, 781, 1016 lower triangular mask 1007, 1200, 1382 multiplication denoted by dot 23 multiplication, intrinsic function 949, 969, 1026, 1040, 1050, 1200, 1326 norm 50 normal 450ff. nullity 53 nullspace 25, 53f., 449, 795 orthogonal 91, 450, 463ff., 587 orthogonal transformation 452, 463ff., 469, 1227 orthonormal basis 58, 94 outer product denoted by cross 66, 420 partitioning for determinant 70 partitioning for inverse 70 pattern multiply of sparse 74 physical dimension 24 positive definite 26, 89f., 668, 1038 QR decomposition 91f., 382, 386, 668, 1039, 1199 range 53 rank 53 residual 49 row and column indices 23 row vs. column operations 31f. self-adjoint 450 set diagonal elements 1005, 1200, 1366, 1382 similarity transform 452ff., 456, 476, 478, 482 singular 53f., 58, 449 singular value decomposition 26, 51ff., 797 sparse 23, 63ff., 71, 598, 732, 754, 804, 1030ff. special forms 26 splitting in relaxation method 856f. spread 808 square root of 423, 455 symmetric 26, 89, 450, 454, 462ff., 668, 785, 1038, 1225, 1227 threshold multiply of sparse 74, 1031 Toeplitz 82, 85ff., 195, 1038 transpose() intrinsic function 950 transpose of sparse 73f., 1033 triangular 453 tridiagonal 26, 42f., 64, 109, 150, 453f., 462ff., 469ff., 488, 839f., 853, 861f., 1018f., 1227ff. tridiagonal with fringes 822 unitary 450 updating 94, 382, 386, 1041, 1199 upper triangular 34f., 91, 1016 upper triangular mask 1006, 1226, 1305, 1382 Vandermonde 82ff., 114, 1037, 1047 see also Eigensystems Matrix equations see Linear algebraic equations Matterhorn 606
maxexponent() intrinsic function 1107 Maximization see Minimization Maximum entropy method (MEM) 565ff., 1258 algorithms for image restoration 815f. Bayesian 816f. Cornwell-Evans algorithm 816 demystified 814 historic vs. Bayesian 816f. image restoration 809ff. intrinsic correlation function (ICF) model 817 for inverse problems 809ff. operation count 567 see also Linear prediction Maximum likelihood estimate (M-estimates) 690, 694ff. and Bayes’ Theorem 811 chi-square test 690 defined 652 how to compute 697f. mean absolute deviation 696, 698, 1294 relation to least squares 652 maxloc() intrinsic function 949, 992f., 1015 modified in Fortran 95 961 maxval() intrinsic function 945, 948, 961, 1016, 1273 Maxwell’s equations 825f. Mean(s) of distribution 604f., 608f., 1269 statistical differences between two 609ff., 1269f. Mean absolute deviation of distribution 605, 696, 1294 related to median 698 Measurement errors 650 Median 320 calculating 333 of distribution 605, 608f. as L-estimate 694 role in robust straight line fitting 698 by selection 698, 1294 Median-of-three, in Quicksort 324 MEM see Maximum entropy method (MEM) Memory leak 953, 956, 1071, 1327 Memory management 938, 941f., 953ff., 1327, 1336 merge construct 945, 950, 1099f. for conditional scalar expression 1010, 1094f. contrasted with where 1023 parallelization 1011 Merge-with-dummy-values idiom 1090 Merit function 650 in general linear least squares 665 for inverse problems 797 nonlinear models 675 for straight line fitting 656, 698 for straight line fitting, errors in both coordinates 660, 1286 Mesh-drift instability 834f. Mesokurtic distribution 606 Metcalf, Michael 2/viii see also M&R Method of regularization 799ff.
Index to Volumes 1 and 2
Metropolis algorithm 437f., 1219 Microsoft 1/xxii, 2/xix Microsoft Fortran PowerStation 2/viii Midpoint method see Modified midpoint method; Semi-implicit midpoint rule Mikado, or Town of Titipu 714 Miller’s algorithm 175, 228, 1106 MIMD machines (Multiple Instruction Multiple Data) 964, 985, 1071, 1084 Minimal solution of recurrence relation 174 Minimax polynomial 186, 198, 1076 Minimax rational function 198 Minimization 387ff. along a ray 77, 376f., 389, 406ff., 412f., 415f., 418, 1195f., 1211, 1213 annealing, method of simulated 387f., 436ff., 1219ff. bracketing of minimum 390ff., 402, 1201f. Brent’s method 389, 395ff., 399, 660f., 1204ff., 1286 Broyden-Fletcher-Goldfarb-Shanno algorithm 390, 418ff., 1215 chi-square 653ff., 675ff., 1285, 1292 choice of methods 388f. combinatorial 436f., 1219 conjugate gradient method 390, 413ff., 804, 815, 1210, 1214 convergence rate 393, 409 Davidon-Fletcher-Powell algorithm 390, 418ff., 1215 degenerate 795 direction-set methods 389, 406ff., 1210ff. downhill simplex method 389, 402ff., 444, 697f., 1208, 1222ff. finding best-fit parameters 650 Fletcher-Reeves algorithm 390, 414ff., 1214 functional 795 global 387f., 443f., 650, 1219, 1222 globally convergent multidimensional 418, 1215 golden section search 390ff., 395, 1202ff. multidimensional 388f., 402ff., 1208ff., 1214 in nonlinear model fitting 675f., 1292 Polak-Ribiere algorithm 389, 414ff., 1214 Powell’s method 389, 402, 406ff., 1210ff. quasi-Newton methods 376, 390, 418ff., 1215 and root finding 375 scaling of variables 420 by searching smaller subspaces 815 steepest descent method 414, 804 termination criterion 392, 404 use in finding double roots 341 use for sparse linear systems 77ff. using derivatives 389f., 399ff., 1205ff. variable metric methods 390, 418ff., 1215 see also Linear programming Minimum residual method, for sparse system 78 minloc() intrinsic function 949, 992f. modified in Fortran 95 961 MINPACK 683 minval() intrinsic function 948, 961
957
MIPS 886 Missing data problem 569 Mississippi River 438f., 447 MMP (massively multiprocessor) machines 965ff., 974, 981, 984, 1016ff., 1021, 1045, 1226ff., 1250 Mode of distribution 605, 609 Modeling of data see Fitting Model-trust region 386, 683 Modes, homogeneous, of recursive filters 554 Modified Bessel functions see Bessel functions Modified Lentz’s method, for continued fractions 165 Modified midpoint method 716ff., 720, 1302f. Modified moments 152 Modula-2 7 Modular arithmetic, without overflow 269, 271, 275 Modular programming 2/xiii, 7f., 956ff., 1209, 1293, 1296, 1346 MODULE facility 2/xiii, 936f., 939f., 957, 1067, 1298, 1320, 1322, 1324, 1330, 1346 initializing random number generator 1144ff. in nr.f90 936, 941f., 1362, 1384ff. in nrtype.f90 936f., 1361f. in nrutil.f90 936, 1070, 1362, 1364ff. sparse matrix 1031 undefined variables on exit 953, 1266 Module subprogram 940 modulo() intrinsic function 946, 1156 Modulus of linear congruential generator 268 Moments of distribution 604ff., 1269 filter that preserves 645 modified problem of 151f. problem of 83 and quadrature formulas 791, 1328 semi-invariants 608 Monic polynomial 142f. Monotonicity constraint, in upwind differencing 837 Monte Carlo 155ff., 267 adaptive 306ff., 1161ff. bootstrap method 686f. comparison of sampling methods 309 exploration of binary tree 290 importance sampling 306f. integration 124, 155ff., 295ff., 306ff., 1161 integration, recursive 314ff., 1164ff. integration, using Sobol’ sequence 304 integration, VEGAS algorithm 309ff., 1161 and Kolmogorov-Smirnov statistic 622, 640 partial differential equations 824 quasi-random sequences in 299ff. quick and dirty 686f. recursive 306ff., 314ff., 1161, 1164ff. significance of Lomb periodogram 570 simulation of data 654, 684ff., 690 stratified sampling 308f., 314, 1164
958
Index to Volumes 1 and 2
Moon, calculate phases of 1f., 14f., 936, 1010f. Mother functions 584 Mother Nature 684, 686 Moving average (MA) model 566 Moving window averaging 644 Mozart 9 MS 1/xxii, 2/xix Muller’s method 364, 372 Multidimensional confidence levels of fitting 688f. data, use of binning 623 Fourier transform 515ff., 1241, 1246, 1251 Fourier transform, real data 519ff., 1248f. initial value problems 844ff. integrals 124, 155ff., 295ff., 306ff., 1065ff., 1161ff. interpolation 116ff., 1049ff. Kolmogorov-Smirnov test 640, 1281 least squares fitting 675 minimization 402ff., 406ff., 413ff., 1208ff., 1214f., 1222ff. Monte Carlo integration 295ff., 306ff., 1161ff. normal (Gaussian) distribution 690 optimization 388f. partial differential equations 844ff. root finding 340ff., 358, 370, 372ff., 746, 749f., 752, 754, 1194ff., 1314ff. search using quasi-random sequence 300 secant method 373, 382f., 1199f. wavelet transform 595, 1267f. Multigrid method 824, 862ff., 1334ff. avoid SOR 866 boundary conditions 868f. choice of operators 868 coarse-to-fine operator 864, 1337 coarse-grid correction 864f. cycle 865 dual viewpoint 875 fine-to-coarse operator 864, 1337 full approximation storage (FAS) algorithm 874, 1339ff. full multigrid method (FMG) 863, 868, 1334ff. full weighting 867 Gauss-Seidel relaxation 865f., 1338 half weighting 867, 1337 importance of adjoint operator 867 injection operator 864, 1337 interpolation operator 864, 1337 line relaxation 866 local truncation error 875 Newton’s rule 874, 876, 1339, 1341 nonlinear equations 874ff., 1339ff. nonlinear Gauss-Seidel relaxation 876, 1341 odd-even ordering 866, 869, 1338 operation count 862 prolongation operator 864, 1337 recursive nature 865, 1009, 1336 relative truncation error 875 relaxation as smoothing operator 865 restriction operator 864, 1337
speeding up FMG algorithm 873 stopping criterion 875f. straight injection 867 symbol of operator 866f. use of Richardson extrapolation 869 V-cycle 865, 1336 W-cycle 865, 1336 zebra relaxation 866 Multiple precision arithmetic 906ff., 1352ff. Multiple roots 341, 362 Multiplication, complex 171 Multiplication, multiple precision 907, 909, 1353f. Multiplier of linear congruential generator 268 Multistep and multivalue methods (ODEs) 740ff. see also Differential Equations; Predictorcorrector methods Multivariate normal distribution 690 Murphy’s Law 407 Musical scores 5f.
N AG
1/xxiii, 2/xx, 26, 64, 205, 454 Fortran 90 compiler 2/viii, 2/xiv Named constant 940 initialization 1012 for Numerical Recipes 1361 Named control structure 959, 1219, 1305 National Science Foundation (U.S.) 1/xvii, 1/xix, 2/ix Natural cubic spline 109, 1044f. Navier-Stokes equation 830f. nearest() intrinsic function 952, 1146 Needle, eye of (minimization) 403 Negation, multiple precision 907, 1353f. Negentropy 811, 896 Nelder-Mead minimization method 389, 402, 1208 Nested iteration 868 Neumann boundary conditions 820, 840, 851, 858 Neutrino 640 Neville’s algorithm 102f., 105, 134, 182, 1043 Newton-Cotes formulas 125ff., 140 Newton-Raphson method see Newton’s rule Newton’s rule 143f., 180, 341, 355ff., 362, 364, 469, 1059, 1189 with backtracking 376, 1196 caution on use of numerical derivatives 356ff. fractal domain of convergence 360f. globally convergent multidimensional 373, 376ff., 382, 749f., 752, 1196, 1199, 1314f. for matrix inverse 49, 598 in multidimensions 370, 372ff., 749f., 752, 754, 1194ff., 1314ff. in nonlinear multigrid 874, 876, 1339, 1341 nonlinear Volterra equations 787 for reciprocal of number 911, 1355 safe 359, 1190 scaling of variables 381
Index to Volumes 1 and 2
singular Jacobian 386 solving stiff ODEs 740 for square root of number 912, 1356 Niederreiter sequence 300 NL2SOL 683 Noise bursty 889 effect on maximum entropy method 567 equivalent bandwidth 548 fitting data which contains 647f., 650 model, for optimal filtering 541 Nominal variable (statistics) 623 Nonexpansive projection operator 805 Non-interfering directions see Conjugate directions Nonlinear eigenvalue problems 455 Nonlinear elliptic equations, multigrid method 874ff., 1339ff. Nonlinear equations, in MEM inverse problems 813 Nonlinear equations, roots of 340ff. Nonlinear instability 831 Nonlinear integral equations 781, 787 Nonlinear programming 436 Nonnegativity constraints 423 Nonparametric statistics 633ff., 1277ff. Nonpolynomial complete (NP-complete) 438 Norm, of matrix 50 Normal (Gaussian) distribution 267, 652, 682, 798, 1294 central limit theorem 652f. deviates from 279f., 571, 1152 kurtosis of 607 multivariate 690 semi-invariants of 608 tails compared to Poisson 653 two-dimensional (binormal) 631 variance of skewness of 606 Normal equations (fitting) 26, 645, 666ff., 795, 800, 1288 often are singular 670 Normalization of Bessel functions 175 of floating-point representation 19, 882, 1343 of functions 142, 765 of modified Bessel functions 232 not() intrinsic function 951 Notch filter 551, 555f. NP-complete problem 438 nr.f90 (module file) 936, 1362, 1384ff. nrerror() utility function 989, 995 nrtype.f90 (module file) 936f. named constants 1361 nrutil.f90 (module file) 936, 1070, 1362, 1364ff. table of contents 1364 Null hypothesis 603 nullify statement 953f., 1070, 1302 Nullity 53 Nullspace 25, 53f., 449, 795 Number-theoretic transforms 503f. Numeric inquiry functions 2/xiv, 1107, 1231, 1343 Numerical derivatives 180ff., 645, 1075
959
Numerical integration see Quadrature Numerical intrinsic functions 946, 951f. Numerical Recipes compatibility with First Edition 4 Example Book 3 Fortran 90 types 936f., 1361 how to get programs 1/xx, 2/xvii how to report bugs 1/iv, 2/iv interface blocks (Fortran 90) 937, 941f., 1084, 1384ff. no warranty on 1/xx, 2/xvii plan of two-volume edition 1/xiii table of dependencies 921ff., 1434ff. as trademark 1/xxiii, 2/xx utility functions (Fortran 90) 936f., 945, 968, 970, 972ff., 977, 984, 987ff., 1015, 1071f., 1361ff. Numerical Recipes Software 1/xv, 1/xxiiff., 2/xviiff. address and fax number 1/iv, 1/xxii, 2/iv, 2/xix Web home page 1/xx, 2/xvii Nyquist frequency 494ff., 520, 543, 545, 569ff. Nystrom method 782f., 789, 1325 product version 789, 1331
O bject extensibility 8 Objective function 424 Object-oriented programming 2/xvi, 2, 8 Oblateness parameter 764 Obsolete features see Fortran, Obsolescent features Octal constant, initialization 959 Odd-even ordering allows parallelization 1333 in Gauss-Seidel relaxation 866, 869, 1338 in successive over-relaxation (SOR) 859, 1332 Odd parity 888 OEM information 1/xxii One-sided power spectral density 492 ONLY option, for USE statement 941, 957, 1067 Operation count balancing 476 Bessel function evaluation 228 bisection method 346 Cholesky decomposition 90 coefficients of interpolating polynomial 114f. complex multiplication 97 cubic spline interpolation 109 evaluating polynomial 168 fast Fourier transform (FFT) 498 Gauss-Jordan elimination 34, 39 Gaussian elimination 34 Givens reduction 463 Householder reduction 467 interpolation 100 inverse iteration 488 iterative improvement 48 Jacobi transformation 460 Kendall’s tau 637
960
Index to Volumes 1 and 2
linear congruential generator 268 LU decomposition 36, 39 matrix inversion 97 matrix multiplication 96 maximum entropy method 567 multidimensional minimization 413f. multigrid method 862 multiplication 909 polynomial evaluation 97f., 168 QL method 470, 473 QR decomposition 92 QR method for Hessenberg matrices 484 reduction to Hessenberg form 479 selection by partitioning 333 sorting 320ff. Spearman rank-order coefficient 638 Toeplitz matrix 83 Vandermonde matrix 83 Operator overloading 2/xiif., 7 Operator splitting 823, 847f., 861 Operator, user-defined 2/xii Optimal feasible vector 424 Optimal (Wiener) filtering 535, 539ff., 558, 644 compared with regularization 801 Optimization see Minimization Optimization of code 2/xiii Optional argument 2/xiv, 947f., 1092, 1228, 1230, 1256, 1272, 1275, 1340 dim 948 mask 948, 968, 1038 testing for 952 Ordering Numerical Recipes 1/xxf., 2/xviif. Ordinal variable (statistics) 623 Ordinary differential equations see Differential equations Orthogonal see Orthonormal functions; Orthonormal polynomials Orthogonal transformation 452, 463ff., 469, 584, 1227 Orthonormal basis, constructing 58, 94, 1039 Orthonormal functions 142, 246 Orthonormal polynomials Chebyshev 144, 184ff., 1076ff. construct for arbitrary weight 151ff., 1064 in Gauss-Hermite integration 147, 1062 and Gaussian quadrature 142, 1009, 1061 Gaussian weights from recurrence 150, 1064 Hermite 144, 1062 Jacobi 144, 1063 Laguerre 144, 1060 Legendre 144, 1059 weight function log x 153 Orthonormality 51, 142, 463 Outer product Gaussian elimination 1017 Outer product of matrices (denoted by cross) 66, 420, 949, 969f., 989, 1000ff., 1017, 1026, 1040, 1076, 1200, 1216, 1275 outerand() utility function 989, 1002, 1015 outerdiff() utility function 989, 1001 outerdiv() utility function 989, 1001 outerprod() utility function 970, 989, 1000, 1017, 1026, 1040, 1076, 1200, 1216, 1275
outersum() utility function 989, 1001 Outgoing wave boundary conditions 820 Outlier 605, 653, 656, 694, 697 see also Robust estimation Overcorrection 857 Overflow 882, 1343 how to avoid in modulo multiplication 269 in complex arithmetic 171 Overlap-add and overlap-save methods 536f. Overloading operator 2/xiif. procedures 940, 1015, 1083, 1094, 1096 Overrelaxation parameter 857, 1332 choice of 858
P ack() intrinsic function
945, 950, 964, 991, 1031 communication bottleneck 969 for index table 1176 for partition-exchange 1170 for selection 1178 for selective evaluation 1087 Pack-unpack idiom 1087, 1134, 1153 Pad´e approximant 194ff., 1080f. Pad´e approximation 105 Parabolic interpolation 395, 1204 Parabolic partial differential equations 818, 838ff. Parallel axis theorem 308 Parallel programming 2/xv, 941, 958ff., 962ff., 965f., 968f., 987 array operations 964f. array ranking 1278f. band diagonal linear equations 1021 Bessel functions 1107ff. broadcasts 965ff. C and C++ 2/viii communication costs 969, 981, 1250 counting do-loops 1015 cyclic reduction 974 deflation 977ff. design matrix 1082 dimensional expansion 965ff. eigensystems 1226, 1229f. fast Fourier transform (FFT) 981, 1235ff., 1250 in Fortran 90 963ff. Fortran 90 tricks 1009, 1274, 1278, 1280 function evaluation 986, 1009, 1084f., 1087, 1090, 1102, 1128, 1134 Gaussian quadrature 1009, 1061 geometric progressions 972 index loss 967f., 1038 index table 1176f. interprocessor communication 981 Kendall’s tau 1280 linear algebra 969f., 1000ff., 1018f., 1026, 1040, 1200, 1326 linear recurrence 973f., 1073ff. logo 2/viii, 1009 masks 967f., 1006f., 1038, 1102, 1200, 1226, 1305, 1333f., 1368, 1378, 1382 merge statement 1010
Index to Volumes 1 and 2
MIMD (multiple instruction, multiple data) 964, 985f., 1084 MMP (massively multiprocessor) machines 965ff., 974, 984, 1016ff., 1226ff., 1250 nrutil.f90 (module file) 1364ff. odd-even ordering 1333 one-dimensional FFT 982f. parallel note icon 1009 partial differential equations 1333 in-place selection 1178f. polynomial coefficients from roots 980 polynomial evaluation 972f., 977, 998 random numbers 1009, 1141ff. recursive doubling 973f., 976f., 979, 988, 999, 1071ff. scatter-with-combine 984, 1002f., 1032f. second order recurrence 974f., 1074 SIMD (Single Instruction Multiple Data) 964, 985f., 1009, 1084f. singular value decomposition (SVD) 1026 sorting 1167ff., 1171, 1176f. special functions 1009 SSP (small-scale parallel) machines 965ff., 984, 1010ff., 1016ff., 1059f., 1226ff., 1250 subvector scaling 972, 974, 996, 1000 successive over-relaxation (SOR) 1333 supercomputers 2/viii, 962 SVD algorithm 1026 synthetic division 977ff., 999, 1048, 1071f., 1079, 1192 tridiagonal systems 975f., 1018, 1229f. utilities 1364ff. vector reduction 972f., 977, 998 vs. serial programming 965, 987 PARAMETER attribute 1012 Parameters in fitting function 651, 684ff. Parity bit 888 Park and Miller minimal standard random generator 269, 1142 Parkinson’s Law 328 Parseval’s Theorem 492, 544 discrete form 498 Partial differential equations 818ff., 1332ff. advective equation 826 alternating-direction implicit method (ADI) 847, 861f. amplification factor 828, 834 analyze/factorize/operate package 824 artificial viscosity 831, 837 biconjugate gradient method 824 boundary conditions 819ff. boundary value problems 819, 848 Cauchy problem 818f. caution on high-order methods 844f. Cayley’s form 844 characteristics 818 Chebyshev acceleration 859f., 1332 classification of 818f. comparison of rapid methods 854 conjugate gradient method 824 Courant condition 829, 832ff., 836 Courant condition (multidimensional) 846 Crank-Nicholson method 840, 842, 844, 846
961
cyclic reduction (CR) method 848f., 852ff. diffusion equation 818, 838ff., 846, 855 Dirichlet boundary conditions 508, 820, 840, 850, 856, 858 elliptic, defined 818 error, varieties of 831ff. explicit vs. implicit differencing 827 FACR method 854 finite difference method 821ff. finite element methods 824 flux-conservative initial value problems 825ff. forward Euler differencing 826f. Forward Time Centered Space (FTCS) 827ff., 839ff., 843, 855 Fourier analysis and cyclic reduction (FACR) 848ff., 854 Gauss-Seidel method (relaxation) 855, 864ff., 876, 1338, 1341 Godunov’s method 837 Helmholtz equation 852 hyperbolic 818, 825f. implicit differencing 840 incomplete Cholesky conjugate gradient method (ICCG) 824 inhomogeneous boundary conditions 850f. initial value problems 818f. initial value problems, recommendations on 838ff. Jacobi’s method (relaxation) 855ff., 864 Laplace’s equation 818 Lax method 828ff., 836, 845f. Lax method (multidimensional) 845f. matrix methods 824 mesh-drift instability 834f. Monte Carlo methods 824 multidimensional initial value problems 844ff. multigrid method 824, 862ff., 1009, 1334ff. Neumann boundary conditions 508, 820, 840, 851, 858 nonlinear diffusion equation 842 nonlinear instability 831 numerical dissipation or viscosity 830 operator splitting 823, 847f., 861 outgoing wave boundary conditions 820 parabolic 818, 838ff. parallel computing 1333 periodic boundary conditions 850, 858 piecewise parabolic method (PPM) 837 Poisson equation 818, 852 rapid (Fourier) methods 508ff., 824, 848ff. relaxation methods 823, 854ff., 1332f. Schr¨odinger equation 842ff. second-order accuracy 833ff., 840 shock 831, 837 sparse matrices from 64 spectral methods 825 spectral radius 856ff., 862 stability vs. accuracy 830 stability vs. efficiency 821 staggered grids 513, 852 staggered leapfrog method 833f. strongly implicit procedure 824
962
Index to Volumes 1 and 2
successive over-relaxation (SOR) 857ff., 862, 866, 1332f. time splitting 847f., 861 two-step Lax-Wendroff method 835ff. upwind differencing 832f., 837 variational methods 824 varieties of error 831ff. von Neumann stability analysis 827f., 830, 833f., 840 wave equation 818, 825f. see also Elliptic partial differential equations; Finite difference equations (FDEs) Partial pivoting 29 Partition-exchange 323, 333 and pack() intrinsic function 1170 Partitioned matrix, inverse of 70 Party tricks 95ff., 168 Parzen window 547 Pascal, Numerical Recipes in 2/x, 2/xvii, 1 Pass-the-buck idiom 1102, 1128 Path integration, for function evaluation 201ff., 263, 1138 Pattern multiply of sparse matrices 74 PBCG (preconditioned biconjugate gradient method) 78f., 824 PC methods see Predictor-corrector methods PCGPACK 71 PDEs see Partial differential equations Pearson’s r 630ff., 1276 PECE method 741 Pentagon, symmetries of 895 Percentile 320 Period of linear congruential generator 268 Periodic boundary conditions 850, 858 Periodogram 543ff., 566, 1258ff. Lomb’s normalized 569f., 574f., 1258ff. variance of 544f. Perl (programming language) 1/xvi Perron’s theorems, for convergence of recurrence relations 174f. Perturbation methods for matrix inversion 65ff. Phase error 831 Phase-locked loop 700 Phi statistic 625 Phillips-Twomey method 799ff. Pi, computation of 906ff., 1352ff., 1357f. Piecewise parabolic method (PPM) 837 Pincherle’s theorem 175 Pivot element 29, 33, 757 in linear programming 428f. Pivoting 27, 29ff., 46, 66, 90, 1014 full 29, 1014 implicit 30, 38, 1014, 1017 in LU decomposition 37f., 1017 partial 29, 33, 37f., 1017 and QR decomposition 92 in reduction to Hessenberg form 478 in relaxation method 757 as row and column operations 32 for tridiagonal systems 43 Pixel 519, 596, 803, 811 PL/1 2/x Planck’s constant 842
Plane rotation see Givens reduction; Jacobi transformation (or rotation) Platykurtic distribution 606 Plotting of functions 342, 1182f. POCS (projection onto convex sets) 805 Poetry 5f. Pointer (Fortran 90) 2/xiiif., 938f., 944f., 953ff., 1197, 1212, 1266 as alias 939, 944f., 1286, 1333 allocating an array 941 allocating storage for derived type 955 for array of arrays 956, 1336 array of, forbidden 956, 1337 associated with target 938f., 944f., 952f., 1197 in Fortran 95 961 to function, forbidden 1067, 1210 initialization to null 2/xv, 961 returning array of unknown size 955f., 1184, 1259, 1261, 1327 undefined status 952f., 961, 1070, 1266, 1302 Poisson equation 519, 818, 852 Poisson probability function cumulative 214 deviates from 281, 283ff., 571, 1154 semi-invariants of 608 tails compared to Gaussian 653 Poisson process 278, 282ff., 1153 Polak-Ribiere algorithm 390, 414ff., 1214 Poles see Complex plane, poles in Polishing of roots 356, 363ff., 370f., 1193 poly() utility function 973, 977, 989, 998, 1072, 1096, 1192, 1258, 1284 Polymorphism 8 Polynomial interpolation 99, 102ff., 1043 Aitken’s algorithm 102 in Bulirsch-Stoer method 724, 726, 1305 coefficients for 113ff., 1047f. Lagrange’s formula 84, 102f. multidimensional 116ff., 1049ff. Neville’s algorithm 102f., 105, 134, 182, 1043 pathology in determining coefficients for 116 in predictor-corrector method 740 smoothing filters 645 see also Interpolation Polynomials 167ff. algebraic manipulations 169, 1072 approximate roots of Hermite polynomials 1062 approximate roots of Jacobi polynomials 1064 approximate roots of Laguerre polynomials 1061 approximating modified Bessel functions 230 approximation from Chebyshev coefficients 191, 1078f. AUTODIN-II 890 CCITT 889f. characteristic 368, 1193 characteristic, for digital filters 554, 559, 1257
Index to Volumes 1 and 2
characteristic, for eigenvalues of matrix 449, 469 Chebyshev 184ff., 1076ff. coefficients from roots 980 CRC-16 890 cumulants of 977, 999, 1071f., 1192, 1365, 1378f. deflation 362ff., 370f., 977 derivatives of 167, 978, 1071 division 84, 169, 362, 370, 977, 1072 evaluation of 167, 972, 977, 998f., 1071, 1258, 1365, 1376ff. evaluation of derivatives 167, 978, 1071 extrapolation in Bulirsch-Stoer method 724, 726, 1305f. extrapolation in Romberg integration 134 fitting 83, 114, 191, 645, 665, 674, 1078f., 1291 generator for CRC 889 ill-conditioned 362 masked evaluation of 1378 matrix method for roots 368, 1193 minimax 186, 198, 1076 monic 142f. multiplication 169 operation count for 168 orthonormal 142, 184, 1009, 1061 parallel operations on 977ff., 998f., 1071f., 1192 primitive modulo 2 287ff., 301f., 889 roots of 178ff., 362ff., 368, 1191ff. shifting of 192f., 978, 1079 stopping criterion in root finding 366 poly term() utility function 974, 977, 989, 999, 1071f., 1192 Port, serial data 892 Portability 3, 963 Portable random number generator see Random number generator Positive definite matrix, testing for 90 Positivity constraints 423 Postal Service (U.S.), barcode 894 PostScript 1/xvi, 1/xxiii, 2/xx Powell’s method 389, 402, 406ff., 1210ff. Power (in a signal) 492f. Power series 159ff., 167, 195 economization of 192f., 1061, 1080 Pad´e approximant of 194ff., 1080f. Power spectral density see Fourier transform; Spectral density Power spectrum estimation see Fourier transform; Spectral density PowerStation, Microsoft Fortran 2/xix PPM (piecewise parabolic method) 837 Precision converting to double 1362 floating point 882, 937, 1343, 1361ff. multiple 906ff., 1352ff., 1362 Preconditioned biconjugate gradient method (PBCG) 78f. Preconditioning, in conjugate gradient methods 824 Predictor-corrector methods 702, 730, 740ff. Adams-Bashforth-Moulton schemes 741 adaptive order methods 744
963
compared to other methods 740 fallacy of multiple correction 741 with fixed number of iterations 741 functional iteration vs. Newton’s rule 742 multivalue compared with multistep 742ff. starting and stopping 742, 744 stepsize control 742f. present() intrinsic function 952 Prime numbers 915 Primitive polynomials modulo 2 287ff., 301f., 889 Principal directions 408f., 1210 Principal solution, of inverse problem 797 PRIVATE attribute 957, 1067 Prize, $1000 offered 272, 1141, 1150f. Probability see Random number generator; Statistical tests Probability density, change of variables in 278f. Procedure see Program(s); Subprogram Process loss 548 product() intrinsic function 948 Product Nystrom method 789, 1331 Program(s) as black boxes 1/xviii, 6, 26, 52, 205, 341, 406 dependencies 921ff., 1434ff. encapsulation 7 interfaces 2, 8 modularization 7f. organization 5ff. type declarations 2 typography of 2f., 12, 937 validation 3f. Programming, serial vs. parallel 965, 987 Projection onto convex sets (POCS) 805 Projection operator, nonexpansive 805 Prolongation operator 864, 1337 Protocol, for communications 888 PSD (power spectral density) see Fourier transform; Spectral density Pseudo-random numbers 266ff., 1141ff. PUBLIC attribute 957, 1067 Puns, particularly bad 167, 744, 747 PURE attribute 2/xv, 960f., 964, 986 put diag() utility function 985, 990, 1005, 1200 Pyramidal algorithm 586, 1264 Pythagoreans 392
Q L see Eigensystems QR see Eigensystems QR decomposition 91f., 382, 386, 1039f., 1199 backsubstitution 92, 1040 and least squares 668 operation count 92 pivoting 92 updating 94, 382, 386, 1041, 1199 use for orthonormal basis 58, 94 Quadratic convergence 49, 256, 351, 356, 409f., 419, 906 equations 20, 178, 391, 457
964
Index to Volumes 1 and 2
interpolation 353, 364 programming 436 Quadrature 123ff., 1052ff. adaptive 123, 190, 788 alternative extended Simpson’s rule 128 arbitrary weight function 151ff., 789, 1064, 1328 automatic 154 Bode’s rule 126 change of variable in 137ff., 788, 1056ff. by Chebyshev fitting 124, 189, 1078 classical formulas for 124ff. Clenshaw-Curtis 124, 190, 512f. closed formulas 125, 127f. and computer science 881 by cubic splines 124 error estimate in solution 784 extended midpoint rule 129f., 135, 1054f. extended rules 127ff., 134f., 786, 788ff., 1326, 1328 extended Simpson’s rule 128 Fourier integrals 577ff., 1261ff. Fourier integrals, infinite range 583 Gauss-Chebyshev 144, 512f. Gauss-Hermite 144, 789, 1062 Gauss-Jacobi 144, 1063 Gauss-Kronrod 154 Gauss-Laguerre 144, 789, 1060 Gauss-Legendre 144, 783, 789, 1059, 1325 Gauss-Lobatto 154, 190, 512 Gauss-Radau 154 Gaussian integration 127, 140ff., 781, 783, 788f., 1009, 1059ff., 1325, 1328f. Gaussian integration, nonclassical weight function 151ff., 788f., 1064f., 1328f. for improper integrals 135ff., 789, 1055, 1328 for integral equations 781f., 786, 1325ff. Monte Carlo 124, 155ff., 295ff., 306ff., 1161ff. multidimensional 124, 155ff., 1052, 1065ff. multidimensional, by recursion 1052, 1065 Newton-Cotes formulas 125ff., 140 open formulas 125ff., 129f., 135 related to differential equations 123 related to predictor-corrector methods 740 Romberg integration 124, 134f., 137, 182, 717, 788, 1054f., 1065, 1067 semi-open formulas 130 Simpson’s rule 126, 133, 136f., 583, 782, 788ff., 1053 Simpson’s three-eighths rule 126, 789f. singularity removal 137ff., 788, 1057ff., 1328ff. singularity removal, worked example 792, 1328ff. trapezoidal rule 125, 127, 130ff., 134f., 579, 583, 782, 786, 1052ff., 1326f. using FFTs 124 weight function log x 153 see also Integration of functions Quadrature mirror filter 585, 593
Quantum mechanics, Uncertainty Principle 600 Quartile value 320 Quasi-Newton methods for minimization 390, 418ff., 1215 Quasi-random sequence 299ff., 318, 881, 888 Halton’s 300 for Monte Carlo integration 304, 309, 318 Sobol’s 300ff., 1160 see also Random number generator Quicksort 320, 323ff., 330, 333, 1169f. Quotient-difference algorithm 164
R -estimates
694 Radioactive decay 278 Radix base for floating point arithmetic 476, 882, 907, 913, 1231, 1343, 1357 Radix conversion 902, 906, 913, 1357 radix() intrinsic function 1231 Radix sort 1172 Ramanujan’s identity for π 915 Random bits, generation of 287ff., 1159f. Random deviates 266ff., 1141ff. binomial 285f., 1155 exponential 278, 1151f. gamma distribution 282f., 1153 Gaussian 267, 279f., 571, 798, 1152f. normal 267, 279f., 571, 1152f. Poisson 283ff., 571, 1154f. quasi-random sequences 299ff., 881, 888, 1160f. uniform 267ff., 1158f., 1166 uniform integer 270, 274ff. Random number generator 266ff., 1141ff. bitwise operations 287 Box-Muller algorithm 279, 1152 Data Encryption Standard 290ff., 1144, 1156ff. good choices for modulus, multiplier and increment 274ff. initializing 1144ff. for integer-valued probability distribution 283f., 1154 integer vs. real implementation 273 L’Ecuyer’s long period 271f. lagged Fibonacci generator 1142, 1148ff. linear congruential generator 267ff., 1142 machine language 269 Marsaglia shift register 1142, 1148ff. Minimal Standard, Park and Miller’s 269, 1142 nonrandomness of low-order bits 268f. parallel 1009 perfect 272, 1141, 1150f. planes, numbers lie on 268 portable 269ff., 1142 primitive polynomials modulo 2 287ff. pseudo-DES 291, 1144, 1156ff. quasi-random sequences 299ff., 881, 888, 1160f. quick and dirty 274 quicker and dirtier 275 in Quicksort 324 random access to nth number 293
Index to Volumes 1 and 2
random bits 287ff., 1159f. recommendations 276f. rejection method 281ff. serial 1141f. shuffling procedure 270, 272 in simulated annealing method 438 spectral test 274 state space 1143f. state space exhaustion 1141 subtractive method 273, 1143 system-supplied 267f. timings 276f., 1151 transformation method 277ff. trick for trigonometric functions 280 Random numbers see Monte Carlo; Random deviates Random walk 20 random number() intrinsic function 1141, 1143 random seed() intrinsic function 1141 RANDU, infamous routine 268 Range 53f. Rank (matrix) 53 kernel of finite 785 Rank (sorting) 320, 332, 1176 Rank (statistics) 633ff., 694f., 1277 Kendall’s tau 637ff., 1279 Spearman correlation coefficient 634f., 1277ff. sum squared differences of 634, 1277 Ratio variable (statistics) 623 Rational Chebyshev approximation 197ff., 1081f. Rational function 99, 167ff., 194ff., 1080f. approximation for Bessel functions 225 approximation for continued fraction 164, 211, 219f. Chebyshev approximation 197ff., 1081f. evaluation of 170, 1072f. extrapolation in Bulirsch-Stoer method 718ff., 726, 1306f. interpolation and extrapolation using 99, 104ff., 194ff., 718ff., 726 as power spectrum estimate 566 interpolation and extrapolation using 1043f., 1080ff., 1306 minimax 198 Re-entrant procedure 1052 real() intrinsic function, ambiguity of 947 Realizable (causal) 552, 554f. reallocate() utility function 955, 990, 992, 1070, 1302 Rearranging see Sorting Reciprocal, multiple precision 910f., 1355f. Record, in data file 329 Recurrence relation 172ff., 971ff. arithmetic progression 971f., 996 associated Legendre polynomials 247 Bessel function 172, 224, 227f., 234 binomial coefficients 209 Bulirsch-Stoer 105f. characteristic polynomial of tridiagonal matrix 469 Clenshaw’s recurrence formula 176f. and continued fraction 175
965
continued fraction evaluation 164f. convergence 175 cosine function 172, 500 cyclic reduction 974 dominant solution 174 exponential integrals 172 gamma function 206 generation of random bits 287f. geometric progression 972, 996 Golden Mean 21 Legendre polynomials 172 minimal vs. dominant solution 174 modified Bessel function 232 Neville’s 103, 182 orthonormal polynomials 142 Perron’s theorems 174f. Pincherle’s theorem 175 for polynomial cumulants 977, 999, 1071f. polynomial interpolation 103, 183 primitive polynomials modulo 2 287f. random number generator 268 rational function interpolation 105f., 1043 recursive doubling 973, 977, 988, 999, 1071f., 1073 second order 974f., 1074 sequence of trig functions 173 sine function 172, 500 spherical harmonics 247 stability of 21, 173ff., 177, 224f., 227f., 232, 247, 975 trig functions 572 weight of Gaussian quadrature 144f. Recursion in Fortran 90 958 in multigrid method 865, 1009, 1336 Recursive doubling 973f., 979 cumulants of polynomial 977, 999, 1071f. linear recurrences 973, 988, 1073 tridiagonal systems 976 RECURSIVE keyword 958, 1065, 1067 Recursive Monte Carlo integration 306ff., 1161 Recursive procedure 2/xiv, 958, 1065, 1067, 1166 as parallelization tool 958 base case 958 for multigrid method 1009, 1336 re-entrant 1052 Recursive stratified sampling 314ff., 1164ff. Red-black see Odd-even ordering Reduction functions 948ff. Reduction of variance in Monte Carlo integration 299, 306ff. References (explanation) 4f. References (general bibliography) 916ff., 1359f. Reflection formula for gamma function 206 Regula falsi (false position) 347ff., 1185f. Regularity condition 775 Regularization compared with optimal filtering 801 constrained linear inversion method 799ff. of inverse problems 796ff. linear 799ff. nonlinear 813
966
Index to Volumes 1 and 2
objective criterion 802 Phillips-Twomey method 799ff. Tikhonov-Miller 799ff. trade-off curve 799 two-dimensional 803 zeroth order 797 see also Inverse problems Regularizing operator 798 Reid, John 2/xiv, 2/xvi Rejection method for random number generator 281ff. Relaxation method for algebraically difficult sets 763 automated allocation of mesh points 774f., 777 computation of spheroidal harmonics 764ff., 1319ff. for differential equations 746f., 753ff., 1316ff. elliptic partial differential equations 823, 854ff., 1332f. example 764ff., 1319ff. Gauss-Seidel method 855, 864ff., 876, 1338, 1341 internal boundary conditions 775ff. internal singular points 775ff. Jacobi’s method 855f., 864 successive over-relaxation (SOR) 857ff., 862, 866, 1332f. see also Multigrid method Remes algorithms exchange algorithm 553 for minimax rational function 199 reshape() intrinsic function 950 communication bottleneck 969 order keyword 1050, 1246 Residual 49, 54, 78 in multigrid method 863, 1338 Resolution function, in Backus-Gilbert method 807 Response function 531 Restriction operator 864, 1337 RESULT keyword 958, 1073 Reward, $1000 offered 272, 1141, 1150f. Richardson’s deferred approach to the limit 134, 137, 182, 702, 718ff., 726, 788, 869 see also Bulirsch-Stoer method Richtmyer artificial viscosity 837 Ridders’ method, for numerical derivatives 182, 1075 Ridders’ method, root finding 341, 349, 351, 1187 Riemann shock problem 837 Right eigenvalues and eigenvectors 451 Rise/fall time 548f. Robust estimation 653, 694ff., 700, 1294 Andrew’s sine 697 average deviation 605 double exponential errors 696 Kalman filtering 700 Lorentzian errors 696f. mean absolute deviation 605 nonparametric correlation 633ff., 1277 Tukey’s biweight 697
use of a priori covariances 700 see also Statistical tests Romberg integration 124, 134f., 137, 182, 717, 788, 1054f., 1065 Root finding 143, 340ff., 1009, 1059 advanced implementations of Newton’s rule 386 Bairstow’s method 364, 370, 1193 bisection 343, 346f., 352f., 359, 390, 469, 698, 1184f. bracketing of roots 341, 343ff., 353f., 362, 364, 369, 1183f. Brent’s method 341, 349, 660f., 1188f., 1286 Broyden’s method 373, 382f., 386, 1199 compared with multidimensional minimization 375 complex analytic functions 364 in complex plane 204 convergence criteria 347, 374 deflation of polynomials 362ff., 370f., 1192 without derivatives 354 double root 341 eigenvalue methods 368, 1193 false position 347ff., 1185f. Jenkins-Traub method 369 Laguerre’s method 341, 366f., 1191f. Lehmer-Schur algorithm 369 Maehly’s procedure 364, 371 matrix method 368, 1193 Muller’s method 364, 372 multiple roots 341 Newton’s rule 143f., 180, 341, 355ff., 362, 364, 370, 372ff., 376, 469, 740, 749f., 754, 787, 874, 876, 911f., 1059, 1189, 1194, 1196, 1314ff., 1339, 1341, 1355f. pathological cases 343, 356, 362, 372 polynomials 341, 362ff., 449, 1191f. in relaxation method 754, 1316 Ridders’ method 341, 349, 351, 1187 root-polishing 356, 363ff., 369ff., 1193 safe Newton’s rule 359, 1190 secant method 347ff., 358, 364, 399, 1186f. in shooting method 746, 749f., 1314f. singular Jacobian in Newton’s rule 386 stopping criterion for polynomials 366 use of minimum finding 341 using derivatives 355ff., 1189 zero suppression 372 see also Roots Root polishing 356, 363ff., 369ff., 1193 Roots Chebyshev polynomials 184 complex nth root of unity 999f., 1379 cubic equations 179f. Hermite polynomials, approximate 1062 Jacobi polynomials, approximate 1064 Laguerre polynomials, approximate 1061 multiple 341, 364ff., 1192 nonlinear equations 340ff. polynomials 341, 362ff., 449, 1191f. quadratic equations 178
Index to Volumes 1 and 2
reflection in unit circle 560, 1257 square, multiple precision 912, 1356 see also Root finding Rosenbrock method 730, 1308 compared with semi-implicit extrapolation 739 stepsize control 731, 1308f. Roundoff error 20, 881, 1362 bracketing a minimum 399 compile time vs. run time 1012 conjugate gradient method 824 eigensystems 458, 467, 470, 473, 476, 479, 483 extended trapezoidal rule 132 general linear least squares 668, 672 graceful 883, 1343 hardware aspects 882, 1343 Householder reduction 466 IEEE standard 882f., 1343 interpolation 100 least squares fitting 658, 668 Levenberg-Marquardt method 679 linear algebraic equations 23, 27, 29, 47, 56, 84, 1022 linear predictive coding (LPC) 564 magnification of 20, 47, 1022 maximum entropy method (MEM) 567 measuring 881f., 1343 multidimensional minimization 418, 422 multiple roots 362 numerical derivatives 180f. recurrence relations 173 reduction to Hessenberg form 479 series 164f. straight line fitting 658 variance 607 Row degeneracy 22 Row-indexed sparse storage 71f., 1030 transpose 73f. Row operations on matrix 28, 31f. Row totals 624 RSS algorithm 314ff., 1164 RST properties (reflexive, symmetric, transitive) 338 Runge-Kutta method 702, 704ff., 731, 740, 1297ff., 1308 Cash-Karp parameters 710, 1299f. embedded 709f., 731, 1298, 1308 high-order 705 quality control 722 stepsize control 708ff. Run-length encoding 901 Runge-Kutta method high-order 1297 stepsize control 1298f. Rybicki, G.B. 84ff., 114, 145, 252, 522, 574, 600
S -box for Data Encryption Standard
1148 Sampling importance 306f. Latin square or hypercube 305f. recursive stratified 314ff., 1164 stratified 308f. uneven or irregular 569, 648f., 1258
967
Sampling theorem 495, 543 for numerical approximation 600ff. Sande-Tukey FFT algorithm 503 SAVE attribute 953f., 958f., 961, 1052, 1070, 1266, 1293 redundant use of 958f. SAVE statements 3 Savitzky-Golay filters for data smoothing 644ff., 1283f. for numerical derivatives 183, 645 scale() intrinsic function 1107 Scallop loss 548 Scatter-with-combine functions 984, 1002f., 1032, 1366, 1380f. scatter add() utility function 984, 990, 1002, 1032 scatter max() utility function 984, 990, 1003 Schonfelder, Lawrie 2/xi Schrage’s algorithm 269 Schr¨odinger equation 842ff. Schultz’s method for matrix inverse 49, 598 Scope 956ff., 1209, 1293, 1296 Scoping unit 939 SDLC checksum 890 Searching with correlated values 111, 1046f. an ordered table 110f., 1045f. selection 333, 1177f. Secant method 341, 347ff., 358, 364, 399, 1186f. Broyden’s method 382f., 1199f. multidimensional (Broyden’s) 373, 382f., 1199 Second Euler-Maclaurin summation formula 135f. Second order differential equations 726, 1307 Seed of random number generator 267, 1146f. select case statement 2/xiv, 1010, 1036 Selection 320, 333, 1177f. find m largest elements 336, 1179f. heap algorithm 336, 1179 for median 698, 1294 operation count 333 by packing 1178 parallel algorithms 1178 by partition-exchange 333, 1177f. without rearrangement 335, 1178f. timings 336 use to find median 609 Semi-implicit Euler method 730, 735f. Semi-implicit extrapolation method 730, 735f., 1310f. compared with Rosenbrock method 739 stepsize control 737, 1311f. Semi-implicit midpoint rule 735f., 1310f. Semi-invariants of a distribution 608 Sentinel, in Quicksort 324, 333 Separable kernel 785 Separation of variables 246 Serial computing convergence of quadrature 1060 random numbers 1141 sorting 1167 Serial data port 892
968
Index to Volumes 1 and 2
Series 159ff. accelerating convergence of 159ff. alternating 160f., 1070 asymptotic 161 Bessel function Kν 241 Bessel function Yν 235 Bessel functions 160, 223 cosine integral 250 divergent 161 economization 192f., 195, 1080 Euler’s transformation 160f., 1070 exponential integral 216, 218 Fresnel integral 248 hypergeometric 202, 263, 1138 incomplete beta function 219 incomplete gamma function 210, 1090f. Laurent 566 relation to continued fractions 163f. roundoff error in 164f. sine and cosine integrals 250 sine function 160 Taylor 355f., 408, 702, 709, 754, 759 transformation of 160ff., 1070 van Wijngaarden’s algorithm 161, 1070 Shaft encoder 886 Shakespeare 9 Shampine’s Rosenbrock parameters 732, 1308 shape() intrinsic function 938, 949 Shell algorithm (Shell’s sort) 321ff., 1168 Sherman-Morrison formula 65ff., 83, 382 Shifting of eigenvalues 449, 470f., 480 Shock wave 831, 837 Shooting method computation of spheroidal harmonics 772, 1321ff. for differential equations 746, 749ff., 770ff., 1314ff., 1321ff. for difficult cases 753, 1315f. example 770ff., 1321ff. interior fitting point 752, 1315f., 1323ff. Shuffling to improve random number generator 270, 272 Side effects prevented by data hiding 957, 1209, 1293, 1296 and PURE subprograms 960 Sidelobe fall-off 548 Sidelobe level 548 sign() intrinsic function, modified in Fortran 95 961 Signal, bandwidth limited 495 Significance (numerical) 19 Significance (statistical) 609f. one- vs. two-sided 632 peak in Lomb periodogram 570 of 2-d K-S test 640, 1281 two-tailed 613 SIMD machines (Single Instruction Multiple Data) 964, 985f., 1009, 1084f. Similarity transform 452ff., 456, 476, 478, 482 Simplex defined 402 method in linear programming 389, 402, 423ff., 431ff., 1216ff.
method of Nelder and Mead 389, 402ff., 444, 697f., 1208f., 1222ff. use in simulated annealing 444, 1222ff. Simpson’s rule 124ff., 128, 133, 136f., 583, 782, 788f., 1053f. Simpson’s three-eighths rule 126, 789f. Simulated annealing see Annealing, method of simulated Simulation see Monte Carlo Sine function evaluated from tan(θ/2) 173 recurrence 172 series 160 Sine integral 248, 250ff., 1123, 1125f. continued fraction 250 series 250 see also Cosine integral Sine transform see Fast Fourier transform (FFT); Fourier transform Singleton’s algorithm for FFT 525 Singular value decomposition (SVD) 23, 25, 51ff., 1022 approximation of matrices 58f. backsubstitution 56, 1022f. and bases for nullspace and range 53 confidence levels from 693f. covariance matrix 693f. fewer equations than unknowns 57 for inverse problems 797 and least squares 54ff., 199f., 668, 670ff., 1081, 1290f. in minimization 410 more equations than unknowns 57f. parallel algorithms 1026 and rational Chebyshev approximation 199f., 1081f. of square matrix 53ff., 1023 use for ill-conditioned matrices 56, 58, 449 use for orthonormal basis 58, 94 Singularities of hypergeometric function 203, 263 in integral equations 788ff., 1328 in integral equations, worked example 792, 1328ff. in integrands 135ff., 788, 1055, 1328ff. removal in numerical integration 137ff., 788, 1057ff., 1328ff. Singularity, subtraction of the 789 SIPSOL 824 Six-step framework, for FFT 983, 1240 size() intrinsic function 938, 942, 945, 948 Skew array section 2/xii, 945, 960, 985, 1284 Skewness of distribution 606, 608, 1269 Smoothing of data 114, 644ff., 1283f. of data in integral equations 781 importance in multigrid method 865 sn function 261, 1137f. Snyder, N.L. 1/xvi Sobol’s quasi-random sequence 300ff., 1160f. Sonata 9 Sonnet 9 Sorting 320ff., 1167ff. bubble sort 1168
Index to Volumes 1 and 2
bubble sort cautioned against 321 compared to selection 333 covariance matrix 669, 681, 1289 eigenvectors 461f., 1227 Heapsort 320, 327f., 336, 1171f., 1179 index table 320, 329f., 1170, 1173ff., 1176 operation count 320ff. by packing 1171 parallel algorithms 1168, 1171f., 1176 Quicksort 320, 323ff., 330, 333, 1169f. radix sort 1172 rank table 320, 332, 1176 ranking 329, 1176 by reshaping array slices 1168 Shell’s method 321ff., 1168 straight insertion 321f., 461f., 1167, 1227 SP, defined 937 SPARC or SPARCstation 1/xxii, 2/xix, 4 Sparse linear equations 23, 63ff., 732, 1030 band diagonal 43, 1019ff. biconjugate gradient method 77, 599, 1034 data type for 1030 indexed storage 71f., 1030 in inverse problems 804 minimum residual method 78 named patterns 64, 822 partial differential equations 822ff. relaxation method for boundary value problems 754, 1316 row-indexed storage 71f., 1030 wavelet transform 584, 598 see also Matrix Spearman rank-order coefficient 634f., 694f., 1277 Special functions see Function Spectral analysis see Fourier transform; Periodogram Spectral density 541 and data windowing 545ff. figures of merit for data windows 548f. normalization conventions 542f. one-sided PSD 492 periodogram 543ff., 566, 1258ff. power spectral density (PSD) 492f. power spectral density per unit time 493 power spectrum estimation by FFT 542ff., 1254ff. power spectrum estimation by MEM 565ff., 1258 two-sided PSD 493 variance reduction in spectral estimation 545 Spectral lines, how to smooth 644 Spectral methods for partial differential equations 825 Spectral radius 856ff., 862 Spectral test for random number generator 274 Spectrum see Fourier transform Spherical Bessel functions 234 routine for 245, 1121 Spherical harmonics 246ff. orthogonality 246
969
routine for 247f., 1122 stable recurrence for 247 table of 246 see also Associated Legendre polynomials Spheroidal harmonics 764ff., 770ff., 1319ff. boundary conditions 765 normalization 765 routine for 768ff., 1319ff., 1323ff. Spline 100 cubic 107ff., 1044f. gives tridiagonal system 109 natural 109, 1044f. operation count 109 two-dimensional (bicubic) 120f., 1050f. spread() intrinsic function 945, 950, 969, 1000, 1094, 1290f. and dimensional expansion 966ff. Spread matrix 808 Spread spectrum 290 Square root, complex 172 Square root, multiple precision 912, 1356f. Square window 546, 1254ff. SSP (small-scale parallel) machines 965ff., 972, 974, 984, 1011, 1016ff., 1021, 1059f., 1226ff., 1250 Stability 20f. of Clenshaw’s recurrence 177 Courant condition 829, 832ff., 836, 846 diffusion equation 840 of Gauss-Jordan elimination 27, 29 of implicit differencing 729, 840 mesh-drift in PDEs 834f. nonlinear 831, 837 partial differential equations 820, 827f. of polynomial deflation 363 in quadrature solution of Volterra equation 787f. of recurrence relations 173ff., 177, 224f., 227f., 232, 247 and stiff differential equations 728f. von Neumann analysis for PDEs 827f., 830, 833f., 840 see also Accuracy Stabilized Kolmogorov-Smirnov test 621 Stabilizing functional 798 Staggered leapfrog method 833f. Standard (probable) errors 1288, 1290 Standard deviation of a distribution 605, 1269 of Fisher’s z 632 of linear correlation coefficient 630 of sum squared difference of ranks 635, 1277 Standard (probable) errors 610, 656, 661, 667, 671, 684 Stars, as text separator 1009 Statement function, superseded by internal subprogram 1057, 1256 Statement labels 9 Statistical error 653 Statistical tests 603ff., 1269ff. Anderson-Darling 621 average deviation 605, 1269 bootstrap method 686f. chi-square 614f., 623ff., 1272, 1275f.
970
Index to Volumes 1 and 2
contingency coefficient C 625, 1275 contingency tables 622ff., 638, 1275f. correlation 603f. Cramer’s V 625, 1275 difference of distributions 614ff., 1272 difference of means 609ff., 1269f. difference of variances 611, 613, 1271 entropy measures of association 626ff., 1275f. F-test 611, 613, 1271 Fisher’s z-transformation 631f., 1276 general paradigm 603 Kendall’s tau 634, 637ff., 1279 Kolmogorov-Smirnov 614, 617ff., 640, 694, 1273f., 1281 Kuiper’s statistic 621 kurtosis 606, 608, 1269 L-estimates 694 linear correlation coefficient 630ff., 1276 M-estimates 694ff. mean 603ff., 608ff., 1269f. measures of association 604, 622ff., 1275 measures of central tendency 604ff., 1269 median 605, 694 mode 605 moments 604ff., 608, 1269 nonparametric correlation 633ff., 1277 Pearson’s r 630ff., 1276 for periodic signal 570 phi statistic 625 R-estimates 694 rank correlation 633ff., 1277 robust 605, 634, 694ff. semi-invariants 608 for shift vs. for spread 620f. significance 609f., 1269ff. significance, one- vs. two-sided 613, 632 skewness 606, 608, 1269 Spearman rank-order coefficient 634f., 694f., 1277 standard deviation 605, 1269 strength vs. significance 609f., 622 Student’s t 610, 631, 1269 Student’s t, for correlation 631 Student’s t, paired samples 612, 1271 Student’s t, Spearman rank-order coefficient 634, 1277 Student’s t, unequal variances 611, 1270 sum squared difference of ranks 635, 1277 Tukey’s trimean 694 two-dimensional 640, 1281ff. variance 603ff., 607f., 612f., 1269ff. Wilcoxon 694 see also Error; Robust estimation Steak, without sizzle 809 Steed’s method Bessel functions 234, 239 continued fractions 164f. Steepest descent method 414 in inverse problems 804 Step doubling 130, 708f., 1052 tripling 136, 1055 Stieltjes, procedure of 151
Stiff equations 703, 727ff., 1308ff. Kaps-Rentrop method 730, 1308 methods compared 739 predictor-corrector method 730 r.h.s. independent of x 729f. Rosenbrock method 730, 1308 scaling of variables 730 semi-implicit extrapolation method 730, 1310f. semi-implicit midpoint rule 735f., 1310f. Stiff functions 100, 399 Stirling’s approximation 206, 812 Stoermer’s rule 726, 1307 Stopping criterion, in multigrid method 875f. Stopping criterion, in polynomial root finding 366 Storage band diagonal matrix 44, 1019 sparse matrices 71f., 1030 Storage association 2/xiv Straight injection 867 Straight insertion 321f., 461f., 1167, 1227 Straight line fitting 655ff., 667f., 1285ff. errors in both coordinates 660ff., 1286ff. robust estimation 698, 1294ff. Strassen’s fast matrix algorithms 96f. Stratified sampling, Monte Carlo 308f., 314 Stride (of an array) 944 communication bottleneck 969 Strongly implicit procedure (SIPSOL) 824 Structure constructor 2/xii Structured programming 5ff. Student’s probability distribution 221f. Student’s t-test for correlation 631 for difference of means 610, 1269 for difference of means (paired samples) 612, 1271 for difference of means (unequal variances) 611, 1270 for difference of ranks 635, 1277 Spearman rank-order coefficient 634, 1277 Sturmian sequence 469 Sub-random sequences see Quasi-random sequence Subprogram 938 for data hiding 957, 1209, 1293, 1296 internal 954, 957, 1057, 1067, 1226, 1256 in module 940 undefined variables on exit 952f., 961, 1070, 1266, 1293, 1302 Subscript triplet (for array) 944 Subtraction, multiple precision 907, 1353 Subtractive method for random number generator 273, 1143 Subvector scaling 972, 974, 996, 1000 Successive over-relaxation (SOR) 857ff., 862, 1332f. bad in multigrid method 866 Chebyshev acceleration 859f., 1332f. choice of overrelaxation parameter 858 with logical mask 1333f. parallelization 1333 sum() intrinsic function 945, 948, 966
Index to Volumes 1 and 2
Sum squared difference of ranks 634, 1277 Sums see Series Sun 1/xxii, 2/xix, 886 SPARCstation 1/xxii, 2/xix, 4 Supernova 1987A 640 SVD see Singular value decomposition (SVD) swap() utility function 987, 990f., 1015, 1210 Symbol, of operator 866f. Synthetic division 84, 167, 362, 370 parallel algorithms 977ff., 999, 1048, 1071f., 1079, 1192 repeated 978f. Systematic errors 653
T ableau (interpolation)
103, 183 Tangent function, continued fraction 163 Target, for pointer 938f., 945, 952f. Taylor series 180, 355f., 408, 702, 709, 742, 754, 759 Test programs 3 Thermodynamics, analogy for simulated annealing 437 Thinking Machines, Inc. 964 Threshold multiply of sparse matrices 74, 1031 Tides 560f. Tikhonov-Miller regularization 799ff. Time domain 490 Time splitting 847f., 861 tiny() intrinsic function 952 Toeplitz matrix 82, 85ff., 195, 1038 LU decomposition 87 new, fast algorithms 88f. nonsymmetric 86ff., 1038 Tongue twisters 333 Torus 297f., 304 Trade-off curve 795, 809 Trademarks 1/xxii, 2/xixf. Transformation Gauss 256 Landen 256 method for random number generator 277ff. Transformational functions 948ff. Transforms, number theoretic 503f. Transport error 831ff. transpose() intrinsic function 950, 960, 969, 981, 1050, 1246 Transpose of sparse matrix 73f. Trapezoidal rule 125, 127, 130ff., 134f., 579, 583, 782, 786, 1052, 1326f. Traveling salesman problem 438ff., 1219ff. Tridiagonal matrix 42, 63, 150, 453f., 488, 839f., 1018f. in alternating-direction implicit method (ADI) 861f. from cubic spline 109 cyclic 67, 1030 in cyclic reduction 853 eigenvalues 469ff., 1228 with fringes 822 from operator splitting 861f. parallel algorithm 975, 1018, 1229f. recursive splitting 1229f. reduction of symmetric matrix to 462ff., 470, 1227f.
971
serial algorithm 1018f. see also Matrix Trigonometric functions, linear sequences 173 functions, recurrence relation 172, 572 functions, tan(θ/2) as minimal 173 interpolation 99 solution of cubic equation 179f. Truncation error 20f., 399, 709, 881, 1362 in multigrid method 875 in numerical derivatives 180 Tukey’s biweight 697 Tukey’s trimean 694 Turbo Pascal (Borland) 8 Twin errors 895 Two-dimensional see Multidimensional Two-dimensional K–S test 640, 1281ff. Two-pass algorithm for variance 607, 1269 Two-point boundary value problems 702, 745ff., 1314ff. automated allocation of mesh points 774f., 777 boundary conditions 745ff., 749, 751f., 771, 1314ff. difficult cases 753, 1315f. eigenvalue problem for differential equations 748, 764ff., 770ff., 1319ff. free boundary problem 748, 776 grid (mesh) points 746f., 754, 774f., 777 internal boundary conditions 775ff. internal singular points 775ff. linear requires no iteration 751 multiple shooting 753 problems reducible to standard form 748 regularity condition 775 relaxation method 746f., 753ff., 1316ff. relaxation method, example of 764ff., 1319 shooting to a fitting point 751ff., 1315f., 1323ff. shooting method 746, 749ff., 770ff., 1314ff., 1321ff. shooting method, example of 770ff., 1321ff. singular endpoints 751, 764, 771, 1315f., 1319ff. see also Elliptic partial differential equations Two-sided exponential error distribution 696 Two-sided power spectral density 493 Two-step Lax-Wendroff method 835ff. Two-volume edition, plan of 1/xiii Two’s complement arithmetic 1144 Type declarations, explicit vs. implicit 2
U bound() intrinsic function 949 ULTRIX 1/xxiii, 2/xix Uncertainty coefficient 628 Uncertainty principle 600 Undefined status, of arrays and pointers 952f., 961, 1070, 1266, 1293, 1302 Underflow, in IEEE arithmetic 883, 1343 Underrelaxation 857 Uniform deviates see Random deviates, uniform
972
Index to Volumes 1 and 2
Unitary (function) 843f. Unitary (matrix) see Matrix unit matrix() utility function 985, 990, 1006, 1216, 1226, 1325 UNIX 1/xxiii, 2/viii, 2/xix, 4, 17, 276, 293, 886 Upper Hessenberg matrix see Hessenberg matrix U.S. Postal Service barcode 894 unpack() intrinsic function 950, 964 communication bottleneck 969 Upper subscript 944 upper triangle() utility function 990, 1006, 1226, 1305 Upwind differencing 832f., 837 USE statement 936, 939f., 954, 957, 1067, 1384 USES keyword in program listings 2 Utility functions 987ff., 1364ff. add vector to matrix diagonal 1004, 1234, 1366, 1381 alphabetical listing 988ff. argument checking 994f., 1370f. arithmetic progression 996, 1072, 1127, 1365, 1371f. array reallocation 992, 1070f., 1365, 1368f. assertion of numerical equality 995, 1022, 1365, 1370f. compared to intrinsics 990ff. complex nth root of unity 999f., 1379 copying arrays 991, 1034, 1327f., 1365f. create unit matrix 1006, 1382 cumulative product of an array 997f., 1072, 1086, 1375 cumulative sum of an array 997, 1280f., 1365, 1375 data types 1361 elemental functions 1364 error handling 994f., 1036, 1370f. generic functions 1364 geometric progression 996f., 1365, 1372ff. get diagonal of matrix 1005, 1226f., 1366, 1381f. length of a vector 1008, 1383 linear recurrence 996 location in an array 992ff., 1015, 1017ff. location of first logical “true” 993, 1041, 1369 location of maximum array value 993, 1015, 1017, 1365, 1369 location of minimum array value 993, 1369f. logical assertion 994, 1086, 1090, 1092, 1365, 1370 lower triangular mask 1007, 1200, 1382 masked polynomial evaluation 1378 masked swap of elements in two arrays 1368 moving data 990ff., 1015 multiply vector into matrix diagonal 1004f., 1366, 1381 nrutil.f90 (module file) 1364ff. outer difference of vectors 1001, 1366, 1380 outer logical and of vectors 1002
outer operations on vectors 1000ff., 1379f. outer product of vectors 1000f., 1076, 1365f., 1379 outer quotient of vectors 1001, 1379 outer sum of vectors 1001, 1379f. overloading 1364 partial cumulants of a polynomial 999, 1071, 1192f., 1365, 1378f. polynomial evaluation 996, 998f., 1258, 1365, 1376ff. scatter-with-add 1002f., 1032f., 1366, 1380f. scatter-with-combine 1002f., 1032f., 1380f. scatter-with-max 1003f., 1366, 1381 set diagonal elements of matrix 1005, 1200, 1366, 1382 skew operation on matrices 1004ff., 1381ff. swap elements of two arrays 991, 1015, 1365ff. upper triangular mask 1006, 1226, 1305, 1382
V -cycle 865, 1336 vabs() utility function 990, 1008, 1290 Validation of Numerical Recipes procedures 3f. Valley, long or narrow 403, 407, 410 Van Cittert’s method 804 Van Wijngaarden-Dekker-Brent method see Brent’s method Vandermonde matrix 82ff., 114, 1037, 1047 Variable length code 896, 1346ff. Variable metric method 390, 418ff., 1215 compared to conjugate gradient method 418 Variable step-size integration 123, 135, 703, 707ff., 720, 726, 731, 737, 742ff., 1298ff., 1303, 1308f., 1311ff. Variance(s) correlation 605 of distribution 603ff., 608, 611, 613, 1269 pooled 610 reduction of (in Monte Carlo) 299, 306ff. statistical differences between two 609, 1271 two-pass algorithm for computing 607, 1269 see also Covariance Variational methods, partial differential equations 824 VAX 275, 293 Vector(s) length 1008, 1383 norms 1036 outer difference 1001, 1366, 1380 outer operations 1000ff., 1379f. outer product 1000f., 1076, 1365f., 1379 Vector reduction 972, 977, 998 Vector subscripts 2/xiif., 984, 1002, 1032, 1034 communication bottleneck 969, 981, 1250 VEGAS algorithm for Monte Carlo 309ff., 1161 Verhoeff’s algorithm for checksums 894f., 1345
973
Index to Volumes 1 and 2
Vi`ete’s formulas for cubic roots 179 Vienna Fortran 2/xv Virus, computer 889 Viscosity artificial 831, 837 numerical 830f., 837 Visibility 956ff., 1209, 1293, 1296 VMS 1/xxii, 2/xix Volterra equations 780f., 1326 adaptive stepsize control 788 analogy with ODEs 786 block-by-block method 788 first kind 781, 786 nonlinear 781, 787 second kind 781, 786ff., 1326f. unstable quadrature 787f. von Neuman, John 963, 965 von Neumann-Richtmyer artificial viscosity 837 von Neumann stability analysis for PDEs 827f., 830, 833f., 840 Vowellish (coding example) 896f., 902
W -cycle
865, 1336 Warranty, disclaimer of 1/xx, 2/xvii Wave equation 246, 818, 825f. Wavelet transform 584ff., 1264ff. appearance of wavelets 590ff. approximation condition of order p 585 coefficient values 586, 589, 1265 contrasted with Fourier transform 584, 594 Daubechies wavelet filter coefficients 584ff., 588, 590f., 594, 598, 1264ff. detail information 585 discrete wavelet transform (DWT) 586f., 1264 DWT (discrete wavelet transform) 586f., 1264ff. eliminating wrap-around 587 fast solution of linear equations 597ff. filters 592f. and Fourier domain 592f. image processing 596f. for integral equations 782 inverse 587 Lemarie’s wavelet 593 of linear operator 597ff. mother-function coefficient 587 mother functions 584 multidimensional 595, 1267f. nonsmoothness of wavelets 591 pyramidal algorithm 586, 1264 quadrature mirror filter 585 smooth information 585 truncation 594f. wavelet filter coefficient 584, 587 wavelets 584, 590ff. Wavelets see Wavelet transform Weber function 204 Weighted Kolmogorov-Smirnov test 621 Weighted least-squares fitting see Least squares fitting
Weighting, full vs. half in multigrid 867 Weights for Gaussian quadrature 140ff., 788f., 1059ff., 1328f. nonclassical weight function 151ff., 788f., 1064f., 1328f. Welch window 547, 1254ff. WG5 (ISO/IEC JTC1/SC22/WG5 Committee) 2/xiff. where construct 943, 1291 contrasted with merge 1023 for iteration of a vector 1060 nested 2/xv, 943, 960, 1100 not MIMD 985 While iteration 13 Wiener filtering 535, 539ff., 558, 644 compared to regularization 801 Wiener-Khinchin theorem 492, 558, 566f. Wilcoxon test 694 Window function Bartlett 547, 1254ff. flat-topped 549 Hamming 547 Hann 547 Parzen 547 square 544, 546, 1254ff. Welch 547, 1254ff. Windowing for spectral estimation 1255f. Windows 95 2/xix Windows NT 2/xix Winograd Fourier transform algorithms 503 Woodbury formula 68ff., 83 Wordlength 18 Workspace, reallocation in Fortran 90 1070f. World Wide Web, Numerical Recipes site 1/xx, 2/xvii Wraparound in integer arithmetic 1146, 1148 order for storing spectrum 501 problem in convolution 533 Wronskian, of Bessel functions 234, 239
X .25 protocol 890 X3J3 Committee 2/viii, 2/xff., 2/xv, 947, 959, 964, 968, 990 XMODEM checksum 889 X-ray diffraction pattern, processing of 805 Y ale Sparse Matrix Package Z -transform
64, 71
554, 559, 565 Z-transformation, Fisher’s 631f., 1276 Zaman, A. 1149 Zealots 814 Zebra relaxation 866 Zero contours 372 Zero-length array 944 Zeroth-order regularization 796ff. Zip code, barcode for 894 Ziv-Lempel compression 896 zroots unity() utility function 974, 990, 999
in Fortran 90 Second Edition
Volume 2 of Fortran Numerical Recipes
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Numerical Recipes
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
in Fortran 90
The Art of Parallel Scientific Computing Second Edition
Volume 2 of Fortran Numerical Recipes
William H. Press
Harvard-Smithsonian Center for Astrophysics
Saul A. Teukolsky
Department of Physics, Cornell University
William T. Vetterling
Polaroid Corporation
Brian P. Flannery
EXXON Research and Engineering Company
Foreword by
Michael Metcalf
CERN, Geneva, Switzerland
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Numerical Recipes
Published by the Press Syndicate of the University of Cambridge The Pitt Building, Trumpington Street, Cambridge CB2 1RP 40 West 20th Street, New York, NY 10011-4211, USA 10 Stamford Road, Oakleigh, Melbourne 3166, Australia
Numerical Recipes in Fortran 90: The Art of Parallel Scientific Computing, Volume 2 of Fortran Numerical Recipes, Second Edition, first published 1996. Reprinted with corrections 1997. The code in this volume is corrected to software version 2.08 Printed in the United States of America Typeset in TEX
Without an additional license to use the contained software, this book is intended as a text and reference book, for reading purposes only. A free license for limited use of the software by the individual owner of a copy of this book who personally types one or more routines into a single computer is granted under terms described on p. xviii. See the section “License Information” (pp. xvii–xx) for information on obtaining more general licenses at low cost. Machine-readable media containing the software in this book, with included licenses for use on a single screen, are available from Cambridge University Press. See the order form at the back of the book, email to “[email protected]” (North America) or “[email protected]” (rest of world), or write to Cambridge University Press, 110 Midland Avenue, Port Chester, NY 10573 (USA), for further information. The software may also be downloaded, with immediate purchase of a license also possible, from the Numerical Recipes Software Web site (http://www.nr.com). Unlicensed transfer of Numerical Recipes programs to any other format, or to any computer except one that is specifically licensed, is strictly prohibited. Technical questions, corrections, and requests for information should be addressed to Numerical Recipes Software, P.O. Box 243, Cambridge, MA 02238 (USA), email “[email protected]”, or fax 781 863-1739.
Library of Congress Cataloging-in-Publication Data Numerical recipes in Fortran 90 : the art of parallel scientific computing / William H. Press . . . [et al.]. – 2nd ed. p. cm. Includes bibliographical references and index. ISBN 0-521-57439-0 (hardcover) 1. FORTRAN 90 (Computer program language) 2. Parallel programming (Computer science) 3. Numerical analysis–Data processing. I. Press, William H. QA76.73.F25N85 1996 96-5567 519.4 0285 52–dc20 CIP A catalog record for this book is available from the British Library. ISBN ISBN ISBN ISBN ISBN ISBN
0 0 0 0 0 0
521 57439 521 43064 521 43721 521 57440 521 57608 521 57607
0 X 0 4 3 5
Volume 2 (this book) Volume 1 Example book in FORTRAN FORTRAN diskette (IBM 3.5 ) CDROM (IBM PC/Macintosh) CDROM (UNIX)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
c Cambridge University Press 1986, 1996, Copyright except for all computer programs and procedures, which are c Numerical Recipes Software 1986, 1996, Copyright and except for Appendix C1, which is placed into the public domain. All Rights Reserved.
Contents
viii
Foreword by Michael Metcalf License Information
x xvii
21 Introduction to Fortran 90 Language Features 21.0 Introduction 21.1 Quick Start: Using the Fortran 90 Numerical Recipes Routines 21.2 Fortran 90 Language Concepts 21.3 More on Arrays and Array Sections 21.4 Fortran 90 Intrinsic Procedures 21.5 Advanced Fortran 90 Topics 21.6 And Coming Soon: Fortran 95
22 Introduction to Parallel Programming 22.0 Why Think Parallel? 22.1 Fortran 90 Data Parallelism: Arrays and Intrinsics 22.2 Linear Recurrence and Related Calculations 22.3 Parallel Synthetic Division and Related Algorithms 22.4 Fast Fourier Transforms 22.5 Missing Language Features
935 935 936 937 941 945 953 959
962 962 964 971 977 981 983
23 Numerical Recipes Utility Functions for Fortran 90 987 23.0 Introduction and Summary Listing 23.1 Routines That Move Data 23.2 Routines Returning a Location 23.3 Argument Checking and Error Handling 23.4 Routines for Polynomials and Recurrences 23.5 Routines for Outer Operations on Vectors 23.6 Routines for Scatter with Combine 23.7 Routines for Skew Operations on Matrices 23.8 Other Routines
987 990 992 994 996 1000 1002 1004 1007
Fortran 90 Code Chapters
1009
B1
Preliminaries
1010
B2
Solution of Linear Algebraic Equations
1014
B3
Interpolation and Extrapolation
1043
v
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Preface to Volume 2
vi
Contents
Integration of Functions
1052
B5
Evaluation of Functions
1070
B6
Special Functions
1083
B7
Random Numbers
1141
B8
Sorting
1167
B9
Root Finding and Nonlinear Sets of Equations
1182
B10 Minimization or Maximization of Functions
1201
B11 Eigensystems
1225
B12 Fast Fourier Transform
1235
B13 Fourier and Spectral Applications
1253
B14 Statistical Description of Data
1269
B15 Modeling of Data
1285
B16 Integration of Ordinary Differential Equations
1297
B17 Two Point Boundary Value Problems
1314
B18 Integral Equations and Inverse Theory
1325
B19 Partial Differential Equations
1332
B20 Less-Numerical Algorithms
1343
References
1359
Appendices C1
Listing of Utility Modules (nrtype and nrutil)
1361
C2
Alphabetical Listing of Explicit Interfaces
1384
C3
Index of Programs and Dependencies
1434
General Index to Volumes 1 and 2
1447
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
B4
Contents
vii
Contents of Volume 1: Numerical Recipes in Fortran 77 Plan of the Two-Volume Edition
xiii
Preface to the Second Edition
xv
License Information Computer Programs by Chapter and Section
xviii xx xxiv
1
Preliminaries
1
2
Solution of Linear Algebraic Equations
22
3
Interpolation and Extrapolation
99
4
Integration of Functions
123
5
Evaluation of Functions
159
6
Special Functions
205
7
Random Numbers
266
8
Sorting
320
9
Root Finding and Nonlinear Sets of Equations
340
10
Minimization or Maximization of Functions
387
11
Eigensystems
449
12
Fast Fourier Transform
490
13
Fourier and Spectral Applications
530
14
Statistical Description of Data
603
15
Modeling of Data
650
16
Integration of Ordinary Differential Equations
701
17
Two Point Boundary Value Problems
745
18
Integral Equations and Inverse Theory
779
19
Partial Differential Equations
818
20
Less-Numerical Algorithms
881
References
916
Index of Programs and Dependencies
921
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Preface to the First Edition
Preface to Volume 2
f90
viii
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Fortran 90 is not just the long-awaited updating of the Fortran language to modern computing practices. It is also the vanguard of a much larger revolution in computing, that of multiprocessor computers and widespread parallel programming. Parallel computing has been a feature of the largest supercomputers for quite some time. Now, however, it is rapidly moving towards the desktop. As we watched the gestation and birth of Fortran 90 by its governing “X3J3 Committee” (a process interestingly described by a leading committee member, Michael Metcalf, in the Foreword that follows), it became clear to us that the right moment for moving Numerical Recipes from Fortran 77 to Fortran 90 was sooner, rather than later. Fortran 90 compilers are now widely available. Microsoft’s Fortran PowerStation for Windows 95 brings that firm’s undeniable marketing force to PC desktop; we have tested this compiler thoroughly on our code and found it excellent in compatibility and performance. In the UNIX world, we have similarly tested, and had generally fine experiences with, DEC’s Fortran 90 for Alpha AXP and IBM’s xlf for RS/6000 and similar machines. NAG’s Fortran 90 compiler also brings excellent Fortran 90 compatibility to a variety of UNIX platforms. There are no doubt other excellent compilers, both available and on the way. Fortran 90 is completely backwards compatible with Fortran 77, by the way, so you don’t have to throw away your legacy code, or keep an old compiler around. There have been previous special versions of Fortran for parallel supercomputers, but always specific to a particular hardware. Fortran 90, by contrast, is designed to provide a general, architecture-independent framework for parallel computation. Equally importantly, it is an international standard, agreed upon by a large group of computer hardware and software manufacturers and international standards bodies. With the Fortran 90 language as a tool, we want this volume to be your complete guide for learning how to “think parallel.” The language itself is very general in this regard, and applicable to many present and future computers, or even to other parallel computing languages as they come along. Our treatment emphasizes general principles, but we are also not shy about pointing out parallelization “tricks” that have frequent applicability. These are not only discussed in this volume’s principal text chapters (Chapters 21–23), but are also sprinkled throughout the chapters of Fortran 90 code, called out by a special “parallel hint” logo (left, above). Also scattered throughout the code chapters are specific “Fortran 90 tips,” with their own distinct graphic call-out (left). After you read the text chapters, you might want simply to browse among these hints and tips. A special note to C programmers: Right now, there is no effort at producing a parallel version of C that is comparable to Fortran 90 in maturity, acceptance, and stability. We think, therefore, that C programmers will be well served by using this volume for an educational excursion into Fortran 90, its parallel programming constructions, and the numerical algorithms that capitalize on them. C and C++ programming have not been far from our minds as we have written this volume, and we think that you will find that time spent in absorbing its principal lessons (in Chapters 21–23) will be amply repaid in the future, as C and C++ eventually develop standard parallel extensions.
Preface to Volume 2
ix
Acknowledgments We continue to be in the debt of many colleagues who give us the benefit of their numerical and computational experience. Many, though not all, of these are listed by name in the preface to the second edition, in Volume 1. To that list we must now certainly add George Marsaglia, whose ideas have greatly influenced our new discussion of random numbers in this volume (Chapter B7). With this volume, we must acknowledge our additional gratitude and debt to a number of people who generously provided advice, expertise, and time (a great deal of time, in some cases) in the areas of parallel programming and Fortran 90. The original inspiration for this volume came from Mike Metcalf, whose clear lectures on Fortran 90 (in this case, overlooking the beautiful Adriatic at Trieste) convinced us that Fortran 90 could serve as the vehicle for a book with the larger scope of parallel programming generally, and whose continuing advice throughout the project has been indispensable. Gyan Bhanot also played a vital early role in the development of this book; his first translations of our Fortran 77 programs taught us a lot about parallel programming. We are also grateful to Greg Lindhorst, Charles Van Loan, Amos Yahil, Keith Kimball, Malcolm Cohen, Barry Caplin, Loren Meissner, Mitsu Sakamoto, and George Schnurer for helpful correspondence and/or discussion of Fortran 90’s subtler aspects. We once again express in the strongest terms our gratitude to programming consultant Seth Finkelstein, whose contribution to both the coding and the thorough testing of all the routines in this book (against multiple compilers and in sometimesbuggy, and always challenging, early versions) cannot be overstated. WHP and SAT acknowledge the continued support of the U.S. National Science Foundation for their research on computational methods. February 1996
William H. Press Saul A. Teukolsky William T. Vetterling Brian P. Flannery
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
A final word of truth in packaging: Don’t buy this volume unless you also buy (or already have) Volume 1 (now retitled Numerical Recipes in Fortran 77). Volume 2 does not repeat any of the discussion of what individual programs actually do, or of the mathematical methods they utilize, or how to use them. While our Fortran 90 code is thoroughly commented, and includes a header comment for each routine that describes its input and output quantities, these comments are not supposed to be a complete description of the programs; the complete descriptions are in Volume 1, which we reference frequently. But here’s a money-saving hint to our previous readers: If you already own a Second Edition version whose title is Numerical Recipes in FORTRAN (which doesn’t indicate either “Volume 1” or “Volume 2” on its title page) then take a marking pen and write in the words “Volume 1.” There! (Differences between the previous reprintings and the newest reprinting, the one labeled “Volume 1,” are minor.)
Foreword by Michael Metcalf
The Birth of a Standard Back in 1966, the version of Fortran now known as Fortran 66 was the first language ever to be standardized, by the predecessor of the present American National Standards Institute (ANSI). It was an all-American affair. Fortran had first been developed by John Backus of IBM in New York, and it was the dominant scientific programming language in North America. Many Europeans preferred Algol (in which Backus had also had a hand). Eventually, however, the mathematicians who favored Algol for its precisely expressible syntax began to defer to the scientists and engineers who appreciated Fortran’s pragmatic, even natural, style. In 1978, the upgraded Fortran 77 was standardized by the ANSI technical committee, X3J3, and subsequently endorsed by other national bodies and by ISO in Geneva, Switzerland. Its dominance in all fields of scientific and numerical computing grew as new, highly optimizing compilers came onto the market. Although newer languages, particularly Pascal, Basic, PL/1, and later Ada attracted their own adherents, scientific users throughout the 1980s remained true to Fortran. Only towards the end of that decade did C draw increasing support from scientific programmers who had discovered the power of structures and pointers. During all this time, X3J3 kept functioning, developing the successor version to Fortran 77. It was to be a decade of strife and contention. The early plans, in the late 1970s, were mainly to add to Fortran 77 features that had had to be left out of that standard. Among these were dynamic storage and an array language, enabling it to map directly onto the architecture of supercomputers, then coming onto the market. The intention was to have this new version ready within five years, in 1982. But two new factors became significant at that time. The first was the decision that the next standard should not just codify existing practice, as had largely been the case in 1966 and 1978, but also extend the functionality of the language through x
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Sipping coffee on a sunbaked terrace can be surprisingly productive. One of the Numerical Recipes authors and I were each lecturing at the International Center for Theoretical Physics in Trieste, Italy, he on numerical analysis and I on Fortran 90. The numerical analysis community had made important contributions to the development of the new Fortran standard, and so, unsurprisingly, it became quickly apparent that the algorithms for which Numerical Recipes had become renowned could, to great advantage, be recast in a new mold. These algorithms had, hitherto, been expressed in serial form, first in Fortran 77 and then in C, Pascal, and Basic. Now, nested iterations could be replaced by array operations and assignments, and the other features of a rich array language could be exploited. Thus was the idea of a “Numerical Recipes in Fortran 90" first conceived and, after three years’ gestation, it is a delight to assist at the birth. But what is Fortran 90? How did it begin, what shaped it, and how, after nearly foundering, did its driving forces finally steer it to a successful conclusion?
Foreword
xi
Conflict The meetings of X3J3 were often full of drama. Most compiler vendors were represented as a matter of course but, for many, their main objective appeared to be to maintain the status quo and to ensure that Fortran 90 never saw the light of
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
innovative additions (even though, for the array language, there was significant borrowing from John Iverson’s APL and from DAP Fortran). The second factor was that X3J3 no longer operated under only American auspices. In the course of the 1980s, the standardization of programming languages came increasingly under the authority of the international body, ISO. Initially this was in an advisory role, but now ISO is the body that, through its technical committee WG5 (in full, ISO/IEC JTC1/SC22/WG5), is responsible for determining the course of the language. WG5 also steers the work of the development body, then as now, the highly skilled and competent X3J3. As we shall see, this shift in authority was crucial at the most difficult moment of Fortran 90’s development. The internationalization of the standards effort was reflected in the welcome given by X3J3 to six or seven European members; they, and about one-third of X3J3’s U.S. members, provided the overlapping core of membership of X3J3 and WG5 that was vital in the final years in bringing the work to a successful conclusion. X3J3 membership, which peaked at about 45, is restricted to one voting member per organization, and significant decisions require a majority of two-thirds of those voting. Nationality plays no role, except in determining the U.S. position on an international issue. Members, who are drawn mainly from the vendors, large research laboratories, and academia, must be present or represented at two-thirds of all meetings in order to retain voting rights. In 1980, X3J3 reported on its plans to the forerunner of WG5 in Amsterdam, Holland. Fortran 8x, as it was dubbed, was to have a basic array language, new looping constructs, a bit data type, data structures, a free source form, a mechanism to “group” procedures, and another to manage the global name space. Old features, including COMMON, EQUIVALENCE, and the arithmetic-IF, were to be consigned to a so-called obsolete module, destined to disappear in a subsequent revision. This was part of the “core plus modules” architecture, for adding new features and retiring old ones, an aid to backwards compatibility. Even though Fortran 77 compilers were barely available, the work seemed well advanced and the mood was optimistic. Publication was intended to take place in 1985. It was not to be. One problem was the sheer number of new features that were proposed as additions to the language, most of them worthwhile in themselves but with the totality being too large. This became a recurrent theme throughout the development of the standard. One example was the suggestion of Lawrie Schonfelder (Liverpool University), at a WG5 meeting in Vienna, Austria, in 1982, that certain features already proposed as additions could be combined to provide a full-blown derived data type facility, thus providing Fortran with abstract data types. This idea was taken up by X3J3 and has since come to be recognized, along with the array language, as one of the two main advances brought about by what became Fortran 90. However, the ramifications go very deep: all the technical details of how to handle arrays of objects of derived types that in turn have array components that have the pointer attribute, and so forth, have to be precisely defined and rigorously specified.
xii
Foreword
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
day. One vendor’s extended (and much-copied) version of Fortran 77 had virtually become an industry standard, and it saw as its mission the maintenance of this lead. A new standard would cost it its perceived precious advantage. Other large vendors had similar points of view, although those marketing supercomputers were clearly keen on the array language. Most users, on the other hand, were hardly prepared to invest large amounts of their employers’ and their own resources in simply settling for a trivial set of improvements to the existing standard. However, as long as X3J3 worked under a simple-majority voting rule, at least some apparent progress could be made, although the underlying differences often surfaced. These were even sometimes between users — those who wanted Fortran to become a truly modern language and those wanting to maintain indefinite backwards compatibility for their billions of lines of existing code. At a watershed meeting, in Scranton, Pennsylvania, in 1986, held in an atmosphere that sometimes verged on despair, a fragile compromise was reached as a basis for further work. One breakthrough was to weaken the procedures for removing outdated features from the language, particularly by removing no features whatsoever from the next standard and by striking storage association (i.e., COMMON and EQUIVALENCE) from the list of features to be designated as obsolescent (as they are now known). A series of votes definitively removed from the language all plans to add: arrays of arrays, exception handling, nesting of internal procedures, the FORALL statement (now in Fortran 95), and a means to access skew array sections. There were other features on this list that, although removed, were reinstated at later meetings: user-defined operators, operator overloading, array and structure constructors, and vector-valued subscripts. After many more travails, the committee voted, a year later, by 26 votes to 9, to forward the document for what was to become the first of three periods of public comment. While the document was going through the formal standards bureaucracy and being placed before the public, X3J3 polished it further. X3J3 also prepared procedures for processing the comments it anticipated receiving from the public, and to each of which, under the rules, it would have to reply individually. It was just as well. Roughly 400 replies flooded in, many of them very detailed and, disappointingly for those of us wanting a new standard quickly, unquestionably negative towards our work. For many it was too radical, but many others pleaded for yet more modern features, such as pointers. Now the committee was deadlocked. Given that a document had already been published, any further change required not a simple but a two-thirds majority. The conservatives and the radicals could each block a move to modify the draft standard, or to accept a revised one for public review — and just that happened, in Champagne-Urbana, Illinois, in 1988. Any change, be it on the one hand to modify the list of obsolescent features, to add the pointers or bit data type wanted by the public, to add multi-byte characters to support Kanji and other non-European languages or, on the other hand, to emasculate the language by removing modules or operator overloading, and hence abstract data types, to name but some suggestions, none of these could be done individually or collectively in a way that would achieve consensus. I wrote: “In my opinion, no standard can now emerge without either a huge concession by the users to the vendors (MODULE / USE) and/or a major change in the composition of the committee. I do not see how members who have worked for up to a decade
Foreword
xiii
Resolution The “August meeting” was, unexpectedly, imbued with a spirit of compromise that had been so sadly lacking at the previous one. Nevertheless, after a week of discussing four separate plans to rescue the standard, no agreement was reached. Now the question seriously arose: Was X3J3 incapable of producing a new Fortran standard for the international community, doomed to eternal deadlock, a victim of ANSI procedures? Breakthrough was achieved at a traumatic meeting of WG5 in Paris, France, a month later. The committee spent several extraordinary days drawing up a detailed list of what it wanted to be in Fortran 8x. Finally, it set X3J3 an ultimatum that was unprecedented in the standards world: The ANSI committee was to produce a new draft document, corresponding to WG5’s wishes, within five months! Failing that, WG5 would assume responsibility and produce the new standard itself. This decision was backed by the senior U.S. committee, X3, which effectively directed X3J3 to carry out WG5’s wishes. And it did! The following November, it implemented most of the technical changes, adding pointers, bit manipulation intrinsic procedures, and vector-valued subscripts, and removing user-defined elemental functions (now in Fortran 95). The actual list of changes was much longer. X3J3 and WG5, now collaborating closely, often in gruelling six-day meetings, spent the next 18 months and two more periods of (positive) public comment putting the finishing touches to what was now called Fortran 90, and it was finally adopted, after some cliff-hanging votes, for forwarding as a U.S. and international standard on April 11, 1991, in Minneapolis, Minnesota. Among the remaining issues that were decided along the way were whether pointers should be a data type or be defined in terms of an attribute of a variable, implying strong typing (the latter was chosen), whether the new standard should coexist alongside the old one rather than definitively replace it (it coexisted for a while in the U.S., but was a replacement elsewhere, under ISO rules), and whether, in the new free source form, blanks should be significant (fortunately, they are).
Fortran 90 The main new features of Fortran 90 are, first and foremost, the array language and abstract data types. The first is built on whole array operations and assignments, array sections, intrinsic procedures for arrays, and dynamic storage. It was designed with optimization in mind. The second is built on modules and module procedures, derived data types, operator overloading and generic interfaces, together with
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
or more, devoting time and intellectual energy far beyond the call of duty, can be expected to make yet more personal sacrifices if no end to the work is in sight, or if that end is nothing but a travesty of what had been designed and intended as a modern scientific programming language. . . . I think the August meeting will be a watershed — if no progress is achieved there will be dramatic resignations, and ISO could even remove the work from ANSI, which is failing conspicuously in its task." (However, the same notes began with a quotation from The Taming of the Shrew: “And do as adversaries do in law, / Strive mightily, but eat and drink / as friend." That we always did, copiously.)
xiv
Foreword
Fortran Evolution The formal procedures under which languages are standardized require them either to evolve or to die. A standard that has not been revised for some years must either be revised and approved anew, or be withdrawn. This matches the technical pressure on the language developers to accommodate the increasing complexity both of the problems to be tackled in scientific computation and of the underlying hardware
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
pointers. Also important are the new facilities for numerical computation including a set of numeric inquiry functions, the parametrization of the intrinsic types, new control constructs — SELECT CASE and new forms of DO, internal and recursive procedures and optional and keyword arguments, improved I/O facilities, and many new intrinsic procedures. Last but not least are the new free source form, an improved style of attribute-oriented specifications, the IMPLICIT NONE statement, and a mechanism for identifying redundant features for subsequent removal from the language. The requirement on compilers to be able to identify, for example, syntax extensions, and to report why a program has been rejected, are also significant. The resulting language is not only a far more powerful tool than its successor, but a safer and more reliable one too. Storage association, with its attendant dangers, is not abolished, but rendered unnecessary. Indeed, experience shows that compilers detect errors far more frequently than before, resulting in a faster development cycle. The array syntax and recursion also allow quite compact code to be written, a further aid to safe programming. No programming language can succeed if it consists simply of a definition (witness Algol 68). Also required are robust compilers from a wide variety of vendors, documentation at various levels, and a body of experience. The first Fortran 90 compiler appeared surprisingly quickly, in 1991, especially in view of the widely touted opinion that it would be very difficult to write one. Even more remarkable was that it was written by one person, Malcolm Cohen of NAG, in Oxford, U.K. There was a gap before other compilers appeared, but now they exist as native implementations for almost all leading computers, from the largest to PCs. For the most part, they produce very efficient object code; where, for certain new features, this is not the case, work is in progress to improve them. The first book, Fortran 90 Explained, was published by John Reid and me shortly before the standard itself was published. Others followed in quick succession, including excellent texts aimed at the college market. At the time of writing there are at least 19 books in English and 22 in various other languages: Chinese, Dutch, French, Japanese, Russian, and Swedish. Thus, the documentation condition is fulfilled. The body of experience, on the other hand, has yet to be built up to a critical size. Teaching of the language at college level has only just begun. However, I am certain that this present volume will contribute decisively to a significant breakthrough, as it provides models not only of the numerical algorithms for which previous editions are already famed, but also of an excellent Fortran 90 style, something that can develop only with time. Redundant features are abjured. It shows that, if we abandon these features and use new ones in their place, the appearance of code can initially seem unfamiliar, but, in fact, the advantages become rapidly apparent. This new edition of Numerical Recipes stands as a landmark in this regard.
Foreword
xv
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
on which programs run. Increasing problem complexity requires more powerful features and syntax; new hardware needs language features that map onto it well. Thus it was that X3J3 and WG5, having finished Fortran 90, began a new round of improvement. They decided very quickly on new procedures that would avoid the disputes that bedevilled the previous work: WG5 would decide on a plan for future standards, and X3J3 would act as the so-called development body that would actually produce them. This would be done to a strict timetable, such that any feature that could not be completed on time would have to wait for the next round. It was further decided that the next major revision should appear a decade after Fortran 90 but, given the somewhat discomforting number of requests for interpretation that had arrived, about 200, that a minor revision should be prepared for mid-term, in 1995. This should contain only “corrections, clarifications and interpretations” and a very limited number (some thought none) of minor improvements. At the same time, scientific programmers were becoming increasingly concerned at the variety of methods that were necessary to gain efficient performance from the ever-more widely used parallel architectures. Each vendor provided a different set of parallel extensions for Fortran, and some academic researchers had developed yet others. On the initiative of Ken Kennedy of Rice University, a High-Performance Fortran Forum was established. A coalition of vendors and users, its aim was to produce an ad hoc set of extensions to Fortran that would become an informal but widely accepted standard for portable code. It set itself the daunting task of achieving that in just one year, and succeeded. Melding existing dialects like Fortran D, CM Fortran, and Vienna Fortran, and adopting the new Fortran 90 as a base, because of its array syntax, High-Performance Fortran (HPF) was published in 1993 and has since become widely implemented. However, although HPF was designed for data parallel codes and mainly implemented in the form of directives that appear to non-HPF processors as comment lines, an adequate functionality could not be achieved without extending the Fortran syntax. This was done in the form of the PURE attribute for functions — an assertion that they contain no side effects — and the FORALL construct — a form of array assignment expressed with the help of indices. The dangers of having diverging or competing forms of Fortran 90 were immediately apparent, and the standards committees wisely decided to incorporate these two syntactic changes also into Fortran 95. But they didn’t stop there. Two further extensions, useful not only for their expressive power but also to access parallel hardware, were added: elemental functions, ones written in terms of scalars but that accept array arguments of any permitted shape or size, and an extension to allow nesting of WHERE constructs, Fortran’s form of masked assignment. To readers of Numerical Recipes, perhaps the most relevant of the minor improvements that Fortran 95 brings are the ability to distinguish between a negative and a positive real zero, automatic deallocation of allocatable arrays, and a means to initialize the values of components of objects of derived data types and to initialize pointers to null. The medium-term objective of a relatively minor upgrade has been achieved on schedule. But what does the future hold? Developments in the underlying principles of procedural programming languages have not ceased. Early Fortran introduced the concepts of expression abstraction (X=Y+Z) and later control expression (e.g., the DO loop). Fortran 77 continued this with the if-then-else, and Fortran 90 with the DO and SELECT CASE constructs. Fortran 90 has a still higher level of expression abstraction (array assignments and expressions) as well as data structures and even
xvi
Foreword
Conclusion Writing a book is always something of a gamble. Unlike a novel that stands or falls on its own, a book devoted to a programming language is dependent on the success of others, and so the risk is greater still. However, this new Numerical Recipes in Fortran 90 volume is no ordinary book, since it comes as the continuation of a highly successful series, and so great is its significance that it can, in fact, influence the outcome in its own favor. I am entirely confident that its publication will be seen as an important event in the story of Fortran 90, and congratulate its authors on having performed a great service to the field of numerical computing. Geneva, Switzerland January 1996
Michael Metcalf
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
full-blown abstract data types. However, during the 1980s the concept of objects came to the fore, with methods bound to the objects on which they operate. Here, one particular language, C++, has come to dominate the field. Fortran 90 lacks a means to point to functions, but otherwise has most of the necessary features in place, and the standards committees are now faced with the dilemma of deciding whether to make the planned Fortran 2000 a fully object-oriented language. This could possibly jeopardize its powerful, and efficient, numerical capabilities by too great an increase in language complexity, so should they simply batten down the hatches and not defer to what might be only a passing storm? At the time of writing, this is an open issue. One issue that is not open is Fortran’s lack of in-built exception handling. It is virtually certain that such a facility, much requested by the numerical community, and guided by John Reid, will be part of the next major revision. The list of other requirements is long but speculative, but some at the top of the list are conditional compilation, command line argument handling, I/O for objects of derived type, and asynchronous I/O (which is also planned for the next release of HPF). In the meantime, some particularly pressing needs have been identified, for the handling of floating-point exceptions, interoperability with C, and allowing allocatable arrays as structure components, dummy arguments, and function results. These have led WG5 to begin processing these three items using a special form of fast track, so that they might become optional but standard extensions well before Fortran 2000 itself is published in the year 2001.
License Information
Disclaimer of Warranty We make no warranties, express or implied, that the programs contained in this volume are free of error, or are consistent with any particular standard of merchantability, or that they will meet your requirements for any particular application. They should not be relied on for solving a problem whose incorrect solution could result in injury to a person or loss of property. If you do use the programs in such a manner, it is at your own risk. The authors and publisher disclaim all liability for direct or consequential damages resulting from your use of the programs.
How to Get the Code onto Your Computer Pick one of the following methods: • You can type the programs from this book directly into your computer. In this case, the only kind of license available to you is the free “immediate license” (see below). You are not authorized to transfer or distribute a machine-readable copy to any other person, nor to have any other person type the programs into a computer on your behalf. We do not want to hear bug reports from you if you choose this option, because experience has shown that virtually all reported bugs in such cases are typing errors! • You can download the Numerical Recipes programs electronically from the Numerical Recipes On-Line Software Store, located at our Web site (http://www.nr.com). They are packaged as a password-protected file, and you’ll need to purchase a license to unpack them. You can get a single-screen license and password immediately, on-line, from the On-Line Store, with fees ranging from $50 (PC, Macintosh, educational institutions’ UNIX) to $140 (general UNIX). Downloading the packaged software from the On-Line Store is also the way to start if you want to acquire a more general (multiscreen, site, or corporate) license. • You can purchase media containing the programs from Cambridge University Press. Diskette versions are available in IBM-compatible format for machines running Windows 3.1, 95, or NT. CDROM versions in ISO9660 format for PC, Macintosh, and UNIX systems are also available; these include both Fortran and C versions (as well as versions in Pascal xvii
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Read this section if you want to use the programs in this book on a computer. You’ll need to read the following Disclaimer of Warranty, get the programs onto your computer, and acquire a Numerical Recipes software license. (Without this license, which can be the free “immediate license” under terms described below, the book is intended as a text and reference book, for reading purposes only.)
xviii
License Information
Types of License Offered Here are the types of licenses that we offer. Note that some types are automatically acquired with the purchase of media from Cambridge University Press, or of an unlocking password from the Numerical Recipes On-Line Software Store, while other types of licenses require that you communicate specifically with Numerical Recipes Software (email: [email protected] or fax: 781 863-1739). Our Web site http://www.nr.com has additional information. • [“Immediate License”] If you are the individual owner of a copy of this book and you type one or more of its routines into your computer, we authorize you to use them on that computer for your own personal and noncommercial purposes. You are not authorized to transfer or distribute machine-readable copies to any other person, or to use the routines on more than one machine, or to distribute executable programs containing our routines. This is the only free license. • [“Single-Screen License”] This is the most common type of low-cost license, with terms governed by our Single Screen (Shrinkwrap) License document (complete terms available through our Web site). Basically, this license lets you use Numerical Recipes routines on any one screen (PC, workstation, X-terminal, etc.). You may also, under this license, transfer pre-compiled, executable programs incorporating our routines to other, unlicensed, screens or computers, providing that (i) your application is noncommercial (i.e., does not involve the selling of your program for a fee), (ii) the programs were first developed, compiled, and successfully run on a licensed screen, and (iii) our routines are bound into the programs in such a manner that they cannot be accessed as individual routines and cannot practicably be unbound and used in other programs. That is, under this license, your program user must not be able to use our programs as part of a program library or “mix-and-match” workbench. Conditions for other types of commercial or noncommercial distribution may be found on our Web site (http://www.nr.com). • [“Multi-Screen, Server, Site, and Corporate Licenses”] The terms of the Single Screen License can be extended to designated groups of machines, defined by number of screens, number of machines, locations, or ownership. Significant discounts from the corresponding single-screen
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
and BASIC from the first edition) on a single CDROM. Diskettes purchased from Cambridge University Press include a single-screen license for PC or Macintosh only. The CDROM is available with a singlescreen license for PC or Macintosh (order ISBN 0 521 576083), or (at a slightly higher price) with a single-screen license for UNIX workstations (order ISBN 0 521 576075). Orders for media from Cambridge University Press can be placed at 800 872-7423 (North America only) or by email to [email protected] (North America) or [email protected] (rest of world). Or, visit the Web sites http://www.cup.org (North America) or http://www.cup.cam.ac.uk (rest of world).
License Information
xix
prices are available when the estimated number of screens exceeds 40. Contact Numerical Recipes Software (email: [email protected] or fax: 781 863-1739) for details.
About Copyrights on Computer Programs Like artistic or literary compositions, computer programs are protected by copyright. Generally it is an infringement for you to copy into your computer a program from a copyrighted source. (It is also not a friendly thing to do, since it deprives the program’s author of compensation for his or her creative effort.) Under copyright law, all “derivative works” (modified versions, or translations into another computer language) also come under the same copyright as the original work. Copyright does not protect ideas, but only the expression of those ideas in a particular form. In the case of a computer program, the ideas consist of the program’s methodology and algorithm, including the necessary sequence of steps adopted by the programmer. The expression of those ideas is the program source code (particularly any arbitrary or stylistic choices embodied in it), its derived object code, and any other derivative works. If you analyze the ideas contained in a program, and then express those ideas in your own completely different implementation, then that new program implementation belongs to you. That is what we have done for those programs in this book that are not entirely of our own devising. When programs in this book are said to be “based” on programs published in copyright sources, we mean that the ideas are the same. The expression of these ideas as source code is our own. We believe that no material in this book infringes on an existing copyright.
Trademarks Several registered trademarks appear within the text of this book: Sun is a trademark of Sun Microsystems, Inc. SPARC and SPARCstation are trademarks of SPARC International, Inc. Microsoft, Windows 95, Windows NT, PowerStation, and MS are trademarks of Microsoft Corporation. DEC, VMS, Alpha AXP, and ULTRIX are trademarks of Digital Equipment Corporation. IBM is a trademark of International Business Machines Corporation. Apple and Macintosh are trademarks of Apple Computer, Inc. UNIX is a trademark licensed exclusively through X/Open
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
• [“Course Right-to-Copy License”] Instructors at accredited educational institutions who have adopted this book for a course, and who have already purchased a Single Screen License (either acquired with the purchase of media, or from the Numerical Recipes On-Line Software Store), may license the programs for use in that course as follows: Mail your name, title, and address; the course name, number, dates, and estimated enrollment; and advance payment of $5 per (estimated) student to Numerical Recipes Software, at this address: P.O. Box 243, Cambridge, MA 02238 (USA). You will receive by return mail a license authorizing you to make copies of the programs for use by your students, and/or to transfer the programs to a machine accessible to your students (but only for the duration of the course).
xx
License Information
Co. Ltd. IMSL is a trademark of Visual Numerics, Inc. NAG refers to proprietary computer software of Numerical Algorithms Group (USA) Inc. PostScript and Adobe Illustrator are trademarks of Adobe Systems Incorporated. Last, and no doubt least, Numerical Recipes (when identifying products) is a trademark of Numerical Recipes Software.
The fact that ideas are legally “free as air” in no way supersedes the ethical requirement that ideas be credited to their known originators. When programs in this book are based on known sources, whether copyrighted or in the public domain, published or “handed-down,” we have attempted to give proper attribution. Unfortunately, the lineage of many programs in common circulation is often unclear. We would be grateful to readers for new or corrected information regarding attributions, which we will attempt to incorporate in subsequent printings.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Attributions
21.0 Introduction Fortran 90 is in many respects a backwards-compatible modernization of the long-used (and much abused) Fortran 77 language, but it is also, in other respects, a new language for parallel programming on present and future multiprocessor machines. These twin design goals of the language sometimes add confusion to the process of becoming fluent in Fortran 90 programming. In a certain trivial sense, Fortran 90 is strictly backwards-compatible with Fortran 77. That is, any Fortran 90 compiler is supposed to be able to compile any legacy Fortran 77 code without error. The reason for terming this compatibility trivial, however, is that you have to tell the compiler (usually via a source file name ending in “.f” or “.for”) that it is dealing with a Fortran 77 file. If you instead try to pass off Fortran 77 code as native Fortran 90 (e.g., by naming the source file something ending in “.f90”) it will not always work correctly! It is best, therefore, to approach Fortran 90 as a new computer language, albeit one with a lot in common with Fortran 77. Indeed, in such terms, Fortran 90 is a fairly big language, with a large number of new constructions and intrinsic functions. Here, in one short chapter, we do not pretend to provide a complete description of the language. Luckily, there are good books that do exactly that. Our favorite one is by Metcalf and Reid [1], cited throughout this chapter as “M&R.” Other good starting points include [2] and [3]. Our goal, in the remainder of this chapter, is to give a good, working description of those Fortran 90 language features that are not immediately self-explanatory to Fortran 77 programmers, with particular emphasis on those that occur most frequently in the Fortran 90 versions of the Numerical Recipes routines. This chapter, by itself, will not teach you to write Fortran 90 code. But it ought to help you acquire a reading knowledge of the language, and perhaps provide enough of a head start that you can rapidly pick up the rest of what you need to know from M&R or another Fortran 90 reference book. CITED REFERENCES AND FURTHER READING: Metcalf, M., and Reid, J. 1996, Fortran 90/95 Explained (Oxford: Oxford University Press). [1]
935
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter 21. Introduction to Fortran 90 Language Features
936
Chapter 21.
Introduction to Fortran 90 Language Features
Kerrigan, J.F. 1993, Migrating to Fortran 90 (Sebastopol, CA: O’Reilly). [2] Brainerd, W.S., Goldberg, C.H., and Adams, J.C. 1996, Programmer’s Guide to Fortran 90, 3rd ed. (New York: Springer-Verlag). [3]
This section is for people who want to jump right in. We’ll compute a Bessel function J 0 (x), where x is equal to the fourth root of the Julian Day number of the 200th full moon since January 1900. (Now there’s a useful quantity!) First, locate the important files nrtype.f90, nrutil.f90, and nr.f90, as listed in Appendices C1, C1, and C2, respectively. These contain modules that either are (i) used by our routines, or else (ii) describe the calling conventions of our routines to (your) user programs. Compile each of these files, producing (with most compilers) a .mod file and a .o (or similarly named) file for each one. Second, create this main program file: PROGRAM hello_bessel USE nrtype USE nr, ONLY: flmoon, bessj0 IMPLICIT NONE INTEGER(I4B) :: n=200,nph=2,jd REAL(SP) :: x,frac,ans call flmoon(n,nph,jd,frac) x=jd**0.25_sp ans=bessj0(x) write (*,*) ’Hello, Bessel: ’, ans END PROGRAM
Here is a quick explanation of some elements of the above program: The first USE statement includes a module of ours named nrtype, whose purpose is to give symbolic names to some kinds of data types, among them single-precision reals (“sp”) and four-byte integers (“i4b”). The second USE statement includes a module of ours that defines the calling sequences, and variable types, expected by (in this case) the Numerical Recipes routines flmoon and bessj0. The IMPLICIT NONE statement signals that we want the compiler to require us explicitly to declare all variable types. We strongly urge that you always take this option. The next two lines declare integer and real variables of the desired kinds. The variable n is initialized to the value 200, nph to 2 (a value expected by flmoon). We call flmoon, and take the fourth root of the answer it returns as jd. Note that the constant 0.25 is typed to be single-precision by the appended sp. We call the bessj0 routine, and print the answer.
Third, compile the main program file, and also the files flmoon.f90, bessj0.f90. Then, link the resulting object files with also nrutil.o (or similar system-dependent name, as produced in step 1). Some compilers will also require you to link with nr.o and nrtype.o. Fourth, run the resulting executable file. Typical output is: Hello, Bessel:
7.3096365E-02
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
21.1 Quick Start: Using the Fortran 90 Numerical Recipes Routines
21.2 Fortran 90 Language Concepts
937
21.2 Fortran 90 Language Concepts
Data Types and Kinds Data types (also called simply types) can be either intrinsic data types (the familiar INTEGER, REAL, LOGICAL, and so forth) or else derived data types that are built up in the manner of what are called “structures” or “records” in other computer languages. (We’ll use derived data types very sparingly in this book.) Intrinsic data types are further specified by their kind parameter (or simply kind), which is simply an integer. Thus, on many machines, REAL(4) (with kind = 4) is a single-precision real, while REAL(8) (with kind = 8) is a double-precision real. Literal constants (or simply literals) are specified as to kind by appending an underscore, as 1.5 4 for single precision, or 1.5 8 for double precision. [M&R, §2.5–§2.6] Unfortunately, the specific integer values that define the different kind types are not specified by the language, but can vary from machine to machine. For that reason, one almost never uses literal kind parameters like 4 or 8, but rather defines in some central file, and imports into all one’s programs, symbolic names for the kinds. For this book, that central file is the module named nrtype, and the chosen symbolic names include SP, DP (for reals); I2B, I4B (for two- and four-byte integers); and LGT for the default logical type. You will therefore see us consistently writing REAL(SP), or 1.5 sp, and so forth. Here is an example of declaring some variables, including a one-dimensional array of length 500, and a two-dimensional array with 100 rows and 200 columns: USE nrtype REAL(SP) :: x,y,z INTEGER(I4B) :: i,j,k REAL(SP), DIMENSION(500) :: arr REAL(SP), DIMENSION(100,200) :: barr REAL(SP) :: carr(500)
The last line shows an alternative form for array syntax. And yes, there are default kind parameters for each intrinsic type, but these vary from machine to machine and can get you into trouble when you try to move code. We therefore specify all kind parameters explicitly in almost all situations.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
The Fortran 90 language standard defines and uses a number of standard terms for concepts that occur in the language. Here we summarize briefly some of the most important concepts. Standard Fortran 90 terms are shown in italics. While by no means complete, the information in this section should help you get a quick start with your favorite Fortran 90 reference book or language manual. A note on capitalization: Outside a character context, Fortran 90 is not casesensitive, so you can use upper and lower case any way you want, to improve readability. A variable like SP (see below) is the same variable as the variable sp. We like to capitalize keywords whose use is primarily at compile-time (statements that delimit program and subprogram boundaries, declaration statements of variables, fixed parameter values), and use lower case for the bulk of run-time code. You can adopt any convention that you find helpful to your own programming style; but we strongly urge you to adopt and follow some convention.
938
Chapter 21.
Introduction to Fortran 90 Language Features
Array Shapes and Sizes
Memory Management Fortran 90 is greatly superior to Fortran 77 in its memory-management capabilities, seen by the user as the ability to create, expand, or contract workspace for programs. Within subprograms (that is, subroutines and functions), one can have automatic arrays (or other automatic data objects) that come into existence each time the subprogram is entered, and disappear (returning their memory to the pool) when the subprogram is exited. The size of automatic objects can be specified by arbitrary expressions involving values passed as actual arguments in the calling program, and thus received by the subprogram through its corresponding dummy arguments. [M&R, §6.4] Here is an example that creates some automatic workspace named carr: SUBROUTINE dosomething(j,k) USE nrtype REAL(SP), DIMENSION(2*j,k**2) :: carr
Finer control on when workspace is created or destroyed can be achieved by declaring allocatable arrays, which exist as names only, without associated memory, until they are allocated within the program or subprogram. When no longer needed, they can be deallocated. The allocation status of an allocatable array can be tested by the program via the allocated intrinsic function (p. 952). [M&R, §6.5] Here is an example in outline: REAL(SP), DIMENSION(:,:), ALLOCATABLE :: darr ... allocate(darr(10,20)) ... deallocate(darr) ... allocate(darr(100,200)) ... deallocate(darr)
Notice that darr is originally declared with only “slots” (colons) for its dimensions, and is then allocated/deallocated twice, with different sizes. Yet finer control is achieved by the use of pointers. Like an allocatable array, a pointer can be allocated, at will, its own associated memory. However, it has the additional flexibility of alternatively being pointer associated with a target that
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
The shape of an array refers to both its dimensionality (called its rank), and also the lengths along each dimension (called the extents). The shape of an array is specified by a rank-one array whose elements are the extents along each dimension, and can be queried with the shape intrinsic (see p. 949). Thus, in the above example, shape(barr) returns an array of length 2 containing the values (100, 200). The size of an array is its total number of elements, so the intrinsic size(barr) would return 20000 in the above example. More often one wants to know the extents along each dimension, separately: size(barr,1) returns the value 100, while size(barr,2) returns the value 200. [M&R, §2.10] Section §21.3, below, discusses additional aspects of arrays in Fortran 90.
21.2 Fortran 90 Language Concepts
939
already exists under another name. Thus, pointers can be used as redefinable aliases for other variables, arrays, or (see §21.3) array sections. [M&R, §6.12] Here is an example that first associates the pointer parr with the array earr, then later cancels that association and allocates it its own storage of size 50:
Procedure Interfaces When a procedure is referenced (e.g., called) from within a program or subprogram (examples of scoping units), the scoping unit must be told, or must deduce, the procedure’s interface, that is, its calling sequence, including the types and kinds of all dummy arguments, returned values, etc. The recommended procedure is to specify this interface via an explicit interface, usually an interface block (essentially a declaration statement for subprograms) in the calling subprogram or in some module that the calling program includes via a USE statement. In this book all interfaces are explicit, and the module named nr contains interface blocks for all of the Numerical Recipes routines. [M&R, §5.11] Here is a typical example of an interface block: INTERFACE SUBROUTINE caldat(julian,mm,id,iyyy) USE nrtype INTEGER(I4B), INTENT(IN) :: julian INTEGER(I4B), INTENT(OUT) :: mm,id,iyyy END SUBROUTINE caldat END INTERFACE
Once this interface is made known to a program that you are writing (by either explicit inclusion or a USE statement), then the compiler is able to flag for you a variety of otherwise difficult-to-find bugs. Although interface blocks can sometimes seem overly wordy, they give a big payoff in ultimately minimizing programmer time and frustration. For compatibility with Fortran 77, the language also allows for implicit interfaces, where the calling program tries to figure out the interface by the old rules of Fortran 77. These rules are quite limited, and prone to producing devilishly obscure program bugs. We strongly recommend that implicit interfaces never be used.
Elemental Procedures and Generic Interfaces Many intrinsic procedures (those defined by the language standard and thus usable without any further definition or specification) are also generic. This means that a single procedure name, such as log(x), can be used with a variety of types and kind parameters for the argument x, and the result returned will have the same type and kind parameter as the argument. In this example, log(x) allows any real or complex argument type.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
REAL(SP), DIMENSION(:), POINTER :: parr REAL(SP), DIMENSION(100), TARGET :: earr ... parr => earr ... nullify(parr) allocate(parr(50)) ... deallocate(parr)
940
Chapter 21.
Introduction to Fortran 90 Language Features
Better yet, most generic functions are also elemental. The argument of an elemental function can be an array of arbitrary shape! Then, the returned result is an array of the same shape, with each element containing the result of applying the function to the corresponding element of the argument. (Hence the name elemental, meaning “applied element by element.”) [M&R, §8.1] For example:
Fortran 90 has no facility for creating new, user-defined elemental functions. It does have, however, the related facility of overloading by the use of generic interfaces. This is invoked by the use of an interface block that attaches a single generic name to a number of distinct subprograms whose dummy arguments have different types or kinds. Then, when the generic name is referenced (e.g., called), the compiler chooses the specific subprogram that matches the types and kinds of the actual arguments used. [M&R, §5.18] Here is an example of a generic interface block: INTERFACE myfunc FUNCTION myfunc_single(x) USE nrtype REAL(SP) :: x,myfunc_single END FUNCTION myfunc_single FUNCTION myfunc_double(x) USE nrtype REAL(DP) :: x,myfunc_double END FUNCTION myfunc_double END INTERFACE
A program with knowledge of this interface could then freely use the function reference myfunc(x) for x’s of both type SP and type DP. We use overloading quite extensively in this book. A typical use is to provide, under the same name, both scalar and vector versions of a function such as a Bessel function, or to provide both single-precision and double-precision versions of procedures (as in the above example). Then, to the extent that we have provided all the versions that you need, you can pretend that our routine is elemental. In such a situation, if you ever call our function with a type or kind that we have not provided, the compiler will instantly flag the problem, because it is unable to resolve the generic interface.
Modules Modules, already referred to several times above, are Fortran 90’s generalization of Fortran 77’s common blocks, INCLUDEd files of parameter statements, and (to some extent) statement functions. Modules are program units, like main programs or subprograms (subroutines and functions), that can be separately compiled. A module is a convenient place to stash global data, named constants (what in Fortran 77 are called “symbolic constants” or “PARAMETERs”), interface blocks to subprograms and/or actual subprograms themselves (module subprograms). The convenience is that a module’s information can be incorporated into another program unit via a simple, one-line USE statement. [M&R, §5.5] Here is an example of a simple module that defines a few parameters, creates some global storage for an array named arr (as might be done with a Fortran 77 common block), and defines the interface to a function yourfunc:
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
REAL(SP), DIMENSION(100,100) :: a,b b=sin(a)
21.3 More on Arrays and Array Sections
941
As mentioned earlier, the module nr contains INTERFACE declarations for all the Numerical Recipes. When we include a statement of the form USE nr, ONLY: recipe1
it means that the program uses the additional routine recipe1. The compiler is able to use the explicit interface declaration in the module to check that recipe1 is invoked with arguments of the correct type, shape, and number. However, a weakness of Fortran 90 is that there is no fail-safe way to be sure that the interface module (here nr) stays synchronized with the underlying routine (here recipe1). You might think that you could accomplish this by putting USE nr, ONLY: recipe1 into the recipe1 program itself. Unfortunately, the compiler interprets this as an erroneous double definition of recipe1’s interface, rather than (as would be desirable) as an opportunity for a consistency check. (To achieve this kind of consistency check, you can put the procedures themselves, not just their interfaces, into the module.) CITED REFERENCES AND FURTHER READING: Metcalf, M., and Reid, J. 1996, Fortran 90/95 Explained (Oxford: Oxford University Press).
21.3 More on Arrays and Array Sections Arrays are the central conceptual core of Fortran 90 as a parallel programming language, and thus worthy of some further discussion. We have already seen that arrays can “come into existence” in Fortran 90 in several ways, either directly declared, as REAL(SP), DIMENSION(100,200) :: arr
or else allocated by an allocatable variable or a pointer variable, REAL(SP), DIMENSION(:,:), ALLOCATABLE :: arr REAL(SP), DIMENSION(:,:), POINTER :: barr ... allocate(arr(100,200),barr(100,200))
or else (not previously mentioned) passed into a subprogram through a dummy argument: SUBROUTINE myroutine(carr) USE nrtype REAL(SP), DIMENSION(:,:) :: carr ... i=size(carr,1)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
MODULE mymodule USE nrtype REAL(SP), PARAMETER :: con1=7.0_sp/3.0_sp,con2=10.0_sp INTEGER(I4B), PARAMETER :: ndim=10,mdim=9 REAL(SP), DIMENSION(ndim,mdim) :: arr INTERFACE FUNCTION yourfunc(x) USE nrtype REAL(SP) :: x,yourfunc END FUNCTION yourfunc END INTERFACE END MODULE mymodule
942
Chapter 21.
Introduction to Fortran 90 Language Features
j=size(carr,2)
PROGRAM use_myroutine USE nrtype REAL(SP), DIMENSION(10,10) :: arr INTERFACE SUBROUTINE myroutine(carr) USE nrtype REAL(SP), DIMENSION(:,:) :: carr END SUBROUTINE myroutine END INTERFACE ... call myroutine(a)
Of course, for the recipes we have provided all the interface blocks in the file nr.f90, and you need only a USE nr statement in your calling program.
Conformable Arrays Two arrays are said to be conformable if their shapes are the same. Fortran 90 allows practically all operations among conformable arrays and elemental functions that are allowed for scalar variables. Thus, if arr, barr, and carr are mutually conformable, we can write, arr=barr+cos(carr)+2.0_sp
and have the indicated operations performed, element by corresponding element, on the entire arrays. The above line also illustrates that a scalar (here the constant 2.0 sp, but a scalar variable would also be fine) is deemed conformable with any array — it gets “expanded” to the shape of the rest of the expression that it is in. [M&R, §3.11] In Fortran 90, as in Fortran 77, the default lower bound for an array subscript is 1; however, it can be made some other value at the time that the array is declared: REAL(SP), DIMENSION(100,200) :: farr REAL(SP), DIMENSION(0:99,0:199) :: garr ... farr = 3.0_sp*garr + 1.0_sp
Notice that farr and garr are conformable, since they have the same shape, in this case (100, 200). Also note that when they are used in an array expression, the operations are taken between the corresponding elements of their shapes, not necessarily the corresponding elements of their indices. [M&R, §3.10] In other words, one of the components evaluated is, farr(1,1) = 3.0_sp*garr(0,0) + 1.0_sp
This illustrates a fundamental aspect of array (or data) parallelism in Fortran 90. Array constructions should not be thought of as merely abbreviations for do-loops
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
In the above example we also show how the subprogram can find out the size of the actual array that is passed, using the size intrinsic. This routine is an example of the use of an assumed-shape array, new to Fortran 90. The actual extents along each dimension are inherited from the calling routine at run time. A subroutine with assumed-shape array arguments must have an explicit interface in the calling routine, otherwise the compiler doesn’t know about the extra information that must be passed. A typical setup for calling myroutine would be:
21.3 More on Arrays and Array Sections
943
where (harr > 0.0_sp) farr = 3.0_sp*garr + 1.0_sp end where
Here harr must also be conformable to farr and garr. Analogously with the Fortran if-statement, there is also a one-line form of the where-statement. There is also a where ... elsewhere ... end where form of the statement, analogous to if ... else if ... end if. A significant language limitation in Fortran 90 is that nested where-statements are not allowed. [M&R, §6.8]
Array Sections Much of the versatility of Fortran 90’s array facilities stems from the availability of array sections. An array section acts like an array, but its memory location, and thus the values of its elements, is actually a subset of the memory location of an already-declared array. Array sections are thus “windows into arrays,” and they can appear on either the left side, or the right side, or both, of a replacement statement. Some examples will clarify these ideas. Let us presume the declarations REAL(SP), DIMENSION(100) :: arr INTEGER(I4B), DIMENSION(6) :: iarr=(/11,22,33,44,55,66/)
Note that iarr is not only declared, it is also initialized by an initialization expression (a replacement for Fortran 77’s DATA statement). [M&R, §7.5] Here are some array sections constructed from these arrays: Array Section
What It Means
arr(:) arr(1:100) arr(1:10)
same as arr same as arr one-dimensional array containing first 10 elements of arr one-dimensional array containing second half of arr same as arr(51:100) one-dimensional array containing first 10 elements of arr, but in reverse order one-dimensional array containing elements 10, 99, 1, and 6 of arr, in that order one-dimensional array containing elements 11, 22, 33, 44, 55, 66 of arr, in that order
arr(51:100) arr(51:) arr(10:1:-1) arr( (/10,99,1,6/) )
arr(iarr)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
over indices, but rather as genuinely parallel operations on same-shaped objects, abstracted of their indices. This is why the standard makes no statement about the order in which the individual operations in an array expression are executed; they might in fact be carried out simultaneously, on parallel hardware. By default, array expressions and assignments are performed for all elements of the same-shaped arrays referenced. This can be modified, however, by use of a where construction like this:
944
Chapter 21.
Introduction to Fortran 90 Language Features
Now let’s try some array sections of the two-dimensional array REAL(SP), DIMENSION(100,100) :: barr
What It Means
barr(:,:) barr(1:100,1:100) barr(7,:)
same as barr same as barr one-dimensional array containing the 7th row of barr same as barr(7,:) one-dimensional array containing the 7th column of barr two-dimensional array containing the sub-block of barr with the indicated ranges of indices; the shape of this array section is (10, 20) two-dimensional array formed by flipping barr upside down and backwards two-dimensional array of shape (50, 50) containing the elements of barr whose row and column indices are both even
barr(7,1:100) barr(:,7) barr(21:30,71:90)
barr(100:1:-1,100:1:-1) barr(2:100:2,2:100:2)
Some terminology: A construction like 2:100:2, above, is called a subscript triplet. Its integer pieces (which may be integer constants, or more general integer expressions) are called lower, upper, and stride. Any of the three may be omitted. An omitted stride defaults to the value 1. Notice that, if (upper − lower) has a different sign from stride, then a subscript triplet defines an empty or zero-length array, e.g., 1:5:-1 or 10:1:1 (or its equivalent form, simply 10:1). Zero-length arrays are not treated as errors in Fortran 90, but rather as “no-ops.” That is, no operation is performed in an expression or replacement statement among zero-length arrays. (This is essentially the same convention as in Fortran 77 for do-loop indices, which array expressions often replace.) [M&R, §6.10] It is important to understand that array sections, when used in array expressions, match elements with other parts of the expression according to shape, not according to indices. (This is exactly the same principle that we applied, above, to arrays with subscript lower bounds different from the default value of 1.) One frequently exploits this feature in using array sections to carry out operations on arrays that access neighboring elements. For example, carr(1:n-1,1:n-1) = barr(1:n-1,1:n-1)+barr(2:n,2:n)
constructs in the (n − 1) × (n − 1) matrix carr the sum of each of the corresponding elements in n × n barr added to its diagonally lower-right neighbor. Pointers are often used as aliases for array sections, especially if the same array sections are used repeatedly. [M&R, §6.12] For example, with the setup REAL(SP), DIMENSION(:,:), POINTER :: leftb,rightb
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Array Section
21.4 Fortran 90 Intrinsic Procedures
945
leftb=>barr(1:n-1,1:n-1) rightb=>barr(2:n,2:n)
the statement above can be coded as carr(1:n-1,1:n-1)=leftb+rightb
CITED REFERENCES AND FURTHER READING: Metcalf, M., and Reid, J. 1996, Fortran 90/95 Explained (Oxford: Oxford University Press).
21.4 Fortran 90 Intrinsic Procedures Much of Fortran 90’s power, both for parallel programming and for its concise expression of algorithmic ideas, comes from its rich set of intrinsic procedures. These have the effect of making the language “large,” hence harder to learn. However, effort spent on learning to use the intrinsics — particularly some of their more obscure, and more powerful, optional arguments — is often handsomely repaid. This section summarizes the intrinsics that we find useful in numerical work. We omit, here, discussion of intrinsics whose exclusive use is for character and string manipulation. We intend only a summary, not a complete specification, which can be found in M&R’s Chapter 8, or other reference books. If you find the sheer number of new intrinsic procedures daunting, you might want to start with our list of the “top 10” (with the number of different Numerical Recipes routines that use each shown in parentheses): size (254), sum (44), dot product (31), merge (27), all (25), maxval (23), matmul (19), pack (18), any (17), and spread (15). (Later, in Chapter 23, you can compare these numbers with our frequency of using the short utility functions that we define in a module named nrutil — several of which we think ought to have been included as Fortran 90 intrinsic procedures.) The type, kind, and shape of the value returned by intrinsic functions will usually be clear from the short description that we give. As an additional hint (though not necessarily a precise description), we adopt the following codes:
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
We should also mention that array sections, while powerful and concise, are sometimes not quite powerful enough. While any row or column of a matrix is easily accessible as an array section, there is no good way, in Fortran 90, to access (e.g.) the diagonal of a matrix, even though its elements are related by a linear progression in the Fortran storage order (by columns). These so-called skew-sections were much discussed by the Fortran 90 standards committee, but they were not implemented. We will see examples later in this volume of work-around programming tricks (none totally satisfactory) for this omission. (Fortran 95 corrects the omission; see §21.6.)
946
Chapter 21.
Introduction to Fortran 90 Language Features
Hint [Int] [Real]
What It Means an INTEGER kind type a REAL kind type a COMPLEX kind type a numerical type and kind a LOGICAL kind type a one-dimensional INTEGER array
[argTS]
same type and shape as the first argument same type as the first argument, but not necessarily the same shape
[argT]
Numerical Elemental Functions Little needs to be said about the numerical functions with identical counterparts in Fortran 77: abs, acos, aimag, asin, atan, atan2, conjg, cos, cosh, dim, exp, log, log10, max, min, mod, sign, sin, sinh, sqrt, tan, and tanh. In Fortran 90 these are all elemental functions, so that any plausible type, kind, and shape of argument may be used. Except for aimag, which returns a real type from a complex argument, these all return [argTS] (see table above). Although Fortran 90 recognizes, for compatibility, Fortran 77’s so-called specific names for these functions (e.g., iabs, dabs, and cabs for the generic abs), these are entirely superfluous and should be avoided. Fortran 90 corrects some ambiguity (or at least inconvenience) in Fortran 77’s mod(a,p) function, by introducing a new function modulo(a,p). The functions are essentially identical for positive arguments, but for negative a and positive p, modulo gives results more compatible with one’s mathematical expectation that the answer should always be in the positive range 0 to p. E.g., modulo(11,5)=1, and modulo(-11,5)=4. [M&R, §8.3.2]
Conversion and Truncation Elemental Functions Fortran 90’s conversion (or, in the language of C, casting) and truncation functions are generally modeled on their Fortran 77 antecedents, but with the addition of an optional second integer argument, kind, that determines the kind of the result. Note that, if kind is omitted, you get a default kind — not necessarily related to the kind of your argument. The kind of the argument is of course known to the compiler by its previous declaration. Functions in this category (see below for explanation of arguments in slanted type) are: [Real]
aint(a,kind) Truncate to integer value, return as a real kind.
[Real]
anint(a,kind) Nearest whole number, return as a real kind.
[Cmplx] cmplx(x,y,kind)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
[Cmplx] [Num] [Lgcl] [Iarr]
21.4 Fortran 90 Intrinsic Procedures
947
Convert to complex kind. If y is omitted, it is taken to be 0. int(a,kind) Convert to integer kind, truncating towards zero.
[Int]
nint(a,kind) Convert to integer kind, choosing the nearest whole number.
[Real]
real(a,kind) Convert to real kind.
[Lgcl]
logical(a,kind) Convert one logical kind to another.
We must digress here to explain the use of optional arguments and keywords as Fortran 90 language features. [M&R, §5.13] When a routine (either intrinsic or user-defined) has arguments that are declared to be optional, then the dummy names given to them also become keywords that distinguish — independent of their position in a calling list — which argument is intended to be passed. (There are some additional rules about this that we will not try to summarize here.) In this section’s tabular listings, we indicate optional arguments in intrinsic routines by printing them in smaller slanted type. For example, the intrinsic function eoshift(array,shift,boundary,dim) has two required arguments, array and shift, and two optional arguments, boundary and dim. Suppose we want to call this routine with the actual arguments myarray, myshift, and mydim, but omitting the argument in the boundary slot. We do this by the expression eoshift(myarray,myshift,dim=mydim) Conversely, if we wanted a boundary argument, but no dim, we might write eoshift(myarray,myshift,boundary=myboundary) It is always a good idea to use this kind of keyword construction when invoking optional arguments, even though the rules allow keywords to be omitted in some unambiguous cases. Now back to the lists of intrinsic routines. A peculiarity of the real function derives from its use both as a type conversion and for extracting the real part of complex numbers (related, but not identical, usages): If the argument of real is complex, and kind is omitted, then the result isn’t a default real kind, but rather is (as one generally would want) the real kind type corresponding to the kind type of the complex argument, that is, single-precision real for single-precision complex, double-precision for double-precision, and so on. [M&R, §8.3.1] We recommend never using kind when you intend to extract the real part of a complex, and always using kind when you intend conversion of a real or integer value to a particular kind of REAL. (Use of the deprecated function dble is not recommended.) The last two conversion functions are the exception in that they don’t allow a kind argument, but rather return default integer kinds. (The X3J3 standards committee has fixed this in Fortran 95.) [Int]
ceiling(a) Convert to integer, truncating towards more positive.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
[Int]
948 [Int]
Chapter 21.
Introduction to Fortran 90 Language Features
floor(a) Convert to integer, truncating towards more negative.
Reduction and Inquiry Functions on Arrays
[Lgcl]
all(mask,dim) Returns true if all elements of mask are true, false otherwise.
[Lgcl]
any(mask,dim) Returns true if any of the elements of mask are true, false otherwise.
[Int]
count(mask,dim) Counts the true elements in mask.
[Num] maxval(array,dim,mask) Maximum value of the array elements. [Num] minval(array,dim,mask) Minimum value of the array elements. [Num] product(array,dim,mask) Product of the array elements. [Int]
size(array,dim) Size (total number of elements) of array, or its extent along dimension dim.
[Num] sum(array,dim,mask) Sum of the array elements. The use of the dim argument can be confusing, so an example may be helpful. Suppose we have 1 2 3 4 myarray = 5 6 7 8 9 10 11 12 where, as always, the i index in array(i,j) numbers the rows while the j index numbers the columns. Then sum(myarray,dim=1) = (15, 18, 21, 24) that is, the i indices are “summed away” leaving only a j index on the result; while sum(myarray,dim=2) = (10, 26, 42)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
These are mostly the so-called transformational functions that accept array arguments and return either scalar values or else arrays of lesser rank. [M&R, §8.11] With no optional arguments, such functions act on all the elements of their single array argument, regardless of its shape, and produce a scalar result. When the optional argument dim is specified, they instead act on all one-dimensional sections that span the dimension dim, producing an answer one rank lower than the first argument (that is, omitting the dim dimension from its shape). When the optional argument mask is specified, only the elements with a corresponding true value in mask are scanned.
21.4 Fortran 90 Intrinsic Procedures
949
that is, the j indices are “summed away” leaving only an i index on the result. Of course we also have sum(myarray) = 78
[Iarr]
maxloc(array,mask) Location of the maximum value in an array.
[Iarr]
minloc(array,mask) Location of the minimum value in an array. Similarly returning a one-dimensional integer array are
[Iarr]
shape(array) Returns the shape of array as a one-dimensional integer array.
[Iarr]
lbound(array,dim) When dim is absent, returns an array of lower bounds for each dimension of subscripts of array. When dim is present, returns the value only for dimension dim, as a scalar.
[Iarr]
ubound(array,dim) When dim is absent, returns an array of upper bounds for each dimension of subscripts of array. When dim is present, returns the value only for dimension dim, as a scalar.
Array Unary and Binary Functions The most powerful array operations are simply built into the language as operators. All the usual arithmetic and logical operators (+, -, *, /, **, .not., .and., .or., .eqv., .neqv.) can be applied to arrays of arbitrary shape or (for the binary operators) between two arrays of the same shape, or between arrays and scalars. The types of the arrays must, of course, be appropriate to the operator used. The result in all cases is to perform the operation element by element on the arrays. We also have the intrinsic functions, [Num] dot product(veca,vecb) Scalar dot product of two one-dimensional vectors veca and vecb. [Num] matmul(mata,matb) Result of matrix-multiplying the two two-dimensional matrices mata and matb. The shapes have to be such as to allow matrix multiplication. Vectors (one-dimensional arrays) are additionally allowed as either the first or second argument, but not both; they are treated as row vectors in the first argument, and as column vectors in the second. You might wonder how to form the outer product of two vectors, since matmul specifically excludes this case. (See §22.1 and §23.5 for answer.)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Two related functions return the location of particular elements in an array. The returned value is a one-dimensional integer array containing the respective subscript of the element along each dimension. Note that when the argument object is a one-dimensional array, the returned object is an integer array of length 1, not simply an integer. (Fortran 90 distinguishes between these.)
950
Chapter 21.
Introduction to Fortran 90 Language Features
Array Manipulation Functions These include many powerful features that a good Fortran 90 programmer should master.
[argTS] merge(tsource,fsource,mask) Returns same shape object as tsource and fsource containing the former’s components where mask is true, the latter’s where it is false. [argTS] eoshift(array,shift,boundary,dim) If dim is omitted, it is taken to be 1. Returns the result of end-off leftshifting every one-dimensional section of array (in dimension dim) by shift (which may be negative). That is, for positive shift, values are moved to smaller subscript positions. If boundary is present as a scalar, it supplies elements to fill in the blanks; if it is not present, zero values are used. Consult a Fortran 90 reference (e.g., [M&R, §8.13.5]) for the case where boundary and/or shift is an array. [argT]
pack(array,mask,vector) Returns a one-dimensional array containing the elements of array that pass the mask. Components of optional vector are used to pad out the result to the size of vector with specified values.
[argT]
reshape(source,shape,pad,order) Takes the elements of source, in normal Fortran order, and returns them (as many as will fit) as an array whose shape is specified by the one-dimensional integer array shape. If there is space remaining, then pad must be specified, and is used (as many sequential copies as necessary) to fill out the rest. For description of order, consult a Fortran 90 reference, e.g., [M&R, 8.13.3].
[argT]
spread(source,dim,ncopies) Returns an array whose rank is one greater than source, and whose dim dimension is of length ncopies. Each of the result’s ncopies array sections having a fixed subscript in dimension dim is a copy of source. (That is, it spreads source into the dimth dimension.)
[argT]
transpose(matrix) Returns the transpose of matrix, which must be two-dimensional.
[argT]
unpack(vector,mask,field) Returns an array whose type is that of vector, but whose shape is that of mask. The components of vector are put, in order, into the positions where mask is true. Where mask is false, components of field (which may be a scalar or an array with the same shape as mask) are used instead.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
[argTS] cshift(array,shift,dim) If dim is omitted, it is taken to be 1. Returns the result of circularly left-shifting every one-dimensional section of array (in dimension dim) by shift (which may be negative). That is, for positive shift, values are moved to smaller subscript positions. Consult a Fortran 90 reference (e.g., [M&R, §8.13.5]) for the case where shift is an array.
21.4 Fortran 90 Intrinsic Procedures
951
Bitwise Functions
[Int]
bit size(i) Number of bits in the integer type of i.
[Lgcl]
btest(i,pos) True if bit position pos is 1, false otherwise.
[Int]
iand(i,j) Bitwise logical and.
[Int]
ibclr(i,pos) Returns i but with bit position pos set to zero.
[Int]
ibits(i,pos,len) Extracts len consecutive bits starting at position pos and puts them in the low bit positions of the returned value. (The high positions are zero.)
[Int]
ibset(i,pos) Returns i but with bit position pos set to 1.
[Int]
ieor(i,j) Bitwise exclusive or.
[Int]
ior(i,j) Bitwise logical or.
[Int]
ishft(i,shift) Bitwise left shift by shift (which may be negative) with zeros shifted in from the other end.
[Int]
ishftc(i,shift) Bitwise circularly left shift by shift (which may be negative).
[Int]
not(i) Bitwise logical complement.
Some Functions Relating to Numerical Representations [Real]
epsilon(x) Smallest nonnegligible quantity relative to 1 in the numerical model of x.
[Num] huge(x) Largest representable number in the numerical model of x. [Int]
kind(x)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Most of the bitwise functions should be familiar to Fortran 77 programmers as longstanding standard extensions of that language. Note that the bit positions number from zero to one less than the value returned by the bit size function. Also note that bit positions number from right to left. Except for bit size, the following functions are all elemental.
952
Chapter 21.
Introduction to Fortran 90 Language Features
Returns the kind value for the numerical model of x. nearest(x,s) Real number nearest to x in the direction specified by the sign of s.
[Real]
tiny(x) Smallest positive number in the numerical model of x.
Other Intrinsic Procedures [Lgcl]
present(a) True, within a subprogram, if an optional argument is actually present, otherwise false.
[Lgcl]
associated(pointer,target) True if pointer is associated with target or (if target is absent) with any target, otherwise false.
[Lgcl]
allocated(array) True if the allocatable array is allocated, otherwise false. There are some pitfalls in using associated and allocated, having to do with arrays and pointers that can find themselves in undefined status [see §21.5, and also M&R, §3.3 and §6.5.1]. For example, pointers are always “born” in an undefined status, where the associated function returns unpredictable values. For completeness, here is a list of Fortran 90’s intrinsic procedures not already mentioned: Other Numerical Representation Functions: digits, exponent, fraction, rrspacing, scale, set exponent, spacing, maxexponent, minexponent, precision, radix, range, selected int kind, selected real kind. Lexical comparison: lge, lgt, lle, llt. Character functions: ichar, char, achar, iachar, index, adjustl, adjustr, len trim, repeat, scan, trim, verify. Other: mvbits, transfer, date and time, system clock, random seed, random number. (We will discuss random numbers in some detail in Chapter B7.) CITED REFERENCES AND FURTHER READING: Metcalf, M., and Reid, J. 1996, Fortran 90/95 Explained (Oxford: Oxford University Press).
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
[Real]
21.5 Advanced Fortran 90 Topics
953
21.5 Advanced Fortran 90 Topics Pointers, Arrays, and Memory Management Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
One of the biggest improvements in Fortran 90 over Fortran 77 is in the handling of arrays, which are the cornerstone of many numerical algorithms. In this subsection we will take a closer look at how to use some of these new array features effectively. We will look at how to code certain commonly occurring elements of program design, and we will pay particular attention to avoiding “memory leaks,” where — usually inadvertently — we keep cumulatively allocating new storage for an array, every time some piece of code is invoked. Let’s first review some of the rules for using allocatable arrays and pointers to arrays. Recall that a pointer is born with an undefined status. Its status changes to “associated” when you make it refer to a target, and to “disassociated” when you nullify the pointer. [M&R, §3.3] You can also use nullify on a newly born pointer to change its status from undefined to disassociated; this allows you to test the status with the associated inquiry function. [M&R, §6.5.4] (While many compilers will not produce a run-time error if you test an undefined pointer with associated, you can’t rely on this laissez-faire in your programming.) The initial status of an allocatable array is “not currently allocated.” Its status changes to “allocated” when you give it storage with allocate, and back to “not currently allocated” when you use deallocate. [M&R, §6.5.1] You can test the status with the allocated inquiry function. Note that while you can also give a pointer fresh storage with allocate, you can’t test this with allocated — only associated is allowed with pointers. Note also that nullifying an allocated pointer leaves its associated storage in limbo. You must instead deallocate, which gives the pointer a testable “disassociated” status. While allocating an array that is already allocated gives an error, you are allowed to allocate a pointer that already has a target. This breaks the old association, and could leave the old target inaccessible if there is no other pointer associated with it. [M&R, §6.5.2] Deallocating an array or pointer that has not been allocated is always an error. Allocated arrays that are local to a subprogram acquire the “undefined” status on exit from the subprogram unless they have the SAVE attribute. (Again, not all compilers enforce this, but be warned!) Such undefined arrays cannot be referenced in any way, so you should explicitly deallocate all allocated arrays that are not saved before returning from a subprogram. [M&R, §6.5.1] The same rule applies to arrays declared in modules that are currently accessed only by the subprogram. While you can reference undefined pointers (e.g., by first nullifying them), it is good programming practice to deallocate explicitly any allocated pointers declared locally before leaving a subprogram or module. Now let’s turn to using these features in programs. The simplest example is when we want to implement global storage of an array that needs to be accessed by two or more different routines, and we want the size of the array to be determined at run time. As mentioned earlier, we implement global storage with a MODULE rather than a COMMON block. (We ignore here the additional possibility of passing
954
Chapter 21.
Introduction to Fortran 90 Language Features
global variables by having one routine CONTAINed within the other.) There are two good ways of handling the dynamical allocation in a MODULE. Method 1 uses an allocatable array: MODULE a REAL(SP), DIMENSION(:), ALLOCATABLE :: x END MODULE a
Here the global variable x gets assigned storage in subroutine b (in this case, the same as the length of y). The length of y is of course defined in the procedure that calls b. The array x is made available to any other subroutine called by b by including a USE a statement. The status of x can be checked with an allocated inquiry function on entry into either b or the other subroutine if necessary. As discussed above, you must be sure to deallocate x before returning from subroutine b. If you want x to retain its values between calls to b, you add the SAVE attribute to its declaration in a, and don’t deallocate it on returning from b. (Alternatively, you could put a USE a in your main program, but we consider that bug-prone, since forgetting to do so can create all manner of difficult-to-diagnose havoc.) To avoid allocating x more than once, you test it on entry into b: if (.not. allocated(x)) allocate(x(size(y)))
The second way to implement this type of global storage (Method 2) uses a pointer: MODULE a REAL(SP), DIMENSION(:), POINTER :: x END MODULE a SUBROUTINE b(y) USE a REAL(SP), DIMENSION(:) :: y REAL(SP), DIMENSION(size(y)), TARGET :: xx ... x=>xx ... [other routines using x called here] ... END SUBROUTINE b
Here the automatic array xx gets its temporary storage automatically on entry into b, and automatically gets deallocated on exit from b. [M&R, §6.4] The global pointer x can access this storage in any routine with a USE a that is called by b. You can check that things are in order in such a called routine by testing x with associated. If you are going to use x for some other purpose as well, you should nullify it on leaving b so that it doesn’t have undefined status. Note that this implementation does not allow values to be saved between calls: You can’t SAVE automatic arrays — that’s not what they’re for. You would have to SAVE x in the module, and allocate it in the subroutine instead of pointing it to a suitable automatic array. But this is essentially Method 1 with the added complication of using a pointer, so Method 1 is simpler when you want to save values. When you don’t
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE b(y) USE a REAL(SP), DIMENSION(:) :: y ... allocate(x(size(y))) ... [other routines using x called here] ... END SUBROUTINE b
21.5 Advanced Fortran 90 Topics
955
SUBROUTINE a USE nrutil, ONLY : reallocate REAL(SP), DIMENSION(:), POINTER, SAVE :: wksp LOGICAL(LGT), SAVE :: init=.true. if (init) then init=.false. nullify(wksp) wksp=>reallocate(wksp,100) end if ... if (nterm > size(wksp)) wksp=>reallocate(wksp,2*size(wksp)) ... END SUBROUTINE a
Here the workspace is initially allocated a size of 100. If the number of elements used (nterm) ever exceeds the size of the workspace, the workspace is doubled. (In a realistic example, one would of course check that the doubled size is in fact big enough.) Fortran 90 experts can note that the SAVE on init is not strictly necessary: Any local variable that is initialized is automatically saved. [M&R, §7.5] You can find similar examples of reallocate (with some further discussion) in eulsum (p. 1070), hufenc (p. 1348), and arcode (p. 1350). Examples of reallocate used with global variables in modules are in odeint (p. 1300) and ran state (p. 1144). Another situation where we have to use pointers and not allocatable arrays is when the storage is required for components of a derived type, which are not allowed to have the allocatable attribute. Examples are in hufmak (p. 1346) and arcmak (p. 1349). Turning away from issues relating to global variables, we now consider several other important programming situations that are nicely handled with pointers. The first case is when we want a subroutine to return an array whose size is not known in advance. Since dummy arguments are not allocatable, we must use a pointer. Here is the basic construction: SUBROUTINE a(x,nx) REAL(SP), DIMENSION(:), POINTER :: x INTEGER(I4B), INTENT(OUT) :: nx LOGICAL(LGT), SAVE :: init=.true. if (init) then
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
need to save values between calls, we lean towards Method 2 over Method 1 because we like the automatic allocation and deallocation, but either method works fine. An example of Method 1 (allocatable array) is in routine rkdumb on page 1297. An example of Method 1 with SAVE is in routine pwtset on p. 1265. Method 2 (pointer) shows up in routines newt (p. 1196), broydn (p. 1199), and fitexy (p. 1286). A variation is shown in routines linmin (p. 1211) and dlinmin (p. 1212): When the array that needs to be shared is an argument of one of the routines, Method 2 is better. An extension of these ideas occurs if we allocate some storage for an array initially, but then might need to increase the size of the array later without losing the already-stored values. The function reallocate in our utility module nrutil will handle this for you, but it expects a pointer argument as in Method 2. Since no automatic arrays are used, you are free to SAVE the pointer if necessary. Here is a simple example of how to use reallocate to create a workspace array that is local to a subroutine:
956
Chapter 21.
Introduction to Fortran 90 Language Features
Since the length of x can be found from size(x), it is not absolutely necessary to pass nx as an argument. Note the use of the initial logic to avoid memory leaks. If a higher-level subroutine wants to recover the memory associated with x from the last call to SUBROUTINE a, it can do so by first deallocating it, and then nullifying the pointer. Examples of this structure are in zbrak (p. 1184), period (p. 1258), and fasper (p. 1259). A related situation is where we want a function to return an array whose size is not predetermined, such as in voltra on (p. 1326). The discussion of voltra also explains the potential pitfalls of functions returning pointers to dynamically allocated arrays. A final useful pointer construction enables us to set up a data structure that is essentially an array of arrays, independently allocatable on each part. We are not allowed to declare an array of pointers in Fortran 90, but we can do this indirectly by defining a derived type that consists of a pointer to the appropriate kind of array. [M&R, §6.11] We can then define a variable that is an allocatable array of the new type. For example, TYPE ptr_to_arr REAL(SP), DIMENSION(:), POINTER :: arr END TYPE TYPE(ptr_to_arr), DIMENSION(:), ALLOCATABLE :: x ... allocate(x(n)) ... do i=1,n allocate(x(i)%arr(m)) end do
sets up a set x of n arrays of length m. See also the example in mglin (p. 1334). There is a potential problem with dynamical memory allocation that we should mention. The Fortran 90 standard does not require that the compiler perform “garbage collection,” that is, it is not required to recover deallocated memory into nice contiguous pieces for reuse. If you enter and exit a subroutine many times, and each time a large chunk of memory gets allocated and deallocated, you could run out of memory with a “dumb” compiler. You can often alleviate the problem by deallocating variables in the reverse order that you allocated them. This tends to keep a large contiguous piece of memory free at the top of the heap.
Scope, Visibility, and Data Hiding An important principle of good programming practice is modularization, the idea that different parts of a program should be insulated from each other as much as possible. An important subcase of modularization is data hiding, the principle that actions carried out on variables in one part of the code should not be able to
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
init=.false. nullify(x) else if (associated(x)) deallocate(x) end if ... nx=... allocate(x(nx)) x(1:nx)=... END SUBROUTINE a
21.5 Advanced Fortran 90 Topics
957
SUBROUTINE recipe(arg) REAL(SP) :: arg REAL(SP) :: global_var call recipe_private CONTAINS
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
affect the values of variables in other parts of the code. When it is necessary for one “island” of code to communicate with another, the communication should be through a well-defined interface that makes it obvious exactly what communication is taking place, and prevents any other interchange from occurring. Otherwise, different sections of code should not have access to variables that they don’t need. The concept of data hiding extends not only to variables, but also to the names of procedures that manipulate the variables: A program for screen graphics might give the user access to a routine for drawing a circle, but it might “hide” the names (and methods of operation) of the primitive routines used for calculating the coordinates of the points on the circumference. Besides producing code that is easier to understand and to modify, data hiding prevents unintended side effects from producing hard-to-find errors. In Fortran, the principal language construction that effects data hiding is the use of subroutines. If all subprograms were restricted to have no more than ten executable statements per routine, and to communicate between routines only by an explicit list of arguments, the number of programming errors might be greatly reduced! Unfortunately few tasks can be easily coded in this style. For this and other reasons, we think that too much procedurization is a bad thing; one wants to find the right amount. Fortunately Fortran 90 provides several additional tools to help with data hiding. Global variables and routine names are important, but potentially dangerous, things. In Fortran 90, global variables are typically encapsulated in modules. Access is granted only to routines with an appropriate USE statement, and can be restricted to specific identifiers by the ONLY option. [M&R, §7.10] In addition, variable and routine names within the module can be designated as PUBLIC or PRIVATE (see, e.g., quad3d on p. 1065). [M&R, §7.6] The other way global variables get communicated is by having one routine CONTAINed within another. [M&R, §5.6] This usage is potentially lethal, however, because all the outer routine’s variables are visible to the inner routine. You can try to control the problem somewhat by passing some variables back and forth as arguments of the inner routine, but that still doesn’t prevent inadvertent side effects. (The most common, and most stupid, is inadvertent reuse of variables named i or j in the CONTAINed routine.) Also, a long list of arguments reduces the convenience of using an internal routine in the first place. We advise that internal subprograms be used with caution, and only to carry out simple tasks. There are some good ways to use CONTAINS, however. Several of our recipes have the following structure: A principal routine is invoked with several arguments. It calls a subsidiary routine, which needs to know some of the principal routine’s arguments, some global variables, and some values communicated directly as arguments to the subsidiary routine. In Fortran 77, we have usually coded this by passing the global variables in a COMMON block and all other variables as arguments to the subsidiary routine. If necessary, we copied the arguments of the primary routine before passing them to the subsidiary routine. In Fortran 90, there is a more elegant way of accomplishing this, as follows:
958
Chapter 21.
Introduction to Fortran 90 Language Features
SUBROUTINE recipe_private ... call subsidiary(local_arg) ... END SUBROUTINE recipe_private
Notice that the principal routine (recipe) has practically nothing in it — only declarations of variables intended to be visible to the subsidiary routine (subsidiary). All the real work of recipe is done in recipe private. This latter routine has visibility on all of recipe’s variables, while any additional variables that recipe private defines are not visible to subsidiary — which is the whole purpose of this way of organizing things. Obviously arg and global var can be much more general data types than the example shown here, including function names. For examples of this construction, see amoeba (p. 1208), amebsa (p. 1222), mrqmin (p. 1292), and medfit (p. 1294).
Recursion A subprogram is recursive if it calls itself. While forbidden in Fortran 77, recursion is allowed in Fortran 90. [M&R, §5.16–§5.17] You must supply the keyword RECURSIVE in front of the FUNCTION or SUBROUTINE keyword. In addition, if a FUNCTION calls itself directly, as opposed to calling another subprogram that in turn calls it, you must supply a variable to hold the result with the RESULT keyword. Typical syntax for this case is: RECURSIVE FUNCTION f(x) RESULT(g) REAL(SP) :: x,g if ... g=... else g=f(...) end if END FUNCTION f
When a function calls itself directly, as in this example, there always has to be a “base case” that does not call the function; otherwise the recursion never terminates. We have indicated this schematically with the if...else...end if structure. On serial machines we tend to avoid recursive implementations because of the additional overhead they incur at execution time. Occasionally there are algorithms for which the recursion overhead is relatively small, and the recursive implementation is simpler than an iterative version. Examples in this book are quad 3d (p. 1065), miser (p. 1164), and mglin (p. 1334). Recursion is much more important when parallelization is the goal. We will encounter in Chapter 22 numerous examples of algorithms that can be parallelized with recursion.
SAVE Usage Style A quirk of Fortran 90 is that any variable with initial values acquires the SAVE attribute automatically. [M&R, §7.5 and §7.9] As a help to understanding
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE subsidiary(local_arg) ... END SUBROUTINE subsidiary END SUBROUTINE recipe
21.6 And Coming Soon: Fortran 95
959
Named Control Structures Fortran 90 allows control structures such as do loops and if blocks to be named. [M&R, §4.3–§4.5] Typical syntax is name:do i=1,n ... end do name
One use of naming control structures is to improve readability of the code, especially when there are many levels of nested loops and if blocks. A more important use is to allow exit and cycle statements, which normally refer to the innermost do loop in which they are contained, to transfer execution to the end of some outer loop. This is effected by adding the name of the outer loop to the statement: exit name or cycle name. There is great potential for misuse with named control structures, since they share some features of the much-maligned goto. We recommend that you use them sparingly. For a good example of their use, contrast the Fortran 77 version of simplx with the Fortran 90 version on p. 1216. CITED REFERENCES AND FURTHER READING: Metcalf, M., and Reid, J. 1996, Fortran 90/95 Explained (Oxford: Oxford University Press).
21.6 And Coming Soon: Fortran 95 One of the more positive effects of Fortran 90’s long gestation period has been the general recognition, both by the X3J3 committee and by the community at large, that Fortran needs to evolve over time. Indeed, as we write, the process of bringing forth a minor, but by no means insignificant, updating of Fortran 90 — named Fortran 95 — is well under way. Fortran 95 will differ from Fortran 90 in about a dozen features, only a handful of which are of any importance to this book. Generally these are extensions that will make programming, especially parallel programming, easier. In this section we give a summary of the anticipated language changes. In §22.1 and §22.5 we will comment further on the implications of Fortran 95 to some parallel programming tasks; in §23.7 we comment on what differences Fortran 95 will make to our nrutil utility functions. No programs in Chapters B1 through B20 of this book edition use any Fortran 95 extensions.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
an algorithm, we have elected to put an explicit SAVE on all variables that really do need to retain their values between calls to a routine. We do this even if it is redundant because the variables are initialized. Note that we generally prefer to assign initial values with initialization expressions rather than with DATA statements. We reserve DATA statements for cases where it is convenient to use the repeat count feature to set multiple occurrences of a value, or when binary, octal, or hexadecimal constants are used. [M&R, §2.6.1]
960
Chapter 21.
Introduction to Fortran 90 Language Features
FORALL Statements and Blocks
forall (i=1:20, j=1:10:2) x(i,j)=y(j,i)
The block form allows more than one executable statement: forall (i=1:20, j=1:10:2) x(i,j)=y(j,i) z(i,j)=y(i,j)**2 end forall
Here is an example that cannot be done with Fortran 90 array sections: forall (i=1:20, j=1:20) a(i,j)=3*i+j**2
forall statements can also take optional masks that restrict their action to a subset of the loop index combinations: forall (i=1:100, j=1:100, (i>=j .and. x(i,j)/=0.0) ) x(i,j)=1.0/x(i,j)
forall constructions can be nested, or nested inside where blocks, or have where constructions inside them. An additional new feature in Fortran 95 is that where blocks can themselves be nested.
PURE Procedures Because the inside iteration of a forall block can be done in any order, or in parallel, there is a logical difficulty in allowing functions or subroutines inside such blocks: If the function or subroutine has side effects (that is, if it changes any data elsewhere in the machine, or in its own saved variables) then the result of a forall calculation could depend on the order in which the iterations happen to be done. This can’t be tolerated, of course; hence a new PURE attribute for subprograms. While the exact stipulations are somewhat technical, the basic idea is that if you declare a function or subroutine as PURE, with a syntax like, PURE FUNCTION myfunc(x,y,z)
or PURE SUBROUTINE mysub(x,y,z)
then you are guaranteeing to the compiler (and it will enforce) that the only values changed by mysub or myfunc are returned function values, subroutine arguments with the INTENT(OUT) attribute, and automatic (scratch) variables within the procedure. You can then use your pure procedures within forall constructions. Pure functions are also allowed in some specification statements.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Fortran 95 introduces a new forall control structure, somewhat akin to the where construct, but allowing for greater flexibility. It is something like a do-loop, but with the proviso that the indices looped over are allowed to be done in any order (ideally, in parallel). The forall construction comes in both single-statement and block variants. Instead of using the do-loop’s comma-separated triplets of lowervalue, upper-value, and increment, it borrows its syntax from the colon-separated form of array sections. Some examples will give you the idea. Here is a simple example that could alternatively be done with Fortran 90’s array sections and transpose intrinsic:
21.6 And Coming Soon: Fortran 95
961
ELEMENTAL Procedures
ELEMENTAL FUNCTION myfunc(x,y,z) REAL :: x,y,z,myfunc ... myfunc = ... END
In a program with an explicit interface for myfunc you could now have REAL, DIMENSION(10,20) :: x,y,z,w ... w=myfunc(x,y,z)
Pointer and Allocatable Improvements Fortran 95, unlike Fortran 90, requires that any allocatable variables (except those with SAVE attributes) that are allocated within a subprogram be automatically deallocated by the compiler when the subprogram is exited. This will remove Fortran 90’s “undefined allocation status” bugaboo. Fortran 95 also provides a method for pointer variables to be born with disassociated association status, instead of the default (and often inconvenient) “undefined” status. The syntax is to add an initializing => NULL() to the declaration, as: REAL, DIMENSION(:,:), POINTER :: mypoint => NULL()
This does not, however, eliminate the possibility of undefined association status, because you have to remember to use the null initializer if want your pointer to be disassociated.
Some Other Fortran 95 Features In Fortran 95, maxloc and minloc have the additional optional argument DIM, which causes them to act on all one-dimensional sections that span through the named dimension. This provides a means for getting the locations of the values returned by the corresponding functions maxval and minval in the case that their DIM argument is present. The sign intrinsic can now distinguish a negative from a positive real zero value: sign(2.0,-0.0) is −2.0. There is a new intrinsic subroutine cpu time(time) that returns as a real value time a process’s elapsed CPU time. There are some minor changes in the namelist facility, in defining minimum field widths for the I, B, O, Z, and F edit descriptors, and in resolving minor conflicts with some other standards.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Fortran 95 removes Fortran 90’s nagging restriction that only intrinsic functions are elemental. The way this works is that you write a pure procedure that operates on scalar values, but include the attribute ELEMENTAL (which automatically implies PURE). Then, as long as the function has an explicit interface in the referencing program, you can call it with any shape of argument, and it will act elementally. Here’s an example:
22.0 Why Think Parallel? In recent years we Numerical Recipes authors have increasingly become convinced that a certain revolution, cryptically denoted by the words “parallel programming,” is about to burst forth from its gestation and adolescence in the community of supercomputer users, and become the mainstream methodology for all computing. Let’s review the past: Take a screwdriver and open up the computer (workstation or PC) that sits on your desk. (Don’t blame us if this voids your warranty; and be sure to unplug it first!) Count the integrated circuits — just the bigger ones, with more than a million gates (transistors). As we write, in 1995, even lowly memory chips have one or four million gates, and this number will increase rapidly in coming years. You’ll probably count at least dozens, and often hundreds, of such chips in your computer. Next ask, how many of these chips are CPUs? That is, how many implement von Neumann processors capable of executing arbitrary, stored program code? For most computers, in 1995, the answer is: about one. A significant number of computers do have secondary processors that offload input-output and/or video functions. So, two or three is often a more accurate answer, but only one is usually under the user’s direct control. Why do our desktop computers have dozens or hundreds of memory chips, but most often only one (user-accessible) CPU? Do CPU chips intrinsically cost more to manufacture? No. Are CPU chips more expensive than memory chips? Yes, primarily because fixed development and design costs must be distributed over a smaller number of units sold. We have been in a kind of economic equilibrium: CPU’s are relatively expensive because there is only one per computer; and there is only one per computer, because they are relatively expensive. Stabilizing this equilibrium has been the fact that there has been no standard, or widely taught, methodology for parallel programming. Except for the special case of scientific computing on supercomputers (where large problems often have a regular or geometric character), it is not too much of an exaggeration to say that nobody really knows how to program multiprocessor machines. Symmetric multiprocessor 962
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter 22. Introduction to Parallel Programming
22.0 Why Think Parallel?
963
Thinking Parallel with Fortran 90 Until very recently, a strong inhibition to thinking parallel was the lack of any standard, architecture-independent, computer language in which to think. That has changed with the finalization of the Fortran 90 language standard, and with the availability of good, optimizing Fortran 90 compilers on a variety of platforms. There is a significant body of opinion (with which we, however, disagree) that there is no such thing as architecture-independent parallel programming. Proponents of this view, who are generally committed wizards at programming on one or another particular architecture, point to the fact that algorithms that are optimized to one architecture can run hundreds of times more slowly on other architectures. And, they are correct! Our opposing point of view is one of pragmatism. We think that it is not hard to learn, in a general way, what kinds of architectures are in general use, and what kinds of parallel constructions work well (or poorly) on each kind. With this knowledge (much of which we hope to develop in this book) the user can, we think, write good, general-purpose parallel code that works on a variety of architectures — including, importantly, on purely serial machines. Equally important, the user will be aware of when certain parts of a code can be significantly improved on some, but not other, architectures. Fortran 90 is a good test-bench for this point of view. It is not the perfect language for parallel programming. But it is a language, and it is the only
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
operating systems, for example, have been very slow in developing; and efficient, parallel methodologies for query-serving on large databases are even now a subject of continuing research. However, things are now changing. We consider it an easy prognostication that, by the first years of the new century, the typical desktop computer will have 4 to 8 user-accessible CPUs; ten years after that, the typical number will be between 16 and 512. It is not coincidence that these numbers are characteristic of supercomputers (including some quite different architectures) in 1995. The rough rule of ten years’ lag from supercomputer to desktop has held firm for quite some time now. Scientists and engineers have the advantage that techniques for parallel computation in their disciplines have already been developed. With multiprocessor workstations right around the corner, we think that now is the right time for scientists and engineers who use computers to start thinking parallel. We don’t mean that you should put an axe through the screen of your fast serial (single-CPU) workstation. We do mean, however, that you should start programming somewhat differently on that workstation, indeed, start thinking a bit differently about the way that you approach numerical problems in general. In this volume of Numerical Recipes in Fortran, our pedagogical goal is to show you that there are conceptual and practical benefits in parallel thinking, even if you are using a serial machine today. These benefits include conciseness and clarity of code, reusability of code in wider contexts, and (not insignificantly) increased portability of code to today’s parallel supercomputers. Of course, on parallel machines, either supercomputers today or desktop machines tomorrow, the benefits of thinking parallel are much more tangible: They translate into significant improvements in efficiency and computational capability.
964
Chapter 22.
Introduction to Parallel Programming
CITED REFERENCES AND FURTHER READING: Metcalf, M., and Reid, J. 1996, Fortran 90/95 Explained (Oxford: Oxford University Press).
22.1 Fortran 90 Data Parallelism: Arrays and Intrinsics The underlying model for parallel computation in Fortran 90 is data parallelism, implemented by the use of arrays of data, and by the provision of operations and intrinsic functions that act on those arrays in parallel, in a manner optimized by the compiler for each particular hardware architecture. We will not try to draw a fine definitional distinction between “data parallelism” and so-called SIMD (single instruction multiple data) programming. For our purposes the two terms mean about the same thing: The programmer writes a single operation, “+” say, and the compiler causes it to be carried out on multiple pieces of data in as parallel a manner as the underlying hardware allows. Any kind of parallel computing that is not SIMD is generally called MIMD (multiple instruction multiple data). A parallel programming language with MIMD features might allow, for example, several different subroutines — acting on different parts of the data — to be called into execution simultaneously. Fortran 90 has few, if any, MIMD constructions. A Fortran 90 compiler might, on some machines, execute MIMD code in implementing some Fortran 90 intrinsic functions (pack or unpack, e.g.), but this will be hidden from the Fortran 90 user. Some extensions of Fortran 90, like HPF, do implement MIMD features explicitly; but we will not consider these in this book. Fortran 95’s forall and PURE extensions (see §21.6) will allow some significantly greater access to MIMD features (see §22.5).
Array Parallel Operations We have already met the most basic, and most important, parallel facility of Fortran 90, namely, the ability to use whole arrays in expressions and assignments, with the indicated operations being effected in parallel across the array. Suppose, for example, we have the two-dimensional matrices a, b, and c, REAL, DIMENSION(30,30) :: a,b,c
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
cross-platform standard language now available. The committee that developed the language between 1978 and 1991 (known technically as X3J3) had strong representation from both a traditional “vectorization” viewpoint (e.g., from the Cray XMP and YMP series of computers), and also from the “data parallel” or “SIMD” viewpoints of parallel machines like the CM-2 and CM-5 from Thinking Machines, Inc. Language compromises were made, and a few (in our view) almost essential features were left out (see §22.5). But, by and large, the necessary tools are there: If you learn to think parallel in Fortran 90, you will easily be able to transfer the skill to future parallel standards, whether they are Fortran-based, C-based, or other.
965
22.1 Fortran 90 Data Parallelism: Arrays and Intrinsics
Then, instead of the serial construction, do j=1,30 do k=1,30 c(j,k)=a(j,k)+b(j,k) end do end do
c=a+b
The compiler deduces from the declaration statement that a, b, and c are matrices, and what their bounding dimensions are. Let us dwell for a moment on the conceptual differences between the serial code and parallel code for the above matrix addition. Although one is perhaps used to seeing the nested do-loops as simply an idiom for “do-the-enclosed-on-allcomponents,” it in fact, according to the rules of Fortran, specifies a very particular time-ordering for the desired operations. The matrix elements are added by rows, in order (j=1,30), and within each row, by columns, in order (k=1,30). In fact, the serial code above overspecifies the desired task, since it is guaranteed by the laws of mathematics that the order in which the element operations are done is of no possible relevance. Over the 50 year lifetime of serial von Neuman computers, we programmers have been brainwashed to break up all problems into single executable streams in the time dimension only. Indeed, the major design problem for supercomputer compilers for the last 20 years has been to undo such serial constructions and recover the underlying “parallel thoughts,” for execution in vector or parallel processors. Now, rather than taking this expensive detour into and out of serial-land, we are asked simply to say what we mean in the first place, c=a+b. The essence of parallel programming is not to force “into the time dimension” (i.e., to serialize) operations that naturally extend across a span of data, that is, “in the space dimension.” If it were not for 50-year-old collective habits, and the languages designed to support them, parallel programming would probably strike us as more natural than its serial counterpart.
Broadcasts and Dimensional Expansion: SSP vs. MMP We have previously mentioned the Fortran 90 rule that a scalar variable is conformable with any shape array. Thus, we can implement a calculation such as yi = xi + s,
i = 1, . . . , n
(22.1.1)
with code like y=x+s
where we of course assume previous declarations like REAL(SP) :: s REAL(SP), DIMENSION(n) :: x,y
with n a compile-time constant or dummy argument. (Hereafter, we will omit the declarations in examples that are this simple.) This seemingly simple construction actually hides an important underlying parallel capability, namely, that of broadcast. The sums in y=x+s are done in parallel
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
which is of course perfectly valid Fortran 90 code, we can simply write
966
Chapter 22.
Introduction to Parallel Programming
wi =
n
|xi + xj |,
i = 1, . . . , n
(22.1.2)
j=1
Here, we are doing n2 operations: For each of n values of i there is a sum over n values of j. Serial code for this calculation might be do i=1,n w(i)=0. do j=1,n w(i)=w(i)+abs(x(i)+x(j)) end do end do
The obvious immediate parallelization in Fortran 90 uses the sum intrinsic function to eliminate the inner do-loop. This would be a suitable amount of parallelization for a small-scale parallel machine, with a few processors: do i=1,n w(i)=sum(abs(x(i)+x)) end do
Notice that the conformability rule implies that a new value of x(i), a scalar, is being broadcast to all the processors involved in the abs and sum, with each iteration of the loop over i. What about the outer do-loop? Do we need, or want, to eliminate it, too? That depends on the architecture of your computer, and on the tradeoff between time and memory in your problem (a common feature of all computing, no less so parallel computing). Here is an implementation that is free of all do-loops, in principle capable of being executed in a small number (independent of n) of parallel operations: REAL(SP), DIMENSION(n,n) :: a ... a = spread(x,dim=2,ncopies=n)+spread(x,dim=1,ncopies=n) w = sum(abs(a),dim=1)
This is an example of what we call dimensional expansion, as implemented by the spread intrinsic. Although the above may strike you initially as quite a cryptic construction, it is easy to learn to read it. In the first assignment line, a matrix is constructed with all possible values of x(i)+x(j). In the second assignment line, this matrix is collapsed back to a vector by applying the sum operation to the absolute value of its elements, across one of its dimensions.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
on different CPUs, each CPU accessing different components of x and y. Yet, they all must access the same scalar value s. If the hardware has local memory for each CPU, the value of s must be replicated and transferred to each CPU’s local memory. On the other hand, if the hardware implements a single, global memory space, it is vital to do something that mitigates the traffic jam potentially caused by all the CPUs trying to access the same memory location at the same time. (We will use the term “broadcast” to refer equally to both cases.) Although hidden from the user, Fortran 90’s ability to do broadcasts is an essential feature of it as a parallel language. Broadcasts can be more complicated than the above simple example. Consider, for example, the calculation
22.1 Fortran 90 Data Parallelism: Arrays and Intrinsics
967
More explicitly, the first line creates a matrix a by adding two matrices each constructed via spread. In spread, the dim argument specifies which argument is duplicated, so that the first term varies across its first (row) dimension, and vice versa for the second term:
x1 x2 x3 .. .
x1 ... . . . x1 . . . + x1 .. .. . .
x2 x2 x2 .. .
x3 x3 x3 .. .
... . . . . . . .. .
(22.1.3)
Since equation (22.1.2) above is symmetric in i and j, it doesn’t really matter what value of dim we put in the sum construction, but the value dim=1 corresponds to summing across the rows, that is, down each column of equation (22.1.3). Be sure that you understand that the spread construction changed an O(n) memory requirement into an O(n 2 ) one! If your values of n are large, this is an impossible burden, and the previous implementation with a single do-loop remains the only practical one. On the other hand, if you are working on a massively parallel machine, whose number of processors is comparable to n2 (or at least much larger than n), then the spread construction, and the underlying broadcast capability that it invokes, leads to a big win: All n 2 operations can be done in parallel. This distinction between small-scale parallel machines — which we will hereafter refer to as SSP machines — and massively multiprocessor machines — which we will refer to as MMP machines — is an important one. A main goal of parallelism is to saturate the available number of processors, and algorithms for doing so are often different in the SSP and MMP opposite limits. Dimensional expansion is one method for saturating processors in the MMP case.
Masks and “Index Loss” An instructive extension of the above example is the following case of a product that omits one term (the diagonal one): wi =
n
(xj − xi ),
i = 1, . . . , n
(22.1.4)
j=1 j=i
Formulas like equation (22.1.4) frequently occur in the context of interpolation, where all the xi ’s are known to be distinct, so let us for the moment assume that this is the case. Serial code for equation (22.1.4) could be do i=1,n w(i)=1.0_sp do j=1,n if (j /= i) w(i)=w(i)*(x(j)-x(i)) end do end do
Parallel code for SSP machines, or for large enough n on MMP machines, could be
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
aij = xi + xj x1 x1 x2 x2 = x3 x3 .. .. . .
968
Chapter 22.
Introduction to Parallel Programming
do i=1,n w(i)=product( x-x(i), mask=(x/=x(i)) ) end do
a = spread(x,dim=2,ncopies=n)-spread(x,dim=1,ncopies=n) w = product(a,dim=1,mask=(a/=0.))
This time it does matter that the value of dim in the product intrinsic is 1 rather than 2. If you write out the analog of equation (22.1.3) for the present example, you’ll see that the above fragment is the right way around. The problem of index loss is still with us: we have to construct a mask from the array a, not from its indices, both of which are now lost to us! In most cases, there are workarounds (more, or less, awkward as they may be) for the problem of index loss. In the worst cases, which are quite rare, you have to create objects to hold, and thus bring back into play, the lost indices. For example, INTEGER(I4B), DIMENSION(n) :: jj ... jj = (/ (i,i=1,n) /) do i=1,n w(i)=product( x-x(i), mask=(jj/=i) ) end do
Now the array jj is filled with the “lost” j index, so that it is available for use in the mask. A similar technique, involving spreads of jj, can be used in the above MMP code fragment, which used dimensional expansion. (Fortran 95’s forall construction will make index loss much less of a problem. See §21.6.) Incidentally, the above Fortran 90 construction, (/ (i,i=1,n) /), is called an array constructor with implied do list. For reasons to be explained in §22.2, we almost never use this construction, in most cases substituting a Numerical Recipes utility function for generating arithmetical progressions, which we call arth.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Here, the mask argument in the product intrinsic function causes the diagonal term to be omitted from the product, as we desire. There are some features of this code, however, that bear commenting on. First, notice that, according to the rules of conformability, the expression x/=x(i) broadcasts the scalar x(i) and generates a logical array of length n, suitable for use as a mask in the product intrinsic. It is quite common in Fortran 90 to generate masks “on the fly” in this way, particularly if the mask is to be used only once. Second, notice that the j index has disappeared completely. It is now implicit in the two occurrences of x (equivalent to x(1:n)) on the right-hand side. With the disappearance of the j index, we also lose the ability to do the test on i and j, but must use, in essence, x(i) and x(j) instead! That is a very general feature in Fortran 90: when an operation is done in parallel across an array, there is no associated index available within the operation. This “index loss,” as we will see in later discussion, can sometimes be quite an annoyance. A language construction present in CM [Connection Machine] Fortran, the so-called forall, which would have allowed access to an associated index in many cases, was eliminated from Fortran 90 by the X3J3 committee, in a controversial decision. Such a construction will come into the language in Fortran 95. What about code for an MMP machine, where we are willing to use dimensional expansion to achieve greater parallelism? Here, we can write,
22.1 Fortran 90 Data Parallelism: Arrays and Intrinsics
969
Interprocessor Communication Costs
a(1:10,1:10) = b(1:10,1:10) + c(10:1:-1,10:1:-1)
the required upside-down-and-backwards values of the array c are just there, no matter that a great deal of routing and switching may have taken place. An ancillary blessing is that this book, unlike so many other (more highly technical) books on parallel programming (see references below) need not be filled with complex and subtle discussions of CPU connectivity, topology, routing algorithms, and so on. The curse is, just as you might expect, that the Fortran 90 programmer can’t control the interprocessor communication, even when it is desirable to do so. A few regular communication patterns are “known” to the compiler through Fortran 90 intrinsic functions, for example b=transpose(a). These, presumably, are done in an optimal way. However, many other regular patterns of communication, which might also allow highly optimized implementations, don’t have corresponding intrinsic functions. (An obvious example is the “butterfly” pattern of communication that occurs in fast Fourier transforms.) These, if coded in Fortran 90 by using general vector subscripts (e.g., barr=arr(iarr) or barr(jarr)=arr, where iarr and jarr are integer arrays), lose all possibility of being optimized. The compiler can’t distinguish a communication step with regular structure from one with general structure, so it must assume the worst case, potentially resulting in very slow execution. About the only thing a Fortran 90 programmer can do is to start with a general awareness of the kind of apparently parallel constructions that might be quite slow on his/her parallel machine, and then to refine that awareness by actual experience and experiment. Here is our list of constructions most likely to cause interprocessor communication bottlenecks: • vector subscripts, like barr=arr(iarr) or barr(jarr)=arr (that is, general gather/scatter operations) • the pack and unpack intrinsic functions • mixing positive strides and negative strides in a single expression (as in the above b(1:10,1:10)+c(10:1:-1,10:1:-1)) • the reshape intrinsic when used with the order argument • possibly, the cshift and eoshift extrinsics, especially for nonsmall values of the shift. On the other hand, the fact is that these constructions are parallel, and are there for you to use. If the alternative to using them is strictly serial code, you should almost always give them a try.
Linear Algebra You should be alert for opportunities to use combinations of the matmul, spread, and dot product intrinsics to perform complicated linear algebra calculations. One useful intrinsic that is not provided in Fortran 90 is the outer product
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
It is both a blessing and a curse that Fortran 90 completely hides from the user the underlying machinery of interprocessor communication, that is, the way that data values computed by (or stored locally near) one CPU make their way to a different CPU that might need them next. The blessing is that, by and large, the Fortran 90 programmer need not be concerned with how this machinery works. If you write
970
Chapter 22.
Introduction to Parallel Programming
of two vectors, cij = ai bj
(22.1.5)
We already know how to implement this (cf. equation 22.1.3): In fact, this operation occurs frequently enough to justify making it a utility function, outerprod, which we will do in Chapter 23. There we also define other “outer” operations between vectors, where the multiplication in the outer product is replaced by another binary operation, such as addition or division. Here is an example of using these various functions: Many linear algebra routines require that a submatrix be updated according to a formula like ajk = ajk + bi aji
m
api apk ,
j = i, . . . , m,
k = l, . . . , n
(22.1.6)
p=i
where i, m, l, and n are fixed values. Using an array slice like a(:,i) to turn api into a vector indexed by p, we can code the sum with a matmul, yielding a vector indexed by k: temp(l:n)=b(i)*matmul(a(i:m,i),a(i:m,l:n))
Here we have also included the multiplication by b i , a scalar for fixed i. The vector temp, along with the vector a ji = a(:,i), is then turned into a matrix by the outerprod utility and used to increment a jk : a(i:m,l:n)=a(i:m,l:n)+outerprod(a(i:m,i),temp(l:n))
Sometimes the update formula is similar to (22.1.6), but with a slight permutation of the indices. Such cases can be coded as above if you are careful about the order of the quantities in the matmul and the outerprod. CITED REFERENCES AND FURTHER READING: Akl, S.G. 1989, The Design and Analysis of Parallel Algorithms (Englewood Cliffs, NJ: Prentice Hall). Bertsekas, D.P., and Tsitsiklis, J.N. 1989, Parallel and Distributed Computation: Numerical Methods (Englewood Cliffs, NJ: Prentice Hall). Carey, G.F. 1989, Parallel Supercomputing: Methods, Algorithms, and Applications (New York: Wiley). Fountain, T.J. 1994, Parallel Computing: Principles and Practice (New York: Cambridge University Press). Golub, G., and Ortega, J.M. 1993, Scientific Computing: An Introduction with Parallel Computing (San Diego, CA: Academic Press). Fox, G.C., et al. 1988, Solving Problems on Concurrent Processors, Volume I (Englewood Cliffs, NJ: Prentice Hall). Hockney, R.W., and Jesshope, C.R. 1988, Parallel Computers 2 (Bristol and Philadelphia: Adam Hilger). Kumar, V., et al. 1994, Introduction to Parallel Computing: Design and Analysis of Parallel Algorithms (Redwood City, CA: Benjamin/Cummings). Lewis, T.G., and El-Rewini, H. 1992, Introduction to Parallel Computing (Englewood Cliffs, NJ: Prentice Hall).
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
c = spread(a,dim=2,ncopies=size(b))*spread(b,dim=1,ncopies=size(a))
971
22.2 Linear Recurrence and Related Calculations
Modi, J.J. 1988, Parallel Algorithms and Matrix Computation (New York: Oxford University Press). Smith, J.R. 1993, The Design and Analysis of Parallel Algorithms (New York: Oxford University Press). Van de Velde, E. 1994, Concurrent Scientific Computing (New York: Springer-Verlag).
We have already seen that Fortran 90’s array constructor with implied do list can be used to generate simple series of integers, like (/ (i,i=1,n) /). Slightly more generally, one might want to generate an arithmetic progression, by the formula vj = b + (j − 1)a,
j = 1, . . . , n
(22.2.1)
This is readily coded as v(1:n) = (/ (b+(j-1)*a, j=1,n) /)
Although it is concise, and valid, we don’t like this coding. The reason is that it violates the fundamental rule of “thinking parallel”: it turns a parallel operation across a data vector into a serial do-loop over the components of that vector. Yes, we know that the compiler might be smart enough to generate parallel code for implied do lists; but it also might not be smart enough, here or in more complicated examples. Equation (22.2.1) is also the simplest example of a linear recurrence relation. It can be rewritten as v1 = b,
vj = vj−1 + a,
j = 2, . . . , n
(22.2.2)
In this form (assuming that, in more complicated cases, one doesn’t know an explicit solution like equation 22.2.1) one can’t write an explicit array constructor. Code like v(1) = b v(2:n) = (/ (v(j-1)+a,j=2,n) /)
! wrong
is legal Fortran 90 syntax, but illegal semantics; it does not do the desired recurrence! (The rules of Fortran 90 require that all the components of v on the right-hand side be evaluated before any of the components on the left-hand side are set.) Yet, as we shall see, techniques for accomplishing the evaluation in parallel are available. With this as our starting point, we now survey some particular tricks of the (parallel) trade.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
22.2 Linear Recurrence and Related Calculations
972
Chapter 22.
Introduction to Parallel Programming
Subvector Scaling: Arithmetic and Geometric Progressions For explicit arithmetic progressions like equation (22.2.1), the simplest parallel technique is subvector scaling [1]. The idea is to work your way through the desired vector in larger and larger parallel chunks:
v9...16 = v1...8 + 8a
(22.2.3)
And so on, until you reach the length of your vector. (The last step will not necessarily go all the way to the next power of 2, therefore.) The powers of 2, times a, can of course be obtained by successive doublings, rather than the explicit multiplications shown above. You can see that subvector scaling requires about log2 n parallel steps to process a vector of length n. Equally important for serial machines, or SSP machines, the scalar operation count for subvector scaling is no worse than entirely serial code: each new component vi is produced by a single addition. If addition is replaced by multiplication, the identical algorithm will produce geometric progressions, instead of arithmetic progressions. In Chapter 23, we will use subvector scaling to implement our utility functions arth and geop for these two progressions. (You can then call one of these functions instead of recoding equation 22.2.3 every time you need it.)
Vector Reduction: Evaluation of Polynomials Logically related to subvector scaling is the case where a calculation can be parallelized across a vector that shrinks by a factor of 2 in each iteration, until a desired scalar result is reached. A good example of this is the parallel evaluation of a polynomial [2] P (x) =
N
cj x j
(22.2.4)
j=0
For clarity we take the special case of N = 5. Start with the vector of coefficients (imagining appended zeros, as shown): c0 ,
c1 ,
c2 ,
c3 ,
c4 ,
c5 ,
0,
...
Now, add the elements by pairs, multiplying the second of each pair by x: c0 + c1 x,
c2 + c3 x,
c4 + c5 x,
0,
...
Now, the same operation, but with the multiplier x 2 : (c0 + c1 x) + (c2 + c3 x)x2 ,
(c4 + c5 x) + (0)x2 ,
0,
...
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
v1 = b v2 = b + a v3...4 = v1...2 + 2a v5...8 = v1...4 + 4a
973
22.2 Linear Recurrence and Related Calculations
And a final time, with multiplier x 4 : [(c0 + c1 x) + (c2 + c3 x)x2 ] + [(c4 + c5 x) + (0)x2 ]x4 ,
0,
...
Recursive Doubling: Linear Recurrence Relations Please don’t confuse our use of the word “recurrence” (as in “recurrence relation,” “linear recurrence,” or equation 22.2.2) with the words “recursion” and “recursive,” which both refer to the idea of a subroutine calling itself to obtain an efficient or concise algorithm. There are ample grounds for confusion, because recursive algorithms are in fact a good way of obtaining parallel solutions to linear recurrence relations, as we shall now see! Consider the general first order linear recurrence relation uj = aj + bj−1 uj−1 ,
j = 2, 3, . . . , n
(22.2.5)
with initial value u 1 = a1 . On a serial machine, we evaluate such a recurrence with a simple do-loop. To parallelize the recurrence, we can employ the powerful general strategy of recursive doubling. Write down equation (22.2.5) for 2j and for 2j − 1: u2j = a2j + b2j−1u2j−1 u2j−1 = a2j−1 + b2j−2 u2j−2
(22.2.6) (22.2.7)
Substitute equation (22.2.7) in equation (22.2.6) to eliminate u 2j−1 and get u2j = (a2j + a2j−1b2j−1 ) + (b2j−2b2j−1)u2j−2
(22.2.8)
This is a new recurrence of the same form as (22.2.5) but over only the even uj , and hence involving only n/2 terms. Clearly we can continue this process recursively, halving the number of terms in the recurrence at each stage, until we are left with a recurrence of length 1 or 2 that we can do explicitly. Each time we finish a subpart of the recursion, we fill in the odd terms in the recurrence, using equation (22.2.7). In practice, it’s even easier than it sounds. Turn to Chapter B5 to see a straightforward implementation of this algorithm as the recipe recur1. On a machine with more processors than n, all the arithmetic at each stage of the recursion can be done simultaneously. Since there are of order log n stages in the
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
We are left with a vector of (active) length 1, whose value is the desired polynomial evaluation. (You can see that the zeros are just a bookkeeping device for taking account of the case where the active subvector has odd length.) The key point is that the combining by pairs is a parallel operation at each stage. As in subvector scaling, there are about log2 n parallel stages. Also as in subvector scaling, our total operations count is only negligibly different from purely scalar code: We do one add and one multiply for each original coefficient c j . The only extra operations are log 2 n successive squarings of x; but this comes with the extra benefit of better roundoff properties than the standard scalar coding. In Chapter 23 we use vector reduction to implement our utility function poly for polynomial evaluation.
974
Chapter 22.
Introduction to Parallel Programming
Cyclic Reduction: Linear Recurrence Relations There is a variant of recursive doubling, called cyclic reduction, that can be implemented with a straightforward iteration loop, instead of a recursive procedure call. [3] Here we start by writing down the recurrence (22.2.5) for all adjacent terms uj and uj−1 (not just the even ones, as before). Eliminating u j−1 , just as in equation (22.2.8), gives uj = (aj + aj−1 bj−1) + (bj−2 bj−1 )uj−2
(22.2.9)
which is a first order recurrence with new coefficients aj and bj . Repeating this process gives successive formulas for uj in terms of uj−2, uj−4 , uj−8 . . . . The procedure terminates when we reach uj−n (for n a power of 2), which is zero for all j. Thus the last step gives uj equal to the last set of a j ’s. Here is a code fragment that implements cyclic reduction by direct iteration. The quantities aj are stored in the variable recur1. recur1=a bb=b j=1 do if (j >= n) exit recur1(j+1:n)=recur1(j+1:n)+bb(j:n-1)*recur1(1:n-j) bb(2*j:n-1)=bb(2*j:n-1)*bb(j:n-j-1) j=2*j enddo
In cyclic reduction the length of the vector u j that is updated at each stage does not decrease by a factor of 2 at each stage, but rather only decreases from ∼ n to ∼ n/2 during all log 2 n stages. Thus the total number of operations carried out is O(n log n), as opposed to O(n) for recursive doubling. For a serial machine or SSP machine, therefore, cyclic reduction is rarely superior to recursive doubling when the latter can be used. For an MMP machine, however, the issue is less clear cut, because the pattern of communication in cyclic reduction is quite different (and, for some parallel architectures, possibly more favorable) than that of recursive doubling.
Second Order Recurrence Relations Consider the second order recurrence relation yj = aj + bj−2 yj−1 + cj−2 yj−2 ,
j = 3, 4, . . . , n
(22.2.10)
with initial values y1 = a1 ,
y2 = a2
(22.2.11)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
recursion, the execution time is O(log n). The total number of operations carried out is of order n + n/2 + n/4 + · · · = O(n), the same as for the obvious serial do-loop. In the utility routines of Chapter 23, we will use recursive doubling to implement the routines poly term, cumsum, and cumprod. We could use recursive doubling to implement parallel versions of arth and geop (arithmetic and geometric progressions), and zroots unity (complex nth roots of unity), but these can be done slightly more efficiently by subvector scaling, as discussed above.
975
22.2 Linear Recurrence and Related Calculations
uj = aj + bj−1 · uj−1 , where
yj , uj = yj+1
aj =
0
j = 2, . . . , n − 1
,
aj+1
bj−1 =
0
1
cj−1
bj−1
(22.2.13)
,
j = 2, . . . , n − 1 (22.2.14)
and
u1 = a1 =
y1 y2
=
a1 a2
(22.2.15)
This is a first order recurrence relation for the vectors u j , and can be solved by the algorithm described above (and implemented in the recipe recur1). The only difference is that the multiplicationsare matrix multiplications with the 2×2 matrices bj . After the first recursive call, the zeros in a and b are lost, so we have to write the routine for general two-dimensional vectors and matrices. Note that this algorithm does not avoid the potential instability problems associated with second order recurrences that are discussed in §5.5 of Volume 1. Also note that the algorithm generalizes in the obvious way to higher-order recurrences: An nth order recurrence can be written as a first order recurrence involving n-dimensional vectors and matrices.
Parallel Solution of Tridiagonal Systems Closely related to recurrence relations, recursive doubling, and cyclic reduction is the parallel solution of tridiagonal systems. Since Fortran 90 vectors “know their own size,” it is most logical to number the components of both the sub- and super-diagonals of the tridiagonal matrix from 1 to N − 1. Thus equation (2.4.1), here written in the special case of N = 7, becomes (blank elements denoting zero),
b1 a1
c1 b2 a2
c2 b3 a3
c3 b4 a4
c4 b5 a5
c5 b6 a6
u1 r1 u2 r2 u3 r3 · u4 = r4 u5 r5 c6 u6 r6 b7 u7 r7
(22.2.16)
The basic idea for solving equation (22.2.16) on a parallel computer is to partition the problem into even and odd elements, recurse to solve the former, and
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Our labeling of subscripts is designed to make it easy to enter the coefficients in a computer program: You need to supply a 1 , . . . , an , b1 , . . . , bn−2, and c1 , . . . , cn−2. Rewrite the recurrence relation in the form ( [3])
0 0 1 yj−1 yj = + , j = 2, . . . , n − 1 yj+1 yj aj+1 cj−1 bj−1 (22.2.12) that is,
976
Chapter 22.
Introduction to Parallel Programming
then solve the latter in parallel. Specifically, we first rewrite (22.2.16), by permuting its rows and columns, as
c1 a2
b1 b3 b5
c3 a4
b7 c2 a3
b2 c4 a5
b4 c6
u1 r1 u3 r3 c5 u 5 r 5 a6 · u 7 = r 7 u2 r2 u4 r4 b6 u6 r6
(22.2.17)
Now observe that, by row operations that subtract multiples of the first four rows from each of the last three rows, we can eliminate all nonzero elements in the lower-left quadrant. The price we pay is bringing some new elements into the lower-right quadrant, whose nonzero elements we now call x’s, y’s, and z’s. We call the modified right-hand sides q. The transformed problem is now
b1
c1 a2
b3 b5
c3 a4
b7 y1 x1
z1 y2 x2
u1 r1 u3 r3 c5 u 5 r 5 a6 · u 7 = r 7 u2 q 1 z2 u4 q2 y3 u6 q3
(22.2.18)
Notice that the last three rows form a new, smaller, tridiagonal problem, which we can solve simply by recursing! Once its solution is known, the first four rows can be solved by a simple, parallelizable, substitution. This algorithm is implemented in tridag in Chapter B2. The above method is essentially cyclic reduction, but in the case of the tridiagonal problem, it does not “unwind” into a simple iteration; on the contrary, a recursive subroutine is required. For discussion of this and related methods for parallelizing tridiagonal systems, and references to the literature, see Hockney and Jesshope [3]. Recursive doubling can also be used to solve tridiagonal systems, the method requiring the parallel solution (as above) of both a first order recurrence and a second order recurrence [3,4]. For tridiagonal systems, however, cyclic reduction is usually more efficient than recursive doubling. CITED REFERENCES AND FURTHER READING: Van Loan, C.F. 1992, Computational Frameworks for the Fast Fourier Transform (Philadelphia: S.I.A.M.) §1.4.2. [1] Estrin, G. 1960, quoted in Knuth, D.E. 1981, Seminumerical Algorithms, volume 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §4.6.4. [2] Hockney, R.W., and Jesshope, C.R. 1988, Parallel Computers 2: Architecture, Programming, and Algorithms (Bristol and Philadelphia: Adam Hilger), §5.2.4 (cyclic reduction); §5.4.2 (second order recurrences); §5.4 (tridiagonal systems). [3] Stone, H.S. 1973, Journal of the ACM, vol. 20, pp. 27–38; 1975, ACM Transactions on Mathematical Software, vol. 1, pp. 289–307. [4]
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
a1
977
22.3 Parallel Synthetic Division and Related Algorithms
22.3 Parallel Synthetic Division and Related Algorithms
Cumulants of a Polynomial Suppose we have a polynomial P (x) =
N
cj xN−j
(22.3.1)
j=0
(Note that, here, the cj ’s are indexed from highest degree to lowest, the reverse of the usual convention.) Then we can define the cumulants of the polynomial to be partial sums that occur in the polynomial’s usual, serial evaluation, P 0 = c0 P 1 = c0 x + c1 ··· PN = c0 xN + · · · + cN = P (x)
(22.3.2)
Evidently, the cumulants satisfy a simple, linear first order recurrence relation, P 0 = c0 ,
Pj = cj + xPj−1,
j = 2, . . . , N
(22.3.3)
This is slightly simpler than the general first order recurrence, because the value of x does not depend on j. We already know, from §22.2’s discussion of recursive doubling, how to parallelize equation (22.3.3) via a recursive subroutine. In Chapter 23, the utility routine poly term will implement just such a procedure. An example of a routine that calls poly term to evaluate a recurrence equivalent to equation (22.3.3) is eulsum in Chapter B5. Notice that while we could use equation (22.3.3), parallelized by recursive doubling, simply to evaluate the polynomial P (x) = P N , this is likely somewhat slower than the alternative technique of vector reduction, also discussed in §22.2, and implemented in the utility function poly. Equation (22.3.3) should be saved for cases where the rest of the Pj ’s (not just P N ) can be put to good use.
Synthetic Division by a Monomial We now show that evaluation of the cumulants of a polynomial is equivalent to synthetic division of the polynomial by a monomial, also called deflation (see §9.5 in Volume 1). To review briefly, and by example, here is a standard tableau from high school algebra for the (long) division of a polynomial 2x 3 − 7x2 + x + 3 by the monomial factor x − 3.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
There are several techniques for parallelization that relate to synthetic division but that can also find application in wider contexts, as we shall see.
978
Chapter 22.
Introduction to Parallel Programming
(22.3.4)
Now, here is the same calculation written as a synthetic division, really the same procedure as tableau (22.3.4), but with unnecessary notational baggage omitted (and also a changed sign for the monomial’s constant, so that subtractions become additions): 6 −3 −6 2 −7 +1 +3 3 2 −1 −2 −3
(22.3.5)
If we substitute symbols for the above quantities with the correspondence x c0 c1 c2 c3 P0 P1 P2 P3 (22.3.6) then it is immediately clear that the P j ’s in equation (22.3.6) are simply the P j ’s of equation (22.3.3); the calculation is thus parallelizable by recursive doubling. In this context, the utility routine poly term is used by the routine zroots in Chapter B9.
Repeated Synthetic Division It is well known from high-school algebra that repeated synthetic division of a polynomial yields, as the remainders that occur, first the value of the polynomial, next the value of its first derivative, and then (up to multiplication by the factorial of an integer) the values of higher derivatives. If you want to parallelize the calculation of the value of a polynomial and one or two of its derivatives, it is not unreasonable to evaluate equation (22.3.3), parallelized by recursive doubling, two or three times. Our routine ddpoly in Chapter B5 is meant for such use, and it does just this, as does the routine laguer in Chapter B9. There are other cases, however, for which you want to perform repeated synthetic division and “go all the way,” until only a constant remains. For example, this is the preferred way of “shifting a polynomial,” that is, evaluating the coefficients of a polynomial in a variable y that differs from the original variable x by an additive constant. (The recipe pcshft has this as its assigned task.) By way of example, consider the polynomial 3x 3 + x2 + 4x + 7, and let us perform repeated synthetic division by a general monomial x − a. The conventional calculation then proceeds according to the following tableau, reading it in conventional lexical order (left-to-right and top-to-bottom):
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
2 2x − x − 2 x − 3 2x3 − 7x2 + x + 3 2x3 − 6x2 −x2 + x −x2 +3x −2x + 3 −2x + 6 − 3 (remainder)
979
22.3 Parallel Synthetic Division and Related Algorithms
3
1
4
a
a
a
a
7 a
3 −→ 3a + 1 −→ 3a2 + a + 4 −→ 3a3 + a2 + 4a + 7
a
3 −→ 9a + 1 3
(22.3.7)
Here, each row (after the first) shows a synthetic division or, equivalently, evaluation of the cumulants of the polynomial whose coefficients are the preceding row. The results at the right edge of the rows are the values of the polynomial and (up to integer factorials) its three nonzero derivatives, or (equivalently, without factorials) coefficients of the shifted polynomial. We could parallelize the calculation of each row of tableau (22.3.7) by recursive doubling. That is a lot of recursion, which incurs a nonnegligible overhead. A much better way of doing the calculation is to deform tableau (22.3.7) into the following equivalent tableau, 3 −→
3 a
1 −→
3a + 1 a
4 −→
2
3a + a + 4 a
7 −→ 3a + a + 4a + 7 3
2
3 a
6a + 1 a 2
9a + 2a + 4
3 a 9a + 1
3
(22.3.8)
Now each row explicitly depends on only the previous row (and the given first column), so the rows can be calculated in turn by an explicit parallel expression, with no recursive calls needed. An example of coding (22.3.8) in Fortran 90 can be found in the routine pcshft in Chapter B5. (It is also possible to eliminate most of the multiplications in (22.3.8), at the expense of a much smaller number of divisions. We have not done this because of the necessity for then treating all possible divisions by zero as special cases. See [1] for details and references.) Actually, the deformation of (22.3.7) into (22.3.8) is the same trick as was used in Volume 1, p. 167, for evaluating a polynomial and its derivative simultaneously, also generalized in the Fortran 77 implementation of the routine ddpoly (Chapter 5). In the Fortran 90 implementation of ddpoly (Chapter B5) we don’t use this trick, but instead use poly term, because, there, we want to parallelize over the length of the polynomial, not over the number of desired derivatives.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
3 −→ 6a + 1 −→ 9a2 + 2a + 4
980
Chapter 22.
Introduction to Parallel Programming
Don’t confuse the cases of iterated synthetic division, discussed here, with the simpler case of doing many simultaneous synthetic divisions. In the latter case, you can simply implement equation (22.3.3) serially, exactly as written, but with each operation being data-parallel across your problem set. (This case occurs in our routine polcoe in Chapter B3.)
A parallel calculation algorithmically very similar to (22.3.7) or (22.3.8) occurs when we want to find the coefficients of a polynomial P (x) from its roots r1 , . . . , rN . For this, the tableau is r1 r2 : ↓
r1 + r2
r1 r2
r3 : ↓
r1 + r2 + r3
↓
r1 r2 + r3 (r1 + r2 )
r1 r2 r3
(22.3.9)
As before, the rows are computed consecutively, from top to bottom. Each row is computed via a single parallel expression. Note that values moving on vertical arrows are simply added in, while values moving on diagonal arrows are multiplied by a new root before adding. Examples of coding (22.3.9) in Fortran 90 can be found in the routines vander (Chapter B2) and polcoe (Chapter B3). An equivalent deformation of (22.3.9) is r1 r2 : ↓
r1 r2
r1 + r2
r3 : ↓ r1 r2 r3
↓
r1 r2 + r3 (r1 + r2 )
r1 + r2 + r3
(22.3.10)
Here the diagonal arrows are simple additions, while the vertical arrows represent multiplication by a root value. Note that the coefficient answers in (22.3.10) come out in the opposite order from (22.3.9). An example of coding (22.3.10) in Fortran 90 can be found in the routine fixrts in Chapter B13. CITED REFERENCES AND FURTHER READING: Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §4.6.4, p. 470. [1]
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Polynomial Coefficients from Roots
22.4 Fast Fourier Transforms
981
22.4 Fast Fourier Transforms
Column- and Row-Parallel FFTs The basic building block that we assume (and implement in Chapter B12) is a routine for simultaneously taking the FFT of each row of a two-dimensional matrix. The method is exactly that of Volume 1’s four1 routine, but with array sections like data(:,j) replacing scalars like data(j). Chapter B12’s implementation of this is a routine called fourrow. If all the data for one column (that is, all the values data(i,:), for some i) are local to a single processor, then the parallelism involves no interprocessor communication at all: The independent FFTs simply proceed, data parallel and in lockstep. This is architecture-independent parallelism with a vengeance. We will also need to take the FFT of each column of a two-dimensional matrix. One way to do this is to take the transpose (a Fortran 90 intrinsic that hides a lot of interprocessor communication), then take the FFT of the rows using fourrow, then take the transpose again. An alternative method is to recode the four1 routine with array sections in the other dimension (data(j,:)) replacing four1’s scalars (data(j)). This scheme, in Chapter B12, is a routine called fourcol. In this case, good parallelism will be achieved only if the values data(:,i), for some i, are local to a single processor. Of course, Fortran 90 does not give the user direct control over how data are distributed over the machine; but extensions such as HPF are designed to give just such control.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Fast Fourier transforms are beloved by computer scientists, especially those who are interested in parallel algorithms, because the FFT’s hierarchical structure generates a complicated, but analyzable, set of requirements for interprocessor communication on MMPs. Thus, almost all books on parallel algorithms (e.g., [1–3]) have a chapter on FFTs. Unfortunately, the resulting algorithms are highly specific to particular parallel architectures, and therefore of little use to us in writing general purpose code in an architecture-independent parallel language like Fortran 90. Luckily there is a good alternative that covers almost all cases of both serial and parallel machines. √ If, for a one-dimensional FFT of size N , one is satisfied with parallelism of order N , then there is a good, general way of achieving a parallel FFT with quite minimal interprocessor communication; and the communication required is simply the matrix transpose operation, which Fortran 90 implements as an intrinsic. That is the approach that we discuss in this section, and implement in Chapter B12. For a machine with M processors, this approach will saturate the processors (the desirable condition where none are idle) when the size of a one-dimensional Fourier transform, N , is large enough: N > M 2 . Smaller N ’s will not achieve maximum parallelism. But such N ’s are in fact so small for one-dimensional problems that they are unlikely to be the rate-determining step in scientific calculations. If they are, it is usually because you are doing many such transforms independently, and you should recover “outer parallelism” by doing them all at once. For two or more dimensions, the adopted approach will saturate M processors when each dimension of the problem is larger than M .
982
Chapter 22.
Introduction to Parallel Programming
One-Dimensional FFTs Turn now to the problem of how to do a single, one-dimensional, FFT. We are given a complex array f of length N , an integer power of 2. The basic idea is to address the input array as if it were a two-dimensional array of size m × M , where m and M are each integer powers of 2. Then the components of f can be addressed as 0 ≤ j < m, 0 ≤ J < M
f(Jm + j),
(22.4.1)
where the j index changes more rapidly, the J index more slowly, and parentheses denote Fortran-style subscripts. Now, suppose we had some magical (parallel) method to compute the discrete Fourier transform F (kM + K) ≡
e2πi(kM +K)(Jm+j)/(M m) f(Jm + j),
j,J
0 ≤ k < m, 0 ≤ K < M
(22.4.2)
Then, you can see that the indices k and K would address the desired result (FFT of the original array), with K varying more rapidly. Starting with equation (22.4.2) it is easy to verify the following identity,
F (kM + K) =
j
2πijk/m
e
2πijK/(M m)
e
2πiJK/M
e
f(Jm + j)
J
(22.4.3) But this, reading it from the innermost operation outward, is just the magical method that we need: • Reshape the original array to m × M in Fortran normal order (storage by columns). • FFT on the second (column) index for all values of the first (row) index, using the routine fourrow. • Multiply each component by a phase factor exp[2πijK/(M m)]. • Transpose.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
On a serial machine, you might think that fourrow and fourcol should have identical timings (acting on a square matrix, say). The two routines do exactly the same operations, after all. Not so! On modern serial computers, fourrow and fourcol can have timings that differ by a factor of 2 or more, even when their detailed arithmetic is made identical (by giving to one a data array that is the transpose of the data array given to the other). This effect is due to the multilevel cache architecture of most computer memories, and the fact that serial Fortran always stores matrices by columns (first index changing most rapidly). On our workstations, fourrow is significantly faster than fourcol, and this is likely the generic behavior. However, we do not exclude the possibility that some machines, and some sizes of matrices, are the other way around.
22.5 Missing Language Features
983
CITED REFERENCES AND FURTHER READING: Fox, G.C., et al. 1988, Solving Problems on Concurrent Processors, Volume I (Englewood Cliffs, NJ: Prentice Hall), Chapter 11. [1] Akl, S.G. 1989, The Design and Analysis of Parallel Algorithms (Englewood Cliffs, NJ: Prentice Hall), Chapter 9. [2] Hockney, R.W., and Jesshope, C.R. 1988, Parallel Computers 2 (Bristol and Philadelphia: Adam Hilger), §5.5. [3] Van Loan, C. 1992, Computational Frameworks for the Fast Fourier Transform (Philadelphia: S.I.A.M.), §3.3. [4]
22.5 Missing Language Features A few facilities that are fairly important to parallel programming are missing from the Fortran 90 language standard. On scalar machines this lack is not a
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
• Again FFT on the second (column) index for all values of the first (row) index, using the routine fourrow. • Reshape the two-dimensional array back into one-dimensional output. The above scheme uses fourrow exclusively, on the assumption that it is faster than its sibling fourcol. When that is the case (as we typically find), it is likely that the above method, implemented as four1 in Chapter B12, is faster, even on scalar machines, than Volume 1’s scalar version of four1 (Chapter 12). The reason, as already mentioned, is that fourrow’s parallelism is taking better advantage of cache memory locality. If fourrow is not faster than fourcol on your machine, then you should instead try the following alternative scheme, using fourcol only: • Reshape the original array to m × M in Fortran normal order (storage by columns). • Transpose. • FFT on the first (row) index for all values of the second (column) index, using the routine fourcol. • Multiply each component by a phase factor exp[2πijK/(M m)]. • Transpose. • Again FFT on the first (row) index for all values of the second (column) index, using the routine fourcol. • Transpose. • Reshape the two-dimensional array back into one-dimensional output. In Chapter B12, this scheme is implemented as four1 alt. You might wonder why four1 alt has three transpose operations, while four1 had only one. Shouldn’t there be a symmetry here? No. Fortran makes the arbitrary, but consistent, choice of storing two-dimensional arrays by columns, and this choice favors four1 in terms of transposes. Luckily, at least on our serial workstations, fourrow (used by four1) is faster than fourcol (used by four1 alt), so it is a double win. For further discussion and references on the ideas behind four1 and four1 alt see [4], where these algorithms are called the four-step and six-step frameworks, respectively.
984
Chapter 22.
Introduction to Parallel Programming
problem, since one can readily program the missing features by using do-loops. On parallel machines, both SSP machines and MMP machines, one must hope that hardware manufacturers provide library routines, callable from Fortran 90, that provide access to the necessary facilities, or use extensions of Fortran 90, such as High Performance Fortran (HPF).
Fortran 90 allows the use of vector subscripts for so-called gather and scatter operations. For example, with the setup REAL(SP), DIMENSION(6) :: arr,barr,carr INTEGER(I4B), DIMENSION(6) :: iarr,jarr ... iarr = (/ 1,3,5,2,4,6 /) jarr = (/ 3,2,3,2,1,1 /)
Fortran 90 allows both the one-to-one gather and the one-to-many gather, barr=arr(iarr) carr=arr(jarr)
It also allows the one-to-one scatter, barr(iarr)=carr
where the elements of carr are “scattered” into barr under the direction of the vector subscript iarr. Fortran 90 does not allow the many-to-one scatter barr(jarr)=carr
! illegal for this jarr
because the repeated values in jarr try to assign different components of carr to the same location in barr. The result would not be deterministic. Sometimes, however, one would in fact like a many-to-one construction, where the colliding elements get combined by a (commutative and associative) operation, like + or *, or max(). These so-called scatter-with-combine functions are readily implemented on serial machines by a do-loop, for example, barr=0. do j=1,size(carr) barr(jarr(j))=barr(jarr(j))+carr(j) end do
Fortran 90 unfortunately provides no means for effecting scatter-with-combine functions in parallel. Luckily, almost all parallel machines do provide such a facility as a library program, as does HPF, where the above facility is called SUM SCATTER. In Chapter 23 we will define utility routines scatter add and scatter max for scatter-with-combine functionalities, but the implementation given in Fortran 90 will be strictly serial, with a do-loop.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Scatter-with-Combine Functions
22.5 Missing Language Features
985
Skew Sections Fortran 90 provides no good, parallel way to access the diagonal elements of a matrix, either to read them or to set them. Do-loops will obviously serve this need on serial machines. In principle, a construction like the following bizarre fragment could also be utilized,
which extracts every (n + 1)st element from a one-dimensional array derived by reshaping the input matrix. However, it is unlikely that any foreseeable parallel compiler will implement the above fragment without a prohibitive amount of unnecessary data movement; and code like the above is also exceedingly slow on all serial machines that we have tried. In Chapter 23 we will define utility routines get diag, put diag, diagadd, diagmult, and unit matrix to manipulate diagonal elements, but the implementation given in Fortran 90 will again be strictly serial, with do-loops. Fortran 95 (see §21.6) will essentially fix Fortran 90’s skew sections deficiency. For example, using its forall construction, the diagonal elements of an array can be accessed by a statement like forall (j=1:20) diag(j) = arr(j,j)
SIMD vs. MIMD Recall that we use “SIMD” (single-instruction, multiple data) and “data parallel” as interchangeable terms, and that “MIMD” (multiple-instruction, multiple data) is a more general programming model. (See §22.1.) You should not be too quick to jump to the conclusion that Fortran 90’s data parallel or SIMD model is “bad,” and that MIMD features, absent in Fortran 90, are therefore “good.” On the contrary, Fortran 90’s basic data-parallel paradigm has a lot going for it. As we discussed in §22.1, most scientific problems naturally have a “data dimension” across which the time ordering of the calculation is irrelevant. Parallelism across this dimension, which is by nature most often SIMD, frees the mind to think clearly about the computational steps in an algorithm that actually need to be sequential. SIMD code has advantages of clarity and predictability that should not be taken lightly. The general MIMD model of “lots of different things all going on at the same time and communicating data with each other” is a programming and debugging nightmare. Having said this, we must at the same time admit that a few MIMD features — most notably the ability to go through different logical branches for calculating different data elements in a data-parallel computation — are badly needed in certain programming situations. Fortran 90 is quite weak in this area. Note that the where...elsewhere...end where construction is not a MIMD construction. Fortran 90 requires that the where clause be executed completely before the elsewhere is started. (This allows the results of any calculations in the former
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
REAL(SP), DIMENSION(n,n) :: mat REAL(SP), DIMENSION(n*n) :: arr REAL(SP), DIMENSION(n) :: diag ... arr = reshape(mat,shape(arr)) diag = arr(1:n*n:n+1)
986
Chapter 22.
Introduction to Parallel Programming
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
clause to be available for use in the latter.) So, this construction cannot be used to allow two logical branches to be calculated in parallel. Special functions, where one would like to calculate function values for an array of input quantities, are a particularly compelling example of the need for some MIMD access. Indeed, you will find that Chapter B6 contains a number of intricate, and in a few cases truly bizarre, workarounds, using allowed combinations of merge, where, and CONTAINS (the latter, for separating different logical branches into formally different subprograms). Fortran 95’s ELEMENTAL and PURE constructions, and to some extent also forall (whose body will be able to include PURE function calls), will go a long way towards providing exactly the kind of MIMD constructions that are most needed. Once Fortran 95 becomes available and widespread, you can expect to see a new version of this volume, with a much-improved Chapter B6. Conversely, the number of routines outside of Chapter B6 that can be significantly improved by the use of MIMD features is relatively small; this illustrates the underlying viability of the basic data-parallel SIMD model, even in a future language version with useful MIMD features.
23.0 Introduction and Summary Listing This chapter describes and summarizes the Numerical Recipes utility routines that are used throughout the rest of this volume. A complete implementation of these routines in Fortran 90 is listed in Appendix C1. Why do we need utility routines? Aren’t there already enough of them built into the language as Fortran 90 intrinsics? The answers lie in this volume’s dual purpose: to implement the Numerical Recipes routines in Fortran 90 code that runs efficiently on fast serial machines, and to implement them, wherever possible, with efficient parallel code for multiprocessor machines that will become increasingly common in the future. We have found three kinds of situations where additional utility routines seem desirable: 1. Fortran 90 is a big language, with many high-level constructs — single statements that actually result in a lot of computing. We like this; it gives the language the potential for expressing algorithms very readably, getting them “out of the mud” of microscopic coding. In coding the 350+ Recipes for this volume, we kept a systematic watch for bits of microscopic coding that were repeated in many routines, and that seemed to be at a lower level of coding than that aspired to by good Fortran 90 style. Once these bits were identified, we pulled them out and substituted calls to new utility routines. These are the utilities that arguably ought to be new language intrinsics, equally useful for serial and parallel machines. (A prime example is swap.) 2. Fortran 90 contains many highly parallelizable language constructions. But, as we have seen in §22.5, it is also missing a few important constructions. Most parallel machines will provide these missing elements as machine-coded library subroutines. Some of our utility routines are provided simply as a standard interface to these common, but nonstandard, functionalities. Note that it is the nature of these routines that our specific implementation, in Appendix C1, will be serial, and therefore inefficient on parallel machines. If you have a parallel machine, you will need to recode these; this often involves no more than substituting a one-line library function call for the body of our implementation. Utilities in this category will likely become unnecessary over time, either as machine-dependent libraries converge to standard interfaces, or as the utilities get added to future Fortran 987
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter 23. Numerical Recipes Utility Functions for Fortran 90
988
Chapter 23.
Numerical Recipes Utility Functions for Fortran 90
Alphabetical Listing The following list gives an abbreviated mnemonic for the type, rank, and/or shape of the returned values (as in §21.4), the routine’s calling sequence (optional arguments shown in italics), and a brief, often incomplete, description. The complete description of the routine is given in the later section shown in square brackets. For each entry, the number shown in parentheses is the approximate number of distinct Recipes in this book that make use of that particular utility function, and is thus a rough guide to that utility’s importance. (There may be multiple invocations of the utility in each such Recipe.) Where this number is small or zero, it is usually because the utility routine is a member of a related family of routines whose total usage was deemed significant enough to include, and we did not want users to have to “guess” which family members were instantiated. call array copy(src,dest,n copied,n not copied) Copy one-dimensional array (whose size is not necessarily known). [23.1] (9) [Arr]
arth(first,increment,n) Return an arithmetic progression as an array. [23.4] (55) call assert(n1,n2,...,string) Exit with error message if any logical arguments are false. [23.3] (50)
[Int]
assert eq(n1,n2,...,string) Exit with error message if all integer arguments are not equal;otherwise return common value. [23.3] (133)
[argTS] cumprod(arr,seed)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
versions. (Indeed, some routines in this category will be unnecessary in Fortran 95, once it is available; see §23.7.) 3. Some tasks should just be done differently in serial, versus parallel, implementation. Linear recurrence relations are a good example (§22.2). These are trivially coded with a do-loop on serial machines, but require a fairly elaborate recursive construction for good parallelization. Rather than provide separate serial and parallel versions of the Numerical Recipes, we have chosen to pull out of the Recipes, and into utility routines, some identifiable tasks of this kind. These are cases where some recoding of our implementation in Appendix C1 might result in improved performance on your particular hardware. Unfortunately, it is not so simple as providing a single “serial implementation” and another single “parallel implementation,” because even the seemingly simple word “serial” hides, at the hardware level, a variety of different degrees of pipelining, wide instructions, and so on. Appendix C1 therefore provides only a single implementation, although with some adjustable parameters that you can customize (by experiment) to maximize performance on your hardware. The above three cases are not really completely distinct, and it is therefore not possible to assign any single utility routine to exactly one situation. Instead, we organize the rest of this chapter as follows: first, an alphabetical list, with short summary, of all the new utility routines; next, a series of short sections, grouped by functionality, that contain the detailed descriptions of the routines.
23.0 Introduction and Summary Listing
989
Cumulative products of one-dimensional array, with optional seed value. [23.4] (3) [argTS] cumsum(arr,seed) Cumulative sums of one-dimensional array, with optional seed value. [23.4] (9)
call diagmult(mat,diag) Multiplies vector into diagonal of a matrix. [23.7] (2) [Arr]
geop(first,factor,n) Return a geometrical progression as an array. [23.4] (7)
[Arr]
get diag(mat) Gets diagonal of a matrix. [23.7] (2)
[Int]
ifirstloc(arr) Location of first true value in a logical array, returned as an integer. [23.2] (3)
[Int]
imaxloc(arr) Location of array maximum, returned as an integer. [23.2] (11)
[Int]
iminloc(arr) Location of array minimum, returned as an integer. [23.2] (8)
[Mat]
lower triangle(j,k,extra) Returns a lower triangular logical mask. [23.7] (1) call nrerror(string) Exit with error message. [23.3] (96)
[Mat]
outerand(a,b) Returns the outer logical and of two vectors. [23.5] (1)
[Mat]
outerdiff(a,b) Returns the outer difference of two vectors. [23.5] (4)
[Mat]
outerdiv(a,b) Returns the outer quotient of two vectors. [23.5] (0)
[Mat]
outerprod(a,b) Returns the outer product of two vectors. [23.5] (14)
[Mat]
outersum(a,b) Returns the outer sum of two vectors. [23.5] (0)
[argTS] poly(x,coeffs,mask) Evaluate a polynomial P (x) for one or more values x, with optional mask. [23.4] (15) [argTS] poly term(a,x) Returns partial cumulants of a polynomial, equivalent to synthetic
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call diagadd(mat,diag) Adds vector to diagonal of a matrix. [23.7] (4)
990
Chapter 23.
Numerical Recipes Utility Functions for Fortran 90
division. [23.4] (4) call put diag(diag,mat) Sets diagonal of a matrix. [23.7] (0) [Ptr]
call scatter add(dest,source,dest index) Scatter-adds source vector to specified components of destination vector. [23.6] (2) call scatter max(dest,source,dest index) Scatter-max source vector to specified components of destination vector. [23.6] (0) call swap(a,b,mask) Swap corresponding elements of a and b. [23.1] (24) call unit matrix(mat) Sets matrix to be a unit matrix. [23.7] (6) [Mat]
upper triangle(j,k,extra) Returns an upper triangular logical mask. [23.7] (4)
[Real]
vabs(v) Length of a vector in L2 norm. [23.8] (6)
[CArr] zroots unity(n,nn) Returns nn consecutive powers of the complex nth root of unity. [23.4] (4)
Comment on Relative Frequencies of Use We find it interesting to compare our frequency of using the nrutil utility routines, with our most used language intrinsics (see §21.4). On this basis, the following routines are as useful to us as the top 10 language intrinsics: arth, assert, assert eq, outerprod, poly, and swap. We strongly recommend that the X3J3 standards committee, as well as individual compiler library implementors, consider the inclusion of new language intrinsics (or library routines) that subsume the capabilities of at least these routines. In the next tier of importance, we would put some further cumulative operations (geop, cumsum), some other “outer” operations on vectors (e.g., outerdiff), basic operations on the diagonals of matrices (get diag, put diag, diag add), and some means of access to an array of unknown size (array copy).
23.1 Routines That Move Data To describe our utility routines, we introduce two items of Fortran 90 pseudocode: We use the symbol T to denote some type and rank declaration (including
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
reallocate(p,n,m,...) Reallocate pointer to new size, preserving its contents. [23.1] (5)
23.1 Routines That Move Data
991
scalar rank, i.e., zero); and when we append a colon to a type specification, as in INTEGER(I4B)(:), for example, we denote an array of the given type. The routines swap, array copy, and reallocate simply move data around in useful ways.
(swaps corresponding elements) User interface (or, “USE nrutil”): SUBROUTINE swap(a,b,mask) T, INTENT(INOUT) :: a,b LOGICAL(LGT), INTENT(IN), OPTIONAL :: mask END SUBROUTINE swap
Applicable types and ranks: T ≡ any type, any rank Types and ranks implemented (overloaded) in nrutil: T ≡ INTEGER(I4B), REAL(SP), REAL(SP)(:), REAL(DP), COMPLEX(SPC), COMPLEX(SPC)(:), COMPLEX(SPC)(:,:), COMPLEX(DPC), COMPLEX(DPC)(:), COMPLEX(DPC)(:,:) Action: Swaps the corresponding elements of a and b. If mask is present, performs the swap only where mask is true. (Following code is the unmasked case. For speed at run time, the masked case is implemented by overloading, not by testing for the optional argument.) Reference implementation: T :: dum dum=a a=b b=dum
array copy (copy one-dimensional array) User interface (or, “USE nrutil”): SUBROUTINE array_copy(src,dest,n_copied,n_not_copied) T, INTENT(IN) :: src T, INTENT(OUT) :: dest INTEGER(I4B), INTENT(OUT) :: n_copied, n_not_copied END SUBROUTINE array_copy
Applicable types and ranks: T ≡ any type, rank 1 Types and ranks implemented (overloaded) in nrutil: T ≡ INTEGER(I4B)(:), REAL(SP)(:), REAL(DP)(:) Action: Copies to a destination array dest the one-dimensional array src, or as much of src as will fit in dest. Returns the number of components copied as n copied, and the number of components not copied as n not copied. The main use of this utility is where src is an expression that returns an array whose size is not known in advance, for example, the value returned by the pack intrinsic.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
swap
992
Chapter 23.
Numerical Recipes Utility Functions for Fortran 90
Reference implementation: n_copied=min(size(src),size(dest)) n_not_copied=size(src)-n_copied dest(1:n_copied)=src(1:n_copied)
User interface (or, “USE nrutil”): FUNCTION reallocate(p,n[,m, . . .]) T, POINTER :: p, reallocate INTEGER(I4B), INTENT(IN) :: n[, m, . . .] END FUNCTION reallocate
Applicable types and ranks: T ≡ any type, rank 1 or greater Types and ranks implemented (overloaded) in nrutil: T ≡ INTEGER(I4B)(:), INTEGER(I4B)(:,:), REAL(SP)(:), REAL(SP)(:,:), CHARACTER(1)(:) Action: Allocates storage for a new array with shape specified by the integer(s) n, m, . . . (equal in number to the rank of pointer p). Then, copies the contents of p’s target (or as much as will fit) into the new storage. Then, deallocates p and returns a pointer to the new storage. The typical use is p=reallocate(p,n[, m, . . .]), which has the effect of changing the allocated size of p while preserving the contents. The reference implementation, below, shows only the case of rank 1. Reference implementation: INTEGER(I4B) :: nold,ierr allocate(reallocate(n),stat=ierr) if (ierr /= 0) call & nrerror(’reallocate: problem in attempt to allocate memory’) if (.not. associated(p)) RETURN nold=size(p) reallocate(1:min(nold,n))=p(1:min(nold,n)) deallocate(p)
23.2 Routines Returning a Location Fortran 90’s intrinsics maxloc and minloc return rank-one arrays. When, in the most frequent usage, their argument is a one-dimensional array, the answer comes back in the inconvenient form of an array containing a single component, which cannot be itself used in a subscript calculation. While there are workaround tricks (e.g., use of the sum intrinsic to convert the array to a scalar), it seems clearer to define routines imaxloc and iminloc that return integers directly. The routine ifirstloc adds a related facility missing among the intrinsics: Return the first true location in a logical array.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
reallocate (reallocate a pointer, preserving contents)
23.2 Routines Returning a Location
993
imaxloc
(location of array maximum as an integer) User interface (or, “USE nrutil”): FUNCTION imaxloc(arr) T, INTENT(IN) :: arr INTEGER(I4B) :: imaxloc END FUNCTION imaxloc
Reference implementation: INTEGER(I4B), DIMENSION(1) :: imax imax=maxloc(arr(:)) imaxloc=imax(1)
iminloc
(location of array minimum as an integer) User interface (or, “USE nrutil”): FUNCTION iminloc(arr) T, INTENT(IN) :: arr INTEGER(I4B) :: iminloc END FUNCTION iminloc
Applicable types and ranks: T ≡ any integer or real type, rank 1 Types and ranks implemented (overloaded) in nrutil: T ≡ REAL(SP)(:) Action: For one-dimensional arrays, identical to the minloc intrinsic, except returns its answer as an integer rather than as minloc’s somewhat awkward rank-one array containing a single component. Reference implementation: INTEGER(I4B), DIMENSION(1) :: imin imin=minloc(arr(:)) iminloc=imin(1)
ifirstloc (returns location of first “true” in a logical vector) User interface (or, “USE nrutil”): FUNCTION ifirstloc(mask) T, INTENT(IN) :: mask INTEGER(I4B) :: ifirstloc END FUNCTION ifirstloc
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Applicable types and ranks: T ≡ any integer or real type, rank 1 Types and ranks implemented (overloaded) in nrutil: T ≡ INTEGER(I4B)(:), REAL(SP)(:) Action: For one-dimensional arrays, identical to the maxloc intrinsic, except returns its answer as an integer rather than as maxloc’s somewhat awkward rank-one array containing a single component.
994
Chapter 23.
Numerical Recipes Utility Functions for Fortran 90
Note that while the reference implementation uses a do-loop, the function is parallelized in nrutil by instead using the merge and maxloc intrinsics. Reference implementation: INTEGER(I4B) :: i do i=1,size(mask) if (mask(i)) then ifirstloc=i return end if end do ifirstloc=i
23.3 Argument Checking and Error Handling It is good programming practice for a routine to check the assumptions (“assertions”) that it makes about the sizes of input arrays, allowed range of numerical arguments, and so forth. The routines assert and assert eq are meant for this kind of use. The routine nrerror is our default error reporting routine.
assert
(exit with error message if any assertion is false)
User interface (or, “USE nrutil”): SUBROUTINE assert(n1,n2,...,string) CHARACTER(LEN=*), INTENT(IN) :: string LOGICAL, INTENT(IN) :: n1,n2,... END SUBROUTINE assert
Action: Embedding program dies gracefully with an error message if any of the logical arguments are false. Typical use is with logical expressions as the actual arguments. nrutil implements and overloads forms with 1, 2, 3, and 4 logical arguments, plus a form with a vector logical argument, LOGICAL, DIMENSION(:), INTENT(IN) ::
n
that is checked by the all(n) intrinsic.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Applicable types and ranks: T ≡ any logical type, rank 1 Types and ranks implemented (overloaded) in nrutil: T ≡ LOGICAL(LGT) Action: Returns the index (subscript value) of the first location, in a one-dimensional logical mask, that has the value .TRUE., or returns size(mask)+1 if all components of mask are .FALSE.
23.3 Argument Checking and Error Handling
995
Reference implementation: if (.not. (n1.and.n2.and...)) then write (*,*) ’nrerror: an assertion failed with this tag:’, string STOP ’program terminated by assert’ end if
assert eq
(exit with error message if integer arguments not all equal)
User interface (or, “USE nrutil”): FUNCTION assert_eq(n1,n2,n3,...,string) CHARACTER(LEN=*), INTENT(IN) :: string INTEGER, INTENT(IN) :: n1,n2,n3,... INTEGER :: assert_eq END FUNCTION assert_eq
Action: Embedding program dies gracefully with an error message if any of the integer arguments are not equal to the first. Otherwise, return the value of the first argument. Typical use is for enforcing equality on the sizes of arrays passed to a subprogram. nrutil implements and overloads forms with 1, 2, 3, and 4 integer arguments, plus a form with a vector integer argument, INTEGER, DIMENSION(:), INTENT(IN) ::
n
that is checked by the conditional if (all(nn(2:)==nn(1))). Reference implementation: if (n1==n2.and.n2==n3.and...) then assert_eq=n1 else write (*,*) ’nrerror: an assert_eq failed with this tag:’, string STOP ’program terminated by assert_eq’ end if
nrerror
(report error message and stop)
User interface (or, “USE nrutil”): SUBROUTINE nrerror(string) CHARACTER(LEN=*), INTENT(IN) :: string END SUBROUTINE nrerror
Action: This is the minimal error handler used in this book. In applications of any complexity, it is intended only as a placeholder for a user’s more complicated error handling strategy. Reference implementation: write (*,*) ’nrerror: ’,string STOP ’program terminated by nrerror’
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
996
Chapter 23.
Numerical Recipes Utility Functions for Fortran 90
23.4 Routines for Polynomials and Recurrences Apart from programming convenience, these routines are designed to allow for nontrivial parallel implementations, as discussed in §22.2 and §22.3.
arth (returns arithmetic progression as an array) User interface (or, “USE nrutil”): FUNCTION arth(first,increment,n) T, INTENT(IN) :: first,increment INTEGER(I4B), INTENT(IN) :: n T, DIMENSION(n) [or, 1 rank higher than T]:: arth END FUNCTION arth
Applicable types and ranks: T ≡ any numerical type, any rank Types and ranks implemented (overloaded) in nrutil: T ≡ INTEGER(I4B), REAL(SP), REAL(DP) Action: Returns an array of length n containing an arithmetic progression whose first value is first and whose increment is increment. If first and increment have rank greater than zero, returns an array of one larger rank, with the last subscript having size n and indexing the progressions. Note that the following reference implementation (for the scalar case) is definitional only, and neither parallelized nor optimized for roundoff error. See §22.2 and Appendix C1 for implementation by subvector scaling. Reference implementation: INTEGER(I4B) :: k if (n > 0) arth(1)=first do k=2,n arth(k)=arth(k-1)+increment end do
geop (returns geometric progression as an array) User interface (or, “USE nrutil”): FUNCTION geop(first,factor,n) T, INTENT(IN) :: first,factor INTEGER(I4B), INTENT(IN) :: n T, DIMENSION(n) [or, 1 rank higher than T]:: geop END FUNCTION geop
Applicable types and ranks: T ≡ any numerical type, any rank Types and ranks implemented (overloaded) in nrutil: T ≡ INTEGER(I4B), REAL(SP), REAL(DP), REAL(DP)(:), COMPLEX(SPC)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
23.4 Routines for Polynomials and Recurrences
997
Reference implementation: INTEGER(I4B) :: k if (n > 0) geop(1)=first do k=2,n geop(k)=geop(k-1)*factor end do
cumsum
(cumulative sum on an array, with optional additive seed) User interface (or, “USE nrutil”): FUNCTION cumsum(arr,seed) T, DIMENSION(:), INTENT(IN) :: arr T, OPTIONAL, INTENT(IN) :: seed T, DIMENSION(size(arr)), INTENT(OUT) :: cumsum END FUNCTION cumsum
Applicable types and ranks: T ≡ any numerical type Types and ranks implemented (overloaded) in nrutil: T ≡ INTEGER(I4B), REAL(SP) Action: Given the rank 1 array arr of type T, returns an array of identical type and size containing the cumulative sums of arr. If the optional argument seed is present, it is added to the first component (and therefore, by cumulation, all components) of the result. See §22.2 for parallelization ideas. Reference implementation: INTEGER(I4B) :: n,j T :: sd n=size(arr) if (n == 0) return sd=0.0 if (present(seed)) sd=seed cumsum(1)=arr(1)+sd do j=2,n cumsum(j)=cumsum(j-1)+arr(j) end do
cumprod
(cumulative prod on an array, with optional multiplicative seed) User interface (or, “USE nrutil”): FUNCTION cumprod(arr,seed) T, DIMENSION(:), INTENT(IN) :: arr T, OPTIONAL, INTENT(IN) :: seed T, DIMENSION(size(arr)), INTENT(OUT) :: cumprod END FUNCTION cumprod
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Action: Returns an array of length n containing a geometric progression whose first value is first and whose multiplier is factor. If first and factor have rank greater than zero, returns an array of one larger rank, with the last subscript having size n and indexing the progression. Note that the following reference implementation (for the scalar case) is definitional only, and neither parallelized nor optimized for roundoff error. See §22.2 and Appendix C1 for implementation by subvector scaling.
998
Chapter 23.
Numerical Recipes Utility Functions for Fortran 90
INTEGER(I4B) :: n,j T :: sd n=size(arr) if (n == 0) return sd=1.0 if (present(seed)) sd=seed cumprod(1)=arr(1)*sd do j=2,n cumprod(j)=cumprod(j-1)*arr(j) end do
poly (polynomial evaluation) User interface (or, “USE nrutil”): FUNCTION poly(x,coeffs,mask) T,, DIMENSION(:,...), INTENT(IN) :: x T, DIMENSION(:), INTENT(IN) :: coeffs LOGICAL(LGT), DIMENSION(:,...), OPTIONAL, INTENT(IN) :: mask T :: poly END FUNCTION poly
Applicable types and ranks: T ≡ any numerical type (x may be scalar or have any rank; x and coeffs may have different numerical types) Types and ranks implemented (overloaded) in nrutil: T ≡ various combinations of REAL(SP), REAL(SP)(:), REAL(DP), REAL(DP)(:), COMPLEX(SPC) (see Appendix C1 for details) Action: Returns a scalar value or array with the same type and shape as x, containing the result of evaluating the polynomial with one-dimensional coefficient vector coeffs on each component of x. The optional argument mask, if present, has the same shape as x, and suppresses evaluation of the polynomial where its components are .false.. The following reference code shows the case where mask is not present. (The other case can be included by overloading.)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Applicable types and ranks: T ≡ any numerical type Types and ranks implemented (overloaded) in nrutil: T ≡ REAL(SP) Action: Given the rank 1 array arr of type T, returns an array of identical type and size containing the cumulative products of arr. If the optional argument seed is present, it is multiplied into the first component (and therefore, by cumulation, into all components) of the result. See §22.2 for parallelization ideas. Reference implementation:
23.4 Routines for Polynomials and Recurrences
999
Reference implementation:
poly term (partial cumulants of a polynomial) User interface (or, “USE nrutil”): FUNCTION poly_term(a,x) T, DIMENSION(:), INTENT(IN) :: a T, INTENT(IN) :: x T, DIMENSION(size(a)) :: poly_term END FUNCTION poly_term
Applicable types and ranks: T ≡ any numerical type Types and ranks implemented (overloaded) in nrutil: T ≡ REAL(SP), COMPLEX(SPC) Action: Returns an array of type and size the same as the one-dimensional array a, containing the partial cumulants of the polynomial with coefficients a (arranged from highest-order to lowest-order coefficients, n.b.) evaluated at x. This is equivalent to synthetic division, and can be parallelized. See §22.3. Note that the order of arguments is reversed in poly and poly term — each routine returns a value with the size and shape of the first argument, the usual Fortran 90 convention. Reference implementation: INTEGER(I4B) :: n,j n=size(a) if (n 2) then Here is an example of a block IF-structure. jm=mm+1 else jy=jy-1 jm=mm+13 end if julday=int(365.25_sp*jy)+int(30.6001_sp*jm)+id+1720995 if (id+31*(mm+12*iyyy) >= IGREG) then Test whether to change to Gregorian Calja=int(0.01_sp*jy) endar. julday=julday+2-ja+int(0.25_sp*ja) end if END FUNCTION julday
PROGRAM badluk USE nrtype USE nr, ONLY : flmoon,julday IMPLICIT NONE INTEGER(I4B) :: ic,icon,idwk,ifrac,im,iyyy,jd,jday,n INTEGER(I4B) :: iybeg=1900,iyend=2000 The range of dates to be searched. REAL(SP) :: frac REAL(SP), PARAMETER :: TIMZON=-5.0_sp/24.0_sp Time zone −5 is Eastern Standard Time. write (*,’(1x,a,i5,a,i5)’) ’Full moons on Friday the 13th from’,& iybeg,’ to’,iyend do iyyy=iybeg,iyend Loop over each year, do im=1,12 and each month. jday=julday(im,13,iyyy) Is the 13th a Friday? idwk=mod(jday+1,7)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
When the arguments of a merge are vectors, parallelization by the compiler is straightforward as an array parallel operation (see p. 964). Less obvious is how the scalar case, as above, is handled. For small-scale parallel (SSP) machines, the natural gain is via speculative evaluation of both of the first two arguments simultaneously with evaluation of the test. A good compiler should not penalize a scalar machine for use of either the scalar or vector merge construction. The Fortran 90 standard states that “it is not necessary for a processor to evaluate all of the operands of an expression, or to evaluate entirely each operand, if the value of the expression can be determined otherwise.” Therefore, for each test on a scalar machine, only one or the other of the first two argument components need be evaluated.
1012
Chapter B1.
Preliminaries
(in julday), ...TIMZON=-5.0_sp/24.0_sp (in badluk) These are two examples of initialization expressions for “named constants” (that is, PARAMETERs). Because the initialization expressions will generally be evaluated at compile time, Fortran 90 puts some restrictions on what kinds of intrinsic functions they can contain. Although the evaluation of a real expression like -5.0_sp/24.0_sp ought to give identical results at compile time and at execution time, all the way down to the least significant bit, in our opinion the conservative programmer shouldn’t count on strict identity at the level of floating-point roundoff error. (In the special case of cross-compilers, such roundoff-level discrepancies between compile time and run time are almost inevitable.)
f90
...IGREG=15+31*(10+12*1582)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (idwk == 5) then n=12.37_sp*(iyyy-1900+(im-0.5_sp)/12.0_sp) This value n is a first approximation to how many full moons have occurred since 1900. We will feed it into the phase routine and adjust it up or down until we determine that our desired 13th was or was not a full moon. The variable icon signals the direction of adjustment. icon=0 do call flmoon(n,2,jd,frac) Get date of full moon n. ifrac=nint(24.0_sp*(frac+TIMZON)) Convert to hours in correct time if (ifrac < 0) then zone. jd=jd-1 Convert from Julian Days beginning at noon ifrac=ifrac+24 to civil days beginning at midnight. end if if (ifrac > 12) then jd=jd+1 ifrac=ifrac-12 else ifrac=ifrac+12 end if if (jd == jday) then Did we hit our target day? write (*,’(/1x,i2,a,i2,a,i4)’) im,’/’,13,’/’,iyyy write (*,’(1x,a,i2,a)’) ’Full moon ’,ifrac,& ’ hrs after midnight (EST).’ Don’t worry if you are unfamiliar with FORTRAN’s esoteric input/output statements; very few programs in this book do any input/output. exit Part of the break-structure, case of a match. else Didn’t hit it. ic=isign(1,jday-jd) if (ic == -icon) exit Another break, case of no match. icon=ic n=n+ic end if end do end if end do end do END PROGRAM badluk
Chapter B1. Preliminaries
1013
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE caldat(julian,mm,id,iyyy) USE nrtype IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: julian INTEGER(I4B), INTENT(OUT) :: mm,id,iyyy Inverse of the function julday given above. Here julian is input as a Julian Day Number, and the routine outputs mm,id, and iyyy as the month, day, and year on which the specified Julian Day started at noon. INTEGER(I4B) :: ja,jalpha,jb,jc,jd,je INTEGER(I4B), PARAMETER :: IGREG=2299161 if (julian >= IGREG) then Cross-over to Gregorian Calendar produces this jalpha=int(((julian-1867216)-0.25_sp)/36524.25_sp) correction. ja=julian+1+jalpha-int(0.25_sp*jalpha) else if (julian < 0) then Make day number positive by adding integer numja=julian+36525*(1-julian/36525) ber of Julian centuries, then subtract them else off at the end. ja=julian end if jb=ja+1524 jc=int(6680.0_sp+((jb-2439870)-122.1_sp)/365.25_sp) jd=365*jc+int(0.25_sp*jc) je=int((jb-jd)/30.6001_sp) id=jb-jd-int(30.6001_sp*je) mm=je-1 if (mm > 12) mm=mm-12 iyyy=jc-4715 if (mm > 2) iyyy=iyyy-1 if (iyyy irc(1) icol => irc(2) ipiv=0 do i=1,n Main loop over columns to be reduced. lpiv = (ipiv == 0) Begin search for a pivot element. irc=maxloc(abs(a),outerand(lpiv,lpiv)) ipiv(icol)=ipiv(icol)+1 if (ipiv(icol) > 1) call nrerror(’gaussj: singular matrix (1)’) We now have the pivot element, so we interchange rows, if needed, to put the pivot element on the diagonal. The columns are not physically interchanged, only relabeled: indxc(i), the column of the ith pivot element, is the ith column that is reduced, while indxr(i) is the row in which that pivot element was originally located. If indxr(i) = indxc(i) there is an implied column interchange. With this form of bookkeeping, the solution b’s will end up in the correct order, and the inverse matrix will be scrambled by columns. if (irow /= icol) then call swap(a(irow,:),a(icol,:)) call swap(b(irow,:),b(icol,:)) end if indxr(i)=irow We are now ready to divide the pivot row by the pivot indxc(i)=icol element, located at irow and icol. if (a(icol,icol) == 0.0) & call nrerror(’gaussj: singular matrix (2)’) pivinv=1.0_sp/a(icol,icol) a(icol,icol)=1.0 a(icol,:)=a(icol,:)*pivinv b(icol,:)=b(icol,:)*pivinv dumc=a(:,icol) Next, we reduce the rows, except for the pivot one, of a(:,icol)=0.0 course.
1014
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B2. Solution of Linear Algebraic Equations
Chapter B2.
Solution of Linear Algebraic Equations
1015
The maxloc intrinsic returns the location of the maximum value of an array as an integer array, in this case of size 2. Pre-pointing pointer variables to components of the array that will be thus set makes possible convenient references to the desired row and column positions.
f90
irow => irc(1) ... icol => irc(2)
irc=maxloc(abs(a),outerand(lpiv,lpiv)) The combination of maxloc and one of the outer... routines from nrutil allows for a very concise formulation. If this task is done with loops, it becomes the ungainly “flying vee,” aa=0.0 do i=1,n if (lpiv(i)) then do j=1,n if (lpiv(j)) then if (abs(a(i,j)) > aa) then aa=abs(a(i,j)) irow=i icol=j endif endif end do end do end do call swap(a(irow,:),a(icol,:)) The swap routine (in nrutil) is concise and convenient. Fortran 90’s ability to overload multiple routines onto a single name is vital here: Much of the convenience would vanish if we had to remember variant routine names for each variable type and rank of object that might be swapped. Even better, here, than overloading would be if Fortran 90 allowed user-written elemental procedures (procedures with unspecified or arbitrary rank and shape), like the intrinsic elemental procedures built into the language. Fortran 95 will, but Fortran 90 doesn’t.
One quick (if superficial) test for how much parallelism is achieved in a Fortran 90 routine is to count its do-loops, and compare that number to the number of do-loops in the Fortran 77 version of the same routine. Here, in gaussj, 13 do-loops are reduced to 2. a(1:icol-1,:)=... b(1:icol-1,:)=... a(icol+1:,:)=... b(icol+1:,:)=...
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
a(icol,icol)=pivinv a(1:icol-1,:)=a(1:icol-1,:)-outerprod(dumc(1:icol-1),a(icol,:)) b(1:icol-1,:)=b(1:icol-1,:)-outerprod(dumc(1:icol-1),b(icol,:)) a(icol+1:,:)=a(icol+1:,:)-outerprod(dumc(icol+1:),a(icol,:)) b(icol+1:,:)=b(icol+1:,:)-outerprod(dumc(icol+1:),b(icol,:)) end do It only remains to unscramble the solution in view of the column interchanges. We do this by interchanging pairs of columns in the reverse order that the permutation was built up. do l=n,1,-1 call swap(a(:,indxr(l)),a(:,indxc(l))) end do END SUBROUTINE gaussj
1016
Chapter B2.
Solution of Linear Algebraic Equations
SUBROUTINE ludcmp(a,indx,d) USE nrtype; USE nrutil, ONLY : assert_eq,imaxloc,nrerror,outerprod,swap IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a INTEGER(I4B), DIMENSION(:), INTENT(OUT) :: indx REAL(SP), INTENT(OUT) :: d Given an N × N input matrix a, this routine replaces it by the LU decomposition of a rowwise permutation of itself. On output, a is arranged as in equation (2.3.14); indx is an output vector of length N that records the row permutation effected by the partial pivoting; d is output as ±1 depending on whether the number of row interchanges was even or odd, respectively. This routine is used in combination with lubksb to solve linear equations or invert a matrix. REAL(SP), DIMENSION(size(a,1)) :: vv vv stores the implicit scaling of each row. REAL(SP), PARAMETER :: TINY=1.0e-20_sp A small number. INTEGER(I4B) :: j,n,imax n=assert_eq(size(a,1),size(a,2),size(indx),’ludcmp’) d=1.0 No row interchanges yet. vv=maxval(abs(a),dim=2) Loop over rows to get the implicit scaling if (any(vv == 0.0)) call nrerror(’singular matrix in ludcmp’) information. There is a row of zeros. vv=1.0_sp/vv Save the scaling. do j=1,n imax=(j-1)+imaxloc(vv(j:n)*abs(a(j:n,j))) Find the pivot row. if (j /= imax) then Do we need to interchange rows? call swap(a(imax,:),a(j,:)) Yes, do so... d=-d ...and change the parity of d. vv(imax)=vv(j) Also interchange the scale factor. end if indx(j)=imax if (a(j,j) == 0.0) a(j,j)=TINY If the pivot element is zero the matrix is singular (at least to the precision of the algorithm). For some applications on singular matrices, it is desirable to substitute TINY for zero. a(j+1:n,j)=a(j+1:n,j)/a(j,j) Divide by the pivot element. a(j+1:n,j+1:n)=a(j+1:n,j+1:n)-outerprod(a(j+1:n,j),a(j,j+1:n)) Reduce remaining submatrix. end do END SUBROUTINE ludcmp
A single statement finds the maximum absolute value in each row. Fortran 90 intrinsics like maxval generally “do their thing” in the dimension specified by dim and return a result with a shape corresponding to the other dimensions. Thus, here, vv’s size is that of the first dimension of a.
f90
vv=maxval(abs(a),dim=2)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Here the same operation is applied to every row of a, and to every row of b, except row number icol. On a massively multiprocessor (MMP) machine it would be better to use a logical mask and do all of a in a single statement, all of b in another one. For a small-scale parallel (SSP) machine, the lines as written should saturate the machine’s concurrency, and they avoid the additional overhead of testing the mask. This would be a good place to point out, however, that linear algebra routines written in Fortran 90 are likely never to be competitive with the hand-coded library routines that are generally supplied as part of MMP programming environments. If you are using our routines instead of library routines written specifically for your architecture, you are wasting cycles!
Chapter B2.
Solution of Linear Algebraic Equations
1017
a(j+1:n,j+1:n)=a(j+1:n,j+1:n)-outerprod(a(j+1:n,j),a(j,j+1:n))
The Fortran 77 version of ludcmp, using Crout’s algorithm for the reduction, does not parallelize well: The elements are updated by O(N 2 ) separate dot product operations in a particular order. Here we use a slightly different reduction, termed “outer product Gaussian elimination” by Golub and Van Loan [1], that requires just N steps of matrix-parallel reduction. (See their §3.2.3 and §3.2.9 for the algorithm, and their §3.4.1 to understand how the pivoting is performed.) We use nrutil’s routine outerprod instead of the more cumbersome pure Fortran 90 construction: spread(a(j+1:n,j),dim=2,ncopies=n-j)*spread(a(j,j+1:n),dim=1,ncopies=n-j)
SUBROUTINE lubksb(a,indx,b) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(IN) :: a INTEGER(I4B), DIMENSION(:), INTENT(IN) :: indx REAL(SP), DIMENSION(:), INTENT(INOUT) :: b Solves the set of N linear equations A · X = B. Here the N × N matrix a is input, not as the original matrix A, but rather as its LU decomposition, determined by the routine ludcmp. indx is input as the permutation vector of length N returned by ludcmp. b is input as the right-hand-side vector B, also of length N , and returns with the solution vector X. a and indx are not modified by this routine and can be left in place for successive calls with different right-hand sides b. This routine takes into account the possibility that b will begin with many zero elements, so it is efficient for use in matrix inversion. INTEGER(I4B) :: i,n,ii,ll REAL(SP) :: summ n=assert_eq(size(a,1),size(a,2),size(indx),’lubksb’) ii=0 When ii is set to a positive value, it will become the index of the first nonvanishing element of b. We now do do i=1,n the forward substitution, equation (2.3.6). The only new ll=indx(i) wrinkle is to unscramble the permutation as we go. summ=b(ll) b(ll)=b(i) if (ii /= 0) then summ=summ-dot_product(a(i,ii:i-1),b(ii:i-1)) else if (summ /= 0.0) then ii=i A nonzero element was encountered, so from now on we will end if have to do the dot product above. b(i)=summ end do do i=n,1,-1 Now we do the backsubstitution, equation (2.3.7). b(i) = (b(i)-dot_product(a(i,i+1:n),b(i+1:n)))/a(i,i) end do END SUBROUTINE lubksb
Conceptually, the search for the first nonvanishing element of b (index ii) should be moved out of the first do-loop. However, in practice, the need to unscramble the permutation, and also considerations of performance
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
imax=(j-1)+imaxloc(vv(j:n)*abs(a(j:n,j)) Here we see why the nrutil routine imaxloc is handy: We want the index, in the range 1:n of a quantity to be searched for only in the limited range j:n. Using imaxloc, we just add back the proper offset of j-1. (Using only Fortran 90 intrinsics, we could write imax=(j-1)+sum(maxloc(vv(j:n)*abs(a(j:n,j)))), but the use of sum just to turn an array of length 1 into a scalar seems sufficiently confusing as to be avoided.)
1018
Chapter B2.
Solution of Linear Algebraic Equations
on scalar machines, cause us to write this very scalar-looking code. The performance penalty on parallel machines should be minimal.
SUBROUTINE tridag_ser(a,b,c,r,u) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: a,b,c,r REAL(SP), DIMENSION(:), INTENT(OUT) :: u Solves for a vector u of size N the tridiagonal linear set given by equation (2.4.1) using a serial algorithm. Input vectors b (diagonal elements) and r (right-hand sides) have size N , while a and c (off-diagonal elements) are size N − 1. REAL(SP), DIMENSION(size(b)) :: gam One vector of workspace, gam is needed. INTEGER(I4B) :: n,j REAL(SP) :: bet n=assert_eq((/size(a)+1,size(b),size(c)+1,size(r),size(u)/),’tridag_ser’) bet=b(1) if (bet == 0.0) call nrerror(’tridag_ser: Error at code stage 1’) If this happens then you should rewrite your equations as a set of order N − 1, with u2 trivially eliminated. u(1)=r(1)/bet do j=2,n Decomposition and forward substitution. gam(j)=c(j-1)/bet bet=b(j)-a(j-1)*gam(j) if (bet == 0.0) & Algorithm fails; see below routine in Vol. 1. call nrerror(’tridag_ser: Error at code stage 2’) u(j)=(r(j)-a(j-1)*u(j-1))/bet end do do j=n-1,1,-1 Backsubstitution. u(j)=u(j)-gam(j+1)*u(j+1) end do END SUBROUTINE tridag_ser
RECURSIVE SUBROUTINE tridag_par(a,b,c,r,u) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : tridag_ser IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: a,b,c,r REAL(SP), DIMENSION(:), INTENT(OUT) :: u Solves for a vector u of size N the tridiagonal linear set given by equation (2.4.1) using a parallel algorithm. Input vectors b (diagonal elements) and r (right-hand sides) have size N , while a and c (off-diagonal elements) are size N − 1. INTEGER(I4B), PARAMETER :: NPAR_TRIDAG=4 Determines when serial algorithm is inINTEGER(I4B) :: n,n2,nm,nx voked. REAL(SP), DIMENSION(size(b)/2) :: y,q,piva REAL(SP), DIMENSION(size(b)/2-1) :: x,z REAL(SP), DIMENSION(size(a)/2) :: pivc n=assert_eq((/size(a)+1,size(b),size(c)+1,size(r),size(u)/),’tridag_par’) if (n < NPAR_TRIDAG) then call tridag_ser(a,b,c,r,u) else if (maxval(abs(b(1:n))) == 0.0) & Algorithm fails; see below routine in Vol. 1.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Serial and parallel algorithms for tridiagonal problems are quite different. We therefore provide separate routines tridag ser and tridag par. In the MODULE nr interface file, one or the other of these (your choice) is given the generic name tridag. Of course, either version will work correctly on any computer; it is only a question of efficiency. See §22.2 for the numbering of the equation coefficients, and for a description of the parallel algorithm.
Chapter B2.
Solution of Linear Algebraic Equations
1019
The serial version tridag ser is called when the routine has recursed its way down to sufficiently small subproblems. The point at which this occurs is determined by the parameter NPAR TRIDAG whose optimal value is likely machine-dependent. Notice that tridag ser must here be called by its specific name, not by the generic tridag (which might itself be overloaded with either tridag ser or tridag par).
f90
SUBROUTINE banmul(a,m1,m2,x,b) USE nrtype; USE nrutil, ONLY : assert_eq,arth IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(IN) :: a INTEGER(I4B), INTENT(IN) :: m1,m2 REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(OUT) :: b Matrix multiply b = A · x, where A is band diagonal with m1 rows below the diagonal and m2 rows above. If the input vector x and output vector b are of length N , then the array a(1:N ,1:m1+m2+1) stores A as follows: The diagonal elements are in a(1:N ,m1+1). Subdiagonal elements are in a(j :N ,1:m1) (with j > 1 appropriate to the number of elements on each subdiagonal). Superdiagonal elements are in a(1:j ,m1+2:m1+m2+1) with j < N appropriate to the number of elements on each superdiagonal. INTEGER(I4B) :: m,n n=assert_eq(size(a,1),size(b),size(x),’banmul: n’) m=assert_eq(size(a,2),m1+m2+1,’banmul: m’) b=sum(a*eoshift(spread(x,dim=2,ncopies=m), & dim=1,shift=arth(-m1,1,m)),dim=2) END SUBROUTINE banmul
f90
b=sum(a*eoshift(spread(x,dim=2,ncopies=m), & dim=1,shift=arth(-m1,1,m)),dim=2)
This is a good example of Fortran 90 at both its best and its worst: best, because it allows quite subtle combinations of fully parallel operations to be built up; worst, because the resulting code is virtually incomprehensible!
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call nrerror(’tridag_par: possible singular matrix’) n2=size(y) nm=size(pivc) nx=size(x) piva = a(1:n-1:2)/b(1:n-1:2) Zero the odd a’s and even c’s, giving x, pivc = c(2:n-1:2)/b(3:n:2) y, z, q. y(1:nm) = b(2:n-1:2)-piva(1:nm)*c(1:n-2:2)-pivc*a(2:n-1:2) q(1:nm) = r(2:n-1:2)-piva(1:nm)*r(1:n-2:2)-pivc*r(3:n:2) if (nm < n2) then y(n2) = b(n)-piva(n2)*c(n-1) q(n2) = r(n)-piva(n2)*r(n-1) end if x = -piva(2:n2)*a(2:n-2:2) z = -pivc(1:nx)*c(3:n-1:2) call tridag_par(x,y,z,q,u(2:n:2)) Recurse and get even u’s. u(1) = (r(1)-c(1)*u(2))/b(1) Substitute and get odd u’s. u(3:n-1:2) = (r(3:n-1:2)-a(2:n-2:2)*u(2:n-2:2) & -c(3:n-1:2)*u(4:n:2))/b(3:n-1:2) if (nm == n2) u(n)=(r(n)-a(n-1)*u(n-1))/b(n) end if END SUBROUTINE tridag_par
1020
Chapter B2.
Solution of Linear Algebraic Equations
SUBROUTINE bandec(a,m1,m2,al,indx,d) USE nrtype; USE nrutil, ONLY : assert_eq,imaxloc,swap,arth IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a INTEGER(I4B), INTENT(IN) :: m1,m2 REAL(SP), DIMENSION(:,:), INTENT(OUT) :: al INTEGER(I4B), DIMENSION(:), INTENT(OUT) :: indx REAL(SP), INTENT(OUT) :: d REAL(SP), PARAMETER :: TINY=1.0e-20_sp Given an N × N band diagonal matrix A with m1 subdiagonal rows and m2 superdiagonal rows, compactly stored in the array a(1:N ,1:m1+m2+1) as described in the comment for routine banmul, this routine constructs an LU decomposition of a rowwise permutation of A. The upper triangular matrix replaces a, while the lower triangular matrix is returned in al(1:N ,1:m1). indx is an output vector of length N that records the row permutation effected by the partial pivoting; d is output as ±1 depending on whether the number of row interchanges was even or odd, respectively. This routine is used in combination with banbks to solve band-diagonal sets of equations. INTEGER(I4B) :: i,k,l,mdum,mm,n REAL(SP) :: dum n=assert_eq(size(a,1),size(al,1),size(indx),’bandec: n’) mm=assert_eq(size(a,2),m1+m2+1,’bandec: mm’) mdum=assert_eq(size(al,2),m1,’bandec: mdum’) a(1:m1,:)=eoshift(a(1:m1,:),dim=2,shift=arth(m1,-1,m1)) Rearrange the storage a d=1.0 bit. do k=1,n For each row... l=min(m1+k,n) i=imaxloc(abs(a(k:l,1)))+k-1 Find the pivot element. dum=a(i,1) if (dum == 0.0) a(k,1)=TINY Matrix is algorithmically singular, but proceed anyway with TINY pivot (desirable in some applications). indx(k)=i if (i /= k) then Interchange rows. d=-d call swap(a(k,1:mm),a(i,1:mm)) end if do i=k+1,l Do the elimination. dum=a(i,1)/a(k,1) al(k,i-k)=dum a(i,1:mm-1)=a(i,2:mm)-dum*a(k,2:mm) a(i,mm)=0.0 end do end do END SUBROUTINE bandec
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
What is going on becomes clearer if we imagine a temporary array y with a declaration like REAL(SP), DIMENSION(size(a,1),size(a,2)) :: y. Then, the above single line decomposes into y=spread(x,dim=2,ncopies=m) [Duplicate x into columns of y.] y=eoshift(y,dim=1,shift=arth(-m1,1,m)) [Shift columns by a linear progression.] b=sum(a*y,dim=2) [Multiply by the band-diagonal elements, and sum.] We use here a relatively rare subcase of the eoshift intrinsic, using a vector value for the shift argument to accomplish the simultaneous shifting of a bunch of columns, by different amounts (here specified by the linear progression returned by arth). If you still don’t see how this accomplishes the multiplication of a band diagonal matrix by a vector, work through a simple example by hand.
Chapter B2. Solution of Linear Algebraic Equations
f90
a(1:m1,:)=eoshift(a(1:m1,:),...
1021
See similar discussion of eoshift
for banmul, just above.
i=imaxloc(abs(a(k:l,1)))+k-1
See discussion of imaxloc on p. 1017.
SUBROUTINE banbks(a,m1,m2,al,indx,b) USE nrtype; USE nrutil, ONLY : assert_eq,swap IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(IN) :: a,al INTEGER(I4B), INTENT(IN) :: m1,m2 INTEGER(I4B), DIMENSION(:), INTENT(IN) :: indx REAL(SP), DIMENSION(:), INTENT(INOUT) :: b Given the arrays a, al, and indx as returned from bandec, and given a right-hand-side vector b, solves the band diagonal linear equations A·x = b. The solution vector x overwrites b. The other input arrays are not modified, and can be left in place for successive calls with different right-hand sides. INTEGER(I4B) :: i,k,l,mdum,mm,n n=assert_eq(size(a,1),size(al,1),size(b),size(indx),’banbks: n’) mm=assert_eq(size(a,2),m1+m2+1,’banbks: mm’) mdum=assert_eq(size(al,2),m1,’banbks: mdum’) do k=1,n Forward substitution, unscrambling the permuted rows as we l=min(n,m1+k) go. i=indx(k) if (i /= k) call swap(b(i),b(k)) b(k+1:l)=b(k+1:l)-al(k,1:l-k)*b(k) end do do i=n,1,-1 Backsubstitution. l=min(mm,n-i+1) b(i)=(b(i)-dot_product(a(i,2:l),b(1+i:i+l-1)))/a(i,1) end do END SUBROUTINE banbks
As for bandec, the routine banbks is not parallelized on the large dimension N , though it does give the compiler the opportunity for ample small-scale parallelization inside the loops.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Notice that the above is not well parallelized for MMP machines: the outer do-loop is done N times, where N , the diagonal length, is potentially the largest dimension in the problem. Small-scale parallel (SSP) machines, and scalar machines, are not disadvantaged, because the parallelism of order mm=m1+m2+1 in the inner loops can be enough to saturate their concurrency. We don’t know of an N -parallel algorithm for decomposing band diagonal matrices, at least one that has any reasonably concise expression in Fortran 90. Conceptually, one can view a band diagonal matrix as a block tridiagonal matrix, and then apply the same recursive strategy as was used in tridag par. However, the implementation details of this are daunting. (We would welcome a user-contributed routine, clear, concise, and with parallelism of order N .)
1022
Chapter B2.
Solution of Linear Algebraic Equations
This overloaded version of the nrutil routine assert eq makes use of a trick for passing a variable number of scalar arguments to a routine: Put them into an array constructor, (/.../), and pass the array. The receiving routine can use the size intrinsic to count them. The technique has some obvious limitations: All the arguments in the array must be of the same type; and the arguments are passed, in effect, by value, not by address, so they must be, in effect, INTENT(IN).
f90
assert_eq((/.../),’mprove’)
r=matmul(real(a,dp),real(x,dp))-real(b,dp) Since Fortran 90’s elemental intrinsics operate with the type of their arguments, we can use the real(...,dp)’s to force the matmul matrix multiplication to be done in double precision, which is what we want. In Fortran 77, we would have to do the matrix multiplication with temporary double precision variables, both inconvenient and (since Fortran 77 has no dynamic memory allocation) a waste of memory.
SUBROUTINE svbksb_sp(u,w,v,b,x) USE nrtype; USE nrutil, ONLY : assert_eq REAL(SP), DIMENSION(:,:), INTENT(IN) :: u,v REAL(SP), DIMENSION(:), INTENT(IN) :: w,b REAL(SP), DIMENSION(:), INTENT(OUT) :: x Solves A · X = B for a vector X, where A is specified by the arrays u, v, w as returned by svdcmp. Here u is M × N , v is N × N , and w is of length N . b is the M -dimensional input right-hand side. x is the N -dimensional output solution vector. No input quantities are destroyed, so the routine may be called sequentially with different b’s. INTEGER(I4B) :: mdum,ndum REAL(SP), DIMENSION(size(x)) :: tmp mdum=assert_eq(size(u,1),size(b),’svbksb_sp: mdum’) ndum=assert_eq((/size(u,2),size(v,1),size(v,2),size(w),size(x)/),& ’svbksb_sp: ndum’) where (w /= 0.0) tmp=matmul(b,u)/w Calculate diag(1/wj )U T B, elsewhere tmp=0.0 but replace 1/wj by zero if wj = 0. end where
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE mprove(a,alud,indx,b,x) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : lubksb IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(IN) :: a,alud INTEGER(I4B), DIMENSION(:), INTENT(IN) :: indx REAL(SP), DIMENSION(:), INTENT(IN) :: b REAL(SP), DIMENSION(:), INTENT(INOUT) :: x Improves a solution vector x of the linear set of equations A · X = B. The N × N matrix a and the N -dimensional vectors b and x are input. Also input is alud, the LU decomposition of a as returned by ludcmp, and the N -dimensional vector indx also returned by that routine. On output, only x is modified, to an improved set of values. INTEGER(I4B) :: ndum REAL(SP), DIMENSION(size(a,1)) :: r ndum=assert_eq((/size(a,1),size(a,2),size(alud,1),size(alud,2),size(b),& size(x),size(indx)/),’mprove’) r=matmul(real(a,dp),real(x,dp))-real(b,dp) Calculate the right-hand side, accumulating the residual in double precision. call lubksb(alud,indx,r) Solve for the error term, x=x-r and subtract it from the old solution. END SUBROUTINE mprove
Chapter B2. Solution of Linear Algebraic Equations
x=matmul(v,tmp) END SUBROUTINE svbksb_sp
1023
Matrix multiply by V to get answer.
Normally, when a where ...elsewhere construction is used to set a variable (here tmp) to one or another value, we like to replace it with a merge expression. Here, however, the where is required to guarantee that a division by zero doesn’t occur. The rule is that where will never evaluate expressions that are excluded by the mask in the where line, but other constructions, like merge, might perform speculative evaluation of more than one possible outcome before selecting the applicable one. Because singular value decomposition is something that one often wants to do in double precision, we include a double-precision version. In nr, the single- and double-precision versions are overloaded onto the name svbksb. SUBROUTINE svbksb_dp(u,w,v,b,x) USE nrtype; USE nrutil, ONLY : assert_eq REAL(DP), DIMENSION(:,:), INTENT(IN) :: u,v REAL(DP), DIMENSION(:), INTENT(IN) :: w,b REAL(DP), DIMENSION(:), INTENT(OUT) :: x INTEGER(I4B) :: mdum,ndum REAL(DP), DIMENSION(size(x)) :: tmp mdum=assert_eq(size(u,1),size(b),’svbksb_dp: mdum’) ndum=assert_eq((/size(u,2),size(v,1),size(v,2),size(w),size(x)/),& ’svbksb_dp: ndum’) where (w /= 0.0) tmp=matmul(b,u)/w elsewhere tmp=0.0 end where x=matmul(v,tmp) END SUBROUTINE svbksb_dp
SUBROUTINE svdcmp_sp(a,w,v) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror,outerprod USE nr, ONLY : pythag IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a REAL(SP), DIMENSION(:), INTENT(OUT) :: w REAL(SP), DIMENSION(:,:), INTENT(OUT) :: v Given an M × N matrix a, this routine computes its singular value decomposition, A = U · W · V T . The matrix U replaces a on output. The diagonal matrix of singular values W is output as the N -dimensional vector w. The N × N matrix V (not the transpose V T ) is output as v. INTEGER(I4B) :: i,its,j,k,l,m,n,nm REAL(SP) :: anorm,c,f,g,h,s,scale,x,y,z REAL(SP), DIMENSION(size(a,1)) :: tempm REAL(SP), DIMENSION(size(a,2)) :: rv1,tempn m=size(a,1) n=assert_eq(size(a,2),size(v,1),size(v,2),size(w),’svdcmp_sp’) g=0.0 scale=0.0 do i=1,n Householder reduction to bidiagonal form. l=i+1 rv1(i)=scale*g g=0.0 scale=0.0 if (i 2, ’cyclic arg’) gamma=-b(1) Avoid subtraction error in forming bb(1). bb(1)=b(1)-gamma Set up the diagonal of the modified tridiagbb(n)=b(n)-alpha*beta/gamma onal system. bb(2:n-1)=b(2:n-1) call tridag(a(2:n),bb,c(1:n-1),r,x) Solve A · x = r. u(1)=gamma Set up the vector u. u(n)=alpha u(2:n-1)=0.0 call tridag(a(2:n),bb,c(1:n-1),u,z) Solve A · z = u. fact=(x(1)+beta*x(n)/gamma)/(1.0_sp+z(1)+beta*z(n)/gamma) Form v·x/(1+v·z). x=x-fact*z Now get the solution vector x. END SUBROUTINE cyclic
Chapter B2. Solution of Linear Algebraic Equations
1031
SUBROUTINE sprsin_dp(a,thresh,sa) USE nrtype; USE nrutil, ONLY : arth,assert_eq IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(IN) :: a REAL(DP), INTENT(IN) :: thresh TYPE(sprs2_dp), INTENT(OUT) :: sa INTEGER(I4B) :: n,len LOGICAL(LGT), DIMENSION(size(a,1),size(a,2)) :: mask n=assert_eq(size(a,1),size(a,2),’sprsin_dp’) mask=abs(a)>thresh len=count(mask) allocate(sa%val(len),sa%irow(len),sa%jcol(len)) sa%n=n sa%len=len sa%val=pack(a,mask) sa%irow=pack(spread(arth(1,1,n),2,n),mask) sa%jcol=pack(spread(arth(1,1,n),1,n),mask) END SUBROUTINE sprsin_dp
Note that the routines sprsin sp and sprsin dp — single and double precision versions of the same algorithm — are overloaded onto the name sprsin in module nr. We supply both forms because the routine linbcg, below, works in double precision.
f90
sa%irow=pack(spread(arth(1,1,n),2,n),mask) The trick here is to use the same mask, abs(a)>thresh, in three consecutive pack expressions, thus guaranteeing that the corresponding elements of the array argument get selected for packing. The first time, we get the desired matrix element values. The second time (above code fragment), we construct a matrix with each element having the value of its row number. The third time, we construct a matrix with each element having the value of its column number.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE sprsin_sp(a,thresh,sa) USE nrtype; USE nrutil, ONLY : arth,assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(IN) :: a REAL(SP), INTENT(IN) :: thresh TYPE(sprs2_sp), INTENT(OUT) :: sa Converts a square matrix a to sparse storage format as sa. Only elements of a with magnitude ≥ thresh are retained. INTEGER(I4B) :: n,len LOGICAL(LGT), DIMENSION(size(a,1),size(a,2)) :: mask n=assert_eq(size(a,1),size(a,2),’sprsin_sp’) mask=abs(a)>thresh len=count(mask) How many elements to store? allocate(sa%val(len),sa%irow(len),sa%jcol(len)) sa%n=n sa%len=len sa%val=pack(a,mask) Grab the values, row, and column numbers. sa%irow=pack(spread(arth(1,1,n),2,n),mask) sa%jcol=pack(spread(arth(1,1,n),1,n),mask) END SUBROUTINE sprsin_sp
1032
Chapter B2.
Solution of Linear Algebraic Equations
SUBROUTINE sprsax_dp(sa,x,b) USE nrtype; USE nrutil, ONLY : assert_eq,scatter_add IMPLICIT NONE TYPE(sprs2_dp), INTENT(IN) :: sa REAL(DP), DIMENSION (:), INTENT(IN) :: x REAL(DP), DIMENSION (:), INTENT(OUT) :: b INTEGER(I4B) :: ndum ndum=assert_eq(sa%n,size(x),size(b),’sprsax_dp’) b=0.0_dp call scatter_add(b,sa%val*x(sa%jcol),sa%irow) END SUBROUTINE sprsax_dp
Since more than one component of the middle vector argument will, in general, need to be added into the same component of b, we must resort to a call to the nrutil routine scatter add to achieve parallelism. However, this parallelism is achieved only if a parallel version of scatter add is available! As we have discussed previously (p. 984), Fortran 90 does not provide any scatter-with-combine (here, scatter-with-add) facility, insisting instead that indexed operations yield noncolliding addresses. Luckily, almost all parallel machines do provide such a facility as a library program. In HPF, for example, the equivalent of scatter add is SUM SCATTER. The call to scatter add above is equivalent to the do-loop call scatter_add(b,sa%val*x(sa%jcol),sa%irow)
b=0.0 do k=1,sa%len b(sa%irow(k))=b(sa%irow(k))+sa%val(k)*x(sa%jcol(k)) end do SUBROUTINE sprstx_sp(sa,x,b) USE nrtype; USE nrutil, ONLY : assert_eq,scatter_add IMPLICIT NONE TYPE(sprs2_sp), INTENT(IN) :: sa REAL(SP), DIMENSION (:), INTENT(IN) :: x REAL(SP), DIMENSION (:), INTENT(OUT) :: b Multiply the transpose of a matrix sa in sparse matrix format by a vector x, giving a vector b. INTEGER(I4B) :: ndum ndum=assert_eq(sa%n,size(x),size(b),’sprstx_sp’) b=0.0_sp call scatter_add(b,sa%val*x(sa%irow),sa%jcol) Each sparse matrix entry adds a term to some component of b. END SUBROUTINE sprstx_sp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE sprsax_sp(sa,x,b) USE nrtype; USE nrutil, ONLY : assert_eq,scatter_add IMPLICIT NONE TYPE(sprs2_sp), INTENT(IN) :: sa REAL(SP), DIMENSION (:), INTENT(IN) :: x REAL(SP), DIMENSION (:), INTENT(OUT) :: b Multiply a matrix sa in sparse matrix format by a vector x, giving a vector b. INTEGER(I4B) :: ndum ndum=assert_eq(sa%n,size(x),size(b),’sprsax_sp’) b=0.0_sp call scatter_add(b,sa%val*x(sa%jcol),sa%irow) Each sparse matrix entry adds a term to some component of b. END SUBROUTINE sprsax_sp
Chapter B2. Solution of Linear Algebraic Equations
1033
Precisely the same comments as for sprsax apply to sprstx. The call to scatter add is here equivalent to b=0.0 do k=1,sa%len b(sa%jcol(k))=b(sa%jcol(k))+sa%val(k)*x(sa%irow(k)) end do
SUBROUTINE sprstp(sa) USE nrtype IMPLICIT NONE TYPE(sprs2_sp), INTENT(INOUT) :: sa Replaces sa, in sparse matrix format, by its transpose. INTEGER(I4B), DIMENSION(:), POINTER :: temp temp=>sa%irow We need only swap the row and column pointers. sa%irow=>sa%jcol sa%jcol=>temp END SUBROUTINE sprstp
SUBROUTINE sprsdiag_sp(sa,b) USE nrtype; USE nrutil, ONLY : array_copy,assert_eq IMPLICIT NONE TYPE(sprs2_sp), INTENT(IN) :: sa REAL(SP), DIMENSION(:), INTENT(OUT) :: b Extracts the diagonal of a matrix sa in sparse matrix format into a vector b. REAL(SP), DIMENSION(size(b)) :: val INTEGER(I4B) :: k,l,ndum,nerr INTEGER(I4B), DIMENSION(size(b)) :: i LOGICAL(LGT), DIMENSION(:), ALLOCATABLE :: mask ndum=assert_eq(sa%n,size(b),’sprsdiag_sp’) l=sa%len allocate(mask(l)) mask = (sa%irow(1:l) == sa%jcol(1:l)) Find diagonal elements. call array_copy(pack(sa%val(1:l),mask),val,k,nerr) Grab the values... i(1:k)=pack(sa%irow(1:l),mask) ...and their locations. deallocate(mask) b=0.0 Zero b because zero values not stored in sa. b(i(1:k))=val(1:k) Scatter values into correct slots. END SUBROUTINE sprsdiag_sp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE sprstx_dp(sa,x,b) USE nrtype; USE nrutil, ONLY : assert_eq,scatter_add IMPLICIT NONE TYPE(sprs2_dp), INTENT(IN) :: sa REAL(DP), DIMENSION (:), INTENT(IN) :: x REAL(DP), DIMENSION (:), INTENT(OUT) :: b INTEGER(I4B) :: ndum ndum=assert_eq(sa%n,size(x),size(b),’sprstx_dp’) b=0.0_dp call scatter_add(b,sa%val*x(sa%irow),sa%jcol) END SUBROUTINE sprstx_dp
1034
Chapter B2.
Solution of Linear Algebraic Equations
We use the nrutil routine array copy because we don’t know in advance how many nonzero diagonal elements will be selected by mask. Of course we could count them with a count(mask), but this is an extra step, and inefficient on scalar machines.
f90
call array_copy(pack(sa%val(1:l),mask),val,k,nerr)
i(1:k)=pack(sa%irow(1:l),mask) Using the same mask, we pick out the corresponding locations of the diagonal elements. No need to use array copy now, since we know the value of k. b(i(1:k))=val(1:k) Finally, we can put each element in the right place. Notice that if the sparse matrix is ill-formed, with more than one value stored for the same diagonal element (which should not happen!) then the vector subscript i(1:k) is a “many-one section” and its use on the left-hand side is illegal.
SUBROUTINE linbcg(b,x,itol,tol,itmax,iter,err) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : atimes,asolve,snrm IMPLICIT NONE REAL(DP), DIMENSION(:), INTENT(IN) :: b Double precision is a good idea in this REAL(DP), DIMENSION(:), INTENT(INOUT) :: x routine. INTEGER(I4B), INTENT(IN) :: itol,itmax REAL(DP), INTENT(IN) :: tol INTEGER(I4B), INTENT(OUT) :: iter REAL(DP), INTENT(OUT) :: err REAL(DP), PARAMETER :: EPS=1.0e-14_dp Solves A · x = b for x, given b of the same length, by the iterative biconjugate gradient method. On input x should be set to an initial guess of the solution (or all zeros); itol is 1,2,3, or 4, specifying which convergence test is applied (see text); itmax is the maximum number of allowed iterations; and tol is the desired convergence tolerance. On output, x is reset to the improved solution, iter is the number of iterations actually taken, and err is the estimated error. The matrix A is referenced only through the user-supplied routines atimes, which computes the product of either A or its transpose on a vector; and asolve,
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE sprsdiag_dp(sa,b) USE nrtype; USE nrutil, ONLY : array_copy,assert_eq IMPLICIT NONE TYPE(sprs2_dp), INTENT(IN) :: sa REAL(DP), DIMENSION(:), INTENT(OUT) :: b REAL(DP), DIMENSION(size(b)) :: val INTEGER(I4B) :: k,l,ndum,nerr INTEGER(I4B), DIMENSION(size(b)) :: i LOGICAL(LGT), DIMENSION(:), ALLOCATABLE :: mask ndum=assert_eq(sa%n,size(b),’sprsdiag_dp’) l=sa%len allocate(mask(l)) mask = (sa%irow(1:l) == sa%jcol(1:l)) call array_copy(pack(sa%val(1:l),mask),val,k,nerr) i(1:k)=pack(sa%irow(1:l),mask) deallocate(mask) b=0.0 b(i(1:k))=val(1:k) END SUBROUTINE sprsdiag_dp
Chapter B2. Solution of Linear Algebraic Equations
· x = b or A T · x = b for some preconditioner matrix A (possibly the trivial which solves A diagonal part of A). INTEGER(I4B) :: n REAL(DP) :: ak,akden,bk,bkden,bknum,bnrm,dxnrm,xnrm,zm1nrm,znrm REAL(DP), DIMENSION(size(b)) :: p,pp,r,rr,z,zz n=assert_eq(size(b),size(x),’linbcg’) iter=0 call atimes(x,r,0) Calculate initial residual. Input to atimes is x(1:n), output is r(1:n); the final 0 r=b-r indicates that the matrix (not its transrr=r pose) is to be used. call atimes(r,rr,0) Uncomment this line to get the “minimum residual” variant of the algorithm. select case(itol) Calculate norms for use in stopping criterion, case(1) and initialize z. bnrm=snrm(b,itol) call asolve(r,z,0) Input to asolve is r(1:n), output is z(1:n); case(2) the final 0 indicates that the matrix A call asolve(b,z,0) (not its transpose) is to be used. bnrm=snrm(z,itol) call asolve(r,z,0) case(3:4) call asolve(b,z,0) bnrm=snrm(z,itol) call asolve(r,z,0) znrm=snrm(z,itol) case default call nrerror(’illegal itol in linbcg’) end select do Main loop. if (iter > itmax) exit iter=iter+1 T . call asolve(rr,zz,1) Final 1 indicates use of transpose matrix A bknum=dot_product(z,rr) Calculate coefficient bk and direction vectors if (iter == 1) then p and pp. p=z pp=zz else bk=bknum/bkden p=bk*p+z pp=bk*pp+zz end if bkden=bknum Calculate coefficient ak, new iterate x, and call atimes(p,z,0) new residuals r and rr. akden=dot_product(z,pp) ak=bknum/akden call atimes(pp,zz,1) x=x+ak*p r=r-ak*z rr=rr-ak*zz ·z = r and check stopping criterion. call asolve(r,z,0) Solve A select case(itol) case(1) err=snrm(r,itol)/bnrm case(2) err=snrm(z,itol)/bnrm case(3:4) zm1nrm=znrm znrm=snrm(z,itol) if (abs(zm1nrm-znrm) > EPS*znrm) then dxnrm=abs(ak)*snrm(p,itol) err=znrm/abs(zm1nrm-znrm)*dxnrm else err=znrm/bnrm Error may not be accurate, so loop again. cycle
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
!
1035
1036
Chapter B2.
Solution of Linear Algebraic Equations
f90
It’s always a good idea to trap errors when the value of a case construction is supplied externally to the routine, as here.
case default...call nrerror(’illegal itol in linbcg’)
FUNCTION snrm(sx,itol) USE nrtype IMPLICIT NONE REAL(DP), DIMENSION(:), INTENT(IN) :: sx INTEGER(I4B), INTENT(IN) :: itol REAL(DP) :: snrm Compute one of two norms for a vector sx, as signaled by itol. Used by linbcg. if (itol 0.99e30_sp) then “natural” r(n)=0.0 a(n)=0.0 else or else to have a specified first derivative. r(n)=(-3.0_sp/(x(n)-x(n-1)))*((y(n)-y(n-1))/(x(n)-x(n-1))-ypn) a(n)=0.5 end if call tridag(a(2:n),b(1:n),c(1:n-1),r(1:n),y2(1:n)) END SUBROUTINE spline
1046
Chapter B3.
Interpolation and Extrapolation
The use of bisection is perhaps a sin on a genuinely parallel machine, but (since the process takes only logarithmically many sequential steps) it is at most a small sin. One can imagine a “fully parallel” implementation like, k=iminloc(abs(x-xx)) if ((x < xx(k)) .eqv. (xx(1) < xx(n))) then locate=k-1 else locate=k end if
Problem is, unless the number of physical (not logical) processors participating in the iminloc is larger than N , the length of the array, this “parallel” code turns a log N algorithm into one scaling as N , quite an unacceptable inefficiency. So we prefer to be small sinners and bisect. SUBROUTINE hunt(xx,x,jlo) USE nrtype IMPLICIT NONE INTEGER(I4B), INTENT(INOUT) :: jlo REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: xx Given an array xx(1:N ), and given a value x, returns a value jlo such that x is between xx(jlo) and xx(jlo+1). xx must be monotonic, either increasing or decreasing. jlo = 0 or jlo = N is returned to indicate that x is out of range. jlo on input is taken as the initial guess for jlo on output. INTEGER(I4B) :: n,inc,jhi,jm LOGICAL :: ascnd n=size(xx) ascnd = (xx(n) >= xx(1)) True if ascending order of table, false otherwise. if (jlo n) then Input guess not useful. Go immediately to bisecjlo=0 tion. jhi=n+1 else inc=1 Set the hunting increment. if (x >= xx(jlo) .eqv. ascnd) then Hunt up: do jhi=jlo+inc if (jhi > n) then Done hunting, since off end of table.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
n=size(xx) ascnd = (xx(n) >= xx(1)) True if ascending order of table, false otherwise. jl=0 Initialize lower ju=n+1 and upper limits. do if (ju-jl = xx(jm))) then jl=jm and replace either the lower limit else ju=jm or the upper limit, as appropriate. end if end do if (x == xx(1)) then Then set the output, being careful with the endpoints. locate=1 else if (x == xx(n)) then locate=n-1 else locate=jl end if END FUNCTION locate
Chapter B3. Interpolation and Extrapolation
end END
jhi=n+1 exit else if (x < xx(jhi) .eqv. ascnd) exit jlo=jhi Not done hunting, inc=inc+inc so double the increment end if end do and try again. else Hunt down: jhi=jlo do jlo=jhi-inc if (jlo < 1) then Done hunting, since off end of table. jlo=0 exit else if (x >= xx(jlo) .eqv. ascnd) exit jhi=jlo Not done hunting, inc=inc+inc so double the increment end if end do and try again. end if if Done hunting, value bracketed. Hunt is done, so begin the final bisection phase: if (jhi-jlo = xx(jm) .eqv. ascnd) then jlo=jm else jhi=jm end if end if do SUBROUTINE hunt
FUNCTION polcoe(x,y) USE nrtype; USE nrutil, ONLY : assert_eq,outerdiff IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y REAL(SP), DIMENSION(size(x)) :: polcoe Given same-size arrays x and y containing a tabulated function yi = f (xi ), this routine returns a same-size array of coefficients cj , such that yi = j cj xij−1 . INTEGER(I4B) :: i,k,n REAL(SP), DIMENSION(size(x)) :: s REAL(SP), DIMENSION(size(x),size(x)) :: a n=assert_eq(size(x),size(y),’polcoe’) s=0.0 Coefficients si of the master polynomial P (x) are found by s(n)=-x(1) recurrence. do i=2,n s(n+1-i:n-1)=s(n+1-i:n-1)-x(i)*s(n+2-i:n) s(n)=s(n)-x(i) end do a=outerdiff(x,x) Make vector wj = j=n (xj − xn ), using polcoe for tempopolcoe=product(a,dim=2,mask=a /= 0.0) rary storage.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
end do
1047
1048
Chapter B3.
Interpolation and Extrapolation
For a description of the coding here, see §22.3, especially equation (22.3.9). You might also want to compare the coding here with the Fortran 77 version, and also look at the description of the method on p. 84 in Volume 1. The Fortran 90 implementation here is in fact much closer to that description than is the Fortran 77 method, which goes through some acrobatics to roll the synthetic division and matrix multiplication into a single set of two nested loops. The price we pay, here, is storage for the matrix a. Since the degree of any useful polynomial is not a very large number, this is essentially no penalty. Also worth noting is the way that parallelism is brought to the required synthetic division. For a single such synthetic division (e.g., as accomplished by the nrutil routine poly term), parallelism can be obtained only by recursion. Here things are much simpler, because we need a whole bunch of simultaneous and independent synthetic divisions; so we can just do them in the obvious, data-parallel, way. FUNCTION polcof(xa,ya) USE nrtype; USE nrutil, ONLY : assert_eq,iminloc USE nr, ONLY : polint IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: xa,ya REAL(SP), DIMENSION(size(xa)) :: polcof Given same-size arrays xa and ya containing a tabulated function yai = f (xai ), this routine returns a same-size array of coefficients cj such that yai = j cj xaij−1 . INTEGER(I4B) :: j,k,m,n REAL(SP) :: dy REAL(SP), DIMENSION(size(xa)) :: x,y n=assert_eq(size(xa),size(ya),’polcof’) x=xa y=ya do j=1,n m=n+1-j call polint(x(1:m),y(1:m),0.0_sp,polcof(j),dy) Use the polynomial interpolation routine of §3.1 to extrapolate to x = 0. k=iminloc(abs(x(1:m))) Find the remaining xk of smallest absolute value, where (x(1:m) /= 0.0) y(1:m)=(y(1:m)-polcof(j))/x(1:m) reduce all the terms, y(k:m-1)=y(k+1:m) and eliminate xk . x(k:m-1)=x(k+1:m) end do END FUNCTION polcof
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Now do synthetic division by x − xj . The division for all xj can be done in parallel (on a parallel machine), since the : in the loop below is over j. a(:,1)=-s(1)/x(:) do k=2,n a(:,k)=-(s(k)-a(:,k-1))/x(:) end do s=y/polcoe polcoe=matmul(s,a) Solve linear system. END FUNCTION polcoe
Chapter B3.
Interpolation and Extrapolation
1049
SUBROUTINE bcucof(y,y1,y2,y12,d1,d2,c) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: d1,d2 REAL(SP), DIMENSION(4), INTENT(IN) :: y,y1,y2,y12 REAL(SP), DIMENSION(4,4), INTENT(OUT) :: c Given arrays y, y1, y2, and y12, each of length 4, containing the function, gradients, and cross derivative at the four grid points of a rectangular grid cell (numbered counterclockwise from the lower left), and given d1 and d2, the length of the grid cell in the 1- and 2directions, this routine returns the 4 × 4 table c that is used by routine bcuint for bicubic interpolation. REAL(SP), DIMENSION(16) :: x REAL(SP), DIMENSION(16,16) :: wt DATA wt /1,0,-3,2,4*0,-3,0,9,-6,2,0,-6,4,& 8*0,3,0,-9,6,-2,0,6,-4,10*0,9,-6,2*0,-6,4,2*0,3,-2,6*0,-9,6,& 2*0,6,-4,4*0,1,0,-3,2,-2,0,6,-4,1,0,-3,2,8*0,-1,0,3,-2,1,0,-3,& 2,10*0,-3,2,2*0,3,-2,6*0,3,-2,2*0,-6,4,2*0,3,-2,0,1,-2,1,5*0,& -3,6,-3,0,2,-4,2,9*0,3,-6,3,0,-2,4,-2,10*0,-3,3,2*0,2,-2,2*0,& -1,1,6*0,3,-3,2*0,-2,2,5*0,1,-2,1,0,-2,4,-2,0,1,-2,1,9*0,-1,2,& -1,0,1,-2,1,10*0,1,-1,2*0,-1,1,6*0,-1,1,2*0,2,-2,2*0,-1,1/ x(1:4)=y Pack a temporary vector x. x(5:8)=y1*d1 x(9:12)=y2*d2 x(13:16)=y12*d1*d2 x=matmul(wt,x) Matrix multiply by the stored table. c=reshape(x,(/4,4/),order=(/2,1/)) Unpack the result into the output table. END SUBROUTINE bcucof
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE polin2(x1a,x2a,ya,x1,x2,y,dy) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : polint IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x1a,x2a REAL(SP), DIMENSION(:,:), INTENT(IN) :: ya REAL(SP), INTENT(IN) :: x1,x2 REAL(SP), INTENT(OUT) :: y,dy Given arrays x1a of length M and x2a of length N of independent variables, and an M × N array of function values ya, tabulated at the grid points defined by x1a and x2a, and given values x1 and x2 of the independent variables, this routine returns an interpolated function value y, and an accuracy indication dy (based only on the interpolation in the x1 direction, however). INTEGER(I4B) :: j,m,ndum REAL(SP), DIMENSION(size(x1a)) :: ymtmp REAL(SP), DIMENSION(size(x2a)) :: yntmp m=assert_eq(size(x1a),size(ya,1),’polin2: m’) ndum=assert_eq(size(x2a),size(ya,2),’polin2: ndum’) do j=1,m Loop over rows. yntmp=ya(j,:) Copy row into temporary storage. call polint(x2a,yntmp,x2,ymtmp(j),dy) Interpolate answer into temporary storend do age. call polint(x1a,ymtmp,x1,y,dy) Do the final interpolation. END SUBROUTINE polin2
1050
Chapter B3.
Interpolation and Extrapolation
It is a powerful technique to combine the matmul intrinsic with reshape’s of the input or output. The idea is to use matmul whenever the calculation can be cast into the form of a linear mapping between input and output objects. Here the order=(/2,1/) parameter specifies that we want the packing to be by rows, not by Fortran’s default of columns. (In this two-dimensional case, it’s the equivalent of applying transpose.)
f90
x=matmul(wt,x) ... c=reshape(x,(/4,4/),order=(/2,1/))
SUBROUTINE splie2(x1a,x2a,ya,y2a) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : spline IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x1a,x2a REAL(SP), DIMENSION(:,:), INTENT(IN) :: ya REAL(SP), DIMENSION(:,:), INTENT(OUT) :: y2a Given an M × N tabulated function ya, and N tabulated independent variables x2a, this routine constructs one-dimensional natural cubic splines of the rows of ya and returns the second derivatives in the M × N array y2a. (The array x1a is included in the argument list merely for consistency with routine splin2.) INTEGER(I4B) :: j,m,ndum m=assert_eq(size(x1a),size(ya,1),size(y2a,1),’splie2: m’) ndum=assert_eq(size(x2a),size(ya,2),size(y2a,2),’splie2: ndum’) do j=1,m call spline(x2a,ya(j,:),1.0e30_sp,1.0e30_sp,y2a(j,:))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE bcuint(y,y1,y2,y12,x1l,x1u,x2l,x2u,x1,x2,ansy,ansy1,ansy2) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : bcucof IMPLICIT NONE REAL(SP), DIMENSION(4), INTENT(IN) :: y,y1,y2,y12 REAL(SP), INTENT(IN) :: x1l,x1u,x2l,x2u,x1,x2 REAL(SP), INTENT(OUT) :: ansy,ansy1,ansy2 Bicubic interpolation within a grid square. Input quantities are y,y1,y2,y12 (as described in bcucof); x1l and x1u, the lower and upper coordinates of the grid square in the 1direction; x2l and x2u likewise for the 2-direction; and x1,x2, the coordinates of the desired point for the interpolation. The interpolated function value is returned as ansy, and the interpolated gradient values as ansy1 and ansy2. This routine calls bcucof. INTEGER(I4B) :: i REAL(SP) :: t,u REAL(SP), DIMENSION(4,4) :: c call bcucof(y,y1,y2,y12,x1u-x1l,x2u-x2l,c) Get the c’s. if (x1u == x1l .or. x2u == x2l) call & nrerror(’bcuint: problem with input values - boundary pair equal?’) t=(x1-x1l)/(x1u-x1l) Equation (3.6.4). u=(x2-x2l)/(x2u-x2l) ansy=0.0 ansy2=0.0 ansy1=0.0 do i=4,1,-1 Equation (3.6.6). ansy=t*ansy+((c(i,4)*u+c(i,3))*u+c(i,2))*u+c(i,1) ansy2=t*ansy2+(3.0_sp*c(i,4)*u+2.0_sp*c(i,3))*u+c(i,2) ansy1=u*ansy1+(3.0_sp*c(4,i)*t+2.0_sp*c(3,i))*t+c(2,i) end do ansy1=ansy1/(x1u-x1l) ansy2=ansy2/(x2u-x2l) END SUBROUTINE bcuint
Chapter B3.
Interpolation and Extrapolation
1051
Values 1 × 1030 signal a natural spline. end do END SUBROUTINE splie2
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION splin2(x1a,x2a,ya,y2a,x1,x2) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : spline,splint IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x1a,x2a REAL(SP), DIMENSION(:,:), INTENT(IN) :: ya,y2a REAL(SP), INTENT(IN) :: x1,x2 REAL(SP) :: splin2 Given x1a, x2a, ya as described in splie2 and y2a as produced by that routine; and given a desired interpolating point x1,x2; this routine returns an interpolated function value by bicubic spline interpolation. INTEGER(I4B) :: j,m,ndum REAL(SP), DIMENSION(size(x1a)) :: yytmp,y2tmp2 m=assert_eq(size(x1a),size(ya,1),size(y2a,1),’splin2: m’) ndum=assert_eq(size(x2a),size(ya,2),size(y2a,2),’splin2: ndum’) do j=1,m yytmp(j)=splint(x2a,ya(j,:),y2a(j,:),x2) Perform m evaluations of the row splines constructed by splie2, using the one-dimensional spline evaluator splint. end do call spline(x1a,yytmp,1.0e30_sp,1.0e30_sp,y2tmp2) Construct the one-dimensional column spline and evaluate it. splin2=splint(x1a,yytmp,y2tmp2,x1) END FUNCTION splin2
Integration of Functions
SUBROUTINE trapzd(func,a,b,s,n) USE nrtype; USE nrutil, ONLY : arth IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), INTENT(INOUT) :: s INTEGER(I4B), INTENT(IN) :: n INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE This routine computes the nth stage of refinement of an extended trapezoidal rule. func is input as the name of the function to be integrated between limits a and b, also input. When b called with n=1, the routine returns as s the crudest estimate of a f (x)dx. Subsequent
calls with n=2,3,... (in that sequential order) will improve the accuracy of s by adding 2n-2 additional interior points. s should not be modified between sequential calls. REAL(SP) :: del,fsum INTEGER(I4B) :: it if (n == 1) then s=0.5_sp*(b-a)*sum(func( (/ a,b /) )) else it=2**(n-2) del=(b-a)/it This is the spacing of the points to be added. fsum=sum(func(arth(a+0.5_sp*del,del,it))) s=0.5_sp*(s+del*fsum) This replaces s by its refined value. end if END SUBROUTINE trapzd
While most of the quadrature routines in this chapter are coded as functions, trapzd is a subroutine because the argument s that returns the function value must also be supplied as an input parameter. We could change the subroutine into a function by declaring s to be a local variable with the SAVE attribute. However, this would prevent us from being able to use the routine recursively to do multidimensional quadrature (see quad3d on p. 1065). When s is left as an argument, a fresh copy is created on each recursive call. As a SAVE’d variable, by contrast, its value would get overwritten on each call, and the code would not be properly “re-entrant.”
f90
s=0.5_sp*(b-a)*sum(func( (/ a,b /) )) Note how we use the (/.../) construct to supply a set of scalar arguments to a vector function.
1052
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B4.
Chapter B4.
Integration of Functions
1053
FUNCTION qsimp(func,a,b) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : trapzd IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP) :: qsimp INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: JMAX=20 REAL(SP), PARAMETER :: EPS=1.0e-6_sp, UNLIKELY=-1.0e30_sp Returns the integral of the function func from a to b. The parameter EPS should be set to the desired fractional accuracy and JMAX so that 2 to the power JMAX-1 is the maximum allowed number of steps. Integration is performed by Simpson’s rule. INTEGER(I4B) :: j REAL(SP) :: os,ost,st ost=UNLIKELY os= UNLIKELY do j=1,JMAX call trapzd(func,a,b,st,j) qsimp=(4.0_sp*st-ost)/3.0_sp Compare equation (4.2.4). if (j > 5) then Avoid spurious early convergence. if (abs(qsimp-os) < EPS*abs(os) .or. & (qsimp == 0.0 .and. os == 0.0)) RETURN end if os=qsimp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION qtrap(func,a,b) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : trapzd IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP) :: qtrap INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: JMAX=20 REAL(SP), PARAMETER :: EPS=1.0e-6_sp, UNLIKELY=-1.0e30_sp Returns the integral of the function func from a to b. The parameter EPS should be set to the desired fractional accuracy and JMAX so that 2 to the power JMAX-1 is the maximum allowed number of steps. Integration is performed by the trapezoidal rule. REAL(SP) :: olds INTEGER(I4B) :: j olds=UNLIKELY Any number that is unlikely to be the average of the do j=1,JMAX function at its endpoints will do here. call trapzd(func,a,b,qtrap,j) if (j > 5) then Avoid spurious early convergence. if (abs(qtrap-olds) < EPS*abs(olds) .or. & (qtrap == 0.0 .and. olds == 0.0)) RETURN end if olds=qtrap end do call nrerror(’qtrap: too many steps’) END FUNCTION qtrap
1054
Chapter B4.
Integration of Functions
ost=st end do call nrerror(’qsimp: too many steps’) END FUNCTION qsimp
SUBROUTINE midpnt(func,a,b,s,n) USE nrtype; USE nrutil, ONLY : arth IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), INTENT(INOUT) :: s INTEGER(I4B), INTENT(IN) :: n INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE This routine computes the nth stage of refinement of an extended midpoint rule. func is input as the name of the function to be integrated between limits a and b, also input. When
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION qromb(func,a,b) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : polint,trapzd IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP) :: qromb INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: JMAX=20,JMAXP=JMAX+1,K=5,KM=K-1 REAL(SP), PARAMETER :: EPS=1.0e-6_sp Returns the integral of the function func from a to b. Integration is performed by Romberg’s method of order 2K, where, e.g., K=2 is Simpson’s rule. Parameters: EPS is the fractional accuracy desired, as determined by the extrapolation error estimate; JMAX limits the total number of steps; K is the number of points used in the extrapolation. REAL(SP), DIMENSION(JMAXP) :: h,s These store the successive trapezoidal apREAL(SP) :: dqromb proximations and their relative stepsizes. INTEGER(I4B) :: j h(1)=1.0 do j=1,JMAX call trapzd(func,a,b,s(j),j) if (j >= K) then call polint(h(j-KM:j),s(j-KM:j),0.0_sp,qromb,dqromb) if (abs(dqromb) 0.0, ’midinf args’) b=1.0_sp/aa These two statements change the limits of integration aca=1.0_sp/bb cordingly. if (n == 1) then From this point on, the routine is exactly identical to midpnt. s=(b-a)*sum(func( (/0.5_sp*(a+b)/) )) else it=3**(n-2) del=(b-a)/(3.0_sp*it) x(1:2*it-1:2)=arth(a+0.5_sp*del,3.0_sp*del,it) x(2:2*it:2)=x(1:2*it-1:2)+2.0_sp*del s=s/3.0_sp+del*sum(func(x)) end if CONTAINS FUNCTION func(x) This internal function effects the change of variable. REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func func=funk(1.0_sp/x)/x**2 END FUNCTION func END SUBROUTINE midinf
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
even powers of the number of steps. The routines midpnt, midinf, midsql, midsqu, and midexp are possible choices for choose. The parameters have the same meaning as in qromb. REAL(SP), DIMENSION(JMAXP) :: h,s REAL(SP) :: dqromo INTEGER(I4B) :: j h(1)=1.0 do j=1,JMAX call choose(func,a,b,s(j),j) if (j >= K) then call polint(h(j-KM:j),s(j-KM:j),0.0_sp,qromo,dqromo) if (abs(dqromo) EPS) end where if (.not. any(unfinished)) exit end do if (its == MAXIT+1) call nrerror(’too many iterations in gauleg’) x(1:m)=xm-xl*z Scale the root to the desired interval, x(n:n-m+1:-1)=xm+xl*z and put in its symmetric counterpart. w(1:m)=2.0_dp*xl/((1.0_dp-z**2)*pp**2) Compute the weight w(n:n-m+1:-1)=w(1:m) and its symmetric counterpart. END SUBROUTINE gauleg
1060
Chapter B4.
Integration of Functions
where (unfinished) ... unfinished=(abs(z-z1) > EPS) end where if (.not. any(unfinished)) exit end do if (its == MAXIT+1) call nrerror(’too many iterations in gauleg’)
SUBROUTINE gaulag(x,w,alf) USE nrtype; USE nrutil, ONLY : arth,assert_eq,nrerror USE nr, ONLY : gammln IMPLICIT NONE REAL(SP), INTENT(IN) :: alf REAL(SP), DIMENSION(:), INTENT(OUT) :: x,w REAL(DP), PARAMETER :: EPS=3.0e-13_dp Given alf, the parameter α of the Laguerre polynomials, this routine returns arrays x and w of length N containing the abscissas and weights of the N -point Gauss-Laguerre quadrature formula. The abscissas are returned in ascending order. The parameter EPS is the relative precision. Note that internal computations are done in double precision. INTEGER(I4B) :: its,j,n INTEGER(I4B), PARAMETER :: MAXIT=10 REAL(SP) :: anu REAL(SP), PARAMETER :: C1=9.084064e-01_sp,C2=5.214976e-02_sp,& C3=2.579930e-03_sp,C4=3.986126e-03_sp REAL(SP), DIMENSION(size(x)) :: rhs,r2,r3,theta REAL(DP), DIMENSION(size(x)) :: p1,p2,p3,pp,z,z1 LOGICAL(LGT), DIMENSION(size(x)) :: unfinished n=assert_eq(size(x),size(w),’gaulag’) anu=4.0_sp*n+2.0_sp*alf+2.0_sp Initial approximations to the roots go into z. rhs=arth(4*n-1,-4,n)*PI/anu r3=rhs**(1.0_sp/3.0_sp) r2=r3**2 theta=r3*(C1+r2*(C2+r2*(C3+r2*C4))) z=anu*cos(theta)**2 unfinished=.true. do its=1,MAXIT Newton’s method carried out simultaneously on where (unfinished) the roots.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
We use the logical mask unfinished to control which vector components are processed inside the where. The mask gets updated on each iteration by testing whether any further vector components have converged. When all have converged, we exit the iteration loop. Finally, we check the value of its to see whether the maximum allowed number of iterations was exceeded before all components converged. The logical expression controlling the where block (in this case unfinished) gets evaluated completely on entry into the where, and it is then perfectly fine to modify it inside the block. The modification affects only the next execution of the where. On a strictly serial machine, there is of course some penalty associated with the above scheme: after a vector component converges, its corresponding component in unfinished is redundantly tested on each further iteration, until the slowestconverging component is done. If the number of iterations required does not vary too greatly from component to component, this is a minor, often negligible, penalty. However, one should be on the alert against algorithms whose worst-case convergence could differ from typical convergence by orders of magnitude. For these, one would need to implement a more complicated packing-unpacking scheme. (See discussion in Chapter B6, especially introduction, p. 1083, and notes for factrl, p. 1087.)
Chapter B4.
Integration of Functions
1061
The key difficulty in parallelizing this routine starting from the Fortran 77 version is that the initial guesses for the roots of the Laguerre polynomials were given in terms of previously determined roots. This prevents one from finding all the roots simultaneously. The solution is to come up with a new approximation to the roots that is a simple explicit formula, like the formula we used for the Legendre roots in gauleg. We start with the approximation to L α n (x) given in equation (10.15.8) of [1]. We keep only the first term and ask when it is zero. This gives the following prescription for the kth root x k of Lα n (x): Solve for θ the equation 2θ − sin 2θ =
4n − 4k + 3 π 4n + 2α + 2
(B4.1)
Since 1 ≤ k ≤ n and α > −1, we can always find a value such that 0 < θ < π/2. Then the approximation to the root is xk = (4n + 2α + 2) cos2 θ
(B4.2)
This typically gives 3-digit accuracy, more than enough for the Newton iteration to be able to refine the root. Unfortunately equation (B4.1) is not an explicit formula for θ. (You may recognize it as being of the same form as Kepler’s equation in mechanics.) If we call the right-hand side of (B4.1) y, then we can get an explicit formula by working out the power series for y 1/3 near θ = 0 (using a computer algebra program). Next invert the series to give θ as a function of y 1/3 . Finally, economize the series (see §5.11). The result is the concise approximation θ = 0.9084064y1/3 + 5.214976 × 10−2 y + 2.579930 × 10−3 y5/3 + 3.986126 × 10−3 y7/3 (B4.3)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
p1=1.0 p2=0.0 end where do j=1,n Loop up the recurrence relation to get the Lawhere (unfinished) guerre polynomials evaluated at z. p3=p2 p2=p1 p1=((2.0_dp*j-1.0_dp+alf-z)*p2-(j-1.0_dp+alf)*p3)/j end where end do p1 now contains the desired Laguerre polynomials. We next compute pp, the derivatives, by a standard relation involving also p2, the polynomials of one lower order. where (unfinished) pp=(n*p1-(n+alf)*p2)/z z1=z z=z1-p1/pp Newton’s formula. unfinished=(abs(z-z1) > EPS*z) end where if (.not. any(unfinished)) exit end do if (its == MAXIT+1) call nrerror(’too many iterations in gaulag’) x=z Store the root and the weight. w=-exp(gammln(alf+n)-gammln(real(n,sp)))/(pp*n*p2) END SUBROUTINE gaulag
1062
Chapter B4.
Integration of Functions
Once again we need an explicit approximation for the polynomial roots, this time for H n (x). We can use the same approximation scheme as for Lα n (x), since −1/2 2 (x ), H2m(x) ∝ Lm
1/2 2 H2m+1 (x) ∝ xLm (x )
(B4.4)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE gauher(x,w) USE nrtype; USE nrutil, ONLY : arth,assert_eq,nrerror IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(OUT) :: x,w REAL(DP), PARAMETER :: EPS=3.0e-13_dp,PIM4=0.7511255444649425_dp This routine returns arrays x and w of length N containing the abscissas and weights of the N -point Gauss-Hermite quadrature formula. The abscissas are returned in descending order. Note that internal computations are done in double precision. Parameters: EPS is the relative precision, PIM4 = 1/π1/4 . INTEGER(I4B) :: its,j,m,n INTEGER(I4B), PARAMETER :: MAXIT=10 REAL(SP) :: anu REAL(SP), PARAMETER :: C1=9.084064e-01_sp,C2=5.214976e-02_sp,& C3=2.579930e-03_sp,C4=3.986126e-03_sp REAL(SP), DIMENSION((size(x)+1)/2) :: rhs,r2,r3,theta REAL(DP), DIMENSION((size(x)+1)/2) :: p1,p2,p3,pp,z,z1 LOGICAL(LGT), DIMENSION((size(x)+1)/2) :: unfinished n=assert_eq(size(x),size(w),’gauher’) m=(n+1)/2 The roots are symmetric about the origin, so we have to anu=2.0_sp*n+1.0_sp find only half of them. rhs=arth(3,4,m)*PI/anu r3=rhs**(1.0_sp/3.0_sp) r2=r3**2 theta=r3*(C1+r2*(C2+r2*(C3+r2*C4))) z=sqrt(anu)*cos(theta) Initial approximations to the roots. unfinished=.true. do its=1,MAXIT Newton’s method carried out simultaneously on the roots. where (unfinished) p1=PIM4 p2=0.0 end where do j=1,n Loop up the recurrence relation to get the Hermite polywhere (unfinished) nomials evaluated at z. p3=p2 p2=p1 p1=z*sqrt(2.0_dp/j)*p2-sqrt(real(j-1,dp)/real(j,dp))*p3 end where end do p1 now contains the desired Hermite polynomials. We next compute pp, the derivatives, by the relation (4.5.21) using p2, the polynomials of one lower order. where (unfinished) pp=sqrt(2.0_dp*n)*p2 z1=z z=z1-p1/pp Newton’s formula. unfinished=(abs(z-z1) > EPS) end where if (.not. any(unfinished)) exit end do if (its == MAXIT+1) call nrerror(’too many iterations in gauher’) x(1:m)=z Store the root x(n:n-m+1:-1)=-z and its symmetric counterpart. w(1:m)=2.0_dp/pp**2 Compute the weight w(n:n-m+1:-1)=w(1:m) and its symmetric counterpart. END SUBROUTINE gauher
Chapter B4.
Integration of Functions
1063
Equations (B4.1) and (B4.2) become 4k − 1 π 2n + 1 √ xk = 2n + 1 cos θ
2θ − sin 2θ =
(B4.5)
SUBROUTINE gaujac(x,w,alf,bet) USE nrtype; USE nrutil, ONLY : arth,assert_eq,nrerror USE nr, ONLY : gammln IMPLICIT NONE REAL(SP), INTENT(IN) :: alf,bet REAL(SP), DIMENSION(:), INTENT(OUT) :: x,w REAL(DP), PARAMETER :: EPS=3.0e-14_dp Given alf and bet, the parameters α and β of the Jacobi polynomials, this routine returns arrays x and w of length N containing the abscissas and weights of the N -point GaussJacobi quadrature formula. The abscissas are returned in descending order. The parameter EPS is the relative precision. Note that internal computations are done in double precision. INTEGER(I4B) :: its,j,n INTEGER(I4B), PARAMETER :: MAXIT=10 REAL(DP) :: alfbet,a,c,temp REAL(DP), DIMENSION(size(x)) :: b,p1,p2,p3,pp,z,z1 LOGICAL(LGT), DIMENSION(size(x)) :: unfinished n=assert_eq(size(x),size(w),’gaujac’) alfbet=alf+bet Initial approximations to the roots go into z. z=cos(PI*(arth(1,1,n)-0.25_dp+0.5_dp*alf)/(n+0.5_dp*(alfbet+1.0_dp))) unfinished=.true. do its=1,MAXIT Newton’s method carried out simultaneously on the roots. temp=2.0_dp+alfbet where (unfinished) Start the recurrence with P0 and P1 to avoid a division p1=(alf-bet+temp*z)/2.0_dp by zero when α + β = 0 or −1. p2=1.0 end where do j=2,n Loop up the recurrence relation to get the Jacobi polya=2*j*(j+alfbet)*temp nomials evaluated at z. temp=temp+2.0_dp c=2.0_dp*(j-1.0_dp+alf)*(j-1.0_dp+bet)*temp where (unfinished) p3=p2 p2=p1 b=(temp-1.0_dp)*(alf*alf-bet*bet+temp*& (temp-2.0_dp)*z) p1=(b*p2-c*p3)/a end where end do p1 now contains the desired Jacobi polynomials. We next compute pp, the derivatives, by a standard relation involving also p2, the polynomials of one lower order. where (unfinished) pp=(n*(alf-bet-temp*z)*p1+2.0_dp*(n+alf)*& (n+bet)*p2)/(temp*(1.0_dp-z*z)) z1=z z=z1-p1/pp Newton’s formula. unfinished=(abs(z-z1) > EPS) end where if (.not. any(unfinished)) exit end do if (its == MAXIT+1) call nrerror(’too many iterations in gaujac’) x=z Store the root and the weight.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Here k = 1, 2, . . . , m where m = [(n + 1)/2], and k = 1 is the largest root. The negative roots follow from symmetry. The root at x = 0 for odd n is included in this approximation.
1064
Chapter B4.
Integration of Functions
w=exp(gammln(alf+n)+gammln(bet+n)-gammln(n+1.0_sp)-& gammln(n+alf+bet+1.0_sp))*temp*2.0_sp**alfbet/(pp*p2) END SUBROUTINE gaujac
x = cos
k − 1/4 + α/2 π n + (α + β + 1)/2
(B4.6)
This is better than the formula (22.16.1) in [2], especially at small and moderate n.
SUBROUTINE gaucof(a,b,amu0,x,w) USE nrtype; USE nrutil, ONLY : assert_eq,unit_matrix USE nr, ONLY : eigsrt,tqli IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: a,b REAL(SP), INTENT(IN) :: amu0 REAL(SP), DIMENSION(:), INTENT(OUT) :: x,w Computes the abscissas and weights for a Gaussian quadrature formula from the Jacobi matrix. On input, a and b of length N are the coefficients of the recurrence relation for the set of monic orthogonal polynomials. The quantity µ0 ≡ ab W (x) dx is input as amu0. The abscissas are returned in descending order in array x of length N , with the corresponding weights in w, also of length N . The arrays a and b are modified. Execution can be speeded up by modifying tqli and eigsrt to compute only the first component of each eigenvector. REAL(SP), DIMENSION(size(a),size(a)) :: z INTEGER(I4B) :: n n=assert_eq(size(a),size(b),size(x),size(w),’gaucof’) b(2:n)=sqrt(b(2:n)) Set up superdiagonal of Jacobi matrix. call unit_matrix(z) Set up identity matrix for tqli to compute eigenvectors. call tqli(a,b,z) call eigsrt(a,z) Sort eigenvalues into descending order. x=a w=amu0*z(1,:)**2 Equation (4.5.12). END SUBROUTINE gaucof
SUBROUTINE orthog(anu,alpha,beta,a,b) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: anu,alpha,beta REAL(SP), DIMENSION(:), INTENT(OUT) :: a,b Computes the coefficients aj and bj , j = 0, . . . N −1, of the recurrence relation for monic orthogonal polynomials with weight function W (x) by Wheeler’s algorithm. On input, alpha and beta contain the 2N − 1 coefficients αj and βj , j = 0, . . . 2N − 2, of the recurrence
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Now we need an explicit approximation for the roots of the Jacobi poly(α,β) nomials Pn (x). We start with the asymptotic expansion (10.14.10) of [1]. Setting this to zero gives the formula
Chapter B4.
Integration of Functions
1065
As discussed in Volume 1, multidimensional quadrature can be performed by calling a one-dimensional quadrature routine along each dimension. If the same routine is used for all such calls, then the calls are recursive. The file quad3d.f90 contains two modules, quad3d qgaus mod and quad3d qromb mod. In the first, the basic one-dimensional quadrature routine is a 10-point Gaussian quadrature routine called qgaus and three-dimensional quadrature is performed by calling quad3d qgaus. In the second, the basic one-dimensional routine is qromb of §4.3 and the three-dimensional routine is quad3d qromb. The Gaussian quadrature is simpler but its accuracy is not controllable. The Romberg integration lets you specify an accuracy, but is apt to be very slow if you try for too much accuracy. The only difference between the stand-alone version of trapzd and the version included here is that we have to add the keyword RECURSIVE. The only changes from the stand-alone version of qromb are: We have to add RECURSIVE; we remove trapzd from the list of routines in USE nr; we increase EPS to 3 × 10 −6 . Even this value could be too ambitious for difficult functions. You may want to set JMAX to a smaller value than 20 to avoid burning up a lot of computer time. Some people advocate using a smaller EPS on the inner quadrature (over z in our routine) than on the outer quadratures (over x or y). That strategy would require separate copies of qromb.
f90
MODULE quad3d_qgaus_mod USE nrtype PRIVATE Hide all names from the outside, PUBLIC quad3d_qgaus except quad3d itself. REAL(SP) :: xsav,ysav INTERFACE User-supplied functions. FUNCTION func(x,y,z) The three-dimensional function to be integrated. USE nrtype REAL(SP), INTENT(IN) :: x,y REAL(SP), DIMENSION(:), INTENT(IN) :: z REAL(SP), DIMENSION(size(z)) :: func END FUNCTION func FUNCTION y1(x) USE nrtype
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
relation for the chosen basis of orthogonal polynomials. The 2N modified moments νj are input in anu for j = 0, . . . 2N − 1. The first N coefficients are returned in a and b. INTEGER(I4B) :: k,n,ndum REAL(SP), DIMENSION(2*size(a)+1,2*size(a)+1) :: sig n=assert_eq(size(a),size(b),’orthog: n’) ndum=assert_eq(2*n,size(alpha)+1,size(anu),size(beta)+1,’orthog: ndum’) sig(1,3:2*n)=0.0 Initialization, Equation (4.5.33). sig(2,2:2*n+1)=anu(1:2*n) a(1)=alpha(1)+anu(2)/anu(1) b(1)=0.0 do k=3,n+1 Equation (4.5.34). sig(k,k:2*n-k+3)=sig(k-1,k+1:2*n-k+4)+(alpha(k-1:2*n-k+2) & -a(k-2))*sig(k-1,k:2*n-k+3)-b(k-2)*sig(k-2,k:2*n-k+3) & +beta(k-1:2*n-k+2)*sig(k-1,k-1:2*n-k+2) a(k-1)=alpha(k-1)+sig(k,k+1)/sig(k,k)-sig(k-1,k)/sig(k-1,k-1) b(k-1)=sig(k,k)/sig(k-1,k-1) end do END SUBROUTINE orthog
1066
Chapter B4.
Integration of Functions
REAL(SP), INTENT(IN) :: x REAL(SP) :: y1 END FUNCTION y1
FUNCTION z1(x,y) USE nrtype REAL(SP), INTENT(IN) :: x,y REAL(SP) :: z1 END FUNCTION z1 FUNCTION z2(x,y) USE nrtype REAL(SP), INTENT(IN) :: x,y REAL(SP) :: z2 END FUNCTION z2 END INTERFACE The routine quad3d qgaus returns as ss the integral of a user-supplied function func over a three-dimensional region specified by the limits x1, x2, and by the user-supplied functions y1, y2, z1, and z2, as defined in (4.6.2). Integration is performed by calling qgaus recursively. CONTAINS FUNCTION h(x) This is H of eq. (4.6.5). REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: h INTEGER(I4B) :: i do i=1,size(x) xsav=x(i) h(i)=qgaus(g,y1(xsav),y2(xsav)) end do END FUNCTION h FUNCTION g(y) This is G of eq. (4.6.4). REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(size(y)) :: g INTEGER(I4B) :: j do j=1,size(y) ysav=y(j) g(j)=qgaus(f,z1(xsav,ysav),z2(xsav,ysav)) end do END FUNCTION g FUNCTION f(z) The integrand f (x, y, z) evaluated at fixed x and y. REAL(SP), DIMENSION(:), INTENT(IN) :: z REAL(SP), DIMENSION(size(z)) :: f f=func(xsav,ysav,z) END FUNCTION f RECURSIVE FUNCTION qgaus(func,a,b) REAL(SP), INTENT(IN) :: a,b REAL(SP) :: qgaus INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE REAL(SP) :: xm,xr REAL(SP), DIMENSION(5) :: dx, w = (/ 0.2955242247_sp,0.2692667193_sp,& 0.2190863625_sp,0.1494513491_sp,0.0666713443_sp /),& x = (/ 0.1488743389_sp,0.4333953941_sp,0.6794095682_sp,&
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION y2(x) USE nrtype REAL(SP), INTENT(IN) :: x REAL(SP) :: y2 END FUNCTION y2
Chapter B4.
Integration of Functions
1067
0.8650633666_sp,0.9739065285_sp /) xm=0.5_sp*(b+a) xr=0.5_sp*(b-a) dx(:)=xr*x(:) qgaus=xr*sum(w(:)*(func(xm+dx)+func(xm-dx))) END FUNCTION qgaus
By default, all module entities are accessible by a routine that uses the module (unless we restrict the USE statement with ONLY). In this module, the user needs access only to the routine quad3d qgaus; the variables xsav, ysav and the procedures f, g, h, and qgaus are purely internal. It is good programming practice to prevent duplicate name conflicts or data overwriting by limiting access to only the desired entities. Here the PRIVATE statement with no variable names resets the default from PUBLIC. Then we include in the PUBLIC statement only the function name we want to be accessible.
f90
PRIVATE...PUBLIC quad3d qgaus
REAL(SP) :: xsav,ysav In Fortran 90, we generally avoid declaring global variables in COMMON blocks. Instead, we give them complete specifications in a module. A deficiency of Fortran 90 is that it does not allow pointers to functions. So here we have to use the fixed-name function func for the function to be integrated over. If we could have a pointer to a function as a global variable, then we would just set the pointer to point to the user function (of any name) in the calling program. Similarly the functions y1, y2, z1, and z2 could also have any name. CONTAINS Here follow the internal subprograms f, g, h, qgaus, and quad3d qgaus. Note that such internal subprograms are all “visible” to each other, i.e., their interfaces are mutually explicit, and do not require INTERFACE statements. RECURSIVE SUBROUTINE qgaus(func,a,b,ss) The RECURSIVE keyword is required for the compiler to process correctly any procedure that is invoked again in its body before the return from the first call has been completed. While some compilers may let you get away without explicitly informing them that a routine is recursive, don’t count on it! MODULE quad3d_qromb_mod Alternative to quad3d qgaus mod that uses qromb to perform each one-dimensional integration. USE nrtype PRIVATE PUBLIC quad3d_qromb REAL(SP) :: xsav,ysav INTERFACE FUNCTION func(x,y,z) USE nrtype REAL(SP), INTENT(IN) :: x,y REAL(SP), DIMENSION(:), INTENT(IN) :: z REAL(SP), DIMENSION(size(z)) :: func END FUNCTION func FUNCTION y1(x)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE quad3d_qgaus(x1,x2,ss) REAL(SP), INTENT(IN) :: x1,x2 REAL(SP), INTENT(OUT) :: ss ss=qgaus(h,x1,x2) END SUBROUTINE quad3d_qgaus END MODULE quad3d_qgaus_mod
1068
Chapter B4.
Integration of Functions
USE nrtype REAL(SP), INTENT(IN) :: x REAL(SP) :: y1 END FUNCTION y1
FUNCTION z1(x,y) USE nrtype REAL(SP), INTENT(IN) :: x,y REAL(SP) :: z1 END FUNCTION z1 FUNCTION z2(x,y) USE nrtype REAL(SP), INTENT(IN) :: x,y REAL(SP) :: z2 END FUNCTION z2 END INTERFACE CONTAINS FUNCTION h(x) REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: h INTEGER(I4B) :: i do i=1,size(x) xsav=x(i) h(i)=qromb(g,y1(xsav),y2(xsav)) end do END FUNCTION h FUNCTION g(y) REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(size(y)) :: g INTEGER(I4B) :: j do j=1,size(y) ysav=y(j) g(j)=qromb(f,z1(xsav,ysav),z2(xsav,ysav)) end do END FUNCTION g FUNCTION f(z) REAL(SP), DIMENSION(:), INTENT(IN) :: z REAL(SP), DIMENSION(size(z)) :: f f=func(xsav,ysav,z) END FUNCTION f RECURSIVE FUNCTION qromb(func,a,b) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : polint IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP) :: qromb INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: JMAX=20,JMAXP=JMAX+1,K=5,KM=K-1 REAL(SP), PARAMETER :: EPS=3.0e-6_sp REAL(SP), DIMENSION(JMAXP) :: h,s REAL(SP) :: dqromb
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION y2(x) USE nrtype REAL(SP), INTENT(IN) :: x REAL(SP) :: y2 END FUNCTION y2
Chapter B4. Integration of Functions
1069
RECURSIVE SUBROUTINE trapzd(func,a,b,s,n) USE nrtype; USE nrutil, ONLY : arth IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), INTENT(INOUT) :: s INTEGER(I4B), INTENT(IN) :: n INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE REAL(SP) :: del,fsum INTEGER(I4B) :: it if (n == 1) then s=0.5_sp*(b-a)*sum(func( (/ a,b /) )) else it=2**(n-2) del=(b-a)/it fsum=sum(func(arth(a+0.5_sp*del,del,it))) s=0.5_sp*(s+del*fsum) end if END SUBROUTINE trapzd SUBROUTINE quad3d_qromb(x1,x2,ss) REAL(SP), INTENT(IN) :: x1,x2 REAL(SP), INTENT(OUT) :: ss ss=qromb(h,x1,x2) END SUBROUTINE quad3d_qromb END MODULE quad3d_qromb_mod
MODULE quad3d qromb_mod The only difference between this module and the previous one is that all calls to qgaus are replaced by calls to qromb and that the routine qgaus is replaced by qromb and trapzd.
CITED REFERENCES AND FURTHER READING: Erdelyi, ´ A., Magnus, W., Oberhettinger, F., and Tricomi, F.G. 1953, Higher Transcendental Functions, Volume II (New York: McGraw-Hill). [1] Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 by Dover Publications, New York). [2]
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
INTEGER(I4B) :: j h(1)=1.0 do j=1,JMAX call trapzd(func,a,b,s(j),j) if (j >= K) then call polint(h(j-KM:j),s(j-KM:j),0.0_sp,qromb,dqromb) if (abs(dqromb) reallocate(wksp,100) wksp(1)=term sum=0.5_sp*term Return first estimate. else if (nterm+1 > size(wksp)) wksp=>reallocate(wksp,2*size(wksp)) wksp(2:nterm+1)=0.5_sp*wksp(1:nterm) Update saved quantities by van Wijnwksp(1)=term gaarden’s algorithm. wksp(1:nterm+1)=poly_term(wksp(1:nterm+1),0.5_sp) if (abs(wksp(nterm+1)) reallocate(wksp,100) allocates an array of length 100 and points wksp to it. On subsequent calls to eulsum, if nterm ever gets bigger than the size of wksp, the call to reallocate doubles the size of wksp and copies the old contents into the new storage. You could achieve the same effect as the code if (init)...nullify(wksp)... wksp=>reallocate(wksp,100) with a simple allocate(wksp,100). You would then use
f90
1070
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B5.
Chapter B5. Evaluation of Functions
1071
wksp(1:nterm+1)=poly_term(wksp(1:nterm+1),0.5_sp) The poly term function in nrutil tabulates the partial sums of a polynomial, or, equivalently, performs the synthetic division of a polynomial by a monomial. Small-scale parallelism in eulsum is achieved straightforwardly by the use of vector constructions and poly term (which parallelizes recursively). The routine is not written to take advantage of data parallelism in the (infrequent) case of wanting to sum many different series simultaneously; nor, since wksp is a SAVEd variable, can it be used in many simultaneous instances on a MIMD machine. (You can easily recode these generalizations if you need them.)
SUBROUTINE ddpoly(c,x,pd) USE nrtype; USE nrutil, ONLY : arth,cumprod,poly_term IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: c REAL(SP), DIMENSION(:), INTENT(OUT) :: pd Given the coefficients of a polynomial of degree Nc − 1 as an array c(1:Nc) with c(1) being the constant term, and given a value x, this routine returns the polynomial evaluated at x as pd(1) and Nd − 1 derivatives as pd(2:Nd). INTEGER(I4B) :: i,nc,nd REAL(SP), DIMENSION(size(pd)) :: fac REAL(SP), DIMENSION(size(c)) :: d nc=size(c) nd=size(pd) d(nc:1:-1)=poly_term(c(nc:1:-1),x) do i=2,min(nd,nc) d(nc:i:-1)=poly_term(d(nc:i:-1),x) end do pd=d(1:nd) fac=cumprod(arth(1.0_sp,1.0_sp,nd)) After the first derivative, factorial constants pd(3:nd)=fac(2:nd-1)*pd(3:nd) come in. END SUBROUTINE ddpoly
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
reallocate only for increasing the storage if necessary. Don’t! The advantage of the above scheme becomes clear if you consider what happens if eulsum is invoked twice by the calling program to evaluate two different sums. On the second invocation, when jterm = 1 again, you would be allocating an already allocated pointer. This does not generate an error — it simply leaves the original target inaccessible. Using reallocate instead not only allocates a new array of length 100, but also detects that wksp had already been associated. It dutifully (and wastefully) copies the first 100 elements of the old wksp into the new storage, and, more importantly, deallocates the old wksp, reclaiming its storage. While only two invocations of eulsum without intervening deallocation of memory would not cause a problem, many such invocations might well. We believe that, as a general rule, the potential for catastrophe from reckless use of allocate is great enough that you should always deallocate whenever storage is no longer required. The unnecessary copying of 100 elements when eulsum is invoked a second time could be avoided by making init an argument. It hardly seems worth it to us. For Fortran 90 neophytes, note that unlike in C you have to do nothing special to get the contents of the storage a pointer is addressing. The compiler figures out from the context whether you mean the contents, such as wksp(1:nterm), or the address, such as both occurrences of wksp in wksp=>reallocate(wksp,100).
1072
Chapter B5.
Evaluation of Functions
The poly term function in nrutil tabulates the partial sums of a polynomial, or, equivalently, performs synthetic division. See §22.3 for a discussion of why ddpoly is coded this way.
f90
d(nc:1:-1)=poly_term(c(nc:1:-1),x)
SUBROUTINE poldiv(u,v,q,r) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: u,v REAL(SP), DIMENSION(:), INTENT(OUT) :: q,r Given the N coefficients of a polynomial in u, and the Nv coefficients of another polynomial in v, divide the polynomial u by the polynomial v (“u”/“v”) giving a quotient polynomial whose coefficients are returned in q, and a remainder polynomial whose coefficients are returned in r. The arrays q and r are of length N , but only the first N − Nv + 1 elements of q and the first Nv − 1 elements of r are used. The remaining elements are returned as zero. INTEGER(I4B) :: i,n,nv n=assert_eq(size(u),size(q),size(r),’poldiv’) nv=size(v) r(:)=u(:) q(:)=0.0 do i=n-nv,0,-1 q(i+1)=r(nv+i)/v(nv) r(i+1:nv+i-1)=r(i+1:nv+i-1)-q(i+1)*v(1:nv-1) end do r(nv:n)=0.0 END SUBROUTINE poldiv
FUNCTION ratval_s(x,cof,mm,kk) USE nrtype; USE nrutil, ONLY : poly IMPLICIT NONE REAL(DP), INTENT(IN) :: x Note precision! Change to REAL(SP) if desired. INTEGER(I4B), INTENT(IN) :: mm,kk REAL(DP), DIMENSION(mm+kk+1), INTENT(IN) :: cof REAL(DP) :: ratval_s Given mm, kk, and cof(1:mm+kk+1), evaluate and return the rational function (cof(1) +
cof(2)x + · · · + cof(mm+1)xmm )/(1 + cof(mm+2)x + · · · + cof(mm+kk+1)xkk ). ratval_s=poly(x,cof(1:mm+1))/(1.0_dp+x*poly(x,cof(mm+2:mm+kk+1))) END FUNCTION ratval_s
This simple routine uses the function poly from nrutil to evaluate the numerator and denominator polynomials. Single- and double-precision versions, ratval s and ratval v, are overloaded onto the name ratval when the module nr is used.
f90
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
fac=cumprod(arth(1.0_sp,1.0_sp,nd)) Here the function arth from nrutil generates the sequence 1, 2, 3. . . . The function cumprod then tabulates the cumulative products, thus making a table of factorials. Notice that ddpoly doesn’t need an argument to pass N d , the number of output terms desired by the user: It gets that information from the length of the array pd that the user provides for it to fill. It is a minor curiosity that pd, declared as INTENT(OUT), can thus be used, on the sly, to pass some INTENT(IN) information. (A Fortran 90 brain teaser could be: A subroutine with only INTENT(OUT) arguments can be called to print any specified integer. How is this done?)
Chapter B5.
Evaluation of Functions
1073
The routines recur1 and recur2 are new in this volume, and do not have Fortran 77 counterparts. First- and second-order linear recurrences are implemented as trivial do-loops on strictly serial machines. On parallel machines, however, they pose different, and quite interesting, programming challenges. Since many calculations can be decomposed into recurrences, it is useful to have general, parallelizable routines available. The algorithms behind recur1 and recur2 are discussed in §22.2. RECURSIVE FUNCTION recur1(a,b) RESULT(u) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: a,b REAL(SP), DIMENSION(size(a)) :: u INTEGER(I4B), PARAMETER :: NPAR_RECUR1=8 Given vectors a of size n and b of size n − 1, returns a vector u that satisfies the first order linear recurrence u1 = a1 , uj = aj + bj−1 uj−1 , for j = 2, . . . , n. Parallelization is via a recursive evaluation. INTEGER(I4B) :: n,j n=assert_eq(size(a),size(b)+1,’recur1’) u(1)=a(1) if (n < NPAR_RECUR1) then Do short vectors as a loop. do j=2,n u(j)=a(j)+b(j-1)*u(j-1) end do else Otherwise, combine coefficients and recurse on the even components, then evaluate all the odd components in parallel. u(2:n:2)=recur1(a(2:n:2)+a(1:n-1:2)*b(1:n-1:2), & b(3:n-1:2)*b(2:n-2:2)) u(3:n:2)=a(3:n:2)+b(2:n-1:2)*u(2:n-1:2) end if END FUNCTION recur1
When a recursive function invokes itself only indirectly through a sequence of function calls, then the function name can be used for the result just as in a nonrecursive function. When the function invokes itself directly, however, as in recur1, then another name must be used for the result. If you are hazy on the syntax for RESULT, see the discussion of recursion in §21.5.
f90
RECURSIVE FUNCTION recur1(a,b) RESULT(u)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION ratval_v(x,cof,mm,kk) USE nrtype; USE nrutil, ONLY : poly IMPLICIT NONE REAL(DP), DIMENSION(:), INTENT(IN) :: x INTEGER(I4B), INTENT(IN) :: mm,kk REAL(DP), DIMENSION(mm+kk+1), INTENT(IN) :: cof REAL(DP), DIMENSION(size(x)) :: ratval_v ratval_v=poly(x,cof(1:mm+1))/(1.0_dp+x*poly(x,cof(mm+2:mm+kk+1))) END FUNCTION ratval_v
1074
Chapter B5.
Evaluation of Functions
RECURSIVE SUBROUTINE recur1_v(a1,a2,b11,b12,b21,b22,u1,u2) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: a1,a2,b11,b12,b21,b22 REAL(SP), DIMENSION(:), INTENT(OUT) :: u1,u2 INTEGER(I4B), PARAMETER :: NPAR_RECUR2=8 Used by recur2 to evaluate first order vector recurrence. Routine is a two-dimensional vector version of recur1, with matrix multiplication replacing scalar multiplication. INTEGER(I4B) :: n,j,nn,nn1 REAL(SP), DIMENSION(size(a1)/2) :: aa1,aa2 REAL(SP), DIMENSION(size(a1)/2-1) :: bb11,bb12,bb21,bb22 n=assert_eq((/size(a1),size(a2),size(b11)+1,size(b12)+1,size(b21)+1,& size(b22)+1,size(u1),size(u2)/),’recur1_v’) u1(1)=a1(1) u2(1)=a2(1) if (n < NPAR_RECUR2) then Do short vectors as a loop. do j=2,n u1(j)=a1(j)+b11(j-1)*u1(j-1)+b12(j-1)*u2(j-1) u2(j)=a2(j)+b21(j-1)*u1(j-1)+b22(j-1)*u2(j-1) end do else Otherwise, combine coefficients and recurse on the even components, then evaluate all the odd components in parallel. nn=n/2 nn1=nn-1 aa1(1:nn)=a1(2:n:2)+b11(1:n-1:2)*a1(1:n-1:2)+& b12(1:n-1:2)*a2(1:n-1:2) aa2(1:nn)=a2(2:n:2)+b21(1:n-1:2)*a1(1:n-1:2)+& b22(1:n-1:2)*a2(1:n-1:2) bb11(1:nn1)=b11(3:n-1:2)*b11(2:n-2:2)+& b12(3:n-1:2)*b21(2:n-2:2) bb12(1:nn1)=b11(3:n-1:2)*b12(2:n-2:2)+& b12(3:n-1:2)*b22(2:n-2:2) bb21(1:nn1)=b21(3:n-1:2)*b11(2:n-2:2)+& b22(3:n-1:2)*b21(2:n-2:2) bb22(1:nn1)=b21(3:n-1:2)*b12(2:n-2:2)+& b22(3:n-1:2)*b22(2:n-2:2) call recur1_v(aa1,aa2,bb11,bb12,bb21,bb22,u1(2:n:2),u2(2:n:2)) u1(3:n:2)=a1(3:n:2)+b11(2:n-1:2)*u1(2:n-1:2)+&
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION recur2(a,b,c) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: a,b,c REAL(SP), DIMENSION(size(a)) :: recur2 Given vectors a of size n and b and c of size n−2, returns a vector u that satisfies the second order linear recurrence u1 = a1 , u2 = a2 , uj = aj +bj−2 uj−1 +cj−2 uj−2 , for j = 3, . . . , n. Parallelization is via conversion to a first order recurrence for a two-dimensional vector. INTEGER(I4B) :: n REAL(SP), DIMENSION(size(a)-1) :: a1,a2,u1,u2 REAL(SP), DIMENSION(size(a)-2) :: b11,b12,b21,b22 n=assert_eq(size(a),size(b)+2,size(c)+2,’recur2’) a1(1)=a(1) Set up vector a. a2(1)=a(2) a1(2:n-1)=0.0 a2(2:n-1)=a(3:n) b11(1:n-2)=0.0 Set up matrix b. b12(1:n-2)=1.0 b21(1:n-2)=c(1:n-2) b22(1:n-2)=b(1:n-2) call recur1_v(a1,a2,b11,b12,b21,b22,u1,u2) recur2(1:n-1)=u1(1:n-1) recur2(n)=u2(n-1) CONTAINS
Chapter B5. Evaluation of Functions
1075
b12(2:n-1:2)*u2(2:n-1:2) u2(3:n:2)=a2(3:n:2)+b21(2:n-1:2)*u1(2:n-1:2)+& b22(2:n-1:2)*u2(2:n-1:2) end if END SUBROUTINE recur1_v END FUNCTION recur2
FUNCTION dfridr(func,x,h,err) USE nrtype; USE nrutil, ONLY : assert,geop,iminloc IMPLICIT NONE REAL(SP), INTENT(IN) :: x,h REAL(SP), INTENT(OUT) :: err REAL(SP) :: dfridr INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE INTEGER(I4B),PARAMETER :: NTAB=10 REAL(SP), PARAMETER :: CON=1.4_sp,CON2=CON*CON,BIG=huge(x),SAFE=2.0 Returns the derivative of a function func at a point x by Ridders’ method of polynomial extrapolation. The value h is input as an estimated initial stepsize; it need not be small, but rather should be an increment in x over which func changes substantially. An estimate of the error in the derivative is returned as err. Parameters: Stepsize is decreased by CON at each iteration. Max size of tableau is set by NTAB. Return when error is SAFE worse than the best so far. INTEGER(I4B) :: ierrmin,i,j REAL(SP) :: hh REAL(SP), DIMENSION(NTAB-1) :: errt,fac REAL(SP), DIMENSION(NTAB,NTAB) :: a call assert(h /= 0.0, ’dfridr arg’) hh=h a(1,1)=(func(x+hh)-func(x-hh))/(2.0_sp*hh) err=BIG fac(1:NTAB-1)=geop(CON2,CON2,NTAB-1) do i=2,NTAB Successive columns in the Neville tableau will go to smaller hh=hh/CON stepsizes and higher orders of extrapolation. a(1,i)=(func(x+hh)-func(x-hh))/(2.0_sp*hh) Try new, smaller stepsize. do j=2,i Compute extrapolations of various orders, requiring no new function evaluations. a(j,i)=(a(j-1,i)*fac(j-1)-a(j-1,i-1))/(fac(j-1)-1.0_sp) end do errt(1:i-1)=max(abs(a(2:i,i)-a(1:i-1,i)),abs(a(2:i,i)-a(1:i-1,i-1))) The error strategy is to compare each new extrapolation to one order lower, both at the present stepsize and the previous one. ierrmin=iminloc(errt(1:i-1)) if (errt(ierrmin) = SAFE*err) RETURN If higher order is worse by a significant factor SAFE, then quit early. end do END FUNCTION dfridr
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
1076
f90
Chapter B5.
Evaluation of Functions
The function iminloc in nrutil is useful when you need to know the index of the smallest element in an array.
ierrmin=iminloc(errt(1:i-1))
Here again Fortran 90 produces a very concise parallelizable formulation that requires some effort to decode. Equation (5.8.7) is a product of the matrix of cosines, where the rows are indexed by j and the columns by k, with the vector of function values indexed by k. We use the outerprod function in nrutil to form the matrix of arguments for the cosine, and rely on the element-by-element application of cos to produce the matrix of cosines. matmul then takes care of the matrix product. A subtlety is that, while the calculation is being done in double precision to minimize roundoff, the function is assumed to be supplied in single precision. Thus real(...,sp) is used to convert the double precision argument to single precision.
f90
chebft(:)=matmul(...)
FUNCTION chebev_s(a,b,c,x) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b,x REAL(SP), DIMENSION(:), INTENT(IN) :: c REAL(SP) :: chebev_s Chebyshev evaluation: All arguments are input. c is an array of length M of Chebyshev coefficients, the first M elements of c output from chebft (which must have been called
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION chebft(a,b,n,func) USE nrtype; USE nrutil, ONLY : arth,outerprod IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(n) :: chebft INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE Chebyshev fit: Given a function func, lower and upper limits of the interval [a,b], and a maximum degree n, this routine computes the n coefficients ck such that func(x) ≈ [ n k=1 ck Tk−1 (y)] − c1 /2, where y and x are related by (5.8.10). This routine is to be used with moderately large n (e.g., 30 or 50), the array of c’s subsequently to be truncated at the smaller value m such that cm+1 and subsequent elements are negligible. REAL(DP) :: bma,bpa REAL(DP), DIMENSION(n) :: theta bma=0.5_dp*(b-a) bpa=0.5_dp*(b+a) theta(:)=PI_D*arth(0.5_dp,1.0_dp,n)/n chebft(:)=matmul(cos(outerprod(arth(0.0_dp,1.0_dp,n),theta)), & func(real(cos(theta)*bma+bpa,sp)))*2.0_dp/n We evaluate the function at the n points required by (5.8.7). We accumulate the sum in double precision for safety. END FUNCTION chebft
Chapter B5.
Evaluation of Functions
1077
FUNCTION chebev_v(a,b,c,x) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), DIMENSION(:), INTENT(IN) :: c,x REAL(SP), DIMENSION(size(x)) :: chebev_v INTEGER(I4B) :: j,m REAL(SP), DIMENSION(size(x)) :: d,dd,sv,y,y2 if (any((x-a)*(x-b) > 0.0)) call nrerror(’x not in range in chebev_v’) m=size(c) d=0.0 dd=0.0 y=(2.0_sp*x-a-b)/(b-a) y2=2.0_sp*y do j=m,2,-1 sv=d d=y2*d-dd+c(j) dd=sv end do chebev_v=y*d-dd+0.5_sp*c(1) END FUNCTION chebev_v
The name chebev is overloaded with scalar and vector versions. chebev v is essentially identical to chebev s except for the declarations of the variables. Fortran 90 does the appropriate scalar or vector arithmetic in the body of the routine, depending on the type of the variables.
f90
FUNCTION chder(a,b,c) USE nrtype; USE nrutil, ONLY : arth,cumsum IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), DIMENSION(:), INTENT(IN) :: c REAL(SP), DIMENSION(size(c)) :: chder This routine returns an array of length N containing the Chebyshev coefficients of the derivative of the function whose coefficients are in the array c. Input are a,b,c, as output
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
with the same a and b). The Chebyshev polynomial M k=1 ck Tk−1 (y) − c1 /2 is evaluated at a point y = [x − (b + a)/2]/[(b − a)/2], and the result is returned as the function value. INTEGER(I4B) :: j,m REAL(SP) :: d,dd,sv,y,y2 if ((x-a)*(x-b) > 0.0) call nrerror(’x not in range in chebev_s’) m=size(c) d=0.0 dd=0.0 y=(2.0_sp*x-a-b)/(b-a) Change of variable. y2=2.0_sp*y do j=m,2,-1 Clenshaw’s recurrence. sv=d d=y2*d-dd+c(j) dd=sv end do chebev_s=y*d-dd+0.5_sp*c(1) Last step is different. END FUNCTION chebev_s
1078
Chapter B5.
Evaluation of Functions
FUNCTION chint(a,b,c) USE nrtype; USE nrutil, ONLY : arth IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), DIMENSION(:), INTENT(IN) :: c REAL(SP), DIMENSION(size(c)) :: chint This routine returns an array of length N containing the Chebyshev coefficients of the integral of the function whose coefficients are in the array c. Input are a,b,c, as output from routine chebft §5.8. The desired degree of approximation N is equal to the length of c supplied. The constant of integration is set so that the integral vanishes at a. INTEGER(I4B) :: n REAL(SP) :: con n=size(c) con=0.25_sp*(b-a) Factor that normalizes to the interval b-a. chint(2:n-1)=con*(c(1:n-2)-c(3:n))/arth(1,1,n-2) Equation (5.9.1). chint(n)=con*c(n-1)/(n-1) Special case of (5.9.1) for n. chint(1)=2.0_sp*(sum(chint(2:n:2))-sum(chint(3:n:2))) Set the constant of inteEND FUNCTION chint gration.
If you look at equation (5.9.1) for the Chebyshev coefficients of the integral of a function, you will see c i−1 and ci+1 and be tempted to use eoshift. We think it is almost always better to use array sections instead, as in the code above, especially if your code will ever run on a serial machine.
f90
FUNCTION chebpc(c) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: c REAL(SP), DIMENSION(size(c)) :: chebpc Chebyshev polynomial coefficients. Given a coefficient array c of length N , this routine N k−1 = returns a coefficient array d of length N such that N k=1 dk y k=1 ck Tk−1 (y) − c1 /2. The method is Clenshaw’s recurrence (5.8.11), but now applied algebraically rather than arithmetically. INTEGER(I4B) :: j,n REAL(SP), DIMENSION(size(c)) :: dd,sv n=size(c) chebpc=0.0 dd=0.0 chebpc(1)=c(n) do j=n-1,2,-1 sv(2:n-j+1)=chebpc(2:n-j+1) chebpc(2:n-j+1)=2.0_sp*chebpc(1:n-j)-dd(2:n-j+1) dd(2:n-j+1)=sv(2:n-j+1)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
from routine chebft §5.8. The desired degree of approximation N is equal to the length of c supplied. INTEGER(I4B) :: n REAL(SP) :: con REAL(SP), DIMENSION(size(c)) :: temp n=size(c) temp(1)=0.0 temp(2:n)=2.0_sp*arth(n-1,-1,n-1)*c(n:2:-1) chder(n:1:-2)=cumsum(temp(1:n:2)) Equation (5.9.2). chder(n-1:1:-2)=cumsum(temp(2:n:2)) con=2.0_sp/(b-a) chder=chder*con Normalize to the interval b-a. END FUNCTION chder
Chapter B5. Evaluation of Functions
1079
sv(1)=chebpc(1) chebpc(1)=-dd(1)+c(j) dd(1)=sv(1) end do chebpc(2:n)=chebpc(1:n-1)-dd(2:n) chebpc(1)=-dd(1)+0.5_sp*c(1) END FUNCTION chebpc
SUBROUTINE pcshft(a,b,d) USE nrtype; USE nrutil, ONLY : geop IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), DIMENSION(:), INTENT(INOUT) :: d Polynomial coefficient shift. Given a coefficient array d of length N , this routine generates N k−1 = k−1 , where a coefficient array g of the same length such that N k=1 dk y k=1 gk x x and y are related by (5.8.10), i.e., the interval −1 < y < 1 is mapped to the interval a < x < b. The array g is returned in d. INTEGER(I4B) :: j,n REAL(SP), DIMENSION(size(d)) :: dd REAL(SP) :: x n=size(d) dd=d*geop(1.0_sp,2.0_sp/(b-a),n) x=-0.5_sp*(a+b) d(1)=dd(n) d(2:n)=0.0 do j=n-1,1,-1 We accomplish the shift by synthetic division, that miracle of d(2:n+1-j)=d(2:n+1-j)*x+d(1:n-j) high-school algebra. d(1)=d(1)*x+dd(j) end do END SUBROUTINE pcshft
There is a subtle, but major, distinction between the synthetic division algorithm used in the Fortran 77 version of pcshft and that used above. In the Fortran 77 version, the synthetic division (translated to Fortran 90 notation) is d(1:n)=dd(1:n) do j=1,n-1 do k=n-1,j,-1 d(k)=x*d(k+1)+d(k) end do end do
while, in Fortran 90, it is d(1)=dd(n) d(2:n)=0.0 do j=n-1,1,-1 d(2:n+1-j)=d(2:n+1-j)*x+d(1:n-j) d(1)=d(1)*x+dd(j) end do
As explained in §22.3, these are algebraically — but not algorithmically — equivalent. The inner loop in the Fortran 77 version does not parallelize, because each k value uses the result of the previous one. In fact, the k loop is a synthetic division, which can be parallelized recursively (as in the nrutil routine poly term), but not simply
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
1080
Chapter B5.
Evaluation of Functions
vectorized. In the Fortran 90 version, since not one but n-1 successive synthetic divisions are to be performed (by the outer loop), it is possible to reorganize the calculation to allow vectorization.
SUBROUTINE pade(cof,resid) USE nrtype USE nr, ONLY : lubksb,ludcmp,mprove IMPLICIT NONE REAL(DP), DIMENSION(:), INTENT(INOUT) :: cof DP for consistency with ratval. REAL(SP), INTENT(OUT) :: resid Given cof(1:2N + 1), the leading terms in the power series expansion of a function, solve the linear Pad´e equations to return the coefficients of a diagonal rational function approximation to the same function, namely (cof(1) + cof(2)x + · · · + cof(N + 1)xN )/(1 + cof(N + 2)x + · · · + cof(2N + 1)xN ). The value resid is the norm of the residual vector; a small value indicates a well-converged solution. INTEGER(I4B) :: k,n INTEGER(I4B), DIMENSION((size(cof)-1)/2) :: indx REAL(SP), PARAMETER :: BIG=1.0e30_sp A big number. REAL(SP) :: d,rr,rrold REAL(SP), DIMENSION((size(cof)-1)/2) :: x,y,z REAL(SP), DIMENSION((size(cof)-1)/2,(size(cof)-1)/2) :: q,qlu n=(size(cof)-1)/2 x=cof(n+2:2*n+1) Set up matrix for solving. y=x do k=1,n q(:,k)=cof(n+2-k:2*n+1-k) end do qlu=q call ludcmp(qlu,indx,d) Solve by LU decomposition and backsubsticall lubksb(qlu,indx,x) tution. rr=BIG do Important to use iterative improvement, since rrold=rr the Pad´ e equations tend to be ill-conditioned.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION pccheb(d) USE nrtype; USE nrutil, ONLY : arth,cumprod,geop IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: d REAL(SP), DIMENSION(size(d)) :: pccheb Inverse of routine chebpc: given an array of polynomial coefficients d, returns an equivalent array of Chebyshev coefficients of the same length. INTEGER(I4B) :: k,n REAL(SP), DIMENSION(size(d)) :: denom,numer,pow n=size(d) pccheb(1)=2.0_sp*d(1) pow=geop(1.0_sp,2.0_sp,n) Powers of 2. numer(1)=1.0 Combinatorial coefficients computed as numer/denom. denom(1)=1.0 denom(2:(n+3)/2)=cumprod(arth(1.0_sp,1.0_sp,(n+1)/2)) pccheb(2:n)=0.0 do k=2,n Loop over orders of x in the polynomial. numer(2:(k+3)/2)=cumprod(arth(k-1.0_sp,-1.0_sp,(k+1)/2)) pccheb(k:1:-2)=pccheb(k:1:-2)+& d(k)/pow(k-1)*numer(1:(k+1)/2)/denom(1:(k+1)/2) end do END FUNCTION pccheb
Chapter B5.
Evaluation of Functions
1081
SUBROUTINE ratlsq(func,a,b,mm,kk,cof,dev) USE nrtype; USE nrutil, ONLY : arth,geop USE nr, ONLY : ratval,svbksb,svdcmp IMPLICIT NONE REAL(DP), INTENT(IN) :: a,b INTEGER(I4B), INTENT(IN) :: mm,kk REAL(DP), DIMENSION(:), INTENT(OUT) :: cof REAL(DP), INTENT(OUT) :: dev INTERFACE FUNCTION func(x) USE nrtype REAL(DP), DIMENSION(:), INTENT(IN) :: x REAL(DP), DIMENSION(size(x)) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: NPFAC=8,MAXIT=5 REAL(DP), PARAMETER :: BIG=1.0e30_dp Returns in cof(1:mm+kk+1) the coefficients of a rational function approximation to the function func in the interval (a, b). Input quantities mm and kk specify the order of the numerator and denominator, respectively. The maximum absolute deviation of the approximation (insofar as is known) is returned as dev. Note that double-precision versions of svdcmp and svbksb are called. INTEGER(I4B) :: it,ncof,npt,npth REAL(DP) :: devmax,e,theta REAL(DP), DIMENSION((mm+kk+1)*NPFAC) :: bb,ee,fs,wt,xs REAL(DP), DIMENSION(mm+kk+1) :: coff,w REAL(DP), DIMENSION(mm+kk+1,mm+kk+1) :: v REAL(DP), DIMENSION((mm+kk+1)*NPFAC,mm+kk+1) :: u,temp ncof=mm+kk+1 npt=NPFAC*ncof Number of points where function is evaluated, npth=npt/2 i.e., fineness of the mesh. dev=BIG theta=PIO2_D/(npt-1) xs(1:npth-1)=a+(b-a)*sin(theta*arth(0,1,npth-1))**2 Now fill arrays with mesh abscissas and function values. At each end, use formula that minimizes roundoff sensitivity in xs. xs(npth:npt)=b-(b-a)*sin(theta*arth(npt-npth,-1,npt-npth+1))**2 fs=func(xs) wt=1.0 In later iterations we will adjust these weights to ee=1.0 combat the largest deviations. e=0.0 do it=1,MAXIT Loop over iterations. bb=wt*(fs+sign(e,ee)) Key idea here: Fit to fn(x) + e where the deviation is positive, to fn(x) − e where it is negative. Then e is supposed to become an approximation to the equal-ripple deviation. temp=geop(spread(1.0_dp,1,npt),xs,ncof)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
z=x call mprove(q,qlu,indx,y,x) rr=sum((z-x)**2) Calculate residual. if (rr >= rrold) exit If it is no longer improving, call it quits. end do resid=sqrt(rrold) do k=1,n Calculate the remaining coefficients. y(k)=cof(k+1)-dot_product(z(1:k),cof(k:1:-1)) end do cof(2:n+1)=y Copy answers to output. cof(n+2:2*n+1)=-z END SUBROUTINE pade
1082
Chapter B5.
Evaluation of Functions
f90
temp=geop(spread(1.0_dp,1,npt),xs,ncof)
The design matrix uij is de-
fined for i = 1, . . . , npts by uij =
wi xij−1 , −bi xij−m−2 ,
j = 1, . . . , m + 1 j = m + 2, . . . , n
(B5.12)
The first case in equation (B5.12) is computed in parallel by constructing the matrix temp equal to 1 x 1 1 x2 1 x3 .. .. . .
x21 x22 x23 .. .
··· ··· ··· .. .
and then multiplying by the matrix spread(wt,2,mm+1), which is just
w1 w2 w3 .. .
w1 w2 w3 .. .
w1 w2 w3 .. .
··· · · · · · · .. .
(Remember that multiplication using * means element-by-element multiplication, not matrix multiplication.) A similar construction is used for the second part of the design matrix.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Note that vector form of geop (returning matrix) is being used. u(:,1:mm+1)=temp(:,1:mm+1)*spread(wt,2,mm+1) Set up the “design matrix” for the least squares fit. u(:,mm+2:ncof)=-temp(:,2:ncof-mm)*spread(bb,2,ncof-mm-1) call svdcmp(u,w,v) Singular Value Decomposition. In especially singular or difficult cases, one might here edit the singular values w(1:ncof), replacing small values by zero. call svbksb(u,w,v,bb,coff) ee=ratval(xs,coff,mm,kk)-fs Tabulate the deviations and revise the weights. wt=abs(ee) Use weighting to emphasize most deviant points. devmax=maxval(wt) e=sum(wt)/npt Update e to be the mean absolute deviation. if (devmax 0. REAL(DP) :: tmp,x Internal arithmetic will be done in double precision, a nicety that you can omit if five-figure accuracy is good enough. REAL(DP) :: stp = 2.5066282746310005_dp REAL(DP), DIMENSION(6) :: coef = (/76.18009172947146_dp,& -86.50532032941677_dp,24.01409824083091_dp,& -1.231739572450155_dp,0.1208650973866179e-2_dp,& -0.5395239384953e-5_dp/) call assert(xx > 0.0, ’gammln_s arg’) x=xx tmp=x+5.5_dp tmp=(x+0.5_dp)*log(tmp)-tmp gammln_s=tmp+log(stp*(1.000000000190015_dp+& sum(coef(:)/arth(x+1.0_dp,1.0_dp,size(coef))))/x) END FUNCTION gammln_s
FUNCTION gammln_v(xx) USE nrtype; USE nrutil, ONLY: assert IMPLICIT NONE INTEGER(I4B) :: i REAL(SP), DIMENSION(:), INTENT(IN) :: xx REAL(SP), DIMENSION(size(xx)) :: gammln_v REAL(DP), DIMENSION(size(xx)) :: ser,tmp,x,y REAL(DP) :: stp = 2.5066282746310005_dp REAL(DP), DIMENSION(6) :: coef = (/76.18009172947146_dp,& -86.50532032941677_dp,24.01409824083091_dp,& -1.231739572450155_dp,0.1208650973866179e-2_dp,& -0.5395239384953e-5_dp/) if (size(xx) == 0) RETURN call assert(all(xx > 0.0), ’gammln_v arg’) x=xx tmp=x+5.5_dp tmp=(x+0.5_dp)*log(tmp)-tmp ser=1.000000000190015_dp y=x do i=1,size(coef) y=y+1.0_dp ser=ser+coef(i)/y end do gammln_v=tmp+log(stp*ser/x) END FUNCTION gammln_v
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
1086
Chapter B6.
Special Functions
We use the nrutil routine assert for functions that have restrictions on the allowed range of arguments. One could instead have used an if statement with a call to nrerror; but we think that the uniformity of using assert, and the fact that its logical arguments read the “desired” way, not the “erroneous” way, make for a clearer programming style. In the vector version, the assert line is:
f90
call assert(xx > 0.0, ’gammln_s arg’)
Notice that the scalar and vector versions achieve parallelism in quite different ways, something that we will see many times in this chapter. In the scalar case, parallelism (at least small-scale) is achieved through constructions like sum(coef(:)/arth(x+1.0_dp,1.0_dp,size(coef)))
Here vector utilities construct the series x + 1, x + 2, . . . and then sum a series with these terms in the denominators and a vector of coefficients in the numerators. (This code may seem terse to Fortran 90 novices, but once you get used to it, it is quite clear to read.) In the vector version, by contrast, parallelism is achieved across the components of the vector argument, and the above series is evaluated sequentially as a do-loop. Obviously the assumption is that the length of the vector argument is much longer than the very modest number (here, 6) of terms in the sum.
FUNCTION factrl_s(n) USE nrtype; USE nrutil, ONLY : arth,assert,cumprod USE nr, ONLY : gammln IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP) :: factrl_s Returns the value n! as a floating-point number. INTEGER(I4B), SAVE :: ntop=0 INTEGER(I4B), PARAMETER :: NMAX=32 REAL(SP), DIMENSION(NMAX), SAVE :: a Table of stored values. call assert(n >= 0, ’factrl_s arg’) if (n < ntop) then Already in table. factrl_s=a(n+1) else if (n < NMAX) then Fill in table up to NMAX. ntop=NMAX a(1)=1.0 a(2:NMAX)=cumprod(arth(1.0_sp,1.0_sp,NMAX-1)) factrl_s=a(n+1) else Larger value than size of table is required. Actually, this big a value is going to overfactrl_s=exp(gammln(n+1.0_sp)) flow on many computers, but no harm in end if trying. END FUNCTION factrl_s
By now you should recognize this as an idiom for generating a vector of consecutive factorials. The routines cumprod and arth, both in nrutil, are both capable of being parallelized, e.g., by recursion, so this idiom is potentially faster than an in-line do-loop.
f90
cumprod(arth(1.0_sp,1.0_sp,NMAX-1))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call assert(all(xx > 0.0), ’gammln_v arg’)
Chapter B6.
Special Functions
1087
Here we meet the first of several solutions to a common problem: How shall we get answers, from an external vector-valued function, for just a subset of vector arguments, those defined by a mask? Here we use what we call the “packunpack” solution: Pack up all the arguments using the mask, send them to the function, and unpack the answers that come back. This packing and unpacking is not without cost (highly dependent on machine architecture, to be sure), but we hope to “earn it back” in the parallelism of the external function. unpack(exp(gammln(pack(n,mask)+1.0_sp)),mask,0.0_sp)
where (.not. mask) factrl_v=a(n+1) In some cases we might take care of the .not.mask case directly within the unpack construction, using its third (“FIELD=”) argument to provide the not-unpacked values. However, there is no guarantee that the compiler won’t evaluate all components of the “FIELD=” array, if it finds it efficient to do so. Here, since the index of a(n+1) would be out of range, we can’t do it this way. Thus the separate where statement.
FUNCTION bico_s(n,k) USE nrtype USE nr, ONLY : factln IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n,k REAL(SP) :: bico_s as a floating-point number. Returns the binomial coefficient n k bico_s=nint(exp(factln(n)-factln(k)-factln(n-k))) The nearest-integer function cleans up roundoff error for smaller values of n and k. END FUNCTION bico_s
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION factrl_v(n) USE nrtype; USE nrutil, ONLY : arth,assert,cumprod USE nr, ONLY : gammln IMPLICIT NONE INTEGER(I4B), DIMENSION(:), INTENT(IN) :: n REAL(SP), DIMENSION(size(n)) :: factrl_v LOGICAL(LGT), DIMENSION(size(n)) :: mask INTEGER(I4B), SAVE :: ntop=0 INTEGER(I4B), PARAMETER :: NMAX=32 REAL(SP), DIMENSION(NMAX), SAVE :: a call assert(all(n >= 0), ’factrl_v arg’) if (ntop == 0) then ntop=NMAX a(1)=1.0 a(2:NMAX)=cumprod(arth(1.0_sp,1.0_sp,NMAX-1)) end if mask = (n >= NMAX) factrl_v=unpack(exp(gammln(pack(n,mask)+1.0_sp)),mask,0.0_sp) where (.not. mask) factrl_v=a(n+1) END FUNCTION factrl_v
1088
Chapter B6.
Special Functions
FUNCTION factln_s(n) USE nrtype; USE nrutil, ONLY : arth,assert USE nr, ONLY : gammln IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP) :: factln_s Returns ln(n!). INTEGER(I4B), PARAMETER :: TMAX=100 REAL(SP), DIMENSION(TMAX), SAVE :: a LOGICAL(LGT), SAVE :: init=.true. if (init) then Initialize the table. a(1:TMAX)=gammln(arth(1.0_sp,1.0_sp,TMAX)) init=.false. end if call assert(n >= 0, ’factln_s arg’) if (n < TMAX) then In range of the table. factln_s=a(n+1) else Out of range of the table. factln_s=gammln(n+1.0_sp) end if END FUNCTION factln_s
FUNCTION factln_v(n) USE nrtype; USE nrutil, ONLY : arth,assert USE nr, ONLY : gammln IMPLICIT NONE INTEGER(I4B), DIMENSION(:), INTENT(IN) :: n REAL(SP), DIMENSION(size(n)) :: factln_v LOGICAL(LGT), DIMENSION(size(n)) :: mask INTEGER(I4B), PARAMETER :: TMAX=100 REAL(SP), DIMENSION(TMAX), SAVE :: a LOGICAL(LGT), SAVE :: init=.true. if (init) then a(1:TMAX)=gammln(arth(1.0_sp,1.0_sp,TMAX)) init=.false. end if call assert(all(n >= 0), ’factln_v arg’) mask = (n >= TMAX) factln_v=unpack(gammln(pack(n,mask)+1.0_sp),mask,0.0_sp) where (.not. mask) factln_v=a(n+1) END FUNCTION factln_v
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION bico_v(n,k) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : factln IMPLICIT NONE INTEGER(I4B), DIMENSION(:), INTENT(IN) :: n,k REAL(SP), DIMENSION(size(n)) :: bico_v INTEGER(I4B) :: ndum ndum=assert_eq(size(n),size(k),’bico_v’) bico_v=nint(exp(factln(n)-factln(k)-factln(n-k))) END FUNCTION bico_v
Chapter B6.
Special Functions
1089
Another example of the programming convenience of combining a function returning a vector (here, arth) with a special function whose generic name (here, gammln) has an overloaded vector version.
f90
gammln(arth(1.0_sp,1.0_sp,TMAX))
FUNCTION beta_v(z,w) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : gammln IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: z,w REAL(SP), DIMENSION(size(z)) :: beta_v INTEGER(I4B) :: ndum ndum=assert_eq(size(z),size(w),’beta_v’) beta_v=exp(gammln(z)+gammln(w)-gammln(z+w)) END FUNCTION beta_v
FUNCTION gammp_s(a,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : gcf,gser IMPLICIT NONE REAL(SP), INTENT(IN) :: a,x REAL(SP) :: gammp_s Returns the incomplete gamma function P (a, x). call assert( x >= 0.0, a > 0.0, ’gammp_s args’) if (x= 0.0), all(a > 0.0), ’gammp_v args’) mask = (x 0.0, ’gammp_s args’) The generic routine assert in nrutil is overloaded with variants for more than one logical assertion, so you can make more than one assertion about argument ranges. call assert( x >= 0.0,
gammp_v=merge(gser(a,merge(x,0.0_sp,mask)), &
FUNCTION gammq_s(a,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : gcf,gser IMPLICIT NONE REAL(SP), INTENT(IN) :: a,x REAL(SP) :: gammq_s Returns the incomplete gamma function Q(a, x) ≡ 1 − P (a, x). call assert( x >= 0.0, a > 0.0, ’gammq_s args’) if (x= 0.0), all(a > 0.0), ’gammq_v args’) mask = (x ITMAX) call nrerror(’a too large, ITMAX too small in gser_v’) if (present(gln)) then if (size(gln) < size(a)) call & nrerror(’gser: Not enough space for gln’) gln=gammln(a) where (.not. zero) gser_v=summ*exp(-x+a*log(x)-gln) else where (.not. zero) gser_v=summ*exp(-x+a*log(x)-gammln(a))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Returns the incomplete gamma function P (a, x) evaluated by its series representation as gamser. Also optionally returns ln Γ(a) as gln. INTEGER(I4B) :: n REAL(SP) :: ap,del,summ if (x == 0.0) then gser_s=0.0 RETURN end if ap=a summ=1.0_sp/a del=summ do n=1,ITMAX ap=ap+1.0_sp del=del*x/ap summ=summ+del if (abs(del) < abs(summ)*EPS) exit end do if (n > ITMAX) call nrerror(’a too large, ITMAX too small in gser_s’) if (present(gln)) then gln=gammln(a) gser_s=summ*exp(-x+a*log(x)-gln) else gser_s=summ*exp(-x+a*log(x)-gammln(a)) end if END FUNCTION gser_s
1092
Chapter B6.
Special Functions
end if END FUNCTION gser_v
Normally, an OPTIONAL argument will be INTENT(IN) and be used to provide a less-often-used extra input argument to a function. Here, the OPTIONAL argument is INTENT(OUT), used to provide a useful value that is a byproduct of the main calculation. Also note that although x ≥ 0 is required, we omit our usual call assert check for this, because gser is supposed to be called only by gammp or gammq — and these routines supply the argument checking themselves. do n=1,ITMAX...end do...if (n > ITMAX)... This is typical code in Fortran 90 for a loop with a maximum number of iterations, relying on Fortran 90’s guarantee that the index of the do-loop will be available after normal completion of the loop with a predictable value, greater by one than the upper limit of the loop. If the exit statement within the loop is ever taken, the if statement is guaranteed to fail; if the loop goes all the way through ITMAX cycles, the if statement is guaranteed to succeed.
This is the code that provides for very low overhead calculation of zero arguments, as is assumed by the merge-with-dummy-values strategy in gammp and gammq. Zero arguments are “pre-converged” and are never the holdouts in the convergence test. zero=(x == 0.0)...where (zero) gser_v=0.0...converged=zero
FUNCTION gcf_s(a,x,gln) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : gammln IMPLICIT NONE REAL(SP), INTENT(IN) :: a,x REAL(SP), OPTIONAL, INTENT(OUT) :: gln REAL(SP) :: gcf_s INTEGER(I4B), PARAMETER :: ITMAX=100 REAL(SP), PARAMETER :: EPS=epsilon(x),FPMIN=tiny(x)/EPS Returns the incomplete gamma function Q(a, x) evaluated by its continued fraction representation as gammcf. Also optionally returns ln Γ(a) as gln. Parameters: ITMAX is the maximum allowed number of iterations; EPS is the relative accuracy; FPMIN is a number near the smallest representable floating-point number. INTEGER(I4B) :: i REAL(SP) :: an,b,c,d,del,h if (x == 0.0) then gcf_s=1.0 RETURN end if b=x+1.0_sp-a Set up for evaluating continued fraction by modc=1.0_sp/FPMIN ified Lentz’s method (§5.2) with b0 = 0. d=1.0_sp/b h=d do i=1,ITMAX Iterate to convergence. an=-i*(i-a) b=b+2.0_sp d=an*d+b if (abs(d) < FPMIN) d=FPMIN c=b+an/c if (abs(c) < FPMIN) c=FPMIN
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
f90
REAL(SP), OPTIONAL, INTENT(OUT) :: gln
Chapter B6.
Special Functions
FUNCTION gcf_v(a,x,gln) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : gammln IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: a,x REAL(SP), DIMENSION(:), OPTIONAL, INTENT(OUT) :: gln REAL(SP), DIMENSION(size(a)) :: gcf_v INTEGER(I4B), PARAMETER :: ITMAX=100 REAL(SP), PARAMETER :: EPS=epsilon(x),FPMIN=tiny(x)/EPS INTEGER(I4B) :: i REAL(SP), DIMENSION(size(a)) :: an,b,c,d,del,h LOGICAL(LGT), DIMENSION(size(a)) :: converged,zero i=assert_eq(size(a),size(x),’gcf_v’) zero=(x == 0.0) where (zero) gcf_v=1.0 elsewhere b=x+1.0_sp-a c=1.0_sp/FPMIN d=1.0_sp/b h=d end where converged=zero do i=1,ITMAX where (.not. converged) an=-i*(i-a) b=b+2.0_sp d=an*d+b d=merge(FPMIN,d, abs(d)= 0.0, (x > 0.0 .or. n > 1), & ’expint args’) if (n == 0) then Special case. expint=exp(-x)/x RETURN end if nm1=n-1 if (x == 0.0) then Another special case. expint=1.0_sp/nm1 else if (x > 1.0) then Lentz’s algorithm (§5.2). b=x+n c=BIG d=1.0_sp/b h=d do i=1,MAXIT a=-i*(nm1+i) b=b+2.0_sp d=1.0_sp/(a*d+b) Denominators cannot be zero. c=b+a/c del=c*d h=h*del if (abs(del-1.0_sp) MAXIT) call nrerror(’expint: continued fraction failed’) expint=h*exp(-x) else Evaluate series. if (nm1 /= 0) then Set first term. expint=1.0_sp/nm1 else expint=-log(x)-EULER end if fact=1.0 do i=1,MAXIT fact=-fact*x/i if (i /= nm1) then del=-fact/(i-nm1)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B6.
Special Functions
1097
expint does not readily parallelize, and we thus don’t provide a vector version. For syntactic convenience you could make a vector version with a do-loop over calls to this scalar version; or, in Fortran 95, you can of course make the function ELEMENTAL.
FUNCTION ei(x) USE nrtype; USE nrutil, ONLY : assert,nrerror IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: ei INTEGER(I4B), PARAMETER :: MAXIT=100 REAL(SP), PARAMETER :: EPS=epsilon(x),FPMIN=tiny(x)/EPS Computes the exponential integral Ei(x) for x > 0. Parameters: MAXIT is the maximum number of iterations allowed; EPS is the relative error, or absolute error near the zero of Ei at x = 0.3725; FPMIN is a number near the smallest representable floating-point number; EULER (in nrtype) is Euler’s constant γ. INTEGER(I4B) :: k REAL(SP) :: fact,prev,sm,term call assert(x > 0.0, ’ei arg’) if (x < FPMIN) then Special case: avoid failure of convergence test ei=log(x)+EULER because of underflow. else if (x MAXIT) call nrerror(’series failed in ei’) ei=sm+log(x)+EULER else Use asymptotic series. sm=0.0 Start with second term. term=1.0 do k=1,MAXIT prev=term term=term*k/x if (term < EPS) exit Since final sum is greater than one, term itself if (term < prev) then approximates the relative error. sm=sm+term Still converging: add new term. else Diverging: subtract previous term and exit. sm=sm-prev exit end if end do if (k > MAXIT) call nrerror(’asymptotic failed in ei’) ei=exp(x)*(1.0_sp+sm)/x end if END FUNCTION ei
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
else ψ(n) appears here. del=fact*(-log(x)-EULER+sum(1.0_sp/arth(1,1,nm1))) end if expint=expint+del if (abs(del) < abs(expint)*EPS) exit end do if (i > MAXIT) call nrerror(’expint: series failed’) end if END FUNCTION expint
1098
Chapter B6.
Special Functions
ei does not readily parallelize, and we thus don’t provide a vector version. For syntactic convenience you could make a vector version with a do-loop over calls to this scalar version; or, in Fortran 95, you can of course make the function ELEMENTAL.
FUNCTION betai_s(a,b,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : betacf,gammln IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b,x REAL(SP) :: betai_s Returns the incomplete beta function Ix (a, b). REAL(SP) :: bt call assert(x >= 0.0, x = 0.0), all(x 0.0), ’bessy1_v arg’) mask = (abs(x) < 8.0) where (mask) y=x**2 bessy1_v=x*(poly(y,r,mask)/poly(y,s,mask))+& 0.636619772_sp*(bessj1(x)*log(x)-1.0_sp/x) elsewhere z=8.0_sp/x y=z**2 xx=x-2.356194491_sp bessy1_v=sqrt(0.636619772_sp/x)*(sin(xx)*& poly(y,p,.not. mask)+z*cos(xx)*poly(y,q,.not. mask)) end where END FUNCTION bessy1_v
1106
Chapter B6.
Special Functions
byp=j*tox*by-bym bym=by by=byp end do bessy_s=by END FUNCTION bessy_s
Notice that the vector routine is exactly the same as the scalar routine, but operates only on vectors, and that nothing in the routine is specific to any level of precision or kind type of real variable. Cases like this make us wish that Fortran 90 provided for “template” types that could automatically take the type and shape of the actual arguments. (Such facilities are available in other, more object-oriented languages such as C++.)
f90
FUNCTION bessj_s(n,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessj0,bessj1 IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), INTENT(IN) :: x REAL(SP) :: bessj_s INTEGER(I4B), PARAMETER :: IACC=40,IEXP=maxexponent(x)/2 Returns the Bessel function Jn (x) for any real x and n ≥ 2. Make the parameter IACC larger to increase accuracy. INTEGER(I4B) :: j,jsum,m REAL(SP) :: ax,bj,bjm,bjp,summ,tox call assert(n >= 2, ’bessj_s args’) ax=abs(x) if (ax*ax real(n,sp)) then Upwards recurrence from J0 and J1 . tox=2.0_sp/ax bjm=bessj0(ax) bj=bessj1(ax) do j=1,n-1
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION bessy_v(n,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessy0,bessy1 IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: bessy_v INTEGER(I4B) :: j REAL(SP), DIMENSION(size(x)) :: by,bym,byp,tox call assert(n >= 2, all(x > 0.0), ’bessy_v args’) tox=2.0_sp/x by=bessy1(x) bym=bessy0(x) do j=1,n-1 byp=j*tox*by-bym bym=by by=byp end do bessy_v=by END FUNCTION bessy_v
Chapter B6. Special Functions
1107
The bessj routine does not conveniently parallelize with Fortran 90’s language constructions, but Bessel functions are of sufficient importance that we feel the need for a parallel version nevertheless. The basic method adopted below is to encapsulate as contained vector functions two separate algorithms, one for the case x ≤ n, the other for x > n. Both of these have masks as input arguments; within each routine, however, they immediately revert to the pack-unpack method. The choice to pack in the subsidiary routines, rather than in the main routine, is arbitrary; the main routine is supposed to be a little clearer this way. In the Fortran 77 version of this routine, we scaled the variables by 10−10 whenever bj was bigger than 1010. On a machine with a large exponent range, we could improve efficiency by scaling less often. In order to remain portable, however, we used the conservative value of 1010 . An elegant way of handling renormalization is provided by the Fortran 90 intrinsic functions that manipulate real numbers. We test with if (exponent(bj) > IEXP) and then if necessary renormalize with bj=scale(bj,IEXP) and similarly for the other variables. Our conservative choice is to set IEXP=maxexponent(x)/2. Note that an added benefit of scaling this way is that only the exponent of each variable is modified; no roundoff error is introduced as it can be if we do a floating-point division instead.
f90
if (exponent(bj) > IEXP) then...
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
bjp=j*tox*bj-bjm bjm=bj bj=bjp end do bessj_s=bj else Downwards recurrence from an even m tox=2.0_sp/ax here computed. m=2*((n+int(sqrt(real(IACC*n,sp))))/2) bessj_s=0.0 jsum=0 jsum will alternate between 0 and 1; when summ=0.0 it is 1, we accumulate in sum the bjp=0.0 even terms in (5.5.16). bj=1.0 do j=m,1,-1 The downward recurrence. bjm=j*tox*bj-bjp bjp=bj bj=bjm if (exponent(bj) > IEXP) then Renormalize to prevent overflows. bj=scale(bj,-IEXP) bjp=scale(bjp,-IEXP) bessj_s=scale(bessj_s,-IEXP) summ=scale(summ,-IEXP) end if if (jsum /= 0) summ=summ+bj Accumulate the sum. jsum=1-jsum Change 0 to 1 or vice versa. if (j == n) bessj_s=bjp Save the unnormalized answer. end do summ=2.0_sp*summ-bj Compute (5.5.16) bessj_s=bessj_s/summ and use it to normalize the answer. end if if (x < 0.0 .and. mod(n,2) == 1) bessj_s=-bessj_s END FUNCTION bessj_s
1108
Chapter B6.
Special Functions
FUNCTION bessjgt_v(n,xx,mask) IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(:), INTENT(IN) :: xx LOGICAL(LGT), DIMENSION(size(xx)), INTENT(IN) :: mask REAL(SP), DIMENSION(size(xx)) :: bessjgt_v npak=count(mask) if (npak == 0) RETURN allocate(x(npak),bj(npak),bjm(npak),bjp(npak),tox(npak)) x=pack(xx,mask) tox=2.0_sp/x bjm=bessj0(x) bj=bessj1(x) do j=1,n-1 bjp=j*tox*bj-bjm bjm=bj bj=bjp end do bessjgt_v=unpack(bj,mask,0.0_sp) deallocate(x,bj,bjm,bjp,tox) END FUNCTION bessjgt_v FUNCTION bessjle_v(n,xx,mask) IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(:), INTENT(IN) :: xx LOGICAL(LGT), DIMENSION(size(xx)), INTENT(IN) :: mask REAL(SP), DIMENSION(size(xx)) :: bessjle_v npak=count(mask) if (npak == 0) RETURN allocate(x(npak),bj(npak),bjm(npak),bjp(npak),summ(npak), & bessjle(npak),tox(npak),renorm(npak)) x=pack(xx,mask) tox=2.0_sp/x m=2*((n+int(sqrt(real(IACC*n,sp))))/2) bessjle=0.0 jsum=0 summ=0.0 bjp=0.0 bj=1.0 do j=m,1,-1 bjm=j*tox*bj-bjp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION bessj_v(n,xx) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessj0,bessj1 IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(:), INTENT(IN) :: xx REAL(SP), DIMENSION(size(xx)) :: bessj_v INTEGER(I4B), PARAMETER :: IACC=40,IEXP=maxexponent(xx)/2 REAL(SP), DIMENSION(size(xx)) :: ax LOGICAL(LGT), DIMENSION(size(xx)) :: mask,mask0 REAL(SP), DIMENSION(:), ALLOCATABLE :: x,bj,bjm,bjp,summ,tox,bessjle LOGICAL(LGT), DIMENSION(:), ALLOCATABLE :: renorm INTEGER(I4B) :: j,jsum,m,npak call assert(n >= 2, ’bessj_v args’) ax=abs(xx) mask = (ax 0.0, ’bessk0_s arg’) if (x 0.0), ’bessk0_v arg’) mask = (x 0.0), ’bessk1_v arg’) mask = (x 0.0, ’bessk1_s arg’) if (x = 2, all(x > 0.0), ’bessk_v args’) tox=2.0_sp/x bkm=bessk0(x) bk=bessk1(x) do j=1,n-1 bkp=bkm+j*tox*bk bkm=bk bk=bkp end do bessk_v=bk END FUNCTION bessk_v
f90
The scalar and vector versions of bessk are identical, and have no precision-specific constants, another example of where we would like to define a generic “template” function if the language had this facility.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION bessk_s(n,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessk0,bessk1 IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), INTENT(IN) :: x REAL(SP) :: bessk_s Returns the modified Bessel function Kn (x) for positive x and n ≥ 2. INTEGER(I4B) :: j REAL(SP) :: bk,bkm,bkp,tox call assert(n >= 2, x > 0.0, ’bessk_s args’) tox=2.0_sp/x bkm=bessk0(x) Upward recurrence for all x... bk=bessk1(x) do j=1,n-1 ...and here it is. bkp=bkm+j*tox*bk bkm=bk bk=bkp end do bessk_s=bk END FUNCTION bessk_s
1114
Chapter B6.
Special Functions
f90
if (exponent(bi) > IEXP) then
See discussion of scaling for bessj on
p. 1107.
FUNCTION bessi_v(n,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessi0 IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: bessi_v INTEGER(I4B), PARAMETER :: IACC=40,IEXP=maxexponent(x)/2 INTEGER(I4B) :: j,m REAL(SP), DIMENSION(size(x)) :: bi,bim,bip,tox LOGICAL(LGT), DIMENSION(size(x)) :: mask call assert(n >= 2, ’bessi_v args’) bessi_v=0.0 mask = (x IEXP) bessi_v=scale(bessi_v,-IEXP)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION bessi_s(n,x) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessi0 IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), INTENT(IN) :: x REAL(SP) :: bessi_s INTEGER(I4B), PARAMETER :: IACC=40,IEXP=maxexponent(x)/2 Returns the modified Bessel function In (x) for any real x and n ≥ 2. Make the parameter IACC larger to increase accuracy. INTEGER(I4B) :: j,m REAL(SP) :: bi,bim,bip,tox call assert(n >= 2, ’bessi_s args’) bessi_s=0.0 if (x*x IEXP) then Renormalize to prevent overflows. bessi_s=scale(bessi_s,-IEXP) bi=scale(bi,-IEXP) bip=scale(bip,-IEXP) end if if (j == n) bessi_s=bip end do bessi_s=bessi_s*bessi0(x)/bi Normalize with bessi0. if (x < 0.0 .and. mod(n,2) == 1) bessi_s=-bessi_s END FUNCTION bessi_s
Chapter B6. Special Functions
1115
mask = (x == 0.0) tox=2.0_sp/merge(2.0_sp,abs(x),mask)
For the special case x = 0, the value of the returned function should be zero; however, the evaluation of tox will give a divide check. We substitute an innocuous value for the zero cases, then fix up their answers at the end.
SUBROUTINE bessjy_s(x,xnu,rj,ry,rjp,ryp) USE nrtype; USE nrutil, ONLY : assert,nrerror USE nr, ONLY : beschb IMPLICIT NONE REAL(SP), INTENT(IN) :: x,xnu REAL(SP), INTENT(OUT) :: rj,ry,rjp,ryp INTEGER(I4B), PARAMETER :: MAXIT=10000 REAL(DP), PARAMETER :: XMIN=2.0_dp,EPS=1.0e-10_dp,FPMIN=1.0e-30_dp Returns the Bessel functions rj = Jν , ry = Yν and their derivatives rjp = Jν , ryp = Yν , for positive x and for xnu = ν ≥ 0. The relative accuracy is within one or two significant digits of EPS, except near a zero of one of the functions, where EPS controls its absolute accuracy. FPMIN is a number close to the machine’s smallest floating-point number. All internal arithmetic is in double precision. To convert the entire routine to double precision, change the SP declaration above and decrease EPS to 10−16 . Also convert the subroutine beschb. INTEGER(I4B) :: i,isign,l,nl REAL(DP) :: a,b,c,d,del,del1,e,f,fact,fact2,fact3,ff,gam,gam1,gam2,& gammi,gampl,h,p,pimu,pimu2,q,r,rjl,rjl1,rjmu,rjp1,rjpl,rjtemp,& ry1,rymu,rymup,rytemp,sum,sum1,w,x2,xi,xi2,xmu,xmu2 COMPLEX(DPC) :: aa,bb,cc,dd,dl,pq call assert(x > 0.0, xnu >= 0.0, ’bessjy args’) nl=merge(int(xnu+0.5_dp), max(0,int(xnu-x+1.5_dp)), x < XMIN) nl is the number of downward recurrences of the J’s and upward recurrences of Y ’s. xmu lies between −1/2 and 1/2 for x < XMIN, while it is chosen so that x is greater than the turning point for x ≥ XMIN. xmu=xnu-nl xmu2=xmu*xmu xi=1.0_dp/x xi2=2.0_dp*xi w=xi2/PI_D The Wronskian. isign=1 Evaluate CF1 by modified Lentz’s method h=xnu*xi (§5.2). isign keeps track of sign changes if (h < FPMIN) h=FPMIN in the denominator. b=xi2*xnu d=0.0 c=h do i=1,MAXIT b=b+xi2 d=b-d if (abs(d) < FPMIN) d=FPMIN c=b-1.0_dp/c
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
bi=scale(bi,-IEXP) bip=scale(bip,-IEXP) end where if (j == n) bessi_v=bip end do bessi_v=bessi_v*bessi0(x)/bi where (mask) bessi_v=0.0_sp where (x < 0.0 .and. mod(n,2) == 1) bessi_v=-bessi_v END FUNCTION bessi_v
1116
Chapter B6.
Special Functions
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (abs(c) < FPMIN) c=FPMIN d=1.0_dp/d del=c*d h=del*h if (d < 0.0) isign=-isign if (abs(del-1.0_dp) < EPS) exit end do if (i > MAXIT) call nrerror(’x too large in bessjy; try asymptotic expansion’) rjl=isign*FPMIN Initialize Jν and Jν for downward recurrence. rjpl=h*rjl rjl1=rjl Store values for later rescaling. rjp1=rjpl fact=xnu*xi do l=nl,1,-1 rjtemp=fact*rjl+rjpl fact=fact-xi rjpl=fact*rjtemp-rjl rjl=rjtemp end do if (rjl == 0.0) rjl=EPS f=rjpl/rjl Now have unnormalized Jµ and Jµ . if (x < XMIN) then Use series. x2=0.5_dp*x pimu=PI_D*xmu if (abs(pimu) < EPS) then fact=1.0 else fact=pimu/sin(pimu) end if d=-log(x2) e=xmu*d if (abs(e) < EPS) then fact2=1.0 else fact2=sinh(e)/e end if call beschb(xmu,gam1,gam2,gampl,gammi) Chebyshev evaluation of Γ1 and Γ2 . ff=2.0_dp/PI_D*fact*(gam1*cosh(e)+gam2*fact2*d) f0 . e=exp(e) p=e/(gampl*PI_D) p0 . q=1.0_dp/(e*PI_D*gammi) q0 . pimu2=0.5_dp*pimu if (abs(pimu2) < EPS) then fact3=1.0 else fact3=sin(pimu2)/pimu2 end if r=PI_D*pimu2*fact3*fact3 c=1.0 d=-x2*x2 sum=ff+r*q sum1=p do i=1,MAXIT ff=(i*ff+p+q)/(i*i-xmu2) c=c*d/i p=p/(i-xmu) q=q/(i+xmu) del=c*(ff+r*q) sum=sum+del del1=c*p-i*del sum1=sum1+del1 if (abs(del) < (1.0_dp+abs(sum))*EPS) exit end do if (i > MAXIT) call nrerror(’bessy series failed to converge’)
Chapter B6.
Special Functions
1117
FUNCTION absc(z) IMPLICIT NONE COMPLEX(DPC), INTENT(IN) :: z REAL(DP) :: absc absc=abs(real(z))+abs(aimag(z)) END FUNCTION absc END SUBROUTINE bessjy_s
Yes there is a vector version bessjy v. Its general scheme is to have a bunch of contained functions for various cases, and then combine their outputs (somewhat like bessj v, above, but much more complicated). A listing runs to about four printed pages, and we judge it to be of not much interest, so we will not include it here. (It is included on the machine-readable media.)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
rymu=-sum ry1=-sum1*xi2 rymup=xmu*xi*rymu-ry1 rjmu=w/(rymup-f*rymu) Equation (6.7.13). else Evaluate CF2 by modified Lentz’s method a=0.25_dp-xmu2 (§5.2). pq=cmplx(-0.5_dp*xi,1.0_dp,kind=dpc) aa=cmplx(0.0_dp,xi*a,kind=dpc) bb=cmplx(2.0_dp*x,2.0_dp,kind=dpc) cc=bb+aa/pq dd=1.0_dp/bb pq=cc*dd*pq do i=2,MAXIT a=a+2*(i-1) bb=bb+cmplx(0.0_dp,2.0_dp,kind=dpc) dd=a*dd+bb if (absc(dd) < FPMIN) dd=FPMIN cc=bb+a/cc if (absc(cc) < FPMIN) cc=FPMIN dd=1.0_dp/dd dl=cc*dd pq=pq*dl if (absc(dl-1.0_dp) < EPS) exit end do if (i > MAXIT) call nrerror(’cf2 failed in bessjy’) p=real(pq) q=aimag(pq) gam=(p-f)/q Equations (6.7.6) – (6.7.10). rjmu=sqrt(w/((p-f)*gam+q)) rjmu=sign(rjmu,rjl) rymu=rjmu*gam rymup=rymu*(p+q/gam) ry1=xmu*xi*rymu-rymup end if fact=rjmu/rjl rj=rjl1*fact Scale original Jν and Jν . rjp=rjp1*fact do i=1,nl Upward recurrence of Yν . rytemp=(xmu+i)*xi2*ry1-rymu rymu=ry1 ry1=rytemp end do ry=rymu ryp=xnu*xi*rymu-ry1 CONTAINS
1118
Chapter B6.
Special Functions
SUBROUTINE beschb_v(x,gam1,gam2,gampl,gammi) USE nrtype USE nr, ONLY : chebev IMPLICIT NONE REAL(DP), DIMENSION(:), INTENT(IN) :: x REAL(DP), DIMENSION(:), INTENT(OUT) :: gam1,gam2,gampl,gammi INTEGER(I4B), PARAMETER :: NUSE1=5,NUSE2=5 REAL(SP), DIMENSION(size(x)) :: xx REAL(SP), DIMENSION(7) :: c1=(/-1.142022680371168_sp,& 6.5165112670737e-3_sp,3.087090173086e-4_sp,-3.4706269649e-6_sp,& 6.9437664e-9_sp,3.67795e-11_sp,-1.356e-13_sp/) REAL(SP), DIMENSION(8) :: c2=(/1.843740587300905_sp,& -7.68528408447867e-2_sp,1.2719271366546e-3_sp,& -4.9717367042e-6_sp, -3.31261198e-8_sp,2.423096e-10_sp,& -1.702e-13_sp,-1.49e-15_sp/) xx=8.0_dp*x*x-1.0_dp gam1=chebev(-1.0_sp,1.0_sp,c1(1:NUSE1),xx) gam2=chebev(-1.0_sp,1.0_sp,c2(1:NUSE2),xx) gampl=gam2-x*gam1 gammi=gam2+x*gam1 END SUBROUTINE beschb_v
SUBROUTINE bessik(x,xnu,ri,rk,rip,rkp) USE nrtype; USE nrutil, ONLY : assert,nrerror USE nr, ONLY : beschb IMPLICIT NONE REAL(SP), INTENT(IN) :: x,xnu REAL(SP), INTENT(OUT) :: ri,rk,rip,rkp INTEGER(I4B), PARAMETER :: MAXIT=10000 REAL(SP), PARAMETER :: XMIN=2.0 REAL(DP), PARAMETER :: EPS=1.0e-10_dp,FPMIN=1.0e-30_dp Returns the modified Bessel functions ri = Iν , rk = Kν and their derivatives rip = Iν , rkp = Kν , for positive x and for xnu = ν ≥ 0. The relative accuracy is within one or
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE beschb_s(x,gam1,gam2,gampl,gammi) USE nrtype USE nr, ONLY : chebev IMPLICIT NONE REAL(DP), INTENT(IN) :: x REAL(DP), INTENT(OUT) :: gam1,gam2,gampl,gammi INTEGER(I4B), PARAMETER :: NUSE1=5,NUSE2=5 Evaluates Γ1 and Γ2 by Chebyshev expansion for |x| ≤ 1/2. Also returns 1/Γ(1 + x) and 1/Γ(1 − x). If converting to double precision, set NUSE1 = 7, NUSE2 = 8. REAL(SP) :: xx REAL(SP), DIMENSION(7) :: c1=(/-1.142022680371168_sp,& 6.5165112670737e-3_sp,3.087090173086e-4_sp,-3.4706269649e-6_sp,& 6.9437664e-9_sp,3.67795e-11_sp,-1.356e-13_sp/) REAL(SP), DIMENSION(8) :: c2=(/1.843740587300905_sp,& -7.68528408447867e-2_sp,1.2719271366546e-3_sp,& -4.9717367042e-6_sp, -3.31261198e-8_sp,2.423096e-10_sp,& -1.702e-13_sp,-1.49e-15_sp/) xx=8.0_dp*x*x-1.0_dp Multiply x by 2 to make range be −1 to 1, and then apply gam1=chebev(-1.0_sp,1.0_sp,c1(1:NUSE1),xx) transformation for evaluating even Chebygam2=chebev(-1.0_sp,1.0_sp,c2(1:NUSE2),xx) shev series. gampl=gam2-x*gam1 gammi=gam2+x*gam1 END SUBROUTINE beschb_s
Chapter B6. Special Functions
1119
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
two significant digits of EPS. FPMIN is a number close to the machine’s smallest floatingpoint number. All internal arithmetic is in double precision. To convert the entire routine to double precision, change the REAL declaration above and decrease EPS to 10−16 . Also convert the subroutine beschb. INTEGER(I4B) :: i,l,nl REAL(DP) :: a,a1,b,c,d,del,del1,delh,dels,e,f,fact,fact2,ff,& gam1,gam2,gammi,gampl,h,p,pimu,q,q1,q2,qnew,& ril,ril1,rimu,rip1,ripl,ritemp,rk1,rkmu,rkmup,rktemp,& s,sum,sum1,x2,xi,xi2,xmu,xmu2 call assert(x > 0.0, xnu >= 0.0, ’bessik args’) nl=int(xnu+0.5_dp) nl is the number of downward recurrences of the I’s and upward recurrences xmu=xnu-nl of K’s. xmu lies between −1/2 and xmu2=xmu*xmu 1/2. xi=1.0_dp/x xi2=2.0_dp*xi h=xnu*xi Evaluate CF1 by modified Lentz’s method if (h < FPMIN) h=FPMIN (§5.2). b=xi2*xnu d=0.0 c=h do i=1,MAXIT b=b+xi2 d=1.0_dp/(b+d) Denominators cannot be zero here, so no c=b+1.0_dp/c need for special precautions. del=c*d h=del*h if (abs(del-1.0_dp) < EPS) exit end do if (i > MAXIT) call nrerror(’x too large in bessik; try asymptotic expansion’) ril=FPMIN Initialize Iν and Iν for downward recurripl=h*ril rence. ril1=ril Store values for later rescaling. rip1=ripl fact=xnu*xi do l=nl,1,-1 ritemp=fact*ril+ripl fact=fact-xi ripl=fact*ritemp+ril ril=ritemp end do f=ripl/ril Now have unnormalized Iµ and Iµ . if (x < XMIN) then Use series. x2=0.5_dp*x pimu=PI_D*xmu if (abs(pimu) < EPS) then fact=1.0 else fact=pimu/sin(pimu) end if d=-log(x2) e=xmu*d if (abs(e) < EPS) then fact2=1.0 else fact2=sinh(e)/e end if call beschb(xmu,gam1,gam2,gampl,gammi) Chebyshev evaluation of Γ1 and Γ2 . ff=fact*(gam1*cosh(e)+gam2*fact2*d) f0 . sum=ff e=exp(e) p=0.5_dp*e/gampl p0 . q=0.5_dp/(e*gammi) q0 . c=1.0 d=x2*x2
1120
Chapter B6.
Special Functions
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
sum1=p do i=1,MAXIT ff=(i*ff+p+q)/(i*i-xmu2) c=c*d/i p=p/(i-xmu) q=q/(i+xmu) del=c*ff sum=sum+del del1=c*(p-i*ff) sum1=sum1+del1 if (abs(del) < abs(sum)*EPS) exit end do if (i > MAXIT) call nrerror(’bessk series failed to converge’) rkmu=sum rk1=sum1*xi2 else Evaluate CF2 by Steed’s algorithm (§5.2), b=2.0_dp*(1.0_dp+x) which is OK because there can be no d=1.0_dp/b zero denominators. delh=d h=delh q1=0.0 Initializations for recurrence (6.7.35). q2=1.0 a1=0.25_dp-xmu2 c=a1 q=c First term in equation (6.7.34). a=-a1 s=1.0_dp+q*delh do i=2,MAXIT a=a-2*(i-1) c=-a*c/i qnew=(q1-b*q2)/a q1=q2 q2=qnew q=q+c*qnew b=b+2.0_dp d=1.0_dp/(b+a*d) delh=(b*d-1.0_dp)*delh h=h+delh dels=q*delh s=s+dels if (abs(dels/s) < EPS) exit Need only test convergence of sum, since end do CF2 itself converges more quickly. if (i > MAXIT) call nrerror(’bessik: failure to converge in cf2’) h=a1*h rkmu=sqrt(PI_D/(2.0_dp*x))*exp(-x)/s Omit the factor exp(−x) to scale all the rk1=rkmu*(xmu+x+0.5_dp-h)*xi returned functions by exp(x) for x ≥ end if XMIN. rkmup=xmu*xi*rkmu-rk1 rimu=xi/(f*rkmu-rkmup) Get Iµ from Wronskian. ri=(rimu*ril1)/ril Scale original Iν and Iν . rip=(rimu*rip1)/ril do i=1,nl Upward recurrence of Kν . rktemp=(xmu+i)*xi2*rk1+rkmu rkmu=rk1 rk1=rktemp end do rk=rkmu rkp=xnu*xi*rkmu-rk1 END SUBROUTINE bessik
Chapter B6.
Special Functions
1121
bessik does not readily parallelize, and we thus don’t provide a vector version. Since airy, immediately following, requires bessik, we don’t have a vector version of it, either.
SUBROUTINE sphbes_s(n,x,sj,sy,sjp,syp) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessjy IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), INTENT(IN) :: x REAL(SP), INTENT(OUT) :: sj,sy,sjp,syp (x), y (x) for Returns spherical Bessel functions jn (x), yn (x), and their derivatives jn n integer n ≥ 0 and x > 0. REAL(SP), PARAMETER :: RTPIO2=1.253314137315500_sp REAL(SP) :: factor,order,rj,rjp,ry,ryp call assert(n >= 0, x > 0.0, ’sphbes_s args’) order=n+0.5_sp call bessjy(x,order,rj,ry,rjp,ryp) factor=RTPIO2/sqrt(x) sj=factor*rj sy=factor*ry
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE airy(x,ai,bi,aip,bip) USE nrtype USE nr, ONLY : bessik,bessjy IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), INTENT(OUT) :: ai,bi,aip,bip Returns Airy functions Ai(x), Bi(x), and their derivatives Ai (x), Bi (x). REAL(SP) :: absx,ri,rip,rj,rjp,rk,rkp,rootx,ry,ryp,z REAL(SP), PARAMETER :: THIRD=1.0_sp/3.0_sp,TWOTHR=2.0_sp/3.0_sp, & ONOVRT=0.5773502691896258_sp absx=abs(x) rootx=sqrt(absx) z=TWOTHR*absx*rootx if (x > 0.0) then call bessik(z,THIRD,ri,rk,rip,rkp) ai=rootx*ONOVRT*rk/PI bi=rootx*(rk/PI+2.0_sp*ONOVRT*ri) call bessik(z,TWOTHR,ri,rk,rip,rkp) aip=-x*ONOVRT*rk/PI bip=x*(rk/PI+2.0_sp*ONOVRT*ri) else if (x < 0.0) then call bessjy(z,THIRD,rj,ry,rjp,ryp) ai=0.5_sp*rootx*(rj-ONOVRT*ry) bi=-0.5_sp*rootx*(ry+ONOVRT*rj) call bessjy(z,TWOTHR,rj,ry,rjp,ryp) aip=0.5_sp*absx*(ONOVRT*ry+rj) bip=0.5_sp*absx*(ONOVRT*rj-ry) else Case x = 0. ai=0.3550280538878172_sp bi=ai/ONOVRT aip=-0.2588194037928068_sp bip=-aip/ONOVRT end if END SUBROUTINE airy
1122
Chapter B6.
Special Functions
sjp=factor*rjp-sj/(2.0_sp*x) syp=factor*ryp-sy/(2.0_sp*x) END SUBROUTINE sphbes_s
Note that sphbes v uses (through overloading) bessjy v. The listing of that routine was omitted above, but it is on the machine-readable media.
FUNCTION plgndr_s(l,m,x) USE nrtype; USE nrutil, ONLY : arth,assert IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: l,m REAL(SP), INTENT(IN) :: x REAL(SP) :: plgndr_s Computes the associated Legendre polynomial Plm (x). Here m and l are integers satisfying 0 ≤ m ≤ l, while x lies in the range −1 ≤ x ≤ 1. INTEGER(I4B) :: ll REAL(SP) :: pll,pmm,pmmp1,somx2 call assert(m >= 0, m m + 1. do ll=m+2,l pll=(x*(2*ll-1)*pmmp1-(ll+m-1)*pmm)/(ll-m) pmm=pmmp1 pmmp1=pll end do plgndr_s=pll
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE sphbes_v(n,x,sj,sy,sjp,syp) USE nrtype; USE nrutil, ONLY : assert USE nr, ONLY : bessjy IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(OUT) :: sj,sy,sjp,syp REAL(SP), PARAMETER :: RTPIO2=1.253314137315500_sp REAL(SP) :: order REAL(SP), DIMENSION(size(x)) :: factor,rj,rjp,ry,ryp call assert(n >= 0, all(x > 0.0), ’sphbes_v args’) order=n+0.5_sp call bessjy(x,order,rj,ry,rjp,ryp) factor=RTPIO2/sqrt(x) sj=factor*rj sy=factor*ry sjp=factor*rjp-sj/(2.0_sp*x) syp=factor*ryp-sy/(2.0_sp*x) END SUBROUTINE sphbes_v
Chapter B6. Special Functions
1123
end if end if END FUNCTION plgndr_s
That is, (2m − 1)!!
FUNCTION plgndr_v(l,m,x) USE nrtype; USE nrutil, ONLY : arth,assert IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: l,m REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: plgndr_v INTEGER(I4B) :: ll REAL(SP), DIMENSION(size(x)) :: pll,pmm,pmmp1,somx2 call assert(m >= 0, m TMIN) then method (§5.2). b=cmplx(1.0_sp,t,kind=spc)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION absc(z) IMPLICIT NONE COMPLEX(SPC), INTENT(IN) :: z REAL(SP) :: absc absc=abs(real(z))+abs(aimag(z)) END FUNCTION absc END SUBROUTINE frenel
1126
Chapter B6.
Special Functions
FUNCTION absc(z) IMPLICIT NONE COMPLEX(SPC), INTENT(IN) :: z REAL(SP) :: absc absc=abs(real(z))+abs(aimag(z)) END FUNCTION absc END SUBROUTINE cisi
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
c=BIG d=1.0_sp/b h=d do i=2,MAXIT a=-(i-1)**2 b=b+2.0_sp d=1.0_sp/(a*d+b) Denominators cannot be zero. c=b+a/c del=c*d h=h*del if (absc(del-1.0_sp) MAXIT) call nrerror(’continued fraction failed in cisi’) h=cmplx(cos(t),-sin(t),kind=spc)*h ci=-real(h) si=PIO2+aimag(h) else Evaluate both series simultaneously. if (t < sqrt(FPMIN)) then Special case: avoid failure of convergence test sumc=0.0 because of underflow. sums=t else sum=0.0 sums=0.0 sumc=0.0 sign=1.0 fact=1.0 odd=.true. do k=1,MAXIT fact=fact*t/k term=fact/k sum=sum+sign*term err=term/abs(sum) if (odd) then sign=-sign sums=sum sum=sumc else sumc=sum sum=sums end if if (err < EPS) exit odd=.not. odd end do if (k > MAXIT) call nrerror(’MAXIT exceeded in cisi’) end if si=sums ci=sumc+log(t)+EULER end if if (x < 0.0) si=-si CONTAINS
Chapter B6. Special Functions
1127
This is one way to give initial values to an array. Actually, we’re somewhat nervous about using the “implied do-loop” form of the array constructor, as above, because our parallel compilers might not always be smart enough to execute the constructor in parallel. In this case, with NMAX=6, the damage potential is quite minimal. An alternative way to initialize the array would be with a data statement, “DATA c /NMAX*0.0_sp/”; however, this is not considered good Fortran 90 style, and there is no reason to think that it would be faster.
f90
REAL(SP), DIMENSION(NMAX), SAVE :: c=(/ (0.0_sp,i=1,NMAX) /)
c(1:NMAX)=exp(-(arth(1,2,NMAX)*H)**2) Another example where the arth function of nrutil comes in handy. Otherwise, this would be do i=1,NMAX c(i)=exp(-((2.0_sp*i-1.0_sp)*H)**2) end do arth(n0+1,2,NMAX)...arth(n0-1,-2,NMAX)...geop(ec,ec**2,NMAX) These are not just notationally convenient for generating the sequences (n 0 +1, n0 +3, n0 +5, . . .), (n0 − 1, n0 − 3, n0 − 5, . . .), and (ec, ec3 , ec5 , . . .). They also may allow parallelization with parallel versions of arth and geop, such as those in nrutil. FUNCTION dawson_v(x) USE nrtype; USE nrutil, ONLY : arth IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: dawson_v INTEGER(I4B), PARAMETER :: NMAX=6
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION dawson_s(x) USE nrtype; USE nrutil, ONLY : arth,geop IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: dawson_s Returns Dawson’s integral F (x) = exp(−x2 ) 0x exp(t2 )dt for any real x. INTEGER(I4B), PARAMETER :: NMAX=6 REAL(SP), PARAMETER :: H=0.4_sp,A1=2.0_sp/3.0_sp,A2=0.4_sp,& A3=2.0_sp/7.0_sp INTEGER(I4B) :: i,n0 REAL(SP) :: ec,x2,xp,xx REAL(SP), DIMENSION(NMAX) :: d1,d2,e1 REAL(SP), DIMENSION(NMAX), SAVE :: c=(/ (0.0_sp,i=1,NMAX) /) if (c(1) == 0.0) c(1:NMAX)=exp(-(arth(1,2,NMAX)*H)**2) Initialize c on first call. if (abs(x) < 0.2_sp) then Use series expansion. x2=x**2 dawson_s=x*(1.0_sp-A1*x2*(1.0_sp-A2*x2*(1.0_sp-A3*x2))) else Use sampling theorem representation. xx=abs(x) n0=2*nint(0.5_sp*xx/H) xp=xx-real(n0,sp)*H ec=exp(2.0_sp*xp*H) d1=arth(n0+1,2,NMAX) d2=arth(n0-1,-2,NMAX) e1=geop(ec,ec**2,NMAX) √ dawson_s=0.5641895835477563_sp*sign(exp(-xp**2),x)*& Constant is 1/ π. sum(c*(e1/d1+1.0_sp/(d2*e1))) end if END FUNCTION dawson_s
1128
Chapter B6.
Special Functions
FUNCTION dawsonseries_v(xin,mask) IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: xin LOGICAL(LGT), DIMENSION(size(xin)), INTENT(IN) :: mask REAL(SP), DIMENSION(size(xin)) :: dawsonseries_v INTEGER(I4B), DIMENSION(:), ALLOCATABLE :: n0 REAL(SP), DIMENSION(:), ALLOCATABLE :: d1,d2,e1,e2,sm,xp,xx,x n=count(mask) if (n == 0) RETURN allocate(n0(n),d1(n),d2(n),e1(n),e2(n),sm(n),xp(n),xx(n),x(n)) x=pack(xin,mask) xx=abs(x) n0=2*nint(0.5_sp*xx/H) xp=xx-real(n0,sp)*H e1=exp(2.0_sp*xp*H) e2=e1**2 d1=n0+1.0_sp d2=d1-2.0_sp sm=0.0 do i=1,NMAX sm=sm+c(i)*(e1/d1+1.0_sp/(d2*e1)) d1=d1+2.0_sp d2=d2-2.0_sp e1=e2*e1 end do sm=0.5641895835477563_sp*sign(exp(-xp**2),x)*sm dawsonseries_v=unpack(sm,mask,0.0_sp) deallocate(n0,d1,d2,e1,e2,sm,xp,xx) END FUNCTION dawsonseries_v END FUNCTION dawson_v
Pass-the-buck method for getting masked values, see note to bessj0 v above, p. 1102. Within the contained dawsonseries, we use the pack-unpack method. Note that, unlike in dawson s, the sums are done by do-loops, because the parallelization is already over the components of the vector argument. dawson_v=dawsonseries_v(x,mask)
FUNCTION rf_s(x,y,z) USE nrtype; USE nrutil, ONLY : assert IMPLICIT NONE REAL(SP), INTENT(IN) :: x,y,z REAL(SP) :: rf_s REAL(SP), PARAMETER :: ERRTOL=0.08_sp,TINY=1.5e-38_sp,BIG=3.0e37_sp,& THIRD=1.0_sp/3.0_sp,&
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
REAL(SP), PARAMETER :: H=0.4_sp,A1=2.0_sp/3.0_sp,A2=0.4_sp,& A3=2.0_sp/7.0_sp INTEGER(I4B) :: i,n REAL(SP), DIMENSION(size(x)) :: x2 REAL(SP), DIMENSION(NMAX), SAVE :: c=(/ (0.0_sp,i=1,NMAX) /) LOGICAL(LGT), DIMENSION(size(x)) :: mask if (c(1) == 0.0) c(1:NMAX)=exp(-(arth(1,2,NMAX)*H)**2) mask = (abs(x) >= 0.2_sp) dawson_v=dawsonseries_v(x,mask) where (.not. mask) x2=x**2 dawson_v=x*(1.0_sp-A1*x2*(1.0_sp-A2*x2*(1.0_sp-A3*x2))) end where CONTAINS
Chapter B6. Special Functions
1129
FUNCTION rf_v(x,y,z) USE nrtype; USE nrutil, ONLY : assert,assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y,z REAL(SP), DIMENSION(size(x)) :: rf_v REAL(SP), PARAMETER :: ERRTOL=0.08_sp,TINY=1.5e-38_sp,BIG=3.0e37_sp,& THIRD=1.0_sp/3.0_sp,& C1=1.0_sp/24.0_sp,C2=0.1_sp,C3=3.0_sp/44.0_sp,C4=1.0_sp/14.0_sp REAL(SP), DIMENSION(size(x)) :: alamb,ave,delx,dely,delz,e2,e3,& sqrtx,sqrty,sqrtz,xt,yt,zt LOGICAL(LGT), DIMENSION(size(x)) :: converged INTEGER(I4B) :: ndum ndum=assert_eq(size(x),size(y),size(z),’rf_v’) call assert(all(min(x,y,z) >= 0.0), all(min(x+y,x+z,y+z) >= TINY), & all(max(x,y,z) = TINY, & max(x,y,z) = TINY, max(x,y,z) = TINY), & all(max(x,y,z) = TINY, & max(x,y,z,abs(p)) 0.0) then xt=x yt=y zt=z pt=p else xt=min(x,y,z) zt=max(x,y,z) yt=x+y+z-xt-zt a=1.0_sp/(yt-p) b=a*(zt-yt)*(yt-xt) pt=yt+b rho=xt*zt/yt tau=p*pt/yt end if do sqrtx=sqrt(xt) sqrty=sqrt(yt) sqrtz=sqrt(zt) alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz alpha=(pt*(sqrtx+sqrty+sqrtz)+sqrtx*sqrty*sqrtz)**2 bet=pt*(pt+alamb)**2 sm=sm+fac*rc(alpha,bet) fac=0.25_sp*fac xt=0.25_sp*(xt+alamb) yt=0.25_sp*(yt+alamb) zt=0.25_sp*(zt+alamb) pt=0.25_sp*(pt+alamb) ave=0.2_sp*(xt+yt+zt+pt+pt) delx=(ave-xt)/ave dely=(ave-yt)/ave delz=(ave-zt)/ave delp=(ave-pt)/ave if (max(abs(delx),abs(dely),abs(delz),abs(delp)) = TINY), & all(max(x,y,z,abs(p)) 0.0) xt=x yt=y zt=z pt=p elsewhere xt=min(x,y,z) zt=max(x,y,z) yt=x+y+z-xt-zt a=1.0_sp/(yt-p) b=a*(zt-yt)*(yt-xt) pt=yt+b rho=xt*zt/yt tau=p*pt/yt end where mask=.false. do where (.not. mask) sqrtx=sqrt(xt) sqrty=sqrt(yt) sqrtz=sqrt(zt) alamb=sqrtx*(sqrty+sqrtz)+sqrty*sqrtz alpha=(pt*(sqrtx+sqrty+sqrtz)+sqrtx*sqrty*sqrtz)**2 bet=pt*(pt+alamb)**2 sm=sm+fac*rc(alpha,bet) fac=0.25_sp*fac xt=0.25_sp*(xt+alamb) yt=0.25_sp*(yt+alamb) zt=0.25_sp*(zt+alamb) pt=0.25_sp*(pt+alamb) ave=0.2_sp*(xt+yt+zt+pt+pt) delx=(ave-xt)/ave dely=(ave-yt)/ave delz=(ave-zt)/ave delp=(ave-pt)/ave mask = (max(abs(delx),abs(dely),abs(delz),abs(delp)) = TINY,x+abs(y) = -COMP1 .or. x = COMP2/),’rc_s’) if (y > 0.0) then xt=x yt=y w=1.0 else xt=x-y yt=-y w=sqrt(x)/sqrt(xt) end if do alamb=2.0_sp*sqrt(xt)*sqrt(yt)+yt xt=0.25_sp*(xt+alamb) yt=0.25_sp*(yt+alamb) ave=THIRD*(xt+yt+yt) s=(yt-ave)/ave if (abs(s) = 0.0),all(y /= 0.0),all(x+abs(y) >= TINY), & all(x+abs(y) = -COMP1 .or. x = COMP2) /),’rc_v’) where (y > 0.0) xt=x yt=y w=1.0 elsewhere xt=x-y yt=-y w=sqrt(x)/sqrt(xt) end where converged=.false. do where (.not. converged) alamb=2.0_sp*sqrt(xt)*sqrt(yt)+yt xt=0.25_sp*(xt+alamb) yt=0.25_sp*(yt+alamb) ave=THIRD*(xt+yt+yt) s=(yt-ave)/ave converged = (abs(s) 1. Parameter: EPS is an accuracy parameter. COMPLEX(SPC), DIMENSION(2) :: y REAL(SP), DIMENSION(4) :: ry if (real(z)**2+aimag(z)**2 = 0) call nrerror(’ran_init: arith assump 6 fails’) if (not(-1_k4b) < 0) call nrerror(’ran_init: arith assump 7 fails’) if (not(0_k4b) >= 0) call nrerror(’ran_init: arith assump 8 fails’) if (not(1_k4b) >= 0) call nrerror(’ran_init: arith assump 9 fails’) if (lenran > 0) then Reallocate space, or ... ranseeds=>reallocate(ranseeds,length,5) ranv=>reallocate(ranv,length-1) new=lenran+1 else allocate space. allocate(ranseeds(length,5)) allocate(ranv(length-1)) new=1 Index of first location not yet initialized. amm=nearest(1.0_sp,-1.0_sp)/hgng Use of nearest is to ensure that returned random deviates are strictly less than 1.0. if (amm*hgng >= 1.0 .or. amm*hgng 1) then Point to vector seeds. iran => ranseeds(2:,1) jran => ranseeds(2:,2) kran => ranseeds(2:,3) mran => ranseeds(2:,4) nran => ranseeds(2:,5) ranv = nran
1146
Chapter B7.
Random Numbers
end if lenran=length END SUBROUTINE ran_init
Bit of dirty laundry here! We are testing whether the most positive integer hg wraps around to the most negative integer hgng when 1 is added to it. We can’t just write hg+1, since some compilers will evaluate this at compile time and return an overflow error message. If your compiler sees through the charade of the temporary variable hgt, you’ll have to find another way to trick it. amm=nearest(1.0_sp,-1.0_sp)/hgng... Logically, amm should be a parameter; but the nearest intrinsic is trouble-prone in the initialization expression for a parameter (named constant), so we compute this at run time. We then check that amm, when multiplied by the largest possible negative integer, does not equal or exceed unity. (Our random deviates are guaranteed never to equal zero or unity exactly.) You might wonder why amm is negative, and why we multiply it by negative integers to get positive random deviates. The answer, which will become manifest in the random generators given below, is that we want to use the fast not operation on integers to convert them to nonzero values of all one sign. This is possible if the conversion is to negative values, since not(i) is negative for all nonnegative i. If the conversion were to positive values, we would have problems both with zero (its sign bit is already positive) and hgng (since not(hgng) is generally zero). iran0=ranseeds(1,1) ... iran => ranseeds(2:,1)...
The initial state information is stored in ranseeds, a two-dimensional array whose column (second) index ranges from 1 to 5 over the state variables. ranseeds(1,:) is reserved for scalar random generators, while ranseeds(2:,:) is for vector-parallel generators. The ranseeds array is made available to vector generators through the pointers iran, jran, kran, mran, and nran. The corresponding scalar values, iran0,. . ., nran0 are simply global variables, not pointers, because the overhead of addressing a scalar through a pointer is often too great. (We will have to copy these scalar values back into ranseeds when it, rarely, needs to be addressed as an array.) call ran_hash(...) Unique, and random, initial state information is obtained by putting a user-settable “sequence number” into iran, a component number into jran, and hashing this pair. Then jran and kran are hashed, kran and mran are hashed, and so forth. SUBROUTINE ran_deallocate User interface to release the workspace used by the random number routines. if (lenran > 0) then deallocate(ranseeds,ranv) nullify(ranseeds,ranv,iran,jran,kran,mran,nran) lenran = 0 end if END SUBROUTINE ran_deallocate
The above routine is supplied as a user interface for deallocating all the state space storage.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
f90
hgt=hg ... if (hgt+1 /= hgng)
Chapter B7.
Random Numbers
1147
f90
ranseeds=reshape(put,shape(ranseeds)) ... get=reshape(ranseeds,shape(get))
Fortran 90’s convention is that random state space is a one-dimensional array, so we map to this on both the get and put keywords. iran0=...jran0=...kran0=... ranseeds(1,1:5)=(/ iran0,jran0,kran0,mran0,nran0 /)
It’s much more convenient to set a vector from a bunch of scalars then the other way around. SUBROUTINE ran_hash_s(il,ir) IMPLICIT NONE INTEGER(K4B), INTENT(INOUT) :: il,ir DES-like hashing of two 32-bit integers, using shifts, xor’s, and adds to make the internal nonlinear function. INTEGER(K4B) :: is,j do j=1,4 is=ir ir=ieor(ir,ishft(ir,5))+1422217823 The various constants are chosen to give ir=ieor(ir,ishft(ir,-16))+1842055030 good bit mixing and should not be ir=ieor(ir,ishft(ir,9))+80567781 changed. ir=ieor(il,ir) il=is end do END SUBROUTINE ran_hash_s
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE ran_seed(sequence,size,put,get) IMPLICIT NONE INTEGER, OPTIONAL, INTENT(IN) :: sequence INTEGER, OPTIONAL, INTENT(OUT) :: size INTEGER, DIMENSION(:), OPTIONAL, INTENT(IN) :: put INTEGER, DIMENSION(:), OPTIONAL, INTENT(OUT) :: get User interface for seeding the random number routines. Syntax is exactly like Fortran 90’s random seed routine, with one additional argument keyword: sequence, set to any integer value, causes an immediate new initialization, seeded by that integer. if (present(size)) then size=5*lenran else if (present(put)) then if (lenran == 0) RETURN ranseeds=reshape(put,shape(ranseeds)) where (ranseeds(:,1:3) < 0) ranseeds(:,1:3)=not(ranseeds(:,1:3)) Enforce nonnegativity and nonzero conditions on any user-supplied seeds. where (ranseeds(:,4:5) == 0) ranseeds(:,4:5)=1 iran0=ranseeds(1,1) jran0=ranseeds(1,2) kran0=ranseeds(1,3) mran0=ranseeds(1,4) nran0=ranseeds(1,5) else if (present(get)) then if (lenran == 0) RETURN ranseeds(1,1:5)=(/ iran0,jran0,kran0,mran0,nran0 /) get=reshape(ranseeds,shape(get)) else if (present(sequence)) then call ran_deallocate seq=sequence end if END SUBROUTINE ran_seed
1148
Chapter B7.
Random Numbers
END MODULE ran_state
The lines ir=ieor(ir,ishft(ir,5))+1422217823 ir=ieor(ir,ishft(ir,-16))+1842055030 ir=ieor(ir,ishft(ir,9))+80567781
are not a Marsaglia shift sequence, though they resemble one. Instead, they implement a fast, nonlinear function on ir that we use as the “S-box” in a DES-like hashing algorithm. (See Volume 1, §7.5.) The triplet (5, −16, 9) is not chosen to give a full period Marsaglia sequence — it doesn’t. Instead it is chosen as being particularly good at separating in Hamming distance (i.e., number of nonidentical bits) two initially close values of ir (e.g., differing by only one bit). The large integer constants are chosen by a similar criterion. Note that the wrap-around of addition without generating an overflow error condition, which was tested in ran init, is relied upon here.
SUBROUTINE ran0_s(harvest) USE nrtype USE ran_state, ONLY: K4B,amm,lenran,ran_init,iran0,jran0,kran0,nran0,rans IMPLICIT NONE REAL(SP), INTENT(OUT) :: harvest Lagged Fibonacci generator combined with a Marsaglia shift sequence. Returns as harvest a uniform random deviate between 0.0 and 1.0 (exclusive of the endpoint values). This generator has the same calling and initialization conventions as Fortran 90’s random number routine. Use ran seed to initialize or reinitialize to a particular sequence. The period of this generator is about 2.0 × 1028, and it fully vectorizes. Validity of the integer model assumed by this generator is tested at initialization. if (lenran < 1) call ran_init(1) Initialization routine in ran state. rans=iran0-kran0 Update Fibonacci generator, which if (rans < 0) rans=rans+2147483579_k4b has period p2 + p + 1, p = 231 − iran0=jran0 69. jran0=kran0 kran0=rans nran0=ieor(nran0,ishft(nran0,13)) Update Marsaglia shift sequence with nran0=ieor(nran0,ishft(nran0,-17)) period 232 − 1. nran0=ieor(nran0,ishft(nran0,5)) rans=ieor(nran0,rans) Combine the generators. harvest=amm*merge(rans,not(rans), rans a. i=i+1 if (arr(i) >= a) exit end do do Scan down to find element < a. j=j-1 if (arr(j) = k) r=j-1 Keep active the partition that contains the kth element.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE icomp_xchg(i,j) IMPLICIT NONE INTEGER(I4B), INTENT(INOUT) :: i,j Swap or don’t swap integer arguments, depending on the ordering of their corresponding elements in an array arr. INTEGER(I4B) :: swp if (arr(j) < arr(i)) then swp=i i=j j=swp end if END SUBROUTINE icomp_xchg END SUBROUTINE index_bypack
1178
Chapter B8.
Sorting
if (j = 1, k arr(r)) middle, and last. call swap(arr(l),arr(i),arr(l)>arr(i)) a=arr(i) mask(l:r) = (arr(l:r) n/2 .or. m < 1) call nrerror(’probable misuse of select_heap’) heap=arr(1:m) call sort(heap) Create initial heap by overkill! We assume m n. do i=m+1,n For each remaining element... if (arr(i) > heap(1)) then Put it on the heap? heap(1)=arr(i) j=1 do Sift down. k=2*j if (k > m) exit if (k /= m) then if (heap(k) > heap(k+1)) k=k+1 end if if (heap(j) j) then Reset bounds to whichever side l=j+1 has the desired element. else if (k < j) then r=j-1 else l=j r=j end if end do if (r-l == 1) call swap(arr(l),arr(r),arr(l)>arr(r)) Case of only two left. select_bypack=arr(k) END FUNCTION select_bypack
1180
Chapter B8.
Sorting
FUNCTION eclazz(equiv,n) USE nrtype; USE nrutil, ONLY : arth IMPLICIT NONE INTERFACE FUNCTION equiv(i,j) USE nrtype IMPLICIT NONE LOGICAL(LGT) :: equiv INTEGER(I4B), INTENT(IN) :: i,j END FUNCTION equiv END INTERFACE INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B), DIMENSION(n) :: eclazz Given a user-supplied logical function equiv that tells whether a pair of elements, each in the range 1...n, are related, return in an array of length n equivalence class numbers for each element. INTEGER :: i,j eclazz(1:n)=arth(1,1,n) do i=2,n Loop over first element of all pairs. do j=1,i-1 Loop over second element of all pairs. eclazz(j)=eclazz(eclazz(j)) Sweep it up this much. if (equiv(i,j)) eclazz(eclazz(eclazz(j)))=i Good exercise for the reader to figure out why this much ancestry is necessary! end do end do do i=1,n Only this much sweeping is needed finally. eclazz(i)=eclazz(eclazz(i)) end do END FUNCTION eclazz
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION eclass(lista,listb,n) USE nrtype; USE nrutil, ONLY : arth,assert_eq IMPLICIT NONE INTEGER(I4B), DIMENSION(:), INTENT(IN) :: lista,listb INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B), DIMENSION(n) :: eclass Given M equivalences between pairs of n individual elements in the form of the input arrays lista and listb of length M , this routine returns in an array of length n the number of the equivalence class of each of the n elements, integers between 1 and n (not all such integers used). INTEGER :: j,k,l,m m=assert_eq(size(lista),size(listb),’eclass’) eclass(1:n)=arth(1,1,n) Initialize each element its own class. do l=1,m For each piece of input information... j=lista(l) do Track first element up to its ancestor. if (eclass(j) == j) exit j=eclass(j) end do k=listb(l) do Track second element up to its ancestor. if (eclass(k) == k) exit k=eclass(k) end do if (j /= k) eclass(j)=k If they are not already related, make them so. end do do j=1,n Final sweep up to highest ancestors. do if (eclass(j) == eclass(eclass(j))) exit eclass(j)=eclass(eclass(j)) end do end do END FUNCTION eclass
1181 Chapter B8. Sorting
CITED REFERENCES AND FURTHER READING:
Knuth, D.E. 1973, Sorting and Searching, vol. 3 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §5.2.5. [1]
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE scrsho(func) USE nrtype IMPLICIT NONE INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: ISCR=60,JSCR=21 For interactive “dumb terminal” use. Produce a crude graph of the function func over the prompted-for interval x1,x2. Query for another plot until the user signals satisfaction. Parameters: Number of horizontal and vertical positions in display. INTEGER(I4B) :: i,j,jz REAL(SP) :: dx,dyj,x,x1,x2,ybig,ysml REAL(SP), DIMENSION(ISCR) :: y CHARACTER(1), DIMENSION(ISCR,JSCR) :: scr CHARACTER(1) :: blank=’ ’,zero=’-’,yy=’l’,xx=’-’,ff=’x’ do write (*,*) ’ Enter x1,x2 (= to stop)’ Query for another plot; quit if x1=x2. read (*,*) x1,x2 if (x1 == x2) RETURN scr(1,1:JSCR)=yy Fill vertical sides with character ’l’. scr(ISCR,1:JSCR)=yy scr(2:ISCR-1,1)=xx Fill top, bottom with character ’-’. scr(2:ISCR-1,JSCR)=xx scr(2:ISCR-1,2:JSCR-1)=blank Fill interior with blanks. dx=(x2-x1)/(ISCR-1) x=x1 do i=1,ISCR Evaluate the function at equal intervals. y(i)=func(x) x=x+dx end do ysml=min(minval(y(:)),0.0_sp) Limits will include 0. ybig=max(maxval(y(:)),0.0_sp) if (ybig == ysml) ybig=ysml+1.0 Be sure to separate top and bottom. dyj=(JSCR-1)/(ybig-ysml) jz=1-ysml*dyj Note which row corresponds to 0. scr(1:ISCR,jz)=zero do i=1,ISCR Place an indicator at function height and 0. j=1+(y(i)-ysml)*dyj scr(i,j)=ff end do write (*,’(1x,1p,e10.3,1x,80a1)’) ybig,(scr(i,JSCR),i=1,ISCR) do j=JSCR-1,2,-1 Display. write (*,’(12x,80a1)’) (scr(i,j),i=1,ISCR) end do write (*,’(1x,1p,e10.3,1x,80a1)’) ysml,(scr(i,1),i=1,ISCR)
1182
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B9. Root Finding and Nonlinear Sets of Equations
Chapter B9.
Root Finding and Nonlinear Sets of Equations
1183
write (*,’(12x,1p,e10.3,40x,e10.3)’) x1,x2 end do END SUBROUTINE scrsho
In Fortran 90, the length of variables of type character should be declared as CHARACTER(1) or CHARACTER(len=1) (for a variable of length 1), rather than the older form CHARACTER*1. While the older form is still legal syntax, the newer one is more consistent with the syntax of other type declarations. (For variables of length 1, you can actually omit the length specifier entirely, and just say CHARACTER.)
SUBROUTINE zbrac(func,x1,x2,succes) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(INOUT) :: x1,x2 LOGICAL(LGT), INTENT(OUT) :: succes INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: NTRY=50 REAL(SP), PARAMETER :: FACTOR=1.6_sp Given a function func and an initial guessed range x1 to x2, the routine expands the range geometrically until a root is bracketed by the returned values x1 and x2 (in which case succes returns as .true.) or until the range becomes unacceptably large (in which case succes returns as .false.). INTEGER(I4B) :: j REAL(SP) :: f1,f2 if (x1 == x2) call nrerror(’zbrac: you have to guess an initial range’) f1=func(x1) f2=func(x2) succes=.true. do j=1,NTRY if ((f1 > 0.0 .and. f2 < 0.0) .or. & (f1 < 0.0 .and. f2 > 0.0)) RETURN if (abs(f1) < abs(f2)) then x1=x1+FACTOR*(x1-x2) f1=func(x1) else x2=x2+FACTOR*(x2-x1) f2=func(x2) end if end do succes=.false. END SUBROUTINE zbrac
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
f90
CHARACTER(1), DIMENSION(ISCR,JSCR) :: scr
1184
Chapter B9.
Root Finding and Nonlinear Sets of Equations
f90
This routine shows how to return arrays xb1 and xb2 whose size is not known in advance. The coding is explained in the subsection on pointers in §21.5.
FUNCTION rtbis(func,x1,x2,xacc) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(IN) :: x1,x2,xacc REAL(SP) :: rtbis INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE zbrak(func,x1,x2,n,xb1,xb2,nb) USE nrtype; USE nrutil, ONLY : arth IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B), INTENT(OUT) :: nb REAL(SP), INTENT(IN) :: x1,x2 REAL(SP), DIMENSION(:), POINTER :: xb1,xb2 INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE Given a function func defined on the interval from x1-x2 subdivide the interval into n equally spaced segments, and search for zero crossings of the function. nb is returned as the number of bracketing pairs xb1(1:nb), xb2(1:nb) that are found. xb1 and xb2 are pointers to arrays of length nb that are dynamically allocated by the routine. INTEGER(I4B) :: i REAL(SP) :: dx REAL(SP), DIMENSION(0:n) :: f,x LOGICAL(LGT), DIMENSION(1:n) :: mask LOGICAL(LGT), SAVE :: init=.true. if (init) then init=.false. nullify(xb1,xb2) end if if (associated(xb1)) deallocate(xb1) if (associated(xb2)) deallocate(xb2) dx=(x2-x1)/n Determine the spacing appropriate to the mesh. x=x1+dx*arth(0,1,n+1) do i=0,n Evaluate the function at the mesh points. f(i)=func(x(i)) end do mask=f(1:n)*f(0:n-1) 0.0 .and. fh > 0.0) .or. & (fl < 0.0 .and. fh < 0.0)) call & nrerror(’rtflsp: root must be bracketed between arguments’) if (fl < 0.0) then Identify the limits so that xl corresponds to xl=x1 the low side. xh=x2 else xl=x2 xh=x1 call swap(fl,fh) end if dx=xh-xl
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
INTEGER(I4B), PARAMETER :: MAXIT=40 Using bisection, find the root of a function func known to lie between x1 and x2. The root, returned as rtbis, will be refined until its accuracy is ±xacc. Parameter: MAXIT is the maximum allowed number of bisections. INTEGER(I4B) :: j REAL(SP) :: dx,f,fmid,xmid fmid=func(x2) f=func(x1) if (f*fmid >= 0.0) call nrerror(’rtbis: root must be bracketed’) if (f < 0.0) then Orient the search so that f>0 lies at x+dx. rtbis=x1 dx=x2-x1 else rtbis=x2 dx=x1-x2 end if do j=1,MAXIT Bisection loop. dx=dx*0.5_sp xmid=rtbis+dx fmid=func(xmid) if (fmid 0.0 .and. fh < 0.0) .or. (fl < 0.0 .and. fh > 0.0)) then xl=x1 xh=x2 zriddr=UNUSED Any highly unlikely value, to simplify logic do j=1,MAXIT below. xm=0.5_sp*(xl+xh) fm=func(xm) First of two function evaluations per its=sqrt(fm**2-fl*fh) eration. if (s == 0.0) RETURN xnew=xm+(xm-xl)*(sign(1.0_sp,fl-fh)*fm/s) Updating formula. if (abs(xnew-zriddr) 0.0) .or. (fa < 0.0 .and. fb < 0.0)) & call nrerror(’root must be bracketed for zbrent’) c=b fc=fb do iter=1,ITMAX if ((fb > 0.0 .and. fc > 0.0) .or. (fb < 0.0 .and. fc < 0.0)) then c=a Rename a, b, c and adjust bounding infc=fa terval d. d=b-a e=d end if if (abs(fc) < abs(fb)) then a=b b=c c=a fa=fb fb=fc fc=fa end if tol1=2.0_sp*EPS*abs(b)+0.5_sp*tol Convergence check. xm=0.5_sp*(c-b) if (abs(xm) = tol1 .and. abs(fa) > abs(fb)) then s=fb/fa Attempt inverse quadratic interpolation. if (a == c) then p=2.0_sp*xm*s q=1.0_sp-s else q=fa/fc r=fb/fc p=s*(2.0_sp*xm*q*(q-r)-(b-a)*(r-1.0_sp)) q=(q-1.0_sp)*(r-1.0_sp)*(s-1.0_sp) end if if (p > 0.0) q=-q Check whether in bounds. p=abs(p) if (2.0_sp*p < min(3.0_sp*xm*q-abs(tol1*q),abs(e*q))) then e=d Accept interpolation.
Chapter B9.
Root Finding and Nonlinear Sets of Equations
1189
f90
The routine zbrent works best when EPS is exactly the machine precision. The Fortran 90 intrinsic function epsilon allows us to code this in a portable fashion.
REAL(SP), PARAMETER :: EPS=epsilon(x1)
FUNCTION rtnewt(funcd,x1,x2,xacc) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(IN) :: x1,x2,xacc REAL(SP) :: rtnewt INTERFACE SUBROUTINE funcd(x,fval,fderiv) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), INTENT(OUT) :: fval,fderiv END SUBROUTINE funcd END INTERFACE INTEGER(I4B), PARAMETER :: MAXIT=20 Using the Newton-Raphson method, find the root of a function known to lie in the interval [x1, x2]. The root rtnewt will be refined until its accuracy is known within ±xacc. funcd is a user-supplied subroutine that returns both the function value and the first derivative of the function. Parameter: MAXIT is the maximum number of iterations. INTEGER(I4B) :: j REAL(SP) :: df,dx,f rtnewt=0.5_sp*(x1+x2) Initial guess. do j=1,MAXIT call funcd(rtnewt,f,df) dx=f/df rtnewt=rtnewt-dx if ((x1-rtnewt)*(rtnewt-x2) < 0.0)& call nrerror(’rtnewt: values jumped out of brackets’) if (abs(dx) < xacc) RETURN Convergence. end do call nrerror(’rtnewt exceeded maximum iterations’) END FUNCTION rtnewt
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
d=p/q else d=xm Interpolation failed; use bisection. e=d end if else Bounds decreasing too slowly; use bisecd=xm tion. e=d end if a=b Move last best guess to a. fa=fb b=b+merge(d,sign(tol1,xm), abs(d) > tol1 ) Evaluate new trial root. fb=func(b) end do call nrerror(’zbrent: exceeded maximum iterations’) zbrent=b END FUNCTION zbrent
1190
Chapter B9.
Root Finding and Nonlinear Sets of Equations
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION rtsafe(funcd,x1,x2,xacc) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(IN) :: x1,x2,xacc REAL(SP) :: rtsafe INTERFACE SUBROUTINE funcd(x,fval,fderiv) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), INTENT(OUT) :: fval,fderiv END SUBROUTINE funcd END INTERFACE INTEGER(I4B), PARAMETER :: MAXIT=100 Using a combination of Newton-Raphson and bisection, find the root of a function bracketed between x1 and x2. The root, returned as the function value rtsafe, will be refined until its accuracy is known within ±xacc. funcd is a user-supplied subroutine that returns both the function value and the first derivative of the function. Parameter: MAXIT is the maximum allowed number of iterations. INTEGER(I4B) :: j REAL(SP) :: df,dx,dxold,f,fh,fl,temp,xh,xl call funcd(x1,fl,df) call funcd(x2,fh,df) if ((fl > 0.0 .and. fh > 0.0) .or. & (fl < 0.0 .and. fh < 0.0)) & call nrerror(’root must be bracketed in rtsafe’) if (fl == 0.0) then rtsafe=x1 RETURN else if (fh == 0.0) then rtsafe=x2 RETURN else if (fl < 0.0) then Orient the search so that f (xl) < 0. xl=x1 xh=x2 else xh=x1 xl=x2 end if rtsafe=0.5_sp*(x1+x2) Initialize the guess for root, dxold=abs(x2-x1) the “stepsize before last,” dx=dxold and the last step. call funcd(rtsafe,f,df) do j=1,MAXIT Loop over allowed iterations. if (((rtsafe-xh)*df-f)*((rtsafe-xl)*df-f) > 0.0 .or. & abs(2.0_sp*f) > abs(dxold*df) ) then Bisect if Newton out of range, or not decreasing fast enough. dxold=dx dx=0.5_sp*(xh-xl) rtsafe=xl+dx if (xl == rtsafe) RETURN Change in root is negligible. else Newton step acceptable. Take it. dxold=dx dx=f/df temp=rtsafe rtsafe=rtsafe-dx if (temp == rtsafe) RETURN end if if (abs(dx) < xacc) RETURN Convergence criterion. call funcd(rtsafe,f,df) One new function evaluation per iteration. if (f < 0.0) then Maintain the bracket on the root. xl=rtsafe else xh=rtsafe
Chapter B9. Root Finding and Nonlinear Sets of Equations
1191
end if end do call nrerror(’rtsafe: exceeded maximum iterations’) END FUNCTION rtsafe
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE laguer(a,x,its) USE nrtype; USE nrutil, ONLY : nrerror,poly,poly_term IMPLICIT NONE INTEGER(I4B), INTENT(OUT) :: its COMPLEX(SPC), INTENT(INOUT) :: x COMPLEX(SPC), DIMENSION(:), INTENT(IN) :: a REAL(SP), PARAMETER :: EPS=epsilon(1.0_sp) INTEGER(I4B), PARAMETER :: MR=8,MT=10,MAXIT=MT*MR M +1 i−1 , and Given an array of M + 1 complex coefficients a of the polynomial i=1 a(i)x given a complex value x, this routine improves x by Laguerre’s method until it converges, within the achievable roundoff limit, to a root of the given polynomial. The number of iterations taken is returned as its. Parameters: EPS is the estimated fractional roundoff error. We try to break (rare) limit cycles with MR different fractional values, once every MT steps, for MAXIT total allowed iterations. INTEGER(I4B) :: iter,m REAL(SP) :: abx,abp,abm,err COMPLEX(SPC) :: dx,x1,f,g,h,sq,gp,gm,g2 COMPLEX(SPC), DIMENSION(size(a)) :: b,d REAL(SP), DIMENSION(MR) :: frac = & (/ 0.5_sp,0.25_sp,0.75_sp,0.13_sp,0.38_sp,0.62_sp,0.88_sp,1.0_sp /) Fractions used to break a limit cycle. m=size(a)-1 do iter=1,MAXIT Loop over iterations up to allowed maximum. its=iter abx=abs(x) b(m+1:1:-1)=poly_term(a(m+1:1:-1),x) Efficient computation of the polynomial d(m:1:-1)=poly_term(b(m+1:2:-1),x) and its first two derivatives. f=poly(x,d(2:m)) err=EPS*poly(abx,abs(b(1:m+1))) Esimate of roundoff in evaluating polynomial. if (abs(b(1)) 0.0) then dx=m/gp else dx=exp(cmplx(log(1.0_sp+abx),iter,kind=spc)) end if x1=x-dx if (x == x1) RETURN Converged. if (mod(iter,MT) /= 0) then x=x1 else Every so often we take a fractional step, to x=x-dx*frac(iter/MT) break any limit cycle (itself a rare occurend if rence). end do call nrerror(’laguer: too many iterations’) Very unusual — can occur only for complex roots. Try a different starting guess for the root. END SUBROUTINE laguer
1192
Chapter B9.
Root Finding and Nonlinear Sets of Equations
The poly term function in nrutil tabulates the partial sums of a polynomial, while poly evaluates the polynomial at x. In this example, we use poly term on the coefficient array in reverse order, so that the value of the polynomial ends up in b(1) and the value of its first derivative in d(1).
f90
b(m+1:1:-1)=poly_term...f=poly(x,d(2:m))
SUBROUTINE zroots(a,roots,polish) USE nrtype; USE nrutil, ONLY : assert_eq,poly_term USE nr, ONLY : laguer,indexx IMPLICIT NONE COMPLEX(SPC), DIMENSION(:), INTENT(IN) :: a COMPLEX(SPC), DIMENSION(:), INTENT(OUT) :: roots LOGICAL(LGT), INTENT(IN) :: polish REAL(SP), PARAMETER :: EPS=1.0e-6_sp M +1 i−1 , this Given the array of M + 1 complex coefficients a of the polynomial i=1 a(i)x routine successively calls laguer and finds all M complex roots. The logical variable polish should be input as .true. if polishing (also by Laguerre’s method) is desired, .false. if the roots will be subsequently polished by other means. Parameter: EPS is a small number. INTEGER(I4B) :: j,its,m INTEGER(I4B), DIMENSION(size(roots)) :: indx COMPLEX(SPC) :: x COMPLEX(SPC), DIMENSION(size(a)) :: ad m=assert_eq(size(roots),size(a)-1,’zroots’) ad(:)=a(:) Copy of coefficients for successive deflation. do j=m,1,-1 Loop over each root to be found. x=cmplx(0.0_sp,kind=spc) Start at zero to favor convergence to smallest remaining root. call laguer(ad(1:j+1),x,its) Find the root. if (abs(aimag(x)) fa) then Switch roles of a and b so that we call swap(ax,bx) can go downhill in the direction call swap(fa,fb) from a to b. end if cx=bx+GOLD*(bx-ax) First guess for c. fc=func(cx) do Do-while-loop: Keep returning here if (fb < fc) RETURN until we bracket. Compute u by parabolic extrapolation from a, b, c. TINY is used to prevent any possible division by zero. r=(bx-ax)*(fb-fc) q=(bx-cx)*(fb-fa) u=bx-((bx-cx)*q-(bx-ax)*r)/(2.0_sp*sign(max(abs(q-r),TINY),q-r)) ulim=bx+GLIMIT*(cx-bx) We won’t go farther than this. Test various possibilities: if ((bx-u)*(u-cx) > 0.0) then Parabolic u is between b and c: try fu=func(u) it. if (fu < fc) then Got a minimum between b and c. ax=bx fa=fb bx=u fb=fu RETURN else if (fu > fb) then Got a minimum between a and u. cx=u fc=fu RETURN
1201
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B10. Minimization or Maximization of Functions
1202
Chapter B10.
Minimization or Maximization of Functions
end if u=cx+GOLD*(cx-bx) fu=func(u) else if ((cx-u)*(u-ulim) > 0.0) then fu=func(u) if (fu < fc) then bx=cx cx=u u=cx+GOLD*(cx-bx) call shft(fb,fc,fu,func(u)) end if else if ((u-ulim)*(ulim-cx) >= 0.0) then u=ulim fu=func(u) else u=cx+GOLD*(cx-bx) fu=func(u) end if call shft(ax,bx,cx,u) call shft(fa,fb,fc,fu) end do CONTAINS
Parabolic fit was no use. Use default magnification. Parabolic fit is between c and its allowed limit.
Reject parabolic u, use default magnification.
Eliminate oldest point and continue.
SUBROUTINE shft(a,b,c,d) REAL(SP), INTENT(OUT) :: a REAL(SP), INTENT(INOUT) :: b,c REAL(SP), INTENT(IN) :: d a=b b=c c=d END SUBROUTINE shft END SUBROUTINE mnbrak
call shft... There are three places in mnbrak where we need to shift four variables around. Rather than repeat code, we make shft an internal subroutine, coming after a CONTAINS statement. It is invisible to all procedures except mnbrak.
f90
FUNCTION golden(ax,bx,cx,func,tol,xmin) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: ax,bx,cx,tol REAL(SP), INTENT(OUT) :: xmin REAL(SP) :: golden INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE REAL(SP), PARAMETER :: R=0.61803399_sp,C=1.0_sp-R Given a function func, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is between ax and cx, and func(bx) is less than both func(ax) and func(cx)), this routine performs a golden section search for the minimum, isolating it to a fractional precision of about tol. The abscissa of the minimum is returned as xmin, and the minimum
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Limit parabolic u to maximum allowed value.
Chapter B10. Minimization or Maximization of Functions
1203
SUBROUTINE shft2(a,b,c) REAL(SP), INTENT(OUT) :: a REAL(SP), INTENT(INOUT) :: b REAL(SP), INTENT(IN) :: c a=b b=c END SUBROUTINE shft2 SUBROUTINE shft3(a,b,c,d) REAL(SP), INTENT(OUT) :: a REAL(SP), INTENT(INOUT) :: b,c REAL(SP), INTENT(IN) :: d a=b b=c c=d END SUBROUTINE shft3 END FUNCTION golden
f90
call shft3...call shft2...
See discussion of shft for mnbrak on
p. 1202.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
function value is returned as golden, the returned function value. Parameters: The golden ratios. REAL(SP) :: f1,f2,x0,x1,x2,x3 x0=ax At any given time we will keep track of x3=cx four points, x0,x1,x2,x3. if (abs(cx-bx) > abs(bx-ax)) then Make x0 to x1 the smaller segment, x1=bx x2=bx+C*(cx-bx) and fill in the new point to be tried. else x2=bx x1=bx-C*(bx-ax) end if f1=func(x1) f2=func(x2) The initial function evaluations. Note that we never need to evaluate the function at the original endpoints. do Do-while-loop: We keep returning here. if (abs(x3-x0) 0.0) p=-p q=abs(q) etemp=e e=d if (abs(p) >= abs(0.5_sp*q*etemp) .or. & p = q*(b-x)) then The above conditions determine the acceptability of the parabolic fit. Here it is not o.k., so we take the golden section step into the larger of the two segments. e=merge(a-x,b-x, x >= xm ) d=CGOLD*e else Take the parabolic step. d=p/q u=x+d if (u-a < tol2 .or. b-u < tol2) d=sign(tol1,xm-x) end if
Chapter B10. Minimization or Maximization of Functions
1205
SUBROUTINE shft(a,b,c,d) REAL(SP), INTENT(OUT) :: a REAL(SP), INTENT(INOUT) :: b,c REAL(SP), INTENT(IN) :: d a=b b=c c=d END SUBROUTINE shft END FUNCTION brent
FUNCTION dbrent(ax,bx,cx,func,dfunc,tol,xmin) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(IN) :: ax,bx,cx,tol REAL(SP), INTENT(OUT) :: xmin REAL(SP) :: dbrent INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func FUNCTION dfunc(x)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
else Take the golden section step into the larger e=merge(a-x,b-x, x >= xm ) of the two segments. d=CGOLD*e end if u=merge(x+d,x+sign(tol1,d), abs(d) >= tol1 ) Arrive here with d computed either from parabolic fit, or else from golden section. fu=func(u) This is the one function evaluation per iteration. if (fu = x) then function evaluation. Housekeeping follows: a=x else b=x end if call shft(v,w,x,u) call shft(fv,fw,fx,fu) else if (u < x) then a=u else b=u end if if (fu = 0.0)-x d=0.5_sp*e Bisect, not golden section. end if if (abs(d) >= tol1) then u=x+d fu=func(u) else u=x+sign(tol1,d) fu=func(u) If the minimum step in the downhill if (fu > fx) exit direction takes us uphill, then we end if are done. du=dfunc(u) Now all the housekeeping, sigh. if (fu = x) then a=x else b=x end if call mov3(v,fv,dv,w,fw,dw) call mov3(w,fw,dw,x,fx,dx) call mov3(x,fx,dx,u,fu,du) else if (u < x) then a=u else b=u end if if (fu = ITMAX) call nrerror(’ITMAX exceeded in amoeba’) Begin a new iteration. First extrapolate by a factor −1 through the face of the simplex across from the high point, i.e., reflect the simplex from the high point. ytry=amotry(-1.0_sp) iter=iter+1 if (ytry = y(inhi)) then The reflected point is worse than the second highest, so look for an intermediate ysave=y(ihi) lower point, i.e., do a one-dimensional ytry=amotry(0.5_sp) contraction. iter=iter+1
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE amoeba(p,y,ftol,func,iter) USE nrtype; USE nrutil, ONLY : assert_eq,imaxloc,iminloc,nrerror,swap IMPLICIT NONE INTEGER(I4B), INTENT(OUT) :: iter REAL(SP), INTENT(IN) :: ftol REAL(SP), DIMENSION(:), INTENT(INOUT) :: y REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: p INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: ITMAX=5000 REAL(SP), PARAMETER :: TINY=1.0e-10 Minimization of the function func in N dimensions by the downhill simplex method of Nelder and Mead. The (N + 1) × N matrix p is input. Its N + 1 rows are N -dimensional vectors that are the vertices of the starting simplex. Also input is the vector y of length N + 1, whose components must be preinitialized to the values of func evaluated at the N + 1 vertices (rows) of p; and ftol the fractional convergence tolerance to be achieved in the function value (n.b.!). On output, p and y will have been reset to N + 1 new points all within ftol of a minimum function value, and iter gives the number of function evaluations taken. Parameters: The maximum allowed number of function evaluations, and a small number. INTEGER(I4B) :: ihi,ndim Global variables. REAL(SP), DIMENSION(size(p,2)) :: psum call amoeba_private CONTAINS
Chapter B10. Minimization or Maximization of Functions
1209
FUNCTION amotry(fac) IMPLICIT NONE REAL(SP), INTENT(IN) :: fac REAL(SP) :: amotry Extrapolates by a factor fac through the face of the simplex across from the high point, tries it, and replaces the high point if the new point is better. REAL(SP) :: fac1,fac2,ytry REAL(SP), DIMENSION(size(p,2)) :: ptry fac1=(1.0_sp-fac)/ndim fac2=fac1-fac ptry(:)=psum(:)*fac1-p(ihi,:)*fac2 ytry=func(ptry) Evaluate the function at the trial point. if (ytry < y(ihi)) then If it’s better than the highest, then replace y(ihi)=ytry the highest. psum(:)=psum(:)-p(ihi,:)+ptry(:) p(ihi,:)=ptry(:) end if amotry=ytry END FUNCTION amotry END SUBROUTINE amoeba
The only action taken by the subroutine amoeba is to call the internal subroutine amoeba private. Why this structure? The reason has to do with meeting the twin goals of data hiding (especially for “safe” scope of variables) and program readability. The situation is this: Logically, amoeba does most of the calculating, but calls an internal subroutine amotry at several different points, with several values of the parameter fac. However, fac is not the only piece of data that must be shared with amotry; the latter also needs access to several shared variables (ihi, ndim, psum) and arguments of amoeba (p, y, func). The obvious (but not best) way of coding this would be to put the computational guts in amoeba, with amotry as the sole internal subprogram. Assuming that fac is passed as an argument to amotry (it being the parameter that is being rapidly altered), one must decide whether to pass all the other quantities to amotry (i) as additional arguments (as is done in the Fortran 77 version), or (ii) “automatically,” i.e., doing nothing except using the fact that an internal subprogram has automatic access to all of its host’s entities. Each of these choices has strong disadvantages. Choice (i) is inefficient (all those arguments) and also obscures the fact that fac is the primary changing argument. Choice (ii) makes the program extremely difficult to read, because it wouldn’t be obvious without careful cross-comparison of the routines which variables in amoeba are actually global variables that are used by amotry. Choice (ii) is also “unsafe scoping” because it gives a nontrivially complicated internal subprogram, amotry, access to all the variables in its host. A common and difficult-to-find bug is the accidental alteration of a variable that one “thought”
f90
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (ytry >= ysave) then Can’t seem to get rid of that high point. Better contract around the lowest (best) point. p(:,:)=0.5_sp*(p(:,:)+spread(p(ilo,:),1,size(p,1))) do i=1,ndim+1 if (i /= ilo) y(i)=func(p(i,:)) end do iter=iter+ndim Keep track of function evaluations. psum(:)=sum(p(:,:),dim=1) end if end if end do Go back for the test of doneness and the next END SUBROUTINE amoeba_private iteration.
1210
Chapter B10.
Minimization or Maximization of Functions
ilo=iminloc...ihi=imaxloc...
See discussion of these functions on p. 1017.
call swap(y(1)...call swap(p(1,:)... Here the swap routine in nrutil is called once with a scalar argument and once with a vector argument. Inside nrutil scalar and vector versions have been overloaded onto the single name swap, hiding all the implementation details from the calling routine.
SUBROUTINE powell(p,xi,ftol,iter,fret) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : linmin IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: p REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: xi INTEGER(I4B), INTENT(OUT) :: iter REAL(SP), INTENT(IN) :: ftol REAL(SP), INTENT(OUT) :: fret INTERFACE FUNCTION func(p) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: p REAL(SP) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: ITMAX=200 REAL(SP), PARAMETER :: TINY=1.0e-25_sp Minimization of a function func of N variables. (func is not an argument, it is a fixed function name.) Input consists of an initial starting point p, a vector of length N ; an initial N × N matrix xi whose columns contain the initial set of directions (usually the N unit vectors); and ftol, the fractional tolerance in the function value such that failure to decrease by more than this amount on one iteration signals doneness. On output, p is set to the best point found, xi is the then-current direction set, fret is the returned function value at p, and iter is the number of iterations taken. The routine linmin is used. Parameters: Maximum allowed iterations, and a small number. INTEGER(I4B) :: i,ibig,n REAL(SP) :: del,fp,fptt,t REAL(SP), DIMENSION(size(p)) :: pt,ptt,xit n=assert_eq(size(p),size(xi,1),size(xi,2),’powell’) fret=func(p)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
was local, but is actually shared. (Simple variables like i, j, and n are the most common culprits.) We are therefore led to reject both choice (i) and choice (ii) in favor of a structure previously described in the subsection on Scope, Visibility, and Data Hiding in §21.5. The guts of amoeba are put in amoeba private, a sister routine to amotry. These two siblings have mutually private name spaces. However, any variables that they need to share (including the top-level arguments of amoeba) are declared as variables in the enclosing amoeba routine. The presence of these “global variables” serves as a warning flag to the reader that data are shared between routines. An alternative attractive way of coding the above situation would be to use a module containing amoeba and amotry. Everything would be declared private except the name amoeba. The global variables would be at the top level, and the arguments of amoeba that need to be passed to amotry would be handled by pointers among the global variables. Unfortunately, Fortran 90 does not support pointers to functions. Sigh!
Chapter B10. Minimization or Maximization of Functions
1211
MODULE f1dim_mod Used for communication from linmin to f1dim. USE nrtype INTEGER(I4B) :: ncom REAL(SP), DIMENSION(:), POINTER :: pcom,xicom CONTAINS FUNCTION f1dim(x) IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: f1dim Used by linmin as the one-dimensional function passed to mnbrak and brent. INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE REAL(SP), DIMENSION(:), ALLOCATABLE :: xt allocate(xt(ncom)) xt(:)=pcom(:)+x*xicom(:) f1dim=func(xt) deallocate(xt) END FUNCTION f1dim END MODULE f1dim_mod
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
pt(:)=p(:) Save the initial point. iter=0 do iter=iter+1 fp=fret ibig=0 del=0.0 Will be the biggest function decrease. do i=1,n Loop over all directions in the set. xit(:)=xi(:,i) Copy the direction, fptt=fret call linmin(p,xit,fret) minimize along it, if (fptt-fret > del) then and record it if it is the largest decrease so del=fptt-fret far. ibig=i end if end do if (2.0_sp*(fp-fret) = fp) cycle One reason not to use new direction. t=2.0_sp*(fp-2.0_sp*fret+fptt)*(fp-fret-del)**2-del*(fp-fptt)**2 if (t >= 0.0) cycle Other reason not to use new direction. call linmin(p,xit,fret) Move to minimum of the new direction, xi(:,ibig)=xi(:,n) and save the new direction. xi(:,n)=xit(:) end do Back for another iteration. END SUBROUTINE powell
1212
Chapter B10.
Minimization or Maximization of Functions
At first sight this situation is like the one involving USE fminln in newt on p. 1197: We want to pass arrays p and xi from linmin to f1dim without having them be arguments of f1dim. If you recall the discussion in §21.5 and on p. 1197, there are two ways of effecting this: via pointers or via allocatable arrays. There is an important difference here, however. The arrays p and xi are themselves arguments of linmin, and so cannot be allocatable arrays in the module. If we did want to use allocatable arrays in the module, we would have to copy p and xi into them. The pointer implementation is much more elegant, since no unnecessary copying is required. The construction here is identical to the one in fminln and newt, except that p and xi are arguments instead of automatic arrays.
f90
USE f1dim_mod
MODULE df1dim_mod Used for communication from dlinmin to f1dim and df1dim. USE nrtype INTEGER(I4B) :: ncom REAL(SP), DIMENSION(:), POINTER :: pcom,xicom CONTAINS FUNCTION f1dim(x) IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: f1dim Used by dlinmin as the one-dimensional function passed to mnbrak. INTERFACE FUNCTION func(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE REAL(SP), DIMENSION(:), ALLOCATABLE :: xt
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE linmin(p,xi,fret) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : mnbrak,brent USE f1dim_mod IMPLICIT NONE REAL(SP), INTENT(OUT) :: fret REAL(SP), DIMENSION(:), TARGET, INTENT(INOUT) :: p,xi REAL(SP), PARAMETER :: TOL=1.0e-4_sp Given an N -dimensional point p and an N -dimensional direction xi, both vectors of length N , moves and resets p to where the fixed-name function func takes on a minimum along the direction xi from p, and replaces xi by the actual vector displacement that p was moved. Also returns as fret the value of func at the returned location p. This is actually all accomplished by calling the routines mnbrak and brent. Parameter: Tolerance passed to brent. REAL(SP) :: ax,bx,fa,fb,fx,xmin,xx ncom=assert_eq(size(p),size(xi),’linmin’) pcom=>p Communicate the global variables to f1dim. xicom=>xi ax=0.0 Initial guess for brackets. xx=1.0 call mnbrak(ax,xx,bx,fa,fx,fb,f1dim) fret=brent(ax,xx,bx,f1dim,TOL,xmin) xi=xmin*xi Construct the vector results to return. p=p+xi END SUBROUTINE linmin
Chapter B10. Minimization or Maximization of Functions
1213
allocate(xt(ncom)) xt(:)=pcom(:)+x*xicom(:) f1dim=func(xt) deallocate(xt) END FUNCTION f1dim
SUBROUTINE dlinmin(p,xi,fret) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : mnbrak,dbrent USE df1dim_mod IMPLICIT NONE REAL(SP), INTENT(OUT) :: fret REAL(SP), DIMENSION(:), TARGET :: p,xi REAL(SP), PARAMETER :: TOL=1.0e-4_sp Given an N -dimensional point p and an N -dimensional direction xi, both vectors of length N , moves and resets p to where the fixed-name function func takes on a minimum along the direction xi from p, and replaces xi by the actual vector displacement that p was moved. Also returns as fret the value of func at the returned location p. This is actually all accomplished by calling the routines mnbrak and dbrent. dfunc is a fixed-name usersupplied function that computes the gradient of func. Parameter: Tolerance passed to dbrent. REAL(SP) :: ax,bx,fa,fb,fx,xmin,xx ncom=assert_eq(size(p),size(xi),’dlinmin’) pcom=>p Communicate the global variables to f1dim. xicom=>xi ax=0.0 Initial guess for brackets. xx=1.0 call mnbrak(ax,xx,bx,fa,fx,fb,f1dim) fret=dbrent(ax,xx,bx,f1dim,df1dim,TOL,xmin) xi=xmin*xi Construct the vector results to return. p=p+xi END SUBROUTINE dlinmin
f90
USE df1dim_mod
See discussion of USE f1dim mod on p. 1212.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION df1dim(x) IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP) :: df1dim Used by dlinmin as the one-dimensional function passed to dbrent. INTERFACE FUNCTION dfunc(x) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP), DIMENSION(size(x)) :: dfunc END FUNCTION dfunc END INTERFACE REAL(SP), DIMENSION(:), ALLOCATABLE :: xt,df allocate(xt(ncom),df(ncom)) xt(:)=pcom(:)+x*xicom(:) df(:)=dfunc(xt) df1dim=dot_product(df,xicom) deallocate(xt,df) END FUNCTION df1dim END MODULE df1dim_mod
1214
Chapter B10.
Minimization or Maximization of Functions
!
FUNCTION dfunc(p) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: p REAL(SP), DIMENSION(size(p)) :: dfunc END FUNCTION dfunc END INTERFACE INTEGER(I4B), PARAMETER :: ITMAX=200 REAL(SP), PARAMETER :: EPS=1.0e-10_sp Given a starting point p that is a vector of length N , Fletcher-Reeves-Polak-Ribiere minimization is performed on a function func, using its gradient as calculated by a routine dfunc. The convergence tolerance on the function value is input as ftol. Returned quantities are p (the location of the minimum), iter (the number of iterations that were performed), and fret (the minimum value of the function). The routine linmin is called to perform line minimizations. Parameters: ITMAX is the maximum allowed number of iterations; EPS is a small number to rectify the special case of converging to exactly zero function value. INTEGER(I4B) :: its REAL(SP) :: dgg,fp,gam,gg REAL(SP), DIMENSION(size(p)) :: g,h,xi fp=func(p) Initializations. xi=dfunc(p) g=-xi h=g xi=h do its=1,ITMAX Loop over iterations. iter=its call linmin(p,xi,fret) Next statement is the normal return: if (2.0_sp*abs(fret-fp) sqrt(EPS*sumdg*sumxi)) then Skip update if fac not sufficiently fac=1.0_sp/fac positive. fad=1.0_sp/fae dg=fac*xi-fad*hdg Vector that makes BFGS different from DFP. hessin=hessin+fac*outerprod(xi,xi)-& The BFGS updating formula. fad*outerprod(hdg,hdg)+fae*outerprod(dg,dg) end if xi=-matmul(hessin,g) Now calculate the next direction to go, end do and go back for another iteration. call nrerror(’dfpmin: too many iterations’) END SUBROUTINE dfpmin
Chapter B10. Minimization or Maximization of Functions
1217
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (nl1 > 0) then kp=l1(imaxloc(a(m+2,l1(1:nl1)+1))) Find the maximum coefficient of the bmax=a(m+2,kp+1) auxiliary objective function. else bmax=0.0 end if phase1a: do if (bmax = n+m1+m2+1) then Exchanged out an artificial variable for an k=ifirstloc(l1(1:nl1) == kp) equality constraint. Make sure it stays nl1=nl1-1 out by removing it from the l1 list. l1(k:nl1)=l1(k+1:nl1+1) else kh=iposv(ip)-m1-n if (kh >= 1) then Exchanged out an m2 type constraint. if (l3(kh) /= 0) then If it’s the first time, correct the pivot coll3(kh)=0 umn for the minus sign and the implicit a(m+2,kp+1)=a(m+2,kp+1)+1.0_sp artificial variable. a(1:m+2,kp+1)=-a(1:m+2,kp+1) end if end if end if call swap(izrov(kp),iposv(ip)) Update lists of left- and right-hand variables. end do phase1 If still in phase one, go back again. phase2: do We have an initial feasible solution. Now optimize it. if (nl1 > 0) then kp=l1(imaxloc(a(1,l1(1:nl1)+1))) Test the z-row for doneness. bmax=a(1,kp+1) else bmax=0.0 end if
1218
Chapter B10.
Minimization or Maximization of Functions
Done. Solution found. Return with the good news. Locate a pivot element (phase two). Objective function is unbounded. Report and return. Exchange a left- and a right-hand variable, update lists of left- and right-hand variables, and return for another iteration.
SUBROUTINE simp1 Locate a pivot element, taking degeneracy into account. IMPLICIT NONE INTEGER(I4B) :: i,k REAL(SP) :: q,q0,q1,qp ip=0 i=ifirstloc(a(2:m+1,kp+1) < -EPS) if (i > m) RETURN No possible pivots. Return with message. q1=-a(i+1,1)/a(i+1,kp+1) ip=i do i=ip+1,m if (a(i+1,kp+1) < -EPS) then q=-a(i+1,1)/a(i+1,kp+1) if (q < q1) then ip=i q1=q else if (q == q1) then We have a degeneracy. do k=1,n qp=-a(ip+1,k+1)/a(ip+1,kp+1) q0=-a(i+1,k+1)/a(i+1,kp+1) if (q0 /= qp) exit end do if (q0 < qp) ip=i end if end if end do END SUBROUTINE simp1 SUBROUTINE simp2(i1,k1) IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: i1,k1 Matrix operations to exchange a left-hand and right-hand variable (see text). INTEGER(I4B) :: ip1,kp1 REAL(SP) :: piv INTEGER(I4B), DIMENSION(k1) :: icol INTEGER(I4B), DIMENSION(i1) :: irow INTEGER(I4B), DIMENSION(max(i1,k1)+1) :: itmp ip1=ip+1 kp1=kp+1 piv=1.0_sp/a(ip1,kp1) itmp(1:k1+1)=arth(1,1,k1+1) icol=pack(itmp(1:k1+1),itmp(1:k1+1) /= kp1) itmp(1:i1+1)=arth(1,1,i1+1) irow=pack(itmp(1:i1+1),itmp(1:i1+1) /= ip1) a(irow,kp1)=a(irow,kp1)*piv a(irow,icol)=a(irow,icol)-outerprod(a(irow,kp1),a(ip1,icol)) a(ip1,icol)=-a(ip1,icol)*piv a(ip1,kp1)=piv END SUBROUTINE simp2 END SUBROUTINE simplx
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (bmax = n(1)) n(2)=n(2)+1 nn=1+mod((n(1)-n(2)+ncity-1),ncity) nn is the number of cities not on if (nn >= 3) exit the segment. end do
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
phase1a: do...end do phase1a This is not a real do-loop: It is executed only once, as you can see from the unconditional exit before the end do. We use this construction to define a block of code that is traversed once but that has several possible exit points.
1220
Chapter B10.
Minimization or Maximization of Functions
FUNCTION alen(x1,x2,y1,y2) IMPLICIT NONE REAL(SP), INTENT(IN) :: x1,x2,y1,y2 REAL(SP) :: alen Computes distance between two cities. alen=sqrt((x2-x1)**2+(y2-y1)**2) END FUNCTION alen FUNCTION alen_v(x1,x2,y1,y2) IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x1,x2,y1,y2 REAL(SP), DIMENSION(size(x1)) :: alen_v Computes distances between pairs of cities. alen_v=sqrt((x2-x1)**2+(y2-y1)**2) END FUNCTION alen_v SUBROUTINE metrop(de,t,ans) IMPLICIT NONE REAL(SP), INTENT(IN) :: de,t LOGICAL(LGT), INTENT(OUT) :: ans Metropolis algorithm. ans is a logical variable that issues a verdict on whether to accept a reconfiguration that leads to a change de in the objective function. If de0, ans is only .true. with probability exp(-de/t), where t is a temperature determined by the annealing schedule. call ran1(harvest) ans=(de < 0.0) .or. (harvest < exp(-de/t)) END SUBROUTINE metrop SUBROUTINE revcst(x,y,iorder,n,de) IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y INTEGER(I4B), DIMENSION(:), INTENT(IN) :: iorder INTEGER(I4B), DIMENSION(:), INTENT(INOUT) :: n REAL(SP), INTENT(OUT) :: de
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call ran1(harvest) Decide whether to do a reversal or a transport. if (harvest < 0.5_sp) then Do a transport. call ran1(harvest) n(3)=n(2)+int(abs(nn-2)*harvest)+1 n(3)=1+mod(n(3)-1,ncity) Transport to a location not on the path. call trncst(x,y,iorder,n,de) Calculate cost. call metrop(de,t,ans) Consult the oracle. if (ans) then nsucc=nsucc+1 path=path+de call trnspt(iorder,n) Carry out the transport. end if else Do a path reversal. call revcst(x,y,iorder,n,de) Calculate cost. call metrop(de,t,ans) Consult the oracle. if (ans) then nsucc=nsucc+1 path=path+de call revers(iorder,n) Carry out the reversal. end if end if if (nsucc >= nlimit) exit Finish early if we have enough successful end do changes. write(*,*) write(*,*) ’T =’,t,’ Path Length =’,path write(*,*) ’Successful Moves: ’,nsucc t=t*tfactr Annealing schedule. if (nsucc == 0) RETURN If no success, we are done. end do CONTAINS
Chapter B10. Minimization or Maximization of Functions
1221
SUBROUTINE revers(iorder,n) IMPLICIT NONE INTEGER(I4B), DIMENSION(:), INTENT(INOUT) :: iorder INTEGER(I4B), DIMENSION(:), INTENT(IN) :: n This routine performs a path segment reversal. iorder is an input array giving the present itinerary. The vector n has as its first four elements the first and last cities n(1), n(2) of the path segment to be reversed, and the two cities n(3) and n(4) that immediately precede and follow this segment. n(3) and n(4) are found by subroutine revcst. On output, iorder contains the segment from n(1) to n(2) in reversed order. INTEGER(I4B) :: j,k,l,nn,ncity ncity=size(iorder) nn=(1+mod(n(2)-n(1)+ncity,ncity))/2 This many cities must be swapped to effect do j=1,nn the reversal. k=1+mod((n(1)+j-2),ncity) Start at the ends of the segment and swap l=1+mod((n(2)-j+ncity),ncity) pairs of cities, moving toward the cencall swap(iorder(k),iorder(l)) ter. end do END SUBROUTINE revers SUBROUTINE trncst(x,y,iorder,n,de) IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y INTEGER(I4B), DIMENSION(:), INTENT(IN) :: iorder INTEGER(I4B), DIMENSION(:), INTENT(INOUT) :: n REAL(SP), INTENT(OUT) :: de This subroutine returns the value of the cost function for a proposed path segment transport. Arrays x and y give the city coordinates. iorder is an array giving the present itinerary. The first three elements of array n give the starting and ending cities of the path to be transported, and the point among the remaining cities after which it is to be inserted. On output, de is the cost of the change. The actual transport is not performed by this routine. INTEGER(I4B) :: ncity REAL(SP), DIMENSION(6) :: xx,yy ncity=size(x) n(4)=1+mod(n(3),ncity) Find the city following n(3) . . . n(5)=1+mod((n(1)+ncity-2),ncity) . . . and the one preceding n(1) . . . n(6)=1+mod(n(2),ncity) . . . and the one following n(2). xx(1:6)=x(iorder(n(1:6))) Determine coordinates for the six cities inyy(1:6)=y(iorder(n(1:6))) volved. de=-alen(xx(2),xx(6),yy(2),yy(6))& Calculate the cost of disconnecting the path segment from n(1) to n(2), opening a -alen(xx(1),xx(5),yy(1),yy(5))& space between n(3) and n(4), connect-alen(xx(3),xx(4),yy(3),yy(4))& ing the segment in the space, and con+alen(xx(1),xx(3),yy(1),yy(3))& necting n(5) to n(6). +alen(xx(2),xx(4),yy(2),yy(4))& +alen(xx(5),xx(6),yy(5),yy(6)) END SUBROUTINE trncst SUBROUTINE trnspt(iorder,n) IMPLICIT NONE
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
This subroutine returns the value of the cost function for a proposed path reversal. The arrays x and y give the coordinates of these cities. iorder holds the present itinerary. The first two values n(1) and n(2) of array n give the starting and ending cities along the path segment which is to be reversed. On output, de is the cost of making the reversal. The actual reversal is not performed by this routine. INTEGER(I4B) :: ncity REAL(SP), DIMENSION(4) :: xx,yy ncity=size(x) n(3)=1+mod((n(1)+ncity-2),ncity) Find the city before n(1) . . . n(4)=1+mod(n(2),ncity) . . . and the city after n(2). xx(1:4)=x(iorder(n(1:4))) Find coordinates for the four cities involved. yy(1:4)=y(iorder(n(1:4))) de=-alen(xx(1),xx(3),yy(1),yy(3))& Calculate cost of disconnecting the segment -alen(xx(2),xx(4),yy(2),yy(4))& at both ends and reconnecting in the op+alen(xx(1),xx(4),yy(1),yy(4))& posite order. +alen(xx(2),xx(3),yy(2),yy(3)) END SUBROUTINE revcst
1222
Chapter B10.
Minimization or Maximization of Functions
SUBROUTINE amebsa(p,y,pb,yb,ftol,func,iter,temptr) USE nrtype; USE nrutil, ONLY : assert_eq,imaxloc,iminloc,swap USE nr, ONLY : ran1 IMPLICIT NONE INTEGER(I4B), INTENT(INOUT) :: iter REAL(SP), INTENT(INOUT) :: yb REAL(SP), INTENT(IN) :: ftol,temptr REAL(SP), DIMENSION(:), INTENT(INOUT) :: y,pb REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: p INTERFACE FUNCTION func(x) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x REAL(SP) :: func END FUNCTION func END INTERFACE INTEGER(I4B), PARAMETER :: NMAX=200 Minimization of the N -dimensional function func by simulated annealing combined with the downhill simplex method of Nelder and Mead. The (N + 1) × N matrix p is input. Its N + 1 rows are N -dimensional vectors that are the vertices of the starting simplex. Also input is the vector y of length N +1, whose components must be preinitialized to the values of func evaluated at the N +1 vertices (rows) of p; ftol, the fractional convergence tolerance to be achieved in the function value for an early return; iter, and temptr. The routine makes iter function evaluations at an annealing temperature temptr, then returns. You should then decrease temptr according to your annealing schedule, reset iter, and call the routine again (leaving other arguments unaltered between calls). If iter is returned with a positive value, then early convergence and return occurred. If you initialize yb to a very large value on the first call, then yb and pb (an array of length N ) will subsequently return the best function value and point ever encountered (even if it is no longer a point in the simplex). INTEGER(I4B) :: ihi,ndim Global variables. REAL(SP) :: yhi REAL(SP), DIMENSION(size(p,2)) :: psum call amebsa_private
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
INTEGER(I4B), DIMENSION(:), INTENT(INOUT) :: iorder INTEGER(I4B), DIMENSION(:), INTENT(IN) :: n This routine does the actual path transport, once metrop has approved. iorder is an input array giving the present itinerary. The array n has as its six elements the beginning n(1) and end n(2) of the path to be transported, the adjacent cities n(3) and n(4) between which the path is to be placed, and the cities n(5) and n(6) that precede and follow the path. n(4), n(5), and n(6) are calculated by subroutine trncst. On output, iorder is modified to reflect the movement of the path segment. INTEGER(I4B) :: m1,m2,m3,nn,ncity INTEGER(I4B), DIMENSION(size(iorder)) :: jorder ncity=size(iorder) m1=1+mod((n(2)-n(1)+ncity),ncity) Find number of cities from n(1) to n(2) . . . m2=1+mod((n(5)-n(4)+ncity),ncity) . . . and the number from n(4) to n(5) m3=1+mod((n(3)-n(6)+ncity),ncity) . . . and the number from n(6) to n(3). jorder(1:m1)=iorder(1+mod((arth(1,1,m1)+n(1)-2),ncity)) Copy the chosen segment. nn=m1 jorder(nn+1:nn+m2)=iorder(1+mod((arth(1,1,m2)+n(4)-2),ncity)) Then copy the segment from n(4) to n(5). nn=nn+m2 jorder(nn+1:nn+m3)=iorder(1+mod((arth(1,1,m3)+n(6)-2),ncity)) Finally, the segment from n(6) to n(3). iorder(1:ncity)=jorder(1:ncity) Copy jorder back into iorder. END SUBROUTINE trnspt END SUBROUTINE anneal
Chapter B10. Minimization or Maximization of Functions
1223
CONTAINS
FUNCTION amotsa(fac) IMPLICIT NONE REAL(SP), INTENT(IN) :: fac REAL(SP) :: amotsa Extrapolates by a factor fac through the face of the simplex across from the high point, tries it, and replaces the high point if the new point is better. REAL(SP) :: fac1,fac2,yflu,ytry,harv REAL(SP), DIMENSION(size(p,2)) :: ptry fac1=(1.0_sp-fac)/ndim fac2=fac1-fac ptry(:)=psum(:)*fac1-p(ihi,:)*fac2 ytry=func(ptry) if (ytry = ysave) then Can’t seem to get rid of that high point. Better contract around the lowest (best) point. p(:,:)=0.5_sp*(p(:,:)+spread(p(ilo,:),1,size(p,1))) do i=1,ndim+1 if (i /= ilo) y(i)=func(p(i,:)) end do iter=iter-ndim Keep track of function evaluations. psum(:)=sum(p(:,:),dim=1) end if end if end do END SUBROUTINE amebsa_private
yb=ytry end if call ran1(harv) yflu=ytry+temptr*log(harv) if (yflu < yhi) then y(ihi)=ytry yhi=yflu psum(:)=psum(:)-p(ihi,:)+ptry(:) p(ihi,:)=ptry(:) end if amotsa=yflu END FUNCTION amotsa END SUBROUTINE amebsa
We added a thermal fluctuation to all the current vertices, but we subtract it here, so as to give the simplex a thermal Brownian motion: It likes to accept any suggested change.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
See the discussion of amoeba on p. 1209 for why the routine is coded this way.
f90
Minimization or Maximization of Functions Chapter B10.
1224
Eigensystems
SUBROUTINE jacobi(a,d,v,nrot) USE nrtype; USE nrutil, ONLY : assert_eq,get_diag,nrerror,unit_matrix,& upper_triangle IMPLICIT NONE INTEGER(I4B), INTENT(OUT) :: nrot REAL(SP), DIMENSION(:), INTENT(OUT) :: d REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a REAL(SP), DIMENSION(:,:), INTENT(OUT) :: v Computes all eigenvalues and eigenvectors of a real symmetric N × N matrix a. On output, elements of a above the diagonal are destroyed. d is a vector of length N that returns the eigenvalues of a. v is an N × N matrix whose columns contain, on output, the normalized eigenvectors of a. nrot returns the number of Jacobi rotations that were required. INTEGER(I4B) :: i,ip,iq,n REAL(SP) :: c,g,h,s,sm,t,tau,theta,tresh REAL(SP), DIMENSION(size(d)) :: b,z n=assert_eq((/size(a,1),size(a,2),size(d),size(v,1),size(v,2)/),’jacobi’) call unit_matrix(v(:,:)) Initialize v to the identity matrix. b(:)=get_diag(a(:,:)) Initialize b and d to the diagonal of d(:)=b(:) a. z(:)=0.0 This vector will accumulate terms of nrot=0 the form tapq as in eq. (11.1.14). do i=1,50 sm=sum(abs(a),mask=upper_triangle(n,n)) Sum off-diagonal elements. if (sm == 0.0) RETURN The normal return, which relies on quadratic convergence to machine underflow. tresh=merge(0.2_sp*sm/n**2,0.0_sp, i < 4 ) On the first three sweeps, we will rotate only if tresh exceeded. do ip=1,n-1 do iq=ip+1,n g=100.0_sp*abs(a(ip,iq)) After four sweeps, skip the rotation if the off-diagonal element is small. if ((i > 4) .and. (abs(d(ip))+g == abs(d(ip))) & .and. (abs(d(iq))+g == abs(d(iq)))) then a(ip,iq)=0.0 else if (abs(a(ip,iq)) > tresh) then h=d(iq)-d(ip) if (abs(h)+g == abs(h)) then t=a(ip,iq)/h t = 1/(2θ) else theta=0.5_sp*h/a(ip,iq) Equation (11.1.10). t=1.0_sp/(abs(theta)+sqrt(1.0_sp+theta**2)) if (theta < 0.0) t=-t end if c=1.0_sp/sqrt(1+t**2) s=t*c tau=s/(1.0_sp+c) h=t*a(ip,iq) z(ip)=z(ip)-h z(iq)=z(iq)+h d(ip)=d(ip)-h d(iq)=d(iq)+h a(ip,iq)=0.0
1225
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B11.
1226
Chapter B11.
Eigensystems
SUBROUTINE jrotate(a1,a2) REAL(SP), DIMENSION(:), INTENT(INOUT) :: a1,a2 REAL(SP), DIMENSION(size(a1)) :: wk1 wk1(:)=a1(:) a1(:)=a1(:)-s*(a2(:)+a1(:)*tau) a2(:)=a2(:)+s*(wk1(:)-a2(:)*tau) END SUBROUTINE jrotate END SUBROUTINE jacobi
As discussed in Volume 1, jacobi is generally not competitive with tqli in terms of efficiency. However, jacobi can be parallelized whereas tqli uses an intrinsically serial algorithm. The version of jacobi implemented here is likely to be adequate for a small-scale parallel (SSP) machine, but is probably still not competitive with tqli. For a massively multiprocessor (MMP) machine, the order of the rotations needs to be chosen in a more complicated pattern than here so that the rotations can be executed in parallel. In this case the Jacobi algorithm may well turn out to be the method of choice. Parallel replacements for tqli based on a divide and conquer algorithm have also been proposed. See the discussion after tqli on p. 1229. These routines in nrutil both require access to the diagonal of a matrix, an operation that is not conveniently provided for in Fortran 90. We have split them off into nrutil in case your compiler provides parallel library routines so you can replace our standard versions.
f90
call unit_matrix...b(:)=get_diag...
sm=sum(abs(a),mask=upper_triangle(n,n)) The upper triangle function in nrutil returns an upper triangular logical mask. As used here, the mask is true everywhere in the upper triangle of an n × n matrix, excluding the diagonal. An optional integer argument extra allows additional diagonals to be set to true. With extra=1 the upper triangle including the diagonal would be true. By using the mask, we can conveniently sum over the desired matrix elements in parallel. SUBROUTINE jrotate(a1,a2) This internal subroutine also uses the values of s and tau from the calling subroutine jacobi. Variables in the calling routine are visible to an internal subprogram, but you should be circumspect in making use of this fact. It is easy to overwrite a value in the calling program inadvertently, and it is
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call jrotate(a(1:ip-1,ip),a(1:ip-1,iq)) Case of rotations 1 ≤ j < p. call jrotate(a(ip,ip+1:iq-1),a(ip+1:iq-1,iq)) Case of rotations p < j < q. call jrotate(a(ip,iq+1:n),a(iq,iq+1:n)) Case of rotations q < j ≤ n. call jrotate(v(:,ip),v(:,iq)) nrot=nrot+1 end if end do end do b(:)=b(:)+z(:) d(:)=b(:) Update d with the sum of tapq , z(:)=0.0 and reinitialize z. end do call nrerror(’too many iterations in jacobi’) CONTAINS
Chapter B11. Eigensystems
1227
often difficult to figure out the logic of an internal routine if not all its variables are declared explicitly. However, jrotate is so simple that there is no danger here.
f90
j=imaxloc...
call swap...
See discussion of imaxloc on p. 1017.
See discussion of overloaded versions of swap after amoeba
on p. 1210.
SUBROUTINE tred2(a,d,e,novectors) USE nrtype; USE nrutil, ONLY : assert_eq,outerprod IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a REAL(SP), DIMENSION(:), INTENT(OUT) :: d,e LOGICAL(LGT), OPTIONAL, INTENT(IN) :: novectors Householder reduction of a real, symmetric, N × N matrix a. On output, a is replaced by the orthogonal matrix Q effecting the transformation. d returns the diagonal elements of the tridiagonal matrix, and e the off-diagonal elements, with e(1)=0. If the optional argument novectors is present, only eigenvalues are to be found subsequently, in which case a contains no useful information on output. INTEGER(I4B) :: i,j,l,n REAL(SP) :: f,g,h,hh,scale REAL(SP), DIMENSION(size(a,1)) :: gg LOGICAL(LGT), SAVE :: yesvec=.true. n=assert_eq(size(a,1),size(a,2),size(d),size(e),’tred2’) if (present(novectors)) yesvec=.not. novectors do i=n,2,-1 l=i-1 h=0.0 if (l > 1) then scale=sum(abs(a(i,1:l))) if (scale == 0.0) then Skip transformation. e(i)=a(i,l) else a(i,1:l)=a(i,1:l)/scale Use scaled a’s for transformation. h=sum(a(i,1:l)**2) Form σ in h.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE eigsrt(d,v) USE nrtype; USE nrutil, ONLY : assert_eq,imaxloc,swap IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: d REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: v Given the eigenvalues d and eigenvectors v as output from jacobi (§11.1) or tqli (§11.3), this routine sorts the eigenvalues into descending order, and rearranges the columns of v correspondingly. The method is straight insertion. INTEGER(I4B) :: i,j,n n=assert_eq(size(d),size(v,1),size(v,2),’eigsrt’) do i=1,n-1 j=imaxloc(d(i:n))+i-1 if (j /= i) then call swap(d(i),d(j)) call swap(v(:,i),v(:,j)) end if end do END SUBROUTINE eigsrt
1228
Chapter B11.
Eigensystems
This routine gives a nice example of the usefulness of optional arguments. The routine is written under the assumption that usually you will want to find both eigenvalues and eigenvectors. In this case you just supply the arguments a, d, and e. If, however, you want only eigenvalues, you supply the additional logical argument novectors with the value .true.. The routine then skips the unnecessary computations. Supplying novectors with the value .false. has the same effect as omitting it.
f90
SUBROUTINE tqli(d,e,z) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : pythag IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: d,e REAL(SP), DIMENSION(:,:), OPTIONAL, INTENT(INOUT) :: z QL algorithm with implicit shifts, to determine the eigenvalues and eigenvectors of a real, symmetric, tridiagonal matrix, or of a real, symmetric matrix previously reduced by tred2
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
f=a(i,l) g=-sign(sqrt(h),f) e(i)=scale*g h=h-f*g Now h is equation (11.2.4). a(i,l)=f-g Store u in the ith row of a. if (yesvec) a(1:l,i)=a(i,1:l)/h Store u/H in ith column of a. do j=1,l Store elements of p in temporarily e(j)=(dot_product(a(j,1:j),a(i,1:j)) & unused elements of e. +dot_product(a(j+1:l,j),a(i,j+1:l)))/h end do f=dot_product(e(1:l),a(i,1:l)) hh=f/(h+h) Form K, equation (11.2.11). e(1:l)=e(1:l)-hh*a(i,1:l) Form q and store in e overwriting p. do j=1,l Reduce a, equation (11.2.13). a(j,1:j)=a(j,1:j)-a(i,j)*e(1:j)-e(j)*a(i,1:j) end do end if else e(i)=a(i,l) end if d(i)=h end do if (yesvec) d(1)=0.0 e(1)=0.0 do i=1,n Begin accumulation of transformaif (yesvec) then tion matrices. l=i-1 if (d(i) /= 0.0) then This block skipped when i=1. Use u and u/H stored in a to form P · Q. gg(1:l)=matmul(a(i,1:l),a(1:l,1:l)) a(1:l,1:l)=a(1:l,1:l)-outerprod(a(1:l,i),gg(1:l)) end if d(i)=a(i,i) a(i,i)=1.0 Reset row and column of a to idena(i,1:l)=0.0 tity matrix for next iteration. a(1:l,i)=0.0 else d(i)=a(i,i) end if end do END SUBROUTINE tred2
Chapter B11. Eigensystems
1229
The routine tqli is intrinsically serial. A parallel replacement based on a divide and conquer algorithm has been proposed [1,2]. The idea is to split the tridiagonal matrix recursively into two tridiagonal matrices of
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
§11.2. d is a vector of length N . On input, its elements are the diagonal elements of the tridiagonal matrix. On output, it returns the eigenvalues. The vector e inputs the subdiagonal elements of the tridiagonal matrix, with e(1) arbitrary. On output e is destroyed. When finding only the eigenvalues, the optional argument z is omitted. If the eigenvectors of a tridiagonal matrix are desired, the N × N matrix z is input as the identity matrix. If the eigenvectors of a matrix that has been reduced by tred2 are required, then z is input as the matrix output by tred2. In either case, the kth column of z returns the normalized eigenvector corresponding to d(k). INTEGER(I4B) :: i,iter,l,m,n,ndum REAL(SP) :: b,c,dd,f,g,p,r,s REAL(SP), DIMENSION(size(e)) :: ff n=assert_eq(size(d),size(e),’tqli: n’) if (present(z)) ndum=assert_eq(n,size(z,1),size(z,2),’tqli: ndum’) e(:)=eoshift(e(:),1) Convenient to renumber the elements of do l=1,n e. iter=0 iterate: do do m=l,n-1 Look for a single small subdiagonal eledd=abs(d(m))+abs(d(m+1)) ment to split the matrix. if (abs(e(m))+dd == dd) exit end do if (m == l) exit iterate if (iter == 30) call nrerror(’too many iterations in tqli’) iter=iter+1 g=(d(l+1)-d(l))/(2.0_sp*e(l)) Form shift. r=pythag(g,1.0_sp) g=d(m)-d(l)+e(l)/(g+sign(r,g)) This is dm − ks . s=1.0 c=1.0 p=0.0 do i=m-1,l,-1 A plane rotation as in the original QL, f=s*e(i) followed by Givens rotations to reb=c*e(i) store tridiagonal form. r=pythag(f,g) e(i+1)=r if (r == 0.0) then Recover from underflow. d(i+1)=d(i+1)-p e(m)=0.0 cycle iterate end if s=f/r c=g/r g=d(i+1)-p r=(d(i)-g)*s+2.0_sp*c*b p=s*r d(i+1)=g+p g=c*r-b if (present(z)) then Form eigenvectors. ff(1:n)=z(1:n,i+1) z(1:n,i+1)=s*z(1:n,i)+c*ff(1:n) z(1:n,i)=c*z(1:n,i)-s*ff(1:n) end if end do d(l)=d(l)-p e(l)=g e(m)=0.0 end do iterate end do END SUBROUTINE tqli
1230
Chapter B11.
Eigensystems
f90
This routine uses z as an optional argument that is required only if eigenvectors are being found as well as eigenvalues.
iterate: do
See discussion of named do loops after simplx on p. 1219.
SUBROUTINE balanc(a) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a REAL(SP), PARAMETER :: RADX=radix(a),SQRADX=RADX**2 Given an N × N matrix a, this routine replaces it by a balanced matrix with identical eigenvalues. A symmetric matrix is already balanced and is unaffected by this procedure. The parameter RADX is the machine’s floating-point radix. INTEGER(I4B) :: i,last,ndum REAL(SP) :: c,f,g,r,s ndum=assert_eq(size(a,1),size(a,2),’balanc’) do last=1 do i=1,size(a,1) Calculate row and column norms. c=sum(abs(a(:,i)))-a(i,i) r=sum(abs(a(i,:)))-a(i,i) if (c /= 0.0 .and. r /= 0.0) then If both are nonzero, g=r/RADX f=1.0 s=c+r do find the integer power of the maif (c >= g) exit chine radix that comes closest to f=f*RADX balancing the matrix. c=c*SQRADX end do g=r*RADX do if (c j + 1 are to be thought of as zero, but are returned with random values. INTEGER(I4B) :: i,m,n REAL(SP) :: x REAL(SP), DIMENSION(size(a,1)) :: y n=assert_eq(size(a,1),size(a,2),’elmhes’) do m=2,n-1 m is called r + 1 in the text. i=imaxloc(abs(a(m:n,m-1)))+m-1 Find the pivot. x=a(i,m-1) if (i /= m) then Interchange rows and columns. call swap(a(i,m-1:n),a(m,m-1:n)) call swap(a(:,i),a(:,m)) end if if (x /= 0.0) then Carry out the elimination. y(m+1:n)=a(m+1:n,m-1)/x a(m+1:n,m-1)=y(m+1:n) a(m+1:n,m:n)=a(m+1:n,m:n)-outerprod(y(m+1:n),a(m,m:n)) a(:,m)=a(:,m)+matmul(a(:,m+1:n),y(m+1:n)) end if end do END SUBROUTINE elmhes
If the four lines of code starting here were all coded for a serial machine in a single do-loop starting with do i=m+1,n (see Volume 1), it would pay to test whether y was zero because the next three lines could then be skipped for that value of i. There is no convenient way to do this here, even with a where, since the shape of the arrays on each of the three lines is different. For a parallel machine it is probably best just to do a few unnecessary multiplies and skip the test for zero values of y.
f90
y(m+1:n)=...
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Fortran 90 provides a nice collection of numeric inquiry intrinsic functions. Here we find the machine’s floating-point radix. Note that only the type of the argument a affects the returned function value. REAL(SP), PARAMETER :: RADX=radix(a)...
1232
Chapter B11.
Eigensystems
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE hqr(a,wr,wi) USE nrtype; USE nrutil, ONLY : assert_eq,diagadd,nrerror,upper_triangle IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(OUT) :: wr,wi REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a Finds all eigenvalues of an N × N upper Hessenberg matrix a. On input a can be exactly as output from elmhes §11.5; on output it is destroyed. The real and imaginary parts of the N eigenvalues are returned in wr and wi, respectively. INTEGER(I4B) :: i,its,k,l,m,n,nn,mnnk REAL(SP) :: anorm,p,q,r,s,t,u,v,w,x,y,z REAL(SP), DIMENSION(size(a,1)) :: pp n=assert_eq(size(a,1),size(a,2),size(wr),size(wi),’hqr’) anorm=sum(abs(a),mask=upper_triangle(n,n,extra=2)) Compute matrix norm for possible use in locating single small subdiagonal element. nn=n t=0.0 Gets changed only by an exceptional shift. do Begin search for next eigenvalue: “Do while if (nn < 1) exit nn >= 1”. its=0 iterate: do Begin iteration. do l=nn,2,-1 Look for single small subdiagonal element. s=abs(a(l-1,l-1))+abs(a(l,l)) if (s == 0.0) s=anorm if (abs(a(l,l-1))+s == s) exit end do x=a(nn,nn) if (l == nn) then One root found. wr(nn)=x+t wi(nn)=0.0 nn=nn-1 exit iterate Go back for next eigenvalue. end if y=a(nn-1,nn-1) w=a(nn,nn-1)*a(nn-1,nn) if (l == nn-1) then Two roots found . . . p=0.5_sp*(y-x) q=p**2+w z=sqrt(abs(q)) x=x+t if (q >= 0.0) then . . . a real pair . . . z=p+sign(z,p) wr(nn)=x+z wr(nn-1)=wr(nn) if (z /= 0.0) wr(nn)=x-w/z wi(nn)=0.0 wi(nn-1)=0.0 else . . . a complex pair. wr(nn)=x+p wr(nn-1)=wr(nn) wi(nn)=z wi(nn-1)=-z end if nn=nn-2 exit iterate Go back for next eigenvalue. end if No roots found. Continue iteration. if (its == 30) call nrerror(’too many iterations in hqr’) if (its == 10 .or. its == 20) then Form exceptional shift. t=t+x call diagadd(a(1:nn,1:nn),-x) s=abs(a(nn,nn-1))+abs(a(nn-1,nn-2)) x=0.75_sp*s y=x w=-0.4375_sp*s**2
Chapter B11. Eigensystems
1233
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
end if its=its+1 do m=nn-2,l,-1 Form shift and then look for 2 consecuz=a(m,m) tive small subdiagonal elements. r=x-z s=y-z p=(r*s-w)/a(m+1,m)+a(m,m+1) Equation (11.6.23). q=a(m+1,m+1)-z-r-s r=a(m+2,m+1) s=abs(p)+abs(q)+abs(r) Scale to prevent overflow or underflow. p=p/s q=q/s r=r/s if (m == l) exit u=abs(a(m,m-1))*(abs(q)+abs(r)) v=abs(p)*(abs(a(m-1,m-1))+abs(z)+abs(a(m+1,m+1))) if (u+v == v) exit Equation (11.6.26). end do do i=m+2,nn a(i,i-2)=0.0 if (i /= m+2) a(i,i-3)=0.0 end do do k=m,nn-1 Double QR step on rows l to nn and if (k /= m) then columns m to nn. p=a(k,k-1) Begin setup of Householder vector. q=a(k+1,k-1) r=0.0 if (k /= nn-1) r=a(k+2,k-1) x=abs(p)+abs(q)+abs(r) if (x /= 0.0) then p=p/x Scale to prevent overflow or underflow. q=q/x r=r/x end if end if s=sign(sqrt(p**2+q**2+r**2),p) if (s /= 0.0) then if (k == m) then if (l /= m) a(k,k-1)=-a(k,k-1) else a(k,k-1)=-s*x end if p=p+s Equations (11.6.24). x=p/s y=q/s z=r/s q=q/p r=r/p Ready for row modification. pp(k:nn)=a(k,k:nn)+q*a(k+1,k:nn) if (k /= nn-1) then pp(k:nn)=pp(k:nn)+r*a(k+2,k:nn) a(k+2,k:nn)=a(k+2,k:nn)-pp(k:nn)*z end if a(k+1,k:nn)=a(k+1,k:nn)-pp(k:nn)*y a(k,k:nn)=a(k,k:nn)-pp(k:nn)*x mnnk=min(nn,k+3) Column modification. pp(l:mnnk)=x*a(l:mnnk,k)+y*a(l:mnnk,k+1) if (k /= nn-1) then pp(l:mnnk)=pp(l:mnnk)+z*a(l:mnnk,k+2) a(l:mnnk,k+2)=a(l:mnnk,k+2)-pp(l:mnnk)*r end if a(l:mnnk,k+1)=a(l:mnnk,k+1)-pp(l:mnnk)*q a(l:mnnk,k)=a(l:mnnk,k)-pp(l:mnnk) end if
1234
Chapter B11.
end do end do iterate end do END SUBROUTINE hqr
Go back for next iteration on current eigenvalue.
See the discussion of upper triangle after jacobi on p. 1226. Setting extra=2 here picks out the upper Hessenberg part of the matrix.
anorm=sum(abs(a),mask=upper_triangle(n,n,extra=2)
iterate: do We use a named loop to improve the readability and structuring of the routine. The if-blocks that test for one or two roots end with exit iterate, transferring control back to the outermost loop and thus starting a search for the next root. call diagadd... The routines that operate on the diagonal of a matrix are collected in nrutil partly so you can write clear code and partly in the hope that compiler writers will provide parallel library routines. Fortran 90 does not provide convenient parallel access to the diagonal of a matrix.
CITED REFERENCES AND FURTHER READING: Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press), §8.6 and references therein. [1] Sorensen, D.C., and Tang, P.T.P. 1991, SIAM Journal on Numerical Analysis, vol. 28, pp. 1752– 1775. [2] Lo, S.-S., Philippe, B., and Sameh, A. 1987, SIAM Journal on Scientific and Statistical Computing, vol. 8, pp. s155–s165. [3]
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
f90
Eigensystems
Fast Fourier Transform
The algorithms underlying the parallel routines in this chapter are described in §22.4. As described there, the basic building block is a routine for simultaneously taking the FFT of each row of a two-dimensional matrix: SUBROUTINE fourrow_sp(data,isign) USE nrtype; USE nrutil, ONLY : assert,swap IMPLICIT NONE COMPLEX(SPC), DIMENSION(:,:), INTENT(INOUT) :: data INTEGER(I4B), INTENT(IN) :: isign Replaces each row (constant first index) of data(1:M ,1:N ) by its discrete Fourier transform (transform on second index), if isign is input as 1; or replaces each row of data by N times its inverse discrete Fourier transform, if isign is input as −1. N must be an integer power of 2. Parallelism is M -fold on the first index of data. INTEGER(I4B) :: n,i,istep,j,m,mmax,n2 REAL(DP) :: theta COMPLEX(SPC), DIMENSION(size(data,1)) :: temp COMPLEX(DPC) :: w,wp Double precision for the trigonometric recurrences. COMPLEX(SPC) :: ws n=size(data,2) call assert(iand(n,n-1)==0, ’n must be a power of 2 in fourrow_sp’) n2=n/2 j=n2 This is the bit-reversal section of the routine. do i=1,n-2 if (j > i) call swap(data(:,j+1),data(:,i+1)) m=n2 do if (m < 2 .or. j < m) exit j=j-m m=m/2 end do j=j+m end do mmax=1 Here begins the Danielson-Lanczos section of the routine. do Outer loop executed log2 N times. if (n i) call swap(data(:,j+1),data(:,i+1)) m=n2 do if (m < 2 .or. j < m) exit j=j-m m=m/2 end do j=j+m end do mmax=1 do if (n i) call swap(data(:,:,j+1),data(:,:,i+1)) m=n2 do if (m < 2 .or. j < m) exit j=j-m m=m/2 end do j=j+m end do mmax=1 Here begins the Danielson-Lanczos section of the routine. do Outer loop executed log2 N times. if (n i) call swap(data(j+1,:),data(i+1,:)) m=n2 do if (m < 2 .or. j < m) exit j=j-m m=m/2 end do j=j+m end do mmax=1 Here begins the Danielson-Lanczos section of the routine. do Outer loop executed log2 N times. if (n i) call swap(data(j+1,:,:),data(i+1,:,:)) m=n2 do if (m < 2 .or. j < m) exit j=j-m m=m/2 end do j=j+m end do mmax=1 Here begins the Danielson-Lanczos section of the routine. do Outer loop executed log2 N times. if (n zdata Use zdata as cdata. if (isign == 1) cdata=cmplx(data(1:n-1:2),data(2:n:2),kind=spc) else allocate(cdata(n/2)) Have to allocate storage ourselves. cdata=cmplx(data(1:n-1:2),data(2:n:2),kind=spc) end if if (isign == 1) then c2=-0.5_sp call four1(cdata,+1) The forward transform is here. else Otherwise set up for an inverse transc2=0.5_sp form. end if w=zroots_unity(sign(n,isign),n/4) w=cmplx(-aimag(w),real(w),kind=spc) h1=c1*(cdata(2:nq)+conjg(cdata(nh:nq+2:-1))) The two separate transforms are seph2=c2*(cdata(2:nq)-conjg(cdata(nh:nq+2:-1))) arated out of cdata. Next they are recombined to form the true transform of the original real data: cdata(2:nq)=h1+w(2:nq)*h2 cdata(nh:nq+2:-1)=conjg(h1-w(2:nq)*h2) z=cdata(1) Squeeze the first and last data toif (isign == 1) then gether to get them all within the cdata(1)=cmplx(real(z)+aimag(z),real(z)-aimag(z),kind=spc) original array. else cdata(1)=cmplx(c1*(real(z)+aimag(z)),c1*(real(z)-aimag(z)),kind=spc) call four1(cdata,-1) This is the inverse transform for the end if case isign=-1. if (present(zdata)) then Ship out answer in data if required. if (isign /= 1) then data(1:n-1:2)=real(cdata) data(2:n:2)=aimag(cdata) end if else data(1:n-1:2)=real(cdata) data(2:n:2)=aimag(cdata) deallocate(cdata) end if
1244
Chapter B12.
Fast Fourier Transform
END SUBROUTINE realft_sp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE realft_dp(data,isign,zdata) USE nrtype; USE nrutil, ONLY : assert,assert_eq,zroots_unity USE nr, ONLY : four1 IMPLICIT NONE REAL(DP), DIMENSION(:), INTENT(INOUT) :: data INTEGER(I4B), INTENT(IN) :: isign COMPLEX(DPC), DIMENSION(:), OPTIONAL, TARGET :: zdata INTEGER(I4B) :: n,ndum,nh,nq COMPLEX(DPC), DIMENSION(size(data)/4) :: w COMPLEX(DPC), DIMENSION(size(data)/4-1) :: h1,h2 COMPLEX(DPC), DIMENSION(:), POINTER :: cdata COMPLEX(DPC) :: z REAL(DP) :: c1=0.5_dp,c2 n=size(data) call assert(iand(n,n-1)==0, ’n must be a power of 2 in realft_dp’) nh=n/2 nq=n/4 if (present(zdata)) then ndum=assert_eq(n/2,size(zdata),’realft_dp’) cdata=>zdata if (isign == 1) cdata=cmplx(data(1:n-1:2),data(2:n:2),kind=spc) else allocate(cdata(n/2)) cdata=cmplx(data(1:n-1:2),data(2:n:2),kind=spc) end if if (isign == 1) then c2=-0.5_dp call four1(cdata,+1) else c2=0.5_dp end if w=zroots_unity(sign(n,isign),n/4) w=cmplx(-aimag(w),real(w),kind=dpc) h1=c1*(cdata(2:nq)+conjg(cdata(nh:nq+2:-1))) h2=c2*(cdata(2:nq)-conjg(cdata(nh:nq+2:-1))) cdata(2:nq)=h1+w(2:nq)*h2 cdata(nh:nq+2:-1)=conjg(h1-w(2:nq)*h2) z=cdata(1) if (isign == 1) then cdata(1)=cmplx(real(z)+aimag(z),real(z)-aimag(z),kind=dpc) else cdata(1)=cmplx(c1*(real(z)+aimag(z)),c1*(real(z)-aimag(z)),kind=dpc) call four1(cdata,-1) end if if (present(zdata)) then if (isign /= 1) then data(1:n-1:2)=real(cdata) data(2:n:2)=aimag(cdata) end if else data(1:n-1:2)=real(cdata) data(2:n:2)=aimag(cdata) deallocate(cdata) end if END SUBROUTINE realft_dp
Chapter B12.
Fast Fourier Transform
1245
SUBROUTINE cosft1(y) USE nrtype; USE nrutil, ONLY : assert,cumsum,zroots_unity USE nr, ONLY : realft IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: y Calculates the cosine transform of a set of N +1 real-valued data points y. The transformed data replace the original data in array y. N must be a power of 2. This program, without changes, also calculates the inverse cosine transform, but in this case the output array should be multiplied by 2/N . COMPLEX(SPC), DIMENSION((size(y)-1)/2) :: w REAL(SP), DIMENSION((size(y)-1)/2-1) :: y1,y2 REAL(SP) :: summ INTEGER(I4B) :: n,nh n=size(y)-1 call assert(iand(n,n-1)==0, ’n must be a power of 2 in cosft1’) nh=n/2 w=zroots_unity(n+n,nh) summ=0.5_sp*(y(1)-y(n+1)) y(1)=0.5_sp*(y(1)+y(n+1)) y1=0.5_sp*(y(2:nh)+y(n:nh+2:-1)) Construct the two pieces of the auxiliary array. y2=y(2:nh)-y(n:nh+2:-1) summ=summ+sum(real(w(2:nh))*y2) Carry along this sum for later use in unfolding y2=y2*aimag(w(2:nh)) the transform. y(2:nh)=y1-y2 Calculate the auxiliary function. y(n:nh+2:-1)=y1+y2 call realft(y(1:n),1) Calculate the transform of the auxiliary function. y(n+1)=y(2) y(2)=summ summ is the value of F1 in equation (12.3.21). y(2:n:2)=cumsum(y(2:n:2)) Equation (12.3.20). END SUBROUTINE cosft1
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE sinft(y) USE nrtype; USE nrutil, ONLY : assert,cumsum,zroots_unity USE nr, ONLY : realft IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: y Calculates the sine transform of a set of N real-valued data points stored in array y. The number N must be a power of 2. On exit y is replaced by its transform. This program, without changes, also calculates the inverse sine transform, but in this case the output array should be multiplied by 2/N . REAL(SP), DIMENSION(size(y)/2+1) :: wi REAL(SP), DIMENSION(size(y)/2) :: y1,y2 INTEGER(I4B) :: n,nh n=size(y) call assert(iand(n,n-1)==0, ’n must be a power of 2 in sinft’) nh=n/2 wi=aimag(zroots_unity(n+n,nh+1)) Calculate the sine for the auxiliary array. y(1)=0.0 y1=wi(2:nh+1)*(y(2:nh+1)+y(n:nh+1:-1)) Construct the two pieces of the auxiliary array. y2=0.5_sp*(y(2:nh+1)-y(n:nh+1:-1)) Put them together to make the auxiliary ary(2:nh+1)=y1+y2 ray. y(n:nh+1:-1)=y1-y2 call realft(y,+1) Transform the auxiliary array. y(1)=0.5_sp*y(1) Initialize the sum used for odd terms. y(2)=0.0 y1=cumsum(y(1:n-1:2)) Odd terms are determined by this running sum. y(1:n-1:2)=y(2:n:2) Even terms in the transform are determined diy(2:n:2)=y1 rectly. END SUBROUTINE sinft
1246
Chapter B12.
Fast Fourier Transform
SUBROUTINE four3(data,isign) USE nrtype USE nr, ONLY : fourrow_3d IMPLICIT NONE COMPLEX(SPC), DIMENSION(:,:,:), INTENT(INOUT) :: data INTEGER(I4B), INTENT(IN) :: isign Replaces a 3-d complex array data by its discrete 3-d Fourier transform, if isign is input as 1; or replaces data by its inverse 3-d discrete Fourier transform times the product of its
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE cosft2(y,isign) USE nrtype; USE nrutil, ONLY : assert,cumsum,zroots_unity USE nr, ONLY : realft IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: y INTEGER(I4B), INTENT(IN) :: isign Calculates the “staggered” cosine transform of a set of N real-valued data points y. The transformed data replace the original data in array y. N must be a power of 2. Set isign to +1 for a transform, and to −1 for an inverse transform. For an inverse transform, the output array should be multiplied by 2/N . COMPLEX(SPC), DIMENSION(size(y)) :: w REAL(SP), DIMENSION(size(y)/2) :: y1,y2 REAL(SP) :: ytemp INTEGER(I4B) :: n,nh n=size(y) call assert(iand(n,n-1)==0, ’n must be a power of 2 in cosft2’) nh=n/2 w=zroots_unity(4*n,n) if (isign == 1) then Forward transform. y1=0.5_sp*(y(1:nh)+y(n:nh+1:-1)) Calculate the auxiliary function. y2=aimag(w(2:n:2))*(y(1:nh)-y(n:nh+1:-1)) y(1:nh)=y1+y2 y(n:nh+1:-1)=y1-y2 call realft(y,1) Calculate transform of the auxiliary function. y1(1:nh-1)=y(3:n-1:2)*real(w(3:n-1:2)) & Even terms. -y(4:n:2)*aimag(w(3:n-1:2)) y2(1:nh-1)=y(4:n:2)*real(w(3:n-1:2)) & +y(3:n-1:2)*aimag(w(3:n-1:2)) y(3:n-1:2)=y1(1:nh-1) y(4:n:2)=y2(1:nh-1) ytemp=0.5_sp*y(2) Initialize recurrence for odd terms with 12 RN/2 . y(n-2:2:-2)=cumsum(y(n:4:-2),ytemp) Recurrence for odd terms. y(n)=ytemp else if (isign == -1) then Inverse transform. ytemp=y(n) y(4:n:2)=y(2:n-2:2)-y(4:n:2) Form difference of odd terms. y(2)=2.0_sp*ytemp y1(1:nh-1)=y(3:n-1:2)*real(w(3:n-1:2)) & Calculate Rk and Ik . +y(4:n:2)*aimag(w(3:n-1:2)) y2(1:nh-1)=y(4:n:2)*real(w(3:n-1:2)) & -y(3:n-1:2)*aimag(w(3:n-1:2)) y(3:n-1:2)=y1(1:nh-1) y(4:n:2)=y2(1:nh-1) call realft(y,-1) y1=y(1:nh)+y(n:nh+1:-1) Invert auxiliary array. y2=(0.5_sp/aimag(w(2:n:2)))*(y(1:nh)-y(n:nh+1:-1)) y(1:nh)=0.5_sp*(y1+y2) y(n:nh+1:-1)=0.5_sp*(y1-y2) end if END SUBROUTINE cosft2
Chapter B12. Fast Fourier Transform
1247
f90
The reshape intrinsic, used with an order= parameter, is the multidimensional generalization of the two-dimensional transpose operation. The line
dat2=reshape(data,shape=shape(dat2),order=(/3,1,2/))
is equivalent to the do-loop do j=1,size(data,1) dat2(:,:,j)=data(j,:,:) end do
Incidentally, we have found some Fortran 90 compilers that (for scalar machines) are significantly slower executing the reshape than executing the equivalent do-loop. This, of course, shouldn’t happen, since the reshape basically is an implicit do-loop. If you find such inefficient behavior on your compiler, you should report it as a bug to your compiler vendor! (Only thus will Fortran 90 compilers be brought to mature states of efficiency.) SUBROUTINE four3_alt(data,isign) USE nrtype USE nr, ONLY : fourcol_3d IMPLICIT NONE COMPLEX(SPC), DIMENSION(:,:,:), INTENT(INOUT) :: data INTEGER(I4B), INTENT(IN) :: isign Replaces a 3-d complex array data by its discrete 2-d Fourier transform, if isign is input as 1; or replaces data by its inverse 3-d discrete Fourier transform times the product of its three sizes, if isign is input as −1. All three of data’s sizes must be integer powers of 2 (this is checked for in fourcol 3d). Parallelism is by use of fourcol 3d. (Use this version only if fourcol 3d is faster than fourrow 3d on your machine.) COMPLEX(SPC), DIMENSION(:,:,:), ALLOCATABLE :: dat2,dat3 call fourcol_3d(data,isign) Transform in first dimension. allocate(dat2(size(data,2),size(data,3),size(data,1))) dat2=reshape(data,shape=shape(dat2),order=(/3,1,2/)) Transpose. call fourcol_3d(dat2,isign) Transform in (original) second dimension. allocate(dat3(size(data,3),size(data,1),size(data,2))) dat3=reshape(dat2,shape=shape(dat3),order=(/3,1,2/)) Transpose. deallocate(dat2) call fourcol_3d(dat3,isign) Transform in (original) third dimension. data=reshape(dat3,shape=shape(data),order=(/3,1,2/)) Transpose back to output ordeallocate(dat3) der. END SUBROUTINE four3_alt
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
three sizes, if isign is input as −1. All three of data’s sizes must be integer powers of 2 (this is checked for in fourrow 3d). Parallelism is by use of fourrow 3d. COMPLEX(SPC), DIMENSION(:,:,:), ALLOCATABLE :: dat2,dat3 call fourrow_3d(data,isign) Transform in third dimension. allocate(dat2(size(data,2),size(data,3),size(data,1))) dat2=reshape(data,shape=shape(dat2),order=(/3,1,2/)) Transpose. call fourrow_3d(dat2,isign) Transform in (original) first dimension. allocate(dat3(size(data,3),size(data,1),size(data,2))) dat3=reshape(dat2,shape=shape(dat3),order=(/3,1,2/)) Transpose. deallocate(dat2) call fourrow_3d(dat3,isign) Transform in (original) second dimension. data=reshape(dat3,shape=shape(data),order=(/3,1,2/)) Transpose back to output ordeallocate(dat3) der. END SUBROUTINE four3
1248
Chapter B12.
Fast Fourier Transform
Note that four3 uses fourrow 3d, the three-dimensional counterpart of fourrow, while four3 alt uses fourcol 3d, the three-dimensional counterpart of fourcol. You may want to time these programs to see which is faster on your machine.
f90
SUBROUTINE rlft2(data,spec,speq,isign) USE nrtype; USE nrutil, ONLY : assert,assert_eq USE nr, ONLY : four2 REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: data COMPLEX(SPC), DIMENSION(:,:), INTENT(INOUT) :: spec COMPLEX(SPC), DIMENSION(:), INTENT(INOUT) :: speq INTEGER(I4B), INTENT(IN) :: isign Given a two-dimensional real array data(1:M ,1:N ), this routine returns (for isign=1) the complex fast Fourier transform as two complex arrays: On output, spec(1:M/2,1:N ) contains the zero and positive frequency values of the first frequency component, while speq(1:N ) contains the Nyquist critical frequency values of the first frequency component. The second frequency components are stored for zero, positive, and negative frequencies, in standard wrap-around order. For isign=-1, the inverse transform (times M × N/2 as a constant multiplicative factor) is performed, with output data deriving from input spec and speq. For inverse transforms on data not generated first by a forward transform, make sure the complex input data array satisfies property (12.5.2). The size of all arrays must always be integer powers of 2. INTEGER :: i1,j1,nn1,nn2 REAL(DP) :: theta COMPLEX(SPC) :: c1=(0.5_sp,0.0_sp),c2,h1,h2,w COMPLEX(SPC), DIMENSION(size(data,2)-1) :: h1a,h2a COMPLEX(DPC) :: ww,wp nn1=assert_eq(size(data,1),2*size(spec,1),’rlft2: nn1’) nn2=assert_eq(size(data,2),size(spec,2),size(speq),’rlft2: nn2’) call assert(iand((/nn1,nn2/),(/nn1,nn2/)-1)==0, & ’dimensions must be powers of 2 in rlft2’) c2=cmplx(0.0_sp,-0.5_sp*isign,kind=spc) theta=TWOPI_D/(isign*nn1) wp=cmplx(-2.0_dp*sin(0.5_dp*theta)**2,sin(theta),kind=spc) if (isign == 1) then Case of forward transform. spec(:,:)=cmplx(data(1:nn1:2,:),data(2:nn1:2,:),kind=spc) call four2(spec,isign) Here is where most all of the compute time speq=spec(1,:) is spent. end if h1=c1*(spec(1,1)+conjg(speq(1))) h1a=c1*(spec(1,2:nn2)+conjg(speq(nn2:2:-1))) h2=c2*(spec(1,1)-conjg(speq(1))) h2a=c2*(spec(1,2:nn2)-conjg(speq(nn2:2:-1))) spec(1,1)=h1+h2 spec(1,2:nn2)=h1a+h2a speq(1)=conjg(h1-h2) speq(nn2:2:-1)=conjg(h1a-h2a) ww=cmplx(1.0_dp,0.0_dp,kind=dpc) Initialize trigonometric recurrence. do i1=2,nn1/4+1 j1=nn1/2-i1+2 Corresponding negative frequency. ww=ww*wp+ww Do the trig recurrence. w=ww h1=c1*(spec(i1,1)+conjg(spec(j1,1))) Equation (12.3.5). h1a=c1*(spec(i1,2:nn2)+conjg(spec(j1,nn2:2:-1)))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
In Volume 1, a single routine named rlft3 was able to serve both as a three-dimensional real FFT, and as a two-dimensional real FFT. The trick is that the Fortran 77 version doesn’t care whether the input array data is dimensioned as two- or three-dimensional. Fortran 90 is not so indifferent, and better programming practice is to have two separate versions of the algorithm:
Chapter B12.
Fast Fourier Transform
1249
Here an overloaded version of assert that takes vector arguments is used to check that each dimension is a power of 2. Note that iand acts element-byelement on an array.
f90
call assert(iand((/nn1,nn2/),(/nn1,nn2/)-1)==0 ...
SUBROUTINE rlft3(data,spec,speq,isign) USE nrtype; USE nrutil, ONLY : assert,assert_eq USE nr, ONLY : four3 REAL(SP), DIMENSION(:,:,:), INTENT(INOUT) :: data COMPLEX(SPC), DIMENSION(:,:,:), INTENT(INOUT) :: spec COMPLEX(SPC), DIMENSION(:,:), INTENT(INOUT) :: speq INTEGER(I4B), INTENT(IN) :: isign Given a three-dimensional real array data(1:L,1:M ,1:N ), this routine returns (for isign=1) the complex Fourier transform as two complex arrays: On output, the zero and positive frequency values of the first frequency component are in spec(1:L/2,1:M ,1:N ), while speq(1:M ,1:N ) contains the Nyquist critical frequency values of the first frequency component. The second and third frequency components are stored for zero, positive, and negative frequencies, in standard wrap-around order. For isign=-1, the inverse transform (times L × M × N/2 as a constant multiplicative factor) is performed, with output data deriving from input spec and speq. For inverse transforms on data not generated first by a forward transform, make sure the complex input data array satisfies property (12.5.2). The size of all arrays must always be integer powers of 2. INTEGER :: i1,i3,j1,j3,nn1,nn2,nn3 REAL(DP) :: theta COMPLEX(SPC) :: c1=(0.5_sp,0.0_sp),c2,h1,h2,w COMPLEX(SPC), DIMENSION(size(data,2)-1) :: h1a,h2a COMPLEX(DPC) :: ww,wp c2=cmplx(0.0_sp,-0.5_sp*isign,kind=spc) nn1=assert_eq(size(data,1),2*size(spec,1),’rlft2: nn1’) nn2=assert_eq(size(data,2),size(spec,2),size(speq,1),’rlft2: nn2’) nn3=assert_eq(size(data,3),size(spec,3),size(speq,2),’rlft2: nn3’) call assert(iand((/nn1,nn2,nn3/),(/nn1,nn2,nn3/)-1)==0, & ’dimensions must be powers of 2 in rlft3’) theta=TWOPI_D/(isign*nn1) wp=cmplx(-2.0_dp*sin(0.5_dp*theta)**2,sin(theta),kind=dpc) if (isign == 1) then Case of forward transform. spec(:,:,:)=cmplx(data(1:nn1:2,:,:),data(2:nn1:2,:,:),kind=spc) call four3(spec,isign) Here is where most all of the compute time speq=spec(1,:,:) is spent. end if do i3=1,nn3 j3=1 if (i3 /= 1) j3=nn3-i3+2 h1=c1*(spec(1,1,i3)+conjg(speq(1,j3))) h1a=c1*(spec(1,2:nn2,i3)+conjg(speq(nn2:2:-1,j3))) h2=c2*(spec(1,1,i3)-conjg(speq(1,j3))) h2a=c2*(spec(1,2:nn2,i3)-conjg(speq(nn2:2:-1,j3))) spec(1,1,i3)=h1+h2 spec(1,2:nn2,i3)=h1a+h2a
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
h2=c2*(spec(i1,1)-conjg(spec(j1,1))) h2a=c2*(spec(i1,2:nn2)-conjg(spec(j1,nn2:2:-1))) spec(i1,1)=h1+w*h2 spec(i1,2:nn2)=h1a+w*h2a spec(j1,1)=conjg(h1-w*h2) spec(j1,nn2:2:-1)=conjg(h1a-w*h2a) end do if (isign == -1) then Case of reverse transform. call four2(spec,isign) data(1:nn1:2,:)=real(spec) data(2:nn1:2,:)=aimag(spec) end if END SUBROUTINE rlft2
1250
Chapter B12.
Fast Fourier Transform
Referring back √ to the discussion of parallelism, §22.4, that led to four1’s implementation with N parallelism, you might wonder whether Fortran 90 provides sufficiently powerful high-level constructs to enable an FFT routine with N -fold parallelism. The answer is, “It does, but you wouldn’t want to use them!” Access to arbitrary interprocessor communication in Fortran 90 is through the mechanism of the “vector subscript” (one-dimensional array of indices in arbitrary order). When a vector subscript is on the right-hand side of an assignment statement, the operation performed is effectively a “gather”; when it is on the left-hand side, the operation is effectively a “scatter.” It is quite possible to write the classic FFT algorithm in terms of gather and scatter operations. In fact, we do so now. The problem is efficiency: The computations involved in constructing the vector subscripts for the scatter/gather operations, and the actual scatter/gather operations themselves, tend to swamp the underlying very lean FFT algorithm. The result is very slow, though theoretically perfectly parallelizable, code. Since small-scale parallel (SSP) machines can saturate their processors with √ N parallelism, while massively multiprocessor (MMP) machines inevitably come with architecture-optimized FFT library calls, there is really no niche for these routines, except as pedagogical demonstrations. We give here a one-dimensional routine, and also an arbitrary-dimensional routine modeled on Volume 1’s fourn. Note the complete absence of do-loops of size N ; the loops that remain are over log N stages, or over the number of dimensions. SUBROUTINE four1_gather(data,isign) USE nrtype; USE nrutil, ONLY : arth,assert IMPLICIT NONE COMPLEX(SPC), DIMENSION(:), INTENT(INOUT) :: data INTEGER(I4B), INTENT(IN) :: isign Replaces a complex array data by its discrete Fourier transform, if isign is input as 1; or replaces data by size(data) times its inverse discrete Fourier transform, if isign is input as −1. The size of data must be an integer power of 2. This routine demonstrates coding the FFT algorithm in high-level Fortran 90 constructs. Generally the result is very
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
speq(1,j3)=conjg(h1-h2) speq(nn2:2:-1,j3)=conjg(h1a-h2a) ww=cmplx(1.0_dp,0.0_dp,kind=dpc) Initialize trigonometric recurrence. do i1=2,nn1/4+1 j1=nn1/2-i1+2 Corresponding negative frequency. ww=ww*wp+ww Do the trig recurrence. w=ww h1=c1*(spec(i1,1,i3)+conjg(spec(j1,1,j3))) Equation (12.3.5). h1a=c1*(spec(i1,2:nn2,i3)+conjg(spec(j1,nn2:2:-1,j3))) h2=c2*(spec(i1,1,i3)-conjg(spec(j1,1,j3))) h2a=c2*(spec(i1,2:nn2,i3)-conjg(spec(j1,nn2:2:-1,j3))) spec(i1,1,i3)=h1+w*h2 spec(i1,2:nn2,i3)=h1a+w*h2a spec(j1,1,j3)=conjg(h1-w*h2) spec(j1,nn2:2:-1,j3)=conjg(h1a-w*h2a) end do end do if (isign == -1) then Case of reverse transform. call four3(spec,isign) data(1:nn1:2,:,:)=real(spec) data(2:nn1:2,:,:)=aimag(spec) end if END SUBROUTINE rlft3
Chapter B12.
Fast Fourier Transform
1251
SUBROUTINE fourn_gather(data,nn,isign) USE nrtype; USE nrutil, ONLY : arth,assert IMPLICIT NONE COMPLEX(SPC), DIMENSION(:), INTENT(INOUT) :: data INTEGER(I4B), DIMENSION(:) :: nn INTEGER(I4B), INTENT(IN) :: isign For data a one-dimensional complex array containing the values (in Fortran normal ordering) of an M -dimensional complex arrray, this routine replaces data by its M -dimensional discrete Fourier transform, if isign is input as 1. nn(1:M ) is an integer array containing the lengths of each dimension (number of complex values), each of which must be a power of 2. If isign is input as −1, data is replaced by its inverse transform times the product of the lengths of all dimensions. This routine demonstrates coding the multidimensional FFT algorithm in high-level Fortran 90 constructs. Generally the result is very much slower than
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
much slower than library routines coded for specific architectures, and also significantly slower than the parallelization-by-rows method used in the routine four1. INTEGER(I4B) :: n,n2,m,mm REAL(DP) :: theta COMPLEX(SPC) :: wp INTEGER(I4B), DIMENSION(size(data)) :: jarr INTEGER(I4B), DIMENSION(:), ALLOCATABLE :: jrev COMPLEX(SPC), DIMENSION(:), ALLOCATABLE :: wtab,dtemp n=size(data) call assert(iand(n,n-1)==0, ’n must be a power of 2 in four1_gather’) if (n = n) exit mm=mm/2 theta=PI_D/(isign*m) Ready for trigonometry? wp=cmplx(-2.0_dp*sin(0.5_dp*theta)**2, sin(theta),kind=spc) Add entries to the table for the next iteration. wtab(mm+1:n2:2*mm)=wtab(1:n2-mm:2*mm)*wp+wtab(1:n2-mm:2*mm) end do deallocate(dtemp,wtab) END SUBROUTINE four1_gather
1252
Chapter B12.
Fast Fourier Transform
f90
Once again the vector version of assert is used to test all the dimensions stored in nn simultaneously.
call assert(iand(nn,nn-1)==0 ...
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
library routines coded for specific architectures, and significantly slower than routines four2 and four3 for the two- and three-dimensional cases. INTEGER(I4B), DIMENSION(:), ALLOCATABLE :: jarr INTEGER(I4B) :: ndim,idim,ntot,nprev,n,n2,msk0,msk1,msk2,m,mm,mn REAL(DP) :: theta COMPLEX(SPC) :: wp COMPLEX(SPC), DIMENSION(:), ALLOCATABLE :: wtab,dtemp call assert(iand(nn,nn-1)==0, & ’each dimension must be a power of 2 in fourn_gather’) ndim=size(nn) ntot=product(nn) nprev=1 allocate(jarr(ntot)) do idim=1,ndim Loop over the dimensions. jarr=arth(0,1,ntot) We begin the bit-reversal section of the n=nn(idim) routine. n2=n/2 msk0=nprev msk1=nprev*n2 msk2=msk0+msk1 do Construct an array of pointers from an if (msk1 = n) exit mn=m*nprev mm=mm/2 theta=PI_D/(isign*m) Ready for trigonometry? wp=cmplx(-2.0_dp*sin(0.5_dp*theta)**2,sin(theta),kind=dpc) Add entries to the table for the next iteration. wtab(mm+1:n2:2*mm)=wtab(1:n2-mm:2*mm)*wp & +wtab(1:n2-mm:2*mm) end do deallocate(dtemp,wtab) nprev=n*nprev end do deallocate(jarr) END SUBROUTINE fourn_gather
FUNCTION convlv(data,respns,isign) USE nrtype; USE nrutil, ONLY : assert,nrerror USE nr, ONLY : realft IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: data REAL(SP), DIMENSION(:), INTENT(IN) :: respns INTEGER(I4B), INTENT(IN) :: isign REAL(SP), DIMENSION(size(data)) :: convlv Convolves or deconvolves a real data set data (of length N , including any user-supplied zero padding) with a response function respns, stored in wrap-around order in a real array of length M ≤ N . (M should be an odd integer, N a power of 2.) Wrap-around order means that the first half of the array respns contains the impulse response function at positive times, while the second half of the array contains the impulse response function at negative times, counting down from the highest element respns(M ). On input isign is +1 for convolution, −1 for deconvolution. The answer is returned as the function convlv, an array of length N . data has INTENT(INOUT) for consistency with realft, but is actually unchanged. INTEGER(I4B) :: no2,n,m COMPLEX(SPC), DIMENSION(size(data)/2) :: tmpd,tmpr n=size(data) m=size(respns) call assert(iand(n,n-1)==0, ’n must be a power of 2 in convlv’) call assert(mod(m,2)==1, ’m must be odd in convlv’) convlv(1:m)=respns(:) Put respns in array of length n. convlv(n-(m-3)/2:n)=convlv((m+3)/2:m) convlv((m+3)/2:n-(m-1)/2)=0.0 Pad with zeros. no2=n/2 call realft(data,1,tmpd) FFT both arrays. call realft(convlv,1,tmpr) if (isign == 1) then Multiply FFTs to convolve. tmpr(1)=cmplx(real(tmpd(1))*real(tmpr(1))/no2, & aimag(tmpd(1))*aimag(tmpr(1))/no2, kind=spc) tmpr(2:)=tmpd(2:)*tmpr(2:)/no2 else if (isign == -1) then Divide FFTs to deconvolve. if (any(abs(tmpr(2:)) == 0.0) .or. real(tmpr(1)) == 0.0 & .or. aimag(tmpr(1)) == 0.0) call nrerror & (’deconvolving at response zero in convlv’) tmpr(1)=cmplx(real(tmpd(1))/real(tmpr(1))/no2, & aimag(tmpd(1))/aimag(tmpr(1))/no2, kind=spc) tmpr(2:)=tmpd(2:)/tmpr(2:)/no2 else call nrerror(’no meaning for isign in convlv’) end if call realft(convlv,-1,tmpr) Inverse transform back to time domain. END FUNCTION convlv
1253
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B13. Fourier and Spectral Applications
1254
Chapter B13.
Fourier and Spectral Applications
The intrinsic function cmplx returns a quantity of type default complex unless the kind argument is present. It is therefore a good idea always to include this argument. The intrinsic functions real and aimag, on the other hand, when called with a complex argument, return the same kind as their argument. So it is a good idea not to put in a kind argment for these. (In fact, aimag doesn’t allow one.) Don’t confuse these situations, regarding complex variables, with the completely unrelated use of real to convert a real or integer variable to a real value of specified kind. In this latter case, kind should be specified.
f90
tmpr(1)=cmplx(...kind=spc)
FUNCTION correl(data1,data2) USE nrtype; USE nrutil, ONLY : assert,assert_eq USE nr, ONLY : realft IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: data1,data2 REAL(SP), DIMENSION(size(data1)) :: correl Computes the correlation of two real data sets data1 and data2 of length N (including any user-supplied zero padding). N must be an integer power of 2. The answer is returned as the function correl, an array of length N . The answer is stored in wraparound order, i.e., correlations at increasingly negative lags are in correl(N ) on down to correl(N/2 + 1), while correlations at increasingly positive lags are in correl(1) (zero lag) on up to correl(N/2). Sign convention of this routine: if data1 lags data2, i.e., is shifted to the right of it, then correl will show a peak at positive lags. COMPLEX(SPC), DIMENSION(size(data1)/2) :: cdat1,cdat2 INTEGER(I4B) :: no2,n Normalization for inverse FFT. n=assert_eq(size(data1),size(data2),’correl’) call assert(iand(n,n-1)==0, ’n must be a power of 2 in correl’) no2=n/2 call realft(data1,1,cdat1) Transform both data vectors. call realft(data2,1,cdat2) cdat1(1)=cmplx(real(cdat1(1))*real(cdat2(1))/no2, & Multiply to find FFT of their aimag(cdat1(1))*aimag(cdat2(1))/no2, kind=spc) correlation. cdat1(2:)=cdat1(2:)*conjg(cdat2(2:))/no2 call realft(correl,-1,cdat1) Inverse transform gives correlation. END FUNCTION correl
f90
See just above for why we use the explicit kind type parameter spc for cmplx, but omit sp for real.
cdat1(1)=cmplx(...kind=spc)
SUBROUTINE spctrm(p,k,ovrlap,unit,n_window) USE nrtype; USE nrutil, ONLY : arth,nrerror USE nr, ONLY : four1 IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(OUT) :: p INTEGER(I4B), INTENT(IN) :: k LOGICAL(LGT), INTENT(IN) :: ovrlap True for overlapping segments, false otherINTEGER(I4B), OPTIONAL, INTENT(IN) :: n_window,unit wise. Reads data from input unit 9, or if the optional argument unit is present, from that input unit. The output is an array p of length M that contains the data’s power (mean square amplitude) at frequency (j − 1)/2M cycles per grid point, for j = 1, 2, . . . , M , based on (2*k+1)*M data points (if ovrlap is set .true.) or 4*k*M data points (if ovrlap is set .false.). The number of segments of the data is 2*k in both cases: The routine calls four1 k times, each call with 2 partitions each of 2M real data points. If the optional argument n window is present, the routine uses the Bartlett window, the square window,
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B13. Fourier and Spectral Applications
1255
FUNCTION window(j,facm,facp,nn_window) IMPLICIT NONE INTEGER(I4B), DIMENSION(:), INTENT(IN) :: j INTEGER(I4B), INTENT(IN) :: nn_window REAL(SP), INTENT(IN) :: facm,facp REAL(SP), DIMENSION(size(j)) :: window select case(nn_window) case(1)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
or the Welch window for n window = 1, 2, 3 respectively. If n window is not present, the Bartlett window is used. INTEGER(I4B) :: j,joff,joffn,kk,m,m4,m43,m44,mm,iunit,nn_window REAL(SP) :: den,facm,facp,sumw REAL(SP), DIMENSION(2*size(p)) :: w REAL(SP), DIMENSION(4*size(p)) :: w1 REAL(SP), DIMENSION(size(p)) :: w2 COMPLEX(SPC), DIMENSION(2*size(p)) :: cw1 m=size(p) if (present(n_window)) then nn_window=n_window else nn_window=1 end if if (present(unit)) then iunit=unit else iunit=9 end if mm=m+m Useful factors. m4=mm+mm m44=m4+4 m43=m4+3 den=0.0 facm=m Factors used by the window function. facp=1.0_sp/m w1(1:mm)=window(arth(1,1,mm),facm,facp,nn_window) sumw=dot_product(w1(1:mm),w1(1:mm)) Accumulate the squared sum of the weights. p(:)=0.0 Initialize the spectrum to zero. if (ovrlap) read (iunit,*) (w2(j),j=1,m) Initialize the “save” half-buffer. do kk=1,k Loop over data segments in groups of two. do joff=-1,0,1 Get two complete segments into workspace. if (ovrlap) then w1(joff+2:joff+mm:2)=w2(1:m) read (iunit,*) (w2(j),j=1,m) joffn=joff+mm w1(joffn+2:joffn+mm:2)=w2(1:m) else read (iunit,*) (w1(j),j=joff+2,m4,2) end if end do w=window(arth(1,1,mm),facm,facp,nn_window) Apply the window to the data. w1(2:m4:2)=w1(2:m4:2)*w w1(1:m4:2)=w1(1:m4:2)*w cw1(1:mm)=cmplx(w1(1:m4:2),w1(2:m4:2),kind=spc) call four1(cw1(1:mm),1) Fourier transform the windowed data. w1(1:m4:2)=real(cw1(1:mm)) w1(2:m4:2)=aimag(cw1(1:mm)) p(1)=p(1)+w1(1)**2+w1(2)**2 Sum results into previous segments. p(2:m)=p(2:m)+w1(4:2*m:2)**2+w1(3:2*m-1:2)**2+& w1(m44-4:m44-2*m:-2)**2+w1(m43-4:m43-2*m:-2)**2 den=den+sumw end do p(:)=p(:)/(m4*den) Normalize the output. CONTAINS
1256
Chapter B13.
Fourier and Spectral Applications
Bartlett window. Square window. Welch window. spctrm’)
The Fortran 90 optional argument feature allows us to make unit 9 the default output unit in this routine, but leave the user the option of specifying a different output unit by supplying an actual argument for unit. We also use an optional argument to allow the user the option of overriding the default selection of the Bartlett window function.
f90
FUNCTION window(j,facm,facp,nn_window) In Fortran 77 we coded this as a statement function. Here the internal function is equivalent, but allows full specification of the interface and so is preferred.
SUBROUTINE memcof(data,xms,d) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(OUT) :: xms REAL(SP), DIMENSION(:), INTENT(IN) :: data REAL(SP), DIMENSION(:), INTENT(OUT) :: d Given a real vector data of length N , this routine returns M linear prediction coefficients in a vector d of length M , and returns the mean square discrepancy as xms. INTEGER(I4B) :: k,m,n REAL(SP) :: denom,pneum REAL(SP), DIMENSION(size(data)) :: wk1,wk2,wktmp REAL(SP), DIMENSION(size(d)) :: wkm m=size(d) n=size(data) xms=dot_product(data,data)/n wk1(1:n-1)=data(1:n-1) wk2(1:n-1)=data(2:n) do k=1,m pneum=dot_product(wk1(1:n-k),wk2(1:n-k)) denom=dot_product(wk1(1:n-k),wk1(1:n-k))+ & dot_product(wk2(1:n-k),wk2(1:n-k)) d(k)=2.0_sp*pneum/denom xms=xms*(1.0_sp-d(k)**2) d(1:k-1)=wkm(1:k-1)-d(k)*wkm(k-1:1:-1) The algorithm is recursive, although it is implemented as an iteration. It builds up the answer for larger and larger values of m until the desired value is reached. At this point in the algorithm, one could return the vector d and scalar xms for a set of LP coefficients with k (rather than m) terms. if (k == m) RETURN wkm(1:k)=d(1:k) wktmp(2:n-k)=wk1(2:n-k) wk1(1:n-k-1)=wk1(1:n-k-1)-wkm(k)*wk2(1:n-k-1) wk2(1:n-k-1)=wk2(2:n-k)-wkm(k)*wktmp(2:n-k) end do call nrerror(’never get here in memcof’) END SUBROUTINE memcof
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
window(j)=(1.0_sp-abs(((j-1)-facm)*facp)) case(2) window(j)=1.0 case(3) window(j)=(1.0_sp-(((j-1)-facm)*facp)**2) case default call nrerror(’unimplemented window function in end select END FUNCTION window END SUBROUTINE spctrm
Chapter B13.
Fourier and Spectral Applications
1257
f90
a(m+1)=cmplx(1.0_sp,kind=spc) See after convlv on p. 1254 to review why we use the explicit kind type parameter spc for cmplx.
FUNCTION predic(data,d,nfut) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: data,d INTEGER(I4B), INTENT(IN) :: nfut REAL(SP), DIMENSION(nfut) :: predic Given an array data, and given the data’s LP coefficients d in an array of length M , this routine applies equation (13.6.11) to predict the next nfut data points, which it returns in an array as the function value predic. Note that the routine references only the last M values of data, as initial values for the prediction. INTEGER(I4B) :: j,ndata,m REAL(SP) :: discrp,sm REAL(SP), DIMENSION(size(d)) :: reg m=size(d) ndata=size(data) reg(1:m)=data(ndata:ndata+1-m:-1) do j=1,nfut discrp=0.0 This is where you would put in a known discrepancy if you were reconstructing a function by linear predictive coding rather than extrapolating a function by linear prediction. See text. sm=discrp+dot_product(d,reg) reg=eoshift(reg,-1,sm) [If you want to implement circular arrays, you can predic(j)=sm avoid this shifting of coefficients!] end do END FUNCTION predic
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE fixrts(d) USE nrtype USE nr, ONLY : zroots IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: d Given the LP coefficients d, this routine finds all roots of the characteristic polynomial (13.6.14), reflects any roots that are outside the unit circle back inside, and then returns a modified set of coefficients in d. INTEGER(I4B) :: i,m LOGICAL(LGT) :: polish COMPLEX(SPC), DIMENSION(size(d)+1) :: a COMPLEX(SPC), DIMENSION(size(d)) :: roots m=size(d) a(m+1)=cmplx(1.0_sp,kind=spc) Set up complex coefficients for polynomial a(m:1:-1)=cmplx(-d(1:m),kind=spc) root finder. polish=.true. call zroots(a(1:m+1),roots,polish) Find all the roots. where (abs(roots) > 1.0) roots=1.0_sp/conjg(roots) Reflect all roots outside the unit circle back inside. a(1)=-roots(1) Now reconstruct the polynomial coefficients, a(2:m+1)=cmplx(1.0_sp,kind=spc) do i=2,m by looping over the roots a(2:i)=a(1:i-1)-roots(i)*a(2:i) and synthetically multiplying. a(1)=-roots(i)*a(1) end do d(m:1:-1)=-real(a(1:m)) The polynomial coefficients are guaranteed END SUBROUTINE fixrts to be real, so we need only return the real part as new LP coefficients.
1258
Chapter B13.
Fourier and Spectral Applications
zz=...poly(z,d) The poly function in nrutil returns the value of the polynomial with coefficients d(:) at z. Here a version that takes real coefficients and a complex argument is actually invoked, but all the different versions have been overloaded onto the same name poly.
f90
SUBROUTINE period(x,y,ofac,hifac,px,py,jmax,prob) USE nrtype; USE nrutil, ONLY : assert_eq,imaxloc USE nr, ONLY : avevar IMPLICIT NONE INTEGER(I4B), INTENT(OUT) :: jmax REAL(SP), INTENT(IN) :: ofac,hifac REAL(SP), INTENT(OUT) :: prob REAL(SP), DIMENSION(:), INTENT(IN) :: x,y REAL(SP), DIMENSION(:), POINTER :: px,py Input is a set of N data points with abscissas x (which need not be equally spaced) and ordinates y, and a desired oversampling factor ofac (a typical value being 4 or larger). The routine returns pointers to internally allocated arrays px and py. px is filled with an increasing sequence of frequencies (not angular frequencies) up to hifac times the “average” Nyquist frequency, and py is filled with the values of the Lomb normalized periodogram at those frequencies. The length of these arrays is 0.5*ofac*hifac*N . The arrays x and y are not altered. The routine also returns jmax such that py(jmax) is the maximum element in py, and prob, an estimate of the significance of that maximum against the hypothesis of random noise. A small value of prob indicates that a significant periodic signal is present. INTEGER(I4B) :: i,n,nout REAL(SP) :: ave,cwtau,effm,expy,pnow,sumc,sumcy,& sums,sumsh,sumsy,swtau,var,wtau,xave,xdif,xmax,xmin REAL(DP), DIMENSION(size(x)) :: tmp1,tmp2,wi,wpi,wpr,wr LOGICAL(LGT), SAVE :: init=.true. n=assert_eq(size(x),size(y),’period’) if (init) then init=.false. nullify(px,py) else if (associated(px)) deallocate(px) if (associated(py)) deallocate(py) end if nout=0.5_sp*ofac*hifac*n allocate(px(nout),py(nout)) call avevar(y(:),ave,var) Get mean and variance of the input data. xmax=maxval(x(:)) Go through data to get the range of abscisxmin=minval(x(:)) sas. xdif=xmax-xmin
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION evlmem(fdt,d,xms) USE nrtype; USE nrutil, ONLY : poly IMPLICIT NONE REAL(SP), INTENT(IN) :: fdt,xms REAL(SP), DIMENSION(:), INTENT(IN) :: d REAL(SP) :: evlmem Given d and xms as returned by memcof, this function returns the power spectrum estimate P (f ) as a function of fdt = f ∆. COMPLEX(SPC) :: z,zz REAL(DP) :: theta Trigonometric recurrences in double precision. theta=TWOPI_D*fdt z=cmplx(cos(theta),sin(theta),kind=spc) zz=1.0_sp-z*poly(z,d) evlmem=xms/abs(zz)**2 Equation (13.7.4). END FUNCTION evlmem
Chapter B13. Fourier and Spectral Applications
1259
This routine shows another example of how to return arrays whose size is not known in advance (cf. zbrac in Chapter B9). The coding is explained in the subsection on pointers in §21.5. The size of the output arrays, nout in the code, is available as size(px).
f90
jmax=imaxloc...
See discussion of imaxloc on p. 1017.
SUBROUTINE fasper(x,y,ofac,hifac,px,py,jmax,prob) USE nrtype; USE nrutil, ONLY : arth,assert_eq,imaxloc,nrerror USE nr, ONLY : avevar,realft IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y REAL(SP), INTENT(IN) :: ofac,hifac INTEGER(I4B), INTENT(OUT) :: jmax REAL(SP), INTENT(OUT) :: prob REAL(SP), DIMENSION(:), POINTER :: px,py INTEGER(I4B), PARAMETER :: MACC=4 Input is a set of N data points with abscissas x (which need not be equally spaced) and ordinates y, and a desired oversampling factor ofac (a typical value being 4 or larger). The routine returns pointers to internally allocated arrays px and py. px is filled with an increasing sequence of frequencies (not angular frequencies) up to hifac times the “average” Nyquist frequency, and py is filled with the values of the Lomb normalized periodogram at those frequencies. The length of these arrays is 0.5*ofac*hifac*N . The arrays x and y are not altered. The routine also returns jmax such that py(jmax) is the maximum element in py, and prob, an estimate of the significance of that maximum against the hypothesis of random noise. A small value of prob indicates that a significant
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
xave=0.5_sp*(xmax+xmin) pnow=1.0_sp/(xdif*ofac) Starting frequency. tmp1(:)=TWOPI_D*((x(:)-xave)*pnow) Initialize values for the trigonometric recurwpr(:)=-2.0_dp*sin(0.5_dp*tmp1)**2 rences at each data point. The recurwpi(:)=sin(tmp1(:)) rences are done in double precision. wr(:)=cos(tmp1(:)) wi(:)=wpi(:) do i=1,nout Main loop over the frequencies to be evalupx(i)=pnow ated. sumsh=dot_product(wi,wr) First, loop over the data to get τ and related sumc=dot_product(wr(:)-wi(:),wr(:)+wi(:)) quantities. wtau=0.5_sp*atan2(2.0_sp*sumsh,sumc) swtau=sin(wtau) cwtau=cos(wtau) tmp1(:)=wi(:)*cwtau-wr(:)*swtau Then, loop over the data again to get the tmp2(:)=wr(:)*cwtau+wi(:)*swtau periodogram value. sums=dot_product(tmp1,tmp1) sumc=dot_product(tmp2,tmp2) sumsy=dot_product(y(:)-ave,tmp1) sumcy=dot_product(y(:)-ave,tmp2) tmp1(:)=wr(:) Update the trigonometric recurrences. wr(:)=(wr(:)*wpr(:)-wi(:)*wpi(:))+wr(:) wi(:)=(wi(:)*wpr(:)+tmp1(:)*wpi(:))+wi(:) py(i)=0.5_sp*(sumcy**2/sumc+sumsy**2/sums)/var pnow=pnow+1.0_sp/(ofac*xdif) The next frequency. end do jmax=imaxloc(py(1:nout)) expy=exp(-py(jmax)) Evaluate statistical significance of the maxieffm=2.0_sp*nout/ofac mum. prob=effm*expy if (prob > 0.01_sp) prob=1.0_sp-(1.0_sp-expy)**effm END SUBROUTINE period
1260
Chapter B13.
Fourier and Spectral Applications
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
periodic signal is present. Parameter: MACC is the number of interpolation points per 1/4 cycle of highest frequency. INTEGER(I4B) :: j,k,n,ndim,nfreq,nfreqt,nout REAL(SP) :: ave,ck,ckk,cterm,cwt,den,df,effm,expy,fac,fndim,hc2wt,& hs2wt,hypo,sterm,swt,var,xdif,xmax,xmin REAL(SP), DIMENSION(:), ALLOCATABLE :: wk1,wk2 LOGICAL(LGT), SAVE :: init=.true. n=assert_eq(size(x),size(y),’fasper’) if (init) then init=.false. nullify(px,py) else if (associated(px)) deallocate(px) if (associated(py)) deallocate(py) end if nfreqt=ofac*hifac*n*MACC nfreq=64 do Size the FFT as next power of 2 above nfreqt. if (nfreq >= nfreqt) exit nfreq=nfreq*2 end do ndim=2*nfreq allocate(wk1(ndim),wk2(ndim)) call avevar(y(1:n),ave,var) Compute the mean, variance, and range of the data. xmax=maxval(x(:)) xmin=minval(x(:)) xdif=xmax-xmin wk1(1:ndim)=0.0 Zero the workspaces. wk2(1:ndim)=0.0 fac=ndim/(xdif*ofac) fndim=ndim do j=1,n Extirpolate the data into the workspaces. ck=1.0_sp+mod((x(j)-xmin)*fac,fndim) ckk=1.0_sp+mod(2.0_sp*(ck-1.0_sp),fndim) call spreadval(y(j)-ave,wk1,ck,MACC) call spreadval(1.0_sp,wk2,ckk,MACC) end do call realft(wk1(1:ndim),1) Take the fast Fourier transforms. call realft(wk2(1:ndim),1) df=1.0_sp/(xdif*ofac) nout=0.5_sp*ofac*hifac*n allocate(px(nout),py(nout)) k=3 do j=1,nout Compute the Lomb value for each frequency. hypo=sqrt(wk2(k)**2+wk2(k+1)**2) hc2wt=0.5_sp*wk2(k)/hypo hs2wt=0.5_sp*wk2(k+1)/hypo cwt=sqrt(0.5_sp+hc2wt) swt=sign(sqrt(0.5_sp-hc2wt),hs2wt) den=0.5_sp*n+hc2wt*wk2(k)+hs2wt*wk2(k+1) cterm=(cwt*wk1(k)+swt*wk1(k+1))**2/den sterm=(cwt*wk1(k+1)-swt*wk1(k))**2/(n-den) px(j)=j*df py(j)=(cterm+sterm)/(2.0_sp*var) k=k+2 end do deallocate(wk1,wk2) jmax=imaxloc(py(1:nout)) expy=exp(-py(jmax)) Estimate significance of largest peak value. effm=2.0_sp*nout/ofac prob=effm*expy if (prob > 0.01_sp) prob=1.0_sp-(1.0_sp-expy)**effm CONTAINS
Chapter B13. Fourier and Spectral Applications
1261
This routine shows another example of how to return arrays whose size is not known in advance (cf. zbrac in Chapter B9). The coding is explained in the subsection on pointers in §21.5. The size of the output arrays, nout in the code, is available as size(px).
f90
jmax=imaxloc...
See discussion of imaxloc on p. 1017.
if (x == real(ix,sp)) then Without the explicit kind type parameter sp, real returns a value of type default real for an integer argument. This prevents automatic conversion of the routine from single to double precision. Here all you have to do is redefine sp in nrtype to get double precision.
SUBROUTINE dftcor(w,delta,a,b,endpts,corre,corim,corfac) USE nrtype; USE nrutil, ONLY : assert IMPLICIT NONE REAL(SP), INTENT(IN) :: w,delta,a,b REAL(SP), INTENT(OUT) :: corre,corim,corfac REAL(SP), DIMENSION(:), INTENT(IN) :: endpts For an integral approximated by a discrete Fourier transform, this routine computes the correction factor that multiplies the DFT and the endpoint correction to be added. Input is the angular frequency w, stepsize delta, lower and upper limits of the integral a and b, while the array endpts of length 8 contains the first 4 and last 4 function values. The
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE spreadval(y,yy,x,m) IMPLICIT NONE REAL(SP), INTENT(IN) :: y,x REAL(SP), DIMENSION(:), INTENT(INOUT) :: yy INTEGER(I4B), INTENT(IN) :: m Given an array yy of length N , extirpolate (spread) a value y into m actual array elements that best approximate the “fictional” (i.e., possibly noninteger) array element number x. The weights used are coefficients of the Lagrange interpolating polynomial. INTEGER(I4B) :: ihi,ilo,ix,j,nden,n REAL(SP) :: fac INTEGER(I4B), DIMENSION(10) :: nfac = (/ & 1,1,2,6,24,120,720,5040,40320,362880 /) if (m > 10) call nrerror(’factorial table too small in spreadval’) n=size(yy) ix=x if (x == real(ix,sp)) then yy(ix)=yy(ix)+y else ilo=min(max(int(x-0.5_sp*m+1.0_sp),1),n-m+1) ihi=ilo+m-1 nden=nfac(m) fac=product(x-arth(ilo,1,m)) yy(ihi)=yy(ihi)+y*fac/(nden*(x-ihi)) do j=ihi-1,ilo,-1 nden=(nden/(j+1-ilo))*(j-ihi) yy(j)=yy(j)+y*fac/(nden*(x-j)) end do end if END SUBROUTINE spreadval END SUBROUTINE fasper
1262
Chapter B13.
Fourier and Spectral Applications
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
correction factor W (θ) is returned as corfac, while the real and imaginary parts of the endpoint correction are returned as corre and corim. REAL(SP) :: a0i,a0r,a1i,a1r,a2i,a2r,a3i,a3r,arg,c,cl,cr,s,sl,sr,t,& t2,t4,t6 REAL(DP) :: cth,ctth,spth2,sth,sth4i,stth,th,th2,th4,& tmth2,tth4i th=w*delta call assert(a < b, th >= 0.0, th = 0) then Wavelet transform. nn=n Start at largest hierarchy, do if (nn < 4) exit call wtstep(a(1:nn),isign) nn=nn/2 and work towards smallest. end do else Inverse wavelet transform. nn=4 Start at smallest hierarchy, do if (nn > n) exit call wtstep(a(1:nn),isign) nn=nn*2 and work towards largest. end do end if END SUBROUTINE wt1
Chapter B13. Fourier and Spectral Applications
1265
MODULE pwtcom USE nrtype INTEGER(I4B), SAVE :: ncof=0,ioff,joff These module variables communicate the REAL(SP), DIMENSION(:), ALLOCATABLE, SAVE :: cc,cr filter to pwt. END MODULE pwtcom
SUBROUTINE pwtset(n) USE nrtype; USE nrutil, ONLY : nrerror USE pwtcom IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n Initializing routine for pwt, here implementing the Daubechies wavelet filters with 4, 12, and 20 coefficients, as selected by the input value n. Further wavelet filters can be included in the obvious manner. This routine must be called (once) before the first use of pwt. (For the case n=4, the specific routine daub4 is considerably faster than pwt.) REAL(SP) :: sig REAL(SP), PARAMETER :: & c4(4)=(/& 0.4829629131445341_sp, 0.8365163037378079_sp, & 0.2241438680420134_sp,-0.1294095225512604_sp /), & c12(12)=(/& 0.111540743350_sp, 0.494623890398_sp, 0.751133908021_sp, & 0.315250351709_sp,-0.226264693965_sp,-0.129766867567_sp, & 0.097501605587_sp, 0.027522865530_sp,-0.031582039318_sp, & 0.000553842201_sp, 0.004777257511_sp,-0.001077301085_sp /), & c20(20)=(/& 0.026670057901_sp, 0.188176800078_sp, 0.527201188932_sp, & 0.688459039454_sp, 0.281172343661_sp,-0.249846424327_sp, & -0.195946274377_sp, 0.127369340336_sp, 0.093057364604_sp, & -0.071394147166_sp,-0.029457536822_sp, 0.033212674059_sp, & 0.003606553567_sp,-0.010733175483_sp, 0.001395351747_sp, & 0.001992405295_sp,-0.000685856695_sp,-0.000116466855_sp, & 0.000093588670_sp,-0.000013264203_sp /) if (allocated(cc)) deallocate(cc) if (allocated(cr)) deallocate(cr) allocate(cc(n),cr(n)) ncof=n ioff=-n/2 These values center the “support” of the wavelets at each level. Alternatively, the “peaks” of the wavelets can joff=-n/2 be approximately centered by the choices ioff=-2 sig=-1.0 and joff=-n+2. Note that daub4 and pwtset with select case(n) n=4 use different default centerings. case(4)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (isign >= 0) then Apply filter. wksp(1:nhm) = C0*a(1:n-3:2)+C1*a(2:n-2:2) & +C2*a(3:n-1:2)+C3*a(4:n:2) wksp(nh)=C0*a(n-1)+C1*a(n)+C2*a(1)+C3*a(2) wksp(nhp:n-1) = C3*a(1:n-3:2)-C2*a(2:n-2:2) & +C1*a(3:n-1:2)-C0*a(4:n:2) wksp(n)=C3*a(n-1)-C2*a(n)+C1*a(1)-C0*a(2) else Apply transpose filter. wksp(1)=C2*a(nh)+C1*a(n)+C0*a(1)+C3*a(nhp) wksp(2)=C3*a(nh)-C0*a(n)+C1*a(1)-C2*a(nhp) wksp(3:n-1:2) = C2*a(1:nhm)+C1*a(nhp:n-1) & +C0*a(2:nh)+C3*a(nh+2:n) wksp(4:n:2) = C3*a(1:nhm)-C0*a(nhp:n-1) & +C1*a(2:nh)-C2*a(nh+2:n) end if a(1:n)=wksp(1:n) END SUBROUTINE daub4
1266
Chapter B13.
Fourier and Spectral Applications
Here we need to have as global variables arrays whose dimensions are known only at run time. At first sight the situation is the same as with the module fminln in newt on p. 1197. If you review the discussion there and in §21.5, you will recall that there are two good ways to implement this: with allocatable arrays (“Method 1”) or with pointers (“Method 2”). There is a difference here that makes allocatable arrays simpler. We do not wish to deallocate the arrays on exiting pwtset. On the contrary, the values in cc and cr need to be preserved for use in pwt. Since allocatable arrays are born in the well-defined state of “not currently allocated,” we can declare the arrays here as
f90
REAL(SP), DIMENSION(:), ALLOCATABLE, SAVE :: cc,cr
and test whether they were used on a previous call with if (allocated(cc)) deallocate(cc) if (allocated(cr)) deallocate(cr)
We are then ready to allocate the new storage: allocate(cc(n),cr(n))
With pointers, we would need the additional machinery of nullifying the pointers on the initial call, since pointers are born in an undefined state (see §21.5). There is an additional important point in this example. The module variables need to be used by a “sibling” routine, pwt. We need to be sure that they do not become undefined when we exit pwtset. We could ensure this by putting a USE pwtcom in the main program that calls both pwtset and pwt, but it’s easy to forget to do this. It is preferable to put explicit SAVEs on all the module variables. SUBROUTINE pwt(a,isign) USE nrtype; USE nrutil, ONLY : arth,nrerror USE pwtcom IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: a INTEGER(I4B), INTENT(IN) :: isign Partial wavelet transform: applies an arbitrary wavelet filter to data vector a (for isign=1) or applies its transpose (for isign=-1). Used hierarchically by routines wt1 and wtn. The actual filter is determined by a preceding (and required) call to pwtset, which initializes the module pwtcom. REAL(SP), DIMENSION(size(a)) :: wksp INTEGER(I4B), DIMENSION(size(a)/2) :: jf,jr INTEGER(I4B) :: k,n,nh,nmod n=size(a) if (n < 4) RETURN if (ncof == 0) call nrerror(’pwt: must call pwtset before pwt’) nmod=ncof*n A positive constant equal to zero mod n.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
cc=c4 case(12) cc=c12 case(20) cc=c20 case default call nrerror(’unimplemented value n in pwtset’) end select cr(n:1:-1) = cc cr(n:1:-2) = -cr(n:1:-2) END SUBROUTINE pwtset
Chapter B13. Fourier and Spectral Applications
1267
SUBROUTINE wtn(a,nn,isign,wtstep) USE nrtype; USE nrutil, ONLY : arth,assert IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: a INTEGER(I4B), DIMENSION(:), INTENT(IN) :: nn INTEGER(I4B), INTENT(IN) :: isign INTERFACE SUBROUTINE wtstep(a,isign) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: a INTEGER(I4B), INTENT(IN) :: isign END SUBROUTINE wtstep END INTERFACE Replaces a by its N -dimensional discrete wavelet transform, if isign is input as 1. nn is an integer array of length N , containing the lengths of each dimension (number of real values), which must all be powers of 2. a is a real array of length equal to the product of these lengths, in which the data are stored as in a multidimensional real FORTRAN array. If isign is input as −1, a is replaced by its inverse wavelet transform. The subroutine wtstep, whose actual name must be supplied in calling this routine, is the underlying wavelet filter. Examples of wtstep are daub4 and (preceded by pwtset) pwt. INTEGER(I4B) :: i1,i2,i3,idim,n,ndim,nnew,nprev,nt,ntot REAL(SP), DIMENSION(:), ALLOCATABLE :: wksp call assert(iand(nn,nn-1)==0, ’each dimension must be a power of 2 in wtn’) allocate(wksp(maxval(nn))) ndim=size(nn) ntot=product(nn(:)) nprev=1 do idim=1,ndim Main loop over the dimensions. n=nn(idim) nnew=n*nprev if (n > 4) then do i2=0,ntot-1,nnew do i1=1,nprev i3=i1+i2 wksp(1:n)=a(arth(i3,nprev,n)) Copy the relevant row or column i3=i3+n*nprev or etc. into workspace. if (isign >= 0) then Do one-dimensional wavelet transnt=n form. do
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
nh=n/2 wksp(:)=0.0 jf=iand(n-1,arth(2+nmod+ioff,2,nh)) Use bitwise AND to wrap-around the pointjr=iand(n-1,arth(2+nmod+joff,2,nh)) ers. n-1 is a mask of all bits, since n is do k=1,ncof a power of 2. if (isign >= 0) then Apply filter. wksp(1:nh)=wksp(1:nh)+cc(k)*a(jf+1) wksp(nh+1:n)=wksp(nh+1:n)+cr(k)*a(jr+1) else Apply transpose filter. wksp(jf+1)=wksp(jf+1)+cc(k)*a(1:nh) wksp(jr+1)=wksp(jr+1)+cr(k)*a(nh+1:n) end if if (k == ncof) exit jf=iand(n-1,jf+1) jr=iand(n-1,jr+1) end do a(:)=wksp(:) Copy the results back from workspace. END SUBROUTINE pwt
Fourier and Spectral Applications Chapter B13.
1268
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (nt < 4) exit call wtstep(wksp(1:nt),isign) nt=nt/2 end do else Or inverse transform. nt=4 do if (nt > n) exit call wtstep(wksp(1:nt),isign) nt=nt*2 end do end if i3=i1+i2 a(arth(i3,nprev,n))=wksp(1:n) Copy back from workspace. i3=i3+n*nprev end do end do end if nprev=nnew end do deallocate(wksp) END SUBROUTINE wtn
SUBROUTINE moment(data,ave,adev,sdev,var,skew,curt) USE nrtype; USE nrutil, ONLY : nrerror IMPLICIT NONE REAL(SP), INTENT(OUT) :: ave,adev,sdev,var,skew,curt REAL(SP), DIMENSION(:), INTENT(IN) :: data Given an array of data, this routine returns its mean ave, average deviation adev, standard deviation sdev, variance var, skewness skew, and kurtosis curt. INTEGER(I4B) :: n REAL(SP) :: ep REAL(SP), DIMENSION(size(data)) :: p,s n=size(data) if (n var2) then Make F the ratio of the larger variance to the smaller one. f=var1/var2 df1=n1-1 df2=n2-1 else f=var2/var1 df1=n2-1 df2=n1-1 end if prob=2.0_sp*betai(0.5_sp*df2,0.5_sp*df1,df2/(df2+df1*f)) if (prob > 1.0) prob=2.0_sp-prob END SUBROUTINE ftest
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE tptest(data1,data2,t,prob) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : avevar,betai IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: data1,data2 REAL(SP), INTENT(OUT) :: t,prob Given the paired arrays data1 and data2 of the same length, this routine returns Student’s t for paired data as t, and its significance as prob, small values of prob indicating a significant difference of means. INTEGER(I4B) :: n REAL(SP) :: ave1,ave2,cov,df,sd,var1,var2 n=assert_eq(size(data1),size(data2),’tptest’) call avevar(data1,ave1,var1) call avevar(data2,ave2,var2) cov=dot_product(data1(:)-ave1,data2(:)-ave2) df=n-1 cov=cov/df sd=sqrt((var1+var2-2.0_sp*cov)/n) t=(ave1-ave2)/sd prob=betai(0.5_sp*df,0.5_sp,df/(df+t**2)) END SUBROUTINE tptest
1272
Chapter B14.
Statistical Description of Data
SUBROUTINE chstwo(bins1,bins2,knstrn,df,chsq,prob) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : gammq IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: knstrn REAL(SP), INTENT(OUT) :: df,chsq,prob REAL(SP), DIMENSION(:), INTENT(IN) :: bins1,bins2 Given the same-size arrays bins1 and bins2, containing two sets of binned data, and given the number of constraints knstrn (normally 1 or 0), this routine returns the number of degrees of freedom df, the chi-square chsq, and the significance prob. A small value of prob indicates a significant difference between the distributions bins1 and bins2. Note that bins1 and bins2 are both real arrays, although they will normally contain integer values. INTEGER(I4B) :: ndum LOGICAL(LGT), DIMENSION(size(bins1)) :: nzeromask ndum=assert_eq(size(bins1),size(bins2),’chstwo’) nzeromask = bins1(:) /= 0.0 .or. bins2(:) /= 0.0 chsq=sum((bins1(:)-bins2(:))**2/(bins1(:)+bins2(:)),mask=nzeromask) df=count(nzeromask)-knstrn No data means one less degree of freedom. prob=gammq(0.5_sp*df,0.5_sp*chsq) Chi-square probability function. See §6.2. END SUBROUTINE chstwo
We use the optional argument mask in sum to select out the elements to be summed over. In this case, at least one of the elements of bins1 or bins2 is not zero for each term in the sum.
f90
nzeromask=...chisq=sum(...mask=nzeromask)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE chsone(bins,ebins,knstrn,df,chsq,prob) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : gammq IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: knstrn REAL(SP), INTENT(OUT) :: df,chsq,prob REAL(SP), DIMENSION(:), INTENT(IN) :: bins,ebins Given the same-size arrays bins containing the observed numbers of events, and ebins containing the expected numbers of events, and given the number of constraints knstrn (normally one), this routine returns (trivially) the number of degrees of freedom df, and (nontrivially) the chi-square chsq and the significance prob. A small value of prob indicates a significant difference between the distributions bins and ebins. Note that bins and ebins are both real arrays, although bins will normally contain integer values. INTEGER(I4B) :: ndum ndum=assert_eq(size(bins),size(ebins),’chsone’) if (any(ebins(:) 0.0) & .or. (a1(j+1:n) < 0.0 .and. a2(j+1:n) < 0.0)) - & count((a1(j+1:n) > 0.0 .and. a2(j+1:n) < 0.0) & .or. (a1(j+1:n) < 0.0 .and. a2(j+1:n) > 0.0)) end do tau=real(is,sp)/sqrt(real(n1,sp)*real(n2,sp)) Equation (14.6.8). var=(4.0_sp*n+10.0_sp)/(9.0_sp*n*(n-1.0_sp)) Equation (14.6.9). z=tau/sqrt(var) prob=erfcc(abs(z)/SQRT2) Significance. END SUBROUTINE kendl1
SUBROUTINE kendl2(tab,tau,z,prob) USE nrtype; USE nrutil, ONLY : cumsum USE nr, ONLY : erfcc IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(IN) :: tab REAL(SP), INTENT(OUT) :: tau,z,prob Given a two-dimensional table tab such that tab(k, l) contains the number of events falling in bin k of one variable and bin l of another, this program returns Kendall’s τ as tau, its number of standard deviations from zero as z, and its two-sided significance level as prob. Small values of prob indicate a significant correlation (tau positive) or anticorrelation (tau
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
But we still need two passes through the mask array. The beauty of the array copy routine is that nties is determined from the size of the first argument, without the necessity for a second pass through the array.
1280
Chapter B14.
Statistical Description of Data
The underlying algorithm in kendl2 might seem to require looping over all pairs of cells in the two-dimensional table tab. Actually, however, clever use of the cumsum utility function reduces this to a simple loop over all the cells; moreover this “loop” parallelizes into a simple parallel product and call to the sum intrinsic. The basic idea is shown in the following table:
d
d
t
y
y
x
c
c
x
c
c
x
c
c
Relative to the cell marked t (which we use to denote the numerical value it contains), the cells marked d contribute to the “discordant” tally in Volume 1’s equation (14.6.8),
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
negative) between the two variables. Although tab is a real array, it will normally contain integral values. REAL(SP), DIMENSION(size(tab,1),size(tab,2)) :: cum,cumt INTEGER(I4B) :: i,j,ii,jj REAL(SP) :: sc,sd,en1,en2,points,var ii=size(tab,1) jj=size(tab,2) do i=1,ii Get cumulative sums leftward along cumt(i,jj:1:-1)=cumsum(tab(i,jj:1:-1)) rows. end do en2=sum(tab(1:ii,1:jj-1)*cumt(1:ii,2:jj)) Tally the extra-y pairs. do j=1,jj Get counts of points to lower-right cum(ii:1:-1,j)=cumsum(cumt(ii:1:-1,j)) of each cell in cum. end do points=cum(1,1) Total number of entries in table. sc=sum(tab(1:ii-1,1:jj-1)*cum(2:ii,2:jj)) Tally the concordant pairs. do j=1,jj Now get counts of points to uppercum(1:ii,j)=cumsum(cumt(1:ii,j)) right of each cell in cum, end do sd=sum(tab(2:ii,1:jj-1)*cum(1:ii-1,2:jj)) giving tally of discordant points. do j=1,jj Finally, get cumulative sums upward cumt(ii:1:-1,j)=cumsum(tab(ii:1:-1,j)) along columns, end do en1=sum(tab(1:ii-1,1:jj)*cumt(2:ii,1:jj)) giving the count of extra-x pairs, tau=(sc-sd)/sqrt((en1+sc+sd)*(en2+sc+sd)) and compute desired results. var=(4.0_sp*points+10.0_sp)/(9.0_sp*points*(points-1.0_sp)) z=tau/sqrt(var) prob=erfcc(abs(z)/SQRT2) END SUBROUTINE kendl2
1281
Chapter B14. Statistical Description of Data
concordant
=
tn
n
discordant
=
extra-x
=
n
extra-y
=
n
cm
lower right
tn
n
dm
upper right
tn
xm
below
tn
(B14.1)
ym
to the right
Here n varies over all the positions in the table, while the limits of the inner sums are relative to the position of n. (The letters t n , cm , dm , xm , ym all represent the value in a cell; we use different letters only to make the relation with the above table clear.) Now the final trick is to recognize that the inner sums, over cells to the loweror upper-right, below, and to the right can be done in parallel by cumulative sums (cumsum) sweeping to the right and up. The routine does these in a nonintuitive order merely to be able to reuse maximally the scratch spaces cum and cumt.
SUBROUTINE ks2d1s(x1,y1,quadvl,d1,prob) USE nrtype; USE nrutil, ONLY : assert_eq USE nr, ONLY : pearsn,probks,quadct IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x1,y1 REAL(SP), INTENT(OUT) :: d1,prob INTERFACE SUBROUTINE quadvl(x,y,fa,fb,fc,fd) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x,y REAL(SP), INTENT(OUT) :: fa,fb,fc,fd END SUBROUTINE quadvl END INTERFACE Two-dimensional Kolmogorov-Smirnov test of one sample against a model. Given the xand y-coordinates of a set of data points in arrays x1 and y1 of the same length, and given a user-supplied function quadvl that exemplifies the model, this routine returns the twodimensional K-S statistic as d1, and its significance level as prob. Small values of prob show that the sample is significantly different from the model. Note that the test is slightly distribution-dependent, so prob is only an estimate. INTEGER(I4B) :: j,n1 REAL(SP) :: dum,dumm,fa,fb,fc,fd,ga,gb,gc,gd,r1,rr,sqen n1=assert_eq(size(x1),size(y1),’ks2d1s’) d1=0.0
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
while the cells marked c contribute to the “concordant” tally. Likewise, the cells marked x and y contribute, respectively, to the “extra-x” and “extra-y” tallies. What about the cells left blank? Since we want to count pairs of cells only once, without duplication, these cells will be counted, relative to the location shown as t, when t itself moves into the blank-cell area. Symbolically we have
1282
Chapter B14.
Statistical Description of Data
SUBROUTINE quadct(x,y,xx,yy,fa,fb,fc,fd) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), INTENT(IN) :: x,y REAL(SP), DIMENSION(:), INTENT(IN) :: xx,yy REAL(SP), INTENT(OUT) :: fa,fb,fc,fd Given an origin (x, y), and an array of points with coordinates xx and yy, count how many of them are in each quadrant around the origin, and return the normalized fractions. Quadrants are labeled alphabetically, counterclockwise from the upper right. Used by ks2d1s and ks2d2s. INTEGER(I4B) :: na,nb,nc,nd,nn REAL(SP) :: ff nn=assert_eq(size(xx),size(yy),’quadct’) na=count(yy(:) > y .and. xx(:) > x) nb=count(yy(:) > y .and. xx(:) t Use temporary variable t to store weights. wt(:)=1.0_sp/(sig(:)**2) ss=sum(wt(:)) Accumulate sums with weights. sx=dot_product(wt,x) sy=dot_product(wt,y) else ndata=assert_eq(size(x),size(y),’fit’) ss=real(size(x),sp) Accumulate sums without weights. sx=sum(x) sy=sum(y) end if sxoss=sx/ss t(:)=x(:)-sxoss if (present(sig)) then t(:)=t(:)/sig(:) b=dot_product(t/sig,y) else b=dot_product(t,y) end if st2=dot_product(t,t) b=b/st2 Solve for a, b, σa , and σb . a=(sy-sx*b)/ss siga=sqrt((1.0_sp+sx*sx/(ss*st2))/ss) sigb=sqrt(1.0_sp/st2) t(:)=y(:)-a-b*x(:) q=1.0 if (present(sig)) then t(:)=t(:)/sig(:) chi2=dot_product(t,t) Calculate χ2 . if (ndata > 2) q=gammq(0.5_sp*(size(x)-2),0.5_sp*chi2) Equation (15.2.12). else chi2=dot_product(t,t)
1285
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B15.
1286
Chapter B15.
Modeling of Data
For unweighted data evaluate typical sig using chi2, and adjust the standard deviations.
sigdat=sqrt(chi2/(size(x)-2)) siga=siga*sigdat sigb=sigb*sigdat end if END SUBROUTINE fit
f90
SUBROUTINE fitexy(x,y,sigx,sigy,a,b,siga,sigb,chi2,q) USE nrtype; USE nrutil, ONLY : assert_eq,swap USE nr, ONLY : avevar,brent,fit,gammq,mnbrak,zbrent USE chixyfit IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y,sigx,sigy REAL(SP), INTENT(OUT) :: a,b,siga,sigb,chi2,q REAL(SP), PARAMETER :: POTN=1.571000_sp,BIG=1.0e30_sp,ACC=1.0e-3_sp Straight-line fit to input data x and y with errors in both x and y, the respective standard deviations being the input quantities sigx and sigy. x, y, sigx, and sigy are all arrays of the same length. Output quantities are a and b such that y = a + bx minimizes χ2 , whose value is returned as chi2. The χ2 probability is returned as q, a small value indicating a poor fit (sometimes indicating underestimated errors). Standard errors on a and b are returned as siga and sigb. These are not meaningful if either (i) the fit is poor, or (ii) b is so large that the data are consistent with a vertical (infinite b) line. If siga and sigb are returned as BIG, then the data are consistent with all values of b. INTEGER(I4B) :: j,n REAL(SP), DIMENSION(size(x)), TARGET :: xx,yy,sx,sy,ww REAL(SP), DIMENSION(6) :: ang,ch REAL(SP) :: amx,amn,varx,vary,scale,bmn,bmx,d1,d2,r2,& dum1,dum2,dum3,dum4,dum5 n=assert_eq(size(x),size(y),size(sigx),size(sigy),’fitexy’) xxp=>xx Set up communication with function chixy yyp=>yy through global variables in the module sxp=>sx chixyfit. syp=>sy wwp=>ww call avevar(x,dum1,varx) Find the x and y variances, and scale the call avevar(y,dum1,vary) data. scale=sqrt(varx/vary) xx(:)=x(:) yy(:)=y(:)*scale sx(:)=sigx(:) sy(:)=sigy(:)*scale ww(:)=sqrt(sx(:)**2+sy(:)**2) Use both x and y weights in first trial fit. call fit(xx,yy,dum1,b,dum2,dum3,dum4,dum5,ww) Trial fit for b. offs=0.0 ang(1)=0.0 Construct several angles for reference points. ang(2)=atan(b) Make b an angle. ang(4)=0.0 ang(5)=ang(2) ang(6)=POTN do j=4,6 ch(j)=chixy(ang(j))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
When standard deviations are supplied in sig, we need to compute the weights for the least squares fit in a temporary array wt. Later in the routine, we need another temporary array, which we call t to correspond to the variable in equation (15.2.15). It would be confusing to use the same name for both arrays. In Fortran 77 the arrays could share storage with an EQUIVALENCE declaration, but that is a deprecated feature in Fortran 90. We accomplish the same thing by making wt a pointer alias to t. REAL(SP), DIMENSION(:), POINTER :: wt...wt=>t
Chapter B15. Modeling of Data
1287
We need to pass arrays and other variables to chixy, but not as arguments. See §21.5 and the discussion of fminln on p. 1197 for two good ways to do this. The pointer construction here is analogous to the one used in fminln.
f90
USE chixyfit
MODULE chixyfit USE nrtype; USE nrutil, ONLY : nrerror REAL(SP), DIMENSION(:), POINTER :: xxp,yyp,sxp,syp,wwp REAL(SP) :: aa,offs CONTAINS FUNCTION chixy(bang) IMPLICIT NONE REAL(SP), INTENT(IN) :: bang REAL(SP) :: chixy REAL(SP), PARAMETER :: BIG=1.0e30_sp Captive function of fitexy, returns the value of (χ2 − offs) for the slope b=tan(bang). Scaled data and offs are communicated via the module chixyfit. REAL(SP) :: avex,avey,sumw,b if (.not. associated(wwp)) call nrerror("chixy: bad pointers") b=tan(bang) wwp(:)=(b*sxp(:))**2+syp(:)**2 where (wwp(:) < 1.0/BIG) wwp(:)=BIG elsewhere wwp(:)=1.0_sp/wwp(:) end where
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
end do call mnbrak(ang(1),ang(2),ang(3),ch(1),ch(2),ch(3),chixy) Bracket the χ2 minimum and then locate it with brent. chi2=brent(ang(1),ang(2),ang(3),chixy,ACC,b) chi2=chixy(b) a=aa q=gammq(0.5_sp*(n-2),0.5_sp*chi2) Compute χ2 probability. r2=1.0_sp/sum(ww(:)) Save inverse sum of weights at the minimum. bmx=BIG Now, find standard errors for b as points where bmn=BIG ∆χ2 = 1. offs=chi2+1.0_sp do j=1,6 Go through saved values to bracket the deif (ch(j) > offs) then sired roots. Note periodicity in slope and1=mod(abs(ang(j)-b),PI) gles. d2=PI-d1 if (ang(j) < b) call swap(d1,d2) if (d1 < bmx) bmx=d1 if (d2 < bmn) bmn=d2 end if end do if (bmx < BIG) then Call zbrent to find the roots. bmx=zbrent(chixy,b,b+bmx,ACC)-b amx=aa-a bmn=zbrent(chixy,b,b-bmn,ACC)-b amn=aa-a sigb=sqrt(0.5_sp*(bmx**2+bmn**2))/(scale*cos(b)**2) siga=sqrt(0.5_sp*(amx**2+amn**2)+r2)/scale Error in a has additional piece else r2. sigb=BIG siga=BIG end if a=a/scale Unscale the answers. b=tan(b)/scale END SUBROUTINE fitexy
1288
Chapter B15.
Modeling of Data
sumw=sum(wwp) avex=dot_product(wwp,xxp)/sumw avey=dot_product(wwp,yyp)/sumw aa=avey-b*avex chixy=sum(wwp(:)*(yyp(:)-aa-b*xxp(:))**2)-offs END FUNCTION chixy END MODULE chixyfit
SUBROUTINE lfit(x,y,sig,a,maska,covar,chisq,funcs) USE nrtype; USE nrutil, ONLY : assert_eq,diagmult,nrerror USE nr, ONLY :covsrt,gaussj IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y,sig REAL(SP), DIMENSION(:), INTENT(INOUT) :: a LOGICAL(LGT), DIMENSION(:), INTENT(IN) :: maska REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: covar REAL(SP), INTENT(OUT) :: chisq INTERFACE SUBROUTINE funcs(x,arr) USE nrtype IMPLICIT NONE REAL(SP),INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(OUT) :: arr END SUBROUTINE funcs END INTERFACE Given a set of N data points x, y with individual standard deviations sig, all arrays of length N , use χ2 minimization to fit for some or all of the M coefficients a of a function that depends linearly on a, y = M i=1 ai × afunci (x). The input logical array maska of length M indicates by true entries those components of a that should be fitted for, and by false entries those components that should be held fixed at their input values. The program returns values for a, χ2 = chisq, and the M × M covariance matrix covar. (Parameters held fixed will return zero covariances.) The user supplies a subroutine funcs(x,afunc) that returns the M basis functions evaluated at x = x in the array afunc. INTEGER(I4B) :: i,j,k,l,ma,mfit,n REAL(SP) :: sig2i,wt,ym REAL(SP), DIMENSION(size(maska)) :: afunc REAL(SP), DIMENSION(size(maska),1) :: beta n=assert_eq(size(x),size(y),size(sig),’lfit: n’) ma=assert_eq(size(maska),size(a),size(covar,1),size(covar,2),’lfit: ma’) mfit=count(maska) Number of parameters to fit for. if (mfit == 0) call nrerror(’lfit: no parameters to be fitted’) covar(1:mfit,1:mfit)=0.0 Initialize the (symmetric) matrix. beta(1:mfit,1)=0.0 do i=1,n Loop over data to accumulate coefficients of call funcs(x(i),afunc) the normal equations. ym=y(i) if (mfit < ma) ym=ym-sum(a(1:ma)*afunc(1:ma), mask=.not. maska) Subtract off dependences on known pieces of the fitting function. sig2i=1.0_sp/sig(i)**2 j=0 do l=1,ma if (maska(l)) then j=j+1 wt=afunc(l)*sig2i k=count(maska(1:l)) covar(j,1:k)=covar(j,1:k)+wt*pack(afunc(1:l),maska(1:l)) beta(j,1)=beta(j,1)+ym*wt end if end do
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
1289
Chapter B15. Modeling of Data
f90
if (mfit < ma) ym=ym-sum(a(1:ma)*afunc(1:ma), mask=.not. maska)
This is the first of several uses of maska in this routine to control which elements of an array are to be used. Here we include in the sum only elements for which maska is false, i.e., elements corresponding to parameters that are not being fitted for. covar(j,1:k)=covar(j,1:k)+wt*pack(afunc(1:l),maska(1:l)) Here maska controls which elements of afunc get packed into the covariance matrix. call diagmult(covar(1:mfit,1:mfit),0.5_sp)
See discussion of diagadd
after hqr on p. 1234. a(1:ma)=unpack(beta(1:ma,1),maska,a(1:ma)) And here maska controls which elements of beta get unpacked into the appropriate slots in a. Where maska is false, corresponding elements are selected from the third argument of unpack, here a itself. The net effect is that those elements remain unchanged.
SUBROUTINE covsrt(covar,maska) USE nrtype; USE nrutil, ONLY : assert_eq,swap IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: covar LOGICAL(LGT), DIMENSION(:), INTENT(IN) :: maska Expand in storage the covariance matrix covar, so as to take into account parameters that are being held fixed. (For the latter, return zero covariances.) INTEGER(I4B) :: ma,mfit,j,k ma=assert_eq(size(covar,1),size(covar,2),size(maska),’covsrt’) mfit=count(maska) covar(mfit+1:ma,1:ma)=0.0 covar(1:ma,mfit+1:ma)=0.0 k=mfit do j=ma,1,-1 if (maska(j)) then call swap(covar(1:ma,k),covar(1:ma,j)) call swap(covar(k,1:ma),covar(j,1:ma)) k=k-1 end if end do END SUBROUTINE covsrt
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
end do call diagmult(covar(1:mfit,1:mfit),0.5_sp) covar(1:mfit,1:mfit)= & Fill in above the diagonal from symmetry. covar(1:mfit,1:mfit)+transpose(covar(1:mfit,1:mfit)) call gaussj(covar(1:mfit,1:mfit),beta(1:mfit,1:1)) Matrix solution. a(1:ma)=unpack(beta(1:ma,1),maska,a(1:ma)) Partition solution to appropriate coefficients a. chisq=0.0 Evaluate χ2 of the fit. do i=1,n call funcs(x(i),afunc) chisq=chisq+((y(i)-dot_product(a(1:ma),afunc(1:ma)))/sig(i))**2 end do call covsrt(covar,maska) Sort covariance matrix to true order of fitting END SUBROUTINE lfit coefficients.
1290
Chapter B15.
Modeling of Data
Remember how spread works: the vector sigi is copied along the dimension 2, making a matrix whose columns are each a copy of sigi. The multiplication here is element by element, so each row of usav is multiplied by the corresponding element of sigi.
f90
u=usav*spread(sigi,dim=2,ncopies=ma)
chisq=vabs(matmul(usav,a)-b)**2 Fortran 90’s matmul intrinsic allows us to evaluate χ2 from the mathematical definition in terms of matrices. vabs in nrutil returns the length of a vector (L 2 norm). SUBROUTINE svdvar(v,w,cvm) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(IN) :: v REAL(SP), DIMENSION(:), INTENT(IN) :: w REAL(SP), DIMENSION(:,:), INTENT(OUT) :: cvm
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE svdfit(x,y,sig,a,v,w,chisq,funcs) USE nrtype; USE nrutil, ONLY : assert_eq,vabs USE nr, ONLY : svbksb,svdcmp IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y,sig REAL(SP), DIMENSION(:), INTENT(OUT) :: a,w REAL(SP), DIMENSION(:,:), INTENT(OUT) :: v REAL(SP), INTENT(OUT) :: chisq INTERFACE FUNCTION funcs(x,n) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(n) :: funcs END FUNCTION funcs END INTERFACE REAL(SP), PARAMETER :: TOL=1.0e-5_sp Given a set of N data points x, y with individual standard deviations sig, all arrays of length N , use χ2 minimization to determine the M coefficients a of a function that depends linearly on a, y = M i=1 ai × afunci (x). Here we solve the fitting equations using singular value decomposition of the N × M matrix, as in §2.6. On output, the M × M array v and the vector w of length M define part of the singular value decomposition, and can be used to obtain the covariance matrix. The program returns values for the M fit parameters a, and χ2 , chisq. The user supplies a subroutine funcs(x,afunc) that returns the M basis functions evaluated at x = X in the array afunc. INTEGER(I4B) :: i,ma,n REAL(SP), DIMENSION(size(x)) :: b,sigi REAL(SP), DIMENSION(size(x),size(a)) :: u,usav n=assert_eq(size(x),size(y),size(sig),’svdfit: n’) ma=assert_eq(size(a),size(v,1),size(v,2),size(w),’svdfit: ma’) sigi=1.0_sp/sig Accumulate coefficients of the fitting matrix in b=y*sigi u. do i=1,n usav(i,:)=funcs(x(i),ma) end do u=usav*spread(sigi,dim=2,ncopies=ma) usav=u call svdcmp(u,w,v) Singular value decomposition. where (w < TOL*maxval(w)) w=0.0 Edit the singular values, given TOL from the pacall svbksb(u,w,v,b,a) rameter statement. chisq=vabs(matmul(usav,a)-b)**2 Evaluate chi-square. END SUBROUTINE svdfit
Chapter B15. Modeling of Data
1291
f90
This is the standard Fortran 90 construction for doing different things to a matrix depending on some condition. Here we want to avoid inverting elements of w that are zero.
where (w /= 0.0)...elsewhere...end where
cvm=v*spread(wti,dim=1,ncopies=ma) Each column of v gets multiplied by the corresponding element of wti. Contrast the construction spread(...dim=2...) in svdfit.
FUNCTION fpoly(x,n) USE nrtype; USE nrutil, ONLY : geop IMPLICIT NONE REAL(SP), INTENT(IN) :: x INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(n) :: fpoly Fitting routine for a polynomial of degree n − 1, returning n coefficients in fpoly. fpoly=geop(1.0_sp,x,n) END FUNCTION fpoly
FUNCTION fleg(x,nl) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x INTEGER(I4B), INTENT(IN) :: nl REAL(SP), DIMENSION(nl) :: fleg Fitting routine for an expansion with nl Legendre polynomials evaluated at x and returned in the array fleg of length nl. The evaluation uses the recurrence relation as in §5.5. INTEGER(I4B) :: j REAL(SP) :: d,f1,f2,twox fleg(1)=1.0 fleg(2)=x if (nl > 2) then twox=2.0_sp*x f2=x d=1.0 do j=3,nl f1=d f2=f2+twox d=d+1.0_sp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
To evaluate the covariance matrix cvm of the fit for M parameters obtained by svdfit, call this routine with matrices v,w as returned from svdfit. The dimensions are M for w and M × M for v and cvm. INTEGER(I4B) :: ma REAL(SP), DIMENSION(size(w)) :: wti ma=assert_eq((/size(v,1),size(v,2),size(w),size(cvm,1),size(cvm,2)/),& ’svdvar’) where (w /= 0.0) wti=1.0_sp/(w*w) elsewhere wti=0.0 end where cvm=v*spread(wti,dim=1,ncopies=ma) cvm=matmul(cvm,transpose(v)) Covariance matrix is given by (15.4.20). END SUBROUTINE svdvar
1292
Chapter B15.
Modeling of Data
fleg(j)=(f2*fleg(j-1)-f1*fleg(j-2))/d end do end if END FUNCTION fleg
SUBROUTINE mrqmin(x,y,sig,a,maska,covar,alpha,chisq,funcs,alamda) USE nrtype; USE nrutil, ONLY : assert_eq,diagmult USE nr, ONLY : covsrt,gaussj IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: x,y,sig REAL(SP), DIMENSION(:), INTENT(INOUT) :: a REAL(SP), DIMENSION(:,:), INTENT(OUT) :: covar,alpha REAL(SP), INTENT(OUT) :: chisq REAL(SP), INTENT(INOUT) :: alamda LOGICAL(LGT), DIMENSION(:), INTENT(IN) :: maska INTERFACE SUBROUTINE funcs(x,a,yfit,dyda) USE nrtype REAL(SP), DIMENSION(:), INTENT(IN) :: x,a REAL(SP), DIMENSION(:), INTENT(OUT) :: yfit REAL(SP), DIMENSION(:,:), INTENT(OUT) :: dyda END SUBROUTINE funcs END INTERFACE Levenberg-Marquardt method, attempting to reduce the value χ2 of a fit between a set of N data points x, y with individual standard deviations sig, and a nonlinear function dependent on M coefficients a. The input logical array maska of length M indicates by true entries those components of a that should be fitted for, and by false entries those components that should be held fixed at their input values. The program returns current best-fit values for the parameters a, and χ2 = chisq. The M × M arrays covar and alpha are used as working space during most iterations. Supply a subroutine funcs(x,a,yfit,dyda) that evaluates the fitting function yfit, and its derivatives dyda with respect to the fitting parameters a at x. On the first call provide an initial guess for the parameters a, and set alamda EPS) ) END FUNCTION rofunc END SUBROUTINE medfit
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
sxx=dot_product(x,x) del=ndata*sxx-sx**2 aa=(sxx*sy-sx*sxy)/del Least squares solutions. bb=(ndata*sxy-sx*sy)/del tmp(:)=y(:)-(aa+bb*x(:)) chisq=dot_product(tmp,tmp) sigb=sqrt(chisq/del) The standard deviation will give some idea of how b1=bb big an iteration step to take. f1=rofunc(b1) b2=bb+sign(3.0_sp*sigb,f1) Guess bracket as 3-σ away, in the downhill direction f2=rofunc(b2) known from f1. if (b2 == b1) then a=aa b=bb RETURN endif do Bracketing. if (f1*f2 abs(dxsav))) & Store intermediate results. call save_a_step if ((x+h-x2)*(x+h-x1) > 0.0) h=x2-x If stepsize can overshoot, decrease. call rkqs(y,dydx,x,h,eps,yscal,hdid,hnext,derivs) if (hdid == h) then nok=nok+1 else nbad=nbad+1 end if if ((x-x2)*(x2-x1) >= 0.0) then Are we done? ystart(:)=y(:) if (save_steps) call save_a_step Save final step. RETURN Normal exit. end if if (abs(hnext) < hmin)& call nrerror(’stepsize smaller than minimum in odeint’) h=hnext end do call nrerror(’too many steps in odeint’)
1302
Chapter B16.
Integration of Ordinary Differential Equations
CONTAINS
The situation here is similar to rkdumb path, except we don’t know at run time how much storage to allocate. We may need to use reallocate from nrutil to increase the storage. The solution is pointers to arrays, with a nullify to be sure the pointer status is well-defined at the beginning of the routine.
f90
MODULE ode_path
SUBROUTINE save_a_step An internal subprogram with no arguments is like a macro in C: you could imagine just copying its code wherever it is called in the parent routine.
SUBROUTINE mmid(y,dydx,xs,htot,nstep,yout,derivs) USE nrtype; USE nrutil, ONLY : assert_eq,swap IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: nstep REAL(SP), INTENT(IN) :: xs,htot REAL(SP), DIMENSION(:), INTENT(IN) :: y,dydx REAL(SP), DIMENSION(:), INTENT(OUT) :: yout INTERFACE SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx END SUBROUTINE derivs END INTERFACE Modified midpoint step. Dependent variable vector y and its derivative vector dydx are input at xs. Also input is htot, the total step to be taken, and nstep, the number of substeps to be used. The output is returned as yout, which need not be a distinct array from y; if it is distinct, however, then y and dydx are returned undamaged. y, dydx, and yout must all have the same length. INTEGER(I4B) :: n,ndum REAL(SP) :: h,h2,x REAL(SP), DIMENSION(size(y)) :: ym,yn ndum=assert_eq(size(y),size(dydx),size(yout),’mmid’) h=htot/nstep Stepsize this trip. ym=y yn=y+h*dydx First step. x=xs+h call derivs(x,yn,yout) Will use yout for temporary storage of derivatives. h2=2.0_sp*h do n=2,nstep General step. call swap(ym,yn) yn=yn+h2*yout
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE save_a_step kount=kount+1 if (kount > size(xp)) then xp=>reallocate(xp,2*size(xp)) yp=>reallocate(yp,size(yp,1),size(xp)) end if xp(kount)=x yp(:,kount)=y(:) xsav=x END SUBROUTINE save_a_step END SUBROUTINE odeint
Chapter B16.
Integration of Ordinary Differential Equations
x=x+h call derivs(x,yn,yout) end do yout=0.5_sp*(ym+yn+h*yout) END SUBROUTINE mmid
1303
Last step.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE bsstep(y,dydx,x,htry,eps,yscal,hdid,hnext,derivs) USE nrtype; USE nrutil, ONLY : arth,assert_eq,cumsum,iminloc,nrerror,& outerdiff,outerprod,upper_triangle USE nr, ONLY : mmid,pzextr IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: y REAL(SP), DIMENSION(:), INTENT(IN) :: dydx,yscal REAL(SP), INTENT(INOUT) :: x REAL(SP), INTENT(IN) :: htry,eps REAL(SP), INTENT(OUT) :: hdid,hnext INTERFACE SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx END SUBROUTINE derivs END INTERFACE INTEGER(I4B), PARAMETER :: IMAX=9, KMAXX=IMAX-1 REAL(SP), PARAMETER :: SAFE1=0.25_sp,SAFE2=0.7_sp,REDMAX=1.0e-5_sp,& REDMIN=0.7_sp,TINY=1.0e-30_sp,SCALMX=0.1_sp Bulirsch-Stoer step with monitoring of local truncation error to ensure accuracy and adjust stepsize. Input are the dependent variable vector y and its derivative dydx at the starting value of the independent variable x. Also input are the stepsize to be attempted htry, the required accuracy eps, and the vector yscal against which the error is scaled. On output, y and x are replaced by their new values, hdid is the stepsize that was actually accomplished, and hnext is the estimated next stepsize. derivs is the user-supplied subroutine that computes the right-hand-side derivatives. y, dydx, and yscal must all have the same length. Be sure to set htry on successive steps to the value of hnext returned from the previous step, as is the case if the routine is called by odeint. Parameters: KMAXX is the maximum row number used in the extrapolation; IMAX is the next row number; SAFE1 and SAFE2 are safety factors; REDMAX is the maximum factor used when a stepsize is reduced, REDMIN the minimum; TINY prevents division by zero; 1/SCALMX is the maximum factor by which a stepsize can be increased. INTEGER(I4B) :: k,km,ndum INTEGER(I4B), DIMENSION(IMAX) :: nseq = (/ 2,4,6,8,10,12,14,16,18 /) INTEGER(I4B), SAVE :: kopt,kmax REAL(SP), DIMENSION(KMAXX,KMAXX), SAVE :: alf REAL(SP), DIMENSION(KMAXX) :: err REAL(SP), DIMENSION(IMAX), SAVE :: a REAL(SP), SAVE :: epsold = -1.0_sp,xnew REAL(SP) :: eps1,errmax,fact,h,red,scale,wrkmin,xest REAL(SP), DIMENSION(size(y)) :: yerr,ysav,yseq LOGICAL(LGT) :: reduct LOGICAL(LGT), SAVE :: first=.true. ndum=assert_eq(size(y),size(dydx),size(yscal),’bsstep’) if (eps /= epsold) then A new tolerance, so reinitialize. hnext=-1.0e29_sp “Impossible” values. xnew=-1.0e29_sp eps1=SAFE1*eps a(:)=cumsum(nseq,1) Compute α(k, q): where (upper_triangle(KMAXX,KMAXX)) alf=eps1** & (outerdiff(a(2:),a(2:))/outerprod(arth( &
1304
Chapter B16.
Integration of Ordinary Differential Equations
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
3.0_sp,2.0_sp,KMAXX),(a(2:)-a(1)+1.0_sp))) epsold=eps do kopt=2,KMAXX-1 Determine optimal row number for conif (a(kopt+1) > a(kopt)*alf(kopt-1,kopt)) exit vergence. end do kmax=kopt end if h=htry ysav(:)=y(:) Save the starting values. if (h /= hnext .or. x /= xnew) then A new stepsize or a new integration: Refirst=.true. establish the order window. kopt=kmax end if reduct=.false. main_loop: do do k=1,kmax Evaluate the sequence of modified midxnew=x+h point integrations. if (xnew == x) call nrerror(’step size underflow in bsstep’) call mmid(ysav,dydx,x,h,nseq(k),yseq,derivs) xest=(h/nseq(k))**2 Squared, since error series is even. call pzextr(k,xest,yseq,y,yerr) Perform extrapolation. if (k /= 1) then Compute normalized error estimate (k). errmax=maxval(abs(yerr(:)/yscal(:))) errmax=max(TINY,errmax)/eps Scale error relative to tolerance. km=k-1 err(km)=(errmax/SAFE1)**(1.0_sp/(2*km+1)) end if if (k /= 1 .and. (k >= kopt-1 .or. first)) then In order window. if (errmax < 1.0) exit main_loop Converged. if (k == kmax .or. k == kopt+1) then Check for possible stepred=SAFE2/err(km) size reduction. exit else if (k == kopt) then if (alf(kopt-1,kopt) < err(km)) then red=1.0_sp/err(km) exit end if else if (kopt == kmax) then if (alf(km,kmax-1) < err(km)) then red=alf(km,kmax-1)*SAFE2/err(km) exit end if else if (alf(km,kopt) < err(km)) then red=alf(km,kopt-1)/err(km) exit end if end if end do red=max(min(red,REDMIN),REDMAX) Reduce stepsize by at least REDMIN and h=h*red at most REDMAX. reduct=.true. end do main_loop Try again. x=xnew Successful step taken. hdid=h first=.false. kopt=1+iminloc(a(2:km+1)*max(err(1:km),SCALMX)) Compute optimal row for convergence and corresponding stepsize. scale=max(err(kopt-1),SCALMX) wrkmin=scale*a(kopt) hnext=h/scale if (kopt >= k .and. kopt /= kmax .and. .not. reduct) then Check for possible order increase, but fact=max(scale/alf(kopt-1,kopt),SCALMX) not if stepsize was if (a(kopt+1)*fact IEST_MAX) call & nrerror(’pzextr: probable misuse, too much extrapolation’) if (nv /= nvold) then Set up internal storage. if (allocated(qcol)) deallocate(qcol) allocate(qcol(nv,IEST_MAX)) nvold=nv end if x(iest)=xest Save current independent variable. dy(:)=yest(:) yz(:)=yest(:) if (iest == 1) then Store first estimate in first column. qcol(:,1)=yest(:) else d(:)=yest(:) do j=1,iest-1 delta=1.0_sp/(x(iest-j)-xest) f1=xest*delta f2=x(iest-j)*delta q(:)=qcol(:,j) Propagate tableau 1 diagonal more.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
f90
a(:)=cumsum(nseq,1) The function cumsum in nrutil with the optional argument seed=1 gives a direct implementation of equation (16.4.6).
1306
Chapter B16.
Integration of Ordinary Differential Equations
The second dimension of qcol is known at compile time to be IEST MAX, but the first dimension is known only at run time, from size(yz). The language requires us to have all dimensions allocatable if any one of them is.
f90
REAL(SP), DIMENSION(:,:), ALLOCATABLE, SAVE :: qcol
if (nv /= nvold) then... This routine generally gets called many times with iest cycling repeatedly through the values 1, 2, . . . , up to some value less than IEST MAX. The number of variables, nv, is fixed during the solution of the problem. The routine might be called again in solving a different problem with a new value of nv. This if block ensures that qcol is dimensioned correctly both for the first and subsequent problems, if any. SUBROUTINE rzextr(iest,xest,yest,yz,dy) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: iest REAL(SP), INTENT(IN) :: xest REAL(SP), DIMENSION(:), INTENT(IN) :: yest REAL(SP), DIMENSION(:), INTENT(OUT) :: yz,dy Exact substitute for pzextr, but uses diagonal rational function extrapolation instead of polynomial extrapolation. INTEGER(I4B), PARAMETER :: IEST_MAX=16 INTEGER(I4B) :: k,nv INTEGER(I4B), SAVE :: nvold=-1 REAL(SP), DIMENSION(size(yz)) :: yy,v,c,b,b1,ddy REAL(SP), DIMENSION(:,:), ALLOCATABLE, SAVE :: d REAL(SP), DIMENSION(IEST_MAX), SAVE :: fx,x nv=assert_eq(size(yz),size(dy),size(yest),’rzextr’) if (iest > IEST_MAX) call & nrerror(’rzextr: probable misuse, too much extrapolation’) if (nv /= nvold) then if (allocated(d)) deallocate(d) allocate(d(nv,IEST_MAX)) nvold=nv end if x(iest)=xest Save current independent variable. if (iest == 1) then yz=yest d(:,1)=yest dy=yest else fx(2:iest)=x(iest-1:1:-1)/xest yy=yest Evaluate next diagonal in tableau. v=d(1:nv,1) c=yy d(1:nv,1)=yy do k=2,iest b1=fx(k)*v b=b1-c where (b /= 0.0)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
qcol(:,j)=dy(:) tmp(:)=d(:)-q(:) dy(:)=f1*tmp(:) d(:)=f2*tmp(:) yz(:)=yz(:)+dy(:) end do qcol(:,iest)=dy(:) end if END SUBROUTINE pzextr
Chapter B16. Integration of Ordinary Differential Equations
1307
SUBROUTINE stoerm(y,d2y,xs,htot,nstep,yout,derivs) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: y,d2y REAL(SP), INTENT(IN) :: xs,htot INTEGER(I4B), INTENT(IN) :: nstep REAL(SP), DIMENSION(:), INTENT(OUT) :: yout INTERFACE SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx END SUBROUTINE derivs END INTERFACE Stoermer’s rule for integrating y = f (x, y) for a system of n equations. On input y contains y in its first n elements and y in its second n elements, all evaluated at xs. d2y contains the right-hand-side function f (also evaluated at xs) in its first n elements. Its second n elements are not referenced. Also input is htot, the total step to be taken, and nstep, the number of substeps to be used. The output is returned as yout, with the same storage arrangement as y. derivs is the user-supplied subroutine that calculates f . INTEGER(I4B) :: neqn,neqn1,nn,nv REAL(SP) :: h,h2,halfh,x REAL(SP), DIMENSION(size(y)) :: ytemp nv=assert_eq(size(y),size(d2y),size(yout),’stoerm’) neqn=nv/2 Number of equations. neqn1=neqn+1 h=htot/nstep Stepsize this trip. halfh=0.5_sp*h First step. ytemp(neqn1:nv)=h*(y(neqn1:nv)+halfh*d2y(1:neqn)) ytemp(1:neqn)=y(1:neqn)+ytemp(neqn1:nv) x=xs+h call derivs(x,ytemp,yout) Use yout for temporary storage of derivah2=h*h tives. do nn=2,nstep General step. ytemp(neqn1:nv)=ytemp(neqn1:nv)+h2*yout(1:neqn) ytemp(1:neqn)=ytemp(1:neqn)+ytemp(neqn1:nv) x=x+h call derivs(x,ytemp,yout) end do yout(neqn1:nv)=ytemp(neqn1:nv)/h+halfh*yout(1:neqn) Last step. yout(1:neqn)=ytemp(1:neqn) END SUBROUTINE stoerm
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
b=(c-v)/b ddy=c*b c=b1*b elsewhere Care needed to avoid division by 0. ddy=v end where if (k /= iest) v=d(1:nv,k) d(1:nv,k)=ddy yy=yy+ddy end do dy=ddy yz=yy end if END SUBROUTINE rzextr
1308
Chapter B16.
Integration of Ordinary Differential Equations
SUBROUTINE jacobn(x,y,dfdx,dfdy) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dfdx REAL(SP), DIMENSION(:,:), INTENT(OUT) :: dfdy END SUBROUTINE jacobn END INTERFACE INTEGER(I4B), PARAMETER :: MAXTRY=40 REAL(SP), PARAMETER :: SAFETY=0.9_sp,GROW=1.5_sp,PGROW=-0.25_sp,& SHRNK=0.5_sp,PSHRNK=-1.0_sp/3.0_sp,ERRCON=0.1296_sp,& GAM=1.0_sp/2.0_sp,& A21=2.0_sp,A31=48.0_sp/25.0_sp,A32=6.0_sp/25.0_sp,C21=-8.0_sp,& C31=372.0_sp/25.0_sp,C32=12.0_sp/5.0_sp,& C41=-112.0_sp/125.0_sp,C42=-54.0_sp/125.0_sp,& C43=-2.0_sp/5.0_sp,B1=19.0_sp/9.0_sp,B2=1.0_sp/2.0_sp,& B3=25.0_sp/108.0_sp,B4=125.0_sp/108.0_sp,E1=17.0_sp/54.0_sp,& E2=7.0_sp/36.0_sp,E3=0.0_sp,E4=125.0_sp/108.0_sp,& C1X=1.0_sp/2.0_sp,C2X=-3.0_sp/2.0_sp,C3X=121.0_sp/50.0_sp,& C4X=29.0_sp/250.0_sp,A2X=1.0_sp,A3X=3.0_sp/5.0_sp Fourth order Rosenbrock step for integrating stiff ODEs, with monitoring of local truncation error to adjust stepsize. Input are the dependent variable vector y and its derivative dydx at the starting value of the independent variable x. Also input are the stepsize to be attempted htry, the required accuracy eps, and the vector yscal against which the error is scaled. On output, y and x are replaced by their new values, hdid is the stepsize that was actually accomplished, and hnext is the estimated next stepsize. derivs is a user-supplied subroutine that computes the derivatives of the right-hand side with respect to x, while jacobn (a fixed name) is a user-supplied subroutine that computes the Jacobi matrix of derivatives of the right-hand side with respect to the components of y. y, dydx, and yscal must have the same length. Parameters: GROW and SHRNK are the largest and smallest factors by which stepsize can change in one step; ERRCON=(GROW/SAFETY)**(1/PGROW) and handles the case when errmax 0. INTEGER(I4B) :: jtry,ndum INTEGER(I4B), DIMENSION(size(y)) :: indx REAL(SP), DIMENSION(size(y)) :: dfdx,dytmp,err,g1,g2,g3,g4,ysav REAL(SP), DIMENSION(size(y),size(y)) :: a,dfdy REAL(SP) :: d,errmax,h,xsav ndum=assert_eq(size(y),size(dydx),size(yscal),’stiff’) xsav=x Save initial values. ysav(:)=y(:) call jacobn(xsav,ysav,dfdx,dfdy) The user must supply this subroutine to return the n × n matrix dfdy and the vector dfdx. h=htry Set stepsize to the initial trial value. do jtry=1,MAXTRY
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE stiff(y,dydx,x,htry,eps,yscal,hdid,hnext,derivs) USE nrtype; USE nrutil, ONLY : assert_eq,diagadd,nrerror USE nr, ONLY : lubksb,ludcmp IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: y REAL(SP), DIMENSION(:), INTENT(IN) :: dydx,yscal REAL(SP), INTENT(INOUT) :: x REAL(SP), INTENT(IN) :: htry,eps REAL(SP), INTENT(OUT) :: hdid,hnext INTERFACE SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx END SUBROUTINE derivs
Chapter B16.
Integration of Ordinary Differential Equations
1309
f90
call diagadd(...)
See discussion of diagadd after hqr on p. 1234.
SUBROUTINE jacobn(x,y,dfdx,dfdy) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dfdx REAL(SP), DIMENSION(:,:), INTENT(OUT) :: dfdy Routine for Jacobi matrix corresponding to example in equations (16.6.27). dfdx(:)=0.0 dfdy(1,1)=-0.013_sp-1000.0_sp*y(3) dfdy(1,2)=0.0 dfdy(1,3)=-1000.0_sp*y(1) dfdy(2,1)=0.0 dfdy(2,2)=-2500.0_sp*y(3) dfdy(2,3)=-2500.0_sp*y(2) dfdy(3,1)=-0.013_sp-1000.0_sp*y(3) dfdy(3,2)=-2500.0_sp*y(3) dfdy(3,3)=-1000.0_sp*y(1)-2500.0_sp*y(2) END SUBROUTINE jacobn
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
a(:,:)=-dfdy(:,:) Set up the matrix 1 − γhf . call diagadd(a,1.0_sp/(GAM*h)) call ludcmp(a,indx,d) LU decomposition of the matrix. g1=dydx+h*C1X*dfdx Set up right-hand side for g1 . call lubksb(a,indx,g1) Solve for g1 . y=ysav+A21*g1 Compute intermediate values of y and x. x=xsav+A2X*h call derivs(x,y,dytmp) Compute dydx at the intermediate values. g2=dytmp+h*C2X*dfdx+C21*g1/h Set up right-hand side for g2 . call lubksb(a,indx,g2) Solve for g2 . y=ysav+A31*g1+A32*g2 Compute intermediate values of y and x. x=xsav+A3X*h call derivs(x,y,dytmp) Compute dydx at the intermediate values. g3=dytmp+h*C3X*dfdx+(C31*g1+C32*g2)/h Set up right-hand side for g3 . call lubksb(a,indx,g3) Solve for g3 . g4=dytmp+h*C4X*dfdx+(C41*g1+C42*g2+C43*g3)/h Set up right-hand side for g4 . call lubksb(a,indx,g4) Solve for g4 . y=ysav+B1*g1+B2*g2+B3*g3+B4*g4 Get fourth order estimate of y and error eserr=E1*g1+E2*g2+E3*g3+E4*g4 timate. x=xsav+h if (x == xsav) call & nrerror(’stepsize not significant in stiff’) errmax=maxval(abs(err/yscal))/eps Evaluate accuracy. if (errmax ERRCON) RETURN else Truncation error too large, reduce stepsize. hnext=SAFETY*h*errmax**PSHRNK h=sign(max(abs(hnext),SHRNK*abs(h)),h) end if end do Go back and retry step. call nrerror(’exceeded MAXTRY in stiff’) END SUBROUTINE stiff
1310
Chapter B16.
Integration of Ordinary Differential Equations
SUBROUTINE simpr(y,dydx,dfdx,dfdy,xs,htot,nstep,yout,derivs) USE nrtype; USE nrutil, ONLY : assert_eq,diagadd USE nr, ONLY : lubksb,ludcmp IMPLICIT NONE REAL(SP), INTENT(IN) :: xs,htot REAL(SP), DIMENSION(:), INTENT(IN) :: y,dydx,dfdx REAL(SP), DIMENSION(:,:), INTENT(IN) :: dfdy INTEGER(I4B), INTENT(IN) :: nstep REAL(SP), DIMENSION(:), INTENT(OUT) :: yout INTERFACE SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx END SUBROUTINE derivs END INTERFACE Performs one step of semi-implicit midpoint rule. Input are the dependent variable y, its derivative dydx, the derivative of the right-hand side with respect to x, dfdx, which are all vectors of length N , and the N × N Jacobian dfdy at xs. Also input are htot, the total step to be taken, and nstep, the number of substeps to be used. The output is returned as yout, a vector of length N . derivs is the user-supplied subroutine that calculates dydx. INTEGER(I4B) :: ndum,nn INTEGER(I4B), DIMENSION(size(y)) :: indx REAL(SP) :: d,h,x REAL(SP), DIMENSION(size(y)) :: del,ytemp REAL(SP), DIMENSION(size(y),size(y)) :: a ndum=assert_eq((/size(y),size(dydx),size(dfdx),size(dfdy,1),& size(dfdy,2),size(yout)/),’simpr’) h=htot/nstep Stepsize this trip. a(:,:)=-h*dfdy(:,:) Set up the matrix 1 − hf . call diagadd(a,1.0_sp) call ludcmp(a,indx,d) LU decomposition of the matrix. yout=h*(dydx+h*dfdx) Set up right-hand side for first step. Use yout for call lubksb(a,indx,yout) temporary storage. del=yout First step. ytemp=y+del x=xs+h call derivs(x,ytemp,yout) Use yout for temporary storage of derivatives. do nn=2,nstep General step. yout=h*yout-del Set up right-hand side for general step. call lubksb(a,indx,yout) del=del+2.0_sp*yout ytemp=ytemp+del x=x+h call derivs(x,ytemp,yout)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx Routine for right-hand side of example in equations (16.6.27). dydx(1)=-0.013_sp*y(1)-1000.0_sp*y(1)*y(3) dydx(2)=-2500.0_sp*y(2)*y(3) dydx(3)=-0.013_sp*y(1)-1000.0_sp*y(1)*y(3)-2500.0_sp*y(2)*y(3) END SUBROUTINE derivs
Chapter B16.
Integration of Ordinary Differential Equations
end do yout=h*yout-del call lubksb(a,indx,yout) yout=ytemp+yout END SUBROUTINE simpr
call diagadd(...)
Set up right-hand side for last step. Take last step.
See discussion of diagadd after hqr on p. 1234.
SUBROUTINE stifbs(y,dydx,x,htry,eps,yscal,hdid,hnext,derivs) USE nrtype; USE nrutil, ONLY : arth,assert_eq,cumsum,iminloc,nrerror,& outerdiff,outerprod,upper_triangle USE nr, ONLY : simpr,pzextr IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(INOUT) :: y REAL(SP), DIMENSION(:), INTENT(IN) :: dydx,yscal REAL(SP), INTENT(IN) :: htry,eps REAL(SP), INTENT(INOUT) :: x REAL(SP), INTENT(OUT) :: hdid,hnext INTERFACE SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx END SUBROUTINE derivs SUBROUTINE jacobn(x,y,dfdx,dfdy) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dfdx REAL(SP), DIMENSION(:,:), INTENT(OUT) :: dfdy END SUBROUTINE jacobn END INTERFACE INTEGER(I4B), PARAMETER :: IMAX=8, KMAXX=IMAX-1 REAL(SP), PARAMETER :: SAFE1=0.25_sp,SAFE2=0.7_sp,REDMAX=1.0e-5_sp,& REDMIN=0.7_sp,TINY=1.0e-30_sp,SCALMX=0.1_sp Semi-implicit extrapolation step for integrating stiff ODEs, with monitoring of local truncation error to adjust stepsize. Input are the dependent variable vector y and its derivative dydx at the starting value of the independent variable x. Also input are the stepsize to be attempted htry, the required accuracy eps, and the vector yscal against which the error is scaled. On output, y and x are replaced by their new values, hdid is the stepsize that was actually accomplished, and hnext is the estimated next stepsize. derivs is a usersupplied subroutine that computes the derivatives of the right-hand side with respect to x, while jacobn (a fixed name) is a user-supplied subroutine that computes the Jacobi matrix of derivatives of the right-hand side with respect to the components of y. y, dydx, and yscal must all have the same length. Be sure to set htry on successive steps to the value of hnext returned from the previous step, as is the case if the routine is called by odeint. INTEGER(I4B) :: k,km,ndum INTEGER(I4B), DIMENSION(IMAX) :: nseq = (/ 2,6,10,14,22,34,50,70 /) Sequence is different from bsstep. INTEGER(I4B), SAVE :: kopt,kmax,nvold=-1 REAL(SP), DIMENSION(KMAXX,KMAXX), SAVE :: alf REAL(SP), DIMENSION(KMAXX) :: err REAL(SP), DIMENSION(IMAX), SAVE :: a REAL(SP), SAVE :: epsold = -1.0 REAL(SP) :: eps1,errmax,fact,h,red,scale,wrkmin,xest
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
f90
1311
1312
Chapter B16.
Integration of Ordinary Differential Equations
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
REAL(SP), SAVE :: xnew REAL(SP), DIMENSION(size(y)) :: dfdx,yerr,ysav,yseq REAL(SP), DIMENSION(size(y),size(y)) :: dfdy LOGICAL(LGT) :: reduct LOGICAL(LGT), SAVE :: first=.true. ndum=assert_eq(size(y),size(dydx),size(yscal),’stifbs’) if (eps /= epsold .or. nvold /= size(y)) then Reinitialize also if number of varihnext=-1.0e29_sp ables has changed. xnew=-1.0e29_sp eps1=SAFE1*eps a(:)=cumsum(nseq,1) where (upper_triangle(KMAXX,KMAXX)) alf=eps1** & (outerdiff(a(2:),a(2:))/outerprod(arth( & 3.0_sp,2.0_sp,KMAXX),(a(2:)-a(1)+1.0_sp))) epsold=eps nvold=size(y) Save number of variables. a(:)=cumsum(nseq,1+nvold) Add cost of Jacobian evaluations to work codo kopt=2,KMAXX-1 efficients. if (a(kopt+1) > a(kopt)*alf(kopt-1,kopt)) exit end do kmax=kopt end if h=htry ysav(:)=y(:) call jacobn(x,y,dfdx,dfdy) Evaluate Jacobian. if (h /= hnext .or. x /= xnew) then first=.true. kopt=kmax end if reduct=.false. main_loop: do do k=1,kmax xnew=x+h if (xnew == x) call nrerror(’step size underflow in stifbs’) call simpr(ysav,dydx,dfdx,dfdy,x,h,nseq(k),yseq,derivs) Here is the call to the semi-implicit midpoint rule. xest=(h/nseq(k))**2 The rest of the routine is identical to bsstep. call pzextr(k,xest,yseq,y,yerr) if (k /= 1) then errmax=maxval(abs(yerr(:)/yscal(:))) errmax=max(TINY,errmax)/eps km=k-1 err(km)=(errmax/SAFE1)**(1.0_sp/(2*km+1)) end if if (k /= 1 .and. (k >= kopt-1 .or. first)) then if (errmax < 1.0) exit main_loop if (k == kmax .or. k == kopt+1) then red=SAFE2/err(km) exit else if (k == kopt) then if (alf(kopt-1,kopt) < err(km)) then red=1.0_sp/err(km) exit end if else if (kopt == kmax) then if (alf(km,kmax-1) < err(km)) then red=alf(km,kmax-1)*SAFE2/err(km) exit end if else if (alf(km,kopt) < err(km)) then red=alf(km,kopt-1)/err(km) exit end if end if
end do red=max(min(red,REDMIN),REDMAX) h=h*red reduct=.true. end do main_loop x=xnew hdid=h first=.false. kopt=1+iminloc(a(2:km+1)*max(err(1:km),SCALMX)) scale=max(err(kopt-1),SCALMX) wrkmin=scale*a(kopt) hnext=h/scale if (kopt >= k .and. kopt /= kmax .and. .not. reduct) then fact=max(scale/alf(kopt-1,kopt),SCALMX) if (a(kopt+1)*fact v(1:n2), v2=>v(n2 + 1:N ). Here N = n1 + n2 . The routine integrates the ODEs to xf using the Runge-Kutta method with tolerance EPS, initial stepsize h1, and minimum stepsize hmin. At xf it calls the user-supplied subroutine score to evaluate the N functions f1 and f2 that ought to match at xf. The differences funcv are returned on output. newt uses a globally convergent Newton’s method to adjust the values of v until the functions funcv are zero. The user-supplied subroutine derivs(x,y,dydx) supplies derivative information to the ODE integrator (see Chapter 16). The module sphfpt caller receives its values from the main program so that funcv can have the syntax required by newt. Set nn2 = n2 in the main program. REAL(SP) :: h1,hmin REAL(SP), DIMENSION(size(v)) :: f1,f2,y INTERFACE SUBROUTINE derivs(x,y,dydx) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx END SUBROUTINE derivs SUBROUTINE load1(x1,v1,y) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x1 REAL(SP), DIMENSION(:), INTENT(IN) :: v1 REAL(SP), DIMENSION(:), INTENT(OUT) :: y END SUBROUTINE load1 SUBROUTINE load2(x2,v2,y) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x2 REAL(SP), DIMENSION(:), INTENT(IN) :: v2 REAL(SP), DIMENSION(:), INTENT(OUT) :: y END SUBROUTINE load2 SUBROUTINE score(x2,y,f) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: x2 REAL(SP), DIMENSION(:), INTENT(IN) :: y
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
1316
Chapter B17.
Two Point Boundary Value Problems
SUBROUTINE solvde(itmax,conv,slowc,scalv,indexv,nb,y) USE nrtype; USE nrutil, ONLY : assert_eq,imaxloc,nrerror USE nr, ONLY : difeq IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: itmax,nb REAL(SP), INTENT(IN) :: conv,slowc REAL(SP), DIMENSION(:), INTENT(IN) :: scalv INTEGER(I4B), DIMENSION(:), INTENT(IN) :: indexv REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: y Driver routine for solution of two point boundary value problems with N equations by relaxation. itmax is the maximum number of iterations. conv is the convergence criterion (see text). slowc controls the fraction of corrections actually used after each iteration. scalv, a vector of length N , contains typical sizes for each dependent variable, used to weight errors. indexv, also of length N , lists the column ordering of variables used to construct the matrix s of derivatives. (The nb boundary conditions at the first mesh point must contain some dependence on the first nb variables listed in indexv.) There are a total of M mesh points. y is the N × M array that contains the initial guess for all the dependent variables at each mesh point. On each iteration, it is updated by the calculated correction. INTEGER(I4B) :: ic1,ic2,ic3,ic4,it,j,j1,j2,j3,j4,j5,j6,j7,j8,& j9,jc1,jcf,jv,k,k1,k2,km,kp,m,ne,nvars INTEGER(I4B), DIMENSION(size(scalv)) :: kmax REAL(SP) :: err,fac REAL(SP), DIMENSION(size(scalv)) :: ermax REAL(SP), DIMENSION(size(scalv),2*size(scalv)+1) :: s REAL(SP), DIMENSION(size(scalv),size(scalv)-nb+1,size(y,2)+1) :: c ne=assert_eq(size(scalv),size(indexv),size(y,1),’solvde: ne’) m=size(y,2) k1=1 Set up row and column markers. k2=m nvars=ne*m j1=1 j2=nb j3=nb+1 j4=ne j5=j4+j1 j6=j4+j2 j7=j4+j3 j8=j4+j4 j9=j8+j1 ic1=1 ic2=ne-nb ic3=ic2+1 ic4=ne jc1=1 jcf=ic3 do it=1,itmax Primary iteration loop. k=k1 Boundary conditions at first point.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
REAL(SP), DIMENSION(:), INTENT(OUT) :: f END SUBROUTINE score END INTERFACE h1=(x2-x1)/100.0_sp hmin=0.0 call load1(x1,v,y) Path from x1 to xf with best trial values V1 . if (associated(xp)) deallocate(xp,yp) Prevent memory leak if save steps set call odeint(y,x1,xf,EPS,h1,hmin,derivs,rkqs) to .true. call score(xf,y,f1) call load2(x2,v(nn2+1:),y) Path from x2 to xf with best trial values V2 . call odeint(y,x2,xf,EPS,h1,hmin,derivs,rkqs) call score(xf,y,f2) funcv(:)=f1(:)-f2(:) END FUNCTION funcv
Chapter B17. Two Point Boundary Value Problems
1317
SUBROUTINE bksub(ne,nb,jf,k1,k2,c) IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: ne,nb,jf,k1,k2 REAL(SP), DIMENSION(:,:,:), INTENT(INOUT) :: c Backsubstitution, used internally by solvde. INTEGER(I4B) :: im,k,nbf nbf=ne-nb im=1 do k=k2,k1,-1 Use recurrence relations to eliminate remaining dependences. if (k == k1) im=nbf+1 Special handling of first point. c(im:ne,jf,k)=c(im:ne,jf,k)-matmul(c(im:ne,1:nbf,k),c(1:nbf,jf,k+1)) end do c(1:nb,1,k1:k2)=c(1+nbf:nb+nbf,jf,k1:k2) Reorder corrections to be in column 1. c(1+nb:nbf+nb,1,k1:k2)=c(1:nbf,jf,k1+1:k2+1) END SUBROUTINE bksub SUBROUTINE pinvs(ie1,ie2,je1,jsf,jc1,k,c,s) IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: ie1,ie2,je1,jsf,jc1,k REAL(SP), DIMENSION(:,:,:), INTENT(OUT) :: c REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: s Diagonalize the square subsection of the s matrix, and store the recursion coefficients in c; used internally by solvde. INTEGER(I4B) :: i,icoff,id,ipiv,jcoff,je2,jp,jpiv,js1 INTEGER(I4B), DIMENSION(ie2) :: indxr REAL(SP) :: big,piv,pivinv REAL(SP), DIMENSION(ie2) :: pscl je2=je1+ie2-ie1 js1=je2+1 pscl(ie1:ie2)=maxval(abs(s(ie1:ie2,je1:je2)),dim=2) Implicit pivoting, as in §2.1.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call difeq(k,k1,k2,j9,ic3,ic4,indexv,s,y) call pinvs(ic3,ic4,j5,j9,jc1,k1,c,s) do k=k1+1,k2 Finite difference equations at all point kp=k-1 pairs. call difeq(k,k1,k2,j9,ic1,ic4,indexv,s,y) call red(ic1,ic4,j1,j2,j3,j4,j9,ic3,jc1,jcf,kp,c,s) call pinvs(ic1,ic4,j3,j9,jc1,k,c,s) end do k=k2+1 Final boundary conditions. call difeq(k,k1,k2,j9,ic1,ic2,indexv,s,y) call red(ic1,ic2,j5,j6,j7,j8,j9,ic3,jc1,jcf,k2,c,s) call pinvs(ic1,ic2,j7,j9,jcf,k2+1,c,s) call bksub(ne,nb,jcf,k1,k2,c) Backsubstitution. do j=1,ne Convergence check, accumulate average jv=indexv(j) error. km=imaxloc(abs(c(jv,1,k1:k2)))+k1-1 Find point with largest error, for each dependent variable. ermax(j)=c(jv,1,km) kmax(j)=km end do ermax(:)=ermax(:)/scalv(:) Weighting for each dependent variable. err=sum(sum(abs(c(indexv(:),1,k1:k2)),dim=2)/scalv(:))/nvars fac=slowc/max(slowc,err) Reduce correction applied when error is large. y(:,k1:k2)=y(:,k1:k2)-fac*c(indexv(:),1,k1:k2) Apply corrections. write(*,’(1x,i4,2f12.6)’) it,err,fac Summary of corrections for this step. Point with largest error for each variable can be monitored by writing out kmax and ermax. if (err < conv) RETURN end do call nrerror(’itmax exceeded in solvde’) Convergence failed. CONTAINS
1318
Chapter B17.
Two Point Boundary Value Problems
SUBROUTINE red(iz1,iz2,jz1,jz2,jm1,jm2,jmf,ic1,jc1,jcf,kc,c,s) IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: iz1,iz2,jz1,jz2,jm1,jm2,jmf,ic1,jc1,jcf,kc REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: s REAL(SP), DIMENSION(:,:,:), INTENT(IN) :: c Reduce columns jz1-jz2 of the s matrix, using previous results as stored in the c matrix. Only columns jm1-jm2,jmf are affected by the prior results. red is used internally by solvde. INTEGER(I4B) :: ic,l,loff loff=jc1-jm1 ic=ic1 do j=jz1,jz2 Loop over columns to be zeroed. do l=jm1,jm2 Loop over columns altered. s(iz1:iz2,l)=s(iz1:iz2,l)-s(iz1:iz2,j)*c(ic,l+loff,kc) Loop over rows. end do s(iz1:iz2,jmf)=s(iz1:iz2,jmf)-s(iz1:iz2,j)*c(ic,jcf,kc) Plus final element. ic=ic+1 end do END SUBROUTINE red END SUBROUTINE solvde
f90
km=imaxloc...
See discussion of imaxloc on p. 1017.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (any(pscl(ie1:ie2) == 0.0)) & call nrerror(’singular matrix, row all 0 in pinvs’) pscl(ie1:ie2)=1.0_sp/pscl(ie1:ie2) indxr(ie1:ie2)=0 do id=ie1,ie2 piv=0.0 do i=ie1,ie2 Find pivot element. if (indxr(i) == 0) then jp=imaxloc(abs(s(i,je1:je2)))+je1-1 big=abs(s(i,jp)) if (big*pscl(i) > piv) then ipiv=i jpiv=jp piv=big*pscl(i) end if end if end do if (s(ipiv,jpiv) == 0.0) call nrerror(’singular matrix in pinvs’) indxr(ipiv)=jpiv In place reduction. Save column orderpivinv=1.0_sp/s(ipiv,jpiv) ing. s(ipiv,je1:jsf)=s(ipiv,je1:jsf)*pivinv Normalize pivot row. s(ipiv,jpiv)=1.0 do i=ie1,ie2 Reduce nonpivot elements in column. if (indxr(i) /= jpiv .and. s(i,jpiv) /= 0.0) then s(i,je1:jsf)=s(i,je1:jsf)-s(i,jpiv)*s(ipiv,je1:jsf) s(i,jpiv)=0.0 end if end do end do jcoff=jc1-js1 Sort and store unreduced coefficients. icoff=ie1-je1 c(indxr(ie1:ie2)+icoff,js1+jcoff:jsf+jcoff,k)=s(ie1:ie2,js1:jsf) END SUBROUTINE pinvs
Chapter B17.
Two Point Boundary Value Problems
MODULE sfroid_data USE nrtype INTEGER(I4B), PARAMETER :: M=41 INTEGER(I4B) :: mm,n REAL(SP) :: anorm,c2,h REAL(SP), DIMENSION(M) :: x END MODULE sfroid_data
1319
Communicates with difeq.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
PROGRAM sfroid USE nrtype; USE nrutil, ONLY : arth USE nr, ONLY : plgndr,solvde USE sfroid_data IMPLICIT NONE INTEGER(I4B), PARAMETER :: NE=3,NB=1 Sample program using solvde. Computes eigenvalues of spheroidal harmonics Smn (x; c) for m ≥ 0 and n ≥ m. In the program, m is mm, c2 is c2, and γ of equation (17.4.20) is anorm. INTEGER(I4B) :: itmax INTEGER(I4B), DIMENSION(NE) :: indexv REAL(SP) :: conv,slowc REAL(SP), DIMENSION(M) :: deriv,fac1,fac2 REAL(SP), DIMENSION(NE) :: scalv REAL(SP), DIMENSION(NE,M) :: y itmax=100 conv=5.0e-6_sp slowc=1.0 h=1.0_sp/(M-1) c2=0.0 write(*,*) ’ENTER M,N’ read(*,*) mm,n indexv(1:3)=merge( (/ 1, 2, 3 /), (/ 2, 1, 3 /), (mod(n+mm,2) == 1) ) No interchanges necessary if n+mm is odd; otherwise interchange y1 and y2 . anorm=1.0 Compute γ. if (mm /= 0) then anorm=(-0.5_sp)**mm*product(& arth(n+1,1,mm)*arth(real(n,sp),-1.0_sp,mm)/arth(1,1,mm)) end if x(1:M-1)=arth(0,1,M-1)*h fac1(1:M-1)=1.0_sp-x(1:M-1)**2 Compute initial guess. fac2(1:M-1)=fac1(1:M-1)**(-mm/2.0_sp) y(1,1:M-1)=plgndr(n,mm,x(1:M-1))*fac2(1:M-1) Pnm from §6.8. deriv(1:M-1)=-((n-mm+1)*plgndr(n+1,mm,x(1:M-1))-(n+1)*& x(1:M-1)*plgndr(n,mm,x(1:M-1)))/fac1(1:M-1) Derivative of Pnm from a recurrence relation. y(2,1:M-1)=mm*x(1:M-1)*y(1,1:M-1)/fac1(1:M-1)+deriv(1:M-1)*fac2(1:M-1) y(3,1:M-1)=n*(n+1)-mm*(mm+1) x(M)=1.0 Initial guess at x = 1 done separately. y(1,M)=anorm y(3,M)=n*(n+1)-mm*(mm+1) y(2,M)=(y(3,M)-c2)*y(1,M)/(2.0_sp*(mm+1.0_sp)) scalv(1:3)=(/ abs(anorm), max(abs(anorm),y(2,M)), max(1.0_sp,y(3,M)) /) do write (*,*) ’ENTER C**2 OR 999 TO END’ read (*,*) c2 if (c2 == 999.0) exit call solvde(itmax,conv,slowc,scalv,indexv,NB,y) write (*,*) ’ M = ’,mm,’ N = ’,n,& ’ C**2 = ’,c2,’ LAMBDA = ’,y(3,1)+mm*(mm+1) end do Go back for another value of c2 . END PROGRAM sfroid
1320
Chapter B17.
f90
Two Point Boundary Value Problems
This module functions just like a common block to communicate variables with difeq. The advantage of a module is that it allows complete specification of the variables.
MODULE sfroid_data
anorm=(-0.5_sp)**mm*product(...
This statement computes equation (17.4.20)
by direct multiplication.
SUBROUTINE difeq(k,k1,k2,jsf,is1,isf,indexv,s,y) USE nrtype USE sfroid_data IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: is1,isf,jsf,k,k1,k2 INTEGER(I4B), DIMENSION(:), INTENT(IN) :: indexv REAL(SP), DIMENSION(:,:), INTENT(OUT) :: s REAL(SP), DIMENSION(:,:), INTENT(IN) :: y Returns matrix s(i,j) for solvde. REAL(SP) :: temp,temp2 INTEGER(I4B), DIMENSION(3) :: indexv3 indexv3(1:3)=3+indexv(1:3) if (k == k1) then Boundary condition at first point. if (mod(n+mm,2) == 1) then s(3,indexv3(1:3))= (/ 1.0_sp, 0.0_sp, 0.0_sp /) Equation s(3,jsf)=y(1,1) Equation else s(3,indexv3(1:3))= (/ 0.0_sp, 1.0_sp, 0.0_sp /) Equation s(3,jsf)=y(2,1) Equation end if else if (k > k2) then Boundary conditions at last point. s(1,indexv3(1:3))= (/ -(y(3,M)-c2)/(2.0_sp*(mm+1.0_sp)),& 1.0_sp, -y(1,M)/(2.0_sp*(mm+1.0_sp)) /) Equation s(1,jsf)=y(2,M)-(y(3,M)-c2)*y(1,M)/(2.0_sp*(mm+1.0_sp)) Equation s(2,indexv3(1:3))=(/ 1.0_sp, 0.0_sp, 0.0_sp /) Equation s(2,jsf)=y(1,M)-anorm Equation else Interior point. s(1,indexv(1:3))=(/ -1.0_sp, -0.5_sp*h, 0.0_sp /) Equation s(1,indexv3(1:3))=(/ 1.0_sp, -0.5_sp*h, 0.0_sp /) temp=h/(1.0_sp-(x(k)+x(k-1))**2*0.25_sp) temp2=0.5_sp*(y(3,k)+y(3,k-1))-c2*0.25_sp*(x(k)+x(k-1))**2 s(2,indexv(1:3))=(/ temp*temp2*0.5_sp,& Equation -1.0_sp-0.5_sp*temp*(mm+1.0_sp)*(x(k)+x(k-1)),& 0.25_sp*temp*(y(1,k)+y(1,k-1)) /) s(2,indexv3(1:3))=s(2,indexv(1:3)) s(2,indexv3(2))=s(2,indexv3(2))+2.0_sp s(3,indexv(1:3))=(/ 0.0_sp, 0.0_sp, -1.0_sp /) Equation s(3,indexv3(1:3))=(/ 0.0_sp, 0.0_sp, 1.0_sp /) s(1,jsf)=y(1,k)-y(1,k-1)-0.5_sp*h*(y(2,k)+y(2,k-1)) Equation s(2,jsf)=y(2,k)-y(2,k-1)-temp*((x(k)+x(k-1))*& Equation 0.5_sp*(mm+1.0_sp)*(y(2,k)+y(2,k-1))-temp2*& 0.5_sp*(y(1,k)+y(1,k-1))) s(3,jsf)=y(3,k)-y(3,k-1) Equation end if END SUBROUTINE difeq
(17.4.32). (17.4.31). (17.4.32). (17.4.31).
(17.4.35). (17.4.33). (17.4.36). (17.4.34). (17.4.28).
(17.4.29).
(17.4.30). (17.4.23). (17.4.24). (17.4.27).
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B17. Two Point Boundary Value Problems
1321
Communicates with load, score, and derivs.
MODULE sphoot_caller USE nrtype INTEGER(I4B) :: nvar REAL(SP) :: x1,x2 END MODULE sphoot_caller
Communicates with shoot.
PROGRAM sphoot Sample program using shoot. Computes eigenvalues of spheroidal harmonics Smn (x; c) for m ≥ 0 and n ≥ m. Be sure that routine funcv for newt is provided by shoot (§17.1). USE nrtype; USE nrutil, ONLY : arth USE nr, ONLY : newt USE sphoot_data USE sphoot_caller IMPLICIT NONE INTEGER(I4B), PARAMETER :: NV=3,N2=1 REAL(SP), DIMENSION(N2) :: v LOGICAL(LGT) :: check nvar=NV Number of equations. dx=1.0e-4_sp Avoid evaluating derivatives exactly at x = do −1. write(*,*) ’input m,n,c-squared (999 to end)’ read(*,*) m,n,c2 if (c2 == 999.0) exit if ((n < m) .or. (m < 0)) cycle gamma=(-0.5_sp)**m*product(& Compute γ of equation (17.4.20). arth(n+1,1,m)*(arth(real(n,sp),-1.0_sp,m)/arth(1,1,m))) v(1)=n*(n+1)-m*(m+1)+c2/2.0_sp Initial guess for eigenvalue. x1=-1.0_sp+dx Set range of integration. x2=0.0 call newt(v,check) Find v that zeros function f in score. if (check) then write(*,*)’shoot failed; bad initial guess’ exit else write(*,’(1x,t6,a)’) ’mu(m,n)’ write(*,’(1x,f12.6)’) v(1) end if end do END PROGRAM sphoot SUBROUTINE load(x1,v,y) USE nrtype USE sphoot_data IMPLICIT NONE REAL(SP), INTENT(IN) :: x1 REAL(SP), DIMENSION(:), INTENT(IN) :: v REAL(SP), DIMENSION(:), INTENT(OUT) :: y Supplies starting values for integration at x = −1 + dx. REAL(SP) :: y1 y(3)=v(1) y1=merge(gamma,-gamma, mod(n-m,2) == 0 ) y(2)=-(y(3)-c2)*y1/(2*(m+1)) y(1)=y1+y(2)*dx END SUBROUTINE load
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
MODULE sphoot_data USE nrtype INTEGER(I4B) :: m,n REAL(SP) :: c2,dx,gamma END MODULE sphoot_data
1322
Chapter B17.
Two Point Boundary Value Problems
These modules function just like common blocks to communicate variables from sphoot to the various subsidiary routines. The advantage of a module is that it allows complete specification of the variables.
f90
MODULE sphoot_data...MODULE sphoot_caller
SUBROUTINE derivs(x,y,dydx) USE nrtype USE sphoot_data IMPLICIT NONE REAL(SP), INTENT(IN) :: x REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: dydx Evaluates derivatives for odeint. dydx(1)=y(2) dydx(2)=(2.0_sp*x*(m+1.0_sp)*y(2)-(y(3)-c2*x*x)*y(1))/(1.0_sp-x*x) dydx(3)=0.0 END SUBROUTINE derivs
MODULE sphfpt_data USE nrtype INTEGER(I4B) :: m,n REAL(SP) :: c2,dx,gamma END MODULE sphfpt_data
Communicates with load1, load2, score, and derivs.
MODULE sphfpt_caller USE nrtype INTEGER(I4B) :: nn2 REAL(SP) :: x1,x2,xf END MODULE sphfpt_caller
Communicates with shootf.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE score(x2,y,f) USE nrtype USE sphoot_data IMPLICIT NONE REAL(SP), INTENT(IN) :: x2 REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: f Tests whether boundary condition at x = 0 is satisfied. f(1)=merge(y(2),y(1), mod(n-m,2) == 0 ) END SUBROUTINE score
Chapter B17. Two Point Boundary Value Problems
1323
SUBROUTINE load1(x1,v1,y) USE nrtype USE sphfpt_data IMPLICIT NONE REAL(SP), INTENT(IN) :: x1 REAL(SP), DIMENSION(:), INTENT(IN) :: v1 REAL(SP), DIMENSION(:), INTENT(OUT) :: y Supplies starting values for integration at x = −1 + dx. REAL(SP) :: y1 y(3)=v1(1) y1=merge(gamma,-gamma,mod(n-m,2) == 0) y(2)=-(y(3)-c2)*y1/(2*(m+1)) y(1)=y1+y(2)*dx END SUBROUTINE load1
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
PROGRAM sphfpt Sample program using shootf. Computes eigenvalues of spheroidal harmonics Smn (x; c) for m ≥ 0 and n ≥ m. Be sure that routine funcv for newt is provided by shootf (§17.2). The routine derivs is the same as for sphoot. USE nrtype; USE nrutil, ONLY : arth USE nr, ONLY : newt USE sphfpt_data USE sphfpt_caller IMPLICIT NONE INTEGER(I4B), PARAMETER :: N1=2,N2=1,NTOT=N1+N2 REAL(SP), PARAMETER :: DXX=1.0e-4_sp REAL(SP), DIMENSION(:), POINTER :: v1,v2 REAL(SP), DIMENSION(NTOT), TARGET :: v LOGICAL(LGT) :: check v1=>v(1:N2) v2=>v(N2+1:NTOT) nn2=N2 dx=DXX Avoid evaluating derivatives exactly at x = do ±1. write(*,*) ’input m,n,c-squared (999 to end)’ read(*,*) m,n,c2 if (c2 == 999.0) exit if ((n < m) .or. (m < 0)) cycle gamma=(-0.5_sp)**m*product(& Compute γ of equation (17.4.20). arth(n+1,1,m)*(arth(real(n,sp),-1.0_sp,m)/arth(1,1,m))) v1(1)=n*(n+1)-m*(m+1)+c2/2.0_sp Initial guess for eigenvalue and function value. v2(2)=v1(1) v2(1)=gamma*(1.0_sp-(v2(2)-c2)*dx/(2*(m+1))) x1=-1.0_sp+dx Set range of integration. x2=1.0_sp-dx xf=0.0 Fitting point. call newt(v,check) Find v that zeros function f in score. if (check) then write(*,*) ’shootf failed; bad initial guess’ exit else write(*,’(1x,t6,a)’) ’mu(m,n)’ write(*,’(1x,f12.6)’) v1(1) end if end do END PROGRAM sphfpt
1324
Chapter B17.
Two Point Boundary Value Problems
SUBROUTINE score(xf,y,f) USE nrtype USE sphfpt_data IMPLICIT NONE REAL(SP), INTENT(IN) :: xf REAL(SP), DIMENSION(:), INTENT(IN) :: y REAL(SP), DIMENSION(:), INTENT(OUT) :: f Tests whether solutions match at fitting point x = 0. f(1:3)=y(1:3) END SUBROUTINE score
These modules function just like common blocks to communicate variables from sphfpt to the various subsidiary routines. The advantage of a module is that it allows complete specification of the variables.
f90
MODULE sphfpt_data...MODULE sphfpt_caller
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE load2(x2,v2,y) USE nrtype USE sphfpt_data IMPLICIT NONE REAL(SP), INTENT(IN) :: x2 REAL(SP), DIMENSION(:), INTENT(IN) :: v2 REAL(SP), DIMENSION(:), INTENT(OUT) :: y Supplies starting values for integration at x = 1 − dx. y(3)=v2(2) y(1)=v2(1) y(2)=(y(3)-c2)*y(1)/(2*(m+1)) END SUBROUTINE load2
SUBROUTINE fred2(a,b,t,f,w,g,ak) USE nrtype; USE nrutil, ONLY : assert_eq,unit_matrix USE nr, ONLY : gauleg,lubksb,ludcmp IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), DIMENSION(:), INTENT(OUT) :: t,f,w INTERFACE FUNCTION g(t) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: t REAL(SP), DIMENSION(size(t)) :: g END FUNCTION g FUNCTION ak(t,s) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: t,s REAL(SP), DIMENSION(size(t),size(s)) :: ak END FUNCTION ak END INTERFACE Solves a linear Fredholm equation of the second kind by N -point Gaussian quadrature. On input, a and b are the limits of integration. g and ak are user-supplied external functions. g returns g(t) as a vector of length N for a vector of N arguments, while ak returns λK(t, s) as an N ×N matrix. The routine returns arrays t and f of length N containing the abscissas ti of the Gaussian quadrature and the solution f at these abscissas. Also returned is the array w of length N of Gaussian weights for use with the Nystrom interpolation routine fredin. INTEGER(I4B) :: n INTEGER(I4B), DIMENSION(size(f)) :: indx REAL(SP) :: d REAL(SP), DIMENSION(size(f),size(f)) :: omk n=assert_eq(size(f),size(t),size(w),’fred2’) call gauleg(a,b,t,w) Replace gauleg with another routine if not call unit_matrix(omk) using Gauss-Legendre quadrature. omk=omk-ak(t,t)*spread(w,dim=1,ncopies=n) Form 1 − λK. f=g(t) call ludcmp(omk,indx,d) Solve linear equations. call lubksb(omk,indx,f) END SUBROUTINE fred2
f90
call unit_matrix(omk) The unit matrix routine in nrutil does exactly what its name suggests.
1325
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B18. Integral Equations and Inverse Theory
1326
Chapter B18.
Integral Equations and Inverse Theory
omk=omk-ak(t,t)*spread(w,dim=1,ncopies=n) By now this idiom should be second nature: the first column of ak gets multiplied by the first element of w, and so on.
FUNCTION ak(t,s) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: t,s REAL(SP), DIMENSION(size(t),size(s)) :: ak END FUNCTION ak END INTERFACE Input are arrays t and w of length N containing the abscissas and weights of the N -point Gaussian quadrature, and the solution array f of length N from fred2. The function fredin returns the array of values of f at an array of points x using the Nystrom interpolation formula. On input, a and b are the limits of integration. g and ak are user-supplied external functions. g returns g(t) as a vector of length N for a vector of N arguments, while ak returns λK(t, s) as an N × N matrix. INTEGER(I4B) :: n n=assert_eq(size(f),size(t),size(w),’fredin’) fredin=g(x)+matmul(ak(x,t),w*f) END FUNCTION fredin
f90
Fortran 90 allows very concise coding here, which also happens to be much closer to the mathematical formulation than the loops required in Fortran 77.
fredin=g(x)+matmul...
SUBROUTINE voltra(t0,h,t,f,g,ak) USE nrtype; USE nrutil, ONLY : array_copy,assert_eq,unit_matrix USE nr, ONLY : lubksb,ludcmp IMPLICIT NONE REAL(SP), INTENT(IN) :: t0,h REAL(SP), DIMENSION(:), INTENT(OUT) :: t REAL(SP), DIMENSION(:,:), INTENT(OUT) :: f INTERFACE FUNCTION g(t) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: t REAL(SP), DIMENSION(:), POINTER :: g END FUNCTION g FUNCTION ak(t,s)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION fredin(x,a,b,t,f,w,g,ak) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(SP), INTENT(IN) :: a,b REAL(SP), DIMENSION(:), INTENT(IN) :: x,t,f,w REAL(SP), DIMENSION(size(x)) :: fredin INTERFACE FUNCTION g(t) USE nrtype IMPLICIT NONE REAL(SP), DIMENSION(:), INTENT(IN) :: t REAL(SP), DIMENSION(size(t)) :: g END FUNCTION g
Chapter B18. Integral Equations and Inverse Theory
1327
The routine voltra requires an argument that is a function returning a vector, but we don’t know the dimension of the vector at compile time. The solution is to make the function return a pointer to the vector. This is not the same thing as a pointer to a function, which is not allowed in Fortran 90. When you use the pointer in the routine, Fortran 90 figures out from the context that you want the vector of values, so the code remains highly readable. Similarly, the argument ak is a function returning a pointer to a matrix. The coding of the user-supplied functions g and ak deserves some comment: functions returning pointers to arrays are potential memory leaks if the arrays are allocated dynamically in the functions. Here the user knows in advance the dimension of the problem, and so there is no need to use dynamical allocation in the functions. For example, in a two-dimensional problem, you can code g as follows:
f90
FUNCTION g(t)...REAL(SP), DIMENSION(:), POINTER :: g
FUNCTION g(t) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: t REAL(SP), DIMENSION(:), POINTER :: g REAL(SP), DIMENSION(2), TARGET, SAVE :: gg g=>gg g(1)=... g(2)=... END FUNCTION g
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: t,s REAL(SP), DIMENSION(:,:), POINTER :: ak END FUNCTION ak END INTERFACE Solves a set of M linear Volterra equations of the second kind using the extended trapezoidal rule. On input, t0 is the starting point of the integration. The routine takes N − 1 steps of size h and returns the abscissas in t, a vector of length N . The solution at these points is returned in the M × N matrix f. g is a user-supplied external function that returns a pointer to the M -dimensional vector of functions gk (t), while ak is another user-supplied external function that returns a pointer to the M × M matrix K(t, s). INTEGER(I4B) :: i,j,n,ncop,nerr,m INTEGER(I4B), DIMENSION(size(f,1)) :: indx REAL(SP) :: d REAL(SP), DIMENSION(size(f,1)) :: b REAL(SP), DIMENSION(size(f,1),size(f,1)) :: a n=assert_eq(size(f,2),size(t),’voltra: n’) t(1)=t0 Initialize. call array_copy(g(t(1)),f(:,1),ncop,nerr) m=assert_eq(size(f,1),ncop,ncop+nerr,’voltra: m’) do i=2,n Take a step h. t(i)=t(i-1)+h b=g(t(i))+0.5_sp*h*matmul(ak(t(i),t(1)),f(:,1)) Accumulate right-hand side do j=2,i-1 of linear equations in b. b=b+h*matmul(ak(t(i),t(j)),f(:,j)) end do call unit_matrix(a) Left-hand side goes in maa=a-0.5_sp*h*ak(t(i),t(i)) trix a. call ludcmp(a,indx,d) Solve linear equations. call lubksb(a,indx,b) f(:,i)=b(:) end do END SUBROUTINE voltra
1328
Chapter B18.
Integral Equations and Inverse Theory
and similarly for ak. Suppose, however, we coded g with dynamical allocation:
Now g never gets deallocated; each time we call the function fresh memory gets consumed. If you have a problem that really does require dynamical allocation in a pointer function, you have to be sure to deallocate the pointer in the calling routine. In voltra, for example, we would declare pointers gtemp and aktemp. Then instead of writing simply b=g(t(i))+...
we would write gtemp=>g(t(i)) b=gtemp+... deallocate(gtemp)
and similarly for each pointer function invocation. call array_copy(g(t(1)),f(:,1),ncop,nerr) The routine would work if we replaced this statement with simply f(:,1)=g(t(1)). The purpose of using array copy from nrutil is that we can check that f and g have consistent dimensions with a call to assert eq.
FUNCTION wwghts(n,h,kermom) USE nrtype; USE nrutil, ONLY : geop IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n REAL(SP), INTENT(IN) :: h REAL(SP), DIMENSION(n) :: wwghts INTERFACE FUNCTION kermom(y,m) USE nrtype IMPLICIT NONE REAL(DP), INTENT(IN) :: y INTEGER(I4B), INTENT(IN) :: m REAL(DP), DIMENSION(m) :: kermom END FUNCTION kermom END INTERFACE Returns in wwghts(1:n) weights for the n-point equal-interval quadrature from 0 to (n − 1)h of a function f (x) times an arbitrary (possibly singular) weight function w(x) whose indefinite-integral moments Fn (y) are provided by the user-supplied function kermom. INTEGER(I4B) :: j REAL(DP) :: hh,hi,c,a,b REAL(DP), DIMENSION(4) :: wold,wnew,w hh=h Double precision on internal calculations even though hi=1.0_dp/hh the interface is in single precision. wwghts(1:n)=0.0 Zero all the weights so we can sum into them. wold(1:4)=kermom(0.0_dp,4) Evaluate indefinite integrals at lower end.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION g(t) USE nrtype IMPLICIT NONE REAL(SP), INTENT(IN) :: t REAL(SP), DIMENSION(:), POINTER :: g allocate(g(2)) g(1)=... g(2)=... END FUNCTION g
Chapter B18. Integral Equations and Inverse Theory
1329
MODULE kermom_info USE nrtype REAL(DP) :: kermom_x END MODULE kermom_info
FUNCTION kermom(y,m) USE nrtype USE kermom_info IMPLICIT NONE REAL(DP), INTENT(IN) :: y INTEGER(I4B), INTENT(IN) :: m REAL(DP), DIMENSION(m) :: kermom Returns in kermom(1:m) the first m indefinite-integral moments of one row of the singular part of the kernel. (For this example, m is hard-wired to be 4.) The input variable y labels the column, while kermom x (in the module kermom info) is the row. REAL(DP) :: x,d,df,clog,x2,x3,x4 x=kermom_x We can take x as the lower limit of integration. Thus, we if (y >= x) then return the moment integrals either purely to the left or d=y-x purely to the right of the diagonal. df=2.0_dp*sqrt(d)*d kermom(1:4) = (/ df/3.0_dp, df*(x/3.0_dp+d/5.0_dp),&
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (n >= 4) then Use highest available order. b=0.0 For another problem, you might change this lower do j=1,n-3 limit. c=j-1 This is called k in equation (18.3.5). a=b Set upper and lower limits for this step. b=a+hh if (j == n-3) b=(n-1)*hh Last interval: go all the way to end. wnew(1:4)=kermom(b,4) w(1:4)=(wnew(1:4)-wold(1:4))*geop(1.0_dp,hi,4) Equation (18.3.4). wwghts(j:j+3)=wwghts(j:j+3)+(/& Equation (18.3.5). ((c+1.0_dp)*(c+2.0_dp)*(c+3.0_dp)*w(1)& -(11.0_dp+c*(12.0_dp+c*3.0_dp))*w(2)& +3.0_dp*(c+2.0_dp)*w(3)-w(4))/6.0_dp,& (-c*(c+2.0_dp)*(c+3.0_dp)*w(1)& +(6.0_dp+c*(10.0_dp+c*3.0_dp))*w(2)& -(3.0_dp*c+5.0_dp)*w(3)+w(4))*0.50_dp,& (c*(c+1.0_dp)*(c+3.0_dp)*w(1)& -(3.0_dp+c*(8.0_dp+c*3.0_dp))*w(2)& +(3.0_dp*c+4.0_dp)*w(3)-w(4))*0.50_dp,& (-c*(c+1.0_dp)*(c+2.0_dp)*w(1)& +(2.0_dp+c*(6.0_dp+c*3.0_dp))*w(2)& -3.0_dp*(c+1.0_dp)*w(3)+w(4))/6.0_dp /) wold(1:4)=wnew(1:4) Reset lower limits for moments. end do else if (n == 3) then Lower-order cases; not recommended. wnew(1:3)=kermom(hh+hh,3) w(1:3)= (/ wnew(1)-wold(1), hi*(wnew(2)-wold(2)),& hi**2*(wnew(3)-wold(3)) /) wwghts(1:3)= (/ w(1)-1.50_dp*w(2)+0.50_dp*w(3),& 2.0_dp*w(2)-w(3), 0.50_dp*(w(3)-w(2)) /) else if (n == 2) then wnew(1:2)=kermom(hh,2) wwghts(2)=hi*(wnew(2)-wold(2)) wwghts(1)=wnew(1)-wold(1)-wwghts(2) end if END FUNCTION wwghts
1330
Chapter B18.
Integral Equations and Inverse Theory
df*((x/3.0_dp + 0.4_dp*d)*x + d**2/7.0_dp),& df*(((x/3.0_dp + 0.6_dp*d)*x + 3.0_dp*d**2/7.0_dp)*x& + d**3/9.0_dp) /)
f90
This module functions just like a common block to share the variable kermom x with the routine quadmx.
MODULE kermom_info
SUBROUTINE quadmx(a) USE nrtype; USE nrutil, ONLY : arth,assert_eq,diagadd,outerprod USE nr, ONLY : wwghts,kermom USE kermom_info IMPLICIT NONE REAL(SP), DIMENSION(:,:), INTENT(OUT) :: a Constructs in the N ×N array a the quadrature matrix for an example Fredholm equation of the second kind. The nonsingular part of the kernel is computed within this routine, while the quadrature weights that integrate the singular part of the kernel are obtained via calls to wwghts. An external routine kermom, which supplies indefinite-integral moments of the singular part of the kernel, is passed to wwghts. INTEGER(I4B) :: j,n REAL(SP) :: h,x REAL(SP), DIMENSION(size(a,1)) :: wt n=assert_eq(size(a,1),size(a,2),’quadmx’) h=PI/(n-1) do j=1,n x=(j-1)*h kermom_x=x Put x in the module kermom info for use by kermom. wt(:)=wwghts(n,h,kermom) Part of nonsingular kernel. a(j,:)=wt(:) Put together all the pieces of the kernel. end do wt(:)=cos(arth(0,1,n)*h) a(:,:)=a(:,:)*outerprod(wt(:),wt(:)) call diagadd(a,1.0_sp) Since equation of the second kind, there is diagonal END SUBROUTINE quadmx piece independent of h.
f90
call diagadd...
See discussion of diagadd after hqr on p. 1234.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
else x2=x**2 x3=x2*x x4=x2*x2 d=x-y clog=log(d) kermom(1:4) = (/ d*(clog-1.0_dp),& -0.25_dp*(3.0_dp*x+y-2.0_dp*clog*(x+y))*d,& (-11.0_dp*x3+y*(6.0_dp*x2+y*(3.0_dp*x+2.0_dp*y))& +6.0_dp*clog*(x3-y**3))/18.0_dp,& (-25.0_dp*x4+y*(12.0_dp*x3+y*(6.0_dp*x2+y*& (4.0_dp*x+3.0_dp*y)))+12.0_dp*clog*(x4-y**4))/48.0_dp /) end if END FUNCTION kermom
Chapter B18. Integral Equations and Inverse Theory
1331
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
PROGRAM fredex USE nrtype; USE nrutil, ONLY : arth USE nr, ONLY : quadmx,ludcmp,lubksb IMPLICIT NONE INTEGER(I4B), PARAMETER :: N=40 INTEGER(I4B) :: j INTEGER(I4B), DIMENSION(N) :: indx REAL(SP) :: d REAL(SP), DIMENSION(N) :: g,x REAL(SP), DIMENSION(N,N) :: a This sample program shows how to solve a Fredholm equation of the second kind using the product Nystrom method and a quadrature rule especially constructed for a particular, singular, kernel. Parameter: N is the size of the grid. call quadmx(a) Make the quadrature matrix; all the action is here. call ludcmp(a,indx,d) Decompose the matrix. x(:)=arth(0,1,n)*PI/(n-1) g(:)=sin(x(:)) Construct the right-hand side, here sin x. call lubksb(a,indx,g) Backsubstitute. do j=1,n Write out the solution. write (*,*) j,x(j),g(j) end do write (*,*) ’normal completion’ END PROGRAM fredex
SUBROUTINE sor(a,b,c,d,e,f,u,rjac) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(IN) :: a,b,c,d,e,f REAL(DP), DIMENSION(:,:), INTENT(INOUT) :: u REAL(DP), INTENT(IN) :: rjac INTEGER(I4B), PARAMETER :: MAXITS=1000 REAL(DP), PARAMETER :: EPS=1.0e-5_dp Successive overrelaxation solution of equation (19.5.25) with Chebyshev acceleration. a, b, c, d, e, and f are input as the coefficients of the equation, each dimensioned to the grid size J × J. u is input as the initial guess to the solution, usually zero, and returns with the final value. rjac is input as the spectral radius of the Jacobi iteration, or an estimate of it. Double precision is a good idea for J bigger than about 25. REAL(DP), DIMENSION(size(a,1),size(a,1)) :: resid INTEGER(I4B) :: jmax,jm1,jm2,jm3,n REAL(DP) :: anorm,anormf,omega jmax=assert_eq((/size(a,1),size(a,2),size(b,1),size(b,2), & size(c,1),size(c,2),size(d,1),size(d,2),size(e,1), & size(e,2),size(f,1),size(f,2),size(u,1),size(u,2)/),’sor’) jm1=jmax-1 jm2=jmax-2 jm3=jmax-3 anormf=sum(abs(f(2:jm1,2:jm1))) Compute initial norm of residual and terminate iteration when norm has been reduced by a factor EPS. This computation assumes initial u is zero. omega=1.0 do n=1,MAXITS First do the even-even and odd-odd squares of the grid, i.e., the red squares of the checkerboard: resid(2:jm1:2,2:jm1:2)=a(2:jm1:2,2:jm1:2)*u(3:jmax:2,2:jm1:2)+& b(2:jm1:2,2:jm1:2)*u(1:jm2:2,2:jm1:2)+& c(2:jm1:2,2:jm1:2)*u(2:jm1:2,3:jmax:2)+& d(2:jm1:2,2:jm1:2)*u(2:jm1:2,1:jm2:2)+& e(2:jm1:2,2:jm1:2)*u(2:jm1:2,2:jm1:2)-f(2:jm1:2,2:jm1:2) u(2:jm1:2,2:jm1:2)=u(2:jm1:2,2:jm1:2)-omega*& resid(2:jm1:2,2:jm1:2)/e(2:jm1:2,2:jm1:2) resid(3:jm2:2,3:jm2:2)=a(3:jm2:2,3:jm2:2)*u(4:jm1:2,3:jm2:2)+& b(3:jm2:2,3:jm2:2)*u(2:jm3:2,3:jm2:2)+& c(3:jm2:2,3:jm2:2)*u(3:jm2:2,4:jm1:2)+& d(3:jm2:2,3:jm2:2)*u(3:jm2:2,2:jm3:2)+& e(3:jm2:2,3:jm2:2)*u(3:jm2:2,3:jm2:2)-f(3:jm2:2,3:jm2:2) u(3:jm2:2,3:jm2:2)=u(3:jm2:2,3:jm2:2)-omega*& resid(3:jm2:2,3:jm2:2)/e(3:jm2:2,3:jm2:2) omega=merge(1.0_dp/(1.0_dp-0.5_dp*rjac**2), & 1.0_dp/(1.0_dp-0.25_dp*rjac**2*omega), n == 1) Now do even-odd and odd-even squares of the grid, i.e., the black squares of the checkerboard: resid(3:jm2:2,2:jm1:2)=a(3:jm2:2,2:jm1:2)*u(4:jm1:2,2:jm1:2)+& b(3:jm2:2,2:jm1:2)*u(2:jm3:2,2:jm1:2)+&
1332
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B19. Partial Differential Equations
Chapter B19. Partial Differential Equations
1333
Red-black iterative schemes like the one used in sor are easily parallelizable. Updating the red grid points requires information only from the black grid points, so they can all be updated independently. Similarly the black grid points can all be updated independently. Since nearest neighbors are involved in the updating, communication costs can be kept to a minimum. There are several possibilities for coding the red-black iteration in a data parallel way using only Fortran 90 and no parallel language extensions. One way is to define an N × N logical mask red that is true on the red grid points and false on the black. Then each iteration consists of an update governed by a where(red)...end where block and a where(.not. red)...end where block. We have chosen a more direct coding that avoids the need for storage of the array red. The red update corresponds to the even-even and odd-odd grid points, the black to the even-odd and odd-even points. We can code each of these four cases directly with array sections, as in the routine above. The array section notation used in sor is rather dense and hard to read. We could use pointer aliases to try to simplify things, but since each array section is different, we end up merely giving names to each term that was there all along. Pointer aliases do help if we code sor using a logical mask. Since there may be machines on which this version is faster, and since it is of some pedagogic interest, we give the alternative code:
f90
SUBROUTINE sor_mask(a,b,c,d,e,f,u,rjac) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror IMPLICIT NONE REAL(DP), DIMENSION(:,:), TARGET, INTENT(IN) :: a,b,c,d,e,f REAL(DP), DIMENSION(:,:), TARGET, INTENT(INOUT) :: u REAL(DP), INTENT(IN) :: rjac INTEGER(I4B), PARAMETER :: MAXITS=1000 REAL(DP), PARAMETER :: EPS=1.0e-5_dp REAL(DP), DIMENSION(:,:), ALLOCATABLE :: resid REAL(DP), DIMENSION(:,:), POINTER :: u_int,u_down,u_up,u_left,& u_right,a_int,b_int,c_int,d_int,e_int,f_int INTEGER(I4B) :: jmax,jm1,jm2,jm3,n REAL(DP) anorm,anormf,omega LOGICAL, DIMENSION(:,:), ALLOCATABLE :: red jmax=assert_eq((/size(a,1),size(a,2),size(b,1),size(b,2), &
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
c(3:jm2:2,2:jm1:2)*u(3:jm2:2,3:jmax:2)+& d(3:jm2:2,2:jm1:2)*u(3:jm2:2,1:jm2:2)+& e(3:jm2:2,2:jm1:2)*u(3:jm2:2,2:jm1:2)-f(3:jm2:2,2:jm1:2) u(3:jm2:2,2:jm1:2)=u(3:jm2:2,2:jm1:2)-omega*& resid(3:jm2:2,2:jm1:2)/e(3:jm2:2,2:jm1:2) resid(2:jm1:2,3:jm2:2)=a(2:jm1:2,3:jm2:2)*u(3:jmax:2,3:jm2:2)+& b(2:jm1:2,3:jm2:2)*u(1:jm2:2,3:jm2:2)+& c(2:jm1:2,3:jm2:2)*u(2:jm1:2,4:jm1:2)+& d(2:jm1:2,3:jm2:2)*u(2:jm1:2,2:jm3:2)+& e(2:jm1:2,3:jm2:2)*u(2:jm1:2,3:jm2:2)-f(2:jm1:2,3:jm2:2) u(2:jm1:2,3:jm2:2)=u(2:jm1:2,3:jm2:2)-omega*& resid(2:jm1:2,3:jm2:2)/e(2:jm1:2,3:jm2:2) omega=1.0_dp/(1.0_dp-0.25_dp*rjac**2*omega) anorm=sum(abs(resid(2:jm1,2:jm1))) if (anorm < EPS*anormf) exit end do if (n > MAXITS) call nrerror(’MAXITS exceeded in sor’) END SUBROUTINE sor
1334
Chapter B19.
Partial Differential Equations
SUBROUTINE mglin(u,ncycle) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : interp,rstrct,slvsml IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(INOUT) :: u INTEGER(I4B), INTENT(IN) :: ncycle Full Multigrid Algorithm for solution of linear elliptic equation, here the model problem (19.0.6). On input u contains the right-hand side ρ in an N × N array, while on output it returns the solution. The dimension N is related to the number of grid levels used in the solution, ng below, by N = 2**ng+1. ncycle is the number of V-cycles to be used at each level. INTEGER(I4B) :: j,jcycle,n,ng,ngrid,nn TYPE ptr2d Define a type so we can have an array of pointers REAL(DP), POINTER :: a(:,:) to arrays of grid variables. END TYPE ptr2d TYPE(ptr2d), ALLOCATABLE :: rho(:)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
size(c,1),size(c,2),size(d,1),size(d,2),size(e,1), & size(e,2),size(f,1),size(f,2),size(u,1),size(u,2)/),’sor’) jm1=jmax-1 jm2=jmax-2 jm3=jmax-3 allocate(resid(jm2,jm2),red(jm2,jm2)) Interior is (jmax − 2) × (jmax − 2). red=.false. red(1:jm2:2,1:jm2:2)=.true. red(2:jm3:2,2:jm3:2)=.true. u_int=>u(2:jm1,2:jm1) u_down=>u(3:jmax,2:jm1) u_up=>u(1:jm2,2:jm1) u_left=>u(2:jm1,1:jm2) u_right=>u(2:jm1,3:jmax) a_int=>a(2:jm1,2:jm1) b_int=>b(2:jm1,2:jm1) c_int=>c(2:jm1,2:jm1) d_int=>d(2:jm1,2:jm1) e_int=>e(2:jm1,2:jm1) f_int=>f(2:jm1,2:jm1) anormf=sum(abs(f_int)) omega=1.0 do n=1,MAXITS where(red) resid=a_int*u_down+b_int*u_up+c_int*u_right+& d_int*u_left+e_int*u_int-f_int u_int=u_int-omega*resid/e_int end where omega=merge(1.0_dp/(1.0_dp-0.5_dp*rjac**2), & 1.0_dp/(1.0_dp-0.25_dp*rjac**2*omega), n == 1) where(.not.red) resid=a_int*u_down+b_int*u_up+c_int*u_right+& d_int*u_left+e_int*u_int-f_int u_int=u_int-omega*resid/e_int end where omega=1.0_dp/(1.0_dp-0.25_dp*rjac**2*omega) anorm=sum(abs(resid)) if(anorm < EPS*anormf)exit end do deallocate(resid,red) if (n > MAXITS) call nrerror(’MAXITS exceeded in sor’) END SUBROUTINE sor_mask
Chapter B19. Partial Differential Equations
1335
RECURSIVE SUBROUTINE mg(j,u,rhs) USE nrtype USE nr, ONLY : interp,relax,resid,rstrct,slvsml IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: j REAL(DP), DIMENSION(:,:), INTENT(INOUT) :: u REAL(DP), DIMENSION(:,:), INTENT(IN) :: rhs INTEGER(I4B), PARAMETER :: NPRE=1,NPOST=1 Recursive multigrid iteration. On input, j is the current level, u is the current value of the solution, and rhs is the right-hand side. On output u contains the improved solution at the current level. Parameters: NPRE and NPOST are the number of relaxation sweeps before and after the coarse-grid correction is computed. INTEGER(I4B) :: jpost,jpre REAL(DP), DIMENSION((size(u,1)+1)/2,(size(u,1)+1)/2) :: res,v if (j == 1) then Bottom of V: Solve on coarsest grid. call slvsml(u,rhs) else On downward stoke of the V. do jpre=1,NPRE Pre-smoothing. call relax(u,rhs) end do res=rstrct(resid(u,rhs)) Restriction of the residual is the next r.h.s. v=0.0 Zero for initial guess in next relaxation. call mg(j-1,v,res) Recursive call for the coarse grid correction. u=u+interp(v) On upward stroke of V. do jpost=1,NPOST Post-smoothing. call relax(u,rhs)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
REAL(DP), DIMENSION(:,:), POINTER :: uj,uj_1 n=assert_eq(size(u,1),size(u,2),’mglin’) ng=nint(log(n-1.0)/log(2.0)) if (n /= 2**ng+1) call nrerror(’n-1 must be a power of 2 in mglin’) allocate(rho(ng)) nn=n ngrid=ng allocate(rho(ngrid)%a(nn,nn)) Allocate storage for r.h.s. on grid ng, rho(ngrid)%a=u and fill it with the input r.h.s. do Similarly allocate storage and fill r.h.s. on all coarse if (nn uj allocate(uj(nn,nn)) uj=interp(uj_1) Interpolate from grid j-1 to next finer grid j. deallocate(uj_1) do jcycle=1,ncycle V-cycle loop. call mg(j,uj,rho(j)%a) end do end do u=uj Return solution in u. deallocate(uj) do j=1,ng deallocate(rho(j)%a) end do deallocate(rho) CONTAINS
1336
Chapter B19.
Partial Differential Equations
end do end if END SUBROUTINE mg END SUBROUTINE mglin
f90
do i=1,merge(gamma,1,j /= 2) call mg(j-1,v,res) end do
The merge expression ensures that there is no more than one call to the coarsest level, where the problem is solved exactly. A second improvement in the Fortran 90 version is to make the procedures resid, interp, and rstrct functions instead of subroutines. This allows us to code the algorithm exactly as written mathematically. TYPE ptr2d... The right-hand-side quantity ρ is supplied initially on the finest grid in the argument u. It has to be defined on the coarser grids by restriction, and then supplied as the right-hand side to mg in the nested iteration loop. This loop starts at the coarsest level and progresses up to the finest level. We thus need a data structure to store ρ on all the grid levels. A convenient way to implement this in Fortran 90 is to define a type ptr2d, a pointer to a two-dimensional array a that represents a grid. (In three dimensions, a would of course be three-dimensional.) We then declare the variable ρ as an allocatable array of type ptr2d: TYPE(ptr2d), ALLOCATABLE :: rho(:)
Next we allocate storage for ρ on each level. The number of levels or grids, ng, is known only at run time: allocate(rho(ng))
Then we allocate storage as needed on particular sized grids. For example, allocate(rho(ngrid)%a(nn,nn))
allocates an nn × nn grid for rho on grid number ngrid. The various subsidiary routines of mglin such as rstrct and interp are written to accept two-dimensional arrays as arguments. With the data structure we’ve employed, using these routines is simple. For example, rho(ngrid)%a=rstrct(rho(ngrid+1)%a)
will restrict rho from the grid ngrid+1 to the grid ngrid. The statement is even more readable if we mentally ignore the %a that is tagged onto each variable. (If
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
The Fortran 90 version of mglin (and of mgfas below) is quite different from the Fortran 77 version, although the algorithm is identical. First, we use a recursive implementation. This makes the code much more transparent. It also makes the memory management much better: we simply define the new arrays res and v as automatic arrays of the appropriate dimension on each recursive call to a coarser level. And a third benefit is that it is trivial to change the code to increase the number of multigrid iterations done at level j − 1 by each iteration at level j, i.e., to set the quantity γ in §19.6 to a value greater than one. (Recall that γ = 1 as chosen in mglin gives V-cycles, γ = 2 gives W-cycles.) Simply enclose the recursive call in a do-loop:
Chapter B19. Partial Differential Equations
1337
we actually did omit %a in the code, the compiler would think we meant the array of type ptr2d instead of the grid array.) Note that while Fortran 90 does not allow you to declare an array of pointers directly, you can achieve the same effect by declaring your own type, as we have done with ptr2d in this example.
FUNCTION interp(uc) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(IN) :: uc REAL(DP), DIMENSION(2*size(uc,1)-1,2*size(uc,1)-1) :: interp Coarse-to-fine prolongation by bilinear interpolation. If Nf is the fine-grid dimension and Nc the coarse-grid dimension, then Nf = 2Nc − 1. The coarse-grid solution is input as uc, the fine-grid solution is returned in interp. INTEGER(I4B) :: nc,nf nc=assert_eq(size(uc,1),size(uc,2),’interp’) nf=2*nc-1 interp(1:nf:2,1:nf:2)=uc(1:nc,1:nc) Do elements that are copies. interp(2:nf-1:2,1:nf:2)=0.5_dp*(interp(3:nf:2,1:nf:2)+ & interp(1:nf-2:2,1:nf:2)) Do odd-numbered columns, interpolating vertically. interp(1:nf,2:nf-1:2)=0.5_dp*(interp(1:nf,3:nf:2)+interp(1:nf,1:nf-2:2)) Do even-numbered columns, interpolating horizontally. END FUNCTION interp
SUBROUTINE slvsml(u,rhs) USE nrtype IMPLICIT NONE REAL(DP), DIMENSION(3,3), INTENT(OUT) :: u REAL(DP), DIMENSION(3,3), INTENT(IN) :: rhs Solution of the model problem on the coarsest grid, where h = 12 . The right-hand side is input in rhs(1:3,1:3) and the solution is returned in u(1:3,1:3). REAL(DP) :: h u=0.0 h=0.5_dp u(2,2)=-h*h*rhs(2,2)/4.0_dp END SUBROUTINE slvsml
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION rstrct(uf) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(IN) :: uf REAL(DP), DIMENSION((size(uf,1)+1)/2,(size(uf,1)+1)/2) :: rstrct Half-weighting restriction. If Nc is the coarse-grid dimension, the fine-grid solution is input in the (2Nc − 1) × (2Nc − 1) array uf, the coarse-grid solution is returned in the Nc × Nc array rstrct. INTEGER(I4B) :: nc,nf nf=assert_eq(size(uf,1),size(uf,2),’rstrct’) nc=(nf+1)/2 rstrct(2:nc-1,2:nc-1)=0.5_dp*uf(3:nf-2:2,3:nf-2:2)+0.125_dp*(& Interior points. uf(4:nf-1:2,3:nf-2:2)+uf(2:nf-3:2,3:nf-2:2)+& uf(3:nf-2:2,4:nf-1:2)+uf(3:nf-2:2,2:nf-3:2)) rstrct(1:nc,1)=uf(1:nf:2,1) Boundary points. rstrct(1:nc,nc)=uf(1:nf:2,nf) rstrct(1,1:nc)=uf(1,1:nf:2) rstrct(nc,1:nc)=uf(nf,1:nf:2) END FUNCTION rstrct
1338
Chapter B19.
Partial Differential Equations
f90
See the discussion of red-black relaxation after sor on p. 1333.
FUNCTION resid(u,rhs) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(IN) :: u,rhs REAL(DP), DIMENSION(size(u,1),size(u,1)) :: resid Returns minus the residual for the model problem. Input quantities are u and rhs, while the residual is returned in resid. All three quantities are square arrays with the same odd dimension. INTEGER(I4B) :: n REAL(DP) :: h,h2i n=assert_eq((/size(u,1),size(u,2),size(rhs,1),size(rhs,2)/),’resid’) n=size(u,1) h=1.0_dp/(n-1) h2i=1.0_dp/(h*h) resid(2:n-1,2:n-1)=-h2i*(u(3:n,2:n-1)+u(1:n-2,2:n-1)+u(2:n-1,3:n)+& u(2:n-1,1:n-2)-4.0_dp*u(2:n-1,2:n-1))+rhs(2:n-1,2:n-1) Interior points. resid(1:n,1)=0.0 Boundary points. resid(1:n,n)=0.0 resid(1,1:n)=0.0 resid(n,1:n)=0.0 END FUNCTION resid
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE relax(u,rhs) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(INOUT) :: u REAL(DP), DIMENSION(:,:), INTENT(IN) :: rhs Red-black Gauss-Seidel relaxation for model problem. The current value of the solution u is updated, using the right-hand-side function rhs. u and rhs are square arrays of the same odd dimension. INTEGER(I4B) :: n REAL(DP) :: h,h2 n=assert_eq(size(u,1),size(u,2),size(rhs,1),size(rhs,2),’relax’) h=1.0_dp/(n-1) h2=h*h First do the even-even and odd-odd squares of the grid, i.e., the red squares of the checkerboard: u(2:n-1:2,2:n-1:2)=0.25_dp*(u(3:n:2,2:n-1:2)+u(1:n-2:2,2:n-1:2)+& u(2:n-1:2,3:n:2)+u(2:n-1:2,1:n-2:2)-h2*rhs(2:n-1:2,2:n-1:2)) u(3:n-2:2,3:n-2:2)=0.25_dp*(u(4:n-1:2,3:n-2:2)+u(2:n-3:2,3:n-2:2)+& u(3:n-2:2,4:n-1:2)+u(3:n-2:2,2:n-3:2)-h2*rhs(3:n-2:2,3:n-2:2)) Now do even-odd and odd-even squares of the grid, i.e., the black squares of the checkerboard: u(3:n-2:2,2:n-1:2)=0.25_dp*(u(4:n-1:2,2:n-1:2)+u(2:n-3:2,2:n-1:2)+& u(3:n-2:2,3:n:2)+u(3:n-2:2,1:n-2:2)-h2*rhs(3:n-2:2,2:n-1:2)) u(2:n-1:2,3:n-2:2)=0.25_dp*(u(3:n:2,3:n-2:2)+u(1:n-2:2,3:n-2:2)+& u(2:n-1:2,4:n-1:2)+u(2:n-1:2,2:n-3:2)-h2*rhs(2:n-1:2,3:n-2:2)) END SUBROUTINE relax
Chapter B19. Partial Differential Equations
1339
RECURSIVE SUBROUTINE mg(j,u,rhs,trerr) USE nrtype USE nr, ONLY : interp,lop,relax2,rstrct,slvsm2 IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: j REAL(DP), DIMENSION(:,:), INTENT(INOUT) :: u REAL(DP), DIMENSION(:,:), INTENT(IN), OPTIONAL :: rhs REAL(DP), INTENT(OUT), OPTIONAL :: trerr
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE mgfas(u,maxcyc) USE nrtype; USE nrutil, ONLY : assert_eq,nrerror USE nr, ONLY : interp,lop,rstrct,slvsm2 IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(INOUT) :: u INTEGER(I4B), INTENT(IN) :: maxcyc Full Multigrid Algorithm for FAS solution of nonlinear elliptic equation, here equation (19.6.44). On input u contains the right-hand side ρ in an N × N array, while on output it returns the solution. The dimension N is related to the number of grid levels used in the solution, ng below, by N = 2**ng+1. maxcyc is the maximum number of V-cycles to be used at each level. INTEGER(I4B) :: j,jcycle,n,ng,ngrid,nn REAL(DP) :: res,trerr TYPE ptr2d Define a type so we can have an array of REAL(DP), POINTER :: a(:,:) pointers to arrays of grid variables. END TYPE ptr2d TYPE(ptr2d), ALLOCATABLE :: rho(:) REAL(DP), DIMENSION(:,:), POINTER :: uj,uj_1 n=assert_eq(size(u,1),size(u,2),’mgfas’) ng=nint(log(n-1.0)/log(2.0)) if (n /= 2**ng+1) call nrerror(’n-1 must be a power of 2 in mgfas’) allocate(rho(ng)) nn=n ngrid=ng allocate(rho(ngrid)%a(nn,nn)) Allocate storage for r.h.s. on grid ng, rho(ngrid)%a=u and fill it with ρ from the fine grid. do Similarly allocate storage and fill r.h.s. by reif (nn uj allocate(uj(nn,nn)) uj=interp(uj_1) Interpolate from grid j-1 to next finer grid deallocate(uj_1) j. do jcycle=1,maxcyc V-cycle loop. call mg(j,uj,trerr=trerr) res=sqrt(sum((lop(uj)-rho(j)%a)**2))/nn Form residual dh . if (res < trerr) exit No more V-cycles needed if residual small end do enough. end do u=uj Return solution in u. deallocate(uj) do j=1,ng deallocate(rho(j)%a) end do deallocate(rho) CONTAINS
1340
Chapter B19.
Partial Differential Equations
f90
See the discussion after mglin on p. 1336 for the changes made in the Fortran 90 versions of the multigrid routines from the Fortran 77 versions.
TYPE ptr2d...
See discussion after mglin on p. 1336.
RECURSIVE SUBROUTINE mg(j,u,rhs,trerr) Recall that mgfas solves the problem Lu = 0, but that nonzero right-hand sides appear during the solution. We implement this by having rhs be an optional argument to mg. On the first call at a given level j, the right-hand side is zero and so you just omit it from the calling sequence. On the other hand, the truncation error trerr is computed only on the first call at a given level, so it is also an optional argument that does get supplied on the first call: call mg(j,uj,trerr=trerr)
The second and subsequent calls at a given level supply rhs=tau but omit trerr:
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
INTEGER(I4B), PARAMETER :: NPRE=1,NPOST=1 REAL(DP), PARAMETER :: ALPHA=0.33_dp Recursive multigrid iteration. On input, j is the current level and u is the current value of the solution. For the first call on a given level, the right-hand side is zero, and the optional argument rhs is not present. Subsequent recursive calls supply a nonzero rhs as in equation (19.6.33). On output u contains the improved solution at the current level. When the first call on a given level is made, the relative truncation error τ is returned in the optional argument trerr. Parameters: NPRE and NPOST are the number of relaxation sweeps before and after the coarse-grid correction is computed; ALPHA relates the estimated truncation error to the norm of the residual. INTEGER(I4B) :: jpost,jpre REAL(DP), DIMENSION((size(u,1)+1)/2,(size(u,1)+1)/2) :: v,ut,tau if (j == 1) then Bottom of V: Solve on coarsest grid. call slvsm2(u,rhs+rho(j)%a) else On downward stoke of the V. do jpre=1,NPRE Pre-smoothing. if (present(rhs)) then call relax2(u,rhs+rho(j)%a) else call relax2(u,rho(j)%a) end if end do ut=rstrct(u) R uh . v=ut Make a copy in v. if (present(rhs)) then uh ) − RLh ( uh ) + tau=lop(ut)-rstrct(lop(u)-rhs) Form τh + fH = LH (R else fH . tau=lop(ut)-rstrct(lop(u)) trerr=ALPHA*sqrt(sum(tau**2))/size(tau,1) Estimate truncation error τ . end if call mg(j-1,v,tau) Recursive call for the coarse-grid correction. =u h + P ( uH − R uh ) u=u+interp(v-ut) u new h do jpost=1,NPOST Post-smoothing. if (present(rhs)) then call relax2(u,rhs+rho(j)%a) else call relax2(u,rho(j)%a) end if end do end if END SUBROUTINE mg END SUBROUTINE mgfas
Chapter B19. Partial Differential Equations
1341
call mg(j-1,v,tau)
tau=lop(ut)-rstrct(lop(u))
is nevertheless correct, since rho would cancel out if it were included in lop. This feature is also true in the Fortran 77 code. SUBROUTINE relax2(u,rhs) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(INOUT) :: u REAL(DP), DIMENSION(:,:), INTENT(IN) :: rhs Red-black Gauss-Seidel relaxation for equation (19.6.44). The current value of the solution u is updated, using the right-hand-side function rhs. u and rhs are square arrays of the same odd dimension. INTEGER(I4B) :: n REAL(DP) :: foh2,h,h2i REAL(DP) :: res(size(u,1),size(u,1)) n=assert_eq(size(u,1),size(u,2),size(rhs,1),size(rhs,2),’relax2’) h=1.0_dp/(n-1) h2i=1.0_dp/(h*h) foh2=-4.0_dp*h2i First do the even-even and odd-odd squares of the grid, i.e., the red squares of the checkerboard: res(2:n-1:2,2:n-1:2)=h2i*(u(3:n:2,2:n-1:2)+u(1:n-2:2,2:n-1:2)+& u(2:n-1:2,3:n:2)+u(2:n-1:2,1:n-2:2)-4.0_dp*u(2:n-1:2,2:n-1:2))& +u(2:n-1:2,2:n-1:2)**2-rhs(2:n-1:2,2:n-1:2) u(2:n-1:2,2:n-1:2)=u(2:n-1:2,2:n-1:2)-res(2:n-1:2,2:n-1:2)/& (foh2+2.0_dp*u(2:n-1:2,2:n-1:2)) res(3:n-2:2,3:n-2:2)=h2i*(u(4:n-1:2,3:n-2:2)+u(2:n-3:2,3:n-2:2)+& u(3:n-2:2,4:n-1:2)+u(3:n-2:2,2:n-3:2)-4.0_dp*u(3:n-2:2,3:n-2:2))& +u(3:n-2:2,3:n-2:2)**2-rhs(3:n-2:2,3:n-2:2) u(3:n-2:2,3:n-2:2)=u(3:n-2:2,3:n-2:2)-res(3:n-2:2,3:n-2:2)/& (foh2+2.0_dp*u(3:n-2:2,3:n-2:2)) Now do even-odd and odd-even squares of the grid, i.e., the black squares of the checkerboard: res(3:n-2:2,2:n-1:2)=h2i*(u(4:n-1:2,2:n-1:2)+u(2:n-3:2,2:n-1:2)+& u(3:n-2:2,3:n:2)+u(3:n-2:2,1:n-2:2)-4.0_dp*u(3:n-2:2,2:n-1:2))& +u(3:n-2:2,2:n-1:2)**2-rhs(3:n-2:2,2:n-1:2) u(3:n-2:2,2:n-1:2)=u(3:n-2:2,2:n-1:2)-res(3:n-2:2,2:n-1:2)/& (foh2+2.0_dp*u(3:n-2:2,2:n-1:2)) res(2:n-1:2,3:n-2:2)=h2i*(u(3:n:2,3:n-2:2)+u(1:n-2:2,3:n-2:2)+& u(2:n-1:2,4:n-1:2)+u(2:n-1:2,2:n-3:2)-4.0_dp*u(2:n-1:2,3:n-2:2))& +u(2:n-1:2,3:n-2:2)**2-rhs(2:n-1:2,3:n-2:2) u(2:n-1:2,3:n-2:2)=u(2:n-1:2,3:n-2:2)-res(2:n-1:2,3:n-2:2)/& (foh2+2.0_dp*u(2:n-1:2,3:n-2:2)) END SUBROUTINE relax2
f90
See the discussion of red-black relaxation after sor on p. 1333.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Note that we can omit the keyword rhs from this call because the variable tau appears in the correct order of arguments. However, in the other call above, the keyword trerr must be supplied because rhs has been omitted. The example equation that is solved in mgfas, equation (19.6.44), is almost linear, and the code is set up so that ρ is supplied as part of the right-hand side instead of pulling it over to the left-hand side. The variable rho is visible to mg by host association. Note also that the function lop does not include rho, but that the statement
1342
Chapter B19.
Partial Differential Equations
FUNCTION lop(u) USE nrtype; USE nrutil, ONLY : assert_eq IMPLICIT NONE REAL(DP), DIMENSION(:,:), INTENT(IN) :: u REAL(DP), DIMENSION(size(u,1),size(u,1)) :: lop uh ) for equation (19.6.44). u and lop are square arrays of the same Given u, returns Lh ( odd dimension. INTEGER(I4B) :: n REAL(DP) :: h,h2i n=assert_eq(size(u,1),size(u,2),’lop’) h=1.0_dp/(n-1) h2i=1.0_dp/(h*h) lop(2:n-1,2:n-1)=h2i*(u(3:n,2:n-1)+u(1:n-2,2:n-1)+u(2:n-1,3:n)+& u(2:n-1,1:n-2)-4.0_dp*u(2:n-1,2:n-1))+u(2:n-1,2:n-1)**2 Interior points. lop(1:n,1)=0.0 Boundary points. lop(1:n,n)=0.0 lop(1,1:n)=0.0 lop(n,1:n)=0.0 END FUNCTION lop
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE slvsm2(u,rhs) USE nrtype IMPLICIT NONE REAL(DP), DIMENSION(3,3), INTENT(OUT) :: u REAL(DP), DIMENSION(3,3), INTENT(IN) :: rhs Solution of equation (19.6.44) on the coarsest grid, where h = 12 . The right-hand side is input in rhs(1:3,1:3) and the solution is returned in u(1:3,1:3). REAL(DP) :: disc,fact,h u=0.0 h=0.5_dp fact=2.0_dp/h**2 disc=sqrt(fact**2+rhs(2,2)) u(2,2)=-rhs(2,2)/(fact+disc) END SUBROUTINE slvsm2
Volume 1’s Fortran 77 routine machar performed various clever contortions (due to Cody, Malcolm, and others) to discover the underlying properties of a machine’s floating-point representation. Fortran 90, by contrast, provides a built-in set of “numeric inquiry functions” that accomplish the same goal. The routine machar included here makes use of these and is included largely for compatibility with the previous version.
f90
SUBROUTINE machar(ibeta,it,irnd,ngrd,machep,negep,iexp,minexp,& maxexp,eps,epsneg,xmin,xmax) USE nrtype IMPLICIT NONE INTEGER(I4B), INTENT(OUT) :: ibeta,iexp,irnd,it,machep,maxexp,minexp,negep,ngrd REAL(SP), INTENT(OUT) :: eps,epsneg,xmax,xmin REAL(SP), PARAMETER :: RX=1.0 Determines and returns machine-specific parameters affecting floating-point arithmetic. Returned values include ibeta, the floating-point radix; it, the number of base-ibeta digits in the floating-point mantissa; eps, the smallest positive number that, added to 1.0, is not equal to 1.0; epsneg, the smallest positive number that, subtracted from 1.0, is not equal to 1.0; xmin, the smallest representable positive number; and xmax, the largest representable positive number. See text for description of other returned parameters. Change all REAL(SP) declarations to REAL(DP) to find double-precision parameters. REAL(SP) :: a,beta,betah,one,temp,tempa,two,zero ibeta=radix(RX) Most of the parameters are easily determined it=digits(RX) from intrinsic functions. machep=exponent(nearest(RX,RX)-RX)-1 negep=exponent(nearest(RX,-RX)-RX)-1 minexp=minexponent(RX)-1 maxexp=maxexponent(RX) iexp=nint(log(real(maxexp-minexp+2,sp))/log(2.0_sp)) eps=real(ibeta,sp)**machep epsneg=real(ibeta,sp)**negep xmax=huge(RX) xmin=tiny(RX) one=RX Determine irnd. two=one+one zero=one-one beta=real(ibeta,sp) a=beta**(-negep) irnd=0 betah=beta/two temp=a+betah if (temp-a /= zero) irnd=1 tempa=a+beta temp=tempa+betah if ((irnd == 0) .and. (temp-tempa /= zero)) irnd=2 ngrd=0 Determine ngrd.
1343
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B20. Less-Numerical Algorithms
1344
Chapter B20.
Less-Numerical Algorithms
temp=one+eps if ((irnd == 0) .and. (temp*one-one /= zero)) ngrd=1 temp=xmin/two if (temp /= zero) irnd=irnd+3 Adjust irnd to reflect partial underflow. END SUBROUTINE machar
FUNCTION igray(n,is) USE nrtype IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n,is INTEGER(I4B) :: igray For zero or positive values of is, return the Gray code of n; if is is negative, return the inverse Gray code of n. INTEGER(I4B) :: idiv,ish if (is >= 0) then This is the easy direction! igray=ieor(n,n/2) else This is the more complicated direction: In hierarchical stages, ish=-1 starting with a one-bit right shift, cause each bit to be igray=n XORed with all more significant bits. do idiv=ishft(igray,ish) igray=ieor(igray,idiv) if (idiv = 0) then Initialize the remainder register. cword=ior(jinit,ishft(jinit,8))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Chapter B20. Less-Numerical Algorithms
1345
FUNCTION hibyte() INTEGER(I2B) :: hibyte Extracts the high byte of the 2-byte integer cword. hibyte = ishft(cword,-8) END FUNCTION hibyte FUNCTION lobyte() INTEGER(I2B) :: lobyte Extracts the low byte of the 2-byte integer cword. lobyte = iand(cword,255_I2B) END FUNCTION lobyte FUNCTION icrc1(crc,onech) INTEGER(I2B), INTENT(IN) :: crc CHARACTER(1), INTENT(IN) :: onech INTEGER(I2B) :: icrc1 Given a remainder up to now, return the new CRC after one character is added. This routine is functionally equivalent to icrc(,,-1,1), but slower. It is used by icrc to initialize its table. INTEGER(I2B) :: i,ich, bit16, ccitt DATA bit16,ccitt /Z’8000’, Z’1021’/ ich=ichar(onech) Here is where the character is folded into the icrc1=ieor(crc,ishft(ich,8)) register. do i=1,8 Here is where 8 one-bit shifts, and some XORs icrc1=merge(ieor(ccitt,ishft(icrc1,1)), & with the generator polynomial, ishft(icrc1,1), iand(icrc1,bit16) /= 0) are done. end do END FUNCTION icrc1 END FUNCTION icrc
f90
The embedded functions hibyte and lobyte always act on the same variable, cword. Thus they don’t need any explicit argument.
FUNCTION decchk(string,ch) USE nrtype; USE nrutil, ONLY : ifirstloc IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(IN) :: string CHARACTER(1), INTENT(OUT) :: ch LOGICAL(LGT) :: decchk Decimal check digit computation or verification. Returns as ch a check digit for appending to string. In this mode, ignore the returned logical value. If string already ends with a check digit, returns the function value .true. if the check digit is valid, otherwise .false. In this mode, ignore the returned value of ch. Note that string and ch contain ASCII characters corresponding to the digits 0-9, not byte values in that range. Other ASCII characters are allowed in string, and are ignored in calculating the check digit. INTEGER(I4B) :: i,j,k,m INTEGER(I4B) :: ip(0:9,0:7) = reshape((/ & Group multiplication and permuta0,1,2,3,4,5,6,7,8,9,1,5,7,6,2,8,3,0,9,4,& tion tables. 5,8,0,3,7,9,6,1,4,2,8,9,1,6,0,4,3,5,2,7,9,4,5,3,1,2,6,8,7,0,& 4,2,8,6,5,7,3,9,0,1,2,7,9,3,8,0,6,4,1,5,7,0,4,6,9,1,3,2,5,8 /),&
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
else if (jrev < 0) then If not initializing, do we reverse the register? cword=ior(rchr(hibyte()),ishft(rchr(lobyte()),8)) end if do j=1,size(buf) Main loop over the characters in the array. ich=ichar(buf(j)) if (jrev < 0) ich=rchr(ich) cword=ieor(icrctb(ieor(ich,hibyte())),ishft(lobyte(),8)) end do icrc=merge(cword, & Do we need to reverse the output? ior(rchr(hibyte()),ishft(rchr(lobyte()),8)), jrev >= 0) CONTAINS
1346
Chapter B20.
Less-Numerical Algorithms
f90
Note the use of the utility function ifirstloc to find the first (in this case, the only) correct check digit.
The Huffman and arithmetic coding routines exemplify the use of modules to encapsulate user-defined data types. In these algorithms, “the code” is a fairly complicated construct containing scalar and array data. We define types huffcode and arithcode, then can pass “the code” from the routine that constructs it to the routine that uses it as a single variable.
f90
MODULE huf_info USE nrtype IMPLICIT NONE TYPE huffcode INTEGER(I4B) :: nch,nodemax INTEGER(I4B), DIMENSION(:), POINTER :: icode,left,iright,ncode END TYPE huffcode CONTAINS SUBROUTINE huff_allocate(hcode,mc) USE nrtype IMPLICIT NONE TYPE(huffcode) :: hcode INTEGER(I4B) :: mc INTEGER(I4B) :: mq mq=2*mc-1 allocate(hcode%icode(mq),hcode%ncode(mq),hcode%left(mq),hcode%iright(mq)) hcode%icode(:)=0 hcode%ncode(:)=0 END SUBROUTINE huff_allocate SUBROUTINE huff_deallocate(hcode) USE nrtype IMPLICIT NONE TYPE(huffcode) :: hcode deallocate(hcode%iright,hcode%left,hcode%ncode,hcode%icode) nullify(hcode%icode) nullify(hcode%ncode) nullify(hcode%left) nullify(hcode%iright)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
(/ 10,8 /) ) INTEGER(I4B) :: ij(0:9,0:9) = reshape((/ & 0,1,2,3,4,5,6,7,8,9,1,2,3,4,0,9,5,6,7,8,2,3,4,0,1,8,9,5,6,& 7,3,4,0,1,2,7,8,9,5,6,4,0,1,2,3,6,7,8,9,5,5,6,7,8,9,0,1,2,3,& 4,6,7,8,9,5,4,0,1,2,3,7,8,9,5,6,3,4,0,1,2,8,9,5,6,7,2,3,4,0,& 1,9,5,6,7,8,1,2,3,4,0 /),(/ 10,10 /)) k=0 m=0 do j=1,size(string) Look at successive characters. i=ichar(string(j)) if (i >= 48 .and. i hcode%nch .or. k < 1) call & ray index range 1..nch. nrerror(’hufenc: ich out of range’) do n=hcode%ncode(k),1,-1 Loop over the bits in the stored Huffman nc=nb/8+1 code for ich. if (nc > size(codep)) codep=>reallocate(codep,2*size(codep)) l=mod(nb,8) if (l == 0) codep(nc)=char(0) if (btest(hcode%icode(k),n-1)) then Set appropriate bits in codep. ntmp=ibset(ichar(codep(nc)),l) codep(nc)=char(ntmp) end if nb=nb+1 end do END SUBROUTINE hufenc
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE hufapp(l) IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: l Used by hufmak to maintain a heap structure in the array indx(1:l). INTEGER(I4B) :: i,j,k,n n=nused i=l k=indx(i) do if (i > n/2) exit j=i+i if (j < n .and. nprob(indx(j)) > nprob(indx(j+1))) & j=j+1 if (nprob(k) size(code)) then Ran out of input; return with ich=nch ich=hcode%nch indicating end of message. RETURN end if l=mod(nb,8) Now decoding this bit. nb=nb+1 if (btest(ichar(code(nc)),l)) then Branch left or right in tree, depending on node=hcode%iright(node) its value. else node=hcode%left(node) end if if (node acode%minint) RETURN Initialization complete. acode%jdif=(acode%jdif+1)*acode%nrad-1 end do call nrerror(’NWK too small in arcode’) else if (isign > 0) then If encoding, check for valid input character. if (ich > acode%nch .or. ich < 0) call nrerror(’bad ich in arcode’) else If decoding, locate the character ich by bija=ichar(codep(lcd))-acode%ilob(acode%nc) section. do j=acode%nc+1,NWK
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE arcmak(nfreq,nradd,acode) USE nrtype; USE nrutil, ONLY : cumsum,nrerror USE arcode_info IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: nradd INTEGER(I4B), DIMENSION(:), INTENT(IN) :: nfreq TYPE(arithcode) :: acode INTEGER(I4B), PARAMETER :: MAXINT=huge(nradd) Given a table nfreq of the frequency of occurrence of size(nfreq) symbols, and given a desired output radix nradd, initialize the cumulative frequency table and other variables for arithmetic compression. Store the code in acode. MAXINT is a large positive integer that does not overflow. if (nradd > 256) call nrerror(’output radix may not exceed 256 in arcmak’) acode%minint=MAXINT/nradd acode%nch=size(nfreq) acode%nrad=nradd call arcode_allocate(acode,acode%nch) acode%ncumfq(1)=0 acode%ncumfq(2:acode%nch+1)=cumsum(max(nfreq(1:acode%nch),1)) acode%ncumfq(acode%nch+2)=acode%ncumfq(acode%nch+1)+1 acode%ncum=acode%ncumfq(acode%nch+2) END SUBROUTINE arcmak
Chapter B20. Less-Numerical Algorithms
1351
FUNCTION jtry(m,n,k) USE nrtype IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: m,n,k INTEGER(I4B) :: jtry Calculate (m*n)/k without overflow. Program efficiency can be improved by substituting an assembly language routine that does integer multiply to a double register. jtry=int((real(m,dp)*real(n,dp))/real(k,dp)) END FUNCTION jtry SUBROUTINE arcsum(iin,iout,ja,nwk,nrad,nc) USE nrtype IMPLICIT NONE INTEGER(I4B), DIMENSION(:), INTENT(IN) :: iin INTEGER(I4B), DIMENSION(:), INTENT(OUT) :: iout INTEGER(I4B), INTENT(IN) :: nwk,nrad,nc INTEGER(I4B), INTENT(INOUT) :: ja
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
ja=ja*acode%nrad+(ichar(codep(j+lcd-acode%nc))-acode%ilob(j)) end do ich=0 ihi=acode%nch+1 do if (ihi-ich = jtry(acode%jdif,acode%ncumfq(m+1),acode%ncum)) then ich=m else ihi=m end if end do if (ich == acode%nch) RETURN Detected end of message. end if Following code is common for encoding and decoding. Convert character ich to a new subrange [ilob,iupb). jh=jtry(acode%jdif,acode%ncumfq(ich+2),acode%ncum) jl=jtry(acode%jdif,acode%ncumfq(ich+1),acode%ncum) acode%jdif=jh-jl call arcsum(acode%ilob,acode%iupb,jh,NWK,acode%nrad,acode%nc) How many leading digits to output (if encoding) or skip over? call arcsum(acode%ilob,acode%ilob,jl,NWK,acode%nrad,acode%nc) do j=acode%nc,NWK if (ich /= acode%nch .and. acode%iupb(j) /= acode%ilob(j)) exit if (acode%nc > size(codep)) codep=>reallocate(codep,2*size(codep)) if (isign > 0) codep(lcd)=char(acode%ilob(j)) lcd=lcd+1 end do if (j > NWK) RETURN Ran out of message. Did someone forget to acode%nc=j encode a terminating ncd? j=0 How many digits to shift? do if (acode%jdif >= acode%minint) exit j=j+1 acode%jdif=acode%jdif*acode%nrad end do if (acode%nc-j < 1) call nrerror(’NWK too small in arcode’) if (j /= 0) then Shift them. acode%iupb((acode%nc-j):(NWK-j))=acode%iupb(acode%nc:NWK) acode%ilob((acode%nc-j):(NWK-j))=acode%ilob(acode%nc:NWK) end if acode%nc=acode%nc-j acode%iupb((NWK-j+1):NWK)=0 acode%ilob((NWK-j+1):NWK)=0 end if Normal return. CONTAINS
1352
Chapter B20.
Less-Numerical Algorithms
MODULE mpops USE nrtype INTEGER(I4B), PARAMETER :: NPAR_ICARRY=64 CONTAINS SUBROUTINE icarry(karry,isum,nbits) IMPLICIT NONE INTEGER(I4B), INTENT(OUT) :: karry Perform deferred carry operation on an array isum of multiple-precision digits. Nonzero bits of higher order than nbits (typically 8) are carried to the next-lower (leftward) component of isum. The final (most leftward) carry value is returned as karry. INTEGER(I2B), DIMENSION(:), INTENT(INOUT) :: isum INTEGER(I4B), INTENT(IN) :: nbits INTEGER(I4B) :: n,j INTEGER(I2B), DIMENSION(size(isum)) :: ihi INTEGER(I2B) :: mb,ihh n=size(isum) mb=ishft(1,nbits)-1 Make mask for low-order bits. karry=0 if (n < NPAR_ICARRY ) then do j=n,2,-1 Keep going until all carries have cascaded. ihh=ishft(isum(j),-nbits) if (ihh /= 0) then isum(j)=iand(isum(j),mb) isum(j-1)=isum(j-1)+ihh end if end do ihh=ishft(isum(1),-nbits) isum(1)=iand(isum(1),mb) karry=karry+ihh else do ihi=ishft(isum,-nbits) Get high bits. if (all(ihi == 0)) exit Check if done. where (ihi /= 0) isum=iand(isum,mb) Remove bits to be carried and add where (ihi(2:n) /= 0) isum(1:n-1)=isum(1:n-1)+ihi(2:n) them to left. karry=karry+ihi(1) Final carry. end do end if END SUBROUTINE icarry
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Add the integer ja to the radix nrad multiple-precision integer iin(nc..nwk). Return the result in iout(nc..nwk). INTEGER(I4B) :: j,jtmp,karry karry=0 do j=nwk,nc+1,-1 jtmp=ja ja=ja/nrad iout(j)=iin(j)+(jtmp-ja*nrad)+karry if (iout(j) >= nrad) then iout(j)=iout(j)-nrad karry=1 else karry=0 end if end do iout(nc)=iin(nc)+ja+karry END SUBROUTINE arcsum END SUBROUTINE arcode
Chapter B20. Less-Numerical Algorithms
1353
SUBROUTINE mpsub(is,w,u,v,n) IMPLICIT NONE INTEGER(I4B), INTENT(OUT) :: is CHARACTER(1), DIMENSION(:), INTENT(OUT) :: w CHARACTER(1), DIMENSION(:), INTENT(IN) :: u,v INTEGER(I4B), INTENT(IN) :: n Subtracts the unsigned radix 256 integer v(1:n) from u(1:n) yielding the unsigned integer w(1:n). If the result is negative (wraps around), is is returned as −1; otherwise it is returned as 0. INTEGER(I4B) :: karry INTEGER(I2B), DIMENSION(n) :: isum isum=255+ichar(u(1:n))-ichar(v(1:n)) isum(n)=isum(n)+1 call icarry(karry,isum,8_I4B) w(1:n)=char(isum) is=karry-1 END SUBROUTINE mpsub SUBROUTINE mpsad(w,u,n,iv) IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: w CHARACTER(1), DIMENSION(:), INTENT(IN) :: u INTEGER(I4B), INTENT(IN) :: n,iv Short addition: The integer iv (in the range 0 ≤ iv ≤ 255) is added to the unsigned radix 256 integer u(1:n), yielding w(1:n+1). INTEGER(I4B) :: karry INTEGER(I2B), DIMENSION(n) :: isum isum=ichar(u(1:n)) isum(n)=isum(n)+iv call icarry(karry,isum,8_I4B) w(2:n+1)=char(isum) w(1)=char(karry) END SUBROUTINE mpsad SUBROUTINE mpsmu(w,u,n,iv) IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: w CHARACTER(1), DIMENSION(:), INTENT(IN) :: u INTEGER(I4B), INTENT(IN) :: n,iv Short multiplication: The unsigned radix 256 integer u(1:n) is multiplied by the integer iv (in the range 0 ≤ iv ≤ 255), yielding w(1:n+1). INTEGER(I4B) :: karry INTEGER(I2B), DIMENSION(n) :: isum isum=ichar(u(1:n))*iv call icarry(karry,isum,8_I4B) w(2:n+1)=char(isum) w(1)=char(karry) END SUBROUTINE mpsmu SUBROUTINE mpneg(u,n) IMPLICIT NONE
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE mpadd(w,u,v,n) IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: w CHARACTER(1), DIMENSION(:), INTENT(IN) :: u,v INTEGER(I4B), INTENT(IN) :: n Adds the unsigned radix 256 integers u(1:n) and v(1:n) yielding the unsigned integer w(1:n+1). INTEGER(I2B), DIMENSION(n) :: isum INTEGER(I4B) :: karry isum=ichar(u(1:n))+ichar(v(1:n)) call icarry(karry,isum,8_I4B) w(2:n+1)=char(isum) w(1)=char(karry) END SUBROUTINE mpadd
1354
Chapter B20.
Less-Numerical Algorithms
SUBROUTINE mplsh(u,n) IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(INOUT) :: u INTEGER(I4B), INTENT(IN) :: n Left shift u(2..n+1) onto u(1:n). u(1:n)=u(2:n+1) END SUBROUTINE mplsh SUBROUTINE mpmov(u,v,n) IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: u CHARACTER(1), DIMENSION(:), INTENT(IN) :: v INTEGER(I4B), INTENT(IN) :: n Move v(1:n) onto u(1:n). u(1:n)=v(1:n) END SUBROUTINE mpmov SUBROUTINE mpsdv(w,u,n,iv,ir) IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: w CHARACTER(1), DIMENSION(:), INTENT(IN) :: u INTEGER(I4B), INTENT(IN) :: n,iv INTEGER(I4B), INTENT(OUT) :: ir Short division: The unsigned radix 256 integer u(1:n) is divided by the integer iv (in the range 0 ≤ iv ≤ 255), yielding a quotient w(1:n) and a remainder ir (with 0 ≤ ir ≤ 255). Note: Your Numerical Recipes authors don’t know how to parallelize this routine in Fortran 90! INTEGER(I4B) :: i,j ir=0 do j=1,n i=256*ir+ichar(u(j)) w(j)=char(i/iv) ir=mod(i,iv) end do END SUBROUTINE mpsdv END MODULE mpops
!
SUBROUTINE mpmul(w,u,v,n,m) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : realft IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n,m CHARACTER(1), DIMENSION(:), INTENT(IN) :: u,v CHARACTER(1), DIMENSION(:), INTENT(OUT) :: w The logical dimensions are: CHARACTER(1) :: w(n+m),u(n),v(m) REAL(DP), PARAMETER :: RX=256.0 Uses fast Fourier transform to multiply the unsigned radix 256 integers u(1:n) and v(1:m), yielding a product w(1:n+m). INTEGER(I4B) :: j,mn,nn REAL(DP) :: cy,t REAL(DP), DIMENSION(:), ALLOCATABLE :: a,b,tb mn=max(m,n) nn=1 Find the smallest useable power of two for the transform.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
CHARACTER(1), DIMENSION(:), INTENT(INOUT) :: u INTEGER(I4B), INTENT(IN) :: n Ones-complement negate the unsigned radix 256 integer u(1:n). INTEGER(I4B) :: karry INTEGER(I2B), DIMENSION(n) :: isum isum=255-ichar(u(1:n)) isum(n)=isum(n)+1 call icarry(karry,isum,8_I4B) u(1:n)=char(isum) END SUBROUTINE mpneg
Chapter B20. Less-Numerical Algorithms
1355
do
SUBROUTINE mpinv(u,v,n,m) USE nrtype; USE nrutil, ONLY : poly USE nr, ONLY : mpmul USE mpops, ONLY : mpmov,mpneg IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: u CHARACTER(1), DIMENSION(:), INTENT(IN) :: v INTEGER(I4B), INTENT(IN) :: n,m INTEGER(I4B), PARAMETER :: MF=4 REAL(SP), PARAMETER :: BI=1.0_sp/256.0_sp Character string v(1:m) is interpreted as a radix 256 number with the radix point after (nonzero) v(1); u(1:n) is set to the most significant digits of its reciprocal, with the radix point after u(1). INTEGER(I4B) :: i,j,mm REAL(SP) :: fu CHARACTER(1), DIMENSION(:), ALLOCATABLE :: rr,s allocate(rr(max(n,m)+n+1),s(n)) mm=min(MF,m) fu=1.0_sp/poly(BI,real(ichar(v(:)),sp)) Use ordinary floating arithmetic to get an do j=1,n initial approximation. i=int(fu) u(j)=char(i) fu=256.0_sp*(fu-i) end do do Iterate Newton’s rule to convergence. call mpmul(rr,u,v,n,m) Construct 2 − U V in S. call mpmov(s,rr(2:),n) call mpneg(s,n) s(1)=char(ichar(s(1))-254) Multiply SU into U . call mpmul(rr,s,u,n,n) call mpmov(u,rr(2:),n)
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (nn >= mn) exit nn=nn+nn end do nn=nn+nn allocate(a(nn),b(nn),tb((nn-1)/2)) a(1:n)=ichar(u(1:n)) Move U to a double-precision floating array. a(n+1:nn)=0.0 b(1:m)=ichar(v(1:m)) Move V to a double-precision floating array. b(m+1:nn)=0.0 call realft(a(1:nn),1) Perform the convolution: First, the two Fourier transcall realft(b(1:nn),1) forms. b(1)=b(1)*a(1) Then multiply the complex results (real and imaginary b(2)=b(2)*a(2) parts). tb=b(3:nn:2) b(3:nn:2)=tb*a(3:nn:2)-b(4:nn:2)*a(4:nn:2) b(4:nn:2)=tb*a(4:nn:2)+b(4:nn:2)*a(3:nn:2) call realft(b(1:nn),-1) Then do the inverse Fourier transform. b(:)=b(:)/(nn/2) cy=0.0 Make a final pass to do all the carries. do j=nn,1,-1 t=b(j)+cy+0.5_dp The 0.5 allows for roundoff error. b(j)=mod(t,RX) cy=int(t/RX) end do if (cy >= RX) call nrerror(’mpmul: sanity check failed in fftmul’) w(1)=char(int(cy)) Copy answer to output. w(2:(n+m))=char(int(b(1:(n+m-1)))) deallocate(a,b,tb) END SUBROUTINE mpmul
1356
Chapter B20.
Less-Numerical Algorithms
if (all(ichar(s(2:n-1)) == 0)) exit end do deallocate(rr,s) END SUBROUTINE mpinv
SUBROUTINE mpdiv(q,r,u,v,n,m) USE nrtype; USE nrutil, ONLY : nrerror USE nr, ONLY : mpinv,mpmul USE mpops, ONLY : mpsad,mpmov,mpsub IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: q,r CHARACTER(1), DIMENSION(:), INTENT(IN) :: u,v The logical dimensions are: CHARACTER(1) :: q(n-m+1),r(m),u(n),v(m) INTEGER(I4B), INTENT(IN) :: n,m Divides unsigned radix 256 integers u(1:n) by v(1:m) (with m ≤ n required), yielding a quotient q(1:n-m+1) and a remainder r(1:m). INTEGER(I4B), PARAMETER :: MACC=6 INTEGER(I4B) :: is CHARACTER(1), DIMENSION(:), ALLOCATABLE, TARGET :: rr,s CHARACTER(1), DIMENSION(:), POINTER :: rr2,s3 allocate(rr(2*(n+MACC)),s(2*(n+MACC))) rr2=>rr(2:) s3=>s(3:) call mpinv(s,v,n+MACC,m) Set S = 1/V . call mpmul(rr,s,u,n+MACC,n) Set Q = SU . call mpsad(s,rr,n+n+MACC/2,1) call mpmov(q,s3,n-m+1) call mpmul(rr,q,v,n-m+1,m) Multiply and subtract to get the remainder. call mpsub(is,rr2,u,rr2,n) if (is /= 0) call nrerror(’MACC too small in mpdiv’) call mpmov(r,rr(n-m+2:),m) deallocate(rr,s) END SUBROUTINE mpdiv
SUBROUTINE mpsqrt(w,u,v,n,m) USE nrtype; USE nrutil, ONLY : poly USE nr, ONLY : mpmul USE mpops, ONLY : mplsh,mpmov,mpneg,mpsdv IMPLICIT NONE CHARACTER(1), DIMENSION(:), INTENT(OUT) :: w,u CHARACTER(1), DIMENSION(:), INTENT(IN) :: v INTEGER(I4B), INTENT(IN) :: n,m INTEGER(I4B), PARAMETER :: MF=3 REAL(SP), PARAMETER :: BI=1.0_sp/256.0_sp Character string v(1:m) is interpreted as a radix 256 number with the radix point after v(1); w(1:n) is set to its square root (radix point after w(1)), and u(1:n) is set to the reciprocal thereof (radix point before u(1)). w and u need not be distinct, in which case they are set to the square root. INTEGER(I4B) :: i,ir,j,mm REAL(SP) :: fu CHARACTER(1), DIMENSION(:), ALLOCATABLE :: r,s allocate(r(2*n),s(2*n)) mm=min(m,MF) fu=1.0_sp/sqrt(poly(BI,real(ichar(v(:)),sp))) Use ordinary floating arithmetic do j=1,n to get an initial approximai=int(fu) tion. u(j)=char(i) fu=256.0_sp*(fu-i) end do do Iterate Newton’s rule to convergence. call mpmul(r,u,u,n,n) Construct S = (3 − V U 2 )/2.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
!
If fractional part of S is not zero, it has not converged to 1.
Chapter B20. Less-Numerical Algorithms
1357
SUBROUTINE mp2dfr(a,s,n,m) USE nrtype USE mpops, ONLY : mplsh,mpsmu IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B), INTENT(OUT) :: m CHARACTER(1), DIMENSION(:), INTENT(INOUT) :: a CHARACTER(1), DIMENSION(:), INTENT(OUT) :: s INTEGER(I4B), PARAMETER :: IAZ=48 Converts a radix 256 fraction a(1:n) (radix point before a(1)) to a decimal fraction represented as an ascii string s(1:m), where m is a returned value. The input array a(1:n) is destroyed. NOTE: For simplicity, this routine implements a slow (∝ N 2) algorithm. Fast (∝ N ln N ), more complicated, radix conversion algorithms do exist. INTEGER(I4B) :: j m=int(2.408_sp*n) do j=1,m call mpsmu(a,a,n,10) s(j)=char(ichar(a(1))+IAZ) call mplsh(a,n) end do END SUBROUTINE mp2dfr
SUBROUTINE mppi(n) USE nrtype USE nr, ONLY : mp2dfr,mpinv,mpmul,mpsqrt USE mpops, ONLY : mpadd,mplsh,mpmov,mpsdv IMPLICIT NONE INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B), PARAMETER :: IAOFF=48 Demonstrate multiple precision routines by calculating and printing the first n bytes of π. INTEGER(I4B) :: ir,j,m CHARACTER(1), DIMENSION(n) :: sx,sxi CHARACTER(1), DIMENSION(2*n) :: t,y CHARACTER(1), DIMENSION(3*n) :: s CHARACTER(1), DIMENSION(n+1) :: x,bigpi t(1)=char(2) Set T = 2. t(2:n)=char(0) √ call mpsqrt(x,x,t,n,n) Set X0 = 2.√ call mpadd(bigpi,t,x,n) Set π0 = 2 + 2. call mplsh(bigpi,n) call mpsqrt(sx,sxi,x,n,n) Set Y0 = 21/4 .
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call mplsh(r,n) call mpmul(s,r,v,n,min(m,n)) call mplsh(s,n) call mpneg(s,n) s(1)=char(ichar(s(1))-253) call mpsdv(s,s,n,2,ir) if (any(ichar(s(2:n-1)) /= 0)) then If fractional part of S is not zero, it has not converged to 1. call mpmul(r,s,u,n,n) Replace U by SU . call mpmov(u,r(2:),n) cycle end if call mpmul(r,u,v,n,min(m,n)) Get square root from reciprocal and return. call mpmov(w,r(2:),n) deallocate(r,s) RETURN end do END SUBROUTINE mpsqrt
1358
Chapter B20.
Less-Numerical Algorithms
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
call mpmov(y,sx,n) do 1/2 −1/2 )/2. call mpadd(x,sx,sxi,n) Set Xi+1 = (Xi + Xi call mpsdv(x,x(2:),n,2,ir) 1/2 −1/2 call mpsqrt(sx,sxi,x,n,n) Form the temporary T = Yi Xi+1 +Xi+1 . call mpmul(t,y,sx,n,n) call mpadd(t(2:),t(2:),sxi,n) x(1)=char(ichar(x(1))+1) Increment Xi+1 and Yi by 1. y(1)=char(ichar(y(1))+1) call mpinv(s,y,n,n) Set Yi+1 = T /(Yi + 1). call mpmul(y,t(3:),s,n,n) call mplsh(y,n) call mpmul(t,x,s,n,n) Form temporary T = (Xi+1 + 1)/(Yi + 1). m=mod(255+ichar(t(2)),256) If T = 1 then we have converged. if (abs(ichar(t(n+1))-m) > 1 .or. any(ichar(t(3:n)) /= m)) then call mpmul(s,bigpi,t(2:),n,n) Set πi+1 = T πi . call mpmov(bigpi,s(2:),n) cycle end if write (*,*) ’pi=’ s(1)=char(ichar(bigpi(1))+IAOFF) s(2)=’.’ call mp2dfr(bigpi(2:),s(3:),n-1,m) Convert to decimal for printing. NOTE: The conversion routine, for this demonstration only, is a slow (∝ N 2 ) algorithm. Fast (∝ N ln N ), more complicated, radix conversion algorithms do exist. write (*,’(1x,64a1)’) (s(j),j=1,m+1) RETURN end do END SUBROUTINE mppi
The references collected here are those of general usefulness, cited in this volume. For references to the material in Volume 1, see the References section of that volume. A first group of references relates to the Fortran 90 language itself:
Metcalf, M., and Reid, J. 1996, Fortran 90/95 Explained (Oxford: Oxford University Press). Kerrigan, J.F. 1993, Migrating to Fortran 90 (Sebastopol, CA: O’Reilly). Brainerd, W.S., Goldberg, C.H., and Adams, J.C. 1996, Programmer’s Guide to Fortran 90, 3rd ed. (New York: Springer-Verlag). A second group of references relates to, or includes material on, parallel programming and algorithms:
Akl, S.G. 1989, The Design and Analysis of Parallel Algorithms (Englewood Cliffs, NJ: Prentice Hall). Bertsekas, D.P., and Tsitsiklis, J.N. 1989, Parallel and Distributed Computation: Numerical Methods (Englewood Cliffs, NJ: Prentice Hall). Carey, G.F. 1989, Parallel Supercomputing: Methods, Algorithms, and Applications (New York: Wiley). Fountain, T.J. 1994, Parallel Computing: Principles and Practice (New York: Cambridge University Press). Fox, G.C., et al. 1988, Solving Problems on Concurrent Processors, Volume I (Englewood Cliffs, NJ: Prentice Hall). Golub, G., and Ortega, J.M. 1993, Scientific Computing: An Introduction with Parallel Computing (San Diego, CA: Academic Press). Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns Hopkins University Press). Hockney, R.W., and Jesshope, C.R. 1988, Parallel Computers 2 (Bristol and Philadelphia: Adam Hilger). Kumar, V., et al. 1994, Introduction to Parallel Computing: Design and Analysis of Parallel Algorithms (Redwood City, CA: Benjamin/Cummings). Lewis, T.G., and El-Rewini, H. 1992, Introduction to Parallel Computing (Englewood Cliffs, NJ: Prentice Hall). 1359
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
References
References
1360
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Modi, J.J. 1988, Parallel Algorithms and Matrix Computation (New York: Oxford University Press). Smith, J.R. 1993, The Design and Analysis of Parallel Algorithms (New York: Oxford University Press). Van de Velde, E. 1994, Concurrent Scientific Computing (New York: Springer-Verlag). Van Loan, C.F. 1992, Computational Frameworks for the Fast Fourier Transform (Philadelphia: S.I.A.M.).
C1.1 Numerical Recipes Types (nrtype) The file supplied as nrtype.f90 contains a single module named nrtype, which in turn contains definitions for a number of named constants (that is, PARAMETERs), and a couple of elementary derived data types used by the sparse matrix routines in this book. Of the named constants, by far the most important are those that define the KIND types of virtually all the variables used in this book: I4B, I2B, and I1B for integer variables, SP and DP for real variables (and SPC and DPC for the corresponding complex cases), and LGT for the default logical type. MODULE nrtype Symbolic names for kind types of 4-, 2-, and 1-byte integers: INTEGER, PARAMETER :: I4B = SELECTED_INT_KIND(9) INTEGER, PARAMETER :: I2B = SELECTED_INT_KIND(4) INTEGER, PARAMETER :: I1B = SELECTED_INT_KIND(2) Symbolic names for kind types of single- and double-precision reals: INTEGER, PARAMETER :: SP = KIND(1.0) INTEGER, PARAMETER :: DP = KIND(1.0D0) Symbolic names for kind types of single- and double-precision complex: INTEGER, PARAMETER :: SPC = KIND((1.0,1.0)) INTEGER, PARAMETER :: DPC = KIND((1.0D0,1.0D0)) Symbolic name for kind type of default logical: INTEGER, PARAMETER :: LGT = KIND(.true.) Frequently used mathematical constants (with precision to spare): REAL(SP), PARAMETER :: PI=3.141592653589793238462643383279502884197_sp REAL(SP), PARAMETER :: PIO2=1.57079632679489661923132169163975144209858_sp REAL(SP), PARAMETER :: TWOPI=6.283185307179586476925286766559005768394_sp REAL(SP), PARAMETER :: SQRT2=1.41421356237309504880168872420969807856967_sp REAL(SP), PARAMETER :: EULER=0.5772156649015328606065120900824024310422_sp REAL(DP), PARAMETER :: PI_D=3.141592653589793238462643383279502884197_dp REAL(DP), PARAMETER :: PIO2_D=1.57079632679489661923132169163975144209858_dp REAL(DP), PARAMETER :: TWOPI_D=6.283185307179586476925286766559005768394_dp Derived data types for sparse matrices, single and double precision (see use in Chapter B2): TYPE sprs2_sp INTEGER(I4B) :: n,len REAL(SP), DIMENSION(:), POINTER :: val INTEGER(I4B), DIMENSION(:), POINTER :: irow INTEGER(I4B), DIMENSION(:), POINTER :: jcol END TYPE sprs2_sp TYPE sprs2_dp INTEGER(I4B) :: n,len REAL(DP), DIMENSION(:), POINTER :: val
1361
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
C1. Listing of Utility Modules (nrtype and nrutil)
1362
Appendix C1.
Listing of Utility Modules (nrtype and nrutil)
INTEGER(I4B), DIMENSION(:), POINTER :: irow INTEGER(I4B), DIMENSION(:), POINTER :: jcol END TYPE sprs2_dp END MODULE nrtype
You might hope that changing all the Numerical Recipes routines from single precision to double precision would be as simple as redefining the values of SP and DP in nrtype. Well . . . not quite. Converting algorithms to a higher precision is not a purely mechanical task because of the distinction between “roundoff error” and “truncation error.” (Please see Volume 1, §1.2, if you are not familiar with these concepts.) While increasing the precision implied by the kind values SP and DP will indeed reduce a routine’s roundoff error, it will not reduce any truncation error that may be intrinsic to the algorithm. Sometimes, a routine contains “accuracy parameters” that can be adjusted to reduce the truncation error to the new, desired level. In other cases, however, the truncation error cannot be so easily reduced; then, a whole new algorithm is needed. Clearly such new algorithms are beyond the scope of a simple mechanical “conversion.” If, despite these cautionary words, you want to proceed with converting some routines to a higher precision, here are some hints: If your machine has a kind type that is distinct from, and has equal or greater precision than, the kind type that we use for DP, then, in nrtype, you can simply redefine DP to this new highest precision and redefine SP to what was previously DP. For example, DEC machines usually have a “quadruple precision” real type available, which can be used in this way. You should not need to make any further edits of nrtype or nrutil. If, on the other hand, the kind type that we already use for DP is the highest precision available, then you must leave DP defined as it is, and redefine SP in nrtype to be this same kind type. Now, however, you will also have to edit nrutil, because some overloaded routines that were previously distinguishable (by the different kind types) will now be seen by the compiler as indistinguishable — and it will object strenuously. Simply delete all the “ dp” function names from the list of overloaded procedures (i.e., from the MODULE PROCEDURE statements). Note that it is not necessary to delete the routines from the MODULE itself. Similarly, in the interface file nr.f90 you must delete the “ dp” interfaces, except for the sprs... routines. (Since they have TYPE(sprs2 dp) or TYPE(sprs2 sp), they are treated as distinct even though they have functionally equivalent kind types.) Finally, the following table gives some suggestions for changing the accuracy parameters, or constants, in some of the routines. Please note that this table is not necessarily complete, and that higher-precision performance is not guaranteed for all the routines, even if you make all the changes indicated. The above edits, and these suggestions, do, however, work in the majority of cases.
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
About Converting to Higher Precision
C1.1 Numerical Recipes Types (nrtype)
change...
to...
beschb bessi bessik bessj bessjy broydn
NUSE1=5,NUSE2=5
NUSE1=7,NUSE2=8
TOLMIN=1.0e-6 sp
TOLMIN=1.0e-12 sp
fdjac frprmn gauher gaujac gaulag gauleg hypgeo linmin newt
EPS=1.0e-4 sp
EPS=1.0e-8 sp
EPS=1.0e-10 sp
EPS=1.0e-18 sp
EPS=3.0e-13 dp EPS=3.0e-14 dp
EPS=1.0e-14 dp EPS=1.0e-14 dp
EPS=3.0e-13 dp
EPS=1.0e-14 dp
EPS=3.0e-14 dp
EPS=1.0e-14 dp
EPS=1.0e-6 sp
EPS=1.0e-14 sp
TOL=1.0e-4 sp
TOL=1.0e-8 sp
TOLF=1.0e-4 sp TOLMIN=1.0e-6 sp
TOLF=1.0e-8 sp TOLMIN=1.0e-12 sp
EPS1=0.001 sp
EPS1=1.0e-6 sp
probks qromb qromo qroot qsimp qtrap rc rd rf rj sfroid shoot shootf simplx sncndn sor sphfpt sphoot svdfit zroots
IACC=40
IACC=200
EPS=1.0e-10 dp
EPS=epsilon(x)
IACC=40
IACC=160
EPS=1.0e-10 dp TOLF=1.0e-4 sp
EPS=epsilon(x) TOLF=1.0e-8 sp
EPS2=1.0e-8 sp
EPS2=1.0e-16 sp
EPS=1.0e-6 sp
EPS=1.0e-10 sp
EPS=1.0e-6 sp TINY=1.0e-6 sp
EPS=1.0e-10 sp TINY=1.0e-14 sp
EPS=1.0e-6 sp
EPS=1.0e-10 sp
EPS=1.0e-6 sp
EPS=1.0e-10 sp
ERRTOL=0.04 sp
ERRTOL=0.0012 sp
ERRTOL=0.05 sp ERRTOL=0.08 sp
ERRTOL=0.0015 sp ERRTOL=0.0025 sp
ERRTOL=0.05 sp
ERRTOL=0.0015 sp
conv=5.0e-6 sp
conv=1.0e-14 sp
EPS=1.0e-6 sp
EPS=1.0e-14 sp
EPS=1.0e-6 sp
EPS=1.0e-14 sp
EPS=1.0e-6 sp CA=0.0003 sp
EPS=1.0e-14 sp CA=1.0e-8 sp
EPS=1.0e-5 dp
EPS=1.0e-13 dp
DXX=1.0e-4 sp
DXX=1.0e-8 sp
dx=1.0e-4 sp
dx=1.0e-8 sp
TOL=1.0e-5 sp EPS=1.0e-6 sp
TOL=1.0e-13 sp EPS=1.0e-14 sp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
In routine...
1363
1364
Appendix C1.
Listing of Utility Modules (nrtype and nrutil)
C1.2 Numerical Recipes Utilities (nrutil)
MODULE nrutil TABLE OF CONTENTS OF THE NRUTIL MODULE: routines that move data: array copy, swap, reallocate routines returning a location as an integer value ifirstloc, imaxloc, iminloc routines for argument checking and error handling: assert, assert eq, nrerror routines relating to polynomials and recurrences arth, geop, cumsum, cumprod, poly, polyterm, zroots unity routines for ”outer” operations on vectors outerand, outersum, outerdiff, outerprod, outerdiv routines for scatter-with-combine scatter add, scatter max routines for skew operations on matrices diagadd, diagmult, get diag, put diag,
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
The file supplied as nrutil.f90 contains a single module named nrutil, which contains specific implementations for all the Numerical Recipes utility functions described in detail in Chapter 23. The specific implementations given are something of a compromise between demonstrating parallel techniques (when they can be achieved in Fortran 90) and running efficiently on conventional, serial machines. The parameters at the beginning of the module (names beginning with NPAR ) are typically related to array lengths below which the implementations revert to serial operations. On a purely serial machine, these can be set to large values to suppress many parallel constructions. The length and repetitiveness of the nrutil.f90 file stems in large part from its extensive use of overloading. Indeed, the file would be even longer if we overloaded versions for all the applicable data types that each utility could, in principle, instantiate. The descriptions in Chapter 23 detail both the full set of intended data types and shapes for each routine, and also the types and shapes actually here implemented (which can also be gleaned by examining the file). The intended result of all this overloading is, in essence, to give the utility routines the desirable properties of many of the Fortran 90 intrinsic functions, namely, to be both generic (apply to many data types) and elemental (apply element-by-element to arbitrary shapes). Fortran 95’s provision of user-defined elemental functions will reduce the multiplicity of overloading in some of our routines; unfortunately the necessity to overload for multiple data types will still be present. Finally, it is worth reemphasizing the following point, already made in Chapter 23: The purpose of the nrutil utilities is to remove from the Numerical Recipes programs just those programming tasks and “idioms” whose efficient implementation is most hardware and compiler dependent, so as to allow for specific, efficient implementations on different machines. One should therefore not expect the utmost in efficiency from the general purpose, one-size-fits-all, implementation listed here. Correspondingly, we would encourage the incorporation of efficient nrutil implementations, and/or comparable capabilities under different names, with as broad as possible a set of overloaded data types, in libraries associated with specific compilers or machines. In support of this goal, we have specifically put this Appendix C1, and the files nrtype.f90 and nrutil.f90, into the public domain.
C1.2 Numerical Recipes Utilities (nrutil)
1365
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
unit matrix, lower triangle, upper triangle miscellaneous routines vabs USE nrtype Parameters for crossover from serial to parallel algorithms (these are used only within this nrutil module): IMPLICIT NONE INTEGER(I4B), PARAMETER :: NPAR_ARTH=16,NPAR2_ARTH=8 Each NPAR2 must be ≤ the INTEGER(I4B), PARAMETER :: NPAR_GEOP=4,NPAR2_GEOP=2 corresponding NPAR. INTEGER(I4B), PARAMETER :: NPAR_CUMSUM=16 INTEGER(I4B), PARAMETER :: NPAR_CUMPROD=8 INTEGER(I4B), PARAMETER :: NPAR_POLY=8 INTEGER(I4B), PARAMETER :: NPAR_POLYTERM=8 Next, generic interfaces for routines with overloaded versions. Naming conventions for appended codes in the names of overloaded routines are as follows: r=real, d=double precision, i=integer, c=complex, z=double-precision complex, h=character, l=logical. Any of r,d,i,c,z,h,l may be followed by v=vector or m=matrix (v,m suffixes are used only when needed to resolve ambiguities). Routines that move data: INTERFACE array_copy MODULE PROCEDURE array_copy_r, array_copy_d, array_copy_i END INTERFACE INTERFACE swap MODULE PROCEDURE swap_i,swap_r,swap_rv,swap_c, & swap_cv,swap_cm,swap_z,swap_zv,swap_zm, & masked_swap_rs,masked_swap_rv,masked_swap_rm END INTERFACE INTERFACE reallocate MODULE PROCEDURE reallocate_rv,reallocate_rm,& reallocate_iv,reallocate_im,reallocate_hv END INTERFACE Routines returning a location as an integer value (ifirstloc, iminloc are not currently overloaded and so do not have a generic interface here): INTERFACE imaxloc MODULE PROCEDURE imaxloc_r,imaxloc_i END INTERFACE Routines for argument checking and error handling (nrerror is not currently overloaded): INTERFACE assert MODULE PROCEDURE assert1,assert2,assert3,assert4,assert_v END INTERFACE INTERFACE assert_eq MODULE PROCEDURE assert_eq2,assert_eq3,assert_eq4,assert_eqn END INTERFACE Routines relating to polynomials and recurrences (cumprod, zroots unity are not currently overloaded): INTERFACE arth MODULE PROCEDURE arth_r, arth_d, arth_i END INTERFACE INTERFACE geop MODULE PROCEDURE geop_r, geop_d, geop_i, geop_c, geop_dv END INTERFACE INTERFACE cumsum MODULE PROCEDURE cumsum_r,cumsum_i END INTERFACE INTERFACE poly MODULE PROCEDURE poly_rr,poly_rrv,poly_dd,poly_ddv,& poly_rc,poly_cc,poly_msk_rrv,poly_msk_ddv END INTERFACE INTERFACE poly_term MODULE PROCEDURE poly_term_rr,poly_term_cc END INTERFACE Routines for “outer” operations on vectors (outerand, outersum, outerdiv are not currently overloaded): INTERFACE outerprod
1366
Appendix C1.
Listing of Utility Modules (nrtype and nrutil)
Routines that move data: SUBROUTINE array_copy_r(src,dest,n_copied,n_not_copied) Copy array where size of source not known in advance. REAL(SP), DIMENSION(:), INTENT(IN) :: src REAL(SP), DIMENSION(:), INTENT(OUT) :: dest INTEGER(I4B), INTENT(OUT) :: n_copied, n_not_copied n_copied=min(size(src),size(dest)) n_not_copied=size(src)-n_copied dest(1:n_copied)=src(1:n_copied) END SUBROUTINE array_copy_r SUBROUTINE array_copy_d(src,dest,n_copied,n_not_copied) REAL(DP), DIMENSION(:), INTENT(IN) :: src REAL(DP), DIMENSION(:), INTENT(OUT) :: dest INTEGER(I4B), INTENT(OUT) :: n_copied, n_not_copied n_copied=min(size(src),size(dest)) n_not_copied=size(src)-n_copied dest(1:n_copied)=src(1:n_copied) END SUBROUTINE array_copy_d SUBROUTINE array_copy_i(src,dest,n_copied,n_not_copied) INTEGER(I4B), DIMENSION(:), INTENT(IN) :: src INTEGER(I4B), DIMENSION(:), INTENT(OUT) :: dest INTEGER(I4B), INTENT(OUT) :: n_copied, n_not_copied n_copied=min(size(src),size(dest)) n_not_copied=size(src)-n_copied dest(1:n_copied)=src(1:n_copied) END SUBROUTINE array_copy_i SUBROUTINE swap_i(a,b) Swap the contents of a and b. INTEGER(I4B), INTENT(INOUT) :: a,b INTEGER(I4B) :: dum dum=a a=b b=dum END SUBROUTINE swap_i
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
MODULE PROCEDURE outerprod_r,outerprod_d END INTERFACE INTERFACE outerdiff MODULE PROCEDURE outerdiff_r,outerdiff_d,outerdiff_i END INTERFACE Routines for scatter-with-combine, scatter add, scatter max: INTERFACE scatter_add MODULE PROCEDURE scatter_add_r,scatter_add_d END INTERFACE INTERFACE scatter_max MODULE PROCEDURE scatter_max_r,scatter_max_d END INTERFACE Routines for skew operations on matrices (unit matrix, lower triangle, upper triangle not currently overloaded): INTERFACE diagadd MODULE PROCEDURE diagadd_rv,diagadd_r END INTERFACE INTERFACE diagmult MODULE PROCEDURE diagmult_rv,diagmult_r END INTERFACE INTERFACE get_diag MODULE PROCEDURE get_diag_rv, get_diag_dv END INTERFACE INTERFACE put_diag MODULE PROCEDURE put_diag_rv, put_diag_r END INTERFACE Other routines (vabs is not currently overloaded): CONTAINS
C1.2 Numerical Recipes Utilities (nrutil)
1367
SUBROUTINE swap_r(a,b) REAL(SP), INTENT(INOUT) :: a,b REAL(SP) :: dum dum=a a=b b=dum END SUBROUTINE swap_r
SUBROUTINE swap_c(a,b) COMPLEX(SPC), INTENT(INOUT) :: a,b COMPLEX(SPC) :: dum dum=a a=b b=dum END SUBROUTINE swap_c SUBROUTINE swap_cv(a,b) COMPLEX(SPC), DIMENSION(:), INTENT(INOUT) :: a,b COMPLEX(SPC), DIMENSION(SIZE(a)) :: dum dum=a a=b b=dum END SUBROUTINE swap_cv SUBROUTINE swap_cm(a,b) COMPLEX(SPC), DIMENSION(:,:), INTENT(INOUT) :: a,b COMPLEX(SPC), DIMENSION(size(a,1),size(a,2)) :: dum dum=a a=b b=dum END SUBROUTINE swap_cm SUBROUTINE swap_z(a,b) COMPLEX(DPC), INTENT(INOUT) :: a,b COMPLEX(DPC) :: dum dum=a a=b b=dum END SUBROUTINE swap_z SUBROUTINE swap_zv(a,b) COMPLEX(DPC), DIMENSION(:), INTENT(INOUT) :: a,b COMPLEX(DPC), DIMENSION(SIZE(a)) :: dum dum=a a=b b=dum END SUBROUTINE swap_zv SUBROUTINE swap_zm(a,b) COMPLEX(DPC), DIMENSION(:,:), INTENT(INOUT) :: a,b COMPLEX(DPC), DIMENSION(size(a,1),size(a,2)) :: dum dum=a a=b b=dum END SUBROUTINE swap_zm SUBROUTINE masked_swap_rs(a,b,mask) REAL(SP), INTENT(INOUT) :: a,b LOGICAL(LGT), INTENT(IN) :: mask REAL(SP) :: swp
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE swap_rv(a,b) REAL(SP), DIMENSION(:), INTENT(INOUT) :: a,b REAL(SP), DIMENSION(SIZE(a)) :: dum dum=a a=b b=dum END SUBROUTINE swap_rv
1368
Appendix C1.
Listing of Utility Modules (nrtype and nrutil)
if (mask) then swp=a a=b b=swp end if END SUBROUTINE masked_swap_rs
SUBROUTINE masked_swap_rm(a,b,mask) REAL(SP), DIMENSION(:,:), INTENT(INOUT) :: a,b LOGICAL(LGT), DIMENSION(:,:), INTENT(IN) :: mask REAL(SP), DIMENSION(size(a,1),size(a,2)) :: swp where (mask) swp=a a=b b=swp end where END SUBROUTINE masked_swap_rm FUNCTION reallocate_rv(p,n) Reallocate a pointer to a new size, preserving its previous contents. REAL(SP), DIMENSION(:), POINTER :: p, reallocate_rv INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B) :: nold,ierr allocate(reallocate_rv(n),stat=ierr) if (ierr /= 0) call & nrerror(’reallocate_rv: problem in attempt to allocate memory’) if (.not. associated(p)) RETURN nold=size(p) reallocate_rv(1:min(nold,n))=p(1:min(nold,n)) deallocate(p) END FUNCTION reallocate_rv FUNCTION reallocate_iv(p,n) INTEGER(I4B), DIMENSION(:), POINTER :: p, reallocate_iv INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B) :: nold,ierr allocate(reallocate_iv(n),stat=ierr) if (ierr /= 0) call & nrerror(’reallocate_iv: problem in attempt to allocate memory’) if (.not. associated(p)) RETURN nold=size(p) reallocate_iv(1:min(nold,n))=p(1:min(nold,n)) deallocate(p) END FUNCTION reallocate_iv FUNCTION reallocate_hv(p,n) CHARACTER(1), DIMENSION(:), POINTER :: p, reallocate_hv INTEGER(I4B), INTENT(IN) :: n INTEGER(I4B) :: nold,ierr allocate(reallocate_hv(n),stat=ierr) if (ierr /= 0) call & nrerror(’reallocate_hv: problem in attempt to allocate memory’) if (.not. associated(p)) RETURN nold=size(p) reallocate_hv(1:min(nold,n))=p(1:min(nold,n))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
SUBROUTINE masked_swap_rv(a,b,mask) REAL(SP), DIMENSION(:), INTENT(INOUT) :: a,b LOGICAL(LGT), DIMENSION(:), INTENT(IN) :: mask REAL(SP), DIMENSION(size(a)) :: swp where (mask) swp=a a=b b=swp end where END SUBROUTINE masked_swap_rv
C1.2 Numerical Recipes Utilities (nrutil)
1369
deallocate(p) END FUNCTION reallocate_hv
FUNCTION reallocate_im(p,n,m) INTEGER(I4B), DIMENSION(:,:), POINTER :: p, reallocate_im INTEGER(I4B), INTENT(IN) :: n,m INTEGER(I4B) :: nold,mold,ierr allocate(reallocate_im(n,m),stat=ierr) if (ierr /= 0) call & nrerror(’reallocate_im: problem in attempt to allocate memory’) if (.not. associated(p)) RETURN nold=size(p,1) mold=size(p,2) reallocate_im(1:min(nold,n),1:min(mold,m))=& p(1:min(nold,n),1:min(mold,m)) deallocate(p) END FUNCTION reallocate_im Routines returning a location as an integer value: FUNCTION ifirstloc(mask) Index of first occurrence of .true. in a logical vector. LOGICAL(LGT), DIMENSION(:), INTENT(IN) :: mask INTEGER(I4B) :: ifirstloc INTEGER(I4B), DIMENSION(1) :: loc loc=maxloc(merge(1,0,mask)) ifirstloc=loc(1) if (.not. mask(ifirstloc)) ifirstloc=size(mask)+1 END FUNCTION ifirstloc FUNCTION imaxloc_r(arr) Index of maxloc on an array. REAL(SP), DIMENSION(:), INTENT(IN) :: arr INTEGER(I4B) :: imaxloc_r INTEGER(I4B), DIMENSION(1) :: imax imax=maxloc(arr(:)) imaxloc_r=imax(1) END FUNCTION imaxloc_r FUNCTION imaxloc_i(iarr) INTEGER(I4B), DIMENSION(:), INTENT(IN) :: iarr INTEGER(I4B), DIMENSION(1) :: imax INTEGER(I4B) :: imaxloc_i imax=maxloc(iarr(:)) imaxloc_i=imax(1) END FUNCTION imaxloc_i FUNCTION iminloc(arr) Index of minloc on an array. REAL(SP), DIMENSION(:), INTENT(IN) :: arr INTEGER(I4B), DIMENSION(1) :: imin INTEGER(I4B) :: iminloc imin=minloc(arr(:))
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
FUNCTION reallocate_rm(p,n,m) REAL(SP), DIMENSION(:,:), POINTER :: p, reallocate_rm INTEGER(I4B), INTENT(IN) :: n,m INTEGER(I4B) :: nold,mold,ierr allocate(reallocate_rm(n,m),stat=ierr) if (ierr /= 0) call & nrerror(’reallocate_rm: problem in attempt to allocate memory’) if (.not. associated(p)) RETURN nold=size(p,1) mold=size(p,2) reallocate_rm(1:min(nold,n),1:min(mold,m))=& p(1:min(nold,n),1:min(mold,m)) deallocate(p) END FUNCTION reallocate_rm
1370
Appendix C1.
Listing of Utility Modules (nrtype and nrutil)
iminloc=imin(1) END FUNCTION iminloc
SUBROUTINE assert2(n1,n2,string) CHARACTER(LEN=*), INTENT(IN) :: string LOGICAL, INTENT(IN) :: n1,n2 if (.not. (n1 .and. n2)) then write (*,*) ’nrerror: an assertion failed with this tag:’, & string STOP ’program terminated by assert2’ end if END SUBROUTINE assert2 SUBROUTINE assert3(n1,n2,n3,string) CHARACTER(LEN=*), INTENT(IN) :: string LOGICAL, INTENT(IN) :: n1,n2,n3 if (.not. (n1 .and. n2 .and. n3)) then write (*,*) ’nrerror: an assertion failed with this tag:’, & string STOP ’program terminated by assert3’ end if END SUBROUTINE assert3 SUBROUTINE assert4(n1,n2,n3,n4,string) CHARACTER(LEN=*), INTENT(IN) :: string LOGICAL, INTENT(IN) :: n1,n2,n3,n4 if (.not. (n1 .and. n2 .and. n3 .and. n4)) then write (*,*) ’nrerror: an assertion failed with this tag:’, & string STOP ’program terminated by assert4’ end if END SUBROUTINE assert4 SUBROUTINE assert_v(n,string) CHARACTER(LEN=*), INTENT(IN) :: string LOGICAL, DIMENSION(:), INTENT(IN) :: n if (.not. all(n)) then write (*,*) ’nrerror: an assertion failed with this tag:’, & string STOP ’program terminated by assert_v’ end if END SUBROUTINE assert_v FUNCTION assert_eq2(n1,n2,string) Report and die if integers not all equal (used for size checking). CHARACTER(LEN=*), INTENT(IN) :: string INTEGER, INTENT(IN) :: n1,n2 INTEGER :: assert_eq2 if (n1 == n2) then assert_eq2=n1 else write (*,*) ’nrerror: an assert_eq failed with this tag:’, & string STOP ’program terminated by assert_eq2’ end if
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
Routines for argument checking and error handling: SUBROUTINE assert1(n1,string) Report and die if any logical is false (used for arg range checking). CHARACTER(LEN=*), INTENT(IN) :: string LOGICAL, INTENT(IN) :: n1 if (.not. n1) then write (*,*) ’nrerror: an assertion failed with this tag:’, & string STOP ’program terminated by assert1’ end if END SUBROUTINE assert1
C1.2 Numerical Recipes Utilities (nrutil)
1371
END FUNCTION assert_eq2
FUNCTION assert_eq4(n1,n2,n3,n4,string) CHARACTER(LEN=*), INTENT(IN) :: string INTEGER, INTENT(IN) :: n1,n2,n3,n4 INTEGER :: assert_eq4 if (n1 == n2 .and. n2 == n3 .and. n3 == n4) then assert_eq4=n1 else write (*,*) ’nrerror: an assert_eq failed with this tag:’, & string STOP ’program terminated by assert_eq4’ end if END FUNCTION assert_eq4 FUNCTION assert_eqn(nn,string) CHARACTER(LEN=*), INTENT(IN) :: string INTEGER, DIMENSION(:), INTENT(IN) :: nn INTEGER :: assert_eqn if (all(nn(2:) == nn(1))) then assert_eqn=nn(1) else write (*,*) ’nrerror: an assert_eq failed with this tag:’, & string STOP ’program terminated by assert_eqn’ end if END FUNCTION assert_eqn SUBROUTINE nrerror(string) Report a message, then die. CHARACTER(LEN=*), INTENT(IN) :: string write (*,*) ’nrerror: ’,string STOP ’program terminated by nrerror’ END SUBROUTINE nrerror Routines relating to polynomials and recurrences: FUNCTION arth_r(first,increment,n) Array function returning an arithmetic progression. REAL(SP), INTENT(IN) :: first,increment INTEGER(I4B), INTENT(IN) :: n REAL(SP), DIMENSION(n) :: arth_r INTEGER(I4B) :: k,k2 REAL(SP) :: temp if (n > 0) arth_r(1)=first if (n 0) arth_d(1)=first if (n = n) exit k2=k+k arth_d(k+1:min(k2,n))=temp+arth_d(1:min(k,n-k)) temp=temp+temp k=k2 end do end if END FUNCTION arth_d FUNCTION arth_i(first,increment,n) INTEGER(I4B), INTENT(IN) :: first,increment,n INTEGER(I4B), DIMENSION(n) :: arth_i INTEGER(I4B) :: k,k2,temp if (n > 0) arth_i(1)=first if (n = n) exit k2=k+k arth_i(k+1:min(k2,n))=temp+arth_i(1:min(k,n-k)) temp=temp+temp k=k2 end do end if END FUNCTION arth_i FUNCTION geop_r(first,factor,n) Array function returning a geometric progression. REAL(SP), INTENT(IN) :: first,factor
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
if (k >= n) exit k2=k+k arth_r(k+1:min(k2,n))=temp+arth_r(1:min(k,n-k)) temp=temp+temp k=k2 end do end if END FUNCTION arth_r
C1.2 Numerical Recipes Utilities (nrutil)
FUNCTION geop_d(first,factor,n) REAL(DP), INTENT(IN) :: first,factor INTEGER(I4B), INTENT(IN) :: n REAL(DP), DIMENSION(n) :: geop_d INTEGER(I4B) :: k,k2 REAL(DP) :: temp if (n > 0) geop_d(1)=first if (n = n) exit k2=k+k geop_d(k+1:min(k2,n))=temp*geop_d(1:min(k,n-k)) temp=temp*temp k=k2 end do end if END FUNCTION geop_d FUNCTION geop_i(first,factor,n) INTEGER(I4B), INTENT(IN) :: first,factor,n INTEGER(I4B), DIMENSION(n) :: geop_i INTEGER(I4B) :: k,k2,temp if (n > 0) geop_i(1)=first if (n 0) geop_r(1)=first if (n = n) exit k2=k+k geop_r(k+1:min(k2,n))=temp*geop_r(1:min(k,n-k)) temp=temp*temp k=k2 end do end if END FUNCTION geop_r
1373
1374
Appendix C1.
Listing of Utility Modules (nrtype and nrutil)
FUNCTION geop_c(first,factor,n) COMPLEX(SP), INTENT(IN) :: first,factor INTEGER(I4B), INTENT(IN) :: n COMPLEX(SP), DIMENSION(n) :: geop_c INTEGER(I4B) :: k,k2 COMPLEX(SP) :: temp if (n > 0) geop_c(1)=first if (n = n) exit k2=k+k geop_c(k+1:min(k2,n))=temp*geop_c(1:min(k,n-k)) temp=temp*temp k=k2 end do end if END FUNCTION geop_c FUNCTION geop_dv(first,factor,n) REAL(DP), DIMENSION(:), INTENT(IN) :: first,factor INTEGER(I4B), INTENT(IN) :: n REAL(DP), DIMENSION(size(first),n) :: geop_dv INTEGER(I4B) :: k,k2 REAL(DP), DIMENSION(size(first)) :: temp if (n > 0) geop_dv(:,1)=first(:) if (n = n) exit k2=k+k geop_dv(:,k+1:min(k2,n))=geop_dv(:,1:min(k,n-k))*& spread(temp,2,size(geop_dv(:,1:min(k,n-k)),2)) temp=temp*temp k=k2 end do end if
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
temp=factor**NPAR2_GEOP k=NPAR2_GEOP do if (k >= n) exit k2=k+k geop_i(k+1:min(k2,n))=temp*geop_i(1:min(k,n-k)) temp=temp*temp k=k2 end do end if END FUNCTION geop_i
C1.2 Numerical Recipes Utilities (nrutil)
1375
END FUNCTION geop_dv
RECURSIVE FUNCTION cumsum_i(arr,seed) RESULT(ans) INTEGER(I4B), DIMENSION(:), INTENT(IN) :: arr INTEGER(I4B), OPTIONAL, INTENT(IN) :: seed INTEGER(I4B), DIMENSION(size(arr)) :: ans INTEGER(I4B) :: n,j,sd n=size(arr) if (n == 0_i4b) RETURN sd=0_i4b if (present(seed)) sd=seed ans(1)=arr(1)+sd if (n < NPAR_CUMSUM) then do j=2,n ans(j)=ans(j-1)+arr(j) end do else ans(2:n:2)=cumsum_i(arr(2:n:2)+arr(1:n-1:2),sd) ans(3:n:2)=ans(2:n-1:2)+arr(3:n:2) end if END FUNCTION cumsum_i RECURSIVE FUNCTION cumprod(arr,seed) RESULT(ans) Cumulative product on an array, with optional multiplicative seed. REAL(SP), DIMENSION(:), INTENT(IN) :: arr REAL(SP), OPTIONAL, INTENT(IN) :: seed REAL(SP), DIMENSION(size(arr)) :: ans INTEGER(I4B) :: n,j REAL(SP) :: sd n=size(arr) if (n == 0_i4b) RETURN sd=1.0_sp if (present(seed)) sd=seed ans(1)=arr(1)*sd if (n < NPAR_CUMPROD) then do j=2,n ans(j)=ans(j-1)*arr(j) end do else ans(2:n:2)=cumprod(arr(2:n:2)*arr(1:n-1:2),sd) ans(3:n:2)=ans(2:n-1:2)*arr(3:n:2) end if END FUNCTION cumprod
Sample page from NUMERICAL RECIPES IN FORTRAN 90: The Art of PARALLEL Scientific Computing (ISBN 0-521-57439-0) Copyright (C) 1986-1996 by Cambridge University Press. Programs Copyright (C) 1986-1996 by Numerical Recipes Software. Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books, diskettes, or CDROMs visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to [email protected] (outside North America).
RECURSIVE FUNCTION cumsum_r(arr,seed) RESULT(ans) Cumulative sum on an array, with optional additive seed. REAL(SP), DIMENSION(:), INTENT(IN) :: arr REAL(SP), OPTIONAL, INTENT(IN) :: seed REAL(SP), DIMENSION(size(arr)) :: ans INTEGER(I4B) :: n,j REAL(SP) :: sd n=size(arr) if (n == 0_i4b) RETURN sd=0.0_sp if (present(seed)) sd=seed ans(1)=arr(1)+sd if (n < NPAR_CUMSUM) then do j=2,n ans(j)=ans(j-1)+arr(j) end do else ans(2:n:2)=cumsum_r(arr(2:n:2)+arr(1:n-1:2),sd) ans(3:n:2)=ans(2:n-1:2)+arr(3:n:2) end if END FUNCTION cumsum_r
1376
Appendix C1.
Listing of Utility Modules (nrtype and nrutil)
FUNCTION poly_dd(x,coeffs) REAL(DP), INTENT(IN) :: x REAL(DP), DIMENSION(:), INTENT(IN) :: coeffs REAL(DP) :: poly_dd REAL(DP) :: pow REAL(DP), DIMENSION(:), ALLOCATABLE :: vec INTEGER(I4B) :: i,n,nn n=size(coeffs) if (n