-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
417 lines (330 loc) · 13 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from matrix import Matrix_Calc, clear_screen
from math import ceil
from sympy import pprint
from pyfiglet import figlet_format
import os
def main_program():
clear_screen()
print()
print(figlet_format("Matrix Master",font='cybermedium'))
print("Welcome to Matrix Master")
input("Press Enter to continue")
start_user_loop(Matrix_Calc())
def start_user_loop(Matrix_Calc):
# Start a loop presenting options to the user
invalid = ""
while True:
clear_screen()
if invalid:
print(invalid)
invalid = ""
Matrix_Calc.multi_matrix_print()
print("\nPlease choose an option:")
print("1) Load File")
print("2) Save to File")
print("3) Delete Files")
print("4) >> Matrix Operations")
print("5) Create Matrix")
print("6) Delete Matrix")
print("7) Quit")
print(f"p) Print Mode: {Matrix_Calc.mode}")
user_input = input("Enter your choice: ")
if user_input == '1':
invalid = Matrix_Calc.load_from_file()
elif user_input == '2':
Matrix_Calc.save_to_file()
elif user_input == '3':
Matrix_Calc.delete_files()
elif user_input == '4':
operations(Matrix_Calc)
elif user_input == '5':
Matrix_Calc.create_matrix()
elif user_input == '6':
Matrix_Calc.delete_matrix()
elif user_input == '7':
return
elif user_input.lower() == 'p':
Matrix_Calc.toggle_print_mode()
continue
else:
invalid = "Invalid selection"
def operations(Matrix_Calc):
invalid = ""
while True:
clear_screen()
if invalid:
print(invalid)
invalid = ""
if not Matrix_Calc.matrices:
Matrix_Calc.load_from_file()
clear_screen()
Matrix_Calc.multi_matrix_print()
print("\nOperations:")
print("1) Single matrix operations")
print("2) Two matrix operations")
print("3) Delete a matrix")
print("c) Clear All")
print("x) Go back")
print(f"p) Print Mode: {Matrix_Calc.mode}")
user_choice = input("Please enter your choice (1-3, x): ")
# Single Matrix Operations
if user_choice == '1':
invalid = ""
while True:
clear_screen()
if invalid:
print(invalid)
invalid = ""
# Print available matrices
if len(Matrix_Calc.matrices) > 1:
print("\nPlease choose a matrix:")
Matrix_Calc.print_heading()
matrix_choice = input("\nEnter your choice ('x' to go back): ")
if matrix_choice.lower() == 'x':
break
if matrix_choice.isdigit() and int(matrix_choice) - 1 in range(len(Matrix_Calc.matrices)):
matrix_choice = int(matrix_choice) - 1
single_matrix_operations(Matrix_Calc,matrix_choice)
break
else:
invalid = f"Invalid matrix choice. Please choose a number between 1 and {len(Matrix_Calc.matrices)}."
elif len(Matrix_Calc.matrices) == 1:
single_matrix_operations(Matrix_Calc,0)
break
elif user_choice == '2':
while len(Matrix_Calc.matrices) < 2:
print("\nYou need at least 2 matrices for this operation.")
if not Matrix_Calc.load_from_file(overwrite=False):
break
if len(Matrix_Calc.matrices) >= 2:
multi_matrix_operations(Matrix_Calc)
elif user_choice == '3':
# Delete a matrix
if Matrix_Calc.delete_matrix():
return
if user_choice.lower() == 'p':
Matrix_Calc.toggle_print_mode()
continue
if user_choice.lower() == 'c':
Matrix_Calc.matrices = []
return
elif user_choice.lower() == 'x':
return
else:
invalid = "Invalid choice. Please choose 1, 2, 3, or x."
def single_matrix_operations(Matrix_Calc, matrix_num):
invalid = ""
while True:
clear_screen()
if invalid:
print(invalid)
invalid = ""
matrix_title, matrix = Matrix_Calc.matrices[matrix_num]
print(f"\nMatrix {matrix_title}:\n")
Matrix_Calc.single_matrix_print(matrix) # pretty print the matrix
space = 24
categories = {
'1': ("{:<{width}}".format('EDIT MATRIX:', width=space) , 'Replace variables, Add, Delete, Modify', {
'1': ('Replace variables', Matrix_Calc.eval_variables),
'2': ('Add a row', Matrix_Calc.add_row),
'3': ('Add a column', Matrix_Calc.add_column),
'4': ('Modify a row', Matrix_Calc.modify_row),
'5': ('Modify a column', Matrix_Calc.modify_column),
'6': ('Modify a cell', Matrix_Calc.modify_cell),
'7': ('Modify diagonal', Matrix_Calc.modify_diagonal),
'8': ('Modify matrix', Matrix_Calc.modify_matrix),
'9': ('Delete a row', Matrix_Calc.delete_row),
'10': ('Delete a column', Matrix_Calc.delete_column),
'11': ('Create column vector', Matrix_Calc.column_vector),
'12': ('Create Matrix', Matrix_Calc.create_matrix)
}),
'2': ("{:<{width}}".format('ROW OPERATIONS:', width=space) , 'Scale and Combine, Echelon Form, RREF', {
'1': ('Scale row', Matrix_Calc.scale_row),
'2': ('Rearrange rows', Matrix_Calc.rearrange),
'3': ('Scale and combine rows', Matrix_Calc.scale_and_combine),
'4': ('Transform to Echelon form', Matrix_Calc.echelon_form),
'5': ('Transform to RREF', Matrix_Calc.rref)
}),
'3': ("{:<{width}}".format('MATRIX TRANSFORMS:', width=space) , 'Scale, Tranpose, Invert, etc.', {
'1': ('Scale matrix', Matrix_Calc.scale_matrix),
'2': ('Transpose matrix', Matrix_Calc.transpose_matrix),
'3': ('Raise matrix to a power', Matrix_Calc.raise_matrix_to_power),
'4': ('Invert Matrix', Matrix_Calc.invert_matrix),
'5': ('Cofactor Matrix', Matrix_Calc.cofactor_matrix),
'6': ('Adjugate', Matrix_Calc.adjugate_matrix),
'7': ('Complex Conjugate', Matrix_Calc.complex_conjugate),
'8': ('Exponential of Matrix', Matrix_Calc.exponential_of_matrix),
}),
'4': ("{:<{width}}".format('DIFFERENCE ANGLES:', width=space) , 'Inner Product AᵀA, Projection onto subspace, Gram-Schmidt Process', {
'1': ('Inner Product', lambda matrix_num, new=False :Matrix_Calc.matrix_mult((matrix_num, matrix_num), new, reverse_mode=False, transpose=True)),
'2': ('Projection onto subspace',Matrix_Calc.projection_onto_subspace),
'3': ('Orthogonal, orthonormal basis',Matrix_Calc.gram_schmidt),
'4': ('Transform Vector',Matrix_Calc.transform_vector),
}),
'5': ("{:<{width}}".format('EIGENVALUES:', width=space) , 'Determinant, Characteristic Polynomial, Diagonalization', {
'1': ('Determinant', Matrix_Calc.determinant),
'2': ('A - λ', Matrix_Calc.minus_lambda),
'3': ('Eigenvalues', Matrix_Calc.eigenvects),
'4': ('Characteristic Polynomial', Matrix_Calc.char_poly),
'5': ('Diagonalization', Matrix_Calc.diagonalization),
'6': ('Jordan Form', Matrix_Calc.jordan_form)
}),
'6': ("{:<{width}}".format('SVD:', width=space) , 'Quadratric Form, Singular Value Decompoisition (SVD)', {
'1': ('Quadratric Form', Matrix_Calc.quadratic_form),
'2': ('Singular Value Decompoisition', Matrix_Calc.svd_decomposition),
}),
'x': ('Go back','', None),
'p': (f"Print Mode: {Matrix_Calc.mode}",'', None)
}
print("\nCategories of Single Matrix Operations:")
for key, (description, description_list, ops_dict) in categories.items():
print(f"{key}) {description}")
if ops_dict is not None:
for op_descr, _ in ops_dict.values():
print(f"\t{op_descr}")
category_choice = input("Please enter your choice of category (1-3, x): ")
if category_choice == 'p':
Matrix_Calc.toggle_print_mode()
continue
if category_choice in categories:
description, _, operations = categories[category_choice]
if operations is None:
return
else:
while True:
operations.update({'p': (f"Print Mode: {Matrix_Calc.mode}", None)})
clear_screen()
if invalid:
print(invalid)
invalid = ""
print(f"\nMatrix {matrix_title}:\n")
Matrix_Calc.single_matrix_print(matrix) # pretty print the matrix
print(f"\nOperations in {description}:")
for key, (op_description, _) in operations.items():
print(f" {key}) {op_description}")
operation_choice = input(f"Please enter your choice of operation (1-{len(operations.values())}, x): ")
if operation_choice == 'p':
Matrix_Calc.toggle_print_mode()
continue
if operation_choice.lower() == 'x':
break
# some operations do not generate a new matrix
no_matrix_output_ops = [Matrix_Calc.determinant]
if operation_choice in operations:
if operations[operation_choice][1] == Matrix_Calc.create_matrix:
if operations[operation_choice][1]():
matrix_num = len(Matrix_Calc.matrices) - 1
matrix_title, matrix = Matrix_Calc.matrices[matrix_num]
continue
string = "Create new matrix? ('n' for new, any other key to to overwrite) "
make_new = input(f"\n" + "* " * (len(string)//2) + f"\n{string}\n> > > ")
make_new = True if make_new.lower() == 'n' else False
op_description, operation = operations[operation_choice]
if operation(matrix_num,make_new):
if make_new:
matrix_num = len(Matrix_Calc.matrices) - 1
matrix_title, matrix = Matrix_Calc.matrices[matrix_num]
if op_description == 'A - λ':
lambda_remove = ['Eigenvalues','Characteristic Polynomial','Diagonalization']
delete_keys = []
for key, (op_description, _) in operations.items():
if op_description in lambda_remove:
delete_keys.append(key)
for key in delete_keys:
del operations[key]
clear_screen()
#if operations[operation_choice][1] not in no_matrix_output_ops:
print(f"{op_description} operation successfully executed.")
print(f"\nMatrix {matrix_title}:\n")
pprint(matrix)
input("\nEnter to continue")
else:
invalid = f"Invalid operation choice. Please choose between 1 and {len(operations.values())}, or 'x' to go back."
else:
invalid = f"Invalid category choice. Please choose from the aviable options, or 'x' to go back."
def multi_matrix_operations(Matrix_Calc):
invalid = ""
reverse_mode = False
while True:
clear_screen()
if invalid:
print(invalid)
invalid = ""
Matrix_Calc.multi_matrix_print()
space = 24
categories = {
'1': ("{:<{width}}".format('MATRIX ALGEBRA:', width=space) , 'Product, Sum, Dot Product, Merge', {
'1': ('Matrix product', Matrix_Calc.matrix_mult),
'2': ('Matrix sum', Matrix_Calc.matrix_sum),
'3': ('Dot Product', lambda indices, new=False, reverse_mode=False, transpose=True: Matrix_Calc.matrix_mult(indices, new, reverse_mode, transpose=True)),
'4': ('Merge Matrices', Matrix_Calc.matrix_merge)
}),
'2': ("{:<{width}}".format('ROW OPERATIONS:', width=space) , 'Replace Variables, Rearrange Rows, Scale and Combine', {
'1': ('Replace variables', Matrix_Calc.eval_variables),
'2': ('Scale row', Matrix_Calc.scale_row),
'3': ('Rearrange rows', Matrix_Calc.rearrange),
'4': ('Scale and combine rows', Matrix_Calc.scale_and_combine),
'r': (f"Reverse mode: {reverse_mode}", None)
}),
'x': ('Go back', '', None),
'p': (f"Print Mode: {Matrix_Calc.mode}", '', None)
}
print("\nCategories of Multi Matrix Operations:")
for key, (description, description_list, _) in categories.items():
print(f"{key}) {description}{description_list}")
# currently only operation of is multiplication
category_choice = input("Please enter your choice of category (1-w, x): ")
if category_choice.lower() == 'p':
Matrix_Calc.toggle_print_mode()
continue
if category_choice in categories:
description, _, operations = categories[category_choice]
if operations is None:
return
else:
invalid = False
indices = Matrix_Calc.get_matrix_choices(description)
if indices == []:
continue
elif description == 'Matrix Algebra' and len(indices) != 2:
continue
while True:
operations.update({'p': (f"Print Mode: {Matrix_Calc.mode}", None)})
clear_screen()
if invalid:
print(invalid)
invalid = ""
Matrix_Calc.print_matrix_indices(indices)
print(f"\nOperations in {description}:")
for key, (op_description, _) in operations.items():
print(f" {key}) {op_description}")
operation_choice = input(f"Please enter your choice of operation (1-{len(operations.values())}, x): ")
if operation_choice.lower() == 'x':
indices = None
break
if operation_choice.lower() == 'p':
Matrix_Calc.toggle_print_mode()
continue
if operation_choice.lower() == 'r':
reverse_mode = not reverse_mode
operations.update({'r': (f"Reverse mode: {reverse_mode}", None)})
continue
elif operation_choice in operations:
string = "Create new matrix? ('n' for new, any other key to to overwrite) "
make_new = input(f"\n" + "* " * (len(string)//2) + f"\n{string}\n> > > ")
make_new = True if make_new.lower() == 'n' else False
op_description, operation = operations[operation_choice]
result = operation(indices, new=make_new, reverse_mode=reverse_mode)
if description == 'Matrix Algebra':
break
elif result:
indices = update_indices(indices, len(Matrix_Calc.matrices))
else:
invalid = f"Invalid operation choice."
else:
invalid = "Invalid category choice."
def update_indices(indices, matrices_len):
choices_length = len(indices)
return list(matrices_len - choices_length + i for i in range(choices_length))
#if __name__ == "__main__":
main_program()