# PyTorch â€“ How to compute QR decomposition of a matrix?

torch.linalg.qr() computes the QR decomposition of a matrix or a batch of matrices. It accepts matrix and batch of matrices of float, double, cfloat and cdouble data types.

It returns a named tuple (Q, R). Q is orthogonal when the matrix is real valued and unitary when matrix is complex valued. And R is an upper triangular matrix.

## Syntax

(Q, R) = torch.linalg.qr(mat, mode='reduced')

### Parameters

• Mat – Square matrix or a batch of square matrices.

• mode – It decides mode of QR decomposition. It is set to one of three modes, 'reduced', 'complete', and 'r'. Default is set to 'reduced'. It's an optional parameter.

## Steps

• Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.

import torch
• Create a matrix or batch of matrices. Here we define a matrix (a 2D torch tensor) of size [3, 2].

mat = torch.tensor([[1.,12.],[14.,5.],[17.,-8.]])
• Compute QR decomposition of the input matrix or batch of matrices using torch.linalg.qr(mat). Here mat is the input matrix.

Q, R = torch.linalg.qr(A)
• Display Q and R.

print("Q:", Q)
print("R:", R)

## Example 1

In this Python program, we compute the QR decomposition of a matrix. We have not given mode parameter. It's set to 'reduced' by default.

# import necessary libraries
import torch

# create a matrix
mat = torch.tensor([[1.,12.],[14.,5.],[17.,-8.]])
print("Matrix:", mat)

# compute QR decomposition
Q, R = torch.linalg.qr(mat)

# print Q and S matrices
print("Q:",Q)
print("R:",R)

## Output

It will produce the following output −

Matrix:
tensor([[ 1., 12.],
[14., 5.],
[17., -8.]])
Q:
tensor([[-0.0454, 0.8038],
[-0.6351, 0.4351],
[-0.7711, -0.4056]])
R:
tensor([[-22.0454, 2.4495],
[ 0.0000, 15.0665]])

## Example 2

In this Python program, we compute the QR decomposition of a matrix. We set mode to 'r'.

# import necessary libraries
import torch

# create a matrix
mat = torch.tensor([[1.,12.],[14.,5.],[17.,-8.]])
print("Matrix:", mat)

# compute QR decomposition
Q, R = torch.linalg.qr(mat, mode = 'r')

# print Q and S matrices
print("Q:",Q)
print("R:",R)

## Output

It will produce the following output −

Matrix:
tensor([[ 1., 12.],
[14., 5.],
[17., -8.]])
Q:
tensor([])
R:
tensor([[-22.0454, 2.4495],
[ 0.0000, 15.0665]])

## Example 3

In this Python3 program we compute the QR decomposition of a matrix. We set mode to 'complete'.

# import necessary libraries
import torch

# create a matrix
mat = torch.tensor([[1.,12.],[14.,5.],[17.,-8.]])
print("Matrix:", mat)

# compute QR decomposition
Q, R = torch.linalg.qr(mat, mode = 'complete')

# print Q and S matrices
print("Q:", Q)
print("R:", R)

## Output

It will produce the following output −

Matrix:
tensor([[ 1., 12.],
[14., 5.],
[17., -8.]])
Q:
tensor([[-0.0454, 0.8038, 0.5931],
[-0.6351, 0.4351, -0.6383],
[-0.7711, -0.4056, 0.4907]])
R:
tensor([[-22.0454, 2.4495],
[ 0.0000, 15.0665],
[ 0.0000, 0.0000]])

Updated on: 07-Jan-2022

204 Views 