Article directory
scipy.linalg
Provides a series of matrix decomposition functions, the most basic of which must be LU decomposition.
L and U
LU decomposition, that is, the matrix AAA decomposed intoLU LULU , whereLLL is the lower triangular matrix,UUU is an upper triangular matrix. For these two matrices,scipy.linalg
provided inkkthtril, triu
can beAll the elements below or above the k diagonals are set to zero, so that the L matrix or U matrix can be obtained.
import numpy as np
import scipy.linalg as sl
x = np.random.rand(4,4)
print(sl.tril(x,-1)) # 返回见[1]
[ 0. 0. 0. 0. 0.62594216 0. 0. 0. 0.16043717 0.5820587 0. 0. 0.24560828 0.76599572 0.1922379 0. ] \begin{bmatrix} 0.&0.&0.&0.\\ 0.62594216&0.&0.&0.\\ 0.16043717&0.5820587&0.&0.\\ 0.24560828&0.76599572&0.1922379& 0.\\ \end{bmatrix} 0.0.625942160.160437170.245608280.0.0.58205870.765995720.0.0.0.19223790.0.0.0.
print(sl.triu(x,2)) #
[ 0. 0. 0.91943758 0.2531733 0. 0. 0. 0.76514452 0. 0. 0. 0. 0. 0. 0. 0. ] \begin{bmatrix} 0.&0.&0.91943758&0.2531733 \\ 0.&0.&0.&0.76514452\\ 0.&0.&0.&0.\\ 0.&0.&0.&0. \end{bmatrix} 0.0.0.0.0.0.0.0.0.919437580.0.0.0.25317330.765144520.0.
lu decomposition
LU
Decomposition is the first matrix decomposition method to appear in almost any book on matrix algorithms. In scipy.linalg
, functions such as are provided lu, lu_factor, lu_solve
for LU decomposition, respectively, and for solving Ax=b
similar problems by LU decomposition.
LU
The decomposition example is as follows
import numpy as np
from scipy.linalg import lu
A = np.random.rand(4,4)
p, l, u = lu(A)
np.allclose(A - p @ l @ u, np.zeros((4, 4)))
# True
lu
In addition to the parameters a
used for finiteness checks and a
overridability, the function also has a permute_l
default value of False
, when it True
is , two parameters will be returned pl, u
. Among them pl==p@l
.
lu_factor(a)
Return the result of the decomposition in another form LU
. There are two return values, namely , lu, piv
L + U − I L+UILU
L+U−I ,III is the identity matrix;piv
represents the matrixPPThe position of the non-zero element in P.
lu_solve
lu_solve
is according to LU LULU decomposition solvesPLU x − b PLUx-bPLUx−The problem of b , whose input is(lu, piv)
andb
, is tested as follows
>>> b = np.array([1,2,3,4])
>>> lu, pix = sl.lu_factor(A)
>>> x = lu_solve((lu, pix), b)
>>> A@x-b
array([ 2.22044605e-16, -4.44089210e-16, 0.00000000e+00, 0.00000000e+00])
Among them A
is the random matrix generated above, the value is
[ 0.2495995 0.59179571 0.34236803 0.78559552 0.39427709 0.36015762 0.23789732 0.09223244 0.79701282 0.40291763 0.93215531 0.10486747 0.46812908 0.58426202 0.16560106 0.96889267 ] \begin{bmatrix} 0.2495995&0.59179571&0.34236803&0.78559552\\ 0.39427709&0.36015762&0.23789732&0.09223244\\ 0.79701282&0.40291763&0.93215531&0.10486747\\ 0.46812908&0.58426202&0.16560106&0.96889267 \end{bmatrix} 0.24959950.394277090.797012820.468129080.591795710.360157620.402917630.584262020.342368030.237897320.932155310.165601060.785595520.092232440.104867470.96889267