Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

2026-09-21 QR Factorization

Last time

  • Revisit projections, rotations, and reflections

  • Constructing orthogonal bases

Today

  • Gram-Schmidt process

  • QR factorization

  • Stability and ill conditioning

  • Intro to performance modeling

using LinearAlgebra
using Plots
using Polynomials
default(linewidth=4, legendfontsize=12)

function vander(x, k=nothing)
    if isnothing(k)
        k = length(x)
    end
    m = length(x)
    V = ones(m, k)
    for j in 2:k
        V[:, j] = V[:, j-1] .* x
    end
    V
end
vander (generic function with 2 methods)

Gram-Schmidt orthogonalization

Suppose we’re given some vectors and want to find an orthogonal basis for their span.

[a1∣a2]=[q1∣q2][r11r120r22]\Bigg[ a_1 \Bigg| a_2 \Bigg] = \Bigg[ q_1 \Bigg| q_2 \Bigg] \begin{bmatrix} r_{11} & r_{12} \\ 0 & r_{22} \end{bmatrix}

A naive algorithm

function gram_schmidt_naive(A)
    m, n = size(A)
    Q = zeros(m, n)
    R = zeros(n, n)
    for j in 1:n
        v = A[:,j]
        for k in 1:j-1
            r = Q[:,k]' * v
            v -= Q[:,k] * r
            R[k,j] = r
        end
        R[j,j] = norm(v)
        Q[:,j] = v / R[j,j]
    end
    Q, R
end
gram_schmidt_naive (generic function with 1 method)
x = LinRange(-1, 1, 100)
A = vander(x, 10)
Q, R = gram_schmidt_naive(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A);
norm(Q' * Q - I) = 5.83022680287482e-14
norm(Q * R - A) = 6.707714767881077e-16

What do orthogonal polynomials look like?

x = LinRange(-1, 1, 100)
A = vander(x, 5)#[:, end:-1:1]
Q, R = gram_schmidt_naive(A)
plot(x, Q, legend=:none)
Loading...

What happens if we use more than 50 values of xx? Is there a continuous limit?

Theorem

Every full-rank m×nm\times n matrix (m≥nm \ge n) has a unique reduced QRQ R factorization with Rj,j>0R_{j,j} > 0

The algorithm we’re using generates this matrix due to the line:

        R[j,j] = norm(v)

Solving equations using QR=AQR = A

If Ax=bA x = b then Rx=QTbRx = Q^T b.

x1 = [-0.9, 0.1, 0.5, 0.8] # points where we know values
y1 = [1, 2.4, -0.2, 1.3]
scatter(x1, y1)
A = vander(x1, 3)
Q, R = gram_schmidt_naive(A)
p = R \ (Q' * y1)
#p = A \ y1
plot!(x, vander(x, 3) * p)
Loading...

How accurate is it?

m = 20 # What happens as it grows?
x = LinRange(-1, 1, m)
A = vander(x, m)
Q, R = gram_schmidt_naive(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A)
norm(Q' * Q - I) = 1.073721107832196e-8
norm(Q * R - A) = 8.268821431611631e-16
8.268821431611631e-16

A variant with more parallelism

(I−q2q2T)(I−q1q1T)v=(I−q1q1T−q2q2T+q2q2Tq1q1T)v=(I−[q1∣q2][q1Tq2T])v\begin{align} (I - q_2 q_2^T) (I - q_1 q_1^T) v &= (I - q_1 q_1^T - q_2 q_2^T + q_2 q_2^T q_1 q_1^T) v \\ &= \Bigg( I - \Big[ q_1 \Big| q_2 \Big] \begin{bmatrix} q_1^T \\ q_2^T \end{bmatrix} \Bigg) v \end{align}
function gram_schmidt_classical(A)
    m, n = size(A)
    Q = zeros(m, n)
    R = zeros(n, n)
    for j in 1:n
        v = A[:,j]
        R[1:j-1,j] = Q[:,1:j-1]' * v
        v -= Q[:,1:j-1] * R[1:j-1,j]
        R[j,j] = norm(v)
        Q[:,j] = v / norm(v)
    end
    Q, R
end
gram_schmidt_classical (generic function with 1 method)
m = 20
x = LinRange(-1, 1, m)
A = vander(x, m)
Q, R = gram_schmidt_classical(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A)
norm(Q' * Q - I) = 1.4985231287367549
norm(Q * R - A) = 7.350692433565389e-16
7.350692433565389e-16

Cost of Gram-Schmidt?

  • We’ll count flops (addition, multiplication, division*)

  • Inner product ∑i=1mxiyi\sum_{i=1}^m x_i y_i?

  • Vector “axpy”: yi=axi+yiy_i = a x_i + y_i, i∈[1,2,…,m]i \in [1, 2, \dotsc, m].

  • Look at the inner loop:

for k in 1:j-1
    r = Q[:,k]' * v
    v -= Q[:,k] * r
    R[k,j] = r
end

Counting flops is a bad model

  • We load a single entry (8 bytes) and do 2 flops (add + multiply). That’s an arithmetic intensity of 0.25 flops/byte.

  • Current hardware can do about 10 flops per byte, so our best algorithms will run at about 2% efficiency.

  • Need to focus on memory bandwidth, not flops.

Dense matrix-matrix mulitply