有人能建议一种方法来获得Scipy中秩不足的Gram矩阵的Cholesky分解吗?
我需要保留原始顺序的东西,因此等效于Cholesky LDL分解(全排位情况)。我下面对scipy.linalg.ldl
的尝试给了我不同的订单
import numpy as np
from scipy import linalg
def modified_cholesky(arr):
"""Use Cholesky to produce LDL' factorization of arr."""
chol = linalg.cholesky(arr, lower=True)
d1 = np.diag(np.diag(chol))
L = chol@linalg.inv(d1)
return L, d1
def modified_ldl(arr):
lu, d, perm=linalg.ldl(arr, lower=True)
lu2 = lu[perm,:]
return lu2, np.sqrt(d), perm
# sanity checks
arr=np.array([[ 3., 5.], [ 5., 11.]])
mchol, d = modified_cholesky(arr)
np.testing.assert_allclose(mchol @ d @ d @ mchol.T, arr, rtol=1e-6, atol=1e-7)
mchol2, d2, perm = modified_ldl(arr)
np.testing.assert_allclose(mchol2 @ d2 @ d2 @ mchol2.T, arr[perm,:][:,perm], rtol=1e-6, atol=1e-7)
np.testing.assert_allclose(mchol, mchol2) # fails because linalg.ldl is permuted
modified_cholesky(np.array([[1,1],[1,1]])) # fails with 2-th leading minor of the array is not positive definite