
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - Matrix LU Decomposition
What is LU Decomposition?
LU decomposition is also called as LU factorization. This is a mathematical technique used to decompose a matrix into the product of two simpler matrices namely a lower triangular matrix L and an upper triangular matrix U. This is particularly useful in solving systems of linear equations, inverting matrices and calculating determinants.
We can explain the LU decomposition as, when given a square matrix A then Lu decomposition finds two matrices as follows −
- L: A lower triangular matrix with ones on the diagonal lii = 1.
- U: An upper triangular matrix.
Such that −
A = L . U
If partial pivoting is used to improve numerical stability then the decomposition includes a permutation matrix P which results in −
P. A = L . U
Here P is a permutation matrix representing row swaps.
Steps in Lu Decomposition
Following are the steps in Lu Decomposition in scipy −
- Start with the matrix A.
- Decompose A into L and U by systematically eliminating elements below the diagonal i.e., Gaussian elimination.
- Optionally include row permutations to form P by ensuring the pivot elements i.e., diagonal entries of U which are nonzero and numerically stable.
Implementation of Matrix LU Decomposition in scipy
In Python's SciPy library LU decomposition is performed using scipy.linalg.lu which provides −
- P, L and U matrices directly.
- Functionality for solving equations using LU decomposition via scipy.linalg.lu_solve in combination with scipy.linalg.lu_factor.
Decomposing a Matrix into P,L,U
Following is the example of performing LU decomposition of a square matrix into permutation, lower and upper triangular matrices by using the function scipy.linalg.lu() −
import numpy as np from scipy.linalg import lu # Define a square matrix A A = np.array([[4, 3], [6, 3]]) # Perform LU decomposition P, L, U = lu(A) # Display the results print("Original Matrix A:") print(A) print("\nPermutation Matrix P:") print(P) print("\nLower Triangular Matrix L:") print(L) print("\nUpper Triangular Matrix U:") print(U) # Verify PA = LU PA = P @ A LU = L @ U print("\nCheck if PA equals LU:") print(np.allclose(PA, LU))
Following is the output of decomposing a matrix into P, L, U in scipy −
Original Matrix A: [[4 3] [6 3]] Permutation Matrix P: [[0. 1.] [1. 0.]] Lower Triangular Matrix L: [[1. 0. ] [0.66666667 1. ]] Upper Triangular Matrix U: [[6. 3.] [0. 1.]] Check if PA equals LU: True
Solving a Linear System Using LU Decomposition
We can solve the linear system using the matrix LU Decomposition in scipy with the help of the functions scipy.linalg.lu() and scipy.linalg.lu_solve(). In this example we are solving AX = B for a given A and B −
import numpy as np from scipy.linalg import lu_factor, lu_solve # Define matrix A and right-hand side vector B A = np.array([[3, 1], [1, 2]]) B = np.array([9, 8]) # Factorize A into LU components lu, piv = lu_factor(A) # Solve for X in AX = B X = lu_solve((lu, piv), B) # Display the results print("Solution X:") print(X) # Verify the solution print("\nCheck if A @ X equals B:") print(np.allclose(A @ X, B))
Here is the output of solving the linear system AX = B −
Solution X: [2. 3.] Check if A @ X equals B: True
LU Decomposition Without Explicit Permutation Matrix
Here is the example of finding LU Decomposition without Explicit Permutation Matrix and return PA directly without separate P −
import numpy as np from scipy.linalg import lu # Define a matrix A A = np.array([[2, 3, 1], [4, 7, 1], [6, 18, 1]]) # Perform LU decomposition with permuted L L_permuted, U = lu(A, permute_l=True) # Display results print("Matrix A:") print(A) print("\nPermuted Lower Triangular Matrix (L_permuted):") print(L_permuted) print("\nUpper Triangular Matrix U:") print(U) # Verify PA = LU PA = L_permuted @ U print("\nReconstructed Matrix (PA):") print(PA) print("\nCheck if PA equals A:") print(np.allclose(PA, A))
Here is the output of the LU Decompositton without explicit Permutation matrix −
Matrix A: [[ 2 3 1] [ 4 7 1] [ 6 18 1]] Permuted Lower Triangular Matrix (L_permuted): [[0.33333333 0.6 1. ] [0.66666667 1. 0. ] [1. 0. 0. ]] Upper Triangular Matrix U: [[ 6. 18. 1. ] [ 0. -5. 0.33333333] [ 0. 0. 0.46666667]] Reconstructed Matrix (PA): [[ 2. 3. 1.] [ 4. 7. 1.] [ 6. 18. 1.]] Check if PA equals A: True