正在加载图片...
176 Chapter 5. Evaluation of Functions Rational Functions You evaluate a rational function like P(z)p0+p1x+…+PxH R()=Q,=90+9nz++9c (5.3.4) in the obvious way,namely as two separate polynomials followed by a divide.As a matter of convention one usually chooses go =1,obtained by dividing numerator and denominator by any other go.It is often convenient to have both sets of 81 coefficients stored in a single array,and to have a standard function available for doing the evaluation: double ratval(double x,double cof[,int mm,int kk) Given mm,kk,and cof [0..mm+kk],evaluate and return the rational function (cof [O]+ Cam ICAL cof[]+..+cof [mm]xmm)/(1+cof [mm+1]x+...+cof [mm+kk]xkk). int j; RECIPES double sumd,sumn; Note precision!Change to float if desired for (sumn=cof [mm],j=mm-1;j>=0;j--)sumn=sumn*x+cof [j]; for (sumd=0.0,j=mm+kk;j>=mm+1;j--)sumd=(sumd+cof [j])*x; computer, return sumn/(1.0+sumd); Americ Press. 9 Programs CITED REFERENCES AND FURTHER READING: SCIENTIFIC Acton,F.S.1970,Numerica/Methods That Work;1990,corrected edition (Washington:Mathe- matical Association of America),pp.183,190.[1] Mathews,J.,and Walker,R.L.1970,Mathematical Methods of Physics,2nd ed.(Reading,MA: W.A.Benjamin/Addison-Wesley),pp.361-363.[2] Knuth.D.E.1981.Seminumerical Algorithms,2nd ed.,vol.2 of The Art of Computer Programming (Reading,MA:Addison-Wesley),84.6.[3] Fike,C.T.1968,Computer Evaluation of Mathematical Functions(Englewood Cliffs,NJ:Prentice- Hall),Chapter 4. 10-621 Winograd,S.1970,Communications on Pure and Applied Mathematics,vol.23,pp.165-179.[4] Kronsjo,L.1987,Algorithms:Their Complexity and Efficiency,2nd ed.(New York:Wiley).[5] uction, Numerical Recipes 43106 (outside North Software. 5.4 Complex Arithmetic As we mentioned in $1.2,the lack of built-in complex arithmetic in c is a nuisance for numerical work.Even in languages like FORTRAN that have complex data types,it is disconcertingly common to encounter complex operations that produce overflows or underflows when both the complex operands and the complex result are perfectly representable.This occurs,we think,because software companies assign inexperienced programmers to what they believe to be the perfectly trivial task of implementing complex arithmetic.176 Chapter 5. Evaluation of Functions Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). Rational Functions You evaluate a rational function like R(x) = Pµ(x) Qν(x) = p0 + p1x + ··· + pµxµ q0 + q1x + ··· + qνxν (5.3.4) in the obvious way, namely as two separate polynomials followed by a divide. As a matter of convention one usually chooses q 0 = 1, obtained by dividing numerator and denominator by any other q0. It is often convenient to have both sets of coefficients stored in a single array, and to have a standard function available for doing the evaluation: double ratval(double x, double cof[], int mm, int kk) Given mm, kk, and cof[0..mm+kk], evaluate and return the rational function (cof[0] + cof[1]x + ··· + cof[mm]xmm)/(1 + cof[mm+1]x + ··· + cof[mm+kk]xkk). { int j; double sumd,sumn; Note precision! Change to float if desired. for (sumn=cof[mm],j=mm-1;j>=0;j--) sumn=sumn*x+cof[j]; for (sumd=0.0,j=mm+kk;j>=mm+1;j--) sumd=(sumd+cof[j])*x; return sumn/(1.0+sumd); } CITED REFERENCES AND FURTHER READING: Acton, F.S. 1970, Numerical Methods That Work; 1990, corrected edition (Washington: Mathe￾matical Association of America), pp. 183, 190. [1] Mathews, J., and Walker, R.L. 1970, Mathematical Methods of Physics, 2nd ed. (Reading, MA: W.A. Benjamin/Addison-Wesley), pp. 361–363. [2] Knuth, D.E. 1981, Seminumerical Algorithms, 2nd ed., vol. 2 of The Art of Computer Programming (Reading, MA: Addison-Wesley), §4.6. [3] Fike, C.T. 1968, Computer Evaluation of Mathematical Functions (Englewood Cliffs, NJ: Prentice￾Hall), Chapter 4. Winograd, S. 1970, Communications on Pure and Applied Mathematics, vol. 23, pp. 165–179. [4] Kronsj¨o, L. 1987, Algorithms: Their Complexity and Efficiency, 2nd ed. (New York: Wiley). [5] 5.4 Complex Arithmetic As we mentioned in §1.2, the lack of built-in complex arithmetic in C is a nuisance for numerical work. Even in languages like FORTRAN that have complex data types, it is disconcertingly common to encounter complex operations that produce overflows or underflows when both the complex operands and the complex result are perfectly representable. This occurs, we think, because software companies assign inexperienced programmers to what they believe to be the perfectly trivial task of implementing complex arithmetic
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有