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.
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.
Create a 2D complex tensor of size 2×2 by generating random numbers and compute the determinant.
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.
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.
Here we create a batch of 3 complex tensors of size 2×2 and compute its determinant.
Here is another program where we create a batch of 3 complex tensors of size 4×4 and compute the determinant.