Skip to main content

14  Pivoted LU decomposition

Lecture 14: Chapter 5: Linear systems of equations

These lecture notes are mainly a record of what we discussed in class. They should not be used as a replacement for attending lectures or reading books. Please let me know if you find any errors or have any questions about the material.

You can use the Hypothesis commenting system available on the right side panel (you can login with your U/Google account). This allows you annotate the notes (private) and leave comments or reply to other people’s comments (public).

I encourage you to download the lecture notes and play around with the Julia code.

Code
using Plots, LaTeXStrings, LinearAlgebra

function forwardSub(L, b)
    n = size(L,1)
    y = zeros(n)
    y[1] = b[1] / L[1, 1]
    for i ∈ 2:n
        s = sum(L[i, j] * y[j] for j ∈ 1:i-1)
        y[i] = (b[i] - s) / L[i, i]
    end
    return y
end

function backwardSub(U, y)
    n = size(U, 1)
    x = zeros(n)
    x[n] = y[n] / U[n, n]
    for i ∈ n-1:-1:1
        s = sum(U[i, j] * x[j] for j ∈ i+1:n)
        x[i] = (y[i] - s) / U[i, i]
    end
    return x
end
backwardSub (generic function with 1 method)

14.1 Previously…..

  • Cramer’s rule requires \(O(n!)\) operations: far too expensive,
  • Gaussian elimination (elementary row operations) requires \(O(n^3)\) operations,
  • If \(L\) is unit lower triangular and \(U\) is upper triangular, then we solve \(LUx = b\) by solving \(Ly = b\) (forwards substitution) and then \(Ux = y\) (backwards substitution): this requires \(O(n^2)\) operations.

14.2 Computing LU decomposition

Algorithm: set \(A^{(1)} := A\) and, for \(k = 1, \dots, n\), let \(u_k\) be the \(k\)-th row of \(A^{(k)}\), \(\ell_k\) the \(k\)-th column of \(A^{(k)}\) divided by \((u_k)_k\), and \[ \begin{align} A^{(k+1)} := A^{(k)} - \ell_k u_k. \end{align} \] Then \(A = LU\) where \(L\) has columns \(\ell_k\) and \(U\) has rows \(u_k\).

Code
function LU_decomp( A )
    n = size(A, 1)      
    L = diagm(ones(n))  
    U = zeros(n, n)

    for j ∈ 1:n-1
        U[j, :] = A[j, :]
        L[:, j] = A[:, j] / U[j, j]
        A = A - L[:, j] * U[j, :]'
    end
    U[n, n] = A[n, n]

    return tril(L), triu(U)
end

A = [ 2 0 4 3 ; -4 5 -7 -10 ; 1 15 2 -9/2; -2 0 2 -13]
(L, U) = LU_decomp(A)

display( L )
display( U ) 
4×4 Matrix{Float64}:
  1.0  0.0   0.0  0.0
 -2.0  1.0   0.0  0.0
  0.5  3.0   1.0  0.0
 -1.0  0.0  -2.0  1.0
4×4 Matrix{Float64}:
 2.0  0.0   4.0   3.0
 0.0  5.0   1.0  -4.0
 0.0  0.0  -3.0   6.0
 0.0  0.0   0.0   2.0

Exercise 14.1 (Warm-up) Use LU decomposition combined with forward and backwards substitution to solve the linear system of equations

\[ \begin{align} \begin{pmatrix} 2 & 3 & 4 \\ 4 & 5 & 10 \\ 4 & 8 & 2 \end{pmatrix} \begin{pmatrix} x\\y\\z \end{pmatrix} = \begin{pmatrix} 1 \\ 0 \\ -1 \end{pmatrix}. \nonumber \end{align} \]

Code
A = [2 3 4 ; 4 5 10 ; 4 8 2]
b = [1; 0 ; -1]
(L, U) = LU_decomp(A) 

y = forwardSub(L, b)
x = backwardSub(U, y)
3-element Vector{Float64}:
 -20.0
   9.0
   3.5

We can use Julia’s A \ b which is described as follows:

“Matrix division using a polyalgorithm. For input matrices A and B, the result X is such that AX = B when A is square. The solver that is used depends upon the structure of A. If A is upper or lower triangular (or diagonal), no factorization of A is required and the system is solved with either forward or backward substitution. For non-triangular square matrices, an LU factorization is used.”

Code
A \ b 
3-element Vector{Float64}:
 -20.00000000000002
   9.000000000000009
   3.5000000000000036

Exercise 14.2

Compute the \(LU\) decomposition of \[ \begin{align*} A = \begin{pmatrix} 1 & 2 & -3 \\ 0 & 1 & -1 \\ 1 & 1 & 0 \end{pmatrix} \end{align*} \] by hand, and check your answer with LU_decomp.

Code
A = [ 1 2 -3 ; 0 1 -1 ; 1 1 0 ]
(L, U) = LU_decomp(A)
U
3×3 Matrix{Float64}:
 1.0  2.0  -3.0
 0.0  1.0  -1.0
 0.0  0.0   2.0

Exercise 14.3 What happens when you apply LU decomposition to the following matrix?

\[ \begin{align} A = \begin{pmatrix} 2 & 0 & 4 & 3 \\ -2 & 0 & 2 & -13 \\ 1 & 15 & 2 & -4.5 \\ -4 & 5 & -7 & -10 \end{pmatrix}. \nonumber \end{align} \]

Let’s see what happens when we apply this algorithm to \[ \begin{align} A = \begin{pmatrix} 2 & 0 & 4 & 3 \\ -2 & 0 & 2 & -13 \\ 1 & 15 & 2 & -4.5 \\ -4 & 5 & -7 & -10 \end{pmatrix} \nonumber \end{align} \]

Code
A = [2 0 4 3 ; -2 0 2 -13 ; 1 15 2 -4.5 ; -4 5 -7 -10];
L, U = LU_decomp(A)
L
4×4 Matrix{Float64}:
  1.0    0.0    0.0  0.0
 -1.0  NaN      0.0  0.0
  0.5   Inf   NaN    0.0
 -2.0   Inf   NaN    1.0

After the first step, the \((2,2)\) entry is zero, so LU_decomp divides by zero.

However, the built-in version works perfectly:

Code
lu( A )
LU{Float64, Matrix{Float64}, Vector{Int64}}
L factor:
4×4 Matrix{Float64}:
  1.0    0.0       0.0        0.0
 -0.25   1.0       0.0        0.0
  0.5   -0.153846  1.0        0.0
 -0.5    0.153846  0.0833333  1.0
U factor:
4×4 Matrix{Float64}:
 -4.0   5.0   -7.0      -10.0
  0.0  16.25   0.25      -7.0
  0.0   0.0    5.53846   -9.07692
  0.0   0.0    0.0       -0.166667

The built-in function uses pivotting:

14.3 Pivotting

Before, we chose \(u_k\) to be the \(k\)-th row of \(A^{(k)}\). Now we instead choose the \(p(k)\) row so that we maximise the absolute value of \([A^{(k)}]_{p(k),k}\). Let’s consider an example:

Example 14.1 Use LU decomposition to solve \(Ax = b\) where \(b = (0, 1, -1)^\intercal\) and

\[ \begin{align}\nonumber A = \begin{pmatrix} 1 & 0 & 2 \\ 0 & 2 & 1 \\ 2 & 0 & 3 \end{pmatrix}. \end{align} \]

First, we choose \(p(1)\) so that \(|A_{p(1),1}|\) is maximised: that is, \(p(1) = 3\). Then, we let \(u_1\) be the \(p(1)\) row of \(A\): that is, \(u_1 = (2, 0, 3)\) and \(\ell_1\) be the first column of \(A\) divided by \((u_1)_1=2\): that is, \(\ell_1 = ( 1/2, 0, 1 )^\intercal\). At this stage we have

\[ \begin{align}\nonumber L = \begin{pmatrix} 1/2 & \bullet & \bullet \\ 0 & \bullet & \bullet \\ 1 & \bullet & \bullet \end{pmatrix}, \qquad U = \begin{pmatrix} 2 & 0 & 3 \\ \bullet & \bullet & \bullet \\ \bullet & \bullet & \bullet \end{pmatrix}, \qquad p(1) = 3. \end{align} \]

Notice that \(L\) is no longer going to be lower triangular, but we will fix that at the end (this is why we are tracking the permutation \(p\)).

Then we consider the matrix \(A^{(2)} := A - \ell_1 u_1\) and repeat the same procedure:

\[ \begin{align}\nonumber A^{(2)} = \begin{pmatrix} 1 & 0 & 2 \\ 0 & 2 & 1 \\ 2 & 0 & 3 \end{pmatrix} - \begin{pmatrix} 1 & 0 & 3/2 \\ 0 & 0 & 0 \\ 2 & 0 & 3 \end{pmatrix} % = \begin{pmatrix} 0 & 0 & 1/2 \\ 0 & 2 & 1 \\ 0 & 0 & 0 \end{pmatrix}. \end{align} \]

We choose \(p(2)\) to maximise \(|A^{(2)}_{p(2),2}|\) and so we set \(p(2) = 2\) and \(u_2 = (0, 2, 1)\). Then we set \(\ell_2\) to be the second column of \(A^{(2)}\) divided by \((u_2)_2 = 2\) (that is, \(\ell_2 = (0, 1, 0)^\intercal\)) and we have

\[ \begin{align}\nonumber L = \begin{pmatrix} 1/2 & 0 & \bullet \\ 0 & 1 & \bullet \\ 1 & 0 & \bullet \end{pmatrix}, \qquad U = \begin{pmatrix} 2 & 0 & 3 \\ 0 & 2 & 1 \\ \bullet & \bullet & \bullet \end{pmatrix}, \qquad p(1) = 3, p(2) = 2. \end{align} \]

Then we consider the matrix \(A^{(3)} := A^{(2)} - \ell_2 u_2\) and repeat the same procedure:

\[ \begin{align}\nonumber A^{(3)} = \begin{pmatrix} 0 & 0 & 1/2 \\ 0 & 2 & 1 \\ 0 & 0 & 0 \end{pmatrix} - \begin{pmatrix} 0 & 0 & 0 \\ 0 & 2 & 1 \\ 0 & 0 & 0 \end{pmatrix} % = \begin{pmatrix} 0 & 0 & 1/2 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{pmatrix} \end{align} \]

and conclude

\[ \begin{align}\nonumber L = \begin{pmatrix} 1/2 & 0 & 1 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \end{pmatrix}, \quad U = \begin{pmatrix} 2 & 0 & 3 \\ 0 & 2 & 1 \\ 0 & 0 & 1/2 \end{pmatrix}, \quad p(1) = 3, p(2) = 2, p(3)=1. \end{align} \]

Now, we want to solve \(A x = b\). Notice that

\[ \begin{align}\nonumber PL = \big( L_{p(i),j} \big) = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 1/2 & 0 & 1 \end{pmatrix} \end{align} \]

is lower triangular (where \(P\) is the permutation matrix with a \(1\) in the \((i,p(i))\) entry). Since \(A = LU\), we have \(PA = (PL)U\) and so \(Ax = b\) is equivalent to

\[ \begin{align}\nonumber (PL) U x = Pb \end{align} \]

which we may solve using forwards and backwards substitution as before.

Exercise 14.4 Use the PLU decomposition as above to solve

\[ \begin{align}\nonumber \begin{pmatrix} 1 & 0 & 2 \\ 0 & 2 & 1 \\ 2 & 0 & 3 \end{pmatrix} \begin{pmatrix} x\\y\\z\end{pmatrix} = \begin{pmatrix}0\\1\\-1\end{pmatrix}. \end{align} \]

Code
A = [ 2 0 4 3 ; -2 0 2 -13 ; 1 15 2 -9/2 ; -4 5 -7 -10 ]

display( lu(A) )

# other examples (we went through some of these)
B = [ 1 2 -3 ; 0 1 -1 ; 1 1 0 ]
C = [0 6 -10 ; 15 0 -6 ; 5 1 -4 ]
D = [ 4 0 8 6 ; -8 10 -14 -20 ; 2 30 4 -9 ; -4 0 4 -26 ];
E = [2 3 4 ; 4 5 10 ; 4 8 2 ]
E \ [6 ; 12 ;7] ;
F = [-2 1 0 0 ; 1 -2 1 0 ; 0 1 -2 1 ; 0 0 1 -2]
lu(F);
LU{Float64, Matrix{Float64}, Vector{Int64}}
L factor:
4×4 Matrix{Float64}:
  1.0    0.0       0.0        0.0
 -0.25   1.0       0.0        0.0
  0.5   -0.153846  1.0        0.0
 -0.5    0.153846  0.0833333  1.0
U factor:
4×4 Matrix{Float64}:
 -4.0   5.0   -7.0      -10.0
  0.0  16.25   0.25      -7.0
  0.0   0.0    5.53846   -9.07692
  0.0   0.0    0.0       -0.166667

14.4 Why use the largest pivot?

Answer: We get better conditioning by choosing the largest pivot. Later, we introduce the condition number of a matrix, written \(\kappa(A)\) (Definition 14.2), which will make the following discussion rigorous.

Suppose we have the matrix

\[ \begin{align}\nonumber M(\epsilon )= \begin{pmatrix} -\epsilon & 1 \\ 1 & -1 \end{pmatrix} \end{align} \]

with \(|\epsilon| \ll 1\).

  1. It turns out that this matrix is well conditioned for all small \(\epsilon\). Here, we can see the condition number is bounded for all small \(\epsilon\):
Code
M(ϵ) = [-ϵ 1; 1 -1]
plot( ϵ->cond(M(ϵ)), -1/2, 1/2, title="Condition number of M", xlabel="ϵ")
  1. If we did not use pivoting, the LU decomposition of \(M(\epsilon)\) is

\[ \begin{align}\nonumber \begin{pmatrix} 1 & 0 \\ -\epsilon^{-1} & 1 \end{pmatrix} \begin{pmatrix} -\epsilon & 1 \\ 0 & \epsilon^{-1}-1 \end{pmatrix}. \end{align} \]

Both of these matrices are ill-conditioned. In the following, we can see that the condition numbers explode near \(\epsilon \approx 0\).

Code
plot( ϵ->cond([1 0 ; -1/ϵ 1 ]), 0, 1/2, yaxis = :log10, title="Condition numbers", xlabel="ϵ", label="κ(L)")
plot!( ϵ->cond([-ϵ 1; 0 -1+1/ϵ]), 0, 1/2, label="κ(U)")

Notice this is not a problem with \(M(\epsilon)\), but with our choice of LU decomposition. That is, without pivoting, our algorithm is unstable.

  1. Using PLU decomposition, we arrive at

\[ \begin{align}\nonumber LU = \begin{pmatrix} -\epsilon & 1 \\ 1 & 0 \end{pmatrix} \begin{pmatrix} 1 & -1 \\ 0 & 1-\epsilon \end{pmatrix} \end{align} \]

(with \(p(1) = 2\) and \(p(2) = 1\)). These factors are again well conditioned:

Code
plot( ϵ->cond([-ϵ 1 ; 1 0 ]), -1/2, 1/2, title="Condition numbers", xlabel="ϵ", label="κ(L)")
plot!( ϵ->cond([1 -1; 0 1-ϵ]), -1/2, 1/2, label="κ(U)")

14.5 Condition numbers of linear systems of equations

In order to fully understand the previous section, we need to make sense of the condition number \(\kappa(A)\). Recall (Definition 3.1) that when we were considering functions \(f : \mathbb R \to \mathbb R\), the condition number was (approximately) the constant of proportionality between the relative error in the input to the relative error of the output. That is, if \(x \approx \tilde{x}\) then, we have

\[ \begin{align}\nonumber \frac{|f(x)- f(\tilde{x})|}{|f(x)|} \approx \kappa_f(x) \frac{|x- \tilde{x}|}{|x|}. \end{align} \]

We now take this idea but the “input” is the vector \(b\) and the “output” is \(x\) solving the linear system \(A x = b\). Again, the condition number measures how sensitive the solution \(x\) is to errors in the data \(b\). Roughly speaking, if the condition number is \(10^\rho\), we expect to lose \(\rho\) digits of accuracy when we go from \(b\) to \(x\). This made perfect sense on \(\mathbb R\) (with the absolute value) but now we have many choices for the norm. We shall fix the Euclidean norm for the remainder of the course:

\[ \begin{align}\nonumber |x| := \sqrt{\sum_{j=1}^n |x_j|^2}. \end{align} \]

We also need the corresponding operator norm on the space of matrices:

Definition 14.1 (Operator norm) \[ \begin{align}\nonumber \|A\| := \max_{x\not=0} \frac{|Ax|}{|x|}. \end{align} \]

Notice that, we have \(|Ax| \leq \|A\| |x|\) for all \(x\). We are now ready to define the condition number of the matrix \(A\) (or the corresponding linear system).

Suppose we have the exact data \(b\) and exact solution \(x\) (solving \(Ax=b\)) and we have the approximate data \(\tilde{b}\) (for example, the floating point representation of \(b\)) and corresponding solution \(\tilde{x}\) solving \(A\tilde{x} = \tilde{b}\). First, notice that \(|b| = |Ax| \leq \|A\| |x|\) and that \(A(x - \tilde{x}) = b - \tilde{b}\) and so

\[ \begin{align}\nonumber |x - \tilde{x}| \leq \|A^{-1}\| |b - \tilde{b}|. \end{align} \]

Therefore, we have

\[ \begin{align} \frac {|x-\tilde{x}|/|x|} {|b-\tilde{b}|/|b|} % = \frac {|x-\tilde{x}|} {|b-\tilde{b}|} \frac {|b|} {|x|} % \leq \|A^{-1}\| \|A\|. \end{align} \tag{14.1}\]

Definition 14.2 (Condition number of a matrix) The condition number of an invertible matrix \(A\) is the right hand side of (14.1): \[ \begin{align}\nonumber \kappa(A) := \|A^{-1}\| \|A\|. \end{align} \]

Exercise 14.5  

  1. Use the definitions directly to show that the following matrices are ill-conditioned for small \(\epsilon > 0\), \[ \begin{align}\nonumber \begin{pmatrix} 1 & 0 \\ -\epsilon^{-1} & 1 \end{pmatrix}, \qquad \begin{pmatrix} -\epsilon & 1 \\ 0 & \epsilon^{-1}-1 \end{pmatrix}, \end{align} \]
  2. Show that \(\|A\| = \max_{|x|=1} |Ax|\),
  3. Show that \(\|AB\| \leq \|A\|\|B\|\) and hence \(\|A^k\| \leq \|A\|^k\),
  4. What is \(\|D\|\) for diagonal \(D\)?
  5. A real matrix \(A\) is symmetric (i.e. \(A^T = A\)) if and only if there exists orthogonal \(P\) (i.e. \(P^{-1} = P^T\)) and diagonal \(\Lambda\) such that \(A = P\Lambda P^T\). Show that the columns of \(P\) are eigenvectors of \(A\),
  6. Show that, for symmetric matrices \(A\), the operator norm \(\|A\|\) is equal to \(\max_i |\lambda_i|\) where \((\lambda_i)\) are the eigenvalues of \(A\).

14.6 Next time….

  • Linear least squares: overdetermined systems \(Ax = b\) with \(A \in \mathbb R^{m\times n}\), \(m > n\),
  • The normal equations and fitting data,
  • QR decomposition.
TipTO-DO
  • Exercises throughout the notes,
  • (Sign-up to office hours if you like)

14.7 Extra exercises

Exercise 14.6 Compute (by hand) the PLU decomposition of \[ \begin{align}\nonumber C = \begin{pmatrix} 0 & 6 & -10 \\ 15 & 0 & -6 \\ 5 & 1 & -4 \end{pmatrix} \end{align} \] using the largest pivot at each step, and check your answer with lu(C). Use your answer to compute \(\det C\).

Exercise 14.7 Let \(A\) be invertible.

  1. Show that \(\kappa(A) \geq 1\) and that \(\kappa(\alpha A) = \kappa(A)\) for all \(\alpha \not= 0\),
  2. Show that, if \(A\) is symmetric with eigenvalues \((\lambda_i)\), then \(\kappa(A) = \max_i |\lambda_i| / \min_i |\lambda_i|\),
  3. Compute \(\kappa(A)\) for \(A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}\) and check your answer with cond(A).
Timer
00:00
min