-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
187 lines (173 loc) · 8 KB
/
calculator.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
def type_of_var(variable):
if '.' in variable:
return float(variable)
else:
return int(variable)
def result(matrixr):
print('The result is:')
for i in matrixr:
r_string = ''
for j in i:
r_string = r_string + str(j) + ' '
print(r_string.rstrip())
def det(tab_d):
if len(tab_d) == 1:
return tab_d[0][0]
elif len(tab_d) == 2:
return tab_d[0][0] * tab_d[1][1] - tab_d[1][0] * tab_d[0][1]
else:
tab_minors = []
for pos, element in enumerate(tab_d[0]):
tab_minor = []
for column in tab_d[1:]:
row_smaller = []
row_smaller.extend(column[:pos])
row_smaller.extend(column[(pos + 1):])
tab_minor.append(row_smaller)
tab_minors.append(tab_minor)
det_sum = 0
for t, t_ms, factor in zip(tab_d[0], tab_minors, range(len(tab_d))):
det_sum += t * det(t_ms) * (-1) ** (factor % 2)
return det_sum
if __name__ == "__main__":
choose = '6'
while choose != '0':
print('1. Add matrices')
print('2. Multiply matrix by a constant')
print('3. Multiply matrices')
print('4. Transpose matrix')
print('5. Calculate a determinant')
print('6. Inverse matrix')
print('0. Exit')
choose = input('Your choice: > ')
if choose == '1':
rows1, columns1 = map(int, input('Enter size of first matrix: > ').split())
print('Enter first matrix:')
matrix1 = [input('> ').split() for i in range(rows1)]
rows2, columns2 = map(int, input('Enter size of second matrix: > ').split())
if not (columns1 == columns2 and rows1 == rows2):
print('ERROR')
else:
print('Enter second matrix:')
matrix2 = [input('> ').split() for i in range(rows2)]
for i in range(rows1):
for j in range(columns1):
matrix1[i][j] = type_of_var(matrix1[i][j]) + type_of_var(matrix2[i][j])
result(matrix1)
print('\n')
if choose == '2':
rows1, columns1 = map(int, input('Enter size of matrix: > ').split())
print('Enter matrix:')
matrix1 = [input('> ').split() for i in range(rows1)]
constant = type_of_var(input('Enter constant: > '))
for i in range(rows1):
for j in range(columns1):
matrix1[i][j] = type_of_var(matrix1[i][j]) * constant
result(matrix1)
print('\n')
if choose == '3':
rows1, columns1 = map(int, input('Enter size of first matrix: > ').split())
print('Enter first matrix:')
matrix1 = [input('> ').split() for i in range(rows1)]
rows2, columns2 = map(int, input('Enter size of second matrix: > ').split())
if not (columns1 == rows2):
print('ERROR')
else:
product = [[0 for j in range(columns2)] for i in range(rows1)]
print('Enter second matrix: > ')
matrix2 = [input('> ').split() for i in range(rows2)]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
product[i][j] += type_of_var(matrix1[i][k]) * type_of_var(matrix2[k][j])
result(product)
if choose == '4':
print('1. Main diagonal')
print('2. Side diagonal')
print('3. Vertical line')
print('4. Horizontal line')
choose_t = input('Your choice: > ')
rows, columns = map(int, input('Enter matrix size: > ').split())
print('Enter matrix:')
matrix = [input('> ').split() for i in range(rows)]
if choose_t == '1': # transposition along the main diagonal
transpose = [[type_of_var(row[i]) for row in matrix] for i in range(len(matrix[0]))]
result(transpose)
if choose_t == '2': # transposition along the side diagonal
transpose = [[row[(-(i + 1))] for row in matrix[::-1]] for i in range(len(matrix[0]))]
result(transpose)
if choose_t == '3': # transposition along the vertical line
transpose = [[j for j in i[::-1]] for i in matrix]
result(transpose)
if choose_t == '4': # transposition along the horizontal line
transpose = [i for i in matrix[::-1]]
result(transpose)
if choose == '5':
rows, columns = map(int, input('Enter matrix size: > ').split())
if rows != columns:
print('ERROR')
continue
print('Enter matrix:')
matrix = [input('> ').split() for i in range(rows)]
matrix = [[type_of_var(j) for j in i] for i in matrix]
print('The result is:')
print(det(matrix), '\n')
if choose == '6':
rows, columns = map(int, input('Enter matrix size: > ').split())
if rows != columns:
print('ERROR')
continue
print('Enter matrix:')
matrix = [input('> ').split() for i in range(rows)]
matrix = [[type_of_var(j) for j in i] for i in matrix]
d = det(matrix)
if d == 0:
print("This matrix doesn't have an inverse.")
continue
else:
inverse_det = 1 / det(matrix)
row_to_det = 0
tab_to_det = []
for by_row in matrix:
column_to_det = 0
row_of_tab_to_det = []
for by_column in by_row:
new_tab = []
for row_number, row in enumerate(matrix):
new_row = []
for column_number, column in enumerate(row):
if column_number != column_to_det and row_number != row_to_det:
new_row.append(column)
if new_row:
new_tab.append(new_row)
column_to_det += 1
row_of_tab_to_det.append(det(new_tab))
tab_to_det.append(row_of_tab_to_det)
row_to_det += 1
tab_proper_sign = []
for row_to_power_number, row_to_power in enumerate(tab_to_det):
row_proper_sign = []
for col_to_pow_num, col_to_pow in enumerate(row_to_power):
row_proper_sign.append(col_to_pow * ((-1) ** (col_to_pow_num + 1 + row_to_power_number + 1)))
tab_proper_sign.append(row_proper_sign)
transposed = [[row[i] for row in tab_proper_sign] for i in range(len(tab_proper_sign[0]))]
for m in range(len(transposed)):
for n in range(len(transposed[0])):
transposed[m][n] = float(transposed[m][n] * inverse_det)
if transposed[m][n] in (-0.0, 0.0):
transposed[m][n] = 0
print('The result is:')
for row_result in transposed:
result_string_row = ''
for column_result in row_result:
result_string_char = str(column_result)
dot_present = 0
for char_pos, char in enumerate(str(column_result)):
if char == '.':
dot_present = char_pos
break
if dot_present != 0:
result_string_char = result_string_char[:(dot_present + 3)]
result_string_row = result_string_row + result_string_char + ' '
print(result_string_row)
print('\n')