LU decomposition

FCC link

Every square matrix $A$ can be decomposed into a product of a lower triangular matrix $L$ and a upper triangular matrix $U$, as described in LU decomposition.

$A = LU$

It is a modified form of Gaussian elimination.

While the Cholesky decomposition only works for symmetric, positive definite matrices, the more general LU decomposition works for any square matrix.

There are several algorithms for calculating $L$ and $U$.

To derive Crout's algorithm for a 3x3 example, we have to solve the following system:

\begin{align}A = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33}\\ \end{pmatrix}= \begin{pmatrix} l_{11} & 0 & 0 \\ l_{21} & l_{22} & 0 \\ l_{31} & l_{32} & l_{33}\\ \end{pmatrix} \begin{pmatrix} u_{11} & u_{12} & u_{13} \\ 0 & u_{22} & u_{23} \\ 0 & 0 & u_{33} \end{pmatrix} = LU\end{align}

We now would have to solve 9 equations with 12 unknowns. To make the system uniquely solvable, usually the diagonal elements of $L$ are set to 1

$l_{11}=1$

$l_{22}=1$

$l_{33}=1$

so we get a solvable system of 9 unknowns and 9 equations.

\begin{align}A = \begin{pmatrix} a_{11} & a_{12} & a_{13}\\ a_{21} & a_{22} & a_{23}\\ a_{31} & a_{32} & a_{33}\\ \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 \\ l_{21} & 1 & 0 \\ l_{31} & l_{32} & 1\\ \end{pmatrix} \begin{pmatrix} u_{11} & u_{12} & u_{13} \\ 0 & u_{22} & u_{23} \\ 0 & 0 & u_{33} \end{pmatrix} = \begin{pmatrix} u_{11} & u_{12} & u_{13} \\ u_{11}l_{21} & u_{12}l_{21}+u_{22} & u_{13}l_{21}+u_{23} \\ u_{11}l_{31} & u_{12}l_{31}+u_{22}l_{32} & u_{13}l_{31} + u_{23}l_{32}+u_{33} \end{pmatrix} = LU\end{align}

Solving for the other $l$ and $u$, we get the following equations:

$u_{11}=a_{11}$

$u_{12}=a_{12}$

$u_{13}=a_{13}$

$u_{22}=a_{22} - u_{12}l_{21}$

$u_{23}=a_{23} - u_{13}l_{21}$

$u_{33}=a_{33} - (u_{13}l_{31} + u_{23}l_{32})$

and for $l$:

$l_{21}=\frac{1}{u_{11}} a_{21}$

$l_{31}=\frac{1}{u_{11}} a_{31}$

$l_{32}=\frac{1}{u_{22}} (a_{32} - u_{12}l_{31})$

We see that there is a calculation pattern, which can be expressed as the following formulas, first for $U$

$u_{ij} = a_{ij} - \sum_{k=1}^{i-1} u_{kj}l_{ik}$

and then for $L$

$l_{ij} = \frac{1}{u_{jj}} (a_{ij} - \sum_{k=1}^{j-1} u_{kj}l_{ik})$

We see in the second formula that to get the $l_{ij}$ below the diagonal, we have to divide by the diagonal element (pivot) $u_{jj}$, so we get problems when $u_{jj}$ is either 0 or very small, which leads to numerical instability.

The solution to this problem is pivoting $A$, which means rearranging the rows of $A$, prior to the $LU$ decomposition, in a way that the largest element of each column gets onto the diagonal of $A$. Rearranging the rows means to multiply $A$ by a permutation matrix $P$:

$PA \Rightarrow A'$

Example:

\begin{align} \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} 1 & 4 \\ 2 & 3 \end{pmatrix} \Rightarrow \begin{pmatrix} 2 & 3 \\ 1 & 4 \end{pmatrix} \end{align}

The decomposition algorithm is then applied on the rearranged matrix so that

$PA = LU$

Test

{{test}}

Console output