I have a matrix A
and I want 2 matrices U
and L
such that U
contains the upper triangular elements of A (all elements above and not including diagonal) and similarly for L
(all elements below and not including diagonal). Is there a numpy
method to do this?
我有一个矩阵A,我想要2个矩阵U和L,使得U包含A的上三角形元素(上面的所有元素,不包括对角线),L类似(下面的所有元素,不包括对角线)。有一个numpy方法来做到这一点?
e.g
例如
A = array([[ 4., 9., -3.],
[ 2., 4., -2.],
[-2., -3., 7.]])
U = array([[ 0., 9., -3.],
[ 0., 0., -2.],
[ 0., 0., 0.]])
L = array([[ 0., 0., 0.],
[ 2., 0., 0.],
[-2., -3., 0.]])
3 个解决方案
#1
46
Try numpy.triu
(triangle-upper) and numpy.tril
(triangle-lower).
尝试numpy.triu(三角形 - 上部)和numpy.tril(三角形 - 下部)。
#2
15
To extract the upper triangle values to a flat vector, you can do something like the following:
要将上三角形值提取到平面矢量,您可以执行以下操作:
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
a[np.triu_indices(3)]
#or
list(a[np.triu_indices(3)])
Similarly, for the lower triangle, use
同样,对于下三角形,请使用
np.tril
np.tril
IMPORTANT
If you want to extract the values that are above the diagonal (or below) then use the k argument. This is usually used when the matrix is symmetric.
如果要提取对角线(或以下)以上的值,请使用k参数。这通常在矩阵对称时使用。
Example:
例:
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a[np.triu_indices(3, k = 1)]
# this returns the following
array([2, 3, 6])
#3
8
Use the Array Creation Routines of numpy.triu and numpy.tril to return a copy of a matrix with the elements above or below the k-th diagonal zeroed.
使用numpy.triu和numpy.tril的Array Creation Routines返回矩阵的副本,其中第k个对角线上方或下方的元素归零。
>>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> tri_upper_diag = np.triu(a, k=0)
>>> tri_upper_diag
array([[1, 2, 3],
[0, 5, 6],
[0, 0, 9]])
>>> tri_upper_no_diag = np.triu(a, k=1)
>>> tri_upper_no_diag
array([[0, 2, 3],
[0, 0, 6],
[0, 0, 0]])
>>> tri_lower_diag = np.tril(a, k=0)
>>> tri_lower_diag
array([[1, 0, 0],
[4, 5, 0],
[7, 8, 9]])
>>> tri_lower_no_diag = np.tril(a, k=-1)
>>> tri_lower_no_diag
array([[0, 0, 0],
[4, 0, 0],
[7, 8, 0]])
#1
46
Try numpy.triu
(triangle-upper) and numpy.tril
(triangle-lower).
尝试numpy.triu(三角形 - 上部)和numpy.tril(三角形 - 下部)。
#2
15
To extract the upper triangle values to a flat vector, you can do something like the following:
要将上三角形值提取到平面矢量,您可以执行以下操作:
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(a)
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
a[np.triu_indices(3)]
#or
list(a[np.triu_indices(3)])
Similarly, for the lower triangle, use
同样,对于下三角形,请使用
np.tril
np.tril
IMPORTANT
If you want to extract the values that are above the diagonal (or below) then use the k argument. This is usually used when the matrix is symmetric.
如果要提取对角线(或以下)以上的值,请使用k参数。这通常在矩阵对称时使用。
Example:
例:
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
a[np.triu_indices(3, k = 1)]
# this returns the following
array([2, 3, 6])
#3
8
Use the Array Creation Routines of numpy.triu and numpy.tril to return a copy of a matrix with the elements above or below the k-th diagonal zeroed.
使用numpy.triu和numpy.tril的Array Creation Routines返回矩阵的副本,其中第k个对角线上方或下方的元素归零。
>>> a = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> tri_upper_diag = np.triu(a, k=0)
>>> tri_upper_diag
array([[1, 2, 3],
[0, 5, 6],
[0, 0, 9]])
>>> tri_upper_no_diag = np.triu(a, k=1)
>>> tri_upper_no_diag
array([[0, 2, 3],
[0, 0, 6],
[0, 0, 0]])
>>> tri_lower_diag = np.tril(a, k=0)
>>> tri_lower_diag
array([[1, 0, 0],
[4, 5, 0],
[7, 8, 9]])
>>> tri_lower_no_diag = np.tril(a, k=-1)
>>> tri_lower_no_diag
array([[0, 0, 0],
[4, 0, 0],
[7, 8, 0]])