I have a nonsymmetric real matrix that I want to calculate the eigenvalues and matrices for. I use the torch.eig() method which works.
But my eigenvalues are complex and I am unsure of how to handle this.
I am using the following to go from eigenvalues/vectors and back.
X = torch.rand(20,20)
D,T = torch.eig(X, eigenvectors=True)
But because D consists of complex eigenvalues, and in PyTorch a complex value is a vector of two values, I am unable to calculate the original matrix again using matrix multiplication. How will I be able to do that? I know torch.diag(D) results in a vector because D is a matrix, but how are you supposed to multiple a complex matrix in PyTorch?
Pytorch does not currently support complex tensor operations (to
the best of my knowledge).
First, what is your actual use case? There may be some other way
to accomplish your goal.
Second, for work on complex tensors in pytorch, please see this
and the linked github issue:
Last, for a number of operations, including matrix multiplication, it is
easy enough to build your own complex tensor operations out of
real tensor operations.
You can write cm = rm + I * im, where cm is your complex matrix, rm and im are real matrices, and I is the notional sqrt (-1) (the
“imaginary unit”). You can then expand out the product of two complex
matrices in terms of these real matrices, and collect terms to get the
real and imaginary parts of your product matrix. (In your example of
reconstructing the real matrix that you diagonalized you should find
that the imaginary part of your final result is zero to within floating-point
I am using the complex eigenvalues for a low-rank approximation of the singular values for my matrix.
The algorithm I am using has an intermediate step of calculating the eigenvalues/vectors of a much smaller matrix.
In this step I actually have to take the square root of the diagonal matrix containing the eigenvalues.
But thank you very much, I will see if I can expand this into something that can be calculated using only real valued matrices.
EDIT: Later on I have figured out I am in fact calculating the eigenvalues for a PSD matrix, so the complex values are small enough to be ignored.