-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
285 lines (193 loc) · 9.14 KB
/
app.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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Dict, Optional, Union
from sympy import Matrix, parse_expr, latex, symbols, Eq, solve, factor, Symbol
from sympy.matrices.common import ShapeError, NonSquareMatrixError, NonInvertibleMatrixError
import numpy as np
import re
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class RootsRequest(BaseModel):
equation: str
class SimultaneousEquation(BaseModel):
equation: Dict[str, str]
class MatrixRequestHistory(BaseModel):
matrixHistory: Dict[str, Union[List[List[Union[str, int, float]]], str, int, float]]
equation: str
class MatrixRequest(BaseModel):
matrix: List[List[str]] = []
power: Optional[int] = None
def convert_to_katex(latex_expression):
latex_expression = latex_expression.replace(r'\\', r' \\newline ')
latex_expression = latex_expression.replace(r'\left[\begin{matrix}', r'\\begin{bmatrix}')
latex_expression = latex_expression.replace(r'\end{matrix}\right]', r'\\end{bmatrix}')
return latex_expression
@app.post("/api/matrix/square")
async def matrix_square(data: MatrixRequest):
matrix = np.array([[int(num) for num in row] for row in data.matrix])
matrix_squared = np.round(np.matmul(matrix, matrix), decimals=2)
return {"result": matrix_squared.tolist()}
@app.post("/api/matrix/power")
async def matrix_square(data: MatrixRequest):
print(data)
matrix = np.array([[int(num) for num in row] for row in data.matrix])
if data.power is not None:
matrix_powered = np.round(np.linalg.matrix_power(matrix, data.power), decimals=2)
else:
matrix_powered = np.round(np.linalg.matrix_power(matrix, 2), decimals=2)
print(matrix_powered.tolist())
return {"result": matrix_powered.tolist()}
@app.post("/api/matrix/inverse")
async def matrix_square(data: MatrixRequest):
try:
matrix = np.array([[int(num) for num in row] for row in data.matrix])
matrix_inverse = np.round(np.linalg.inv(matrix), decimals=2)
return {"result": matrix_inverse.tolist()}
except np.linalg.LinAlgError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/api/matrix/trace")
async def matrix_square(data: MatrixRequest):
matrix = np.array([[int(num) for num in row] for row in data.matrix])
matrix_trace = np.trace(matrix)
if isinstance(matrix_trace, float):
matrix_trace = round(matrix_trace, 3)
return {"result": str(matrix_trace)}
@app.post("/api/matrix/determinant")
async def matrix_square(data: MatrixRequest):
matrix = np.array([[int(num) for num in row] for row in data.matrix])
matrix_det = np.linalg.det(matrix)
if isinstance(matrix_det, float):
matrix_det = round(matrix_det, 3)
return {"result": str(matrix_det)}
@app.post("/api/matrix/rref")
async def matrix_rref(data: MatrixRequest):
matrix = np.array([[int(num) for num in row] for row in data.matrix])
sympy_matrix = Matrix(matrix)
rref_matrix = sympy_matrix.rref()[0]
np_matrix = np.round(np.array(rref_matrix.tolist(), dtype=float), decimals=2)
return {"result": np_matrix.tolist()}
@app.post("/api/matrix/transpose")
async def matrix_square(data: MatrixRequest):
matrix = np.array([[int(num) for num in row] for row in data.matrix])
matrix_transpose = matrix.T
return {"result": matrix_transpose.tolist()}
@app.post("/api/matrix/eigen")
async def matrix_eigen_value(data: MatrixRequest):
matrix = np.array([[int(num) for num in row] for row in data.matrix])
eigenvalues, eigenvectors = np.linalg.eig(matrix)
eigenvalues = np.round(eigenvalues, decimals=2)
eigenvectors = np.round(eigenvectors, decimals=2)
# Convert complex values to strings
eigenvalues = [str(value) for value in eigenvalues]
eigenvectors = [[str(value) for value in vector] for vector in eigenvectors]
return {"value": eigenvalues, "vector": eigenvectors}
@app.post("/api/matrix/equation")
async def matrix_equation(data: MatrixRequestHistory):
try:
matrixHistory = data.matrixHistory
equation = data.equation
variable_symbols = set()
operation_symbols = set()
index = 0
while index < len(equation):
char = equation[index]
if char.isalpha():
print(char)
if char == 'T':
index += 1
continue
if equation[index:index+3] == 'det':
operation_symbols.add('det')
index += 2
elif equation[index:index+5] == 'trace':
operation_symbols.add('trace')
index += 4
elif equation[index:index+4] == 'rref':
operation_symbols.add('rref')
index += 3
else:
variable_symbols.add(char)
index += 1
matrix_values = {symbol: Matrix(matrix) for symbol, matrix in matrixHistory.items()}
expression = equation.strip("'").replace('^', '**').replace('{', '(').replace('}', ')').replace('**T', '.transpose()')
substitution = {}
for symbol in variable_symbols:
symbol_power = symbol + '^2'
symbol_inverse = symbol + '^(-1)'
if symbol_power in equation:
matrix_values[symbol_power] = matrix_values[symbol] ** 2
if symbol_inverse in equation:
matrix_values[symbol_inverse] = matrix_values[symbol].inv()
if 'det' in operation_symbols:
determinant = round(matrix_values[symbol].det(), 2)
expression = expression.replace('det({})'.format(symbol), str(determinant))
if 'trace' in operation_symbols:
trace = round(matrix_values[symbol].trace(), 2)
expression = expression.replace('trace({})'.format(symbol), str(trace))
substitution[symbol] = matrix_values[symbol]
expression_expr = parse_expr(expression)
substituted_expression = expression_expr.subs(substitution)
result = substituted_expression.evalf()
if isinstance(result, Matrix):
np_matrix = np.round(np.array(result.tolist(), dtype=float), decimals=2)
result_list = np_matrix.tolist()
result_type = "matrix"
else:
result_type = "scalar"
latex_expression = latex(result_list if result_type == "matrix" else result)
latex_expression = latex_expression.replace(r'\\', r' \newline ')
latex_expression = latex_expression.replace(r'\left[\begin{matrix}', r'\begin{bmatrix}')
latex_expression = latex_expression.replace(r'\end{matrix}\right]', r'\end{bmatrix}')
result_output = {
"latex": latex_expression,
"type": result_type
}
return result_output
except (ShapeError, NonSquareMatrixError, NonInvertibleMatrixError) as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/api/roots/equation-solver")
async def equation_solver(data: RootsRequest):
expression = data.equation.strip("'").replace('^', '**').replace('{', '(').replace('}', ')').replace("\\", "")
if not expression.startswith('-') and not expression.startswith('+'):
expression = '+' + expression
equation = expression.replace('x', '*x')
equation = equation.split("=")
parsed_equation = parse_expr(equation[0])
x = symbols('x')
equation = Eq(parsed_equation, float(equation[1]))
solutions = solve(equation, x)
factors = factor(parsed_equation)
result = [];
for solution in solutions:
latex_roots = latex(round(solution, 3))
result.append(latex_roots)
latex_factors = latex(factors);
return {"roots": result, "factors": latex_factors}
@app.post("/api/roots/simultaneous-equation")
async def simultaneous_equation(data: SimultaneousEquation):
equations = data.equation
parsed_eqs = []
symbols = []
for eq in equations.values():
eq = eq.replace(' ', '')
lhs, rhs = eq.split('=')
lhs = lhs.replace('x', '*x').replace('y', '*y').replace('z','*z').replace('u','*u')
lhs = '(' + lhs.replace('=', '-') + ')'
parsed_eqs.append(lhs + '-' + rhs)
combined_equations = ' '.join(parsed_eqs)
variables = set(re.findall(r'[a-zA-Z]+', combined_equations))
sympy_eqs = [parse_expr(eq) for eq in parsed_eqs]
for variable in variables:
symbol = Symbol(variable)
symbols.append(symbol)
result = solve(sympy_eqs, symbols)
result_dict = {str(symbol): str(solution) for symbol, solution in result.items()}
print(result_dict)
return {"result": result_dict}