c2-7 (779465), страница 6
Текст из файла (страница 6)
Used by linbcg.{unsigned long i,isamax;double ans;if (itol <= 3) {ans = 0.0;for (i=1;i<=n;i++) ans += sx[i]*sx[i];Vector magnitude norm.return sqrt(ans);} else {isamax=1;for (i=1;i<=n;i++) {Largest component norm.if (fabs(sx[i]) > fabs(sx[isamax])) isamax=i;}return fabs(sx[isamax]);}}So that the specifications for the routines atimes and asolve are clear, we list heresimple versions that assume a matrix A stored somewhere in row-index sparse format.extern unsigned long ija[];extern double sa[];The matrix is stored somewhere.void atimes(unsigned long n, double x[], double r[], int itrnsp){void dsprsax(double sa[], unsigned long ija[], double x[], double b[],unsigned long n);Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use.
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).else if (itol == 3 || itol == 4) {zm1nrm=znrm;znrm=snrm(n,z,itol);if (fabs(zm1nrm-znrm) > EPS*znrm) {dxnrm=fabs(ak)*snrm(n,p,itol);*err=znrm/fabs(zm1nrm-znrm)*dxnrm;} else {*err=znrm/bnrm;Error may not be accurate, so loop again.continue;}xnrm=snrm(n,x,itol);if (*err <= 0.5*xnrm) *err /= xnrm;else {*err=znrm/bnrm;Error may not be accurate, so loop again.continue;}}printf("iter=%4d err=%12.6f\n",*iter,*err);if (*err <= tol) break;}2.7 Sparse Linear Systems89void dsprstx(double sa[], unsigned long ija[], double x[], double b[],unsigned long n);These are double versions of sprsax and sprstx.if (itrnsp) dsprstx(sa,ija,x,r,n);else dsprsax(sa,ija,x,r,n);}The matrix is stored somewhere.void asolve(unsigned long n, double b[], double x[], int itrnsp){unsigned long i;for(i=1;i<=n;i++) x[i]=(sa[i] != 0.0 ? b[i]/sa[i] : b[i]);e is the diagonal part of A, stored in the first n elements of sa.
Since theThe matrix Atranspose matrix has the same diagonal, the flag itrnsp is not used.}CITED REFERENCES AND FURTHER READING:Tewarson, R.P. 1973, Sparse Matrices (New York: Academic Press). [1]Jacobs, D.A.H. (ed.) 1977, The State of the Art in Numerical Analysis (London: AcademicPress), Chapter I.3 (by J.K.
Reid). [2]George, A., and Liu, J.W.H. 1981, Computer Solution of Large Sparse Positive Definite Systems(Englewood Cliffs, NJ: Prentice-Hall). [3]NAG Fortran Library (Numerical Algorithms Group, 256 Banbury Road, Oxford OX27DE, U.K.).[4]IMSL Math/Library Users Manual (IMSL Inc., 2500 CityWest Boulevard, Houston TX 77042). [5]Eisenstat, S.C., Gursky, M.C., Schultz, M.H., and Sherman, A.H.
1977, Yale Sparse Matrix Package, Technical Reports 112 and 114 (Yale University Department of Computer Science). [6]Knuth, D.E. 1968, Fundamental Algorithms, vol. 1 of The Art of Computer Programming (Reading,MA: Addison-Wesley), §2.2.6.
[7]Kincaid, D.R., Respess, J.R., Young, D.M., and Grimes, R.G. 1982, ACM Transactions on Mathematical Software, vol. 8, pp. 302–322. [8]PCGPAK User’s Guide (New Haven: Scientific Computing Associates, Inc.). [9]Bentley, J. 1986, Programming Pearls (Reading, MA: Addison-Wesley), §9. [10]Golub, G.H., and Van Loan, C.F. 1989, Matrix Computations, 2nd ed. (Baltimore: Johns HopkinsUniversity Press), Chapters 4 and 10, particularly §§10.2–10.3. [11]Stoer, J., and Bulirsch, R. 1980, Introduction to Numerical Analysis (New York: Springer-Verlag),Chapter 8.
[12]Baker, L. 1991, More C Tools for Scientists and Engineers (New York: McGraw-Hill). [13]Fletcher, R. 1976, in Numerical Analysis Dundee 1975, Lecture Notes in Mathematics, vol. 506,A. Dold and B Eckmann, eds. (Berlin: Springer-Verlag), pp. 73–89. [14]Saad, Y., and Schulz, M. 1986, SIAM Journal on Scientific and Statistical Computing, vol. 7,pp.
856–869. [15]Bunch, J.R., and Rose, D.J. (eds.) 1976, Sparse Matrix Computations (New York: AcademicPress).Duff, I.S., and Stewart, G.W. (eds.) 1979, Sparse Matrix Proceedings 1978 (Philadelphia:S.I.A.M.).Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).extern unsigned long ija[];extern double sa[];90Chapter 2.Solution of Linear Algebraic Equations2.8 Vandermonde Matrices and ToeplitzMatricesVandermonde MatricesA Vandermonde matrix of size N × N is completely determined by N arbitrarynumbers x1 , x2 , .
. . , xN , in terms of which its N 2 components are the integer powersxj−1, i, j = 1, . . . , N . Evidently there are two possible such forms, depending on whetheriwe view the i’s as rows, j’s as columns, or vice versa. In the former case, we get a linearsystem of equations that looks like this,11. ..1x1x21···x2...xNx22...x2N······−1xN1 −1 xN2·.. . −1xNNc1c2...cN = y1y2 .. .
yN(2.8.1)Performing the matrix multiplication, you will see that this equation solves for the unknowncoefficients ci which fit a polynomial to the N pairs of abscissas and ordinates (xj , yj ).Precisely this problem will arise in §3.5, and the routine given there will solve (2.8.1) by themethod that we are about to describe.Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited.
To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).In §2.4 the case of a tridiagonal matrix was treated specially, because thatparticular type of linear system admits a solution in only of order N operations,rather than of order N 3 for the general linear problem. When such particular typesexist, it is important to know about them.
Your computational savings, should youever happen to be working on a problem that involves the right kind of particulartype, can be enormous.This section treats two special types of matrices that can be solved in of orderN 2 operations, not as good as tridiagonal, but a lot better than the general case.(Other than the operations count, these two types having nothing in common.)Matrices of the first type, termed Vandermonde matrices, occur in some problemshaving to do with the fitting of polynomials, the reconstruction of distributions fromtheir moments, and also other contexts.
In this book, for example, a Vandermondeproblem crops up in §3.5. Matrices of the second type, termed Toeplitz matrices,tend to occur in problems involving deconvolution and signal processing. In thisbook, a Toeplitz problem is encountered in §13.7.These are not the only special types of matrices worth knowing about.
TheHilbert matrices, whose components are of the form aij = 1/(i + j − 1), i, j =1, . . . , N can be inverted by an exact integer algorithm, and are very difficult toinvert in any other way, since they are notoriously ill-conditioned (see [1] for details).The Sherman-Morrison and Woodbury formulas, discussed in §2.7, can sometimesbe used to convert new special forms into old ones. Reference [2] gives some otherspecial forms. We have not found these additional forms to arise as frequently asthe two that we now discuss..