简体   繁体   English

函数scipy.linalg.lu中的上三角矩阵是否总是呈行梯形形式?

[英]Is the upper triangular matrix in function scipy.linalg.lu always in row echelon form?

I have amxn matrix A, with n > m, and I am trying to identify independent rows by means of the row echelon form of it. 我有axn矩阵A,其中n> m,并且我试图通过它的行梯形形式识别独立的行。 Function scipy.linalg.lu returns a PLU factorization of my matrix, but U factor does not seem to be in echelon form, ie, pivots are not in a staircase pattern. 函数scipy.linalg.lu返回矩阵的PLU分解,但是U因子似乎不是梯形形式,即,枢轴不是阶梯形。 As far as I know, U factor should always be in a staircase pattern. 据我所知,U因子应始终呈阶梯状。

Consider the following example: 考虑以下示例:

from numpy import array
from scipy.linalg import lu

A = array([[1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
           [1, 1, 0, 0, 1, 0, 1, 0, 1, 1],
           [1, 1, 0, 0, 0, 1, 1, 0, 0, 0],
           [0, 1, 0, 1, 1, 0, 1, 0, 0, 1],
           [1, 1, 0, 0, 1, 1, 1, 1, 1, 1]])

P, L, U = lu(A)

The U factor is not in row echelon form. U因子不是行梯形形式。 For each row k, pivots should always be to the right of the pivot in row k-1. 对于每行k,枢轴应始终位于行k-1中枢轴的右侧。 See that pivot in fifth row is not to the right of pivot in fourth row: 看到第五行的枢轴不在第四行的枢轴的右侧:

array([[ 1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  0.,  1.],
       [ 0.,  0., -1., -1.,  0.,  0.,  0., -1., -1.,  0.],
       [ 0.,  0.,  0.,  0.,  1., -1.,  0.,  0.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  1.,  1.]])

I've never seen the LUP decomposition before but I suspect it just doesn't do exactly what you think it does. 我以前从未见过LUP分解,但我怀疑它并没有完全按照您的想法进行。 I did the same decomposition in Matlab as you did above in Python and I got the exact same results you did. 我在Matlab中所做的分解与您在Python中所做的分解相同,并且得到的结果完全相同。 So I don't think it as problem with the Python LU function. 因此,我不认为Python LU函数有问题。

Update: I have also implemented this in R (code below) and again the U matrix gives the same results as Matlab and Python. 更新:我也用R(下面的代码)实现了这一点,并且U矩阵给出的结果与Matlab和Python相同。 Unlike the others it gives the following warning: 与其他不同,它给出以下警告:

Warning message:
In .local(x, ...) :
  Exact singularity detected during LU decomposition: U[i,i]=0, i=4.

Matlab: Matlab:

Code: 码:

A = ([[1, 1, 1, 1, 0, 1, 1, 1, 1, 0],
           [1, 1, 0, 0, 1, 0, 1, 0, 1, 1],
           [1, 1, 0, 0, 0, 1, 1, 0, 0, 0],
           [0, 1, 0, 1, 1, 0, 1, 0, 0, 1],
           [1, 1, 0, 0, 1, 1, 1, 1, 1, 1]]);
[L,U,P] = lu(A);
U

Matlab results: Matlab结果:
U = U =

     1     1     1     1     0     1     1     1     1     0
     0     1     0     1     1     0     1     0     0     1
     0     0    -1    -1     0     0     0    -1    -1     0
     0     0     0     0     1    -1     0     0     1     1
     0     0     0     0     1     0     0     1     1     1

Python Results (from your code): Python结果(来自您的代码):

U = 

array([[ 1.,  1.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  0.],
       [ 0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  0.,  1.],
       [ 0.,  0., -1., -1.,  0.,  0.,  0., -1., -1.,  0.],
       [ 0.,  0.,  0.,  0.,  1., -1.,  0.,  0.,  1.,  1.],
       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  1.,  1.]])

R: R:

Code: 码:

library(Matrix)
A <- Matrix( c( 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 , 1, 1, 0, 0, 1, 0, 1, 0, 1, 1 , 1, 1, 0, 0, 0, 1, 1, 0, 0, 0 , 0, 1, 0, 1, 1, 0, 1, 0, 0, 1 , 1, 1, 0, 0, 1, 1, 1, 1, 1, 1 ),nrow=5,ncol=10,byrow=TRUE )
expand(mylu <-lu(A))

Results: 结果:

class "dtrMatrix" (unitriangular)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    .    .    .    .
[2,]    0    1    .    .    .
[3,]    1    0    1    .    .
[4,]    1    0    1    1    .
[5,]    1    0    1    0    1

$U
5 x 10 Matrix of class "dgeMatrix"
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    0    1    1    1    1     0
[2,]    0    1    0    1    1    0    1    0    0     1
[3,]    0    0   -1   -1    0    0    0   -1   -1     0
[4,]    0    0    0    0    1   -1    0    0    1     1
[5,]    0    0    0    0    1    0    0    1    1     1

$P
5 x 5 sparse Matrix of class "pMatrix"

[1,] | . . . .
[2,] . . . | .
[3,] . . | . .
[4,] . | . . .
[5,] . . . . |

There is no guarantee that the LU decomposition will result in a U matrix in row echelon form. 不能保证LU分解将导致形成行梯形形式的U矩阵。 It may be that the reason why it fails in this case is because the matrix is singular; 在这种情况下失败的原因可能是因为矩阵是奇异的。 I strongly suspect that is the reason, because one of the characteristics of a singular matrix is the absence of a complete set of pivots. 我强烈怀疑这是原因,因为奇异矩阵的特征之一是缺少一整套枢轴。

See the section on General Matrices at https://en.wikipedia.org/wiki/LU_decomposition and the reference provided therein for specifics. 有关详细信息,请参见https://en.wikipedia.org/wiki/LU_decomposition上有关“通用矩阵”的部分以及其中提供的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM