Find out if matrix is positive definite with numpy

后端 未结 8 2175
慢半拍i
慢半拍i 2020-12-23 13:40

I need to find out if matrix is positive definite. My matrix is numpy matrix. I was expecting to find any related method in numpy library, but no success. I appreciate any

8条回答
  •  孤独总比滥情好
    2020-12-23 13:46

    For a real matrix $A$, we have $x^TAx=\frac{1}{2}(x^T(A+A^T)x)$, and $A+A^T$ is symmetric real matrix. So $A$ is positive definite iff $A+A^T$ is positive definite, iff all the eigenvalues of $A+A^T$ are positive.

    import numpy as np
    
    def is_pos_def(A):
        M = np.matrix(A)
        return np.all(np.linalg.eigvals(M+M.transpose()) > 0)
    

提交回复
热议问题