import numpy as np
A = np.array([[ 8. , -6. , 2. ],
[-0.5, 8. , -6. ],
[ 0.5, -0.5, 2. ]])
这是一个LU分解(Doolittle的分解)结果。(A = [L \ U])
我想从A中得到L和U
你应该是:
U = np.array([[ 8., -6., 2.],
[ 0., 8., -6.],
[ 0., 0., 2.]])
L应该是:
L = np.array([[ 1. , 0. , 0. ],
[-0.5, 1. , 0. ],
[ 0.5, -0.5, 1.]])
那么,我想知道如何从A获得L和U?
答案 0 :(得分:2)
您不需要任何索引操作。只需使用tril
,triu
和identity
函数:
import numpy as np
A = np.array([[ 8. , -6. , 2. ],
[-0.5, 8. , -6. ],
[ 0.5, -0.5, 2. ]])
U = np.triu(A)
#[[ 8. -6. 2.]
# [-0. 8. -6.]
# [ 0. -0. 2.]]
L = np.tril(A, k=-1) + np.identity(3)
#[[ 1. 0. 0. ]
# [-0.5 1. 0. ]
# [ 0.5 -0.5 1. ]]
答案 1 :(得分:1)
你想要的东西看起来不像LU分解给我, http://en.wikipedia.org/wiki/LU_decomposition
>>> U_ = np.array([[ 8., -6., 2.],
[ 0., 8., -6.],
[ 0., 0., 2.]])
>>> L_ = np.array([[ 1. , 0. , 0. ],
[-0.5, 1. , 0. ],
[ 0.5, -0.5, 1.]])
>>> np.dot(L_, U_)
array([[ 8., -6., 2.],
[ -4., 11., -7.],
[ 4., -7., 6.]])
LU分解在scipy.linalg中可用
>>> A = np.array([[ 8. , -6. , 2. ], [-0.5, 8. , -6. ], [ 0.5, -0.5, 2. ]])
>>> import scipy.linalg as spla
>>> P, L, U = spla.lu(A)
>>> L
array([[ 1. , 0. , 0. ],
[-0.0625 , 1. , 0. ],
[ 0.0625 , -0.01639344, 1. ]])
>>> U
array([[ 8. , -6. , 2. ],
[ 0. , 7.625 , -5.875 ],
[ 0. , 0. , 1.77868852]])
>>> np.dot(L, U)
array([[ 8. , -6. , 2. ],
[-0.5, 8. , -6. ],
[ 0.5, -0.5, 2. ]])