QR decomposition is another decomposition method that factorises a matrix in the product of an orthogonal matrix \(Q\) and an upper triangular matrix \(R\) such that
\[ \begin{align*}
A = QR.
\end{align*} \]
QR decomposition is particularly useful for solving least-squares problems and computing eigenvalues. Unlike LU decomposition, QR decomposition can be applied to rectangular matrices and is numerically more stable.
Definition 6.3 (Orthogonal vectors)
A set of vectors \(\{ \mathbf{v}_1 ,\mathbf{v}_2 ,\mathbf{v}_3 ,\dots \}\) is said to be orthogonal if \(\mathbf{v}_i \cdot \mathbf{v}_j =0\) for \(i\not= j\). Furthermore the set is said to be orthonormal if \(\mathbf{v}_i\) are unit vectors.
Definition 6.4 (Orthogonal matrix)
An orthogonal matrix is a matrix whose columns form an orthonormal set. Equivalently
\[ \begin{align*}
A^\mathsf{T} A = AA^\mathsf{T} = I.
\end{align*} \]
where \(\mathbf{a}_j = (a_{1j}, a_{2j}, a_{3j})^\mathsf{T}\). To calculate the orthogonal \(Q\) matrix we need a set of vectors \(\{\mathbf{q}_1, \mathbf{q}_2, \mathbf{q}_3\}\) that span the same space as \(\{\mathbf{a}_1, \mathbf{a}_2, \mathbf{a}_3\}\) but are orthogonal to each other. One way to do this is using the Gram-Schmidt process. Consider the diagram shown in Fig. 6.1 that shows the first two non-orthogonal vectors \(\mathbf{a}_1\) and \(\mathbf{a}_2\).
We begin by letting \(\mathbf{u}_1 = \mathbf{a}_1\) and seek to find a vector \(\mathbf{u}_2\) that is orthogonal to \(\mathbf{u}_1\) and in the same span as \(\mathbf{a}_2\). To do this we normalise \(\mathbf{u}_1 = \mathbf{q}_1\) and subtract the vector projection of \(\mathbf{a}_2\) onto \(\mathbf{q}_1\), i.e.,
which we also normalise to give \(\mathbf{q}_2\). For the next vector \(\mathbf{a}_3\) we want the vector \(\mathbf{u}_3\) to be orthogonal to both \(\mathbf{q}_1\) and \(\mathbf{q}_2\) so we subtract both the vector projections of \(\mathbf{a}_3\) onto \(\mathbf{q}_1\) and \(\mathbf{q}_2\)
which is normalised to give \(\mathbf{q}_3\). Note that \(\mathbf{u}_i = (\mathbf{q}_i \cdot \mathbf{a}_i) \mathbf{q}_i\), i.e., \(\mathbf{u}_i\) it the vector projection of \(\mathbf{a}_i\) onto the unit vector that points in the direction of \(\mathbf{u}_i\) so (6.4) and (6.5) can be written as
It has become standard convention to impose a condition that the diagonal elements of \(R\) must be positive so that we get a unique factorisation. Consider \(A = QR\) then multiplying a column of \(Q\) by \(-1\) and the corresponding row of \(R\) by \(-1\) doesn’t change the product \(QR\). So if any of the diagonal elements of \(R\) are negative we multiply the corresponding column of \(Q\) and row of \(R\) by \(-1\).
Algorithm 6.6 (QR decomposition using the Gram-Schmidt process)
Inputs: An \(m \times n\) matrix \(A\).
Outputs: An \(m \times n\) orthogonal matrix \(Q\) and and \(n \times n\) upper triangular matrix \(R\) such that \(A = QR\).
6.4.2. QR decomposition using Householder reflections#
Another method we can use to calculate the QR decomposition of a matrix is by the use of Householder reflections. A Householder reflection is a linear transformation that reflects a vector \(\mathbf{x}\) about a line passing through the origin that is normal to another vector \(\mathbf{v}\) (Fig. 6.2).
Fig. 6.2 The vector \(\mathbf{x}\) is reflected about the hyperplane normal to the vector \(\mathbf{v}\).#
The matrix that performs this reflection is known as the Householder matrix.
Definition 6.5 (Householder matrix)
The Householder matrix is an orthogonal reflection matrix of the form
(6.6)#\[ H = I - 2 \frac{\mathbf{v} \mathbf{v}^\mathsf{T}}{\mathbf{v}^\mathsf{T} \mathbf{v}}, \]
where \(\mathbf{v}\) is a non-zero column vector. The matrix \(H\) is symmetric and orthogonal.
To determine the set of orthogonal vectors \(\{ \mathbf{q}_1, \mathbf{q}_2, \ldots, \mathbf{q}_n \}\) that will form the columns of \(Q\) start by letting \(R = A\) and reflect the first column of \(R\) so that it is parallel to the first basis vector \(\mathbf{e}_1 = (1, 0, \ldots, 0)\) (Fig. 6.3).
Fig. 6.3 The vector \(\mathbf{x}\) is reflected about the line normal to \(\mathbf{v}\) so that it is parallel to the basis vector \(\mathbf{e}_1\).#
Let \(\mathbf{x}\) be the first column of \(R\) then
If \(\mathbf{x}\) is nearly parallel to \(\mathbf{e}_1\), then \(\mathbf{v}\) will be close to \(\mathbf{0}\) and the denominator in (6.6) will be very small. To avoid this, \(\mathbf{x}\) is reflected so that it is parallel to \(-\mathbf{e}_1\) instead of \(+\mathbf{e}_1\) (Fig. 6.4).
Fig. 6.4 The vector \(\mathbf{x}\) is reflected about the dashed line so that it is points along the negative basis vector \(-\mathbf{e}_1\).#
where the function \(\operatorname{sign}(x_1)\) returns \(+1\) if \(x_1 \geq 0\) and \(-1\) if \(x_1 < 0\). The Householder matrix is an \(m \times m\) matrix \(H_1\) and is multiplied on the left by \(R\)
The first column of \(R\) is now parallel to \(\mathbf{e}_1\). We now repeat this process for the sub-matrix \(R'\). The resulting Householder matrix will be an \(m -1 \times m-1\) matrix \(H'\). Since we want to apply this to \(R\) we use the first rows and columns of the identity matrix to pad it out, i.e.,
Like any numerical technique, the QR decomposition methods shown here are prone to computational rounding errors. If \(Q\) is perfectly orthogonal then \(Q^\mathsf{T}Q = I\), but if there is bound to be some small error, i.e.,
\[ Q^\mathsf{T}Q = I + E \]
where \(E\) is the orthogonality error matrix. A single value for the loss of orthogonality can be computed using
\[ \| E \|_F = \| Q^\mathsf{T}Q - I \|_F, \]
where \(\| \cdot \|_F\) is the Frobenius norm defined by
\[ \| A \|_F = \sqrt{ \displaystyle\sum_{i=1}^m \displaystyle\sum_{j=1}^n |a_{ij}|^2}. \]
The both QR decomposition methods shown here use an iterative approach where we orthogonalised the columns of the \(A\) matrix in turn based upon the previous iterations. So an error in column \(i\) will have a knock on effect on all subsequent columns. To analyse the methods, the QR decomposition of a random \(200 \times 200\) matrix \(A\) was computed using the Gram-Schmidt process and Householder reflections. The Frobenius norm was calculated for the first \(n\) columns of \(Q\) where \(n = 1 \ldots 200\) giving the loss of orthogonality for those iterations and plotted in below.
Fig. 6.5 Comparisons between the loss of orthogonality using Gram-Schmidt and Householder reflections to compute the QR decomposition of a 200 column matrix.#
The loss of orthogonality grows much more slowly for Householder reflections than for classical Gram-Schmidt. For this reason, Householder QR is the method used in most numerical linear algebra software.
6.4.4. Calculating eigenvalues using QR decomposition#
Eigenvalues and eigenvectors feature prominently in the study of numerical methods for ODEs. Given a system of ODEs, the eigenvalues of the coefficient matrix provide information about the stability, divergence, oscillatory behavior, and constant solutions of the system.
Definition 6.6 (Eigenvalue)
Let \(A\) be an \(n \times n\) matrix. A scalar \(\lambda\) is an eigenvalue of \(A\) if there exists a non-zero vector \(\mathbf{v}\) such that
\[\begin{split} \begin{align*}
A \mathbf{v} &= \lambda \mathbf{v} \\
(A - \lambda I) \mathbf{v} &= \mathbf{0}.
\end{align*} \end{split}\]
Since \(\mathbf{v} \ne \mathbf{0}\), the homogeneous system \((A - \lambda I) \mathbf{v} = \mathbf{0}\) has non-trivial solution. This occurs if and only if \(A - \lambda I\) is singular, which means
so the eigenvalues are \(\lambda_1 = 4\) and \(\lambda_2 = 1\)[1]. The problem with using determinants to calculate eigenvalues is that it is too computationally expensive for larger matrices.
Although the characteristic polynomial can be used to determine eigenvalues analytically for small matrices, it becomes computationally expensive and numerically unreliable for large matrices. Modern numerical software therefore uses iterative methods such as the QR algorithm.
The QR algorithm is a method of computing the eigenvalues of a square matrix \(A\). Let
each iterate is similar to the previous iterate. Therefore all matrices \(A_0, A_1, A_2, \ldots \) have exactly the same eigenvalues. Under suitable conidtions, \(A_k\) converges to an upper triangular matrix.
so \(\lambda_1 = (5 + \sqrt{33})/2 \approx 5.372281\) and \(\lambda_2 = (5 - \sqrt{33}) / 2 \approx -0.372281\) which shows the QR algorithm has calculated the eigenvalues correct to four decimal places.