To write a program to find the LU Decomposition of a matrix.
- Hardware – PCs
- Anaconda – Python 3.7 Installation / Moodle-Code Runner
-
Import numpy
-
Import lu from scipy.linalg
-
Get the inputs using numpy.array
-
Use the function lu() to find the L and U matrices
(i) To find the L and U matrix
/* Program to find the L and U matrix. Developed by: Subashini K RegisterNumber: 212225240160 */
import numpy as np
from scipy.linalg import lu
A=np.array(eval(input()))
P,L,U=lu(A)
print(L)
print(U)
(ii) To find the LU Decomposition of a matrix
/* Program to find the LU Decomposition of a matrix. Developed by: Subashini K RegisterNumber: 212225240160 */
import numpy as np
from scipy.linalg import lu_factor,lu_solve
A=np.array(eval(input()))
B=np.array(eval(input()))
lu,piv=lu_factor(A)
X=lu_solve((lu,piv),B)
print(X)
(i) To find the L and U matrix
(ii) To find the LU Decomposition of a matrix
Thus the program to find the LU Decomposition of a matrix is written and verified using python programming.