The decomposition is closely related to gaussian elimination. It takes the original equation to be solved and splits it up into two separate equations involving a unit lower triangular matrix , and the row echelon matrix :
where . The matrix is a unit lower triangular matrix and thus has ones on the diagonal, whereas is in row echelon form with pivot values in the leading coefficients of each row.
Thus, we have converted our original problem of solving into two separate solves, first solving the equation , and then using the result to solve .
However, each of those solves is very cheap to compute, in this case for the 4x4 matrix shown above the solution of only needs 6 multiplication and 6 additions, whereas requires 4 divisions, 6 multiplications and 6 additions, leading to a total of 28 arithmetic operations, much fewer in comparison with the 62 operations required to solve the original equation . In general, decomposition for an matrix takes about flops, or floating point operations, to compute.
A relativly simple algorithm can be described if we assume that no pivoting is required during a gaussian elimination. In this case, the gaussian elimination process is a sequence of linear operations , with each operation being a row replacement that adds a multiple of one row to another below it (i.e. is lower triangular). The final matrix after applying the sequence of row reductions is in row echelon form, that is:
Since we have , we can show that the sequence of operations is also the sequence that reduces the matrix to an identity matrix:
therefore,
and,
This implies how we can build up the matrix . We choose values for such that the series of row operations convert the matrix to the identity matrix. Since each is lower triangular, we know that both and are also lower triangular.
For example, consider the following matrix
After three row reductions, , , and , we have the following result:
To build the 1st column of , we simply divide the 1st column of by the pivot value 3, giving
For the next column we do the same, using the new pivot value in row 2 to reduce and to zero, and then dividing the column vector under the pivot by the pivot value 2 to obtain the next column of .
Repeating this process for all the columns in , we obtain the final factorisation. You can verify for yourself that repeating the same row operations we did to form to the matrix reduces it to the identity matrix.
Of course, for any practial factorisation we need to consider pivoting. Any matrix can be factorised into , where is a permutation matrix, and and are defined as before. During the gaussian elimination steps we store an array of row indices indicating that row is interchanged with row , and the resultant array of can be used to build the permutation matrix (It would be wasteful to store the entire martix so the array is stored instead).
Thus, the LU algorithm proceeds as follows:
In practice, most library implementation store and in the same matrix since they are lower and upper triangular respectivly.
It is often very benificial when solving linear systems to consider and take advantage of any special structure that the matrix might possesses. The decomposition is a varient on LU decomposition which is only applicable to a symmetric matrix (i.e. ). The advantage of using this decomposition is that it takes advantage of the redundent entries in the matrix to reduce the amount of computation to , which is about a half that required for the decomposition.
Take your gaussian elimination code that you wrote in the previous lesson and use it to
write an LU decomposition function that takes in a martix , and returns , and
the array . You can check your answer using
scipy.linalg.lu_factor
.
Hint: the permutation matrix is tricky to construct, so you might want to use the test
given in the documentation for lu_factor
.
import numpy as np
import matplotlib.pylab as plt
import scipy
import scipy.linalg
import sys
def lu_decomposition(A):
m, n = A.shape
LU = np.copy(A)
pivots = np.empty(n, dtype=int)
# initialise the pivot row and column
h = 0
k = 0
while h < m and k < n:
# Find the k-th pivot:
pivots[k] = np.argmax(LU[h:, k]) + h
if LU[pivots[k], k] == 0:
# No pivot in this column, pass to next column
k = k+1
else:
# swap rows
LU[[h, pivots[k]], :] = LU[[pivots[k], h], :]
# Do for all rows below pivot:
for i in range(h+1, m):
f = LU[i, k] / LU[h, k]
# Store f as the new L column values
LU[i, k] = f
# Do for all remaining elements in current row:
for j in range(k + 1, n):
LU[i, j] = LU[i, j] - LU[h, j] * f
# Increase pivot row and column
h = h + 1
k = k + 1
return LU, pivots
def random_matrix(n):
R = np.random.rand(n, n)
A = np.zeros((n, n))
triu = np.triu_indices(n)
A[triu] = R[triu]
return A
def random_non_singular_matrix(n):
A = np.random.rand(n, n)
while np.linalg.cond(A) > 1/sys.float_info.epsilon:
A = np.random.rand(n, n)
return A
As = [
np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]),
random_non_singular_matrix(3),
random_non_singular_matrix(4),
random_non_singular_matrix(5),
random_non_singular_matrix(6),
]
def pivots_to_row_indices(pivots):
n = len(pivots)
indices = np.array(range(0, n))
for i, p in enumerate(pivots):
indices[i], indices[p] = indices[p], indices[i]
return indices
def calculate_L_mult_U(LU):
L = np.tril(LU)
np.fill_diagonal(L, 1)
U = np.triu(LU)
return L @ U
for A in As:
LU_scipy, pivots_scipy = scipy.linalg.lu_factor(A)
row_indices_scipy = pivots_to_row_indices(pivots_scipy)
LU_mine, pivots_mine = lu_decomposition(A)
row_indices_mine = pivots_to_row_indices(pivots_mine)
np.testing.assert_almost_equal(calculate_L_mult_U(LU_scipy), A[row_indices_scipy])
np.testing.assert_almost_equal(calculate_L_mult_U(LU_mine), A[row_indices_mine])