Find determinant of a complex matrix in PyTorch
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 libraryimport torch# create 2x2 complex matrix using# random numbersmat = torch.randn(2,2, dtype = torch.cfloat)# display the matrixprint("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 2×2 and compute its determinant.
Python3
# import libraryimport torch# create real parts the complex numbersreal = torch.tensor([[1, 2],[5,6]], dtype=torch.float32)# create imaginary parts of the complex numbersimag = 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 libraryimport torch# create a real tensormat = torch.randn(3,3,2)# display the real tensorprint("Real Tensor: \n",mat)# convert the above real tensor# into complex tensormat = 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 2×2 and compute its determinant.
Python3
# import libraryimport torch# create matrixmat = torch.randn(3,2,2, dtype = torch.cfloat)# display matrixprint("Batch of Complex Matrices: \n", mat)# Compute the determinantdet = 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 libraryimport torch# create complex matrixmat = torch.randn(3,4,4, dtype = torch.cfloat)# display matrixprint("Batch of Complex Matrices: \n", mat)# Compute the determinantdet = torch.linalg.det(mat)print("Determinant: \n", det) |
Output:






Please Login to comment...