Spaces:
Running
Running
Update solver.py
Browse files
solver.py
CHANGED
@@ -4,12 +4,8 @@ from sympy.solvers import solve
|
|
4 |
from sympy import integrate, diff, latex,simplify, expand, log, exp, sin, cos, tan, asin, acos, atan, Symbol, factorial, laplace_transform
|
5 |
import re
|
6 |
def format_expression(expr):
|
7 |
-
|
8 |
-
|
9 |
-
if isinstance(expr, (sp.Integer, sp.Float)):
|
10 |
-
return str(float(expr))
|
11 |
-
latex_expr = latex(expr)
|
12 |
-
replacements = {
|
13 |
'**': '^', # Power notation
|
14 |
'*x': 'x', # Remove unnecessary multiplication signs
|
15 |
'*(': '(', # Remove multiplication before parentheses
|
@@ -44,12 +40,10 @@ def format_expression(expr):
|
|
44 |
'Re': 'β', # Real part
|
45 |
'Im': 'β' # Imaginary part
|
46 |
}
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
return latex_expr
|
52 |
-
|
53 |
|
54 |
def preprocess_equation(equation_str):
|
55 |
"""Convert user-friendly equation format to SymPy format."""
|
@@ -118,6 +112,13 @@ def process_expression(expr_str):
|
|
118 |
expr = parse_expr(expr_to_diff, transformations=(standard_transformations + (implicit_multiplication_application,)))
|
119 |
result = diff(expr, x)
|
120 |
return f"d/dx({format_expression(expr)}) = {format_expression(result)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
elif 'factorial' in processed_expr: # Factorial case
|
123 |
expr = parse_expr(processed_expr, transformations=(standard_transformations + (implicit_multiplication_application,)))
|
|
|
4 |
from sympy import integrate, diff, latex,simplify, expand, log, exp, sin, cos, tan, asin, acos, atan, Symbol, factorial, laplace_transform
|
5 |
import re
|
6 |
def format_expression(expr):
|
7 |
+
latex_expr = latex(expr)
|
8 |
+
replacements = {
|
|
|
|
|
|
|
|
|
9 |
'**': '^', # Power notation
|
10 |
'*x': 'x', # Remove unnecessary multiplication signs
|
11 |
'*(': '(', # Remove multiplication before parentheses
|
|
|
40 |
'Re': 'β', # Real part
|
41 |
'Im': 'β' # Imaginary part
|
42 |
}
|
43 |
+
for old, new in replacements.items():
|
44 |
+
latex_expr = latex_expr.replace(old, new)
|
45 |
+
|
46 |
+
return f"$$ {latex_expr} $$"
|
|
|
|
|
47 |
|
48 |
def preprocess_equation(equation_str):
|
49 |
"""Convert user-friendly equation format to SymPy format."""
|
|
|
112 |
expr = parse_expr(expr_to_diff, transformations=(standard_transformations + (implicit_multiplication_application,)))
|
113 |
result = diff(expr, x)
|
114 |
return f"d/dx({format_expression(expr)}) = {format_expression(result)}"
|
115 |
+
|
116 |
+
elif 'sqrt' in processed_expr: # Handle square roots
|
117 |
+
expr = parse_expr(processed_expr, transformations=(standard_transformations + (implicit_multiplication_application,)))
|
118 |
+
result = sp.sqrt(expr)
|
119 |
+
positive_root = result
|
120 |
+
negative_root = -result
|
121 |
+
return f"β{format_expression(expr)} = Β±{format_expression(positive_root)}"
|
122 |
|
123 |
elif 'factorial' in processed_expr: # Factorial case
|
124 |
expr = parse_expr(processed_expr, transformations=(standard_transformations + (implicit_multiplication_application,)))
|