Spaces:
Sleeping
Sleeping
Update polynomial.py
Browse files- polynomial.py +165 -26
polynomial.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
#polynomial.py
|
| 2 |
import gradio as gr
|
| 3 |
import sympy as sp
|
| 4 |
import numpy as np
|
|
@@ -47,13 +46,13 @@ def solve_polynomial(degree, coeff_string, real_only):
|
|
| 47 |
) + "\n$$"
|
| 48 |
|
| 49 |
steps_output = f"""
|
| 50 |
-
###
|
| 51 |
$$ {sp.latex(poly)} = 0 $$
|
| 52 |
-
###
|
| 53 |
$$ {sp.latex(simplified)} = 0 $$
|
| 54 |
-
###
|
| 55 |
$$ {sp.latex(factored)} = 0 $$
|
| 56 |
-
###
|
| 57 |
{roots_output}
|
| 58 |
"""
|
| 59 |
|
|
@@ -65,7 +64,7 @@ $$ {sp.latex(factored)} = 0 $$
|
|
| 65 |
ax.axhline(0, color='black', linewidth=0.5)
|
| 66 |
ax.axvline(0, color='black', linewidth=0.5)
|
| 67 |
ax.grid(True)
|
| 68 |
-
ax.set_title("
|
| 69 |
ax.set_xlabel("x")
|
| 70 |
ax.set_ylabel("f(x)")
|
| 71 |
ax.legend()
|
|
@@ -76,44 +75,29 @@ $$ {sp.latex(factored)} = 0 $$
|
|
| 76 |
|
| 77 |
# Function to create the Polynomial Solver tab with Gradio components
|
| 78 |
def polynomial_tab():
|
| 79 |
-
with gr.Tab("
|
| 80 |
# Row for displaying the equation template and real roots checkbox
|
| 81 |
with gr.Row():
|
| 82 |
-
# Markdown component for displaying the polynomial template with increased height (200px)
|
| 83 |
-
# The 'height' parameter ensures the template is more readable
|
| 84 |
template_display = gr.Markdown(value=generate_polynomial_template(2))
|
| 85 |
-
|
| 86 |
-
# Checkbox to toggle between showing all roots or only real roots
|
| 87 |
real_checkbox = gr.Checkbox(label="Show Only Real Roots", value=False)
|
| 88 |
|
| 89 |
# Row for selecting the polynomial degree and entering coefficients
|
| 90 |
with gr.Row():
|
| 91 |
-
# Slider to select the degree of the polynomial (1 to 8)
|
| 92 |
degree_slider = gr.Slider(1, 8, value=2, step=1, label="Select Degree of Polynomial Equation")
|
| 93 |
-
|
| 94 |
-
# Textbox for user to input coefficients (space-separated)
|
| 95 |
-
coeff_input = gr.Textbox(label="โ๏ธ Enter Coefficients (space-separated)", placeholder="e.g. 1 -3 2")
|
| 96 |
|
| 97 |
# Row for example and preview buttons
|
| 98 |
with gr.Row():
|
| 99 |
-
# Button to load an example set of coefficients
|
| 100 |
example_btn = gr.Button("๐ Load Example")
|
| 101 |
-
|
| 102 |
-
# Button to preview the entered polynomial equation
|
| 103 |
preview_poly_button = gr.Button("๐ Preview Equation")
|
| 104 |
|
| 105 |
-
# Row for displaying the confirmed equation
|
| 106 |
with gr.Row():
|
| 107 |
-
# Markdown component for displaying the confirmed equation with increased height (200px)
|
| 108 |
-
# The 'height' parameter makes the confirmed equation display larger
|
| 109 |
poly_equation_display = gr.Markdown()
|
| 110 |
|
| 111 |
# Row for confirm and cancel buttons
|
| 112 |
with gr.Row():
|
| 113 |
-
# Button to confirm and display the solution (initially hidden)
|
| 114 |
confirm_poly_btn = gr.Button("โ
Display Solution", visible=False)
|
| 115 |
-
|
| 116 |
-
# Button to cancel and edit the equation (initially hidden)
|
| 117 |
cancel_poly_btn = gr.Button("โ๏ธ Make Changes in Equation", visible=False)
|
| 118 |
|
| 119 |
# Markdown component to display step-by-step solution
|
|
@@ -171,6 +155,161 @@ def polynomial_tab():
|
|
| 171 |
# Initialize the template display with the default degree (2) on load
|
| 172 |
template_display.value = generate_polynomial_template(2)
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import sympy as sp
|
| 3 |
import numpy as np
|
|
|
|
| 46 |
) + "\n$$"
|
| 47 |
|
| 48 |
steps_output = f"""
|
| 49 |
+
### Polynomial Expression
|
| 50 |
$$ {sp.latex(poly)} = 0 $$
|
| 51 |
+
### Simplified
|
| 52 |
$$ {sp.latex(simplified)} = 0 $$
|
| 53 |
+
### Factored
|
| 54 |
$$ {sp.latex(factored)} = 0 $$
|
| 55 |
+
### Roots {'(Only Real)' if real_only else '(All Roots)'}
|
| 56 |
{roots_output}
|
| 57 |
"""
|
| 58 |
|
|
|
|
| 64 |
ax.axhline(0, color='black', linewidth=0.5)
|
| 65 |
ax.axvline(0, color='black', linewidth=0.5)
|
| 66 |
ax.grid(True)
|
| 67 |
+
ax.set_title("Graph of the Polynomial")
|
| 68 |
ax.set_xlabel("x")
|
| 69 |
ax.set_ylabel("f(x)")
|
| 70 |
ax.legend()
|
|
|
|
| 75 |
|
| 76 |
# Function to create the Polynomial Solver tab with Gradio components
|
| 77 |
def polynomial_tab():
|
| 78 |
+
with gr.Tab("Polynomial Solver"):
|
| 79 |
# Row for displaying the equation template and real roots checkbox
|
| 80 |
with gr.Row():
|
|
|
|
|
|
|
| 81 |
template_display = gr.Markdown(value=generate_polynomial_template(2))
|
|
|
|
|
|
|
| 82 |
real_checkbox = gr.Checkbox(label="Show Only Real Roots", value=False)
|
| 83 |
|
| 84 |
# Row for selecting the polynomial degree and entering coefficients
|
| 85 |
with gr.Row():
|
|
|
|
| 86 |
degree_slider = gr.Slider(1, 8, value=2, step=1, label="Select Degree of Polynomial Equation")
|
| 87 |
+
coeff_input = gr.Textbox(label="Enter Coefficients (space-separated)", placeholder="e.g. 1 -3 2")
|
|
|
|
|
|
|
| 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():
|
|
|
|
|
|
|
| 96 |
poly_equation_display = gr.Markdown()
|
| 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)
|
| 102 |
|
| 103 |
# Markdown component to display step-by-step solution
|
|
|
|
| 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()
|
| 262 |
+
|
| 263 |
+
# Plot component to display the polynomial graph
|
| 264 |
+
plot_output = gr.Plot()
|
| 265 |
+
|
| 266 |
+
# Textbox to display errors (initially hidden)
|
| 267 |
+
error_box = gr.Textbox(visible=False)
|
| 268 |
+
|
| 269 |
+
# Function to preview the polynomial equation based on user input
|
| 270 |
+
def preview_polynomial(degree, coeff_string, real_only):
|
| 271 |
+
try:
|
| 272 |
+
coeffs = list(map(float, coeff_string.strip().split()))
|
| 273 |
+
if len(coeffs) != degree + 1:
|
| 274 |
+
return f"โ ๏ธ Please enter exactly {degree + 1} coefficients.", gr.update(visible=False), gr.update(visible=False), "", None
|
| 275 |
+
poly = sum([coeffs[i] * x**(degree - i) for i in range(degree + 1)])
|
| 276 |
+
eq_latex = f"### โ
Confirm Polynomial\n\n$$ {sp.latex(poly)} = 0 $$"
|
| 277 |
+
return eq_latex, gr.update(visible=True), gr.update(visible=True), "", None
|
| 278 |
+
except Exception as e:
|
| 279 |
+
return f"โ Error parsing coefficients: {e}", gr.update(visible=False), gr.update(visible=False), "", None
|
| 280 |
+
|
| 281 |
+
# Event handler for preview button click
|
| 282 |
+
preview_poly_button.click(
|
| 283 |
+
fn=preview_polynomial,
|
| 284 |
+
inputs=[degree_slider, coeff_input, real_checkbox],
|
| 285 |
+
outputs=[poly_equation_display, confirm_poly_btn, cancel_poly_btn, steps_md, plot_output]
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
# Function to handle cancellation of the preview
|
| 289 |
+
def cancel_poly():
|
| 290 |
+
return gr.update(visible=False), gr.update(visible=False), "", "", None
|
| 291 |
+
|
| 292 |
+
# Event handler for cancel button click
|
| 293 |
+
cancel_poly_btn.click(
|
| 294 |
+
fn=cancel_poly,
|
| 295 |
+
inputs=[],
|
| 296 |
+
outputs=[confirm_poly_btn, cancel_poly_btn, poly_equation_display, steps_md, plot_output]
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
# Event handler for confirm button click to solve and display results
|
| 300 |
+
confirm_poly_btn.click(
|
| 301 |
+
fn=solve_polynomial,
|
| 302 |
+
inputs=[degree_slider, coeff_input, real_checkbox],
|
| 303 |
+
outputs=[steps_md, plot_output, error_box]
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
# Event handler to update the template when the degree slider changes
|
| 307 |
+
degree_slider.change(fn=generate_polynomial_template, inputs=degree_slider, outputs=template_display)
|
| 308 |
+
|
| 309 |
+
# Event handler to load an example when the example button is clicked
|
| 310 |
+
example_btn.click(fn=load_poly_example, inputs=degree_slider, outputs=coeff_input)
|
| 311 |
+
|
| 312 |
+
# Initialize the template display with the default degree (2) on load
|
| 313 |
+
template_display.value = generate_polynomial_template(2)
|
| 314 |
|
| 315 |
+
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_box
|