2.7 Sparse Linear Systems 71 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(N3)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 both time (order N instead of N3)and space (order N instead of N2).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 v@cambridge.org ·cyclic banded 客 OF SCIENTIFIC COMPUTING (ISBN 1988-1992 by Numerical Recipes 12-:621-43106-50 singly (or doubly)bordered block diagonal singly (or doubly)bordered block triangular singly (or doubly)bordered band diagonal (outside North Software. singly (or doubly)bordered band triangular ·other() You should also be aware of some of the special sparse forms that occur in the ,visit website machine solution of partial differential equations in two or more dimensions.See Chapter 19. 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
2.7 Sparse Linear Systems 71 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 machinereadable 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). 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 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. 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
72 Chapter 2.Solution of Linear Algebraic Equations zeros zeros zeros (a) (b) (c) Permission is Sample page 囚 囚 (d) (e) (f) http:/.nr.1-800-872-7423 (North America ony)orsend email to directcustserv@cambridge.org(outside North America) readable files (including this one)to any server computer,is strictly prohibited. granted for internet users to make one paper Copyright(C)1988-1992 by Cambridge University Press.Programs Copyright(C) copy for thei (g) (h) (i ▣ 1988-1992 by Numerical Recipes from NUMERICAL RECIPES IN C:THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) ▣ ▣ 9 0 0 0 (G) (k) Software. 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]. visit website ying of machine 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
72 Chapter 2. Solution of Linear Algebraic Equations 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 machinereadable 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). (a) (b) (c) (d) (e) (f ) (g) (h) (i) ( j) (k) zeros zeros zeros 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]. 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
2.7 Sparse Linear Systems 73 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. 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 B 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-without repeating your difficult labors?Yes,if your change is of the form RECIPES A→(A+u⑧v) (2.7.1) for some vectors u and v.If u is a unit vector ei,then(2.7.1)adds the components ofv to the ith row.(Recall that u v is a matrix whose i,jth element is the product 荐o∽的门 9 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 a e3房3 The Sherman-Morrison formula gives the inverse (A+uv)-1,and is derived briefly as follows: (A+u⑧v)-1=(1+A-1.u⑧v)-1.A-1 6 =(1-A-1.u⑧v+A-1.u⑧v·A-1.u⑧v-.)·A-1 =A-1-A-1.u8v·A-1(1-入+λ2-.) =A1-A-1四⑧wA- 10.621 1+入 Numerica (2.7.2) 43106 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 A. The use of(2.7.2)is this:Given A-and the vectors u and v,we need only perform two matrix multiplications and a vector dot product, z≡A-1.uw三(A-1)T.v 入=V·Z (2.7.4) to get the desired change in the inverse A-1→A-1-Z8w (2.7.5) 1+入
2.7 Sparse Linear Systems 73 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 machinereadable 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). 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. 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 your difficult labors? Yes, if your change is of the form A → (A + u ⊗ v) (2.7.1) 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 e j , 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: (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)
74 Chapter 2.Solution of Linear Algebraic Equations The whole procedure requires only 3N2 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 N3 method.The constant in front of the N3 is only a few times worse than the 81 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 is not feasible.If you want to add only a single correction of the form uv, and solve the linear system (A+u图v)·x=b (2.7.6) RECIPESI 2 then you proceed as follows.Using the fast method that is presumed available for the matrix A,solve the two auxiliary problems 豆。。 Press. A·y=bA·z=u (2.7.7) for the vectors y and z.In terms of these, x=y- 1+w…az (2.7.8) SCIENTIFIC 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 10-521 b1c10·.· T1 a2b2c2· T2 Numerical Recipes 43106 4。 (2.7.9) aN-1 bN-1 CN-1 CN- (outside L a 0 aN TN TN This is a tridiagonal system,except for the matrix elements a and B in the corners. North 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 (2.7.10) 0 0 ./
74 Chapter 2. Solution of Linear Algebraic Equations 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 machinereadable 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). 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 N3 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 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) then you proceed as follows. Using the fast method that is presumed available for the matrix A, solve the two auxiliary problems A · y = b A · z = u (2.7.7) for the vectors y and z. In terms of these, x = y − v · y 1+(v · z) z (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 b1 c1 0 ··· β a2 b2 c2 ··· ··· ··· aN−1 bN−1 cN−1 α ··· 0 aN bN · x1 x2 ··· xN−1 xN = r1 r2 ··· rN−1 rN (2.7.9) 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 u = γ 0 . . . 0 α v = 1 0 . . . 0 β/γ (2.7.10)
2.7 Sparse Linear Systems 75 Here yy is arbitrary for the moment.Then the matrix A is the tridiagonal part of the matrix in(2.7.9),with two terms modified: b1=b1-Y, BN =bN-aB/ (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 y=-b to avoid loss of precision by subtraction in the first of equations 5常 (2.7.11).In the unlikely event that this causes loss of precision in the second of 81 these equations,you can make a different choice. #include "nrutil.h" void cyclic(float a[],float b[],float c],float alpha,float beta, float r[],float x[],unsigned long n) 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,all dimensioned as [1..n],while alpha and beta are the corner RECIPES I entries in the matrix.The input is not modified. (North void tridag(float a0,f1oatb☐,f1oatc[0,f1oatr0,f1oatu[▣, unsig即ed long n); unsigned long i; Ameri computer, Press. float fact,gamma,*bb,*u,*Z; ART if (n <2)nrerror("n too small in cyclic"); 9 Programs bb=vector(1,n); u=vector(1,n); z=vector(1,n); gamma =-b[1]; Avoid subtraction error in forming bb[1]. bb[1]=b[1]-gamma; Set up the diagonal of the modified tridi- bb[n]=b[n]-alpha*beta/gamma; agonal system. to dir for (i=2;i<n;i++)bb[i]=b[i]; tridag(a,bb,c,r,x,n); Solve A·x=r. u[1]=gamma; Set up the vector u. ectcustser u[n]=alpha; 18881920 SCIENTIFIC COMPUTING(ISBN for(1=2:1<n:1++)u[1]=0.0; tridag(a,bb,c,u,z,n); Solve A·z=u. fact=(x[1]+beta*x [n]/gamma)/ Form v·x/(1+v·z) 10-621 (1.0+z[1]+beta*z[n]/gamma); for(i=1:1<=n;1++)x[1]-=fact*z[i]: Now get the solution vector x. Further reproduction. Numerical Recipes 43108 free_vector(z,1,n); free_vector(u,1,n); free_vector(bb,1,n); (outside Software. 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 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.Vr)-1 (2.7.12) =A-1-A-1.U(1+vr.A-1.U)-1.v.A-1
2.7 Sparse Linear Systems 75 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 machinereadable 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). 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: b 1 = b1 − γ, b N = 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. #include "nrutil.h" void cyclic(float a[], float b[], float c[], float alpha, float beta, float r[], float x[], unsigned long n) 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, all dimensioned as [1..n], while alpha and beta are the corner entries in the matrix. The input is not modified. { void tridag(float a[], float b[], float c[], float r[], float u[], unsigned long n); unsigned long i; float fact,gamma,*bb,*u,*z; if (n <= 2) nrerror("n too small in cyclic"); bb=vector(1,n); u=vector(1,n); z=vector(1,n); gamma = -b[1]; Avoid subtraction error in forming bb[1]. bb[1]=b[1]-gamma; Set up the diagonal of the modified tridibb[n]=b[n]-alpha*beta/gamma; agonal system. for (i=2;i<n;i++) bb[i]=b[i]; tridag(a,bb,c,r,x,n); Solve A · x = r. u[1]=gamma; Set up the vector u. u[n]=alpha; for (i=2;i<n;i++) u[i]=0.0; tridag(a,bb,c,u,z,n); Solve A · z = u. fact=(x[1]+beta*x[n]/gamma)/ Form v · x/(1 + v · z). (1.0+z[1]+beta*z[n]/gamma); for (i=1;i<=n;i++) x[i] -= fact*z[i]; Nowget the solution vector x. free_vector(z,1,n); free_vector(u,1,n); free_vector(bb,1,n); } 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 = A−1 − A−1 · U · (1 + VT · A−1 · U) −1 · VT · A−1 (2.7.12)