-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgauss.py
More file actions
executable file
·115 lines (76 loc) · 2.06 KB
/
gauss.py
File metadata and controls
executable file
·115 lines (76 loc) · 2.06 KB
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
import numpy
import copy
def gaussFunc(a):
eps = 1e-16
c = numpy.array(a)
a = numpy.array(a)
len1 = len(a[:, 0])
len2 = len(a[0, :])
vectB = copy.deepcopy(a[:, len1])
for g in range(len1):
max = abs(a[g][g])
my = g
t1 = g
while t1 < len1:
# for t1 in range(len(a[:,0])):
if abs(a[t1][g]) > max:
max = abs(a[t1][g])
my = t1
t1 += 1
if abs(max) < eps:
raise DetermExeption("Check determinant")
if my != g:
# a[g][:], a[my][:] = a[my][:], a[g][:]
# numpy.swapaxes(a, 1, 0)
b = copy.deepcopy(a[g])
a[g] = copy.deepcopy(a[my])
a[my] = copy.deepcopy(b)
amain = float(a[g][g])
z = g
while z < len2:
a[g][z] = a[g][z] / amain
z += 1
j = g + 1
while j < len1:
b = a[j][g]
z = g
while z < len2:
a[j][z] = a[j][z] - a[g][z] * b
z += 1
j += 1
a = backTrace(a, len1, len2)
print("Погрешность:")
print(vectorN(c, a, len1, vectB))
return a
class DetermExeption(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def backTrace(a, len1, len2):
a = numpy.array(a)
i = len1 - 1
while i > 0:
j = i - 1
while j >= 0:
a[j][len1] = a[j][len1] - a[j][i] * a[i][len1]
j -= 1
i -= 1
return a[:, len2 - 1]
def vectorN(c, a, len1, vectB): # c-начальная матрица a-ответ len-ранг, vectB-вектор B
c = numpy.array(c)
a = numpy.array(a)
vectB = numpy.array(vectB)
b = numpy.zeros((len1))
i = 0
while i<len1:
j = 0
while j<len1:
b[i]+=c[i][j]*a[j]
j+=1
i=i+1
c = copy.deepcopy(b)
print("!")
for i in range(len1):
c[i] = abs(c[i] - vectB[i])
return c