Find determinant of a complex matrix in PyTorch
Last Updated :
08 Oct, 2021
Linear Algebra module torch.linalg of PyTorch provides the function torch.linalg.det() to compute the determinant of a square(real or complex) matrix. This function also computes the determinant of each square matrix in a batch.
Syntax:
torch.linalg.det(input) --> Tensor
input is an (n, n) matrix or a batch of matrices of size (b, n, n) where b is the batch dimension (number of matrices in a batch). This input must be a tensor. This function returns a tensor of the determinant value/s.
Note: This function supports float, double, cfloat, and cdouble dtypes.
Let's first look at how to create a complex matrix in PyTorch and then compute the determinant of the matrix. To create a complex matrix in PyTorch, we use 2D Complex Tensors. We can also create a batch of complex matrices. The supported complex dtypes are torch.cfloat and torch.cdouble.
Below are some examples to create a complex matrix (two-dimensional Complex Tensor) and finding their determinant.
Example 1:
Create a 2D complex tensor of size 2x2 by generating random numbers and compute the determinant.
Python3
# import library
import torch
# create 2x2 complex matrix using
# random numbers
mat = torch.randn(2,2, dtype = torch.cfloat)
# display the matrix
print("Complex Matrix: \n", mat)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(mat)
print("Determinant: \n", det)
Output:
Example 2:
Another way to create a complex matrix in PyTorch is to use torch.complex(real, imag). This creates a complex tensor with real and imaginary parts as real and imag. Both real and imag must be float or double and imag must be the same type as real. In the below program we create a 2D complex tensor of size 2x2 and compute its determinant.
Python3
# import library
import torch
# create real parts the complex numbers
real = torch.tensor([[1, 2],[5,6]], dtype=torch.float32)
# create imaginary parts of the complex numbers
imag = torch.tensor([[3, 4],[8,9]], dtype=torch.float32)
# create complex tensor (matrix)
z = torch.complex(real, imag)
print("Complex Matrix: \n", z)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(z)
print("Determinant: \n", det)
Output:
Example 3:
Complex tensors can also be created from already existed real tensors. Real tensors of shape (..., 2) can easily be converted into complex tensors using torch.view_as_complex() and their determinant can be computed using torch.linalg.det() method.
Python3
# import library
import torch
# create a real tensor
mat = torch.randn(3,3,2)
# display the real tensor
print("Real Tensor: \n",mat)
# convert the above real tensor
# into complex tensor
mat = torch.view_as_complex(mat)
# display the complex tensor (matrix)
print("Complex Tensor (Matrix): \n", mat)
# Compute the determinant of Matrix
# using torch.linalg.det()
det = torch.linalg.det(mat)
print("Determinant: \n", det)
Output:
Example 4:
Here we create a batch of 3 complex tensors of size 2x2 and compute its determinant.
Python3
# import library
import torch
# create matrix
mat = torch.randn(3,2,2, dtype = torch.cfloat)
# display matrix
print("Batch of Complex Matrices: \n", mat)
# Compute the determinant
det = torch.linalg.det(mat)
print("Determinant: \n", det)
Output:
Example 5:
Here is another program where we create a batch of 3 complex tensors of size 4x4 and compute the determinant.
Python3
# import library
import torch
# create complex matrix
mat = torch.randn(3,4,4, dtype = torch.cfloat)
# display matrix
print("Batch of Complex Matrices: \n", mat)
# Compute the determinant
det = torch.linalg.det(mat)
print("Determinant: \n", det)
Output:
Similar Reads
How to find the Determinant of a Matrix? Answer: The following steps are to be followed to find the Determinant of a Matrix Select any row or column. To find the determinant, we normally start with the first row.Determine the co-factors of each of the row/column items that we picked in Step 1.Multiply the row/column items from Step 1 by th
6 min read
How to compute QR decomposition of a matrix in Pytorch? In this article, we are going to discuss how to compute the QR decomposition of a matrix in Python using PyTorch. torch.linalg.qr() method accepts a matrix and a batch of matrices as input. This method also supports the input of float, double, cfloat, and cdouble data types. It will return a named t
2 min read
Determinant of 4x4 Matrix | Examples and How to Find Determinant of 4x4 Matrix: Determinant of a Matrix is a fundamental concept in linear algebra, essential for deriving a single scalar value from the matrix. 4x4 is a square matrix with 4 rows and 4 columns whose determinant can be found by a formula which we will discuss. This article will explore t
15+ min read
Compute the determinant of a given square array using NumPy in Python In Python, the determinant of a square array can be easily calculated using the NumPy package. This package is used to perform mathematical calculations on single and multi-dimensional arrays. numpy.linalg is an important module of NumPy package which is used for linear algebra. We can use det() fun
2 min read
Determinant of 2x2 Matrix Determinant of a 2x2 Matrix A = \begin{bmatrix} a&b \\ c&d \\ \end{bmatrix} is denoted as |A| and is calculated as |A| = [ad - bc]. It is used in solving various problems related to a matrix and is used in finding the Inverse, and Rank of 2Ã2 Matrix.In this article, we will learn about, the
7 min read