PyTorch – How to compute the inverse of a square matrix?

PyTorchServer Side ProgrammingProgramming

To compute the inverse of a square matrix, we could apply torch.linalg.inv() method. It returns a new tensor with inverse of the given matrix. It accepts a square matrix, a batch of square matrices, and also batches of square matrices.

A matrix is a 2D torch Tensor. It supports input of float, double, cfloat, and cdouble data types. The inverse matrix exists if and only if the square matrix is invertible.

Syntax

torch.linalg.inv(M)

Where M is a square matrix or a batch of square matrices. It returns the inverse matrix.

Steps

We could use the following steps to compute the inverse of a square matrix −

  • Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
  • Define a square matrix. Here, we define a square matrix (2D tensor of size 3×3.

M = torch.tensor([[1.,2., 3.],[1.5, 2., 2.3],[.1, .2, .5]])
  • Compute the inverse of square matrix using torch.linalg.inv(M). M is the square matrix or batch/es of square matrices. Optionally assign this value to a new variable.

M_inv = torch.linalg.inv(M)
  • Print the above computed inverse matrix.

print("Norm:", M_inv)

Let's take a couple of examples to demonstrate how to compute the inverse of a square matrix.

Example 1

# Python program to compute the inverse of a square matrix
# import required library
import torch

# define a 3x3 square matrix
M = torch.tensor([[1.,2., 3.],[1.5, 2., 2.3],[.1, .2, .5]])
print("Matrix M:\n", M)

# compute the inverse of above defined matrix
Minv = torch.linalg.inv(M)
print("Inversr Matrix:\n", Minv)

Output

It will produce the following output −

Matrix M:
   tensor([[1.0000, 2.0000, 3.0000],
      [1.5000, 2.0000, 2.3000],
      [0.1000, 0.2000, 0.5000]])
Inversr Matrix:
   tensor([[ -2.7000, 2.0000, 7.0000],
      [ 2.6000, -1.0000, -11.0000],
      [ -0.5000, 0.0000, 5.0000]])

Example 2

# Python program to compute the inverse of a square matrix
# import required library
import torch

# define a 3x3 square matrix of random complex numbers
M = torch.randn(3,3, dtype = torch.complex128)
print("Matrix M:\n", M)

# compute the inverse of above defined matrix
Minv = torch.linalg.inv(M)
print("Inverse Matrix:\n", Minv)

Output

It will produce the following output −

Matrix M:
   tensor([[ 0.4425-1.4046j, -0.2492+0.7280j, -0.4746-0.4261j],
      [-0.0246-0.4826j, -0.0250-0.3656j, 1.1983-0.4130j],
      [ 0.1904+0.7817j, 0.5823-0.2140j, 0.6129+0.0590j]],
dtype=torch.complex128)
Inversr Matrix:
   tensor([[ 0.3491+0.2565j, -0.2743+0.2843j, 0.4041-0.3382j],
      [ 0.4856-0.6789j, -0.2541+0.0598j, 1.2471-0.5962j],
      [ 0.0221+0.2874j, 0.6732+0.0512j, 0.1537+0.5768j]],
      dtype=torch.complex128)

Example 3

# Python program to compute the inverse of batch of matrices
# import required library
import torch

# define a batch of two 3x3 square matrices
B = torch.randn(2,3,3)
print("Batch of Matrices :\n", B)

# compute the inverse of above defined batch matrices
Binv = torch.linalg.inv(B)
print("Inverse Matrices:\n", Binv)

Output

It will produce the following output −

Batch of Matrices :
   tensor([[[ 1.0002, 0.4318, -0.9800],
      [-1.7990, 0.0913, 0.9440],
      [-0.1339, 0.0824, -0.5501]],

      [[ 0.5289, -0.0909, 0.0354],
      [-0.2159, -0.5417, 0.3659],
      [-0.7216, -0.0669, -0.6662]]])
Inverse Matrices:
   tensor([[[ 0.2685, -0.3290, -1.0427],
      [ 2.3415, 1.4297, -1.7177],
      [ 0.2852, 0.2941, -1.8211]],

      [[ 1.6932, -0.2766, -0.0620],
      [-1.7919, -1.4360, -0.8838],
      [-1.6543, 0.4438, -1.3452]]])
raja
Updated on 07-Jan-2022 06:21:14

Advertisements