Spaces:
Runtime error
Runtime error
feat: implement Gradio interfaces for quadratic equation solving and visualization; remove old interfaces
Browse files
maths/equations/equations_interface.py
CHANGED
@@ -25,24 +25,6 @@ def quadratic_solver_wrapper(a, b, c, return_format):
|
|
25 |
else:
|
26 |
return result
|
27 |
|
28 |
-
solve_quadratic_interface = gr.Interface(
|
29 |
-
fn=quadratic_solver_wrapper,
|
30 |
-
inputs=[
|
31 |
-
gr.Number(label="a (coefficient of x²)"),
|
32 |
-
gr.Number(label="b (coefficient of x)"),
|
33 |
-
gr.Number(label="c (constant)"),
|
34 |
-
gr.Radio(
|
35 |
-
choices=["string", "dict", "surd"],
|
36 |
-
value="dict",
|
37 |
-
label="Output Format",
|
38 |
-
info="'string' for text output, 'dict' for formatted output, 'surd' for exact roots"
|
39 |
-
)
|
40 |
-
],
|
41 |
-
outputs="text",
|
42 |
-
title="Quadratic Equation Solver",
|
43 |
-
description="Solve ax² + bx + c = 0 and find vertex"
|
44 |
-
)
|
45 |
-
|
46 |
def plot_quadratic(a, b, c):
|
47 |
import numpy as np
|
48 |
import matplotlib.pyplot as plt
|
@@ -104,23 +86,3 @@ def plot_quadratic(a, b, c):
|
|
104 |
ax.set_title(f'Graph of f(x) = {c} (Constant)')
|
105 |
ax.legend()
|
106 |
return fig
|
107 |
-
|
108 |
-
quadratic_visualizer_interface = gr.Interface(
|
109 |
-
fn=plot_quadratic,
|
110 |
-
inputs=[
|
111 |
-
gr.Number(label="a (coefficient of x²)", value=1),
|
112 |
-
gr.Number(label="b (coefficient of x)", value=0),
|
113 |
-
gr.Number(label="c (constant)", value=0)
|
114 |
-
],
|
115 |
-
outputs=gr.Plot(),
|
116 |
-
title="Quadratic Function Visualizer",
|
117 |
-
description="Visualize the graph of a quadratic function f(x) = ax² + bx + c with its vertex and roots"
|
118 |
-
)
|
119 |
-
|
120 |
-
equations_app = gr.TabbedInterface(
|
121 |
-
[solve_quadratic_interface, quadratic_visualizer_interface],
|
122 |
-
["Quadratic Solver", "Quadratic Visualizer"]
|
123 |
-
)
|
124 |
-
|
125 |
-
if __name__ == "__main__":
|
126 |
-
equations_app.launch()
|
|
|
25 |
else:
|
26 |
return result
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
def plot_quadratic(a, b, c):
|
29 |
import numpy as np
|
30 |
import matplotlib.pyplot as plt
|
|
|
86 |
ax.set_title(f'Graph of f(x) = {c} (Constant)')
|
87 |
ax.legend()
|
88 |
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maths/equations/equations_tab.py
CHANGED
@@ -1,6 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
from maths.equations.
|
3 |
-
from maths.algebra.solve_linear_equation import solve_linear_equation_interface
|
4 |
-
from maths.geometry.trigonometry_interface import solve_trig_equations_interface
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from maths.equations.solve_quadratic import solve_quadratic_interface, quadratic_visualizer_interface
|
|
|
|
|
3 |
|
4 |
+
# You can add more equation-related interfaces here as needed
|
5 |
+
|
6 |
+
equations_tab = gr.TabbedInterface(
|
7 |
+
[solve_quadratic_interface, quadratic_visualizer_interface],
|
8 |
+
["Quadratic Solver", "Quadratic Visualizer"],
|
9 |
+
title="Equations"
|
10 |
+
)
|
maths/equations/solve_quadratic.py
CHANGED
@@ -5,6 +5,7 @@ import cmath
|
|
5 |
from fractions import Fraction
|
6 |
import math
|
7 |
import sympy as sp
|
|
|
8 |
|
9 |
def solve_quadratic(a: float, b: float, c: float, return_format: str = "string"):
|
10 |
if a == 0:
|
@@ -56,4 +57,118 @@ def solve_quadratic(a: float, b: float, c: float, return_format: str = "string")
|
|
56 |
else:
|
57 |
real_part = -b / (2*a)
|
58 |
imag_part = (-delta)**0.5 / (2*a)
|
59 |
-
return f"Two complex roots: x1 = {real_part} + {imag_part}i, x2 = {real_part} - {imag_part}i\nVertex at: {vertex}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from fractions import Fraction
|
6 |
import math
|
7 |
import sympy as sp
|
8 |
+
import gradio as gr
|
9 |
|
10 |
def solve_quadratic(a: float, b: float, c: float, return_format: str = "string"):
|
11 |
if a == 0:
|
|
|
57 |
else:
|
58 |
real_part = -b / (2*a)
|
59 |
imag_part = (-delta)**0.5 / (2*a)
|
60 |
+
return f"Two complex roots: x1 = {real_part} + {imag_part}i, x2 = {real_part} - {imag_part}i\nVertex at: {vertex}"
|
61 |
+
|
62 |
+
def quadratic_solver_wrapper(a, b, c, return_format):
|
63 |
+
result = solve_quadratic(a, b, c, return_format=return_format)
|
64 |
+
if return_format == "dict":
|
65 |
+
if "error" in result:
|
66 |
+
return result["error"]
|
67 |
+
roots = result["roots"]
|
68 |
+
vertex = result["vertex"]
|
69 |
+
output = ""
|
70 |
+
sign_b = "+" if b >= 0 else ""
|
71 |
+
sign_c = "+" if c >= 0 else ""
|
72 |
+
output += f"Equation: {a}x² {sign_b} {b}x {sign_c} {c} = 0\n\n"
|
73 |
+
if roots[1] is None:
|
74 |
+
output += f"Root: {roots[0]}\n"
|
75 |
+
else:
|
76 |
+
output += f"Root 1: {roots[0]}\n"
|
77 |
+
output += f"Root 2: {roots[1]}\n"
|
78 |
+
if vertex:
|
79 |
+
output += f"\nVertex: ({vertex[0]}, {vertex[1]})"
|
80 |
+
return output
|
81 |
+
else:
|
82 |
+
return result
|
83 |
+
|
84 |
+
solve_quadratic_interface = gr.Interface(
|
85 |
+
fn=quadratic_solver_wrapper,
|
86 |
+
inputs=[
|
87 |
+
gr.Number(label="a (coefficient of x²)"),
|
88 |
+
gr.Number(label="b (coefficient of x)"),
|
89 |
+
gr.Number(label="c (constant)"),
|
90 |
+
gr.Radio(
|
91 |
+
choices=["string", "dict", "surd"],
|
92 |
+
value="dict",
|
93 |
+
label="Output Format",
|
94 |
+
info="'string' for text output, 'dict' for formatted output, 'surd' for exact roots"
|
95 |
+
)
|
96 |
+
],
|
97 |
+
outputs="text",
|
98 |
+
title="Quadratic Equation Solver",
|
99 |
+
description="Solve ax² + bx + c = 0 and find vertex"
|
100 |
+
)
|
101 |
+
|
102 |
+
def plot_quadratic(a, b, c):
|
103 |
+
import numpy as np
|
104 |
+
import matplotlib.pyplot as plt
|
105 |
+
result = solve_quadratic(a, b, c, return_format="dict")
|
106 |
+
vertex_x = -b / (2*a) if a != 0 else 0
|
107 |
+
vertex_y = c - (b**2 / (4*a)) if a != 0 else 0
|
108 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
109 |
+
if a != 0:
|
110 |
+
if "roots" in result and result["roots"][0] is not None and result["roots"][1] is not None:
|
111 |
+
root1 = result["roots"][0].real if hasattr(result["roots"][0], "real") else float(result["roots"][0])
|
112 |
+
root2 = result["roots"][1].real if hasattr(result["roots"][1], "real") else float(result["roots"][1])
|
113 |
+
x_min = min(root1, root2, vertex_x) - 2
|
114 |
+
x_max = max(root1, root2, vertex_x) + 2
|
115 |
+
else:
|
116 |
+
x_min = vertex_x - 5
|
117 |
+
x_max = vertex_x + 5
|
118 |
+
x = np.linspace(x_min, x_max, 1000)
|
119 |
+
y = a * (x**2) + b * x + c
|
120 |
+
ax.plot(x, y, 'b-', label=f'f(x) = {a}x² + {b}x + {c}')
|
121 |
+
ax.plot(vertex_x, vertex_y, 'ro', label=f'Vertex: ({vertex_x:.2f}, {vertex_y:.2f})')
|
122 |
+
if "roots" in result:
|
123 |
+
roots = result["roots"]
|
124 |
+
if roots[0] is not None and (isinstance(roots[0], (int, float)) or (hasattr(roots[0], "imag") and roots[0].imag == 0)):
|
125 |
+
root1 = float(roots[0].real if hasattr(roots[0], "real") else roots[0])
|
126 |
+
ax.plot(root1, 0, 'go', label=f'Root 1: {root1:.2f}')
|
127 |
+
if roots[1] is not None and (isinstance(roots[1], (int, float)) or (hasattr(roots[1], "imag") and roots[1].imag == 0)):
|
128 |
+
root2 = float(roots[1].real if hasattr(roots[1], "real") else roots[1])
|
129 |
+
ax.plot(root2, 0, 'go', label=f'Root 2: {root2:.2f}')
|
130 |
+
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
131 |
+
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
132 |
+
ax.grid(True, alpha=0.3)
|
133 |
+
ax.set_xlabel('x')
|
134 |
+
ax.set_ylabel('f(x)')
|
135 |
+
ax.set_title(f'Graph of f(x) = {a}x² + {b}x + {c}')
|
136 |
+
ax.legend()
|
137 |
+
else:
|
138 |
+
if b != 0:
|
139 |
+
x = np.linspace(-5, 5, 100)
|
140 |
+
y = b * x + c
|
141 |
+
ax.plot(x, y, 'b-', label=f'f(x) = {b}x + {c} (Linear)')
|
142 |
+
root = -c/b
|
143 |
+
ax.plot(root, 0, 'go', label=f'Root: {root:.2f}')
|
144 |
+
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
145 |
+
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
146 |
+
ax.grid(True, alpha=0.3)
|
147 |
+
ax.set_xlabel('x')
|
148 |
+
ax.set_ylabel('f(x)')
|
149 |
+
ax.set_title(f'Graph of f(x) = {b}x + {c} (Linear)')
|
150 |
+
ax.legend()
|
151 |
+
else:
|
152 |
+
x = np.linspace(-5, 5, 100)
|
153 |
+
y = c * np.ones_like(x)
|
154 |
+
ax.plot(x, y, 'b-', label=f'f(x) = {c} (Constant)')
|
155 |
+
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
156 |
+
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
157 |
+
ax.grid(True, alpha=0.3)
|
158 |
+
ax.set_xlabel('x')
|
159 |
+
ax.set_ylabel('f(x)')
|
160 |
+
ax.set_title(f'Graph of f(x) = {c} (Constant)')
|
161 |
+
ax.legend()
|
162 |
+
return fig
|
163 |
+
|
164 |
+
quadratic_visualizer_interface = gr.Interface(
|
165 |
+
fn=plot_quadratic,
|
166 |
+
inputs=[
|
167 |
+
gr.Number(label="a (coefficient of x²)", value=1),
|
168 |
+
gr.Number(label="b (coefficient of x)", value=0),
|
169 |
+
gr.Number(label="c (constant)", value=0)
|
170 |
+
],
|
171 |
+
outputs=gr.Plot(),
|
172 |
+
title="Quadratic Function Visualizer",
|
173 |
+
description="Visualize the graph of a quadratic function f(x) = ax² + bx + c with its vertex and roots"
|
174 |
+
)
|
ode_solution_plot.png
ADDED
![]() |