Proofs: LU decomposition
These are the human proofs of LU decomposition, at full length.
LU-DECOMPOSE returns P A = L U with U in echelon form, and LU-SOLVE solves A x = b full
This proof shows: correctness, termination.
We use the notation of the page. is over a field . For a permutation of , is the matrix with row equal to row of . is the matrix with in place and elsewhere.
Termination. The loop of line L2 runs at most times, and the inner loops of lines L6 and L8 have fixed bounds. So LU-DECOMPOSE stops after steps. LU-SOLVE has two loops of steps each.
Invariant. At each test of line L2, with the next column and the next pivot row:
(I1) .
(I2) is unit lower triangular, and for each row , for .
(I3) Rows to of are nonzero, the first nonzero entry of row is in a column with , and rows to of are zero in columns to .
Initialization. Line L1 sets , , the identity and , before column . Then , satisfies (I2), and (I3) holds with no pivot rows and no columns before .
Maintenance. Consider one pass with column , and suppose (else line L3 stops the loop, and the invariant is not changed).
Case 1: no has (line L4 goes to the next column). Then rows to of are zero in columns to , so (I3) holds for . Nothing else changes.
Case 2: line L4 finds with . Let be the transposition of rows and (the identity if ). Line L5 replaces by and by the permutation with . It replaces by restricted as follows: swaps rows and columns of . By (I2), rows and of have zeros in the columns to except on the diagonal, so is obtained from by swapping only the entries in columns to of rows and , which is exactly what line L5 does. Then
since . So (I1) holds after line L5. is still unit lower triangular and satisfies (I2), because the swap keeps zeros in columns to of the rows to , apart from the diagonal. (I3) still holds for rows to , which are not touched, and rows to are still zero in the columns before .
Now let . For , lines L7 and L8 set , store , and replace row of by row minus times row . This is with , and , because for . Right multiplication by adds times column of to column , for each . Column of is the unit vector : its entries above row are zero because is lower triangular, its diagonal entry is , and its entries in the rows are zero by (I2), because . Column of is for the same reason. So is with put in place , which is what line L7 stores. Hence and (I1) holds. After the update, for ; rows to are zero in columns to (they were zero in columns before , and row is too). Row is nonzero, its first nonzero entry is in column . Line L9 sets . Now (I2) holds for the new , because the only new nonzero entries of below the diagonal are in column , and (I3) holds for .
Termination of the loop. When the loop ends, either ran past , or . In the first case (I3) with says that rows to of are zero and rows to have strictly increasing leading columns: is in row echelon form. In the second case all rows are pivot rows with increasing leading columns, which is also an echelon form. With (I1) and (I2), line L10 returns , a unit lower triangular and an echelon with .
LU-SOLVE. Let be and nonsingular. Then is nonsingular, and since it is in echelon form and square, its rows are pivot rows with leading columns in , so and is upper triangular with a nonzero diagonal. Line L11 computes, for in order, the unique with , using only the with already computed; so . Line L12 computes, for , the unique with , which is possible because ; so . Then , and since is invertible, . The solution is unique because is nonsingular.
Determinant. For square , , and , so . If is singular, the echelon form has a zero row, so the product is , as it must be.