Python 对于@Brian Borchers 的建议,尝试 Cholesky 分解:
from sksparse.cholmod import cholesky, CholmodNotPositiveDefiniteError
# https://scikit-sparse.readthedocs.io/en/latest/cholmod.html
def testposdef( A, beta=1e-6, pr="" ):
""" try cholesky( a scipy.sparse matrix + beta I )
Why A + beta I, beta say 1e-6 ?
If A has tiny eigenvalues, 0 to within machine precision,
about half of these "zeros" may be negative -- tough on solvers.
Also the condition number improves to ~ rho(A) / beta.
"""
try:
solve = cholesky( A, beta=beta ) # A + beta I
if pr:
print( "%s + %g I is positive-definite" % (pr, beta ))
return solve # x = solve( b )
except CholmodNotPositiveDefiniteError:
if pr:
print( "%s + %g I is not positive-definite" % (pr, beta ))
return False