16 QR factorisations
Lecture 16: 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.
16.1 Previously…..
- Linear least squares: for \(A \in \mathbb R^{m\times n}\) with \(m > n\), choose \(x\) to minimise \(|Ax - b|^2\) (e.g. data fitting); Julia’s
A \ bdoes this, - If \(x\) satisfies the normal equations \(A^\top A x = A^\top b\), then \(x\) minimises \(|Ax-b|^2\),
- \(A\) has linearly independent columns if and only if \(A^\top A\) is invertible,
- If \(A\) has linearly independent columns, then there is a unique solution to the normal equations \(A^\top A x = A^\top b\) (given by \(x = (A^\top A)^{-1} A^\top b\)),
- For \(A \in \mathbb R^{m\times n}\), we call \(A^+ := (A^\top A)^{-1} A^\top \in \mathbb R^{n\times m}\) the pseudoinverse, so that \(A^+ b\) solves the linear least squares problem \(\min |Ax-b|^2\),
- Condition number: \(\kappa(A^\top A) = \kappa(A)^2\) so that solving \(\min|Ax - b|\) by the normal equations is ill conditioned unless \(\kappa(A)\) is relatively small,
- Instead, we use the QR factorisation: \(A = QR\) with \(Q^\top Q = I\) and \(R\) upper triangular,
- If \(A\) has linearly independent columns, then the normal equations are equivalent to \(Rx = Q^\top b\) (which can be solved by backwards substitution).
16.2 Computing a QR factorisation
Fix \(A\in \mathbb R^{m\times n}\). How to find \(Q\) orthogonal (that is, \(Q^\top Q = I\)) and \(R\) upper triangular (\(R_{ij} = 0\) for all \(i > j\))?
Exercise 16.1 Let \(A = \begin{pmatrix} a_1 | \cdots | a_n\end{pmatrix} \in \mathbb R^{m\times n}\), \(Q = \begin{pmatrix} q_1 | \cdots | q_n\end{pmatrix} \in \mathbb R^{m\times n}\) and let \(R \in \mathbb R^{n\times n}\) be upper triangular. Show that \(A = QR\) if and only if \[ \begin{align} a_j = \sum_{k=1}^j R_{kj} q_{k} \end{align} \tag{16.1}\] for all \(j = 1,...,n\).
In particular, \(a_j \in \mathrm{span}\{q_1, \dots, q_j\}\) (and, if \(R_{11}, \dots, R_{jj} \not= 0\), then \(q_1, \dots, q_j\) span the same space as \(a_1, \dots, a_j\)).
Exercise 16.2 Let \(Q = \begin{pmatrix} q_1 | \cdots | q_n\end{pmatrix} \in \mathbb R^{m\times n}\). Show that \((Q^\top Q)_{ij} = q_i \cdot q_j\).
Therefore, \(Q^\top Q = I\) means exactly that \(q_i \cdot q_j = \delta_{ij}\): the columns of \(Q\) are orthonormal.
Exercise 16.3 Suppose that \(A = QR\) where \(Q \in \mathbb R^{m\times n}\) has orthonormal columns and \(R \in \mathbb R^{n\times n}\) is upper triangular. Show that \[ \begin{align} R_{ij} = q_i \cdot a_j \end{align} \tag{16.2}\] for all \(i\) and \(j\).
One way to obtain \(Q\) and \(R\) is the Gram–Schmidt process: take \(a_j\), subtract its components along \(q_1, \dots, q_{j-1}\) (which give \(R_{ij} = q_i \cdot a_j\)) and normalise what is left.
First we take \(R_{11} := |a_1|\) and \(q_1 = \frac{a_1}{|a_1|}\) (so that \(q_1\) is normalised): \[ \begin{align} \sum_{k=1}^1 R_{k1} q_{k} &= R_{11} q_{1} % %\nonumber\\& % = |a_1| \frac{a_1}{|a_1|} = a_1, \end{align} \] as required.
Next, by (16.2), we can define \(R_{12} := q_1 \cdot a_2\) and, since we require \(a_2 = R_{12}q_1 + R_{22}q_2\) (by (16.1)), we define \[ \begin{align} R_{22}q_2 % &= a_2 - R_{12} q_1 % \nonumber\\& % = a_2 - (q_1 \cdot a_2) q_1. \nonumber \end{align} \] That is, we remove from \(a_2\) the component of \(a_2\) in the direction \(q_1\) (see Figure 16.1). Since \(q_2\) is normalised, we can set \(R_{22} := |a_2 - (q_1 \cdot a_2) q_1|\) and \(q_2 := \big( a_2 - (q_1 \cdot a_2) q_1 \big)/R_{22}\).
Similarly, again by (16.2), we define \(R_{ij} := q_i \cdot a_j\) for \(i = 1, \dots, j-1\) and, since we require \(a_j = \sum_{k=1}^{j} R_{kj} q_k\), we define \[ \begin{align} R_{jj} q_j % &= a_j - \sum_{k=1}^{j-1} R_{kj} q_k % \nonumber\\& % = a_j - \sum_{k=1}^{j-1} (q_k \cdot a_j) q_k =: v_j. \nonumber \end{align} \] That is, we remove from \(a_j\) its components in the directions \(q_1, \dots, q_{j-1}\). Since \(q_j\) is normalised, we set \(R_{jj} := |v_j|\) and \(q_j := v_j / R_{jj}\).
Repeating this for \(j = 1, 2, \dots, n\) gives \(Q = \begin{pmatrix} q_1 | \cdots | q_n \end{pmatrix} \in \mathbb R^{m\times n}\) with \(Q^\top Q = I\) and \(R \in \mathbb R^{n \times n}\) upper triangular: this is the reduced (or thin) QR factorisation, and it is all we need to solve \(Rx = Q^\top b\).
(To obtain a square orthogonal \(Q \in \mathbb R^{m\times m}\), extend \(q_1, \dots, q_n\) to an orthonormal basis \(q_1, \dots, q_m\) of \(\mathbb R^m\) and add \(m - n\) rows of zeros to the bottom of \(R\).)
Theorem 16.1 Suppose that \(A \in \mathbb R^{m\times n}\) has linearly independent columns. Then, the Gram–Schmidt process produces \(Q \in \mathbb R^{m\times n}\) with orthonormal columns (\(Q^\top Q = I\)) and \(R\in \mathbb R^{n\times n}\) upper triangular with positive diagonal entries, and \(A = QR\).
Proof (non-examinable)
Proof (non-examinable). \(R\) is upper triangular because we only define the entries \(R_{ij}\) with \(i \leq j\) (all the others are \(0\)). We show by induction on \(j\) that
- \(R_{jj} = |v_j| > 0\) and so \(q_j = v_j / R_{jj}\) is well defined,
- \(q_1, \dots, q_j\) are orthonormal,
- \(a_j = \sum_{k=1}^j R_{kj} q_k\).
Base case \(j = 1\). We have \(v_1 = a_1 \not= 0\) because the columns of \(A\) are linearly independent. So \(R_{11} = |a_1| > 0\), \(|q_1| = 1\), \(a_1 = R_{11} q_1\) and \(\mathrm{span}\{q_1\} = \mathrm{span}\{a_1\}\).
Inductive step. Suppose 1–3 hold for \(1, \dots, j-1\).
- If \(v_j = 0\), then \(a_j = \sum_{k=1}^{j-1} (q_k\cdot a_j) q_k \in \mathrm{span}\{q_1, \dots, q_{j-1}\} = \mathrm{span}\{a_1, \dots, a_{j-1}\}\), which contradicts the linear independence of the columns of \(A\). Therefore \(R_{jj} = |v_j| > 0\).
- \(|q_j| = |v_j|/R_{jj} = 1\) and, for \(i < j\), since \(q_i \cdot q_k = \delta_{ik}\) for \(i, k \leq j-1\), \[ \begin{align} R_{jj}\, q_i \cdot q_j = q_i \cdot v_j % &= q_i \cdot a_j - \sum_{k=1}^{j-1} (q_k \cdot a_j)\, q_i \cdot q_k % \nonumber\\& % = q_i \cdot a_j - q_i \cdot a_j = 0. \nonumber \end{align} \]
- Rearranging the definition of \(v_j\) gives \(a_j = v_j + \sum_{k=1}^{j-1} R_{kj} q_k = \sum_{k=1}^{j} R_{kj} q_k\) (since \(v_j = R_{jj} q_j\)).
Finally, part 2 with \(j = n\) says that \(Q^\top Q = I\) and, since \(R_{kj} = 0\) for \(k > j\), part 3 says that the \(j\)-th column of \(A\) is \(\sum_{k=1}^n R_{kj} q_k\), which is the \(j\)-th column of \(QR\). That is, \(A = QR\).
In the following code, the inner loop computes \(R_{ij} = q_i \cdot a_j\), subtracts \(R_{ij} q_i\) from \(v\), and then we normalise:
Code
function gram_schmidt(A)
m, n = size(A)
Q = zeros(m, n)
R = zeros(n, n)
for j ∈ 1:n
v = A[:, j]
for i ∈ 1:j-1
R[i, j] = Q[:, i]' * A[:, j]
v -= R[i, j] * Q[:, i]
end
R[j, j] = norm(v)
Q[:, j] = v / R[j, j]
end
return Q, R
end
A = [2.0 1 0; 1 3 1; 0 1 4]
Q, R = gram_schmidt(A)
display(Q)
display(R)
println("|QᵀQ - I| = ", norm(Q' * Q - I))
println("|Q R - A| = ", norm(A - Q * R))3×3 Matrix{Float64}:
0.894427 -0.408248 0.182574
0.447214 0.816497 -0.365148
0.0 0.408248 0.912871
3×3 Matrix{Float64}:
2.23607 2.23607 0.447214
0.0 2.44949 2.44949
0.0 0.0 3.28634
|QᵀQ - I| = 3.2247307035915016e-16
|Q R - A| = 2.220446049250313e-16
Julia’s built-in qr agrees (up to signs):
Code
F = qr(A)
display(Matrix(F.Q))
display(F.R)3×3 Matrix{Float64}:
-0.894427 0.408248 0.182574
-0.447214 -0.816497 -0.365148
0.0 -0.408248 0.912871
3×3 Matrix{Float64}:
-2.23607 -2.23607 -0.447214
0.0 -2.44949 -2.44949
0.0 0.0 3.28634
Example 16.1 (Population of the UK, again) Let us return to Example 15.2 from the previous lecture: we fit \(p(t) = c_1 + c_2 t + c_3 t^2\) to the population of the UK (in millions) at the times \(t = 1, 11, 21, \dots, 51\) (years since 1970). That is, we solve the least squares problem \(\min |Vc - y|\) where \(V\) has columns \(1, t, t^2\). Using our gram_schmidt, we compute \(V = QR\) and then solve \(Rc = Q^\top y\) by backwards substitution (Lecture 13), and compare with V \ y:
Code
years = 1971:2024
pop = [
55928000 56096700 56222900 56235600 56225700 56216100 56189900 56178000 56240100 56329700 56357500 56290700 56315700 56409300 56554000 56683800 56804000 56916400 57076500 57237500 57438700 57584500 57713900 57862100 58024800 58164400 58314200 58474900 58684400 58886100 59113000 59365700 59636700 59950400 60413300 60827000 61319100 61823800 62260500 62759500 63285100 63710500 64138200 64618700 65087000 65605800 65964300 66286700 66627500 66739900 66978000 67636100 68526200 69281400
]
r = 1:10:54
y = pop[r] / 1e6
V = r .^ (0:2)'
Q, R = gram_schmidt(V)
c_gs = UpperTriangular(R) \ (Q' * y) # backwards substitution
c_bs = V \ y
println("Gram–Schmidt: c = ", c_gs)
println("V \\ y: c = ", c_bs)
println("difference: ", norm(c_gs - c_bs))
scatter(1:54, pop' / 1e6, label = "testing data", color = "red", xlabel = "years since 1970", ylabel = "/millions", title = "population of UK", legend = :topleft)
scatter!(r, y, label = "training data", color = "blue")
plot!(Polynomial(c_gs), -5, 60, label = "model (Gram–Schmidt)", lw = 3)
plot!(Polynomial(c_bs), -5, 60, label = "model (V ∖ y)", ls = :dash, lw = 2)Gram–Schmidt: c = [56.04589946428572, -0.04429957142856852, 0.005121535714285644]
V \ y: c = [56.04589946428568, -0.04429957142856859, 0.005121535714285657]
difference: 3.5527221162974273e-14
The two answers agree to about 14 digits.
Let’s apply our algorithm to a larger matrix and see what happens:
Example 16.2 Let \(p(t) = 1 + t + t^2 + \cdots + t^{10}\) and consider the least squares problem of finding this (known) polynomial using the data \((t_j, p(t_j))\) where \(t_j\) are \(50\) equally spaced points in \([0, 1]\). Let \(V\) be the Vandermonde matrix with \(V_{jk} := t_j^k\) and \(b := Vc\) where \(c := (1, 1, \dots, 1)^\top\) (the coefficients of \(p\)). We know the solution exactly so we can use this problem as a test for our methods:
Code
t = range(0, 1, length = 50) # equispaced points in [0, 1]
d = 10 # degree of polynomial in p
V = t .^ (0:d)' # 50 × 11 Vandermonde matrix
println("κ(V) = ", cond(V))
println("κ(VᵀV) = ", cond(V' * V))
c = ones(d + 1) # the exact coefficients
b = V * c # stored value of b (= b_exact + floating point error)
c_ne = (V' * V) \ (V' * b) # solving via normal equations VᵀVc = Vᵀb
println("\nnormal eqns: |c - c_exact| = ", norm(c_ne - c))
Q, R = gram_schmidt(V)
c_gs = UpperTriangular(R) \ (Q' * b)
println("\nGram–Schmidt: |V - QR| = ", norm(V - Q * R))
println(" |QᵀQ - I| = ", norm(Q' * Q - I))
println(" |c - c_exact| = ", norm(c_gs - c))
F = qr(V)
Q₂ = Matrix(F.Q)
println("\nJulia QR: |V - QR| = ", norm(V - Q₂ * F.R))
println(" |QᵀQ - I| = ", norm(Q₂' * Q₂ - I))
println(" |c - c_exact| = ", norm(UpperTriangular(F.R) \ (Q₂' * b) - c))κ(V) = 2.035662964244265e7
κ(VᵀV) = 4.1327367350368525e14
normal eqns: |c - c_exact| = 0.023015315500473796
Gram–Schmidt: |V - QR| = 6.808212398882048e-16
|QᵀQ - I| = 1.3004557672155472
|c - c_exact| = 474.2742230550594
Julia QR: |V - QR| = 4.467631842559112e-15
|QᵀQ - I| = 1.5955311331105928e-15
|c - c_exact| = 1.3560894753698679e-9
We can see that the condition number \(\kappa(V) \approx 2 \times 10^7\) is large and so solving the least squares problem via the normal equations directly is a bad idea! Indeed, if we have \(V^\top b\) with error \(10^{-16}\), since \(\approx \kappa(V^\top V) = \kappa(V)^2 \approx 4\times 10^{14}\), we expect to lose \(14\) digits of accuracy when we solve \(V^\top V x = V^\top b\) (which is what we see).
Instead, we implement the Gram–Schmidt algorithm and we see that \(V\) is well approximated by the computed QR factorisation \(V \approx QR\) but \(Q\) is far from being orthogonal and the computed coefficients are very far from being exact!!
The built-in Julia function does much better.
Take a moment to discuss what you think is going on here with the people around you.
In exact arithmetic, Theorem 16.1 says that Gram–Schmidt works whenever the columns of \(A\) are linearly independent. However, in floating point, it can go badly wrong when the columns are nearly linearly dependent. When \(a_j\) is almost in \(\mathrm{span}\{q_1, \dots, q_{j-1}\}\), we have \(v_j = a_j - \sum_{k<j} (q_k \cdot a_j) q_k\) is the difference of two almost equal vectors. The rounding errors (of size \(\approx \epsilon_{\rm mach} |a_j|\)) are then large compared to \(|v_j|\) (subtractive cancellation!). Therefore, after normalising, the computed \(q_j\) is no longer orthogonal to \(q_1, \dots, q_{j-1}\).
In this case the “obvious” way of computing a QR factorisation is far worse than the normal equations: the problem is not with the factorisation \(A = QR\) but the algorithm we used to compute it. To get a \(Q\) which is orthogonal to machine precision, Julia uses Householder reflections instead.
16.3 Householder reflections
Gram–Schmidt orthogonalises the columns of \(A\) one at a time (“triangular orthogonalisation”; we find \(Q = A R^{-1}\)). Householder’s method does the opposite: it applies a sequence of orthogonal matrices (reflections) to \(A\) until it becomes upper triangular (“orthogonal triangularisation”; we find \(R = Q^\top A\)). Since multiplying by an orthogonal matrix does not amplify errors, this is stable.
Exercise 16.4 Let \(Q \in \mathbb R^{n\times n}\) be orthogonal. Show that \(\kappa(Q) = 1\).
Again suppose that \(A = \begin{pmatrix} a_1 | a_2 | \cdots | a_n \end{pmatrix}\).
Idea: Suppose we can find an orthogonal matrix \(P_1\) such that \[ \begin{align} P_1 A &= \begin{pmatrix} P_1 a_1 | P_1 a_2 | \cdots | P_1 a_n \end{pmatrix} % \nonumber\\& % = \left(\begin{array}{c|ccc} \pm|a_1| & \bullet & \cdots & \bullet \\ \hline 0 & & & \\ \vdots & & A^{(2)} & \\ 0 & & & \end{array}\right), \end{align} \] where each \(\bullet\) denotes a number (that we do not need to keep track of) and \(A^{(2)} \in \mathbb R^{(m-1)\times(n-1)}\). Iterating this process, we find an orthogonal \(\hat P_2 \in \mathbb R^{(m-1)\times(m-1)}\) which does the same to \(A^{(2)}\), and let \(P_2 := \begin{pmatrix} 1 & 0 \\ 0 & \hat P_2 \end{pmatrix}\), which does not change the first row or the first column of \(P_1 A\) (Exercise 16.7), and so on. After \(n\) steps, we obtain a matrix \(Q^\top := P_n \cdots P_1\) such that \(R := Q^\top A\) is upper triangular and \(Q^\top\) is orthogonal (as a product of orthogonal matrices). Then \(A = Q R\), as required.
(Here \(Q \in \mathbb R^{m\times m}\) and \(R \in \mathbb R^{m\times n}\), whose last \(m - n\) rows are zero: this is the full QR factorisation. Keeping only the first \(n\) columns of \(Q\) and the first \(n\) rows of \(R\) gives the reduced QR factorisation.)
This is the key idea behind qr() in Julia.
All that remains is to show that such an orthogonal \(P_1\) exists. Figure 16.2 is a picture of this in \(\mathbb R^2\) and we have \(P_1 := I - 2 vv^\top\) with \(|v|=1\).
In general, reflecting across the hyperplane \(v^\perp := \{x \in \mathbb R^m : v \cdot x = 0\}\) perpendicular to a unit vector \(v\) keeps the component of \(x\) in the hyperplane and flips the sign of the component \((v \cdot x)\, v\) along \(v\): \[ \begin{align}\nonumber x &= \big(x - (v \cdot x)\, v\big) + (v \cdot x)\, v \nonumber\\ % &\mapsto \big(x - (v \cdot x)\, v\big) - (v \cdot x)\, v % \nonumber\\& % = (I - 2vv^\top)\, x. \end{align} \]
Definition 16.1 (Householder reflection) Let \(v \in \mathbb R^m\) with \(|v| = 1\). The matrix \(P := I - 2vv^\top \in \mathbb R^{m\times m}\) is called a Householder reflection.
The following exercises show that this does what we want.
Exercise 16.5 Let \(v \in \mathbb R^m\) with \(|v| = 1\) and \(P := I - 2vv^\top\).
- Show that \(P\) is symmetric and orthogonal, and that \(P^2 = I\),
- Show that \(Pv = -v\) and that \(Pw = w\) for all \(w \in v^\perp\) (i.e. with \(v \cdot w = 0\)).
Exercise 16.6 Let \(a, z \in \mathbb R^m\) with \(|a| = |z|\) and \(a \not= z\), and let \(P := I - 2vv^\top\) with \(v := (a - z)/|a - z|\). Show that \(Pa = z\). Deduce that, for every \(a \not= 0\), there is a Householder reflection \(P\) with \(Pa = |a| e_1\) or \(Pa = -|a| e_1\).
Exercise 16.7
- Let \(\hat P \in \mathbb R^{(m-1)\times(m-1)}\) be orthogonal and \(P := \begin{pmatrix} 1 & 0 \\ 0 & \hat P \end{pmatrix}\). Show that \(P\) is orthogonal and that \[ \begin{align} P \begin{pmatrix} \alpha & b^\top \\ 0 & B \end{pmatrix} = \begin{pmatrix} \alpha & b^\top \\ 0 & \hat P B \end{pmatrix} \end{align} \] for all \(\alpha \in \mathbb R\), \(b \in \mathbb R^{n-1}\) and \(B \in \mathbb R^{(m-1)\times(n-1)}\),
- Show that, if \(P_1, \dots, P_n\) are orthogonal, then so is \(P_n \cdots P_1\),
- Deduce that the process described above gives \(Q^\top = P_n \cdots P_1\) orthogonal and \(R = Q^\top A\) upper triangular.
Exercise 16.8 Which sign should you choose in \(z = \pm|a| e_1\)?
16.4 Eigenvalues from QR
The QR factorisation is also the main tool for computing eigenvalues. (The obvious approach of finding the roots of \(\det(\lambda I - A)\) is a bad idea numerically). The basic idea is remarkably simple:
Definition 16.2 (QR algorithm) Let \(A_0 := A\) and, for \(k = 0, 1, 2, \dots\), compute the QR factorisation \(A_k = Q_kR_k\) and set \(A_{k+1} := R_kQ_k\) (multiply in the other order).
Since \(R_k = Q_k^\top A_k\), we have \(A_{k+1} = R_k Q_k = Q_k^\top A_k Q_k\).
Exercise 16.9 Suppose that \(Q\in \mathbb R^{m\times m}\) is orthogonal. Show that \(B = Q^\top A Q\) has the same eigenvalues as \(A\). Show that, if \(A\) is symmetric, then so is \(B\).
By Exercise 16.9, every \(A_k\) has the same eigenvalues as \(A\). Under suitable assumptions (for example, \(A\) symmetric with eigenvalues of distinct absolute values), \(A_k\) converges to a diagonal matrix, whose diagonal entries are the eigenvalues of \(A\).
Example 16.3 For the symmetric matrix \(A = \begin{pmatrix} 2 & 1 & 0 \\ 1 & 3 & 1 \\ 0 & 1 & 4 \end{pmatrix}\) from above (whose eigenvalues are \(3 - \sqrt3\), \(3\) and \(3 + \sqrt3\)), we run the QR algorithm using Julia’s qr:
Code
function qr_iteration(A, N; show = ())
Ak = copy(A)
for k ∈ 1:N
F = qr(Ak)
Ak = F.R * Matrix(F.Q) # Aₖ₊₁ = RₖQₖ
if k ∈ show
display("A_$k = ")
display(round.(Ak, digits = 6))
end
end
return Ak
end
A = [2.0 1 0; 1 3 1; 0 1 4]
Ak = qr_iteration(A, 30, show = (1, 5, 30))
println("diagonal of A₃₀: ", sort(diag(Ak)))
println("eigvals(A): ", eigvals(A))"A_1 = "
3×3 Matrix{Float64}:
3.0 1.09545 -0.0
1.09545 3.0 -1.34164
0.0 -1.34164 3.0
"A_5 = "
3×3 Matrix{Float64}:
4.60621 0.449742 -0.0
0.449742 3.1234 -0.06618
0.0 -0.06618 1.27039
"A_30 = "
3×3 Matrix{Float64}:
4.73205 5.0e-6 0.0
5.0e-6 3.0 0.0
0.0 0.0 1.26795
diagonal of A₃₀: [1.2679491924311233, 3.000000000017212, 4.732050807551667]
eigvals(A): [1.2679491924311228, 3.000000000000001, 4.732050807568877]
We will study this in more detail in Math 5486 next semester.
16.5 Next time….
- Singular value decomposition (SVD).
- Exercises throughout the notes,
- (Sign-up to office hours if you like)
16.6 Extra exercises
Exercise 16.10 Let \(A \in \mathbb R^{m\times n}\) and \(b \in \mathbb R^m\) be such that the columns of \(\begin{pmatrix} A \,|\, b\end{pmatrix} \in \mathbb R^{m\times(n+1)}\) are linearly independent, and suppose that Gram–Schmidt applied to \(\begin{pmatrix} A \,|\, b\end{pmatrix}\) gives \[ \begin{pmatrix} A \,|\, b\end{pmatrix} = \begin{pmatrix} Q \,|\, q_{n+1}\end{pmatrix} \begin{pmatrix} R & z \\ 0 & \rho \end{pmatrix} \] with \(Q \in \mathbb R^{m\times n}\), \(q_{n+1} \in \mathbb R^m\), \(R \in \mathbb R^{n\times n}\), \(z \in \mathbb R^n\) and \(\rho \in \mathbb R\).
- Show that \(A = QR\), \(z = Q^\top b\) and \(b = Qz + \rho q_{n+1}\),
- Deduce that the solution \(x\) of the least squares problem \(\min|Ax - b|\) solves \(Rx = z\), and that \(|Ax - b| = \rho\),
- Modified Gram–Schmidt is
gram_schmidtwithR[i, j] = Q[:, i]' * vin place ofR[i, j] = Q[:, i]' * A[:, j](that is, we remove the component along \(q_i\) from the updated vector \(v\) rather than from \(a_j\)). Write a functionmodified_gram_schmidt, apply it to[V b]with \(V\) and \(b\) as in Example 16.2 (\(d = 10\)) and solve \(Rx = z\) by backwards substitution. Compare the error \(|x - c_{\rm exact}|\) with the methods above. What do you notice?