LU decomposition

Uses your system voice; voice and rate in settings.

Q833089Snapshot

≈ 21 min read

Names in other languages

  • EnglishLU decomposition
  • FrançaisDécomposition LU
  • eoLU-malkomponaĵo
  • PortuguêsDecomposição LU
  • isLU-þáttun
  • Polskimetoda LU
  • 简体中文LU分解
  • 繁體中文LU分解
  • 中文LU分解

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In numerical analysis and linear algebra, lower–upper (LU) decomposition or factorization factors a matrix as the product of a lower triangular matrix and an upper triangular matrix (see matrix multiplication and matrix decomposition). The product sometimes includes a permutation matrix as well. LU decomposition can be viewed as the matrix form of Gaussian elimination. Computers usually solve square systems of linear equations using LU decomposition, and it is also a key step when inverting a matrix or computing the determinant of a matrix. It is also sometimes referred to as LR decomposition (factors into left and right triangular matrices). The LU decomposition algorithm for general matrices was introduced by the Polish astronomer Tadeusz Banachiewicz in 1938.

In numerical analysis and linear algebra, lower–upper (LU) decomposition or factorization factors a matrix as the product of a lower triangular matrix and an upper triangular matrix (see matrix multiplication and matrix decomposition). The product sometimes includes a permutation matrix as well. LU decomposition can be viewed as the matrix form of Gaussian elimination. Computers usually solve square systems of linear equations using LU decomposition, and it is also a key step when inverting a matrix or computing the determinant of a matrix. It is also sometimes referred to as LR decomposition (factors into left and right triangular matrices). The LU decomposition algorithm for general matrices was introduced by the Polish astronomer Tadeusz Banachiewicz in 1938. B = A^\textsf{T} = (LU)^\textsf{T} =U^\textsf{T}L^\textsf{T}. After transposition, is lower triangle while is upper unitriangular factor of . This demonstrates also, that operations on rows (e.g. pivoting) are equivalent to those on columns of a transposed matrix, and in general choice of row or column algorithm offers no advantage. In the lower triangular matrix all elements above the main diagonal are zero, in the upper triangular matrix, all the elements below the diagonal are zero. For example, for a matrix , its LU decomposition looks like this: \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix} = \begin{bmatrix} \ell_{11} & 0 & 0 \\ \ell_{21} & \ell_{22} & 0 \\ \ell_{31} & \ell_{32} & \ell_{33} \end{bmatrix} \begin{bmatrix} u_{11} & u_{12} & u_{13} \\ 0 & u_{22} & u_{23} \\ 0 & 0 & u_{33} \end{bmatrix}. Without a proper ordering or permutations in the matrix, the factorization may fail to materialize. For example, it is easy to verify (by expanding the matrix multiplication) that a_{11} = \ell_{11} u_{11} . If a_{11} = 0 , then at least one of \ell_{11} and u_{11} has to be zero, which implies that either or is singular. This is impossible if is nonsingular (invertible). In terms of operations, zeroing/elimination of remaining elements of first column of involves division of a_{21}, a_{31} with a_{11} , impossible if it is 0. This is a procedural problem. It can be removed by simply reordering the rows of so that the first element of the permuted matrix is nonzero. The same problem in subsequent factorization steps can be removed the same way. For numerical stability against rounding errors/division by small numbers it is important to select a_{11} of large absolute value (cf. pivoting). The above example of matrices demonstrates that matrix product of top row and leftmost columns of involved matrices plays special role for to succeed. Let us mark consecutive versions of matrices with (0),\;(1),\dots and then let us write matrix product A\equiv A^{(0)}=L^{(0)}U^{(0)} in such way that these rows and columns are separated from the rest. In doing so we shall use block matrix notation, such that e.g. a\equiv a_{11} is an ordinary number, {\bf w}^\textsf{T} \equiv(a_{12}, a_{13})^\textsf{T} is a row vector and {\bf v}=(a_{21},a_{31}) is a column vector and A' is sub-matrix of matrix A^{(0)} without top row and leftmost column. Then we can replace A^{(0)}=L^{(0)}U^{(0)} with a block matrix product. Namely it turns out that one can multiply matrix blocks in such way as if they were ordinary numbers, i.e. row times column, except that now their components are sub-matrices, sometimes reduced to scalars or vectors. Thus u{\bf l} denotes a vector obtained from {\bf l} after multiplication of each component by a number {\bf lu}^\textsf{T} is an outer product of vectors i.e. a matrix which first column is next is u_{13}{\bf l} and so on for all components of {\bf u} and L^{(1)}U^{(1)} is a product of sub-matrices of L^{(0)},\;U^{(0)} \begin{align} \left( \begin{array}{c|c} a & {\bf w}^\textsf{T} \\ \hline \\[-0.5em] {\bf v} & \quad A' \quad \\[-0.5em] \\ \end{array} \right) &= \left( \begin{array}{c|c} {\rm 1} & {\bf 0}^\textsf{T} \\ \hline \\[-0.5em] {\bf l} & \quad L^{(1)} \quad \\[-0.5em] \\ \end{array} \right)\; \left( \begin{array}{c|c} u & {\bf u}^\textsf{T} \\ \hline \\[-0.5em] {\bf 0} & \quad U^{(1)} \\[-0.5em] \\ \end{array} \right) \\ &=\left( \begin{array}{c|c} u & {\bf u}^\textsf{T} \\ \hline \\[-0.5em] u{\bf l} & \quad {\bf lu}^\textsf{T} + L^{(1)}U^{(1)} \\[-0.5em] \\ \end{array} \right) \end{align} From equality of first and last matrices follow final {\bf l}={(1/a)}{\bf v} while matrix A' becomes updated/replaced with A^{(1)}\equiv L^{(1)}U^{(1)}= Now comes the crucial observation: nothing prevents us to treat A^{(1)} the same way as we did with repeatedly. If dimension of A is , after such steps all columns \bf v form sub-diagonal part of triangle matrix L and all pivots a combined with rows {\bf w}^\textsf{T} form upper triangle matrix as required. In the above example so only two steps suffice. The above procedure demonstrates that at no step the top diagonal pivot element a of consecutive sub-matrices can be zero. To avoid it columns or rows may be swapped so that a becomes nonzero. Such procedure involving permutation is called LUP, decomposition with pivoting. Permutation of columns corresponds to matrix product AQ^{(0)} where Q^{(0)} is a permutation matrix, i.e. the identity matrix I after the same column permutation. After all steps such LUP decomposition applies to Present computation scheme and similar in Cormen et al. are examples of . They demonstrate two general properties of LU factorization: Recurrence algorithms are not overly costly in terms of algebraic operations yet they suffer from practical disadvantage due to need to update and store most elements of at each step. It will be seen that by reordering calculations it is possible to dispose with storage of intermediate values. PA = LU, \quad (AQ=LU), where and are again lower and upper triangular matrices, and and are corresponding permutation matrices, which, when correspondingly left- and right-multiplied to , reorder the rows and columns of . It turns out that all square matrices can be factorized in this form, and the factorization is numerically stable in practice. This makes LUP decomposition a useful technique in practice. A variant called ' ' at each step involves search of maximum element the way rook moves on a chessboard, along column, row, column again and so on till reaching a pivot maximal in both its row and column. It can be proven that for large matrices of random elements its cost of operations at each step is similarly to partial pivoting proportional to the length of matrix side unlike its square for full pivoting. PAQ = LU, where , , and are defined as before, and is a permutation matrix that reorders the columns of .

Lower-diagonal-upper (LDU) decomposition

A ' ' (LDU) is a decomposition of the form A = LDU, where is a diagonal matrix, and and are unitriangular matrices, meaning that all the entries on the diagonals of and are one. \begin{bmatrix} 4 & 3 \\ 6 & 3 \end{bmatrix} = \begin{bmatrix} \ell_{11} & 0 \\ \ell_{21} & \ell_{22} \end{bmatrix} \begin{bmatrix} u_{11} & u_{12} \\ 0 & u_{22} \end{bmatrix}. One way to find the LU decomposition of this simple matrix would be to simply solve the linear equations by inspection. Expanding the matrix multiplication gives \left\{ \begin{alignat}{4} \ell_{11} \cdot u_{11} && \;+\; && 0 \cdot 0 &&\;=\; && 4 \\ \ell_{11} \cdot u_{12} && \;+\; && 0 \cdot u_{22} &&\;=\; && 3 \\ \ell_{21} \cdot u_{11} && \;+\; && \ell_{22} \cdot 0 &&\;=\; && 6 \\ \ell_{21} \cdot u_{12} && \;+\; && \ell_{22} \cdot u_{22} &&\;=\; && 3 \end{alignat}\right. This system of equations is underdetermined. In this case any two nonzero elements of and matrices are parameters of the solution and can be set arbitrarily to any nonzero value. Therefore, to find the unique LU decomposition, it is necessary to put some restriction on and matrices. For example, we can conveniently require the lower triangular matrix to be a unit triangular matrix, so that all the entries of its main diagonal are set to one. Then the system of equations has the following solution: \begin{align} \ell_{11} &= \ell_{22} = 1 \\ \ell_{21} &= 1.5 \\ u_{11} &= 4 \\ u_{12} &= 3 \\ u_{22} &= -1.5 \end{align} Substituting these values into the LU decomposition above yields \begin{bmatrix} 4 & 3 \\ 6 & 3 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 1.5 & 1 \end{bmatrix} \begin{bmatrix} 4 & 3 \\ 0 & -1.5 \end{bmatrix}. Any square matrix admits LUP and PLU factorizations. If is invertible, then it admits an LU (or LDU) factorization if and only if all its leading principal minors are nonzero (for example \left[\begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix}\right] does not admit an LU or LDU factorization). If is a singular matrix of rank , then it admits an LU factorization if the first leading principal minors are nonzero, although the converse is not true. If a square, invertible matrix has an LDU factorization (with all diagonal entries of and equal to ), then the factorization is unique. In that case, the LU factorization is also unique if we require that the diagonal of either or consists of ones. In general, any square matrix could have one of the following: a unique LU factorization (as mentioned above); infinitely many LU factorizations if any of the first columns are linearly dependent; no LU factorization if the first columns are linearly independent and at least one leading principal minor is zero. In Case 3, one can approximate an LU factorization by changing a diagonal entry to to avoid a zero leading principal minor. A = LL^*\,. This decomposition is called the Cholesky decomposition. If is positive definite, then the Cholesky decomposition exists and is unique. Furthermore, computing the Cholesky decomposition is more efficient and numerically more stable than computing some other LU decompositions. A^{(n-1)} = \begin{pmatrix} * & & & \cdots & & & * \\ 0 & \ddots & & & & \\ & \ddots & * & & & \\ \vdots & & 0 & a_{n,n}^{(n-1)} & & & \vdots \\ & & \vdots & a_{i,n}^{(n-1)} & * \\ & & & \vdots & \vdots & \ddots \\ 0 & \cdots & 0 & a_{i,n}^{(n-1)} & * & \cdots & * \end{pmatrix} For our matrix , we want to set every element below a_{n,n}^{(n-1)} to zero (where a_{n,n}^{(n-1)} is the element in the -th column of the main diagonal). We will denote each element below a_{n,n}^{(n-1)} as a_{i,n}^{(n-1)} (where ). To set a_{i,n}^{(n-1)} to zero, we set for each row . For this operation, {{nowrap| \ell_{i,n} := {a_{i,n}^{(n-1)}}/{a_{n,n}^{(n-1)}} .}} Once we have performed the row operations for the first columns, we have obtained an upper triangular matrix which is denoted by . We can also create the lower triangular matrix denoted as , by directly inputting the previously calculated values of via the formula below. L = \begin{pmatrix} 1 & 0 & \cdots & 0 \\ \ell_{2,1} & \ddots & \ddots & \vdots \\ \vdots & \ddots & \ddots & 0 \\ \ell_{N,1} & \cdots & \ell_{N,N-1} & 1 \end{pmatrix} A = \begin{pmatrix} 0 & 5 & \frac{22}{3} \\ 4 & 2 & 1 \\ 2 & 7 & 9 \\ \end{pmatrix}, we will choose to implement partial pivoting and thus swap the first and second row so that our matrix and the first iteration of our matrix respectively become A^{(0)}=\begin{pmatrix} 4 & 2 & 1 \\ 0 & 5 & \frac{22}{3} \\ 2 & 7 & 9 \\ \end{pmatrix},\quad P^{(0)}=\begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \\ \end{pmatrix}. Once we have swapped the rows, we can eliminate the elements below the main diagonal on the first column by performing \begin{alignat}{0} row_2=row_2-(\ell_{2,1})\cdot row_1 \\ row_3=row_3-(\ell_{3,1})\cdot row_1 \end{alignat} such that, \begin{alignat}{0} \ell_{2,1}= \frac{0}{4}=0 \\ \ell_{3,1}= \frac{2}{4}=0.5 \end{alignat} Once these rows have been subtracted, we have derived from the matrix A^{(1)}= \begin{pmatrix} 4 & 2 & 1 \\ 0 & 5 & \frac{22}{3} \\ 0 & 6 & 8.5 \\ \end{pmatrix}. Because we are implementing partial pivoting, we swap the second and third rows of our derived matrix and the current version of our matrix respectively to obtain A^{(1)}=\begin{pmatrix} 4 & 2 & 1 \\ 0 & 6 & 8.5 \\ 0 & 5 & \frac{22}{3} \\ \end{pmatrix}, \quad P^{(1)}=\begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \\ \end{pmatrix}. Now, we eliminate the elements below the main diagonal on the second column by performing such that . Because no nonzero elements exist below the main diagonal in our current iteration of after this row subtraction, this row subtraction derives our final matrix (denoted as ) and final matrix: A^{(2)}=A^{(N-1)}=U=\begin{pmatrix} 4 & 2 & 1 \\ 0 & 6 & 8.5 \\ 0 & 0 & 0.25 \\ \end{pmatrix}, \quad P=\begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \\ \end{pmatrix}. After also switching the corresponding rows, we obtain our final matrix: L = \begin{pmatrix} 1 & 0 & 0 \\ \ell_{3,1} & 1 & 0 \\ \ell_{2,1} & \ell_{3,2} & 1 \\ \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 \\ 0.5 & 1 & 0 \\ 0 & \frac{5}{6} & 1 \\ \end{pmatrix} Now these matrices have a relation such that . If we did not swap rows at all during this process, we can perform the row operations simultaneously for each column by setting A^{(n)} := L^{-1}_n A^{(n-1)}, where L^{-1}_n is the identity matrix with its -th column replaced by the transposed vector . In other words, the lower triangular matrix L^{-1}_n = \begin{pmatrix} 1 & & & & & \\ & \ddots & & & & \\ & & 1 & & & \\ & & -\ell_{n+1,n} & & & \\ & & \vdots & & \ddots & \\ & & -\ell_{N,n} & & & 1 \end{pmatrix}. Performing all the row operations for the first columns using the A^{(n)} := L^{-1} _n A^{(n-1)} formula is equivalent to finding the decomposition A = L_1 L_1^{-1} A^{(0)} = L_1 A^{(1)} L_n = \begin{pmatrix} 1 & & & & & \\ & \ddots & & & & \\ & & 1 & & & \\ & & \ell_{n+1,n} & & & \\ & & \vdots & & \ddots & \\ & & \ell_{N,n} & & & 1 \end{pmatrix} If there are two lower triangular matrices with 1s in the main diagonal, and neither have a nonzero item below the main diagonal in the same column as the other, then we can include all nonzero items at their same location in the product of the two matrices. For example: \left(\begin{array}{ccccc} 1 & 0 & 0 & 0 & 0 \\ 77 & 1 & 0 & 0 & 0 \\ 12 & 0 & 1 & 0 & 0 \\ 63 & 0 & 0 & 1 & 0 \\ 7 & 0 & 0 & 0 & 1 \end{array}\right)\left(\begin{array}{ccccc} 1 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 22 & 1 & 0 & 0 \\ 0 & 33 & 0 & 1 & 0 \\ 0 & 44 & 0 & 0 & 1 \end{array}\right)=\left(\begin{array}{ccccc} 1 & 0 & 0 & 0 & 0 \\ 77 & 1 & 0 & 0 & 0 \\ 12 & 22 & 1 & 0 & 0 \\ 63 & 33 & 0 & 1 & 0 \\ 7 & 44 & 0 & 0 & 1 \end{array}\right) Finally, multiply together and generate the fused matrix denoted as (as previously mentioned). Using the matrix , we obtain . It is clear that in order for this algorithm to work, one needs to have a_{n,n}^{(n-1)} \neq 0 at each step (see the definition of . If this assumption fails at some point, one needs to interchange -th row with another row below it before continuing. This is why an LU decomposition in general looks like . All partial pivoting LU algorithms cost roughly the same amount, of order O\left({2\over3}n^3\right) operations, where is the number of rows or columns of . A\mathbf x = \mathbf b, we want to solve the equation for , given and . Suppose we have already obtained the LUP decomposition of such that , so . In this case the solution is done in two logical steps: First, we solve the equation for . Second, we solve the equation for . In both cases we are dealing with triangular matrices ( and ), which can be solved directly by forward and backward substitution without using the Gaussian elimination process (however we do need this process or equivalent to compute the LU decomposition itself). The above procedure can be repeatedly applied to solve the equation multiple times for different . In this case it is faster (and more convenient) to do an LU decomposition of the matrix once and then solve the triangular matrices for the different , rather than using Gaussian elimination each time. The matrices and could be thought to have "encoded" the Gaussian elimination process. The cost of solving a system of linear equations is approximately floating-point operations if the matrix has size . This makes it twice as fast as algorithms based on QR decomposition, which costs about floating-point operations when Householder reflections are used. For this reason, LU decomposition is usually preferred. AX = LUX = B. We can use the same algorithm presented earlier to solve for each column of matrix . Now suppose that is the identity matrix of size , . It follows that the result must be the inverse of . \begin{align} \det(A) &= \det\left(P^{-1}\right) \det(L) \det(U) \\ &= (-1)^S \left( \prod_{i=1}^n l_{ii} \right) \left( \prod_{i=1}^n u_{ii} \right) . \end{align} The second equation follows from the fact that the determinant of a triangular matrix is simply the product of its diagonal entries, and that the determinant of a permutation matrix is equal to where is the number of row exchanges in the decomposition. In the case of LU decomposition with full pivoting, also equals the right-hand side of the above equation, if we let be the total number of row and column exchanges. The same method readily applies to LU decomposition by setting equal to the identity matrix. \begin{align} {\mathbf x}\cdot IA'={\mathbf 0} &\rightarrow A'{\mathbf x=0}\equiv(A|{\mathbf l}){\mathbf x}, \\ A=G\cdot H &\rightarrow A^T=G^TH, \end{align} where ; ; refers to extended with the last column; and the last component of is . Matrix formulae to calculate rows and columns of LU factors by recursion are given in the remaining part of Banachiewicz's paper as Eq. (2.3) and (2.4) . This paper by Banachiewicz contains both derivation of and factors of respectively non-symmetric and symmetric matrices. They are sometimes confused as later publications tend to tie his name solely with the rediscovery of Cholesky decomposition. Banachiewicz himself can be excused of inaction as already next year he suffered from persecution by the Nazi German occupiers of Poland, spending three months in Sachsenhausen concentration camp, on release from which he carried himself from a train his collaborator and co-prisoner Antoni Wilk, who died of exhaustion a week later. Module mlu Implicit None Integer, Parameter :: SP = Kind(1d0) ! set I/O real precision Private Public luban, lusolve Contains Subroutine luban (a, tol, g, h, ip, condinv, detnth) ! By Banachiewicz (1938, hereafter B38) LU decomposition method calculates such ! triangles L=G^T, and U=H that square B=A^T=G^TH=LU. Partial pivoting ! by column permutation IP(:) is modern addition. ! Within the code a, g correspond to B38 A^T and G^T, so that a=gh holds. ! ! Normal use is for square A, however for RHS l already known ! input of (A|l)^T yields (L|y^T)^T where x in L^Tx=y is solution of Ax=l. Real (SP), Intent (In) :: a (:, :)! input matrix A(m,n), n size(a, dim=1) .Or. n /* INPUT: A - array of pointers to rows of a square matrix having dimension N * Tol - small tolerance number to detect failure when the matrix is near degenerate * OUTPUT: Matrix A is changed, it contains a copy of both matrices L-E and U as A=(L-E)+U such that P*A=L*U. * The permutation matrix is not stored as a matrix, but in an integer vector P of size N+1 * containing column indexes where the permutation matrix has "1". The last element P[N]=S+N, * where S is the number of row exchanges needed for determinant computation, det(P)=(-1)^S */ int LUPDecompose(double **A, int N, double Tol, int *P) { int i, j, k, imax; double maxA, *ptr, absA; for (i = 0; i maxA) { maxA = absA; imax = k; } if (maxA = 0; i--) { for (int k = i + 1; k = 0; i--) { for (int k = i + 1; k public class SystemOfLinearEquations { public double[] SolveUsingLU(double[,] matrix, double[] rightPart, int n) { // decomposition of matrix double[,] lu = new double[n, n]; double sum = 0; for (int i = 0; i = 0; i--) { sum = 0; for (int k = i + 1; k function LU = LUDecompDoolittle(A) n = length(A); LU = A; for k = 2:n for i = 1 : k-1 lamda = LU(k,i) / LU (i, i); LU(k,i) = lamda; LU(k,i+1:n) = LU(k,i+1:n) - LU(i,i+1:n) * lamda; end end end function x = SolveLinearSystem(LU, B) n = length(LU); y = zeros(size(B)); % find solution of Ly = B for i = 1:n y(i,:) = B(i,:) - LU(i,1:i)*y(1:i,:); end % find solution of Ux = y x = zeros(size(B)); for i = n:(-1):1 x(i,:) = (y(i,:) - LU(i,(i + 1):n)*x((i + 1):n,:))/LU(i, i); end end A = [ 4 3 3; 6 3 3; 3 4 3 ] LU = LUDecompDoolittle(A) B = [ 1 2 3; 4 5 6; 7 8 9; 10 11 12 ]' x = SolveLinearSystem(LU, B) A * x

Text derived from https://en.wikipedia.org/w/index.php?oldid=1377203557 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

1 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.