Command Palette

Search for a command to run...

Linear Methods of AI

QR Decomposition

QR Decomposition Existence Theorem

We want to transform a matrix into upper triangular form, but not through elementary row operations, rather through orthogonal transformations that have better conditioning. Imagine rotating and reflecting geometric space to simplify the matrix, without changing its fundamental properties.

Let ARm×nA \in \mathbb{R}^{m \times n} be a rectangular matrix with mnm \geq n and RankA=n\text{Rank}A = n. Then there exists an orthogonal matrix QRm×mQ \in \mathbb{R}^{m \times m} with QTQ=IQ^T Q = I and an upper triangular matrix RRm×nR \in \mathbb{R}^{m \times n} with diagonal elements rii>0r_{ii} > 0 for i=1,,ni = 1, \ldots, n, such that:

A=QRA = Q \cdot R

This representation is called the QR decomposition of AA.

Proof using Gram-Schmidt

The columns aja_j for j=1,,nj = 1, \ldots, n of matrix AA can be orthonormalized using the Gram-Schmidt process:

q~j:=ajk=1j1aj,qkqk\tilde{q}_j := a_j - \sum_{k=1}^{j-1} \langle a_j, q_k \rangle \cdot q_k
qj:=q~jq~j2q_j := \frac{\tilde{q}_j}{\|\tilde{q}_j\|_2}

We obtain orthonormal vectors qjq_j for j=1,,nj = 1, \ldots, n as columns of the orthogonal matrix Q1Rm×nQ_1 \in \mathbb{R}^{m \times n}. Conversely:

aj=k=1j1aj,qkqk+q~ja_j = \sum_{k=1}^{j-1} \langle a_j, q_k \rangle \cdot q_k + \tilde{q}_j
=k=1j1aj,qkqk+q~j2qj= \sum_{k=1}^{j-1} \langle a_j, q_k \rangle \cdot q_k + \|\tilde{q}_j\|_2 \cdot q_j
=k=1j1qkrkj+qjrjj= \sum_{k=1}^{j-1} q_k \cdot r_{kj} + q_j \cdot r_{jj}

Thus A=Q1R1A = Q_1 R_1 with upper triangular matrix R1Rn×nR_1 \in \mathbb{R}^{n \times n} whose diagonal elements are rii=q~i2>0r_{ii} = \|\tilde{q}_i\|_2 > 0.

If Q1Q_1 is completed with mnm - n additional columns to become orthogonal matrix Q=(Q1Q2)Rm×mQ = (Q_1 \quad Q_2) \in \mathbb{R}^{m \times m} and R1R_1 becomes R=(R10)Rm×nR = \begin{pmatrix} R_1 \\ 0 \end{pmatrix} \in \mathbb{R}^{m \times n}, then A=Q1R1=QRA = Q_1 R_1 = QR.

Full and Economical QR Decomposition

When we have a matrix ARm×nA \in \mathbb{R}^{m \times n} with mnm \geq n, there are two ways to represent the QR decomposition. The difference lies in the size of the matrices used.

Full QR Decomposition

Full QR decomposition uses full-sized matrices:

A=QRA = Q \cdot R

with QRm×mQ \in \mathbb{R}^{m \times m} being a full-sized orthogonal matrix and RRm×nR \in \mathbb{R}^{m \times n} being an upper triangular matrix.

Economical QR Decomposition

Since the lower part of matrix RR only contains zeros, we can save storage and computation. Economical QR decomposition only uses the parts that are actually needed:

A=QR=(Q1Q2)(R10)=Q1R1A = Q \cdot R = (Q_1 \quad Q_2) \cdot \begin{pmatrix} R_1 \\ 0 \end{pmatrix} = Q_1 \cdot R_1

Here Q1Rm×nQ_1 \in \mathbb{R}^{m \times n} only takes the first through nn-th columns from QQ, and R1Rn×nR_1 \in \mathbb{R}^{n \times n} is a square upper triangular matrix.

Why is it called economical? Because we save storage space and computational time. Instead of storing matrix QQ of size m×mm \times m which can be very large, we only need Q1Q_1 of size m×nm \times n.

The columns of matrix Q2Rm×(mn)Q_2 \in \mathbb{R}^{m \times (m-n)} that we don't use form an orthonormal basis of KernelAT\text{Kernel}A^T:

ATQ2=R1TQ1TQ2=0A^T Q_2 = R_1^T Q_1^T Q_2 = 0

Uniqueness Property

The economical QR decomposition A=Q1R1A = Q_1 \cdot R_1 with condition rii>0r_{ii} > 0 for all i=1,,ni = 1, \ldots, n is unique for matrix AA that has full rank.

Householder Method for QR Decomposition

Although the Gram-Schmidt process provides an elegant theoretical way to obtain QR decomposition, this method is not suitable for practical computation. The main problem is numerical instability due to cancellation, orthogonality of columns is quickly lost during computation.

To overcome this problem, we need a method that is more numerically stable. One of the most successful approaches is the Householder procedure, which uses orthogonal reflection transformations. Another alternative is the Givens procedure with rotation transformations.

Householder Transformation

For a vector vRmv \in \mathbb{R}^m with v2=1\|v\|_2 = 1, we can define the Householder transformation matrix:

S=I2vvTRm×mS = I - 2vv^T \in \mathbb{R}^{m \times m}

Note that vvTvv^T is a dyadic product, which is the multiplication of column vector vRm×1v \in \mathbb{R}^{m \times 1} with row vector vTR1×mv^T \in \mathbb{R}^{1 \times m}. The result of this multiplication is an m×mm \times m matrix with rank 1 for v0v \neq 0. Don't confuse this with scalar multiplication vTvRv^T v \in \mathbb{R}.

Properties of Householder Transformation

Let S=I2vvTRm×mS = I - 2vv^T \in \mathbb{R}^{m \times m} be a Householder transformation matrix for vector vRmv \in \mathbb{R}^m with v2=1\|v\|_2 = 1. Then it holds:

  1. SS is symmetric: ST=SS^T = S

  2. SS is an orthogonal matrix: STS=IS^T S = I

  3. Multiplication SxS \cdot x of SS from the left with vector xRnx \in \mathbb{R}^n causes reflection of xx in the subspace Span(v)\text{Span}(v)^{\perp}, that is, in the hyperplane with normal vector vv

  4. cond2(S)=1\text{cond}_2(S) = 1

Householder Procedure

Given matrix ARm×nA \in \mathbb{R}^{m \times n} with mnm \geq n and RankA=n\text{Rank}A = n. For QR decomposition computation, matrix AA is transformed column by column through Householder reflections into upper triangular form.

Start with A1=AA_1 = A and reflect the first column of A1A_1 with respect to a vector using reflection plane with:

a~1 first column of A1\tilde{a}_1 \text{ first column of } A_1
±a~12e1Rm target vector\pm\|\tilde{a}_1\|_2 \cdot e_1 \in \mathbb{R}^m \text{ target vector}
v1=u1/u12 with Span(v1)v_1 = u_1/\|u_1\|_2 \text{ with } \text{Span}(v_1)^{\perp}
u1=a~1a~12e1u_1 = \tilde{a}_1 \mp \|\tilde{a}_1\|_2 \cdot e_1

Iterative Transformation Process

Householder transformation matrix S1=Im2v1v1TRm×mS_1 = I_m - 2v_1v_1^T \in \mathbb{R}^{m \times m}. We obtain:

A2=S1A1=(r110A~2)A_2 = S_1 \cdot A_1 = \begin{pmatrix} r_{11} & * \\ 0 & \tilde{A}_2 \end{pmatrix}

with r11=±a~12r_{11} = \pm\|\tilde{a}_1\|_2 and A~2Rm1×n1\tilde{A}_2 \in \mathbb{R}^{m-1 \times n-1}.

Continue with reflection of the first column of the submatrix with reflection plane:

a~2 first column of A~2\tilde{a}_2 \text{ first column of } \tilde{A}_2
±a~22e~1Rm1\pm\|\tilde{a}_2\|_2 \cdot \tilde{e}_1 \in \mathbb{R}^{m-1}
v2=u2/u22v_2 = u_2/\|u_2\|_2
u2=a~2a~22e~1u_2 = \tilde{a}_2 \mp \|\tilde{a}_2\|_2 \cdot \tilde{e}_1

With transformation matrix:

S~2=Im12v2v2TRm1×m1\tilde{S}_2 = I_{m-1} - 2v_2v_2^T \in \mathbb{R}^{m-1 \times m-1}
S2=(I100S~2)Rm×mS_2 = \begin{pmatrix} I_1 & 0 \\ 0 & \tilde{S}_2 \end{pmatrix} \in \mathbb{R}^{m \times m}

We obtain:

A3=S2A2=(r110r2200A~3)A_3 = S_2 \cdot A_2 = \begin{pmatrix} r_{11} & * & * \\ 0 & r_{22} & * \\ 0 & 0 & \tilde{A}_3 \end{pmatrix}

with r22=±a~22r_{22} = \pm\|\tilde{a}_2\|_2 and A~3Rm2×n2\tilde{A}_3 \in \mathbb{R}^{m-2 \times n-2}, and so on until:

A~n=(a~nna~mn)Rm(n1)×1\tilde{A}_n = \begin{pmatrix} \tilde{a}_{nn} \\ \vdots \\ \tilde{a}_{mn} \end{pmatrix} \in \mathbb{R}^{m-(n-1) \times 1}

Finally we obtain the upper triangular matrix:

An+1=R=(r110rnn0000)=SnS1A=QTAA_{n+1} = R = \begin{pmatrix} r_{11} & & * \\ & \ddots & \\ 0 & & r_{nn} \\ 0 & \cdots & 0 \\ \vdots & & \vdots \\ 0 & \cdots & 0 \end{pmatrix} = S_n \cdot \ldots \cdot S_1 \cdot A = Q^T \cdot A

Thus we obtain the factorization:

A=QRA = Q \cdot R
Q=(SnS1)T=S1SnQ = (S_n \cdot \ldots \cdot S_1)^T = S_1 \cdot \ldots \cdot S_n

with QQ being an orthogonal matrix.

Householder Implementation Algorithm

Implementation of the Householder procedure:

Start with:

A1:=AA_1 := A
Q1:=IQ_1 := I

For i=1,,ni = 1, \ldots, n:

a~i=(aiiami):=((Ai)ii(Ai)mi)Rmi+1\tilde{a}_i = \begin{pmatrix} a_{ii} \\ \vdots \\ a_{mi} \end{pmatrix} := \begin{pmatrix} (A_i)_{ii} \\ \vdots \\ (A_i)_{mi} \end{pmatrix} \in \mathbb{R}^{m-i+1}

Compute:

σ:=a~i2\sigma := \|\tilde{a}_i\|_2
ui:=a~i+(σ00)Rmi+1u_i := \tilde{a}_i + \begin{pmatrix} \mp\sigma \\ 0 \\ \vdots \\ 0 \end{pmatrix} \in \mathbb{R}^{m-i+1}
u^i:=(00ui)Rm\hat{u}_i := \begin{pmatrix} 0 \\ \vdots \\ 0 \\ u_i \end{pmatrix} \in \mathbb{R}^m

and:

β:=1/(σ(σ+a~ii))\beta := 1/(\sigma(\sigma + |\tilde{a}_{ii}|))

Then:

vi=a~ia~i2e1a~ia~i2e12=uiui2v_i = \frac{\tilde{a}_i \mp \|\tilde{a}_i\|_2 \cdot e_1}{\|\tilde{a}_i \mp \|\tilde{a}_i\|_2 \cdot e_1\|_2} = \frac{u_i}{\|u_i\|_2}
S~i=Imi+12viviT=Imi+1βuiuiT\tilde{S}_i = I_{m-i+1} - 2v_i v_i^T = I_{m-i+1} - \beta u_i u_i^T
Si=Imβu^iu^iTS_i = I_m - \beta \hat{u}_i \hat{u}_i^T

So we obtain:

Ai+1:=SiAi=(Imβu^iu^iT)Ai=Aiu^i(βu^iTAi)A_{i+1} := S_i A_i = (I_m - \beta \hat{u}_i \hat{u}_i^T) A_i = A_i - \hat{u}_i (\beta \hat{u}_i^T A_i)
Qi+1:=QiSi=Qi(Imβu^iu^iT)=Qi(Qiu^i)βu^iTQ_{i+1} := Q_i S_i = Q_i (I_m - \beta \hat{u}_i \hat{u}_i^T) = Q_i - (Q_i \hat{u}_i) \beta \hat{u}_i^T

Finally we obtain:

R:=An+1R := A_{n+1}
Q:=Qn+1Q := Q_{n+1}

Numerical Aspects and Householder Complexity

Computational Complexity

The numerical complexity for computing QR decomposition of matrix ARm×nA \in \mathbb{R}^{m \times n} with Householder procedure is essentially:

n2m arithmetic operations for mnn^2 \cdot m \text{ arithmetic operations for } m \gg n
23n3 arithmetic operations for mn\frac{2}{3}n^3 \text{ arithmetic operations for } m \approx n

Numerical Properties

  1. Due to orthogonal transformation, it holds that cond2(R)=cond2(A)\text{cond}_2(R) = \text{cond}_2(A)

  2. The diagonal elements of RR are the numbers ±a~i2\pm\|\tilde{a}_i\|_2 from the ii-th step. Choosing the transformation so that all are positive gives the QR decomposition. If a~i2=0\|\tilde{a}_i\|_2 = 0, AA does not have full rank and the algorithm stops.

    For numerical reasons choose the sign so that cancellation is avoided:

    ui=a~ia~i2e1 (general form)u_i = \tilde{a}_i \mp \|\tilde{a}_i\|_2 \cdot e_1 \text{ (general form)}
    ui=a~i+sgn(a~i)a~i2e1 (optimal sign choice)u_i = \tilde{a}_i + \text{sgn}(\tilde{a}_i) \cdot \|\tilde{a}_i\|_2 \cdot e_1 \text{ (optimal sign choice)}
  3. Instead of constructing QQ, in compact storage one can also store only the information needed for the transformation:

    Store vector a~ia~i2e1\text{Store vector } \tilde{a}_i \mp \|\tilde{a}_i\|_2 \cdot e_1

    in the free space of AA below the diagonal and diagonal elements in an additional vector.

  4. If you only want to compute economical QR decomposition, remove the corresponding columns of QQ and rows of RR.