How to find the QR Factorization of Matrix using Numpy?

Numpy Linalgn Qr

Working with matrices is always fascinating. The numpy library has a ton of functions that helps with carrying out complicated calculations using matrices. In this article, we shall demonstrate one such function that decomposes a given matrix into a pair of entities. This process is called factorization of a matrix & the function of interest would be the linalg.qr( ) function.

Before exploring this very function, there is something that needs to be done. The following code shall be deployed to import the numpy library.

import numpy as np

Thereafter, we shall explore further the linalg.qr( ) function through each of the following sections.

  • The linalg.qr( ) function – explained
  • Syntax of the linalg.qr( ) function
  • Use cases for the linalg.qr( ) function

Also read: NumPy linalg.matrix_power: Computing the power of a square matrix

The linalg.qr( ) function – explained

QR factorization is a technique through which a matrix is ripped apart into two different entities. Let’s say, we have a matrix ‘X’, then applying this technique shall result in an orthogonal matrix denoted by ‘Q’ and an upper triangular matrix denoted by ‘R’. To put it in simple terms,

X = Q x R

Since the results are derivatives of the input matrix, one can observe that the number of elements in each row of the matrix ‘Q’ would be equal to the number of elements in each column of the matrix ‘R’. This seems to be the case due to the rudimentary rule that demands such an arrangement, only through which matrix multiplication is possible.

Also read: NumPy linalg.det – Compute the determinant of the given array

Syntax of the linalg.qr( ) function

Given below is the syntax of the linalg.qr( ) function with its different components required for its functioning. It is to be noted that the mode can be declared with a number of different options such as reduced, complete, r or raw. The mode is set by default to return the results of the reduced type.

For an input matrix of dimension MxN, the reduced mode results in Q and R matrices of dimensions MxK & KxN respectively. If the complete mode is being used for instance, then the Q and R matrices shall be returned with dimensions MxM & MxN respectively.

Declaring an r mode shall return only the R matrix with KxN dimension. If one chooses the raw mode h & tau are returned with the dimensions NxM & K respectively.

numpy.linalg.qr(x, mode)
  • x – Matrix for which the QR factorization is to be done
  • mode – Type of factorization that is to be done with the input matrix viz. reduced or complete or r or raw

Use cases for linalg.qr( ) function

The QR factorization of the input using each mode shall be demonstrated in this section. Let’s construct a matrix of dimension 4×4 and use the default mode (i.e) reduced to deduce the Q & R matrices. Following is the code and its corresponding result.

m1 = np.array([[-2, 5, 9, 0],
               [0, 8, 1, -6],
               [3, -5, 2, 7],
               [4, 9, 0, -8]])
(q,r) = np.linalg.qr(m1)
Results For Reduced Mode
Results for default (reduced) mode

Now the complete mode shall be used to factorize an input matrix of dimensions 2×3 through the below code.

m2 = np.array([[-2, 5, 0],
               [0, 1, -6]])
(q,r) = np.linalg.qr(m2, mode = "complete")
Results For Complete Mode
Results for complete mode

It could be observed that the dimension of Q is 2×2, which ain’t a surprise when the number of rows in the input matrix is ‘2’. It is time to use the r mode for the same input matrix as used above & see what happens.

np.linalg.qr(m2, mode = "r")
Results For R Mode
Results for r mode

It could be observed that the R matrix in both complete & r modes is the same. Now let’s try raw mode.

np.linalg.qr(m2, mode = "raw")
Results For Raw Mode
Results for raw mode

Of the results given above, the former is ‘h’ & the latter is ‘tau’.


Now that we have reached the end of this article, hope it has elaborated on how to use the linalg.qr( ) function from the numpy library. Here’s another article that details the usage of the i0( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Carpe diem!