Spaces:
Sleeping
Sleeping
Update polynomial.py
Browse files- polynomial.py +4 -161
polynomial.py
CHANGED
|
@@ -88,8 +88,8 @@ def polynomial_tab():
|
|
| 88 |
|
| 89 |
# Row for example and preview buttons
|
| 90 |
with gr.Row():
|
| 91 |
-
example_btn = gr.Button("
|
| 92 |
-
preview_poly_button = gr.Button("
|
| 93 |
|
| 94 |
# Row for displaying the confirmed equation
|
| 95 |
with gr.Row():
|
|
@@ -97,165 +97,8 @@ def polynomial_tab():
|
|
| 97 |
|
| 98 |
# Row for confirm and cancel buttons
|
| 99 |
with gr.Row():
|
| 100 |
-
confirm_poly_btn = gr.Button("
|
| 101 |
-
cancel_poly_btn = gr.Button("
|
| 102 |
-
|
| 103 |
-
# Markdown component to display step-by-step solution
|
| 104 |
-
steps_md = gr.Markdown()
|
| 105 |
-
|
| 106 |
-
# Plot component to display the polynomial graph
|
| 107 |
-
plot_output = gr.Plot()
|
| 108 |
-
|
| 109 |
-
# Textbox to display errors (initially hidden)
|
| 110 |
-
error_box = gr.Textbox(visible=False)
|
| 111 |
-
|
| 112 |
-
# Function to preview the polynomial equation based on user input
|
| 113 |
-
def preview_polynomial(degree, coeff_string, real_only):
|
| 114 |
-
try:
|
| 115 |
-
coeffs = list(map(float, coeff_string.strip().split()))
|
| 116 |
-
if len(coeffs) != degree + 1:
|
| 117 |
-
return f"⚠️ Please enter exactly {degree + 1} coefficients.", gr.update(visible=False), gr.update(visible=False), "", None
|
| 118 |
-
poly = sum([coeffs[i] * x**(degree - i) for i in range(degree + 1)])
|
| 119 |
-
eq_latex = f"### ✅ Confirm Polynomial\n\n$$ {sp.latex(poly)} = 0 $$"
|
| 120 |
-
return eq_latex, gr.update(visible=True), gr.update(visible=True), "", None
|
| 121 |
-
except Exception as e:
|
| 122 |
-
return f"❌ Error parsing coefficients: {e}", gr.update(visible=False), gr.update(visible=False), "", None
|
| 123 |
-
|
| 124 |
-
# Event handler for preview button click
|
| 125 |
-
preview_poly_button.click(
|
| 126 |
-
fn=preview_polynomial,
|
| 127 |
-
inputs=[degree_slider, coeff_input, real_checkbox],
|
| 128 |
-
outputs=[poly_equation_display, confirm_poly_btn, cancel_poly_btn, steps_md, plot_output]
|
| 129 |
-
)
|
| 130 |
-
|
| 131 |
-
# Function to handle cancellation of the preview
|
| 132 |
-
def cancel_poly():
|
| 133 |
-
return gr.update(visible=False), gr.update(visible=False), "", "", None
|
| 134 |
-
|
| 135 |
-
# Event handler for cancel button click
|
| 136 |
-
cancel_poly_btn.click(
|
| 137 |
-
fn=cancel_poly,
|
| 138 |
-
inputs=[],
|
| 139 |
-
outputs=[confirm_poly_btn, cancel_poly_btn, poly_equation_display, steps_md, plot_output]
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
# Event handler for confirm button click to solve and display results
|
| 143 |
-
confirm_poly_btn.click(
|
| 144 |
-
fn=solve_polynomial,
|
| 145 |
-
inputs=[degree_slider, coeff_input, real_checkbox],
|
| 146 |
-
outputs=[steps_md, plot_output, error_box]
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
# Event handler to update the template when the degree slider changes
|
| 150 |
-
degree_slider.change(fn=generate_polynomial_template, inputs=degree_slider, outputs=template_display)
|
| 151 |
-
|
| 152 |
-
# Event handler to load an example when the example button is clicked
|
| 153 |
-
example_btn.click(fn=load_poly_example, inputs=degree_slider, outputs=coeff_input)
|
| 154 |
-
|
| 155 |
-
# Initialize the template display with the default degree (2) on load
|
| 156 |
-
template_display.value = generate_polynomial_template(2)
|
| 157 |
-
|
| 158 |
-
return template_display, real_checkbox, degree_slider, coeff_input, example_btn, preview_poly_button, poly_equation_display, confirm_poly_btn, cancel_poly_btn, steps_md, plot_output, error_boximport gradio as gr
|
| 159 |
-
import sympy as sp
|
| 160 |
-
import numpy as np
|
| 161 |
-
import matplotlib.pyplot as plt
|
| 162 |
-
|
| 163 |
-
# Define symbolic variable for polynomial operations
|
| 164 |
-
x = sp.symbols('x')
|
| 165 |
-
|
| 166 |
-
# Function to generate a LaTeX template for a polynomial based on its degree
|
| 167 |
-
def generate_polynomial_template(degree):
|
| 168 |
-
terms = [f"a_{{{i}}}x^{degree - i}" for i in range(degree)]
|
| 169 |
-
terms.append(f"a_{{{degree}}}")
|
| 170 |
-
return "$$" + " + ".join(terms) + " = 0$$"
|
| 171 |
-
|
| 172 |
-
# Function to load example coefficients for a given polynomial degree
|
| 173 |
-
def load_poly_example(degree):
|
| 174 |
-
examples = {
|
| 175 |
-
1: "3 9",
|
| 176 |
-
2: "1 -3 2",
|
| 177 |
-
3: "1 -6 11 -6",
|
| 178 |
-
4: "1 0 -5 0 4",
|
| 179 |
-
5: "1 -9 3 8 1 8",
|
| 180 |
-
6: "1 -9 3 8 1 8 3",
|
| 181 |
-
7: "1 -9 3 8 1 8 6 2",
|
| 182 |
-
8: "1 -9 3 8 1 8 2 3 7"
|
| 183 |
-
}
|
| 184 |
-
return examples.get(degree, "")
|
| 185 |
-
|
| 186 |
-
# Function to solve the polynomial equation and generate a graph
|
| 187 |
-
def solve_polynomial(degree, coeff_string, real_only):
|
| 188 |
-
try:
|
| 189 |
-
coeffs = list(map(float, coeff_string.strip().split()))
|
| 190 |
-
if len(coeffs) != degree + 1:
|
| 191 |
-
return f"⚠️ Please enter exactly {degree + 1} coefficients.", None, None
|
| 192 |
-
|
| 193 |
-
poly = sum([coeffs[i] * x**(degree - i) for i in range(degree + 1)])
|
| 194 |
-
simplified = sp.simplify(poly)
|
| 195 |
-
factored = sp.factor(simplified)
|
| 196 |
-
roots = sp.solve(sp.Eq(simplified, 0), x)
|
| 197 |
-
|
| 198 |
-
if real_only:
|
| 199 |
-
roots = [r for r in roots if sp.im(r) == 0]
|
| 200 |
-
|
| 201 |
-
roots_output = "$$\n" + "\\ ".join(
|
| 202 |
-
[f"r_{{{i}}} = {sp.latex(sp.nsimplify(r, rational=True))}" for i, r in enumerate(roots, 1)]
|
| 203 |
-
) + "\n$$"
|
| 204 |
-
|
| 205 |
-
steps_output = f"""
|
| 206 |
-
### Polynomial Expression
|
| 207 |
-
$$ {sp.latex(poly)} = 0 $$
|
| 208 |
-
### Simplified
|
| 209 |
-
$$ {sp.latex(simplified)} = 0 $$
|
| 210 |
-
### Factored
|
| 211 |
-
$$ {sp.latex(factored)} = 0 $$
|
| 212 |
-
### Roots {'(Only Real)' if real_only else '(All Roots)'}
|
| 213 |
-
{roots_output}
|
| 214 |
-
"""
|
| 215 |
-
|
| 216 |
-
x_vals = np.linspace(-10, 10, 400)
|
| 217 |
-
y_vals = np.polyval(coeffs, x_vals)
|
| 218 |
-
|
| 219 |
-
fig, ax = plt.subplots(figsize=(6, 4))
|
| 220 |
-
ax.plot(x_vals, y_vals, label="Polynomial", color="blue")
|
| 221 |
-
ax.axhline(0, color='black', linewidth=0.5)
|
| 222 |
-
ax.axvline(0, color='black', linewidth=0.5)
|
| 223 |
-
ax.grid(True)
|
| 224 |
-
ax.set_title("Graph of the Polynomial")
|
| 225 |
-
ax.set_xlabel("x")
|
| 226 |
-
ax.set_ylabel("f(x)")
|
| 227 |
-
ax.legend()
|
| 228 |
-
|
| 229 |
-
return steps_output, fig, ""
|
| 230 |
-
except Exception as e:
|
| 231 |
-
return f"❌ Error: {e}", None, ""
|
| 232 |
-
|
| 233 |
-
# Function to create the Polynomial Solver tab with Gradio components
|
| 234 |
-
def polynomial_tab():
|
| 235 |
-
with gr.Tab("Polynomial Solver"):
|
| 236 |
-
# Row for displaying the equation template and real roots checkbox
|
| 237 |
-
with gr.Row():
|
| 238 |
-
template_display = gr.Markdown(value=generate_polynomial_template(2))
|
| 239 |
-
real_checkbox = gr.Checkbox(label="Show Only Real Roots", value=False)
|
| 240 |
-
|
| 241 |
-
# Row for selecting the polynomial degree and entering coefficients
|
| 242 |
-
with gr.Row():
|
| 243 |
-
degree_slider = gr.Slider(1, 8, value=2, step=1, label="Select Degree of Polynomial Equation")
|
| 244 |
-
coeff_input = gr.Textbox(label="Enter Coefficients (space-separated)", placeholder="e.g. 1 -3 2")
|
| 245 |
-
|
| 246 |
-
# Row for example and preview buttons
|
| 247 |
-
with gr.Row():
|
| 248 |
-
example_btn = gr.Button("🔁 Load Example")
|
| 249 |
-
preview_poly_button = gr.Button("🔍 Preview Equation")
|
| 250 |
-
|
| 251 |
-
# Row for displaying the confirmed equation
|
| 252 |
-
with gr.Row():
|
| 253 |
-
poly_equation_display = gr.Markdown()
|
| 254 |
-
|
| 255 |
-
# Row for confirm and cancel buttons
|
| 256 |
-
with gr.Row():
|
| 257 |
-
confirm_poly_btn = gr.Button("✅ Display Solution", visible=False)
|
| 258 |
-
cancel_poly_btn = gr.Button("✏️ Make Changes in Equation", visible=False)
|
| 259 |
|
| 260 |
# Markdown component to display step-by-step solution
|
| 261 |
steps_md = gr.Markdown()
|
|
|
|
| 88 |
|
| 89 |
# Row for example and preview buttons
|
| 90 |
with gr.Row():
|
| 91 |
+
example_btn = gr.Button("Load Example")
|
| 92 |
+
preview_poly_button = gr.Button("Preview Equation")
|
| 93 |
|
| 94 |
# Row for displaying the confirmed equation
|
| 95 |
with gr.Row():
|
|
|
|
| 97 |
|
| 98 |
# Row for confirm and cancel buttons
|
| 99 |
with gr.Row():
|
| 100 |
+
confirm_poly_btn = gr.Button("Display Solution", visible=False)
|
| 101 |
+
cancel_poly_btn = gr.Button("Make Changes in Equation", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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