How can we check if a matrix is PSD is PyTorch?

1.9k Views Asked by At

There is a poste on checking if a matrix is PSD in Python. I am wondering how we can check it in PyTorch? is there a function for that?

1

There are 1 best solutions below

2
On BEST ANSWER

Haven't found a PyTorch function for that, but you should be able to determine it easily, and similarly to the post you've linked, by checking whether the matrix is symmetric and all eigenvalues are non-negative:

def is_psd(mat):
    return bool((mat == mat.T).all() and (torch.linalg.eigvals(mat).real>=0).all())
#Test:
is_psd(torch.randn(2,2))