To write a program to find the LU Decomposition of a matrix.
- Hardware โ PCs
- Anaconda โ Python 3.7 Installation / Moodle-Code Runner
(i) To find the L and U matrix
- First we import numpy as np and import lu from linalg in scipy module
- Then we initialize a list in the name of arr and ask input from the user
- We initialize P,L,U as lu of A
- We print the output of L and U
(ii) To find the LU Decomposition of a matrix
- We import lu_factor and lu_solve from linalg module in scipy module
- Then we initialize a list in the name of arr and ask input from the user
- Then we initialize a list in the name of constant and ask input from the user
- We initialize result as factorization of A and initialze solution with value of lu_solve method on (result,B)
- Now we print the solution
(i) To find the L and U matrix
#Program to find L and U matrix using LU decomposition.
#Developed by: MANOJ KUMAR S
#RegisterNumber: 23002959
from scipy.linalg import lu
import numpy as np
arr=eval(input())
A=np.array(arr)
P,L,U=lu(A)
print(L)
print(U)
(ii) To find the LU Decomposition of a matrix
#Program to solve a matrix using LU decomposition.
#Developed by: MANOJ KUMAR S
#RegisterNumber: 23002959
from scipy.linalg import lu_factor,lu_solve
import numpy as np
arr=eval(input())
constant=eval(input())
A=np.array(arr)
B=np.array(constant)
result=lu_factor(A)
solution=lu_solve(result,B)
print(solution)
Thus the program to find the LU Decomposition of a matrix is written and verified using python programming.