# LU Decomposition in Python

In this, you will learn how to write a program for LU decomposition using Scipy library. In this case first you will learn about LU decomposition Matrix and working of LU decomposition matrix. Then you will learn how to write code in Python Programming L U decomposition matrix.

### L U decomposition matrix

It is the factorization of a given square matrix into two triangular matrices. In this, one upper triangular matrix and one lower triangular matrix, so that the product of these two matrices gives the first matrix. Introduced by Alan Turing in 1948, who created the experimental machine.

This method of factorizing a matrix as a product of two triangular matrices has a variety of applications such as a mathematical system solution, itself which is an integral part of many applications such as circuit availability and flexibility system solutions; find the inverse of the matrix and determinant of the matrix.

The system of linear equations can be made as a matrix equation, that includes a matrix A and vectors x and b of which x is the solution to be found.

LU decomposition to assist us in resolving the following equation matrix, without the specific need to modify matrix A:

Ax = b

PLU decomposition with partial pivoting the matrix A into PA = LU, where L is the lower triangular matrix,
U is the upper triangle matrix and P is the permutation Matrix.

#### PLU decomposition

Example 1: LU Decomposition in Python using Scipy (linear algebra library)
`import pprintimport scipyimport scipy.linalgA = scipy.array([[7,3,-1,2],[3,8,1,4],[-1,1,4,-1],[2,-4,-1,6]])P,L,U = scipy.linalg.lu(A)print("A:")pprint.pprint(A)print("P:")pprint.pprint(P)print("L:")pprint.pprint(L)print("U:")pprint.pprint(U)`
`Output:`
```A:
array([[ 7,  3, -1,  2],
[ 3,  8,  1,  4],
[-1,  1,  4, -1],
[ 2, -4, -1,  6]])
P:
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
L:
array([[ 1.        ,  0.        ,  0.        ,  0.        ],
[ 0.42857143,  1.        ,  0.        ,  0.        ],
[-0.14285714,  0.21276596,  1.        ,  0.        ],
[ 0.28571429, -0.72340426,  0.08982036,  1.        ]])
U:
array([[ 7.        ,  3.        , -1.        ,  2.        ],
[ 0.        ,  6.71428571,  1.42857143,  3.14285714],
[ 0.        ,  0.        ,  3.55319149, -1.38297872],
[ 0.        ,  0.        ,  0.        ,  7.82634731]])Example 2: LU Decomposition in Python using Scipy (linear algebra library)```
`import pprintimport scipyimport scipy.linalgA = scipy.array([[1,3,5],[2,4,7],[1,1,0]])P,L,U = scipy.linalg.lu(A)print("A:")pprint.pprint(A)print("P:")pprint.pprint(P)print("L:")pprint.pprint(L)print("U:")pprint.pprint(U)`
`Output:`
```A:
array([[1, 3, 5],
[2, 4, 7],
[1, 1, 0]])
P:
array([[0., 1., 0.],
[1., 0., 0.],
[0., 0., 1.]])
L:
array([[ 1. ,  0. ,  0. ],
[ 0.5,  1. ,  0. ],
[ 0.5, -1. ,  1. ]])
U:
array([[ 2. ,  4. ,  7. ],
[ 0. ,  1. ,  1.5],
[ 0. ,  0. , -2. ]])Example 3: LU Decomposition in Python using Scipy (linear algebra library)import pprintimport scipyimport scipy.linalgA = scipy.array([[11,9,24,2],[1,5,2,6],[3,17,18,1],[2,5,7,1]])P,L,U = scipy.linalg.lu(A)print("A:")pprint.pprint(A)print("P:")pprint.pprint(P)print("L:")pprint.pprint(L)print("U:")pprint.pprint(U)Output:```
```A:
array([[11,  9, 24,  2],
[ 1,  5,  2,  6],
[ 3, 17, 18,  1],
[ 2,  5,  7,  1]])
P:
array([[1., 0., 0., 0.],
[0., 0., 1., 0.],
[0., 1., 0., 0.],
[0., 0., 0., 1.]])
L:
array([[1.        , 0.        , 0.        , 0.        ],
[0.27272727, 1.        , 0.        , 0.        ],
[0.09090909, 0.2875    , 1.        , 0.        ],
[0.18181818, 0.23125   , 0.00359712, 1.        ]])
U:
array([[11.        ,  9.        , 24.        ,  2.        ],
[ 0.        , 14.54545455, 11.45454545,  0.45454545],
[ 0.        ,  0.        , -3.475     ,  5.6875    ],
[ 0.        ,  0.        ,  0.        ,  0.51079137]])```