-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
215 lines (157 loc) · 7.23 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
class Data:
def __init__(self, file_name) -> None:
self.file_name = file_name
def get_data(self):
# d = [1]
with open(f'{self.file_name}.txt', 'r+') as f:
# print(f.readlines())
d = f.readlines()
# print(d)
return d
def return_matrix(self):
d = [[1,1,1,1], [1,1,1,1]]
matrix = []
data = self.get_data()
# print(111)
# print(data)
sublist = []
list = []
for i in data:
for j in i:
# print(j)
try:
int(j)
sublist.append(int(j))
except:
pass
list.append(sublist)
sublist = []
# print(list)
return list
def draw_matrix(self, matrix):
matrix_line_str = ''
for i in matrix:
print(i)
class Matrix:
def __init__(self, matrix) -> None:
self.matrix = matrix
def find_inverse(self):
pass
def create_single_matrix(self):
l = len(self.matrix)
r = len (self.matrix[1])
# print(r)
# print(l)
new_matrix = []
for index, line in enumerate(self.matrix):
single_matrix = []
for j in range(0, len(self.matrix[1])):
if index == j:
single_matrix.append(1)
else:
single_matrix.append(0)
new_matrix.append(single_matrix)
# print('new_matrix', new_matrix)
# return new_matrix
for index, line in enumerate(self.matrix):
# print('matrix', self.matrix)
# print('line new_matrix', line, new_matrix)
new_matrix[index] = line + new_matrix[index]
return new_matrix
class ElementaryTransformations(Data):
def __init__(self, matrix) -> None:
self.matrix = matrix
def start(self):
self.draw_matrix(self.matrix)
print('')
for i in range(len(self.matrix)):
print('Нахождение единички в диагонали')
print(f'Деление всех элементов {i} строки на {self.matrix[i][i]}')
self.division_line(i, self.matrix[i][i])
self.draw_matrix(self.matrix)
for j in range(i+1, len(self.matrix)):
index = -(self.matrix[j][i])
# print(f'прибавление к всем элементов {j} строка на {index}')
print(f'умнажаем строку {i} на {index} и прибовлаем к строке {j}')
print(i, j, 'i j')
self.adding_line_2( i, j, index)
self.draw_matrix(self.matrix)
self.draw_matrix(self.matrix)
print(1111)
print(self.matrix)
print("\nНачинаем обратный проход для приведения матрицы к диагональному виду:")
for i in range(len(self.matrix) - 1, -1, -1): # Идём с конца к началу
print(f"Обратный проход: Работа с {i}-й строкой")
for j in range(i - 1, -1, -1): # Обнуляем элементы выше диагонали
if self.matrix[j][i] != 0:
multiplier = -self.matrix[j][i]
print(f"Обнуляем элемент в строке {j}, столбце {i} с помощью строки {i}:")
self.adding_line_2(i, j, multiplier)
self.draw_matrix(self.matrix)
# for i in reversed(range(len(self.matrix))):
# print('Нахождение единички в диагонали')
# print(f'Деление всех элементов {i} строки на {self.matrix[i][i]}')
# self.division_line(i, self.matrix[i][i])
# self.draw_matrix(self.matrix)
# for j in reversed(range(i-1, len(self.matrix))):
# index = -(self.matrix[j][i])
# # print(f'прибавление к всем элементов {j} строка на {index}')
# print(f'умнажаем строку {i} на {index} и прибовлаем к строке {j}')
# print(i, j, 'i j')
# self.adding_line_2( i, j, index)
# self.draw_matrix(self.matrix)
self.draw_matrix(self.matrix)
print(1111)
print(self.matrix)
def multiply_line(self, line_number, number ):
'умножение какойто строки '
for index, integer in enumerate(self.matrix[line_number]):
self.matrix[line_number][index] = self.matrix[line_number][index] * number
if self.matrix[line_number][index] * number in [-0.0, -0]:
self.matrix[line_number][index] = 0
def division_line(self, line_number, number ):
'деление какойто строки '
for index, integer in enumerate(self.matrix[line_number]):
self.matrix[line_number][index] = self.matrix[line_number][index] / number
if self.matrix[line_number][index] / number in [-0.0, -0] :
self.matrix[line_number][index] = 0
# print(self.matrix)
def adding_line(self, line_number, number, over):
for index, integer in enumerate(self.matrix[line_number]):
# for i in self.matrix[over]:
self.matrix[line_number][index] = self.matrix[line_number][index] + number
def adding_line_2(self, x_from, x_to, number):
'c x_from прибавить на x_to'
for index, i in enumerate(self.matrix[x_from]):
print(index)
print(self.matrix[x_to])
print(self.matrix[x_to][index] )
print(enumerate(self.matrix[x_from]))
self.matrix[x_to][index] = self.matrix[x_to][index] +i*number
if (self.matrix[x_to][index] +i*number) in [-0.0, -0]:
self.matrix[x_to][index] = 0
def multiply_line_2(self, x_from, x_to, number):
'умножаем x_from на x_to'
for index, i in enumerate(self.matrix[x_from]):
# print(i)
self.matrix[x_to][index] = self.matrix[x_to][index] +i*number
if (self.matrix[x_to][index] +i*number) in [-0.0, -0]:
self.matrix[x_to][index] = 0
def division_line_2(self, x_from, x_to, number):
'деление x_from / x_to'
for index, i in enumerate(self.matrix[x_from]):
# print(i)
self.matrix[x_to][index] = self.matrix[x_to][index] +i/number
if (self.matrix[x_to][index] +i/number) in [-0.0, -0]:
self.matrix[x_to][index] = 0
print(0.5*-1.0)
data = Data('input')
matrix = Matrix(data.return_matrix())
m = ElementaryTransformations(data.return_matrix())
m.start()
print([1,2,3])
print(0.5*-1.0)
for index, i in enumerate(matrix):
matrix[index] = i+data.return_matrix()[index]
print(matrix)
# data.draw_matrix(matrix.create_single_matrix())