feat(maths): add LU decomposition algorithm for matrix factorization#14696
Closed
MD-Mushfiqur123 wants to merge 2 commits into
Closed
feat(maths): add LU decomposition algorithm for matrix factorization#14696MD-Mushfiqur123 wants to merge 2 commits into
MD-Mushfiqur123 wants to merge 2 commits into
Conversation
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.
for more information, see https://pre-commit.ci
Closing this pull request as invalid@MD-Mushfiqur123, this pull request is being closed as none of the checkboxes have been marked. It is important that you go through the checklist and mark the ones relevant to this pull request. Please read the Contributing guidelines. If you're facing any problem on how to mark a checkbox, please read the following instructions:
NOTE: Only |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LU Decomposition Algorithm
Adds LU decomposition (Doolittle algorithm) to the maths module for matrix factorization and solving systems of linear equations.
What This Adds
A new
maths/lu_decomposition.pyfile containing:lu_decomposition(matrix)Performs LU decomposition on a square matrix, returning
(L, U)where:solve_with_lu(lower, upper, b)Solves the system Ax = b using the LU decomposition, via:
Features
__main__blockAlgorithm
Uses the Doolittle algorithm with O(n^3) time complexity for decomposition and O(n^2) for solving. More efficient than Gaussian elimination when solving multiple systems with the same coefficient matrix.
Testing
All 10 doctests pass.