mjtsai1974's Dev Blog Welcome to mjt's AI world

QR Decomposition

QR Decomposition

QR decomposition claims that A = B⋅X = B⋅D⋅E⋅X = Q⋅R. This article will show you the way to prove it, we will begin from Gram-Schmit Procedure, then, briefly introduce to the projection matrix, then, migrate to triangular matrix, finally to prove the QR decomposition.

Gram-Schmit Procedure

Given a set of linear independent vectors set S = {v1, v2,…, vp} ∈ Rm,
define vectors ui, 1 ≤ i ≤ p by
ui = vi − [((vi)t ⋅ u1) ∕ ((u1)t ⋅ u1)] ⋅ u1 − [((vi)t ⋅ u2) ∕ ((u2)t ⋅ u2)] ⋅ u2 − [((vi)t ⋅ u3) ∕ ((u3)t ⋅ u3)] ⋅ u3 − … − [((vi)t ⋅ ui−1) ∕ ((ui−1)t ⋅ ui−1)] ⋅ ui−1
the set T = {u1, u2,…, up} is a linear independent orthonormal set and aoan(S) = span(T)
we just have below holds:
u1 = v1
u2 = v2 − [((v2)t ⋅ u1) ∕ ((u1)t ⋅ u1)] ⋅ u1
u3 = v3 − [((v3)t ⋅ u1) ∕ ((u1)t ⋅ u1)] ⋅ u1 − [((v3)t ⋅ u2) ∕ ((u2)t ⋅ u2)] ⋅ u2
u4 = v4 − [((v4)t ⋅ u1) ∕ ((u1)t ⋅ u1)] ⋅ u1 − [((v4)t ⋅ u2) ∕ ((u2)t ⋅ u2)] ⋅ u2 − [((v4)t ⋅ u3) ∕ ((u3)t ⋅ u3)] ⋅ u3

up = vp − [((vp)t ⋅ u1) ∕ ((u1)t ⋅ u1)] ⋅ u1 − [((vp)t ⋅ u2) ∕ ((u2)t ⋅ u2)] ⋅ u2 − … − [((vp)t ⋅ up−1) ∕ ((up−1)t ⋅ up−1)] ⋅ up−1

Prove Gram-Schmit Procedure by means of Projection Matrix

Begin by projection matrix to prove Gram-Schmit Procedure illustrated in below pic:
Project y onto column space of x

take x ⋅ b = yproj, the projection of y onto C(x), where C(x) is the column space spanned by vector x
C(x) ⊥ (y − x ⋅ b), then
=>C(x) ⋅ (y − x ⋅ b) = 0,
=>xt ⋅ (y − x ⋅ b) = 0,
=>xt ⋅ x ⋅ b = xt ⋅ y,
=>b = (xt ⋅ x) ⋅ xt ⋅ y; where (xt ⋅ x) is the generalized inverse form,
=>yproj = x ⋅ (xt ⋅ x) ⋅ xt ⋅ y
∵x is itself a column vector, then (xt ⋅ x) = (xt ⋅ x)−1 just holds, for the vector x is in the spanning set/basis
∴yproj = [(xt ⋅ y) ∕ (xt ⋅ x)] ⋅ x = [(yt ⋅ x) ∕ (xt ⋅ x)] ⋅ x

To further explain Gram-Schmit Procedure in terms of Projection Matrix:

take y as v2, x as u1, where u1 = v1, then
u2 = v2 − [((u1)t ⋅ v2) ∕ ((u1)t ⋅ u1)] ⋅ u1, where the second term is just the projection of v2 onto u1
u3 = v3 − Projw2(v3), where w2 = Span(u1, u2)
     = v3 − Proju1(v3) − Proju2(v3)
     = v3 − [((u1)t ⋅ v3) ∕ ((u1)t ⋅ u1)] ⋅ u1 − [((u2)t ⋅ v3) ∕ ((u2)t ⋅ u2)] ⋅ u2
the flow is exhibited by below pic:

u4 = v4 − Projw3(v4), where w3 = Span(u1, u2, u3)
     = v4 − [((u1)t ⋅ v4) ∕ ((u1)t ⋅ u1)] ⋅ u1 − [((u2)t ⋅ v4) ∕ ((u2)t ⋅ u2)] ⋅ u2 − [((u3)t ⋅ v4) ∕ ((u3)t ⋅ u3)] ⋅ u3

finally, we can reach
up = vp − [((u1)t ⋅ vp) ∕ ((u1)t ⋅ u1)] ⋅ u1 − [((u2)t ⋅ vp) ∕ ((u2)t ⋅ u2)] ⋅ u2 − … − [((up−1)t ⋅ vp) ∕ ((up−1)t ⋅ up−1)] ⋅ up−1

Further refine the notation in Gram-Schmit and Formula Representation:

take S = {v1, v2,…, vp} to be S = {a1, a2,…, ap}
take T = {u1, u2,…, up} to be T = {b1, b2,…, bp}
b1 = a1
b2 = a2 − [((b1)t ⋅ a2) ∕ ((b1)t ⋅ b1)] ⋅ b1
b3 = a3 − [((b1)t ⋅ a3) ∕ ((b1)t ⋅ b1)] ⋅ b1 − [((b2)t ⋅ a3) ∕ ((b2)t ⋅ b2)] ⋅ b2
b4 = a4 − [((b1)t ⋅ a4) ∕ ((b1)t ⋅ b1)] ⋅ b1 − [((b2)t ⋅ a4) ∕ ((b2)t ⋅ b2)] ⋅ b2 − [((b3)t ⋅ a4) ∕ ((b3)t ⋅ b3)] ⋅ b3

bp = ap − [((b1)t ⋅ ap) ∕ ((b1)t ⋅ b1)] ⋅ b1 − [((b2)t ⋅ ap) ∕ ((b2)t ⋅ b2)] ⋅ b2 − [((b3)t ⋅ ap) ∕ ((b3)t ⋅ b3)] ⋅ b3 − … − [((bp−1)t ⋅ ap) ∕ ((bp−1)t ⋅ bp−1)] ⋅ bp−1
At this moment, the proof has validated Gram-Schmit by the projection matrix

Express Gram-Schmit Procedure in Matrix Product

Advance one step to represent Gram-Schmit Procedure by matrix product:

if we take Xi,j = ((bi)t ⋅ aj) ∕ ((bi)t ⋅ bi), then, we could have:
b1 = a1
b2 = a2 − X1,2 ⋅ b1
b3 = a3 − X1,3 ⋅ b1 − X2,3 ⋅ b2
b4 = a4 − X1,4 ⋅ b1 − X2,4 ⋅ b2 − X3,4 ⋅ b3

bp = ap − X1,p ⋅ b1 − X2,p ⋅ b2 − X3,p ⋅ b3 − … − Xp−2,p ⋅ bp−2 − Xp−1,p ⋅ bp−1

Then, express ai in terms of bi′s:

a1 = b1
a2 = b2 + X1,2 ⋅ b1
a3 = b3 + X1,3 ⋅ b1 + X2,3 ⋅ b2
a4 = b4 + X1,4 ⋅ b1 + X2,4 ⋅ b2 + X3,4 ⋅ b3

ap = bp + X1,p ⋅ b1 + X2,p ⋅ b2 + X3,p ⋅ b3 + … + Xp−2,p ⋅ bp−2 + Xp−1,p ⋅ bp−1

Further refine:

take Xi,i = 1, that is
a1 = X1,1 ⋅ b1
a2 = X1,2 ⋅ b1 + X2,2 ⋅ b2
a3 = X1,3 ⋅ b1 + X2,3 ⋅ b2 + X3,3 ⋅ b3
a4 = X1,4 ⋅ b1 + X2,4 ⋅ b2 + X3,4 ⋅ b3 + X4,4 ⋅ b4

ap = X1,p ⋅ b1 + X2,p ⋅ b2 + X3,p ⋅ b3 + … + Xp−2,p ⋅ bp−2 + Xp−1,p ⋅ bp−1 + Xp,p ⋅ bp

take X as an upper unit triangular matrix where Xi,i = 1 
X = 
X1,1 X1,2 X1,3 X1,4 .... X1,p  
 0    X2,2 X2,3 X2,4 .... X2,p
 0     0   X3,3 X3,4 .... X3,p
 0     0    0   X4,4 .... X4,p
   ..................
 0     0    0   0  ....   Xp,p

then,
Am×p = [a1|a2|…|ap], where ai ∈ Rm,
Bm×p = [b1|b2|…|bp], where bi ∈ Rm,

take D = diag(||b1||−1, ||b2||−1, ||b3||−1,…,||bp||−1)
take Q = B ⋅ D = diag(b1 ∕ ||b1||, b2 ∕ ||b2||, b3 ∕ ||b3||,…, bp ∕ ||bp||)
take E = diag(||b1||, ||b2||, ||b3||,…,||bp||)…to eliminate D
take R = E ⋅ X = [(||b1|| ⋅ X1)|(||b2|| ⋅ X2)|(||b3|| ⋅ X3)|,…,|(||bp|| ⋅ Xp)]…upper triangular matrix
then, A = B ⋅ X = B ⋅ D ⋅ E ⋅ X = Q ⋅ R …QR decomposition, where such Q ⋅ R is unique, ∵B ⋅ X is also unique, too.