Lesson 17 of 18

LU Decomposition

LU Decomposition

LU decomposition factors a matrix AA into a lower-triangular matrix LL and an upper-triangular matrix UU:

A=LUA = LU

This is the matrix form of Gaussian eliminationLL records the row operations and UU is the result.

Doolittle's Algorithm

For an n×nn \times n matrix:

  • UU has the same rows as the row-echelon form of AA
  • LL is unit lower-triangular (diagonal of 1s), with:

Lik=Aikj=0k1LijUjkUkkL_{ik} = \frac{A_{ik} - \sum_{j=0}^{k-1} L_{ij} U_{jk}}{U_{kk}}

Ukj=Akji=0k1LkiUijU_{kj} = A_{kj} - \sum_{i=0}^{k-1} L_{ki} U_{ij}

Example

A=(2164)=(1031)(2101)=LUA = \begin{pmatrix} 2 & 1 \\ 6 & 4 \end{pmatrix} = \begin{pmatrix}1 & 0 \\ 3 & 1\end{pmatrix}\begin{pmatrix}2 & 1 \\ 0 & 1\end{pmatrix} = LU

Verify: 32+10=63 \cdot 2 + 1 \cdot 0 = 6 ✓, 31+11=43 \cdot 1 + 1 \cdot 1 = 4

Applications

Once you have A=LUA = LU, solving Ax=bAx = b becomes two easy triangular solves:

  1. Solve Ly=bLy = b (forward substitution)
  2. Solve Ux=yUx = y (back substitution)

This is much faster than recomputing Gaussian elimination for every new right-hand side bb.

def lu_decomp(A):
    n = len(A)
    L = [[1.0 if i==j else 0.0 for j in range(n)] for i in range(n)]
    U = [row[:] for row in A]
    for k in range(n):
        for i in range(k+1, n):
            L[i][k] = U[i][k] / U[k][k]
            for j in range(k, n):
                U[i][j] -= L[i][k] * U[k][j]
    return L, U

Your Task

Implement lu_decomp(A) using Doolittle's algorithm. Return (L, U).

Pyodide loading...
Loading...
Click "Run" to execute your code.