-
Notifications
You must be signed in to change notification settings - Fork 1
/
processor.py
202 lines (180 loc) · 6.72 KB
/
processor.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
class Matrix(object):
def __init__(self, R=1, C=1):
"""
takes two ints : R and C specifying rows and columns of matrix
Default is 1x1 0 matrix (or simply 0).
"""
self.R = int(R)
self.C = int(C)
self.matrix = [[0 for c in range(self.C)] for r in range(self.R)]
def check_dims(self, other):
return self.R == other.R and self.C == other.C
def has_floats(self, inp):
return sum(elem.isdigit() for elem in inp) < len(inp)
def make(self):
for i in range(self.R):
row = input().split(maxsplit=self.C)
if self.has_floats(row):
self.matrix[i] = list(map(float, row))
else:
self.matrix[i] = list(map(int, row))
def __str__(self):
vis = ""
for i in range(self.R):
vis += f'{" ".join([str(j) for j in self.matrix[i]])}\n'
return vis
def __add__(self, other):
if self.check_dims(other):
M = Matrix(self.R, self.C)
for i in range(self.R):
for j in range(self.C):
M.matrix[i][j] = self.matrix[i][j] + other.matrix[i][j]
return M
else:
return "The operation cannot be performed."
def __sub__(self, other):
if self.check_dims(other):
M = Matrix(self.R, self.C)
for i in range(self.R):
for j in range(self.C):
M.matrix[i][j] = self.matrix[i][j] - other.matrix[i][j]
return M
else:
return "The operation cannot be performed."
def __mul__(self, other):
# Case 1: Integer multiplication
if isinstance(other, int) or isinstance(other, float):
M = Matrix(self.R, self.C)
M.matrix = [[round(n * other, 3) + 0 for n in self.matrix[i]] for i in range(self.R)]
# Case 2: Matrix multiplication
elif isinstance(other, Matrix):
if self.C != other.R: raise ValueError
M = Matrix(self.R, other.C)
for i in range(self.R):
for j in range(other.C):
for k in range(other.R):
M.matrix[i][j] += self.matrix[i][k] * other.matrix[k][j]
else:
return "The operation cannot be performed."
return M
def transpose(self):
M = Matrix(self.R, self.C)
m = self.matrix
M.matrix = [[m[j][i] for j in range(self.C)] for i in range(self.R)]
return M
def hori_transpose(self):
M = Matrix(self.R, self.C)
n = self.R - 1
for i in range(self.R): M.matrix[i] = self.matrix[n - i]
return M
def ver_transpose(self):
M = Matrix(self.R, self.C)
M.matrix = [[self.matrix[i][self.C - 1 - j] for j in range(self.C)] for i in range(self.R)]
return M
def side_transpose(self):
M = Matrix(self.R, self.C)
M.matrix = [[self.matrix[self.C - 1 - j][self.R - 1 - i] for j in range(self.C)] for i in range(self.R)]
return M
def det_recur(self, A):
d = 0
# Base Case
if len(A) == 2 and len(A[0]) == 2:
v = A[0][0] * A[1][1] - A[1][0] * A[0][1]
return v
for c in range(len(A)):
d += (-1 if c % 2 else 1) * A[0][c] * self.det_recur(self.get_minor(A, 0, c))
return d
def determinant(self):
if self.R == self.C:
if self.R == 1:
return self.matrix[0][0]
else:
M = self.matrix
return self.det_recur(M)
else:
return "The operation cannot be performed."
def get_minor(self, M, a, b):
return [row[:b] + row[b + 1:] for row in (M[:a] + M[a + 1:])]
def inverse(self):
M = self.matrix
if len(M[0]) == 1:
return 1 / M[0][0]
elif len(M) == 2:
det = self.det_recur(M)
if det == 0:
return "This matrix doesn't have an inverse.\nReason: Singular Matrix"
return [[M[1][1] / det, -1 * M[0][1] / det], [-1 * M[1][0] / det, M[0][0] / det]]
elif (self.det_recur(M) != 0) and (self.R == self.C):
det = self.determinant()
cofacts = []
for r in range(len(M)):
cofact_r = []
for c in range(len(M)):
minor = self.get_minor(M, r, c)
cofact_r.append((-1 if ((r + c) % 2) else 1) * self.det_recur(minor))
cofacts.append(cofact_r)
cofact_matrix = Matrix(len(M), len(M))
cofact_matrix.matrix = cofacts
cofact_T = cofact_matrix.transpose()
return cofact_T * (1 / det)
else:
return "This matrix doesn't have an inverse."
def getmatrices():
a, b = map(int, input("Enter size of first matrix: ").split())
A = Matrix(a, b)
print("Enter first matrix: ")
A.make()
c, d = map(int, input("Enter size of second matrix: ").split())
B = Matrix(c, d)
print("Enter second matrix: ")
B.make()
return (A, B)
def getmatrix():
a, b = map(int, input("Enter matrix size: ").split())
A = Matrix(a, b)
print("Enter matrix: ")
A.make()
return A
def init():
transpose = {1: "Main diagonal", 2: "Side Diagonal", 3: "Vertical line", 4: "Horizontal line"}
welp = {1: "Add Matrices", 2: "Multiply matrix by a constant", 3: "Multiply matrices", 4: "Transpose matrix",
5: "Calculate a determinant", 6: "Inverse matrix", 0: "Exit"}
init_b = True
while init_b:
for f in welp.keys():
print("{}. {}".format(f, welp[f]))
i = int(input("Your choice: "))
if i == 0:
init_b = False
elif i in [1, 2, 3, 5, 6]:
A, B = (getmatrix(), 0) if (i > 4 or i == 2) else getmatrices()
if i == 2:
B = input("Enter constant: ")
B = int(B) if B.isdigit() else float(B)
print("The result is: ")
op_dict = {
1: lambda: A + B,
2: lambda: A * B,
3: lambda: A * B,
5: A.determinant,
6: A.inverse
}
print(op_dict[i]())
elif i == 4:
print()
for g in transpose.keys():
print("{}. {}".format(g, transpose[g]))
j = int(input("Your choice: "))
A = getmatrix()
print("The result is: ")
trans_dict = {
1: A.transpose,
2: A.side_transpose,
3: A.ver_transpose,
4: A.hori_transpose
}
if j in trans_dict:
print(trans_dict[j]())
else:
print("Choose an operation.")
init()