-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessor.py
More file actions
244 lines (184 loc) · 6.37 KB
/
Copy pathprocessor.py
File metadata and controls
244 lines (184 loc) · 6.37 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
main_menu = """
1. Add matrices
2. Multiply matrix by a constant
3. Multiply matrices
4. Transpose matrix
5. Calculate a determinant
6. Inverse matrix
0. Exit"""
transposition_menu = """
1. Main diagonal
2. Side diagonal
3. Vertical line
4. Horizontal line"""
class Matrix:
def __init__(self, x, y):
self.num_rows = x
self.num_columns = y
self.matrix = []
self.dimensions = f'{str(self.num_rows)} {str(self.num_columns)}'
def add_row(self, row):
if len(row) == self.num_columns:
self.matrix.append(row)
else:
print(f"Row incorrect length: {row}")
def fill_matrix(self, n=''):
print(f'Enter {n}matrix:')
for i in range(self.num_rows):
row = [float(num) if '.' in num else int(num) for num in list(input().split())]
self.matrix.append(row)
def get_submatrix(matrix, i, j):
submatrix = Matrix(matrix.num_rows -1, matrix.num_columns - 1)
submatrix.matrix = [row[:] for row in matrix.matrix]
submatrix.matrix.pop(i)
for row in submatrix.matrix:
row.pop(j)
return submatrix
def get_determinant(m, total=0):
if m.num_rows != m.num_columns:
nope()
else:
if m.num_rows == 1:
return m.matrix[0][0]
if m.num_rows == 2:
return m.matrix[0][0] * m.matrix[1][1] - m.matrix[0][1] * m.matrix[1][0]
else:
copy_matrix = Matrix(m.num_rows, m.num_columns)
copy_matrix.matrix = [row[:] for row in m.matrix]
i = 0
for j in range(0, copy_matrix.num_columns):
total += m.matrix[i][j] * (-1) ** (j % 2) * get_determinant(get_submatrix(copy_matrix, i, j))
return total
def get_inverse(m):
det = get_determinant(m)
if det == 0:
print("This matrix doesn't have an inverse.")
main()
else:
adjunct_step_1 = Matrix(m.num_rows, m.num_columns)
for i in range(m.num_rows):
new_row = []
for j in range(m.num_columns):
x = (-1) ** (i + j) * get_determinant(get_submatrix(m, i, j))
new_row.append(x)
adjunct_step_1.add_row(new_row)
adjunct = transpose_main_diagonal(adjunct_step_1)
inverse = matrix_by_constant(adjunct, 1 / det)
return inverse
def matrix_by_constant(m, const):
multiplied_matrix = Matrix(m.num_rows, m.num_columns)
for row in m.matrix:
new_row = [0 if num * const == 0 else (round(num * const, 4)) for num in row]
multiplied_matrix.add_row(new_row)
return multiplied_matrix
def create_matrix(n=''):
dimensions = input(f'Enter size of {n}matrix: ')
matrix = Matrix(int(dimensions[0]), int(dimensions[2]))
matrix.fill_matrix(n)
return matrix
def matrix_by_matrix(m1, m2):
if m1.num_columns == m2.num_rows:
m3 = Matrix(m1.num_rows, m2.num_columns)
for i in range(len(m1.matrix)):
new_row = []
for k in range(len(m2.matrix[0])):
x = 0
for j in range(len(m2.matrix)):
x += m1.matrix[i][j] * m2.matrix[j][k]
new_row.append(round(x, 2))
m3.add_row(new_row)
return m3
else:
nope()
def transpose_main_diagonal(m):
tm = Matrix(m.num_rows, m.num_columns)
for i in range(m.num_rows):
new_row = []
for k in range(m.num_columns):
new_row.append(m.matrix[k][i])
tm.add_row(new_row)
return tm
def transpose_side_diagonal(m):
tm = Matrix(m.num_rows, m.num_columns)
for i in range(1, m.num_rows + 1):
new_row = []
for k in range(m.num_columns - 1, -1, -1):
new_row.append(m.matrix[k][-i])
tm.add_row(new_row)
return tm
def transpose_vertical(m):
tm = Matrix(m.num_rows, m.num_columns)
for row in m.matrix:
new_row = []
for i in range(len(row) - 1, -1, -1):
new_row.append(row[i])
tm.add_row(new_row)
return tm
def transpose_horizontal(m):
tm = Matrix(m.num_rows, m.num_columns)
for i in range(m.num_rows - 1, -1, -1):
tm.add_row(m.matrix[i])
return tm
def add_two_matrices(m1, m2):
if m1.dimensions == m2.dimensions:
m3 = Matrix(m1.num_rows, m1.num_columns)
for i in range(m1.num_rows):
new_row = []
for j in range(m1.num_columns):
new_row.append(m1.matrix[i][j] + m2.matrix[i][j])
m3.add_row(new_row)
return m3
else:
nope()
def transpose():
print(transposition_menu)
transposition = input("Your choice: ")
new_matrix = create_matrix()
if transposition == '1':
print_matrix(transpose_main_diagonal(new_matrix))
elif transposition == '2':
print_matrix(transpose_side_diagonal(new_matrix))
elif transposition == '3':
print_matrix(transpose_vertical(new_matrix))
elif transposition == '4':
print_matrix(transpose_horizontal(new_matrix))
def print_matrix(m):
print("The result is:")
printable_matrix = Matrix(m.num_rows, m.num_columns)
for row in m.matrix:
new_row =[str(num).rjust(8) for num in row]
printable_matrix.add_row(new_row)
for row in printable_matrix.matrix:
print(*row)
def nope():
print("The operation cannot be performed.")
main()
def main():
while True:
print(main_menu)
choice = input("Your choice: ")
if choice == '1':
first_matrix = create_matrix('first ')
second_matrix = create_matrix('second ')
print_matrix(add_two_matrices(first_matrix, second_matrix))
elif choice == '2':
new_matrix = create_matrix()
const = int(input("Enter constant: "))
print_matrix(matrix_by_constant(new_matrix, const))
elif choice == '3':
first_matrix = create_matrix('first ')
second_matrix = create_matrix('second ')
print_matrix(matrix_by_matrix(first_matrix, second_matrix))
elif choice == '4':
transpose()
elif choice == '5':
matrix = create_matrix()
print(get_determinant(matrix))
elif choice == '6':
matrix = create_matrix()
print_matrix(get_inverse(matrix))
elif choice == '0':
exit()
else:
print("Invalid choice. Please try again.")
# main()