Lesson 17 of 18
LU Decomposition
LU Decomposition
LU decomposition factors a matrix into a lower-triangular matrix and an upper-triangular matrix :
This is the matrix form of Gaussian elimination — records the row operations and is the result.
Doolittle's Algorithm
For an matrix:
- has the same rows as the row-echelon form of
- is unit lower-triangular (diagonal of 1s), with:
Example
Verify: ✓, ✓
Applications
Once you have , solving becomes two easy triangular solves:
- Solve (forward substitution)
- Solve (back substitution)
This is much faster than recomputing Gaussian elimination for every new right-hand side .
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.