Commit c4269e0
Codebuff Contributor
feat(maths): add LU decomposition algorithm for matrix factorization
This commit introduces a complete implementation of the LU decomposition
algorithm in the maths module, providing a fundamental numerical linear
algebra tool for matrix factorization and solving systems of linear
equations efficiently.
LU Decomposition Overview:
LU decomposition factorizes a square matrix A into the product of two
triangular matrices: a lower triangular matrix L and an upper triangular
matrix U, such that A = L * U. This decomposition is named after the
mathematician Tadeusz Banachiewicz who formalized it in 1938, though
the underlying algorithm traces back to the work of Alan Turing in 1948.
The Doolittle algorithm implementation used in this contribution produces
an L matrix with ones on the main diagonal (unit lower triangular) and a
general upper triangular U matrix. This is one of the most commonly used
variants of LU decomposition and is particularly well-suited for solving
systems of linear equations.
Mathematical Foundation:
Given a square matrix A of size n x n, the decomposition computes:
A = L * U
where:
- L is a lower triangular matrix with L[i][i] = 1 for all i
- U is an upper triangular matrix with U[i][j] = 0 for all i > j
The algorithm computes elements of L and U iteratively:
For each column k from 0 to n-1:
1. Compute the k-th row of U:
U[k][j] = A[k][j] - sum(L[k][s] * U[s][j] for s < k)
2. Compute the k-th column of L:
L[i][k] = (A[i][k] - sum(L[i][s] * U[s][k] for s < k)) / U[k][k]
The algorithm requires that all pivot elements U[k][k] be non-zero.
If a zero pivot is encountered, the matrix may be singular or may
require partial pivoting (row exchanges) for numerical stability.
Implementation Details:
The module provides two main functions:
1. lu_decomposition(matrix):
Takes a square matrix as input and returns the (L, U) tuple.
Includes comprehensive input validation:
- Checks that the matrix is square
- Detects zero pivots and raises informative errors
- Converts all input values to floats for consistent arithmetic
2. solve_with_lu(lower, upper, b):
Solves the system Ax = b given the LU decomposition of A.
Uses a two-step approach:
- Forward substitution to solve L * y = b
- Back substitution to solve U * x = y
Both functions include detailed docstrings following PEP 257 conventions,
with comprehensive doctests that verify correctness across multiple test
cases including:
- Standard 2x2 and 3x3 matrices
- Identity matrix (trivial case)
- Singular matrix detection (zero pivot)
- Non-square matrix validation
- System of equations solving
Algorithmic Complexity:
Time Complexity: O(n^3) for the decomposition, O(n^2) for solving
a system given the decomposition. This is the same asymptotic
complexity as Gaussian elimination, but LU decomposition has the
advantage that once the factorization is computed, solving multiple
systems with the same coefficient matrix only requires O(n^2) work
per system instead of O(n^3).
Space Complexity: O(n^2) for storing the L and U matrices.
Practical Applications:
LU decomposition is used extensively in:
- Engineering simulations (finite element analysis, CFD)
- Computer graphics (transformation matrices)
- Machine learning (linear regression, covariance computation)
- Economics (input-output models, Leontief models)
- Circuit analysis (nodal and mesh analysis)
- Structural engineering (stiffness matrix solutions)
This implementation provides an educational reference for understanding
the algorithm while being functional enough for small to medium-sized
matrices in practical applications.1 parent 791deb4 commit c4269e0
1 file changed
Lines changed: 169 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
0 commit comments