The forward substitution algorithm solves the linear system
A lower-triangular linear system
This can also be written as the set of linear equations:
The forward substitution algorithm solves a lower-triangular linear system by working from the top down and solving each variable in turn. In math this is:
The properties of the forward substitution algorithm are:
The code for the forward substitution algorithm to solve
import numpy as np
def forward_sub(L, b):
"""x = forward_sub(L, b) is the solution to L x = b
L must be a lower-triangular matrix
b must be a vector of the same leading dimension as L
"""
n = L.shape[0]
x = np.zeros(n)
for i in range(n):
tmp = b[i]
for j in range(i):
tmp -= L[i,j] * x[j]
x[i] = tmp / L[i,i]
return x
The back substitution algorithm solves the linear system
The upper-triangular system
The back substitution solution works from the bottom up to give:
The properties of the back substitution algorithm are:
The code for the back substitution algorithm to solve
import numpy as np
def back_sub(U, b):
"""x = back_sub(U, b) is the solution to U x = b
U must be an upper-triangular matrix
b must be a vector of the same leading dimension as U
"""
n = U.shape[0]
x = np.zeros(n)
for i in range(n-1, -1, -1):
tmp = b[i]
for j in range(i+1, n):
tmp -= U[i,j] * x[j]
x[i] = tmp / U[i,i]
return x
The LU decomposition of a matrix
The properties of the LU decomposition are:
Consider the matrix
The LU factorization is
An example of a matrix which has no LU decomposition is
If we try and find the LU decomposition of this matrix then we get
Equating the individual entries gives us four equations to solve. The top-left and bottom-left entries give the two equations:
These equations have no solution, so
Knowing the LU decomposition for a matrix
where we first evaluate
An equivalent way to write this is to introduce a new vector
We have thus replaced
The LU solve algorithm for solving the linear system
import numpy as np
def lu_solve(L, U, b):
"""x = lu_solve(L, U, b) is the solution to L U x = b
L must be a lower-triangular matrix
U must be an upper-triangular matrix of the same size as L
b must be a vector of the same leading dimension as L
"""
y = forward_sub(L, b)
x = back_sub(U, y)
return x
The number of operations for the LU solve algorithm is
Given a matrix
In the above block form of the
Comparing the left- and right-hand side entries of the above block matrix equation we see that:
These four equations can be rearranged to solve for the components of the
The first three equations above can be immediately evaluated to give the first row and column of
The code for the recursive leading-row-column LU algorithm to find
import numpy as np
def lu_decomp(A):
"""(L, U) = lu_decomp(A) is the LU decomposition A = L U
A is any matrix
L will be a lower-triangular matrix with 1 on the diagonal, the same shape as A
U will be an upper-triangular matrix, the same shape as A
"""
n = A.shape[0]
if n == 1:
L = np.array([[1]])
U = A.copy()
return (L, U)
A11 = A[0,0]
A12 = A[0,1:]
A21 = A[1:,0]
A22 = A[1:,1:]
L11 = 1
U11 = A11
L12 = np.zeros(n-1)
U12 = A12.copy()
L21 = A21.copy() / U11
U21 = np.zeros(n-1)
S22 = A22 - np.outer(L21, U12)
(L22, U22) = lu_decomp(S22)
L = np.block([[L11, L12], [L21, L22]])
U = np.block([[U11, U12], [U21, U22]])
return (L, U)
The number of operations for the recursive leading-row-column LU decomposition algorithm is
We can put the above sections together to produce an algorithm for solving the system
The properties of this algorithm are:
The code for the linear solver using LU decomposition is: import numpy as np
import numpy as np
def linear_solve_without_pivoting(A, b):
"""x = linear_solve_without_pivoting(A, b) is the solution to A x = b (computed without pivoting)
A is any matrix
b is a vector of the same leading dimension as A
x will be a vector of the same leading dimension as A
"""
(L, U) = lu_decomp(A)
x = lu_solve(L, U, b)
return x
The LU decomposition can fail when the top-left entry in the matrix
There are many different pivoting algorithms. The most common of these are full pivoting, partial pivoting, and scaled partial pivoting. We will only discuss partial pivoting in detail.
1) Partial pivoting only rearranges the rows of
2) Full pivoting rearranges both rows and columns.
3) Scaled partial pivoting approximates full pivoting without actually rearranging columns.
The LU decomposition with partial pivoting (LUP) of an
The properties of the LUP decomposition are:
Knowing the LUP decomposition for a matrix
The code for the LUP solve algorithm to solve the linear system ${\bf L U x} = {\bf P b}$ is:
import numpy as np
def lup_solve(L, U, P, b):
"""x = lup_solve(L, U, P, b) is the solution to L U x = P b
L must be a lower-triangular matrix
U must be an upper-triangular matrix of the same shape as L
P must be a permutation matrix of the same shape as L
b must be a vector of the same leading dimension as L
"""
z = np.dot(P, b)
x = lu_solve(L, U, z)
return x
The number of operations for the LUP solve algorithm is
Just as there are different LU decomposition algorithms, there are also different algorithms to find a LUP decomposition. Here we use the recursive leading-row-column LUP algorithm.
This algorithm is a recursive method for finding
1) First choose
2) Write
3) Let
4) Factorize the (unknown) full permutation matrix
5) Using the factorization
6) Equating the entries in the above matrices gives the equations
7) Substituting the first three equations above into the last one and rearranging gives
8) Recurse to find the LUP decomposition of
9) Solve for the first rows and columns of
10) Finally, reconstruct the full matrices
In code the recursive leading-row-column LUP algorithm for finding the LU decomposition of
import numpy as np
def lup_decomp(A):
"""(L, U, P) = lup_decomp(A) is the LUP decomposition P A = L U
A is any matrix
L will be a lower-triangular matrix with 1 on the diagonal, the same shape as A
U will be an upper-triangular matrix, the same shape as A
U will be a permutation matrix, the same shape as A
"""
n = A.shape[0]
if n == 1:
L = np.array([[1]])
U = A.copy()
P = np.array([[1]])
return (L, U, P)
i = np.argmax(A[:,0])
A_bar = np.vstack([A[i,:], A[:i,:], A[(i+1):,:]])
A_bar11 = A_bar[0,0]
A_bar12 = A_bar[0,1:]
A_bar21 = A_bar[1:,0]
A_bar22 = A_bar[1:,1:]
S22 = A_bar22 - np.dot(A_bar21, A_bar12) / A_bar11
(L22, U22, P22) = lup_decomp(S22)
L11 = 1
U11 = A_bar11
L12 = np.zeros(n-1)
U12 = A_bar12.copy()
L21 = np.dot(P22, A_bar21) / A_bar11
U21 = np.zeros(n-1)
L = np.block([[L11, L12], [L21, L22]])
U = np.block([[U11, U12], [U21, U22]])
P = np.block([
[np.zeros((1, i-1)), 1, np.zeros((1, n-i))],
[P22[:,:(i-1)], np.zeros((n-1, 1)), P22[:,i:]]
])
return (L, U, P)
The properties of the recursive leading-row-column LUP decomposition algorithm are:
The computational complexity (number of operations) of the algorithm is
The last step in the code that computes
Just as with the plain LU decomposition, we can use LUP decomposition to solve the linear system
The properties of this algorithm are:
The code for the linear solver using LUP decomposition is:
import numpy as np
def linear_solve(A, b):
"""x = linear_solve(A, b) is the solution to A x = b (computed with partial pivoting)
A is any matrix
b is a vector of the same leading dimension as A
x will be a vector of the same leading dimension as A
"""
(L, U, P) = lup_decomp(A)
x = lup_solve(L, U, P, b)
return x
Recall our example of a matrix which has no LU decomposition:
To find the LUP decomposition of