Taizun commited on
Commit
765211f
·
verified ·
1 Parent(s): fef8a14

Update solver.py

Browse files
Files changed (1) hide show
  1. solver.py +34 -24
solver.py CHANGED
@@ -139,16 +139,18 @@ def process_expression(expr_str):
139
  except Exception as e:
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)
@@ -158,24 +160,21 @@ def solve_equation(equation_str):
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
 
@@ -183,24 +182,21 @@ def solve_equation(equation_str):
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
 
@@ -208,22 +204,36 @@ def integrate_expression(expression_str):
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)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Solves an equation and returns a detailed LaTeX-formatted step-by-step solution."""
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 = sp.parse_expr(left_side, transformations=transformations)
156
  right_expr = sp.parse_expr(right_side, transformations=transformations)
 
160
  solutions = solve(equation, x)
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
  steps.append(f"**Step 4:** Solving for x:")
171
  for sol in solutions:
172
+ steps.append(f" x = {to_latex(sol)}")
173
 
 
174
  steps.append("**Step 5:** Verification:")
175
  for sol in solutions:
176
  verification = equation.subs(x, sol)
177
+ steps.append(f" When x = {to_latex(sol)}, the equation evaluates to {to_latex(verification)}")
178
 
179
  return "\n".join(steps)
180
 
 
182
  return f"Error: {str(e)}"
183
 
184
  def integrate_expression(expression_str):
185
+ """Computes the integral of a given expression and provides detailed LaTeX-formatted steps."""
186
  try:
187
  x = Symbol('x')
188
  expr = sp.parse_expr(expression_str, transformations=standard_transformations + (implicit_multiplication_application,))
189
 
190
  steps = []
191
+ steps.append(f"**Step 1:** Original integral: \n$$ \\int {sp.latex(expr)} \\,dx $$")
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
  return "\n".join(steps)
202
 
 
204
  return f"Error: {str(e)}"
205
 
206
  def laplace_transform_expression(expression_str):
207
+ """Computes the Laplace transform of a given expression with LaTeX-formatted steps."""
208
  try:
209
  t, s = sp.symbols('t s')
210
  expr = sp.parse_expr(expression_str, transformations=standard_transformations + (implicit_multiplication_application,))
211
 
212
  steps = []
213
+ steps.append(f"**Step 1:** Original function: \n$$ \\mathcal{L}\\{{ {sp.latex(expr)} \\}}(t) $$")
214
 
 
215
  L_transform = laplace_transform(expr, t, s, noconds=True)
216
 
217
  steps.append(f"**Step 2:** Applying Laplace Transform:")
218
+ steps.append("$$ \\mathcal{L}\\{ f(t) \\} = \\int_{0}^{\\infty} e^{-st} f(t) \\,dt $$")
219
+ steps.append(f"**Step 3:** Solution: \n$$ {sp.latex(L_transform)} $$")
220
 
221
  return "\n".join(steps)
222
 
223
  except Exception as e:
224
  return f"Error: {str(e)}"
225
+
226
+ def process_input(equation):
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
+ return f"Error processing input: {str(e)}"