Find out if matrix is positive definite with numpy

后端 未结 8 2163
慢半拍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

    To illustrate @NPE's answer with some ready-to-use code:

    import numpy as np
    
    def is_pd(K):
        try:
            np.linalg.cholesky(K)
            return 1 
        except np.linalg.linalg.LinAlgError as err:
            if 'Matrix is not positive definite' in err.message:
                return 0
            else:
                raise 
    

提交回复
热议问题