Computes the LU decomposition for a matrix. The form of the
command depends on the type of the argument. For full (non-sparse)
matrices, the primary form for lu
is
[L,U,P] = lu(A),
where L
is lower triangular, U
is upper triangular, and
P
is a permutation matrix such that L*U = P*A
. The second form is
[V,U] = lu(A),
where V
is P'*L
(a row-permuted lower triangular matrix),
and U
is upper triangular. For sparse, square matrices,
the LU decomposition has the following form:
[L,U,P,Q,R] = lu(A),
where A
is a sparse matrix of either double
or dcomplex
type.
The matrices are such that L*U=P*R*A*Q
, where L
is a lower triangular
matrix, U
is upper triangular, P
and Q
are permutation vectors
and R
is a diagonal matrix of row scaling factors. The decomposition
is computed using UMFPACK for sparse matrices, and LAPACK for dense
matrices.
First, we compute the LU decomposition of a dense matrix.
--> a = float([1,2,3;4,5,8;10,12,3]) a = <float> - size: [3 3] Columns 1 to 3 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000 8.0000000 10.000000 12.000000 3.0000000 --> [l,u,p] = lu(a) l = <float> - size: [3 3] Columns 1 to 3 1.0000000 0.00000000 0.00000000 0.10000000 1.0000000 0.00000000 0.40000001 0.24999994 1.0000000 u = <float> - size: [3 3] Columns 1 to 3 10.000000 12.000000 3.0000000 0.00000000 0.79999995 2.7000000 0.00000000 0.00000000 6.1250005 p = <float> - size: [3 3] Columns 1 to 3 0.00000000 0.00000000 1.0000000 1.0000000 0.00000000 0.00000000 0.00000000 1.0000000 0.00000000 --> l*u ans = <float> - size: [3 3] Columns 1 to 3 10.000000 12.000000 3.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000 8.0000000 --> p*a ans = <float> - size: [3 3] Columns 1 to 3 10.000000 12.000000 3.0000000 1.0000000 2.0000000 3.0000000 4.0000000 5.0000000 8.0000000
Now we repeat the exercise with a sparse matrix, and demonstrate the use of the permutation vectors.
--> a = sparse([1,0,0,4;3,2,0,0;0,0,0,1;4,3,2,4]) a = <int32> - size: [4 4] Matrix is sparse with 9 nonzeros --> [l,u,p,q,r] = lu(a) l = <double> - size: [4 4] Matrix is sparse with 4 nonzeros u = <double> - size: [4 4] Matrix is sparse with 9 nonzeros p = <int32> - size: [1 4] Columns 1 to 4 4 2 1 3 q = <int32> - size: [1 4] Columns 1 to 4 3 2 1 4 r = <double> - size: [4 4] Matrix is sparse with 4 nonzeros --> full(l*a) ans = <double> - size: [4 4] Columns 1 to 2 1.000000000000000 0.000000000000000 3.000000000000000 2.000000000000000 0.000000000000000 0.000000000000000 4.000000000000000 3.000000000000000 Columns 3 to 4 0.000000000000000 4.000000000000000 0.000000000000000 0.000000000000000 0.000000000000000 1.000000000000000 2.000000000000000 4.000000000000000 --> b = R*a Error: Undefined function or variable R --> full(b(P,Q)) Error: Undefined function or variable b