### 15.3.5 LU decomposition

The LU decomposition of a square matrix A is P A=L U, where P is a permutation matrix, L is lower triangular with ones on the diagonal, and U is upper triangular. The lu command finds the LU decomposition of a matrix.

- lu takes A, a square matrix.
- lu(A) returns a list [p,L,U] where p is a permutation that determines P, and P, L and U are the LU decomposition of A.

The permutation matrix P is defined from p by:

Pi,p(i)=1, Pi,j=0 if j ≠ p(i).

In other words, it is the identity matrix where the rows are permuted according to the permutation p. You can get the permutation matrix from p by P:=permu2mat(p) (see Section 12.2.6).

#### Example

A:=[[3.,5.],[4.,5.]]:; (p,L,U):=lu(A)

| ⎡ ⎣ | 1,0 | ⎤ ⎦ | , |
⎡ ⎢ ⎣ |
1 | 0
0.75 | 1
| ⎤ ⎥ ⎦
| , |
⎡ ⎢ ⎣ |
4.0 | 5.0
0 | 1.25
| ⎤ ⎥ ⎦

Verification:

permu2mat(p)*A; L*U

⎡ ⎢ ⎣ |
4.0 | 5.0
3.0 | 5.0
| ⎤ ⎥ ⎦
| , |
⎡ ⎢ ⎣ |
4.0 | 5.0
3.0 | 5.0
| ⎤ ⎥ ⎦

Note that the permutation is different for exact input (the choice of pivot is the simplest instead of the largest in absolute value).

lu([[1,2],[3,4]])

| ⎡ ⎣ | 0,1 | ⎤ ⎦ | , |
⎡ ⎢ ⎣ |
1 | 0
3 | 1
| ⎤ ⎥ ⎦
| , |
⎡ ⎢ ⎣ |
1 | 2
0 | −2
| ⎤ ⎥ ⎦

lu([[1.0,2],[3,4]])

| ⎡ ⎣ | 1,0 | ⎤ ⎦ | , |
⎡ ⎢ ⎣ |
1 | 0
0.333333333333 | 1
| ⎤ ⎥ ⎦
| , |
⎡ ⎢ ⎣ |
3.0 | 4.0
0 | 0.666666666667
| ⎤ ⎥ ⎦