scipy.linalg.cholesky
默认情况下为您提供上三角分解,而np.linalg.cholesky
为您提供下三角版本。从文档中scipy.linalg.cholesky
:
cholesky(a, lower=False, overwrite_a=False)
Compute the Cholesky decomposition of a matrix.
Returns the Cholesky decomposition, :math:`A = L L^*` or
:math:`A = U^* U` of a Hermitian positive-definite matrix A.
Parameters
----------
a : ndarray, shape (M, M)
Matrix to be decomposed
lower : bool
Whether to compute the upper or lower triangular Cholesky
factorization. Default is upper-triangular.
overwrite_a : bool
Whether to overwrite data in `a` (may improve performance).
例如:
>>> scipy.linalg.cholesky([[1,2], [1,9]])
array([[ 1. , 2. ],
[ 0. , 2.23606798]])
>>> scipy.linalg.cholesky([[1,2], [1,9]], lower=True)
array([[ 1. , 0. ],
[ 1. , 2.82842712]])
>>> np.linalg.cholesky([[1,2], [1,9]])
array([[ 1. , 0. ],
[ 1. , 2.82842712]])
如果我修改您的代码以两次使用相同的随机矩阵并改为使用linalg.cholesky(C,lower=True)
,那么我会得到如下答案:
>>> Xnp
array([ 79621.02629287+0.j, 78060.96077912+0.j, 77110.92428806+0.j, ...,
75526.55192199+0.j, 77110.92428806+0.j, 78060.96077912+0.j])
>>> Xsp
array([ 79621.02629287+0.j, 78060.96077912+0.j, 77110.92428806+0.j, ...,
75526.55192199+0.j, 77110.92428806+0.j, 78060.96077912+0.j])
>>> np.allclose(Xnp, Xsp)
True