Spaces:
Runtime error
Runtime error
refactor: reorganize algebra interfaces and remove unused quadratic solver
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ from maths.arithmetic.arithmetic_interface import (
|
|
6 |
)
|
7 |
from maths.algebra.algebra_interface import (
|
8 |
solve_linear_equation_interface, evaluate_expression_interface,
|
9 |
-
|
10 |
)
|
11 |
from maths.geometry.trigonometry_interface import (
|
12 |
trig_interface, inverse_trig_interface, solve_trig_equations_interface, trig_identities_interface # Assuming these
|
@@ -33,6 +33,9 @@ from maths.differential_equations.differential_equations_interface import (
|
|
33 |
from maths.operations_research.operations_research_interface import (
|
34 |
branch_and_bound_interface, dual_simplex_interface, simplex_solver_interface
|
35 |
)
|
|
|
|
|
|
|
36 |
|
37 |
# Categorize interfaces by topics
|
38 |
|
@@ -52,7 +55,7 @@ algebra_interfaces_list = [evaluate_expression_interface, simplify_radical_inter
|
|
52 |
algebra_tab_names = ["Evaluate Expressions", "Radical Simplifier", "Polynomial Ops"]
|
53 |
|
54 |
equations_interfaces_list = [
|
55 |
-
solve_linear_equation_interface, solve_quadratic_interface,
|
56 |
solve_trig_equations_interface
|
57 |
]
|
58 |
equations_tab_names = [
|
@@ -102,7 +105,7 @@ operations_research_tab_names = ["Branch & Bound", "Dual Simplex", "Simplex Step
|
|
102 |
arithmetic_tab = gr.TabbedInterface(arithmetic_interfaces_list, arithmetic_tab_names, title="Arithmetic")
|
103 |
number_theory_tab = gr.TabbedInterface(number_theory_interfaces_list, number_theory_tab_names, title="Number Theory")
|
104 |
algebra_tab = gr.TabbedInterface(algebra_interfaces_list, algebra_tab_names, title="Algebra")
|
105 |
-
equations_tab =
|
106 |
geometry_tab = gr.TabbedInterface(geometry_interfaces_list, geometry_tab_names, title="Geometry")
|
107 |
calculus_tab = gr.TabbedInterface(calculus_interfaces_list, calculus_tab_names, title="Calculus")
|
108 |
differential_equations_tab = gr.TabbedInterface(differential_equations_interfaces_list, differential_equations_tab_names, title="Differential Equations")
|
|
|
6 |
)
|
7 |
from maths.algebra.algebra_interface import (
|
8 |
solve_linear_equation_interface, evaluate_expression_interface,
|
9 |
+
simplify_radical_interface, polynomial_interface # Assuming these
|
10 |
)
|
11 |
from maths.geometry.trigonometry_interface import (
|
12 |
trig_interface, inverse_trig_interface, solve_trig_equations_interface, trig_identities_interface # Assuming these
|
|
|
33 |
from maths.operations_research.operations_research_interface import (
|
34 |
branch_and_bound_interface, dual_simplex_interface, simplex_solver_interface
|
35 |
)
|
36 |
+
from maths.equations.equations_interface import (
|
37 |
+
equations_app, solve_quadratic_interface # equations_app is the tabbed interface, solve_quadratic_interface for direct use
|
38 |
+
)
|
39 |
|
40 |
# Categorize interfaces by topics
|
41 |
|
|
|
55 |
algebra_tab_names = ["Evaluate Expressions", "Radical Simplifier", "Polynomial Ops"]
|
56 |
|
57 |
equations_interfaces_list = [
|
58 |
+
solve_linear_equation_interface, solve_quadratic_interface, # keep direct quadratic solver for single tab
|
59 |
solve_trig_equations_interface
|
60 |
]
|
61 |
equations_tab_names = [
|
|
|
105 |
arithmetic_tab = gr.TabbedInterface(arithmetic_interfaces_list, arithmetic_tab_names, title="Arithmetic")
|
106 |
number_theory_tab = gr.TabbedInterface(number_theory_interfaces_list, number_theory_tab_names, title="Number Theory")
|
107 |
algebra_tab = gr.TabbedInterface(algebra_interfaces_list, algebra_tab_names, title="Algebra")
|
108 |
+
equations_tab = equations_app # Only include the composite equations_app in the equations tab to avoid duplicate Gradio blocks
|
109 |
geometry_tab = gr.TabbedInterface(geometry_interfaces_list, geometry_tab_names, title="Geometry")
|
110 |
calculus_tab = gr.TabbedInterface(calculus_interfaces_list, calculus_tab_names, title="Calculus")
|
111 |
differential_equations_tab = gr.TabbedInterface(differential_equations_interfaces_list, differential_equations_tab_names, title="Differential Equations")
|
maths/algebra/algebra.py
DELETED
File without changes
|
maths/algebra/algebra_interface.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from maths.algebra.solve_linear_equation import solve_linear_equation
|
3 |
-
from maths.algebra.
|
4 |
-
from maths.algebra.solve_quadratic import solve_quadratic
|
5 |
from maths.algebra.simplify_radical import simplify_radical
|
6 |
from maths.algebra.polynomial_operations import polynomial_operations
|
7 |
|
@@ -18,7 +17,7 @@ solve_linear_equation_interface = gr.Interface(
|
|
18 |
)
|
19 |
|
20 |
evaluate_expression_interface = gr.Interface(
|
21 |
-
fn=
|
22 |
inputs=[
|
23 |
gr.Number(label="a (coefficient of x²)"),
|
24 |
gr.Number(label="b (coefficient of x)"),
|
@@ -30,64 +29,6 @@ evaluate_expression_interface = gr.Interface(
|
|
30 |
description="Evaluate ax² + bx + c for a given value of x"
|
31 |
)
|
32 |
|
33 |
-
def quadratic_solver_wrapper(a, b, c, return_format):
|
34 |
-
"""Wrapper function for the quadratic solver to format the output nicely for Gradio."""
|
35 |
-
result = solve_quadratic(a, b, c, return_format=return_format)
|
36 |
-
|
37 |
-
if return_format == "dict":
|
38 |
-
# Format the dictionary output for display
|
39 |
-
if "error" in result:
|
40 |
-
return result["error"]
|
41 |
-
|
42 |
-
roots = result["roots"]
|
43 |
-
vertex = result["vertex"]
|
44 |
-
|
45 |
-
# Create a nicely formatted output
|
46 |
-
output = ""
|
47 |
-
|
48 |
-
# Format the equation
|
49 |
-
sign_b = "+" if b >= 0 else ""
|
50 |
-
sign_c = "+" if c >= 0 else ""
|
51 |
-
output += f"Equation: {a}x² {sign_b} {b}x {sign_c} {c} = 0\n\n"
|
52 |
-
|
53 |
-
# Format roots
|
54 |
-
if roots[1] is None: # Linear equation case
|
55 |
-
output += f"Root: {roots[0]}\n"
|
56 |
-
else:
|
57 |
-
# Check if roots are complex
|
58 |
-
if isinstance(roots[0], complex) or isinstance(roots[1], complex):
|
59 |
-
output += f"Root 1: {roots[0]}\n"
|
60 |
-
output += f"Root 2: {roots[1]}\n"
|
61 |
-
else:
|
62 |
-
output += f"Root 1: {roots[0]}\n"
|
63 |
-
output += f"Root 2: {roots[1]}\n"
|
64 |
-
|
65 |
-
# Format vertex
|
66 |
-
if vertex:
|
67 |
-
output += f"\nVertex: ({vertex[0]}, {vertex[1]})"
|
68 |
-
|
69 |
-
return output
|
70 |
-
else:
|
71 |
-
# String format is already formatted well
|
72 |
-
return result
|
73 |
-
|
74 |
-
solve_quadratic_interface = gr.Interface(
|
75 |
-
fn=quadratic_solver_wrapper,
|
76 |
-
inputs=[
|
77 |
-
gr.Number(label="a (coefficient of x²)"),
|
78 |
-
gr.Number(label="b (coefficient of x)"),
|
79 |
-
gr.Number(label="c (constant)"),
|
80 |
-
gr.Radio(
|
81 |
-
choices=["string", "dict"],
|
82 |
-
value="dict",
|
83 |
-
label="Output Format",
|
84 |
-
info="'string' for text output, 'dict' for formatted output with roots and vertex"
|
85 |
-
)
|
86 |
-
],
|
87 |
-
outputs="text",
|
88 |
-
title="Quadratic Equation Solver",
|
89 |
-
description="Solve ax² + bx + c = 0 and find vertex"
|
90 |
-
)
|
91 |
|
92 |
simplify_radical_interface = gr.Interface(
|
93 |
fn=simplify_radical,
|
@@ -119,127 +60,12 @@ polynomial_interface = gr.Interface(
|
|
119 |
description="Add, subtract, or multiply two polynomials. Enter coefficients in descending order of power."
|
120 |
)
|
121 |
|
122 |
-
# Add an interactive visualizer for quadratic functions
|
123 |
-
def plot_quadratic(a, b, c):
|
124 |
-
"""Plot the quadratic function f(x) = ax^2 + bx + c with its vertex and roots."""
|
125 |
-
import numpy as np
|
126 |
-
import matplotlib.pyplot as plt
|
127 |
-
|
128 |
-
# Calculate vertex
|
129 |
-
vertex_x = -b / (2*a) if a != 0 else 0
|
130 |
-
vertex_y = c - (b**2 / (4*a)) if a != 0 else 0
|
131 |
-
|
132 |
-
# Calculate roots (if they exist)
|
133 |
-
result = solve_quadratic(a, b, c, return_format="dict")
|
134 |
-
|
135 |
-
# Create the plot
|
136 |
-
fig, ax = plt.subplots(figsize=(8, 6))
|
137 |
-
|
138 |
-
# Calculate appropriate x range based on vertex and roots
|
139 |
-
if a != 0:
|
140 |
-
# Find appropriate range based on vertex and/or roots
|
141 |
-
if "roots" in result and result["roots"][0] is not None and result["roots"][1] is not None:
|
142 |
-
# Extract real parts of roots
|
143 |
-
root1 = result["roots"][0].real if hasattr(result["roots"][0], "real") else float(result["roots"][0])
|
144 |
-
root2 = result["roots"][1].real if hasattr(result["roots"][1], "real") else float(result["roots"][1])
|
145 |
-
x_min = min(root1, root2, vertex_x) - 2
|
146 |
-
x_max = max(root1, root2, vertex_x) + 2
|
147 |
-
else:
|
148 |
-
# No roots or only one root - use vertex
|
149 |
-
x_min = vertex_x - 5
|
150 |
-
x_max = vertex_x + 5
|
151 |
-
|
152 |
-
x = np.linspace(x_min, x_max, 1000)
|
153 |
-
y = a * (x**2) + b * x + c
|
154 |
-
|
155 |
-
# Plot the function
|
156 |
-
ax.plot(x, y, 'b-', label=f'f(x) = {a}x² + {b}x + {c}')
|
157 |
-
|
158 |
-
# Plot the vertex
|
159 |
-
ax.plot(vertex_x, vertex_y, 'ro', label=f'Vertex: ({vertex_x:.2f}, {vertex_y:.2f})')
|
160 |
-
|
161 |
-
# Plot the roots if they exist and are real
|
162 |
-
if "roots" in result:
|
163 |
-
roots = result["roots"]
|
164 |
-
if roots[0] is not None and (isinstance(roots[0], (int, float)) or
|
165 |
-
(hasattr(roots[0], "imag") and roots[0].imag == 0)):
|
166 |
-
root1 = float(roots[0].real if hasattr(roots[0], "real") else roots[0])
|
167 |
-
ax.plot(root1, 0, 'go', label=f'Root 1: {root1:.2f}')
|
168 |
-
|
169 |
-
if roots[1] is not None and (isinstance(roots[1], (int, float)) or
|
170 |
-
(hasattr(roots[1], "imag") and roots[1].imag == 0)):
|
171 |
-
root2 = float(roots[1].real if hasattr(roots[1], "real") else roots[1])
|
172 |
-
ax.plot(root2, 0, 'go', label=f'Root 2: {root2:.2f}')
|
173 |
-
|
174 |
-
# Plot x and y axes
|
175 |
-
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
176 |
-
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
177 |
-
|
178 |
-
# Add grid
|
179 |
-
ax.grid(True, alpha=0.3)
|
180 |
-
|
181 |
-
# Set labels and title
|
182 |
-
ax.set_xlabel('x')
|
183 |
-
ax.set_ylabel('f(x)')
|
184 |
-
ax.set_title(f'Graph of f(x) = {a}x² + {b}x + {c}')
|
185 |
-
|
186 |
-
# Add legend
|
187 |
-
ax.legend()
|
188 |
-
else:
|
189 |
-
# Handle linear case or invalid case
|
190 |
-
if b != 0: # Linear function
|
191 |
-
x = np.linspace(-5, 5, 100)
|
192 |
-
y = b * x + c
|
193 |
-
ax.plot(x, y, 'b-', label=f'f(x) = {b}x + {c} (Linear)')
|
194 |
-
|
195 |
-
# Plot the root if it exists
|
196 |
-
root = -c/b
|
197 |
-
ax.plot(root, 0, 'go', label=f'Root: {root:.2f}')
|
198 |
-
|
199 |
-
# Plot axes
|
200 |
-
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
201 |
-
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
202 |
-
|
203 |
-
ax.grid(True, alpha=0.3)
|
204 |
-
ax.set_xlabel('x')
|
205 |
-
ax.set_ylabel('f(x)')
|
206 |
-
ax.set_title(f'Graph of f(x) = {b}x + {c} (Linear)')
|
207 |
-
ax.legend()
|
208 |
-
else: # Constant function
|
209 |
-
x = np.linspace(-5, 5, 100)
|
210 |
-
y = c * np.ones_like(x)
|
211 |
-
ax.plot(x, y, 'b-', label=f'f(x) = {c} (Constant)')
|
212 |
-
|
213 |
-
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
214 |
-
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
215 |
-
|
216 |
-
ax.grid(True, alpha=0.3)
|
217 |
-
ax.set_xlabel('x')
|
218 |
-
ax.set_ylabel('f(x)')
|
219 |
-
ax.set_title(f'Graph of f(x) = {c} (Constant)')
|
220 |
-
ax.legend()
|
221 |
-
|
222 |
-
return fig
|
223 |
-
|
224 |
-
quadratic_visualizer_interface = gr.Interface(
|
225 |
-
fn=plot_quadratic,
|
226 |
-
inputs=[
|
227 |
-
gr.Number(label="a (coefficient of x²)", value=1),
|
228 |
-
gr.Number(label="b (coefficient of x)", value=0),
|
229 |
-
gr.Number(label="c (constant)", value=0)
|
230 |
-
],
|
231 |
-
outputs=gr.Plot(),
|
232 |
-
title="Quadratic Function Visualizer",
|
233 |
-
description="Visualize the graph of a quadratic function f(x) = ax² + bx + c with its vertex and roots"
|
234 |
-
)
|
235 |
|
236 |
# Create the main Gradio application that combines all interfaces
|
237 |
algebra_app = gr.TabbedInterface(
|
238 |
[solve_linear_equation_interface, evaluate_expression_interface,
|
239 |
-
solve_quadratic_interface, quadratic_visualizer_interface,
|
240 |
simplify_radical_interface, polynomial_interface],
|
241 |
-
["Linear Solver", "Expression Evaluator",
|
242 |
-
"Quadratic Visualizer", "Radical Simplifier", "Polynomials"]
|
243 |
)
|
244 |
|
245 |
# Allow the Gradio interface to be run directly or imported
|
|
|
1 |
import gradio as gr
|
2 |
from maths.algebra.solve_linear_equation import solve_linear_equation
|
3 |
+
from maths.algebra.evaluate_quadratic_expression import evaluate_quadratic_expression
|
|
|
4 |
from maths.algebra.simplify_radical import simplify_radical
|
5 |
from maths.algebra.polynomial_operations import polynomial_operations
|
6 |
|
|
|
17 |
)
|
18 |
|
19 |
evaluate_expression_interface = gr.Interface(
|
20 |
+
fn=evaluate_quadratic_expression,
|
21 |
inputs=[
|
22 |
gr.Number(label="a (coefficient of x²)"),
|
23 |
gr.Number(label="b (coefficient of x)"),
|
|
|
29 |
description="Evaluate ax² + bx + c for a given value of x"
|
30 |
)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
simplify_radical_interface = gr.Interface(
|
34 |
fn=simplify_radical,
|
|
|
60 |
description="Add, subtract, or multiply two polynomials. Enter coefficients in descending order of power."
|
61 |
)
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
# Create the main Gradio application that combines all interfaces
|
65 |
algebra_app = gr.TabbedInterface(
|
66 |
[solve_linear_equation_interface, evaluate_expression_interface,
|
|
|
67 |
simplify_radical_interface, polynomial_interface],
|
68 |
+
["Linear Solver", "Expression Evaluator", "Radical Simplifier", "Polynomials"]
|
|
|
69 |
)
|
70 |
|
71 |
# Allow the Gradio interface to be run directly or imported
|
maths/algebra/{evaluate_expression.py → evaluate_quadratic_expression.py}
RENAMED
@@ -1,5 +1,5 @@
|
|
1 |
"""
|
2 |
Evaluate the expression ax² + bx + c for a given value of x.
|
3 |
"""
|
4 |
-
def
|
5 |
return a * (x ** 2) + b * x + c
|
|
|
1 |
"""
|
2 |
Evaluate the expression ax² + bx + c for a given value of x.
|
3 |
"""
|
4 |
+
def evaluate_quadratic_expression(a, b, c, x):
|
5 |
return a * (x ** 2) + b * x + c
|
maths/equations/equations_interface.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# filepath: c:\Users\visha\Desktop\coding\counting\maths\equations\equations_interface.py
|
2 |
+
import gradio as gr
|
3 |
+
from maths.equations.solve_quadratic import solve_quadratic
|
4 |
+
|
5 |
+
# Quadratic Equation Solver Interface
|
6 |
+
def quadratic_solver_wrapper(a, b, c, return_format):
|
7 |
+
result = solve_quadratic(a, b, c, return_format=return_format)
|
8 |
+
if return_format == "dict":
|
9 |
+
if "error" in result:
|
10 |
+
return result["error"]
|
11 |
+
roots = result["roots"]
|
12 |
+
vertex = result["vertex"]
|
13 |
+
output = ""
|
14 |
+
sign_b = "+" if b >= 0 else ""
|
15 |
+
sign_c = "+" if c >= 0 else ""
|
16 |
+
output += f"Equation: {a}x² {sign_b} {b}x {sign_c} {c} = 0\n\n"
|
17 |
+
if roots[1] is None:
|
18 |
+
output += f"Root: {roots[0]}\n"
|
19 |
+
else:
|
20 |
+
output += f"Root 1: {roots[0]}\n"
|
21 |
+
output += f"Root 2: {roots[1]}\n"
|
22 |
+
if vertex:
|
23 |
+
output += f"\nVertex: ({vertex[0]}, {vertex[1]})"
|
24 |
+
return output
|
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
|
49 |
+
result = solve_quadratic(a, b, c, return_format="dict")
|
50 |
+
vertex_x = -b / (2*a) if a != 0 else 0
|
51 |
+
vertex_y = c - (b**2 / (4*a)) if a != 0 else 0
|
52 |
+
fig, ax = plt.subplots(figsize=(8, 6))
|
53 |
+
if a != 0:
|
54 |
+
if "roots" in result and result["roots"][0] is not None and result["roots"][1] is not None:
|
55 |
+
root1 = result["roots"][0].real if hasattr(result["roots"][0], "real") else float(result["roots"][0])
|
56 |
+
root2 = result["roots"][1].real if hasattr(result["roots"][1], "real") else float(result["roots"][1])
|
57 |
+
x_min = min(root1, root2, vertex_x) - 2
|
58 |
+
x_max = max(root1, root2, vertex_x) + 2
|
59 |
+
else:
|
60 |
+
x_min = vertex_x - 5
|
61 |
+
x_max = vertex_x + 5
|
62 |
+
x = np.linspace(x_min, x_max, 1000)
|
63 |
+
y = a * (x**2) + b * x + c
|
64 |
+
ax.plot(x, y, 'b-', label=f'f(x) = {a}x² + {b}x + {c}')
|
65 |
+
ax.plot(vertex_x, vertex_y, 'ro', label=f'Vertex: ({vertex_x:.2f}, {vertex_y:.2f})')
|
66 |
+
if "roots" in result:
|
67 |
+
roots = result["roots"]
|
68 |
+
if roots[0] is not None and (isinstance(roots[0], (int, float)) or (hasattr(roots[0], "imag") and roots[0].imag == 0)):
|
69 |
+
root1 = float(roots[0].real if hasattr(roots[0], "real") else roots[0])
|
70 |
+
ax.plot(root1, 0, 'go', label=f'Root 1: {root1:.2f}')
|
71 |
+
if roots[1] is not None and (isinstance(roots[1], (int, float)) or (hasattr(roots[1], "imag") and roots[1].imag == 0)):
|
72 |
+
root2 = float(roots[1].real if hasattr(roots[1], "real") else roots[1])
|
73 |
+
ax.plot(root2, 0, 'go', label=f'Root 2: {root2:.2f}')
|
74 |
+
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
75 |
+
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
76 |
+
ax.grid(True, alpha=0.3)
|
77 |
+
ax.set_xlabel('x')
|
78 |
+
ax.set_ylabel('f(x)')
|
79 |
+
ax.set_title(f'Graph of f(x) = {a}x² + {b}x + {c}')
|
80 |
+
ax.legend()
|
81 |
+
else:
|
82 |
+
if b != 0:
|
83 |
+
x = np.linspace(-5, 5, 100)
|
84 |
+
y = b * x + c
|
85 |
+
ax.plot(x, y, 'b-', label=f'f(x) = {b}x + {c} (Linear)')
|
86 |
+
root = -c/b
|
87 |
+
ax.plot(root, 0, 'go', label=f'Root: {root:.2f}')
|
88 |
+
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
89 |
+
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
90 |
+
ax.grid(True, alpha=0.3)
|
91 |
+
ax.set_xlabel('x')
|
92 |
+
ax.set_ylabel('f(x)')
|
93 |
+
ax.set_title(f'Graph of f(x) = {b}x + {c} (Linear)')
|
94 |
+
ax.legend()
|
95 |
+
else:
|
96 |
+
x = np.linspace(-5, 5, 100)
|
97 |
+
y = c * np.ones_like(x)
|
98 |
+
ax.plot(x, y, 'b-', label=f'f(x) = {c} (Constant)')
|
99 |
+
ax.axhline(y=0, color='k', linestyle='-', alpha=0.3)
|
100 |
+
ax.axvline(x=0, color='k', linestyle='-', alpha=0.3)
|
101 |
+
ax.grid(True, alpha=0.3)
|
102 |
+
ax.set_xlabel('x')
|
103 |
+
ax.set_ylabel('f(x)')
|
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()
|
maths/{algebra → equations}/solve_quadratic.py
RENAMED
@@ -3,6 +3,8 @@ Solve the quadratic equation ax^2 + bx + c = 0.
|
|
3 |
"""
|
4 |
import cmath
|
5 |
from fractions import Fraction
|
|
|
|
|
6 |
|
7 |
def solve_quadratic(a: float, b: float, c: float, return_format: str = "string"):
|
8 |
if a == 0:
|
@@ -18,6 +20,15 @@ def solve_quadratic(a: float, b: float, c: float, return_format: str = "string")
|
|
18 |
vertex_y = c - (b**2 / (4 * a))
|
19 |
vertex = (vertex_x, vertex_y)
|
20 |
delta = b**2 - 4*a*c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
if return_format == "dict":
|
22 |
discriminant = cmath.sqrt(b**2 - 4*a*c)
|
23 |
root1 = (-b + discriminant) / (2 * a)
|
|
|
3 |
"""
|
4 |
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:
|
|
|
20 |
vertex_y = c - (b**2 / (4 * a))
|
21 |
vertex = (vertex_x, vertex_y)
|
22 |
delta = b**2 - 4*a*c
|
23 |
+
# Surd form using sympy
|
24 |
+
if return_format == "surd":
|
25 |
+
x1 = (-b + sp.sqrt(delta)) / (2*a)
|
26 |
+
x2 = (-b - sp.sqrt(delta)) / (2*a)
|
27 |
+
return {
|
28 |
+
"roots": (sp.simplify(x1), sp.simplify(x2)),
|
29 |
+
"vertex": vertex,
|
30 |
+
"discriminant": delta
|
31 |
+
}
|
32 |
if return_format == "dict":
|
33 |
discriminant = cmath.sqrt(b**2 - 4*a*c)
|
34 |
root1 = (-b + discriminant) / (2 * a)
|