QR decomposition

From testwiki
Revision as of 04:49, 1 March 2025 by imported>Brightlightshine (growthexperiments-addlink-summary-summary:3|0|0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:Short description In linear algebra, a QR decomposition, also known as a QR factorization or QU factorization, is a decomposition of a matrix A into a product A = QR of an orthonormal matrix Q and an upper triangular matrix R. QR decomposition is often used to solve the linear least squares (LLS) problem and is the basis for a particular eigenvalue algorithm, the QR algorithm.

Cases and definitions

Square matrix

Any real square matrix A may be decomposed as

A=QR,

where Q is an orthogonal matrix (its columns are orthogonal unit vectors meaning Template:Nowrap and R is an upper triangular matrix (also called right triangular matrix). If A is invertible, then the factorization is unique if we require the diagonal elements of R to be positive.

If instead A is a complex square matrix, then there is a decomposition A = QR where Q is a unitary matrix (so the conjugate transpose Template:Nowrap

If A has n linearly independent columns, then the first n columns of Q form an orthonormal basis for the column space of A. More generally, the first k columns of Q form an orthonormal basis for the span of the first k columns of A for any Template:Nowrap.[1] The fact that any column k of A only depends on the first k columns of Q corresponds to the triangular form of R.[1]

Rectangular matrix

More generally, we can factor a complex m×n matrix A, with Template:Nowrap, as the product of an m×m unitary matrix Q and an m×n upper triangular matrix R. As the bottom (mn) rows of an m×n upper triangular matrix consist entirely of zeroes, it is often useful to partition R, or both R and Q:

A=QR=Q[R10]=[Q1Q2][R10]=Q1R1,

where R1 is an n×n upper triangular matrix, 0 is an Template:Nowrap zero matrix, Q1 is m×n, Q2 is Template:Nowrap, and Q1 and Q2 both have orthogonal columns.

Template:Harvtxt call Q1R1 the thin QR factorization of A; Trefethen and Bau call this the reduced QR factorization.[1] If A is of full rank n and we require that the diagonal elements of R1 are positive then R1 and Q1 are unique, but in general Q2 is not. R1 is then equal to the upper triangular factor of the Cholesky decomposition of ATemplate:Starred A (= ATA if A is real).

QL, RQ and LQ decompositions

Analogously, we can define QL, RQ, and LQ decompositions, with L being a lower triangular matrix.

Computing the QR decomposition

There are several methods for actually computing the QR decomposition, such as the Gram–Schmidt process, Householder transformations, or Givens rotations. Each has a number of advantages and disadvantages.

Using the Gram–Schmidt process

Template:Further Consider the Gram–Schmidt process applied to the columns of the full column rank matrix Template:Nowrap with inner product 𝐯,𝐰=𝐯T𝐰 (or 𝐯,𝐰=𝐯𝐰 for the complex case).

Define the projection:

proj𝐮𝐚=𝐮,𝐚𝐮,𝐮𝐮

then:

𝐮1=𝐚1,𝐞1=𝐮1𝐮1𝐮2=𝐚2proj𝐮1𝐚2,𝐞2=𝐮2𝐮2𝐮3=𝐚3proj𝐮1𝐚3proj𝐮2𝐚3,𝐞3=𝐮3𝐮3𝐮k=𝐚kj=1k1proj𝐮j𝐚k,𝐞k=𝐮k𝐮k

We can now express the 𝐚is over our newly computed orthonormal basis:

𝐚1=𝐞1,𝐚1𝐞1𝐚2=𝐞1,𝐚2𝐞1+𝐞2,𝐚2𝐞2𝐚3=𝐞1,𝐚3𝐞1+𝐞2,𝐚3𝐞2+𝐞3,𝐚3𝐞3𝐚k=j=1k𝐞j,𝐚k𝐞j

where Template:Nowrap This can be written in matrix form:

A=QR

where:

Q=[𝐞1𝐞n]

and

R=[𝐞1,𝐚1𝐞1,𝐚2𝐞1,𝐚3𝐞1,𝐚n0𝐞2,𝐚2𝐞2,𝐚3𝐞2,𝐚n00𝐞3,𝐚3𝐞3,𝐚n000𝐞n,𝐚n].

Example

Consider the decomposition of

A=[1251461676842441].

Recall that an orthonormal matrix Q has the property Template:Nowrap

Then, we can calculate Q by means of Gram–Schmidt as follows:

U=[𝐮1𝐮2𝐮3]=[126958/561586/543033];Q=[𝐮1𝐮1𝐮2𝐮2𝐮3𝐮3]=[6/769/17558/1753/7158/1756/1752/76/3533/35].

Thus, we have

QTA=QTQR=R;R=QTA=[1421140175700035].

Relation to RQ decomposition

The RQ decomposition transforms a matrix A into the product of an upper triangular matrix R (also known as right-triangular) and an orthogonal matrix Q. The only difference from QR decomposition is the order of these matrices.

QR decomposition is Gram–Schmidt orthogonalization of columns of A, started from the first column.

RQ decomposition is Gram–Schmidt orthogonalization of rows of A, started from the last row.

Advantages and disadvantages

The Gram-Schmidt process is inherently numerically unstable. While the application of the projections has an appealing geometric analogy to orthogonalization, the orthogonalization itself is prone to numerical error. A significant advantage is the ease of implementation.

Using Householder reflections

Template:See also

Householder reflection for QR-decomposition: The goal is to find a linear transformation that changes the vector 𝐱 into a vector of the same length which is collinear to 𝐞1. We could use an orthogonal projection (Gram-Schmidt) but this will be numerically unstable if the vectors 𝐱 and 𝐞1 are close to orthogonal. Instead, the Householder reflection reflects through the dotted line (chosen to bisect the angle between 𝐱 and Template:Nowrap The maximum angle with this transform is 45 degrees.

A Householder reflection (or Householder transformation) is a transformation that takes a vector and reflects it about some plane or hyperplane. We can use this operation to calculate the QR factorization of an m-by-n matrix A with Template:Nowrap.

Q can be used to reflect a vector in such a way that all coordinates but one disappear.

Let 𝐱 be an arbitrary real m-dimensional column vector of A such that 𝐱=|α| for a scalar α. If the algorithm is implemented using floating-point arithmetic, then α should get the opposite sign as the k-th coordinate of Template:Nowrap where xk is to be the pivot coordinate after which all entries are 0 in matrix ATemplate:'s final upper triangular form, to avoid loss of significance. In the complex case, set[2]

α=eiargxk𝐱

and substitute transposition by conjugate transposition in the construction of Q below.

Then, where 𝐞1 is the vector Template:Math, Template:Math is the Euclidean norm and I is an Template:Math identity matrix, set

𝐮=𝐱α𝐞1,𝐯=𝐮𝐮,Q=I2𝐯𝐯T.

Or, if A is complex

Q=I2𝐯𝐯.

Q is an m-by-m Householder matrix, which is both symmetric and orthogonal (Hermitian and unitary in the complex case), and

Q𝐱=[α00].

This can be used to gradually transform an m-by-n matrix A to upper triangular form. First, we multiply A with the Householder matrix Q1 we obtain when we choose the first matrix column for x. This results in a matrix Q1A with zeros in the left column (except for the first row).

Q1A=[α10A0]

This can be repeated for A′ (obtained from Q1A by deleting the first row and first column), resulting in a Householder matrix Q2. Note that Q2 is smaller than Q1. Since we want it really to operate on Q1A instead of A′ we need to expand it to the upper left, filling in a 1, or in general:

Qk=[Ik100Qk].

After t iterations of this process, Template:Nowrap

R=QtQ2Q1A

is an upper triangular matrix. So, with

QT=QtQ2Q1,Q=Q1TQ2TQtT

A=QR is a QR decomposition of A.

This method has greater numerical stability than the Gram–Schmidt method above.

The following table gives the number of operations in the k-th step of the QR-decomposition by the Householder transformation, assuming a square matrix with size n.

Operation Number of operations in the k-th step
Multiplications 2(nk+1)2
Additions (nk+1)2+(nk+1)(nk)+2
Division 1
Square root 1

Summing these numbers over the Template:Nowrap steps (for a square matrix of size n), the complexity of the algorithm (in terms of floating point multiplications) is given by

23n3+n2+13n2=O(n3).

Example

Let us calculate the decomposition of

A=[1251461676842441].

First, we need to find a reflection that transforms the first column of matrix A, vector Template:Nowrap into Template:Nowrap

Now,

𝐮=𝐱α𝐞1,

and

𝐯=𝐮𝐮.

Here,

α=14 and 𝐱=𝐚1=[1264]T

Therefore

𝐮=[264]T=2[132]T and Template:Nowrap and then
Q1=I21414[132][132]=I17[132396264]=[6/73/72/73/72/76/72/76/73/7].

Now observe:

Q1A=[14211404914016877],

so we already have almost a triangular matrix. We only need to zero the (3, 2) entry.

Take the (1, 1) minor, and then apply the process again to

A=M11=[491416877].

By the same method as above, we obtain the matrix of the Householder transformation

Q2=[10007/2524/25024/257/25]

after performing a direct sum with 1 to make sure the next step in the process works properly.

Now, we find

Q=Q1TQ2T=[6/769/17558/1753/7158/1756/1752/76/3533/35].

Or, to four decimal digits,

Q=Q1TQ2T=[0.85710.39430.33140.42860.90290.03430.28570.17140.9429]R=Q2Q1A=QTA=[1421140175700035].

The matrix Q is orthogonal and R is upper triangular, so Template:Nowrap is the required QR decomposition.

Advantages and disadvantages

The use of Householder transformations is inherently the most simple of the numerically stable QR decomposition algorithms due to the use of reflections as the mechanism for producing zeroes in the R matrix. However, the Householder reflection algorithm is bandwidth heavy and difficult to parallelize, as every reflection that produces a new zero element changes the entirety of both Q and R matrices.

Parallel implementation of Householder QR

The Householder QR method can be implemented in parallel with algorithms such as the TSQR algorithm (which stands for Tall Skinny QR). This algorithm can be applied in the case when the matrix A has m >> n.[3] This algorithm uses a binary reduction tree to compute local householder QR decomposition at each node in the forward pass, and re-constitute the Q matrix in the backward pass. The binary tree structure aims at decreasing the amount of communication between processor to increase performance.

Using Givens rotations

QR decompositions can also be computed with a series of Givens rotations. Each rotation zeroes an element in the subdiagonal of the matrix, forming the R matrix. The concatenation of all the Givens rotations forms the orthogonal Q matrix.

In practice, Givens rotations are not actually performed by building a whole matrix and doing a matrix multiplication. A Givens rotation procedure is used instead which does the equivalent of the sparse Givens matrix multiplication, without the extra work of handling the sparse elements. The Givens rotation procedure is useful in situations where only relatively few off-diagonal elements need to be zeroed, and is more easily parallelized than Householder transformations.

Example

Let us calculate the decomposition of

A=[1251461676842441].

First, we need to form a rotation matrix that will zero the lowermost left element, Template:Nowrap We form this matrix using the Givens rotation method, and call the matrix G1. We will first rotate the vector Template:Nowrap to point along the X axis. This vector has an angle Template:Nowrap We create the orthogonal Givens rotation matrix, G1:

G1=[cos(θ)0sin(θ)010sin(θ)0cos(θ)][0.9486800.316220100.3162200.94868]

And the result of G1A now has a zero in the a31 element.

G1A[12.6491155.9723116.7600761676806.6407837.6311]

We can similarly form Givens matrices G2 and Template:Nowrap which will zero the sub-diagonal elements a21 and Template:Nowrap forming a triangular matrix Template:Nowrap The orthogonal matrix QT is formed from the product of all the Givens matrices Template:Nowrap Thus, we have Template:Nowrap and the QR decomposition is Template:Nowrap

Advantages and disadvantages

The QR decomposition via Givens rotations is the most involved to implement, as the ordering of the rows required to fully exploit the algorithm is not trivial to determine. However, it has a significant advantage in that each new zero element aij affects only the row with the element to be zeroed (i) and a row above (j). This makes the Givens rotation algorithm more bandwidth efficient and parallelizable than the Householder reflection technique.

Connection to a determinant or a product of eigenvalues

We can use QR decomposition to find the determinant of a square matrix. Suppose a matrix is decomposed as A=QR. Then we have detA=detQdetR.

Q can be chosen such that detQ=1. Thus, detA=detR=irii

where the rii are the entries on the diagonal of R. Furthermore, because the determinant equals the product of the eigenvalues, we have irii=iλi

where the λi are eigenvalues of A.

We can extend the above properties to a non-square complex matrix A by introducing the definition of QR decomposition for non-square complex matrices and replacing eigenvalues with singular values.

Start with a QR decomposition for a non-square matrix A:

A=Q[R0],QQ=I

where 0 denotes the zero matrix and Q is a unitary matrix.

From the properties of the singular value decomposition (SVD) and the determinant of a matrix, we have

|irii|=iσi,

where the σi are the singular values of Template:Nowrap

Note that the singular values of A and R are identical, although their complex eigenvalues may be different. However, if A is square, then

iσi=|iλi|.

It follows that the QR decomposition can be used to efficiently calculate the product of the eigenvalues or singular values of a matrix.

Column pivoting

Pivoted QR differs from ordinary Gram-Schmidt in that it takes the largest remaining column at the beginning of each new step—column pivoting—[4] and thus introduces a permutation matrix P:

AP=QRA=QRPT

Column pivoting is useful when A is (nearly) rank deficient, or is suspected of being so. It can also improve numerical accuracy. P is usually chosen so that the diagonal elements of R are non-increasing: |r11||r22||rnn|. This can be used to find the (numerical) rank of A at lower computational cost than a singular value decomposition, forming the basis of so-called rank-revealing QR algorithms.

Using for solution to linear inverse problems

Compared to the direct matrix inverse, inverse solutions using QR decomposition are more numerically stable as evidenced by their reduced condition numbers.[5]

To solve the underdetermined Template:Nowrap linear problem A𝐱=𝐛 where the matrix A has dimensions m×n and rank Template:Nowrap first find the QR factorization of the transpose of Template:Nowrap Template:Nowrap where Q is an orthogonal matrix (i.e. Template:Nowrap and R has a special form: R=[R10]. Here R1 is a square m×m right triangular matrix, and the zero matrix has dimension Template:Nowrap After some algebra, it can be shown that a solution to the inverse problem can be expressed as: 𝐱=Q[(R1T)1𝐛0] where one may either find R11 by Gaussian elimination or compute (R1T)1𝐛 directly by forward substitution. The latter technique enjoys greater numerical accuracy and lower computations.

To find a solution 𝐱^ to the overdetermined Template:Nowrap problem A𝐱=𝐛 which minimizes the norm Template:Nowrap first find the QR factorization of Template:Nowrap Template:Nowrap The solution can then be expressed as Template:Nowrap where Q1 is an m×n matrix containing the first n columns of the full orthonormal basis Q and where R1 is as before. Equivalent to the underdetermined case, back substitution can be used to quickly and accurately find this 𝐱^ without explicitly inverting Template:Nowrap (Q1 and R1 are often provided by numerical libraries as an "economic" QR decomposition.)

Generalizations

Iwasawa decomposition generalizes QR decomposition to semi-simple Lie groups.

See also

References

  1. 1.0 1.1 1.2 Template:Cite book
  2. Template:Citation
  3. Communication-optimal parallel and sequential QR and LU factorizations: theory and practice, James Demmel and Laura Grigori, 2008, https://arxiv.org/abs/0806.2159,
  4. Template:Cite book
  5. Template:Cite book

Further reading

Template:Numerical linear algebra