# lu

LU decomposition

### Y = lu(A, type)

• A is a matrix, i.e., ndims(A) == 2, which can be real or complex.
• Consider the decomposition [L, U, P]=lu(A). The function call Y=lu(A) returns a matrix Y which has the same size as A. Elements on and above the main diagonal of Y are equal to U, and elements below the main diagonal of Y are equal to L.
• type should be either 'vector' or 'matrix'. But, regardless of type's value, Y=lu(A, type) and Y=lu(A) give the same results.

Example 1: As seen from below, elements on and above the main diagonal of Y are equal to U, and elements below the main diagonal of Y are equal to L.

A=randi(10,3,5);
[L,U,P]=lu(A);
Y=lu(A);
L
U
Y

L =
1.000   0.000   0.000
0.700   1.000   0.000
0.600  -0.235   1.000

U =
10.00   7.000   6.000   5.000   8.000
0.000   5.100   3.800   3.500  -0.600
0.000   0.000  -0.706   6.824  -3.941

Y =
10.00   7.000   6.000   5.000   8.000
0.700   5.100   3.800   3.500  -0.600
0.600  -0.235  -0.706   6.824  -3.941


### [L,U] = lu(A, type)

• A is a matrix, i.e., ndims(A) == 2, which can be real or complex.
• It returns matrices L and U such that L * U == A.
• type should be either 'vector' or 'matrix'. But, regardless of type's value, [L,U]=lu(A, type) and [L,U]=lu(A) give the same results.

Example 2:

% Generate a random matrix
A=rand(6,4);

% LU decomposition
[L,U]=lu(A);
L

% L*U and A are (apprxoimately) identical
L*U-A

L =
0.544   0.626   1.000   0.000
0.800   0.118   0.354   0.046
0.089   1.000   0.000   0.000
0.140   0.165   0.247   1.000
1.000   0.000   0.000   0.000
0.202   0.426  -0.277  -0.099

ans = 1e-16 ×
0.000   1.110   1.110   0.278
1.110   0.000   0.000   0.000
0.069   0.000   0.000  -0.278
0.000  -0.278   0.000   0.000
0.000   0.000   0.000   0.000
0.000   0.555  -0.278   0.000


### [L, U, P] = lu(A, type)

• A is a matrix, i.e., ndims(A) == 2, which can be real or complex.
• type should be a character array, equal to either 'matrix' or 'vector'.
• If type is equal to 'matrix', then P is a permutation matrix such that L*U==P*A holds.
• If type is equal to 'vector', then P is a vector containing information about the permutation used in the decomposition. See Example 3 below for more details.
• [L,U,P]=lu(A) and [L,U,P]=lu(A,'matrix') give the same results.
• L and U are the same in [L,U,P]=lu(A) and [L,U,P]=lu(A,'vector').

Example 3: Permutation matrix and permutation vector returned by lu.

% Randomly generated matrix
a=randi(10,4,5);

% P contains permutation information
[L,U,P]=lu(a,'vector');

% Construct a permuation matrix Q using P
Q=zeros(size(a,1));
for r=1:size(a,1)
Q(r,P(r))=1;
end

% Permutation matrix T given by lu with type equal to 'matrix'
[L,U,T]=lu(a,'matrix');

% Permutation matrices Q and T are the same
Q
T

Q =
0.000   0.000   0.000   1.000
1.000   0.000   0.000   0.000
0.000   0.000   1.000   0.000
0.000   1.000   0.000   0.000

T =
0.000   0.000   0.000   1.000
1.000   0.000   0.000   0.000
0.000   0.000   1.000   0.000
0.000   1.000   0.000   0.000