Numpy中有built-in/easy LDU分解方法吗?

Is there a built-in/easy LDU decomposition method in Numpy?

我在 numpy.linalg.cholesky 中看到了 cholesky 分解,但找不到 LDU 分解。任何人都可以建议使用的功能吗?

Scipy 有一个 LU 分解函数:scipy.linalg.lu. Note that this also introduces a permutation matrix P into the mix. This answer 很好地解释了为什么会发生这种情况。

如果你特别需要LDU,那么你可以直接对U矩阵进行归一化来提取D

您可以这样做:

>>> import numpy as np
>>> import scipy.linalg as la
>>> a = np.array([[2, 4, 5],
                  [1, 3, 2],
                  [4, 2, 1]])
>>> (P, L, U) = la.lu(a)
>>> P
array([[ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 1.,  0.,  0.]])
>>> L
array([[ 1.        ,  0.        ,  0.        ],
       [ 0.5       ,  1.        ,  0.        ],
       [ 0.25      ,  0.83333333,  1.        ]])
>>> U
array([[ 4. ,  2. ,  1. ],
       [ 0. ,  3. ,  4.5],
       [ 0. ,  0. , -2. ]])
>>> D = np.diag(np.diag(U))   # D is just the diagonal of U
>>> U /= np.diag(U)[:, None]  # Normalize rows of U
>>> P.dot(L.dot(D.dot(U)))    # Check
array([[ 2.,  4.,  5.],
       [ 1.,  3.,  2.],
       [ 4.,  2.,  1.]])

试试这个:

import numpy as np

A = np.array([[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])
U = np.triu(A,1)
L = np.tril(A,-1)
D = np.tril(np.triu(A))
print(A)
print(L)
print(D)
print(U)