Taizun commited on
Commit
2bad1d3
·
verified ·
1 Parent(s): e3ba37d

Update solver.py

Browse files
Files changed (1) hide show
  1. solver.py +36 -14
solver.py CHANGED
@@ -114,25 +114,47 @@ def process_expression(expr_str):
114
  return f"d/dx({format_expression(expr)}) = {format_expression(result)}"
115
 
116
 
117
- elif 'sqrt' in processed_expr: # Handle square roots
118
  try:
119
  transformations = standard_transformations + (implicit_multiplication_application,)
120
- expr = sp.parse_expr(processed_expr, transformations=transformations)
121
-
122
- sqrt_result = sqrt(expr) # Compute square root
123
- positive_root = sqrt_result
124
- negative_root = -sqrt_result
125
-
 
 
 
126
  steps = []
127
- steps.append(f"**Step 1:** Original expression: \n{latex(expr)}")
128
- steps.append(f"**Step 2:** Taking square root: \n{latex(sqrt_result)}")
129
- steps.append(f"**Step 3:** Considering both positive and negative roots: \n±{latex(sqrt_result)}")
130
-
131
- solution = "\n".join(steps)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  except Exception as e:
134
- solution = f"Error: {str(e)}"
135
-
136
  elif 'factorial' in processed_expr: # Factorial case
137
  expr = parse_expr(processed_expr, transformations=(standard_transformations + (implicit_multiplication_application,)))
138
  result = expr.doit() # Compute the factorial correctly
 
114
  return f"d/dx({format_expression(expr)}) = {format_expression(result)}"
115
 
116
 
117
+ elif 'sqrt' in processed_expr.lower():
118
  try:
119
  transformations = standard_transformations + (implicit_multiplication_application,)
120
+
121
+ # Remove "sqrt" and parse the expression inside
122
+ expr = sp.parse_expr(processed_expr.replace("sqrt", ""), transformations=transformations)
123
+
124
+ sqrt_result = sp.sqrt(expr)
125
+
126
+ # If it's sqrt(x^2), simplify it to |x|
127
+ simplified_result = sp.simplify(sqrt_result)
128
+
129
  steps = []
130
+ steps.append(f"**Step 1:** Original expression: \n{to_latex(expr)}")
131
+
132
+ # Case 1: Perfect Squares Show exact value (e.g., sqrt(9) = ±3)
133
+ if sqrt_result.is_Integer:
134
+ steps.append(f"**Step 2:** √{to_latex(expr)} is a perfect square")
135
+ steps.append(f"**Step 3:** Solution: \n±{to_latex(sqrt_result)}")
136
+ solution = "\n".join(steps)
137
+
138
+ # Case 2: Non-Perfect Squares → Show decimal value (e.g., sqrt(2) ≈ 1.41)
139
+ elif sqrt_result.is_real and not sqrt_result.is_rational:
140
+ decimal_value = float(sqrt_result.evalf())
141
+ steps.append(f"**Step 2:** √{to_latex(expr)} is not a perfect square")
142
+ steps.append(f"**Step 3:** Approximate value: \n{decimal_value}")
143
+ solution = "\n".join(steps)
144
+
145
+ # Case 3: Expressions like √x² → |x|
146
+ elif simplified_result != sqrt_result:
147
+ steps.append(f"**Step 2:** Simplification using identity: \n{to_latex(simplified_result)}")
148
+ solution = "\n".join(steps)
149
+
150
+ # Case 4: General Expression → Return as-is
151
+ else:
152
+ steps.append(f"**Step 2:** Taking square root: \n{to_latex(sqrt_result)}")
153
+ steps.append(f"**Step 3:** Considering both positive and negative roots: \n±{to_latex(sqrt_result)}")
154
+ solution = "\n".join(steps)
155
 
156
  except Exception as e:
157
+ solution = f"Error: {str(e)}"
 
158
  elif 'factorial' in processed_expr: # Factorial case
159
  expr = parse_expr(processed_expr, transformations=(standard_transformations + (implicit_multiplication_application,)))
160
  result = expr.doit() # Compute the factorial correctly