QR Decomposition Calculator
Welcome to Omni's QR decomposition calculator! Here you can determine the QR decompositions of square and rectangular matrices. Not sure what the QR decomposition (or QR factorization) is?
We will give you the definition of the QR decomposition for square matrices and tell you how it extends to rectangular matrices with more rows than columns. We will explain how to find the QR decomposition and we will discuss the main application of the QR factorization, that is, how it can facilitate solving systems of linear equations. At the very end there will be, of course, a detailed step-by-step example of QR decomposition. Let's go!
What is the QR decomposition?
Decomposition (or factorization) of a matrix is the process of representing this matrix as a product of two or more matrices that have various special properties. The idea is that various matrix operations become simpler thanks to the special properties of constituent matrices. There are lots of different types of matrix decompositions; among the most popular types there is the QR decomposition, which is the topic of this page, as well as the LU decomposition, Cholesky decomposition, and the singular value decomposition (SVD).
In the QR decomposition, we factor a real square matrix
A of size
n x n into the product of two matrices:
A = QR
n x northogonal matrix (i.e., a matrix whose columns form an orthonormal basis for Rⁿ; in other words, these columns are orthogonal vectors of unit length); and
n x nupper triangular matrix (i.e., all the elements below the diagonal are zero).
The QR decomposition can be extended to rectangular matrices of size
m x n, where
m ≥ n.
In such a case, we have
A = QR where
Q is of size
m x n and its
n columns are orthogonal vectors of unit length, and
R is an upper triangular matrix of size
n x n.
💡 You may benefit from the following list of equivalent conditions for
Q being an orthogonal matrix:
- QᵀQ = Id;
- QQᵀ = Id; and
- Q⁻¹ = Qᵀ.
where Qᵀ is the transpose of
Id is the identity matrix. It is also worth remembering that the determinant of an orthogonal matrix satisfies
det(Q) = ±1.
Is the QR factorization unique?
A is invertible and we require the diagonal entries of
R to be positive, then the QR decomposition is unique.
A is a square singular matrix (i.e., non-invertible), then
R is singular too, which means it has some zeros on the diagonal. In such a case, the QR decomposition is not unique.
🙋 If you need a refresher on invertible and non-invertible matrices, check out our inverse matrix calculator.
A is an
m x n (with
m ≥ n) rectangular matrix of full rank (that is, its columns form a set of linearly independent vectors) and we require the diagonal entries of
R to be positive, then the QR decomposition is unique. If
A is not a full-rank matrix, then the QR decomposition is not unique.
How to find the QR decomposition?
There are several methods for performing the QR decomposition of a given matrix
A. The simplest one, which we want to explain here, is via the Gram-Schmidt orthogonalization.
First, we take the columns of
A and subject them to the process of Gram-Schmidt orthogonalization. This results in a collection of orthonormal vectors: e1, e2, ..., en. Form a matrix with these vectors as columns and call this matrix
Well done - you have just found the orthogonal matrix Q from the QR factorization! It remains to find the upper-triangular matrix R. This we can do with the help of the following formula:
⟨v, w⟩ we denote the standard inner (dot) product of vectors
Equivalently, you can determine
R by left-multiplying the original matrix
A by the transpose of our newly-found matrix
Q as follows:
💡 The price we have to pay for the simplicity of this method is numerical instability (that is, it is prone to numerical errors). If you need a numerically stable method, check out the method of Householder reflection (a.k.a. Householder transformation).
How to use this QR decomposition calculator?
As we have seen above, it may be hard to perform the QR decomposition of a given matrix. That's why we've created this QR decomposition calculator! 😀 To use it to factorize a matrix, follow these steps:
Enter the size of the matrix for which you need to determine the QR decomposition: the number of rows and columns.
Remember that for the QR decomposition the number of rows needs to be greater than or equal to the number of columns.
Enter the coefficients of your matrix into the respective fields of our QR decomposition calculator.
Omni's QR decomposition calculator will display the factorization of your matrix.
You can increase the precision of calculations with which this QR decomposition calculator operates. Click the
advanced modebutton and adjust the
precisionfield according to your needs. By default, our QR decomposition calculator displays 3 significant figures.
Applications of the QR decomposition
The QR decomposition has multiple applications. The one we want to discuss here is solving systems of linear equations. Another important field where QR decomposition is often used is in calculating the eigenvalues and eigenvectors of a matrix. This method is known as the QR algorithm or QR iteration.
Now we'll see how the QR factorization procedure can facilitate the task of solving a system of linear equations. Suppose we have to solve the system:
- a11, a12, ..., amn are the coefficients of the system;
- b1, b2, ..., bm are the constant terms; and
- x1, x2, ..., xn are the unknowns.
Our system can be rewritten as:
Ax = b,
So now we have to determine the vector
x given the matrix
A and the vector
m ≥ n and that we have the QR decomposition of
A, i.e., we know the orthogonal matrix
Q and the upper-triangular matrix
R such that
A = QR. This means we have to solve the equation:
QRx = b
Let's multiply both sides of the above equation by
QᵀQRx = Qᵀb
Why have we done that, you may (and should) ask? Now it is more complicated than before, isn't it? At first sight, yes, but recall what we learned about orthogonal matrices back in the first section: they satisfy the property that
QᵀQ = Id. In consequence, the left-hand side simplifies as follows:
QᵀQRx = Rx
and the whole equation now looks like:
Rx = Qᵀb
Since there is a triangular matrix on the left-hand side, this equation is straightforward to solve. An example best shows it, so in the next section, we solve a system of linear equations via the QR factorization.
Example of QR decomposition application
Say we have to solve the following system of linear equations:
w + 3v + 6z = 3
w + 2v + 2z = -1
w + 3v + 8z = 5
w + 2v + 4z = 1
And we want to solve
Ax = b
Using our QR decomposition calculator, we find that
A = QR, where
Using the transformation described in the previous section, we arrive at the system:
We quickly compute
and to find
Qᵀb, we first write down the transpose of
Therefore, we have to solve
Now you can see the power of triangular matrices! Thanks to the fact that
R is upper-triangular, the last equation has an elementary form:
2z = 2
We can easily solve for
z = 1
The penultimate equation is now also simple to solve as it involves only
z (and we already know the value of
v + 4z = 4
Hence, we get
v = 4 - 4z
v = 4 - 4 = 0
There remains only one equation:
2w + 5v + 10z = 4
and, given that
z = 1 and
v = 0, we easily obtain
2w = 4 - 10z
2w = 4 - 10 = -6
w = -3
That's it! We have solved a system of linear equations with the help of QR decomposition! 🥳
How do I calculate the determinant given QR decomposition?
To find the determinant of a matrix
A given a QR decomposition of
A, follow these steps:
A = QR, we have
det(A) = det(Q) × det(R).
- We note that
det(Q) = 1, because
- So we have
det(A) = det(R). Let's focus on
Ris a triangular matrix,
det(R)is the product of its diagonal elements.
- Hence, to find
det(A), you need to multiply the diagonal elements of
R. That's it!
How do I find the QR decomposition?
There are several methods for deriving the QR decomposition of a matrix:
- Gram–Schmidt orthogonalization - this method is easy to understand even with only basic knowledge of linear algebra, but it's numerically unstable and so not very useful in real-life applications.
- Householder transformations - somewhat more complicated yet numerically stable; however, still not the best in terms of the efficiency of calculations.
- Givens rotations - really complicated, numerically stable, and very efficient.
Does a QR decomposition always exist?
Yes, the QR decomposition exists for every matrix (even a non-full rank matrix). However, it may not be unique.