Spaces:
Running
Running
Update solver.py
Browse files
solver.py
CHANGED
@@ -98,6 +98,7 @@ def process_expression(expr_str):
|
|
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()
|
@@ -139,83 +140,90 @@ def process_expression(expr_str):
|
|
139 |
raise Exception(f"Error processing expression: {str(e)}")
|
140 |
|
141 |
def solve_equation(equation_str):
|
142 |
-
"""
|
143 |
try:
|
144 |
if '=' not in equation_str:
|
145 |
return process_expression(equation_str)
|
146 |
|
147 |
-
#
|
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
|
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 |
-
|
|
|
|
|
|
|
|
|
162 |
|
163 |
-
#
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
except Exception as e:
|
172 |
-
|
173 |
|
174 |
-
def
|
175 |
-
"""
|
176 |
-
steps = []
|
177 |
try:
|
178 |
-
|
179 |
-
|
180 |
-
result = process_expression(equation_str)
|
181 |
-
steps.append(f"2. Result: {result}")
|
182 |
-
return steps
|
183 |
|
184 |
-
|
185 |
-
|
186 |
|
187 |
-
#
|
188 |
-
|
|
|
|
|
|
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
|
195 |
-
|
196 |
-
steps.append(f"1. Original equation: {format_expression(left_expr)} = {format_expression(right_expr)}")
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
steps.append(f"2. Move all terms to left side: {format_expression(equation)} = 0")
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
|
|
206 |
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
|
|
211 |
|
212 |
-
|
213 |
-
steps.append("
|
214 |
-
|
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 |
-
|
|
|
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()
|
|
|
140 |
raise Exception(f"Error processing expression: {str(e)}")
|
141 |
|
142 |
def solve_equation(equation_str):
|
143 |
+
"""Solves an equation and returns a detailed step-by-step solution."""
|
144 |
try:
|
145 |
if '=' not in equation_str:
|
146 |
return process_expression(equation_str)
|
147 |
|
148 |
+
# Split into left and right-hand sides
|
|
|
|
|
|
|
149 |
left_side, right_side = [side.strip() for side in equation_str.split('=')]
|
150 |
+
|
151 |
+
# Parse expressions
|
152 |
transformations = standard_transformations + (implicit_multiplication_application,)
|
153 |
+
left_expr = sp.parse_expr(left_side, transformations=transformations)
|
154 |
+
right_expr = sp.parse_expr(right_side, transformations=transformations)
|
155 |
equation = left_expr - right_expr
|
156 |
|
|
|
157 |
x = Symbol('x')
|
158 |
+
solutions = solve(equation, x)
|
159 |
+
|
160 |
+
steps = []
|
161 |
+
steps.append(f"**Step 1:** Original equation: \n{format_expression(left_expr)} = {format_expression(right_expr)}")
|
162 |
+
steps.append(f"**Step 2:** Move all terms to one side: \n{format_expression(equation)} = 0")
|
163 |
|
164 |
+
# Factoring if possible
|
165 |
+
factored = sp.factor(equation)
|
166 |
+
if factored != equation:
|
167 |
+
steps.append(f"**Step 3:** Factorizing the equation: \n{format_expression(factored)} = 0")
|
168 |
+
|
169 |
+
# Solve for x
|
170 |
+
steps.append(f"**Step 4:** Solving for x:")
|
171 |
+
for sol in solutions:
|
172 |
+
steps.append(f" x = {format_expression(sol)}")
|
173 |
+
|
174 |
+
# Verification
|
175 |
+
steps.append("**Step 5:** Verification:")
|
176 |
+
for sol in solutions:
|
177 |
+
verification = equation.subs(x, sol)
|
178 |
+
steps.append(f" When x = {format_expression(sol)}, the equation evaluates to {format_expression(verification)}")
|
179 |
+
|
180 |
+
return "\n".join(steps)
|
181 |
|
182 |
except Exception as e:
|
183 |
+
return f"Error: {str(e)}"
|
184 |
|
185 |
+
def integrate_expression(expression_str):
|
186 |
+
"""Computes the integral of a given expression and provides detailed steps."""
|
|
|
187 |
try:
|
188 |
+
x = Symbol('x')
|
189 |
+
expr = sp.parse_expr(expression_str, transformations=standard_transformations + (implicit_multiplication_application,))
|
|
|
|
|
|
|
190 |
|
191 |
+
steps = []
|
192 |
+
steps.append(f"**Step 1:** Original integral: \n∫ {format_expression(expr)} dx")
|
193 |
|
194 |
+
# Check if substitution can simplify
|
195 |
+
if '^' in expression_str:
|
196 |
+
steps.append("**Step 2:** Substituting variables if needed")
|
197 |
+
# Example: If we have x^n, we might use substitution
|
198 |
+
# More cases can be handled as needed
|
199 |
|
200 |
+
result = integrate(expr, x)
|
201 |
+
steps.append(f"**Step 3:** Applying integration formula(s):")
|
202 |
+
steps.append(f" ∫ f(x) dx = F(x) + C")
|
203 |
+
steps.append(f"**Step 4:** Solution: \n{format_expression(result)} + C")
|
204 |
|
205 |
+
return "\n".join(steps)
|
|
|
206 |
|
207 |
+
except Exception as e:
|
208 |
+
return f"Error: {str(e)}"
|
|
|
209 |
|
210 |
+
def laplace_transform_expression(expression_str):
|
211 |
+
"""Computes the Laplace transform of a given expression with detailed steps."""
|
212 |
+
try:
|
213 |
+
t, s = sp.symbols('t s')
|
214 |
+
expr = sp.parse_expr(expression_str, transformations=standard_transformations + (implicit_multiplication_application,))
|
215 |
|
216 |
+
steps = []
|
217 |
+
steps.append(f"**Step 1:** Original function: \nL[{format_expression(expr)}](t)")
|
218 |
+
|
219 |
+
# Compute Laplace transform
|
220 |
+
L_transform = laplace_transform(expr, t, s, noconds=True)
|
221 |
|
222 |
+
steps.append(f"**Step 2:** Applying Laplace Transform:")
|
223 |
+
steps.append(" L[f(t)] = ∫ e^(-st) f(t) dt, from 0 to ∞")
|
224 |
+
steps.append(f"**Step 3:** Solution: \n{format_expression(L_transform)}")
|
|
|
|
|
225 |
|
226 |
+
return "\n".join(steps)
|
227 |
|
228 |
except Exception as e:
|
229 |
+
return f"Error: {str(e)}"
|