-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_api.py
185 lines (156 loc) · 5.25 KB
/
linear_api.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
# linear_api.py
##############################################################################
# Imports #
##############################################################################
from linear_algebra import *
##############################################################################
# CONSTANTS #
##############################################################################
LINEAR_EQUATION = 1
QUADRATIC_EQUATION = 2
ADD_MATRICES = 3
SUBTRACT_MATRICES = 4
MULTIPLY_MATRIX_BY_SCALAR = 5
MULTIPLY_MATRICES = 6
MATRIX_RANK = 7
MATRIX_SYMMETRIC = 8
MATRIX_INVERSE = 9
MATRIX_TRANSPOSE = 10
MATRIX_TRACE = 11
MATRIX_DETERMINANT = 12
EIGENVALUES_AND_EIGENVECTORS = 13
EXIT = 0
def print_linear_menu() -> int:
print("\nLinear Algebra Menu:")
print("1: Solve a linear equation")
print("2: Solve a quadratic equation")
print("3: Add two matrices")
print("4: Subtract two matrices")
print("5: Multiply a matrix by a scalar")
print("6: Multiply two matrices")
print("7: Calculates the rank of a matrix")
print("8: Check if a matrix is symmetric")
print("9: Calculate the inverse of a matrix")
print("10: Calculate the transpose of a matrix")
print("11: Calculate the trace of a matrix")
print("12: Calculate the determinant of a matrix")
print("13: Calculate the eigenvalues and eigenvectors of a matrix")
print("0: Exit")
la_option = int(input("Enter an option: "))
return la_option
def get_float_input(prompt: str) -> float:
"""
Get a float input from the user.
Parameters:
prompt (str): Prompt message to be displayed to the user.
Returns:
float: User input converted to float.
"""
while True:
try:
return float(input(prompt))
except ValueError:
print("Invalid input. Please enter a number.")
def get_int_input(prompt: str) -> int:
"""
Get an integer input from the user.
Parameters:
prompt (str): Prompt message to be displayed to the user.
Returns:
int: User input converted to integer.
"""
while True:
try:
return int(input(prompt))
except ValueError:
print("Invalid input. Please enter an integer.")
def get_vector_input() -> np.ndarray | None:
"""
Get a vector input from the user.
Returns:
numpy.ndarray: User input converted to a numpy array.
"""
while True:
try:
return np.array(list(map(float, input("Enter the vector elements separated by spaces: ").split())))
except ValueError:
print("Invalid input. Please enter a sequence of numbers separated by spaces.")
return None
def get_matrix_input() -> np.ndarray | None:
"""
Get a matrix input from the user.
Returns:
numpy.ndarray: User input converted to a numpy array.
"""
while True:
try:
rows = int(input("Enter the number of rows in the matrix: "))
if rows <= 0:
raise ValueError
return np.array(
[list(map(float, input(f"Enter the elements of row {i + 1} separated by spaces: ").split())) for i in
range(rows)])
except ValueError:
print("Invalid input. Please enter a sequence of numbers separated by spaces for each row.")
return None
def get_input_and_solve_quadratic() -> bool:
try:
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
except ValueError:
print("Error: Invalid input. Please enter a number.")
return False
solution = solve_quadratic(a, b, c)
if isinstance(solution, str):
print(solution)
elif isinstance(solution, tuple):
print(f"The solutions are {solution[0]} and {solution[1]}")
else:
print(f"The solution is {solution}")
return True
def get_input_and_solve_linear() -> bool:
try:
A = get_matrix_input()
b = get_vector_input()
except ValueError:
print("Error: Invalid input. Please enter a number.")
return False
solution = solve_linear(A, b)
if solution is not None:
print(f"The solution is {solution}")
return True
else:
return False
def linear_menu():
la_option = print_linear_menu()
if la_option == LINEAR_EQUATION:
get_input_and_solve_linear()
elif la_option == QUADRATIC_EQUATION:
get_input_and_solve_quadratic()
elif la_option == ADD_MATRICES:
pass
elif la_option == SUBTRACT_MATRICES:
pass
elif la_option == MULTIPLY_MATRIX_BY_SCALAR:
pass
elif la_option == MULTIPLY_MATRICES:
pass
elif la_option == MATRIX_RANK:
pass
elif la_option == MATRIX_SYMMETRIC:
pass
elif la_option == MATRIX_INVERSE:
pass
elif la_option == MATRIX_TRANSPOSE:
pass
elif la_option == MATRIX_TRACE:
pass
elif la_option == MATRIX_DETERMINANT:
pass
elif la_option == EIGENVALUES_AND_EIGENVECTORS:
pass
elif la_option == EXIT:
return
else:
print("Invalid option. Please try again.")