本文介绍了Numpy中是否有内置/简易的LDU分解方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在numpy.linalg.cholesky中看到cholesky分解,但是找不到LDU分解.有人可以建议要使用的功能吗?
I see cholesky decomposition in numpy.linalg.cholesky, but could not find a LDU decompositon. Can anyone suggest a function to use?
推荐答案
Scipy具有LU分解功能: scipy.linalg.lu
.请注意,这还会在混合中引入置换矩阵P
. 此答案很好地解释了为什么会发生这种情况.
Scipy has an LU decomposition function: scipy.linalg.lu
. Note that this also introduces a permutation matrix P
into the mix. This answer gives a nice explanation of why this happens.
如果您特别需要LDU,则只需将U
矩阵标准化以提取D
.
If you specifically need LDU, then you can just normalize the U
matrix to pull out D
.
这是您可能的操作方式:
Here's how you might do it:
>>> 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.]])
这篇关于Numpy中是否有内置/简易的LDU分解方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!