# QR Decomposition Calculator

Created by Anna Szczepanek, PhD
Reviewed by Dominik Czernia, PhD and Jack Bowater
Last updated: Jan 18, 2024

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?

Scroll down!

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

where,

• Q is an n x n orthogonal 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
• R is an n x n upper 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 Q and 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?

If A is invertible and we require the diagonal entries of R to be positive, then the QR decomposition is unique.

If 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.

Likewise, if 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 Q:

$\small Q = [e_1, \ldots, e_n]$

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:

$\footnotesize R = \begin{bmatrix} \langle e_1, a_1\rangle &\langle e_1, a_2\rangle &\langle e_1, a_3\rangle & \ldots \\ 0 & \langle e_2, a_2\rangle & \langle e_2, a_3 \rangle & \ldots \\ 0 & 0& \langle e_3, a_3 \rangle & \ldots \\ \vdots & \vdots & \vdots & \ddots\\ \end{bmatrix}$

By ⟨v, w⟩ we denote the standard inner (dot) product of vectors v and w.

Equivalently, you can determine R by left-multiplying the original matrix A by the transpose of our newly-found matrix Q as follows:

$\small R = Q^TA$

💡 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:

1. 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.

2. Enter the coefficients of your matrix into the respective fields of our QR decomposition calculator.

3. Omni's QR decomposition calculator will display the factorization of your matrix.

4. You can increase the precision of calculations with which this QR decomposition calculator operates. Click the advanced mode button and adjust the precision field 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:

\small \begin{align*} a_{11}x_1 + a_{12}x_2 + \ldots + a_{1n}x_n & = b_1 \\ a_{21}x_1 + a_{22}x_2 + \ldots + a_{2n}x_n & = b_2 \\ \\ a_{m1}x_1 + a_{m2}x_2 + \ldots + a_{mn}x_n & = b_m \\ \end{align*}

where,

• 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,

where,

$\small A = \begin{bmatrix} a_{11} & a_{12} & \ldots & a_{1n}\\ a_{21} & a_{22} & \ldots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \ldots & a_{mn} \end{bmatrix}$
$\small x = \begin{bmatrix} x_{1} \\ x_{2} \\ \vdots \\ x_{n}\end{bmatrix}$
$\small b = \begin{bmatrix} b_{1} \\ b_{2} \\ \vdots \\ b_{m}\end{bmatrix}$

So now we have to determine the vector x given the matrix A and the vector b.

Assume that 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ᵀ:

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

with

$\small A = \begin{bmatrix} 1& 3& 6 \\ 1& 2& 2 \\ 1& 3& 8 \\ 1& 2 &4 \end{bmatrix}$
$\small b = \begin{bmatrix} 3 \\ -1 \\ 5 \\ 1 \end{bmatrix}$
$\small x = \begin{bmatrix} w \\ v \\ z \end{bmatrix}$

Using our QR decomposition calculator, we find that A = QR, where

$\small Q^T = \begin{bmatrix} 0.5& 0.5& -0.5 \\ 0.5& -0.5& -0.5\\ 0.5& 0.5& 0.5 \\ 0.5& -0.5& 0.5 \end{bmatrix}$
$\small R = \begin{bmatrix} 2& 5& 10 \\ 0 &1& 4 \\ 0& 0 &2 \end{bmatrix}$

Using the transformation described in the previous section, we arrive at the system:

$\small Rx = Qᵀb$

We quickly compute Rx as

$\small Rx = \begin{bmatrix} 2w + 5v + 10z\\ v + 4z \\ 2z \end{bmatrix}$

and to find Qᵀb, we first write down the transpose of Q:

$\small Q^T = \begin{bmatrix} 0.5 & 0.5& 0.5& 0.5 \\ 0.5 &-0.5& 0.5& -0.5 \\ -0.5 &-0.5& 0.5& 0.5 \end{bmatrix}$

and then

$\small Q^Tb = \begin{bmatrix} 4 \\ 4 \\ 2 \end{bmatrix}$

Therefore, we have to solve

$\small \begin{bmatrix} 2w + 5v + 10z\\ v + 4z \\ 2z \end{bmatrix} = \begin{bmatrix} 4 \\ 4 \\ 2 \end{bmatrix}$

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, getting:

z = 1

The penultimate equation is now also simple to solve as it involves only v and z (and we already know the value of z!):

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! 🥳

## FAQ

### 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:

1. Since A = QR, we have det(A) = det(Q) × det(R).
2. We note that det(Q) = 1, because Q is unitary.
3. So we have det(A) = det(R). Let's focus on R.
4. As R is a triangular matrix, det(R) is the product of its diagonal elements.
5. 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.

Anna Szczepanek, PhD
Matrix size
Number of rows
Number of columns
A = a₁a₂
b₁b₂
First row
a₁
a₂
Second row
b₁
b₂
People also viewed…

### BMR - Harris-Benedict equation

Harris-Benedict calculator uses one of the three most popular BMR formulas. Knowing your BMR (basal metabolic weight) may help you make important decisions about your diet and lifestyle.

### Car crash force

With this car crash calculator, you can find out how dangerous car crashes are.

### Floor function

With the floor function calculator you can discover how this function works and why it is useful.

### NOR

The NOR calculator enables you to find the NOR output for a given set of inputs.