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 2×2 by generating random numbers and compute the determinant.
Python3
import torch
mat = torch.randn( 2 , 2 , dtype = torch.cfloat)
print ( "Complex Matrix: \n" , mat)
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 2×2 and compute its determinant.
Python3
import torch
real = torch.tensor([[ 1 , 2 ],[ 5 , 6 ]], dtype = torch.float32)
imag = torch.tensor([[ 3 , 4 ],[ 8 , 9 ]], dtype = torch.float32)
z = torch. complex (real, imag)
print ( "Complex Matrix: \n" , z)
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 torch
mat = torch.randn( 3 , 3 , 2 )
print ( "Real Tensor: \n" ,mat)
mat = torch.view_as_complex(mat)
print ( "Complex Tensor (Matrix): \n" , mat)
det = torch.linalg.det(mat)
print ( "Determinant: \n" , det)
|
Output:
Example 4:
Here we create a batch of 3 complex tensors of size 2×2 and compute its determinant.
Python3
import torch
mat = torch.randn( 3 , 2 , 2 , dtype = torch.cfloat)
print ( "Batch of Complex Matrices: \n" , mat)
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 4×4 and compute the determinant.
Python3
import torch
mat = torch.randn( 3 , 4 , 4 , dtype = torch.cfloat)
print ( "Batch of Complex Matrices: \n" , mat)
det = torch.linalg.det(mat)
print ( "Determinant: \n" , det)
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...