-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_inverse.py
55 lines (46 loc) · 1.33 KB
/
get_inverse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
from functools import reduce
import sys
def LU_deco_inverse(m):
# well done
dim = m.shape[0]
E = np.mat(np.eye(dim))
L = np.mat(np.eye(dim))
U = m.copy()
for i in range(dim):
if abs(m[i,i]) < 1e-8:
print("zero pivot encoUnted")
sys.exit()
L[i+1:,i] = U[i+1:,i] / U[i,i]
E[i+1:,:] = E[i+1:,:] - L[i+1:,i]*E[i,:]
U[i+1:,:] = U[i+1:,:] - L[i+1:,i]*U[i,:]
print("LU decomposition matrix m to L,U:")
print("L=",L,"\n","U=",U)
for i in range(dim-1,-1,-1):
(E[i,:], U[i,:]) = (E[i,:], U[i,:]) / U[i,i]
E[:i,:] = E[:i,:] - U[:i,i]*E[i,:]
U[:i,:] = U[:i,:] - U[:i,i]*U[i,:] # r_j = m_ji - r_j*r_i
print("inverse of m via primary transformation:")
print("E=",E)
print("using the method 'I' of numpy to calculate the inverse of matrix m:")
print("m_inv=",m.I)
if __name__ == '__main__':
A = np.mat([[1.,1,1],[1,2,3],[1,5,1]])
A_dim = A.shape[0]
LU_deco_inverse(A)
### output: ###
# LU decomposition matrix m to L,U:
# L= [[ 1. 0. 0.]
# [ 1. 1. 0.]
# [ 1. 4. 1.]]
# U= [[ 1. 1. 1.]
# [ 0. 1. 2.]
# [ 0. 0. -8.]]
# inverse of m via primary transformation:
# E= [[ 1.625 -0.5 -0.125]
# [-0.25 0. 0.25 ]
# [-0.375 0.5 -0.125]]
# using the method 'I' of numpy to calculate the inverse of matrix m:
# m_inv= [[ 1.625 -0.5 -0.125]
# [-0.25 0. 0.25 ]
# [-0.375 0.5 -0.125]]