This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmulti.py
257 lines (203 loc) · 10.3 KB
/
multi.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
from tkinter import Label, Button, Entry, OptionMenu, IntVar, StringVar, Frame, Toplevel
from tkinter.constants import BOTH
import numpy as np
import menu
alphabet = 'abcdefghijklmnopqrstuvwxyz'
class Multi:
def back_to_menu(self):
self.gui_multi_output.destroy()
menu.gui_menu.deiconify()
def compute_product(self):
try:
# convert matrix_a and matrix_b to int
for i in range(self.rows_a):
for j in range(self.cols_a):
self.matrix_a[i][j] = int(self.matrix_a[i][j])
for i in range(self.rows_b):
for j in range(self.cols_b):
self.matrix_b[i][j] = int(self.matrix_b[i][j])
# use np.matmul to achieve product
self.product_matrix = np.matmul(self.matrix_a, self.matrix_b)
except (TypeError, Exception):
pass
try:
# convert product_matrix back to str
list_mat = [str(i) for i in self.product_matrix]
# remove square brackets
for i in range(len(list_mat)):
list_mat[i] = list_mat[i][1:-1]
# return product_matrix as a list of strings
return list_mat
except (NameError, TypeError, Exception):
pass
def output_matrix(self):
# create output window
self.gui_multi_input.destroy()
self.gui_multi_output = Toplevel()
self.gui_multi_output.title("Multiply")
self.gui_multi_output.resizable(False, False)
# create output frame
self.frame_multi_output = Frame(self.gui_multi_output, highlightbackground='black', highlightthickness=1)
self.frame_multi_output.pack(fill=BOTH, expand=True, padx=5, pady=5)
# go back to menu button
Button(self.frame_multi_output, text="Back", width=4, command=self.back_to_menu).grid(
row=self.rows_a + self.rows_b + 10,
column=1)
# display matrix_a input
Label(self.frame_multi_output, text='Matrix A:', font=('arial', 10, 'bold'), underline=0).grid(row=1, column=1)
for i in range(self.rows_a):
for j in range(self.cols_a):
Label(self.frame_multi_output, text=self.matrix_a[i][j], bd=5).grid(row=i + 1, column=j + 2)
# display matrix_b input
Label(self.frame_multi_output, text='Matrix B:', font=('arial', 10, 'bold'), underline=0).grid(
row=1, column=self.cols_a + 2)
for i in range(self.rows_b):
for j in range(self.cols_b):
Label(self.frame_multi_output, text=self.matrix_b[i][j], bd=5).\
grid(row=i + 1, column=j + self.cols_a * 2 + 2)
# display product
Label(self.frame_multi_output, text='Product:', font=('arial', 10, 'bold'), underline=0).grid(
row=self.rows_a * 2,
column=1)
# compute product
self.product_matrix = self.compute_product()
# display product
for i in range(len(self.product_matrix)):
Label(self.frame_multi_output, text=self.product_matrix[i], bd=5).grid(
row=i + self.rows_a * 2, column=2, columnspan=5, sticky='w ')
# gui stuff
self.gui_multi_output.protocol("WM_DELETE_WINDOW", menu.gui_menu.destroy)
self.gui_multi_output.mainloop()
def input_matrix(self):
# create input window
self.gui_multi_menu.destroy()
self.gui_multi_input = Toplevel()
self.gui_multi_input.title("Multiply")
self.gui_multi_input.resizable(False, False)
# create input frame
self.frame_multi_input = Frame(self.gui_multi_input, highlightbackground='black', highlightthickness=1)
self.frame_multi_input.pack(fill=BOTH, expand=True, padx=5, pady=5)
# window_dimensions = str(self.m_length.get()**3+90) + "x" + str(self.m_height.get())
# print(window_dimensions)
# window.geometry(window_dimensions)
# self.gui_inverse_input.resizable(False, False)
# create matrix A entries
Label(self.frame_multi_input, text="Enter matrix A:", font=('arial', 10, 'bold')).grid(row=1, column=1)
# to create matrix of entry cells we need to create a 2d list of entries
# thank god to stackoverflow peeps for that
# empty arrays for Entry and StringVars
text_var = []
entries = []
# convert rows and cols from IntVar to int
self.rows_a, self.cols_a = (self.ma_rows.get(), self.ma_cols.get())
# create the list of entries with corresponding text_vars
for i in range(self.rows_a):
# append an empty list to append to later
text_var.append([])
entries.append([])
for j in range(self.cols_a):
# for column indications
if i == 1:
Label(self.frame_multi_input, text=alphabet[j]).grid(row=1, column=j + 2)
# append StringVar
text_var[i].append(StringVar())
# append the entry into the list
entries[i].append(Entry(self.frame_multi_input, textvariable=text_var[i][j], width=3))
# display entry
entries[i][j].grid(row=i + 2, column=j + 2)
# for row indications
Label(self.frame_multi_input, text=i + 1).grid(row=i + 2, column=1, sticky='e')
Label(self.frame_multi_input, text="Enter matrix B:", font=('arial', 10, 'bold')).grid(row=self.rows_a * 2,
column=1)
text_var_b = []
entries_b = []
self.rows_b, self.cols_b = (self.ma_cols.get(), self.mb_cols.get())
for i in range(self.rows_b):
text_var_b.append([])
entries_b.append([])
for j in range(self.cols_b):
if i == 1:
Label(self.frame_multi_input, text=alphabet[j]).grid(row=self.rows_a * 2, column=j + 2)
text_var_b[i].append(StringVar())
entries_b[i].append(Entry(self.frame_multi_input, textvariable=text_var_b[i][j], width=3))
entries_b[i][j].grid(row=i + self.rows_a + 5, column=j + 2)
Label(self.frame_multi_input, text=i + 1).grid(row=i + self.rows_a + 5, column=1, sticky='e')
# callback functions to get StringVars/convert them to strings
# and store in matrices
def get_mat_a():
self.matrix_a = []
for i2 in range(self.rows_a):
self.matrix_a.append([])
for j2 in range(self.cols_a):
self.matrix_a[i2].append(text_var[i2][j2].get())
def get_mat_b():
self.matrix_b = []
for i3 in range(self.rows_b):
self.matrix_b.append([])
for j3 in range(self.cols_b):
self.matrix_b[i3].append(text_var_b[i3][j3].get())
def get_mat():
try:
get_mat_a()
get_mat_b()
self.output_matrix()
except (ValueError, Exception):
pass
# button to trigger the entire thing
Button(self.frame_multi_input, text="Enter", width=8, command=get_mat).grid(row=self.cols_a + self.cols_b + 10,
column=1)
# gui stuff
self.gui_multi_input.protocol("WM_DELETE_WINDOW", menu.gui_menu.destroy)
self.gui_multi_input.mainloop()
def __init__(self):
# pre-declare variables
self.product_matrix = None
self.matrix_a, self.matrix_b = None, None
self.matrix = None
self.gui_multi_input = None
self.frame_multi_input = None
self.rows_a, self.cols_a = None, None
self.rows_b, self.cols_b = None, None
self.gui_multi_output = None
self.frame_multi_output = None
# create sub-menu window then withdraw main menu window
menu.gui_menu.withdraw()
self.gui_multi_menu = Toplevel()
self.gui_multi_menu.title("Multiply")
self.gui_multi_menu.resizable(False, False)
# create sub-menu frame
self.frame_multi_menu = Frame(self.gui_multi_menu, highlightbackground='black', highlightthickness=1)
self.frame_multi_menu.pack(fill=BOTH, expand=True, padx=5, pady=5)
# inputs Label(self.frame_multi_menu, text='NOTE: Matrix A height and Matrix B length').grid(row=1, column=1,
# column span =6) Label(self.frame_multi_menu, text='...are to be equal for multiplication').grid(row=2,
# column=1, column span =6) A matrix
# prompt dimensions
Label(self.frame_multi_menu, text='Matrix A dimensions:', font=('arial', 10, 'bold')).grid(row=3, column=1,
columnspan=1)
Label(self.frame_multi_menu, text='Matrix B dimensions:', font=('arial', 10, 'bold')).grid(row=4, column=1,
columnspan=1)
# create var for rows
self.ma_rows = IntVar()
self.ma_rows.set(2)
# drop down for rows
OptionMenu(self.frame_multi_menu, self.ma_rows, *range(2, 5)).grid(row=3, column=2)
# 'x'
Label(self.frame_multi_menu, text='x').grid(row=3, column=3)
# create var for cols
self.ma_cols = IntVar()
self.ma_cols.set(2)
OptionMenu(self.frame_multi_menu, self.ma_cols, *range(2, 5)).grid(row=3, column=4)
# B matrix
self.mb_rows = IntVar()
# self.mb_rows.set(self.ma_cols.get())
Label(self.frame_multi_menu, text="[n]", font=('arial', 10, 'bold'), padx=5, pady=5).grid(row=4, column=2)
# OptionMenu(self.frame_multi_menu, self.mb_rows, *range(2, 16)).grid(row=2, column=2)
Label(self.frame_multi_menu, text='x').grid(row=4, column=3)
self.mb_cols = IntVar()
self.mb_cols.set(2)
OptionMenu(self.frame_multi_menu, self.mb_cols, *range(2, 5)).grid(row=4, column=4)
# in order to move to input window
Button(self.frame_multi_menu, text='Enter', padx=16, pady=5, command=self.input_matrix).grid(row=5, column=4)
# gui stuff
self.gui_multi_menu.protocol("WM_DELETE_WINDOW", menu.gui_menu.destroy)
self.gui_multi_menu.mainloop()