Numerical Recipes in Fortran 77 The Art of Scientific Computing Second Edition Volume 1 of Fortran Numerical Recipes WilliamH.Press Harvard-SmithsonianCenterforAstrophysics SaulA.Teukolsky DepartmentofPhysics,CornellUniversity WilliamT.Vetterling PolaroidCorporation BrianP.Flannery EXXONResearchandEngineeringCompany PublishedbythePressSyndicateoftheUniversityofCambridge ThePitt Building,TrumpingtonStreet, CambridgeCB21RP 40West 20thStreet, NewYork, NY10011-4211,USA 10StamfordRoad,Oakleigh,Melbourne3166,Australia Copyright(cid:13)c CambridgeUniversityPress 1986,1992 exceptforx13.10,whichisplacedintothepublicdomain, andexceptforallothercomputerprogramsandprocedures,whichare Copyright(cid:13)c NumericalRecipesSoftware1986,1992,1997 All Rights Reserved. Somesectionsofthisbookwereoriginallypublished,indifferentform,inComputers inPhysicsmagazine,Copyright(cid:13)c AmericanInstituteofPhysics,1988–1992. FirstEditionoriginallypublished1986;SecondEditionoriginallypublished1992as NumericalRecipesinFORTRAN TheArtofScientificComputing Reprintedwith corrections, 1993, 1994, 1995. Reprintedwithcorrections,1996,1997,asNumericalRecipesinFortran77 TheArtof ScientificComputing(Vol.1ofFortranNumericalRecipes) This reprintingis correctedtosoftwareversion2.08 Printed in the United States of America Typeset in TEX Withoutanadditionallicensetousethecontainedsoftware,thisbookisintendedas atextandreferencebook,forreadingpurposesonly. Afreelicenseforlimiteduseofthe softwarebytheindividualownerofacopyofthisbookwhopersonallytypesoneormore routinesintoasinglecomputerisgrantedundertermsdescribedonp.xxi. Seethesection “LicenseInformation”(pp.xx–xxiii)forinformationonobtainingmoregenerallicensesat lowcost. Machine-readablemediacontainingthesoftwareinthisbook,withincludedlicenses 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 MidlandAvenue,PortChester,NY10573(USA),forfurtherinformation. The software may also be downloaded, with immediate purchase of a license also possible, fromthe NumericalRecipesSoftware Web Site (http://www.nr.com). UnlicensedtransferofNumericalRecipesprogramstoanyotherformat,ortoanycomputer 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. LibraryofCongressCataloginginPublicationData NumericalrecipesinFortran77: theartofscientificcomputing/WilliamH.Press ::: [et al.]. – 2nd ed. Includesbibliographicalreferences(p. ) andindex. ISBN 0-521-43064-X 1. Numericalanalysis–Computerprograms.2. Science–Mathematics–Computerprograms. 3. FORTRAN(Computerprogramlanguage) I.Press, WilliamH. QA297.N866 1992 519.400285053–dc20 92-8876 AcatalogrecordforthisbookisavailablefromtheBritishLibrary. ISBN 052143064X Volume1(thisbook) ISBN 0521574390 Volume2 ISBN 0521437210 ExamplebookinFORTRAN ISBN 0521574404 FORTRANdiskette(IBM3.500) ISBN 0521576083 CDROM(IBMPC/Macintosh) ISBN 0521576075 CDROM(UNIX) Contents PlanoftheTwo-VolumeEdition xiii PrefacetotheSecondEdition xv PrefacetotheFirstEdition xviii LicenseInformation xx ComputerProgramsbyChapterandSection xxiv 1 Preliminaries 1 1.0Introduction 1 1.1ProgramOrganizationandControlStructures 5 1.2Error,Accuracy,andStability 18 2 SolutionofLinearAlgebraicEquations 22 2.0Introduction 22 2.1Gauss-JordanElimination 27 2.2GaussianEliminationwithBacksubstitution 33 2.3LUDecompositionandItsApplications 34 2.4TridiagonalandBandDiagonalSystemsofEquations 42 2.5IterativeImprovementofaSolutiontoLinearEquations 47 2.6SingularValueDecomposition 51 2.7SparseLinearSystems 63 2.8VandermondeMatricesandToeplitzMatrices 82 2.9CholeskyDecomposition 89 2.10QRDecomposition 91 2.11IsMatrixInversionanN3Process? 95 3 InterpolationandExtrapolation 99 3.0Introduction 99 3.1PolynomialInterpolationandExtrapolation 102 3.2RationalFunctionInterpolationandExtrapolation 104 3.3CubicSplineInterpolation 107 3.4HowtoSearchanOrderedTable 110 3.5CoefficientsoftheInterpolatingPolynomial 113 3.6InterpolationinTwoorMoreDimensions 116 v vi Contents 4 IntegrationofFunctions 123 4.0Introduction 123 4.1ClassicalFormulasforEquallySpacedAbscissas 124 4.2ElementaryAlgorithms 130 4.3RombergIntegration 134 4.4ImproperIntegrals 135 4.5GaussianQuadraturesandOrthogonalPolynomials 140 4.6MultidimensionalIntegrals 155 5 EvaluationofFunctions 159 5.0Introduction 159 5.1SeriesandTheirConvergence 159 5.2EvaluationofContinuedFractions 163 5.3PolynomialsandRationalFunctions 167 5.4ComplexArithmetic 171 5.5RecurrenceRelationsandClenshaw’sRecurrenceFormula 172 5.6QuadraticandCubicEquations 178 5.7NumericalDerivatives 180 5.8ChebyshevApproximation 184 5.9DerivativesorIntegralsofaChebyshev-approximatedFunction 189 5.10PolynomialApproximationfromChebyshevCoefficients 191 5.11EconomizationofPowerSeries 192 5.12Pade´ Approximants 194 5.13RationalChebyshevApproximation 197 5.14EvaluationofFunctionsbyPathIntegration 201 6 SpecialFunctions 205 6.0Introduction 205 6.1GammaFunction,BetaFunction,Factorials,BinomialCoefficients 206 6.2IncompleteGammaFunction,ErrorFunction,Chi-Square ProbabilityFunction,CumulativePoissonFunction 209 6.3ExponentialIntegrals 215 6.4IncompleteBetaFunction,Student’sDistribution,F-Distribution, CumulativeBinomialDistribution 219 6.5BesselFunctionsofIntegerOrder 223 6.6ModifiedBesselFunctionsofIntegerOrder 229 6.7BesselFunctionsofFractionalOrder,AiryFunctions,Spherical BesselFunctions 234 6.8SphericalHarmonics 246 6.9FresnelIntegrals,CosineandSineIntegrals 248 6.10Dawson’sIntegral 252 6.11EllipticIntegralsandJacobianEllipticFunctions 254 6.12HypergeometricFunctions 263 7 RandomNumbers 266 7.0Introduction 266 7.1UniformDeviates 267 Contents vii 7.2TransformationMethod: ExponentialandNormalDeviates 277 7.3RejectionMethod: Gamma, Poisson,BinomialDeviates 281 7.4GenerationofRandomBits 287 7.5RandomSequencesBasedonDataEncryption 290 7.6SimpleMonteCarloIntegration 295 7.7Quasi-(thatis,Sub-)RandomSequences 299 7.8AdaptiveandRecursiveMonteCarloMethods 306 8 Sorting 320 8.0Introduction 320 8.1StraightInsertionandShell’sMethod 321 8.2Quicksort 323 8.3Heapsort 327 8.4IndexingandRanking 329 8.5SelectingtheMthLargest 333 8.6DeterminationofEquivalenceClasses 337 9 RootFindingandNonlinearSetsofEquations 340 9.0Introduction 340 9.1BracketingandBisection 343 9.2SecantMethod,FalsePositionMethod,andRidders’Method 347 9.3VanWijngaarden–Dekker–BrentMethod 352 9.4Newton-RaphsonMethodUsingDerivative 355 9.5RootsofPolynomials 362 9.6Newton-RaphsonMethodforNonlinearSystemsofEquations 372 9.7GloballyConvergentMethodsforNonlinearSystemsofEquations 376 10 MinimizationorMaximizationofFunctions 387 10.0Introduction 387 10.1GoldenSectionSearchinOneDimension 390 10.2ParabolicInterpolationandBrent’sMethodinOneDimension 395 10.3One-DimensionalSearchwithFirstDerivatives 399 10.4DownhillSimplexMethodinMultidimensions 402 10.5DirectionSet(Powell’s)MethodsinMultidimensions 406 10.6ConjugateGradientMethodsinMultidimensions 413 10.7VariableMetricMethodsinMultidimensions 418 10.8LinearProgrammingandtheSimplexMethod 423 10.9SimulatedAnnealingMethods 436 11 Eigensystems 449 11.0Introduction 449 11.1JacobiTransformationsofaSymmetricMatrix 456 11.2ReductionofaSymmetricMatrixtoTridiagonalForm: GivensandHouseholderReductions 462 11.3EigenvaluesandEigenvectorsofaTridiagonalMatrix 469 11.4HermitianMatrices 475 11.5ReductionofaGeneralMatrixtoHessenbergForm 476 viii Contents 11.6TheQRAlgorithmforRealHessenbergMatrices 480 11.7ImprovingEigenvaluesand/orFindingEigenvectorsby InverseIteration 487 12 FastFourierTransform 490 12.0Introduction 490 12.1FourierTransformofDiscretelySampledData 494 12.2FastFourierTransform(FFT) 498 12.3FFTofRealFunctions,SineandCosineTransforms 504 12.4FFTinTwoorMoreDimensions 515 12.5FourierTransformsofRealDatainTwoandThreeDimensions 519 12.6ExternalStorageorMemory-LocalFFTs 525 13 FourierandSpectralApplications 530 13.0Introduction 530 13.1ConvolutionandDeconvolutionUsingtheFFT 531 13.2CorrelationandAutocorrelationUsingtheFFT 538 13.3Optimal(Wiener)FilteringwiththeFFT 539 13.4PowerSpectrumEstimationUsingtheFFT 542 13.5DigitalFilteringintheTimeDomain 551 13.6LinearPredictionandLinearPredictiveCoding 557 13.7PowerSpectrumEstimationbytheMaximumEntropy (AllPoles)Method 565 13.8SpectralAnalysisofUnevenlySampledData 569 13.9ComputingFourierIntegralsUsingtheFFT 577 13.10WaveletTransforms 584 13.11NumericalUseoftheSamplingTheorem 600 14 StatisticalDescriptionofData 603 14.0Introduction 603 14.1MomentsofaDistribution:Mean,Variance,Skewness, andSoForth 604 14.2DoTwoDistributionsHavetheSameMeansorVariances? 609 14.3AreTwoDistributionsDifferent? 614 14.4ContingencyTableAnalysisofTwoDistributions 622 14.5LinearCorrelation 630 14.6NonparametricorRankCorrelation 633 14.7DoTwo-DimensionalDistributionsDiffer? 640 14.8Savitzky-GolaySmoothingFilters 644 15 ModelingofData 650 15.0Introduction 650 15.1LeastSquaresasaMaximumLikelihoodEstimator 651 15.2FittingDatatoaStraightLine 655 15.3Straight-LineDatawithErrorsinBothCoordinates 660 15.4GeneralLinearLeastSquares 665 15.5NonlinearModels 675 Contents ix 15.6ConfidenceLimitsonEstimatedModelParameters 684 15.7RobustEstimation 694 16 IntegrationofOrdinaryDifferentialEquations 701 16.0Introduction 701 16.1Runge-KuttaMethod 704 16.2AdaptiveStepsizeControlforRunge-Kutta 708 16.3ModifiedMidpointMethod 716 16.4RichardsonExtrapolationandtheBulirsch-StoerMethod 718 16.5Second-OrderConservativeEquations 726 16.6StiffSetsofEquations 727 16.7Multistep,Multivalue,andPredictor-CorrectorMethods 740 17 TwoPointBoundaryValueProblems 745 17.0Introduction 745 17.1TheShootingMethod 749 17.2ShootingtoaFittingPoint 751 17.3RelaxationMethods 753 17.4AWorkedExample: SpheroidalHarmonics 764 17.5AutomatedAllocationofMeshPoints 774 17.6HandlingInternalBoundaryConditionsorSingularPoints 775 18 IntegralEquationsandInverseTheory 779 18.0Introduction 779 18.1FredholmEquationsoftheSecondKind 782 18.2VolterraEquations 786 18.3IntegralEquationswithSingularKernels 788 18.4InverseProblemsandtheUseofAPrioriInformation 795 18.5LinearRegularizationMethods 799 18.6Backus-GilbertMethod 806 18.7MaximumEntropyImageRestoration 809 19 PartialDifferentialEquations 818 19.0Introduction 818 19.1Flux-ConservativeInitialValueProblems 825 19.2DiffusiveInitialValueProblems 838 19.3InitialValueProblemsinMultidimensions 844 19.4FourierandCyclicReductionMethodsforBoundary ValueProblems 848 19.5RelaxationMethodsforBoundaryValueProblems 854 19.6MultigridMethodsforBoundaryValueProblems 862 20 Less-NumericalAlgorithms 881 20.0Introduction 881 20.1DiagnosingMachineParameters 881 20.2GrayCodes 886 x Contents 20.3CyclicRedundancyandOtherChecksums 888 20.4HuffmanCodingandCompressionofData 896 20.5ArithmeticCoding 902 20.6ArithmeticatArbitraryPrecision 906 ReferencesforVolume1 916 IndexofProgramsandDependencies(Vol.1) 921 GeneralIndextoVolumes1and2 ContentsofVolume2:NumericalRecipesinFortran90 PrefacetoVolume2 viii ForewordbyMichaelMetcalf x LicenseInformation xvii 21 IntroductiontoFortran90LanguageFeatures 935 22 IntroductiontoParallelProgramming 962 23 NumericalRecipesUtilitiesforFortran90 987 Fortran90CodeChapters 1009 B1 Preliminaries 1010 B2 SolutionofLinearAlgebraicEquations 1014 B3 InterpolationandExtrapolation 1043 B4 IntegrationofFunctions 1052 B5 EvaluationofFunctions 1070 B6 SpecialFunctions 1083 B7 RandomNumbers 1141 B8 Sorting 1167 B9 RootFindingandNonlinearSetsofEquations 1182 B10 MinimizationorMaximizationofFunctions 1201 B11 Eigensystems 1225 B12 FastFourierTransform 1235 Contents xi B13 FourierandSpectralApplications 1253 B14 StatisticalDescriptionofData 1269 B15 ModelingofData 1285 B16 IntegrationofOrdinaryDifferentialEquations 1297 B17 TwoPointBoundaryValueProblems 1314 B18 IntegralEquationsandInverseTheory 1325 B19 PartialDifferentialEquations 1332 B20 Less-NumericalAlgorithms 1343 ReferencesforVolume2 1359 Appendices C1 ListingofUtilityModules(nrtypeandnrutil) 1361 C2 ListingofExplicitInterfaces 1384 C3 IndexofProgramsandDependencies(Vol.2) 1434 GeneralIndextoVolumes1and2 1447 xii