Last time¶
Projections
Rotations
Reflections
Today¶
Revisit projections, rotations, and reflections
Constructing orthogonal bases
QR factorization
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
endvander (generic function with 2 methods)Polynomials can be orthogonal too!¶
x = LinRange(-1, 1, 50)
A = vander(x, 4)
M = A * [.5 0 0 0; # 0.5
0 1 0 0; # x
0 0 1 0]' # x^2
scatter(x, M)
plot!(x, 0*x, label=:none, color=:black)Which inner product will be zero?
Which functions are even and odd?
Pairwise inner product¶
The pairwise inner products between two sets of vectors can be expressed by collecting the sets as columns in matrices and writing where . It follows from this definition that
M' * M3×3 Matrix{Float64}:
12.5 -1.11022e-15 8.67347
-1.11022e-15 17.3469 -2.22045e-16
8.67347 -2.22045e-16 10.8272Normalization and orthogonalization¶
q1 = M[:,1]
q1 /= norm(q1) # normalize q1
Q = [q1 M[:, 2:end]]
scatter(x, Q)Q' * Q3×3 Matrix{Float64}:
1.0 -1.94289e-16 2.45323
-1.94289e-16 17.3469 -2.22045e-16
2.45323 -2.22045e-16 10.8272Qn = Q * diagm(1 ./ sqrt.(diag(Q' * Q)))
Qn' * Qn3×3 Matrix{Float64}:
1.0 2.0036e-17 0.745555
2.0036e-17 1.0 -4.87753e-17
0.745555 -4.87753e-17 1.0Orthogonal matrices¶
If then we say and are orthogonal (or “ is orthogonal to ”). A vector is said to be normalized if . If is orthogonal to and then we say and are orthonormal. A square matrix with orthonormal columns is said to be an orthogonal matrix. We typically use or and for matrices that are known/constructed to be orthogonal. Orthogonal matrices are always full rank -- the columns are linearly independent. The inverse of a orthogonal matrix is its transpose:
Orthogonal matrices are a powerful building block for robust numerical algorithms.
Gram-Schmidt orthogonalization¶
Suppose we’re given some vectors and want to find an orthogonal basis for their span.
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
endgram_schmidt_naive (generic function with 1 method)A = vander(x, 4)
Q, R = gram_schmidt_naive(A)
@show norm(Q' * Q - I)
@show norm(Q * R - A);norm(Q' * Q - I) = 6.609184312080184e-16
norm(Q * R - A) = 2.500206551829073e-16
What do orthogonal polynomials look like?¶
x = LinRange(-1, 1, 200)
A = vander(x, 6)
Q, R = gram_schmidt_naive(A)
plot(x, Q, marker=:auto, legend=:none)What happens if we use more than 50 values of ? Is there a continuous limit?
Theorem¶
Every full-rank matrix () has a unique reduced factorization with ¶
The algorithm we’re using generates this matrix due to the line:
R[j,j] = norm(v)Solving equations using ¶
If then .
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)
Q, R = gram_schmidt_naive(vander(x1, 4)) # What if we use 3 instead?
p = R \ (Q' * y1)
plot!(x, vander(x, 4) * p)How accurate is it?¶
m = 30
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) = 0.00043830040698919693
norm(Q * R - A) = 1.25091691161899e-15
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
endgram_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