-
Notifications
You must be signed in to change notification settings - Fork 3
/
linear_equation_group.py
118 lines (109 loc) · 4.05 KB
/
linear_equation_group.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#%%
import numpy as np
def matrix_decomposition(augMatrix):
row,col=augMatrix.shape
for r in range (row):
tmpSum=0
for k in range(0,r,1):
tmpSum=tmpSum+augMatrix[r,k]*augMatrix[k,r]
augMatrix[r,r]=augMatrix[r,r]-tmpSum
s=augMatrix[r,r]
sMax=abs(s)
rowNum=r
for i in range(r+1,row,1):
tmpSum=0
for k in range(0,r,1):
tmpSum=tmpSum+augMatrix[i,k]*augMatrix[k,r]
augMatrix[i,r]=augMatrix[i,r]-tmpSum
s=abs(augMatrix[i,r])
if s>sMax:
sMax=s
rowNum=i
if rowNum!=r:
augMatrix[[r, rowNum], :] = augMatrix[[rowNum, r], :]
for i in range(r+1,row,1):
if r!=row-1:
augMatrix[i,r]=augMatrix[i,r]/augMatrix[r,r]
tmpSum=0
for k in range(0,r,1):
tmpSum=tmpSum+augMatrix[r,k]*augMatrix[k,i]
augMatrix[r,i]=augMatrix[r,i]-tmpSum
for i in range(1,row,1):
tmpSum=0
for k in range(0,i,1):
tmpSum=tmpSum+augMatrix[i,k]*augMatrix[k,col-1]
augMatrix[i,col-1]=augMatrix[i,col-1]-tmpSum
augMatrix[row-1,col-1]=augMatrix[row-1,col-1]/augMatrix[row-1,row-1]
for i in range(row-2,-1,-1):
tmpSum=0
for k in range(i+1,row,1):
tmpSum=tmpSum+augMatrix[i,k]*augMatrix[k,col-1]
augMatrix[i,col-1]=(augMatrix[i,col-1]-tmpSum)/augMatrix[i,i]
print(augMatrix)
return 0
def gauss_column_elimination(augMatrix):
det = 1
row, col = augMatrix.shape
for i in range(row - 1):
colMaxNum = i
for rowNum in range(i, row):
if abs(augMatrix[rowNum, i]) > abs(augMatrix[i, i]):
colMaxNum = rowNum
if augMatrix[colMaxNum, i] == 0:
det = 0
print("矩阵的det=0!")
return 1
if colMaxNum != i:
augMatrix[[i, colMaxNum], :] = augMatrix[[colMaxNum, i], :]
det = det * (-1)
for rowNum in range(i + 1, row):
augMatrix[rowNum, i] = augMatrix[rowNum, i] / augMatrix[i, i]
for colNum in range(i + 1, col):
augMatrix[rowNum, colNum] = augMatrix[rowNum, colNum] - \
augMatrix[rowNum, i] * augMatrix[i, colNum]
det = det * augMatrix[i, i]
if augMatrix[row - 1, row - 1] == 0:
det = 0
print("矩阵的det=0!")
return 1
det = det * augMatrix[row - 1, row - 1]
print("矩阵的det={0:.2f}".format(det))
for i in range(row - 1, -1, -1):
tmpSum = 0
for j in range(i + 1, row, 1):
tmpSum = tmpSum + augMatrix[j, col - 1] * augMatrix[i, j]
augMatrix[i, col - 1] = (augMatrix[i, col - 1] - tmpSum) / augMatrix[i, i]
for i in range(row):
print("x[{0}]={1:.2f}".format(i + 1, augMatrix[i, col - 1]))
return 0
def main():
coeMatrix = np.array([[10, -7, 0,1], [-3, 2.099999, 6,2], [5, -1, 5,-1],[2,1,0,2]],dtype=np.float)
constCol = np.array([[8], [5.900001], [5],[1]],dtype=np.float)
e=np.concatenate((coeMatrix, constCol), axis=1)
d=np.copy(e)
np.set_printoptions(precision=6, suppress=True)
print("增广矩阵为:\n{0}".format(e))
print("高斯列选主元消去法:")
gauss_column_elimination(e)
print("矩阵三角选主元分解法:")
matrix_decomposition(d)
return 0
if __name__ == '__main__':
main()
# result:
# 增广矩阵为:
# [[ 10. -7. 0. 1. 8. ]
# [ -3. 2.099999 6. 2. 5.900001]
# [ 5. -1. 5. -1. 5. ]
# [ 2. 1. 0. 2. 1. ]]
# 高斯列选主元消去法:
# 矩阵的det=-762.00
# x[1]=0.00
# x[2]=-1.00
# x[3]=1.00
# x[4]=1.00
# 矩阵三角选主元分解法:
# [[ 10. -7. 0. 1. -0. ]
# [ 0.5 2.5 5. -1.5 -1. ]
# [ -0.3 -0. 6.000002 2.299999 1. ]
# [ 0.2 0.96 -0.8 5.079999 1. ]]