-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
51 lines (45 loc) · 1.53 KB
/
calculator.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
import sympy as sp
import numpy as np
class Calculator:
def __init__(self):
pass
def evaluate_expression(self, expression):
try:
result = sp.sympify(expression)
return str(result)
except Exception as e:
return f"Error: {str(e)}"
def integrate_expression(self, expression, variable):
try:
var = sp.symbols(variable)
result = sp.integrate(expression, var)
return str(result)
except Exception as e:
return f"Error: {str(e)}"
def differentiate_expression(self, expression, variable):
try:
var = sp.symbols(variable)
result = sp.diff(expression, var)
return str(result)
except Exception as e:
return f"Error: {str(e)}"
def solve_equation(self, equation, variable):
try:
var = sp.symbols(variable)
result = sp.solve(equation, var)
return str(result)
except Exception as e:
return f"Error: {str(e)}"
def matrix_operations(self, operation, *matrices):
try:
if operation == 'add':
result = np.add(*matrices)
elif operation == 'subtract':
result = np.subtract(*matrices)
elif operation == 'multiply':
result = np.matmul(*matrices)
else:
return "Error: Operación no soportada"
return str(result)
except Exception as e:
return f"Error: {str(e)}"