Spaces:
Sleeping
Sleeping
Update solver.py
Browse files
solver.py
CHANGED
|
@@ -98,7 +98,6 @@ def process_expression(expr_str):
|
|
| 98 |
try:
|
| 99 |
processed_expr = preprocess_equation(expr_str)
|
| 100 |
x = Symbol('x')
|
| 101 |
-
|
| 102 |
|
| 103 |
if expr_str.startswith('∫'): # Integration
|
| 104 |
expr_to_integrate = processed_expr[1:].strip()
|
|
@@ -139,101 +138,84 @@ def process_expression(expr_str):
|
|
| 139 |
except Exception as e:
|
| 140 |
raise Exception(f"Error processing expression: {str(e)}")
|
| 141 |
|
| 142 |
-
def to_latex(expr):
|
| 143 |
-
"""Converts a SymPy expression into LaTeX format."""
|
| 144 |
-
return f"$$ {sp.latex(expr)} $$"
|
| 145 |
-
|
| 146 |
def solve_equation(equation_str):
|
| 147 |
-
"""
|
| 148 |
try:
|
| 149 |
if '=' not in equation_str:
|
| 150 |
return process_expression(equation_str)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
left_side, right_side = [side.strip() for side in equation_str.split('=')]
|
| 153 |
-
|
|
|
|
| 154 |
transformations = standard_transformations + (implicit_multiplication_application,)
|
| 155 |
-
left_expr =
|
| 156 |
-
right_expr =
|
| 157 |
equation = left_expr - right_expr
|
| 158 |
|
|
|
|
| 159 |
x = Symbol('x')
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
steps = []
|
| 163 |
-
steps.append(f"**Step 1:** Original equation: \n{to_latex(left_expr)} = {to_latex(right_expr)}")
|
| 164 |
-
steps.append(f"**Step 2:** Move all terms to one side: \n{to_latex(equation)} = 0")
|
| 165 |
-
|
| 166 |
-
factored = sp.factor(equation)
|
| 167 |
-
if factored != equation:
|
| 168 |
-
steps.append(f"**Step 3:** Factorizing the equation: \n{to_latex(factored)} = 0")
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
steps.append(f" When x = {to_latex(sol)}, the equation evaluates to {to_latex(verification)}")
|
| 178 |
-
|
| 179 |
-
return "\n".join(steps)
|
| 180 |
|
| 181 |
except Exception as e:
|
| 182 |
-
|
| 183 |
|
| 184 |
-
def
|
| 185 |
-
"""
|
|
|
|
| 186 |
try:
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
if '^' in expression_str:
|
| 194 |
-
steps.append("**Step 2:** Checking if substitution is needed.")
|
| 195 |
-
|
| 196 |
-
result = integrate(expr, x)
|
| 197 |
-
steps.append(f"**Step 3:** Applying integration formula(s):")
|
| 198 |
-
steps.append(f"$$ \\int f(x) \\,dx = F(x) + C $$")
|
| 199 |
-
steps.append(f"**Step 4:** Solution: \n$$ {sp.latex(result)} + C $$")
|
| 200 |
|
| 201 |
-
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
expr = sp.parse_expr(expression_str, transformations=standard_transformations + (implicit_multiplication_application,))
|
| 211 |
|
| 212 |
-
|
| 213 |
-
steps.append(f"
|
| 214 |
|
| 215 |
-
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
| 220 |
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
-
|
| 227 |
-
"""Determine whether the input is an equation, an integral, or a Laplace transform."""
|
| 228 |
-
try:
|
| 229 |
-
if "laplace" in equation.lower():
|
| 230 |
-
solution = laplace_transform_expression(equation)
|
| 231 |
-
elif "integrate" in equation.lower():
|
| 232 |
-
solution = integrate_expression(equation)
|
| 233 |
-
else:
|
| 234 |
-
solution = solve_equation(equation)
|
| 235 |
-
|
| 236 |
-
return solution
|
| 237 |
|
| 238 |
except Exception as e:
|
| 239 |
-
|
|
|
|
| 98 |
try:
|
| 99 |
processed_expr = preprocess_equation(expr_str)
|
| 100 |
x = Symbol('x')
|
|
|
|
| 101 |
|
| 102 |
if expr_str.startswith('∫'): # Integration
|
| 103 |
expr_to_integrate = processed_expr[1:].strip()
|
|
|
|
| 138 |
except Exception as e:
|
| 139 |
raise Exception(f"Error processing expression: {str(e)}")
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
def solve_equation(equation_str):
|
| 142 |
+
"""Solve the given equation and return the solution."""
|
| 143 |
try:
|
| 144 |
if '=' not in equation_str:
|
| 145 |
return process_expression(equation_str)
|
| 146 |
|
| 147 |
+
# Preprocess equation
|
| 148 |
+
equation_str = preprocess_equation(equation_str)
|
| 149 |
+
|
| 150 |
+
# Split equation into left and right parts
|
| 151 |
left_side, right_side = [side.strip() for side in equation_str.split('=')]
|
| 152 |
+
|
| 153 |
+
# Parse both sides with implicit multiplication
|
| 154 |
transformations = standard_transformations + (implicit_multiplication_application,)
|
| 155 |
+
left_expr = parse_expr(left_side, transformations=transformations)
|
| 156 |
+
right_expr = parse_expr(right_side, transformations=transformations)
|
| 157 |
equation = left_expr - right_expr
|
| 158 |
|
| 159 |
+
# Solve the equation
|
| 160 |
x = Symbol('x')
|
| 161 |
+
solution = solve(equation, x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# Format solution
|
| 164 |
+
if len(solution) == 0:
|
| 165 |
+
return "No solution exists"
|
| 166 |
+
elif len(solution) == 1:
|
| 167 |
+
return f"x = {format_expression(solution[0])}"
|
| 168 |
+
else:
|
| 169 |
+
return "x = " + ", ".join([format_expression(sol) for sol in solution])
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
except Exception as e:
|
| 172 |
+
raise Exception(f"Invalid equation format: {str(e)}")
|
| 173 |
|
| 174 |
+
def generate_steps(equation_str):
|
| 175 |
+
"""Generate step-by-step solution for the equation or expression."""
|
| 176 |
+
steps = []
|
| 177 |
try:
|
| 178 |
+
if '=' not in equation_str:
|
| 179 |
+
steps.append(f"1. Original expression: {equation_str}")
|
| 180 |
+
result = process_expression(equation_str)
|
| 181 |
+
steps.append(f"2. Result: {result}")
|
| 182 |
+
return steps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
# Preprocess equation
|
| 185 |
+
processed_eq = preprocess_equation(equation_str)
|
| 186 |
|
| 187 |
+
# Split equation into left and right parts
|
| 188 |
+
left_side, right_side = [side.strip() for side in processed_eq.split('=')]
|
| 189 |
|
| 190 |
+
# Parse expressions with implicit multiplication
|
| 191 |
+
transformations = standard_transformations + (implicit_multiplication_application,)
|
| 192 |
+
left_expr = parse_expr(left_side, transformations=transformations)
|
| 193 |
+
right_expr = parse_expr(right_side, transformations=transformations)
|
|
|
|
| 194 |
|
| 195 |
+
# Step 1: Show original equation
|
| 196 |
+
steps.append(f"1. Original equation: {format_expression(left_expr)} = {format_expression(right_expr)}")
|
| 197 |
|
| 198 |
+
# Step 2: Move all terms to left side
|
| 199 |
+
equation = left_expr - right_expr
|
| 200 |
+
steps.append(f"2. Move all terms to left side: {format_expression(equation)} = 0")
|
| 201 |
|
| 202 |
+
# Step 3: Factor if possible
|
| 203 |
+
factored = sp.factor(equation)
|
| 204 |
+
if factored != equation:
|
| 205 |
+
steps.append(f"3. Factor the equation: {format_expression(factored)} = 0")
|
| 206 |
|
| 207 |
+
# Step 4: Solve
|
| 208 |
+
x = Symbol('x')
|
| 209 |
+
solution = solve(equation, x)
|
| 210 |
+
steps.append(f"4. Solve for x: x = {', '.join([format_expression(sol) for sol in solution])}")
|
| 211 |
|
| 212 |
+
# Step 5: Verify solutions
|
| 213 |
+
steps.append("5. Verify solutions:")
|
| 214 |
+
for sol in solution:
|
| 215 |
+
result = equation.subs(x, sol)
|
| 216 |
+
steps.append(f" When x = {format_expression(sol)}, equation equals {format_expression(result)}")
|
| 217 |
|
| 218 |
+
return steps
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
except Exception as e:
|
| 221 |
+
raise Exception(f"Error generating steps: {str(e)}")
|