Taizun commited on
Commit
f4b04e0
·
verified ·
1 Parent(s): 7bce5e6

Update solver.py

Browse files
Files changed (1) hide show
  1. solver.py +63 -55
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
- """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)}")
 
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)}"