Update image.py
Browse files
image.py
CHANGED
|
@@ -128,25 +128,28 @@ def extract_polynomial_coefficients(latex_str):
|
|
| 128 |
"variable": "x"
|
| 129 |
}
|
| 130 |
|
| 131 |
-
def solve_polynomial(degree, coeff_string, real_only,
|
| 132 |
try:
|
|
|
|
| 133 |
coeffs = list(map(float, coeff_string.strip().split()))
|
| 134 |
if len(coeffs) != degree + 1:
|
| 135 |
return f"⚠️ Please enter exactly {degree + 1} coefficients.", None, None
|
| 136 |
|
| 137 |
-
|
| 138 |
-
poly = sum([coeffs[i] *
|
| 139 |
simplified = sp.simplify(poly)
|
| 140 |
factored = sp.factor(simplified)
|
| 141 |
-
roots = sp.solve(sp.Eq(simplified, 0),
|
| 142 |
|
| 143 |
if real_only:
|
| 144 |
roots = [r for r in roots if sp.im(r) == 0]
|
| 145 |
|
|
|
|
| 146 |
roots_output = "$$\n" + "\\ ".join(
|
| 147 |
[f"r_{{{i}}} = {sp.latex(sp.nsimplify(r, rational=True))}" for i, r in enumerate(roots, 1)]
|
| 148 |
) + "\n$$"
|
| 149 |
|
|
|
|
| 150 |
steps_output = f"""
|
| 151 |
### Polynomial Expression
|
| 152 |
$$ {sp.latex(poly)} = 0 $$
|
|
@@ -158,6 +161,7 @@ $$ {sp.latex(factored)} = 0 $$
|
|
| 158 |
{roots_output}
|
| 159 |
"""
|
| 160 |
|
|
|
|
| 161 |
x_vals = np.linspace(-10, 10, 400)
|
| 162 |
y_vals = np.polyval(coeffs, x_vals)
|
| 163 |
|
|
@@ -167,8 +171,8 @@ $$ {sp.latex(factored)} = 0 $$
|
|
| 167 |
ax.axvline(0, color='black', linewidth=0.5)
|
| 168 |
ax.grid(True)
|
| 169 |
ax.set_title("Graph of the Polynomial")
|
| 170 |
-
ax.set_xlabel(
|
| 171 |
-
ax.set_ylabel("f(
|
| 172 |
ax.legend()
|
| 173 |
|
| 174 |
return steps_output, fig, ""
|
|
|
|
| 128 |
"variable": "x"
|
| 129 |
}
|
| 130 |
|
| 131 |
+
def solve_polynomial(degree, coeff_string, real_only, variable_name="x"):
|
| 132 |
try:
|
| 133 |
+
variable = sp.Symbol(variable_name)
|
| 134 |
coeffs = list(map(float, coeff_string.strip().split()))
|
| 135 |
if len(coeffs) != degree + 1:
|
| 136 |
return f"⚠️ Please enter exactly {degree + 1} coefficients.", None, None
|
| 137 |
|
| 138 |
+
# Build the polynomial expression
|
| 139 |
+
poly = sum([coeffs[i] * variable**(degree - i) for i in range(degree + 1)])
|
| 140 |
simplified = sp.simplify(poly)
|
| 141 |
factored = sp.factor(simplified)
|
| 142 |
+
roots = sp.solve(sp.Eq(simplified, 0), variable)
|
| 143 |
|
| 144 |
if real_only:
|
| 145 |
roots = [r for r in roots if sp.im(r) == 0]
|
| 146 |
|
| 147 |
+
# Format roots in LaTeX
|
| 148 |
roots_output = "$$\n" + "\\ ".join(
|
| 149 |
[f"r_{{{i}}} = {sp.latex(sp.nsimplify(r, rational=True))}" for i, r in enumerate(roots, 1)]
|
| 150 |
) + "\n$$"
|
| 151 |
|
| 152 |
+
# Format steps in LaTeX
|
| 153 |
steps_output = f"""
|
| 154 |
### Polynomial Expression
|
| 155 |
$$ {sp.latex(poly)} = 0 $$
|
|
|
|
| 161 |
{roots_output}
|
| 162 |
"""
|
| 163 |
|
| 164 |
+
# Generate plot using numeric x-axis
|
| 165 |
x_vals = np.linspace(-10, 10, 400)
|
| 166 |
y_vals = np.polyval(coeffs, x_vals)
|
| 167 |
|
|
|
|
| 171 |
ax.axvline(0, color='black', linewidth=0.5)
|
| 172 |
ax.grid(True)
|
| 173 |
ax.set_title("Graph of the Polynomial")
|
| 174 |
+
ax.set_xlabel(str(variable))
|
| 175 |
+
ax.set_ylabel("f(" + str(variable) + ")")
|
| 176 |
ax.legend()
|
| 177 |
|
| 178 |
return steps_output, fig, ""
|