spagestic commited on
Commit
30ca660
·
1 Parent(s): 4094371

feat: add Gradio interfaces for cubic and polynomial equation solving; update quadratic solver to use numpy

Browse files
maths/equations/equations_tab.py CHANGED
@@ -1,10 +1,11 @@
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
  )
 
1
  import gradio as gr
2
  from maths.equations.solve_quadratic import solve_quadratic_interface, quadratic_visualizer_interface
3
+ from maths.equations.solve_cubic import cubic_solver_interface
4
+ from maths.equations.solve_poly import poly_solver_interface
5
 
 
6
 
7
  equations_tab = gr.TabbedInterface(
8
+ [solve_quadratic_interface, quadratic_visualizer_interface, cubic_solver_interface, poly_solver_interface],
9
+ ["Quadratic Solver", "Quadratic Visualizer", "Cubic Solver", "Polynomial Solver"],
10
  title="Equations"
11
  )
maths/equations/solve_cubic.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+
4
+ def solve_cubic(a, b, c, d):
5
+ """
6
+ Solve cubic equation: a*x^3 + b*x^2 + c*x + d = 0
7
+ Returns a tuple of three roots (complex or real).
8
+ """
9
+ if a == 0:
10
+ # Degenerates to quadratic
11
+ from .solve_quadratic import solve_quadratic
12
+ result = solve_quadratic(b, c, d, return_format="dict")
13
+ roots = result["roots"]
14
+ return (roots[0], roots[1], None)
15
+ # Use numpy.roots for cubic
16
+ roots = np.roots([a, b, c, d])
17
+ # Ensure three roots (may be complex)
18
+ if len(roots) < 3:
19
+ roots = tuple(list(roots) + [None]*(3-len(roots)))
20
+ else:
21
+ roots = tuple(roots)
22
+ return roots
23
+
24
+ def cubic_solver_wrapper(a, b, c, d):
25
+ roots = solve_cubic(a, b, c, d)
26
+ output = f"Equation: {a}x³ + {b}x² + {c}x + {d} = 0\n\n"
27
+ for i, root in enumerate(roots):
28
+ if root is not None:
29
+ if np.isreal(root):
30
+ output += f"Root {i+1}: {root.real}\n"
31
+ else:
32
+ output += f"Root {i+1}: {root}\n"
33
+ return output
34
+
35
+ cubic_solver_interface = gr.Interface(
36
+ fn=cubic_solver_wrapper,
37
+ inputs=[
38
+ gr.Number(label="a (coefficient of x³)"),
39
+ gr.Number(label="b (coefficient of x²)"),
40
+ gr.Number(label="c (coefficient of x)"),
41
+ gr.Number(label="d (constant)")
42
+ ],
43
+ outputs="text",
44
+ title="Cubic Equation Solver",
45
+ description="Solve ax³ + bx² + cx + d = 0"
46
+ )
maths/equations/solve_poly.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ def solve_polynomial(coeffs):
4
+ """
5
+ Find roots of a polynomial with given coefficients.
6
+ coeffs: list or array-like, highest degree first (e.g., [a, b, c, d, ...])
7
+ Returns a numpy array of roots (complex or real).
8
+ """
9
+ if not coeffs or all(c == 0 for c in coeffs):
10
+ raise ValueError("Coefficient list must not be empty or all zeros.")
11
+ return np.roots(coeffs)
12
+
13
+ def polynomial_solver_interface(coeffs):
14
+ """
15
+ Gradio wrapper for solving higher order polynomials.
16
+ coeffs: comma-separated string of coefficients, highest degree first.
17
+ """
18
+ import numpy as np
19
+ try:
20
+ coeff_list = [float(c) for c in coeffs.split(",") if c.strip() != ""]
21
+ if not coeff_list:
22
+ return "Please enter at least one coefficient."
23
+ roots = solve_polynomial(coeff_list)
24
+ output = f"Polynomial coefficients: {coeff_list}\n\n"
25
+ for i, root in enumerate(roots):
26
+ if np.isreal(root):
27
+ output += f"Root {i+1}: {root.real}\n"
28
+ else:
29
+ output += f"Root {i+1}: {root}\n"
30
+ return output
31
+ except Exception as e:
32
+ return f"Error: {e}"
33
+
34
+ poly_solver_interface = gr.Interface(
35
+ fn=polynomial_solver_interface,
36
+ inputs=gr.Textbox(label="Coefficients (comma-separated, highest degree first)", placeholder="e.g. 1, 0, -2, -8"),
37
+ outputs="text",
38
+ title="Polynomial Equation Solver",
39
+ description="Find roots of any polynomial. Enter coefficients separated by commas (highest degree first)."
40
+ )
maths/equations/solve_quadratic.py CHANGED
@@ -3,9 +3,9 @@ Solve the quadratic equation ax^2 + bx + c = 0.
3
  """
4
  import cmath
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:
@@ -31,13 +31,21 @@ def solve_quadratic(a: float, b: float, c: float, return_format: str = "string")
31
  "discriminant": delta
32
  }
33
  if return_format == "dict":
34
- discriminant = cmath.sqrt(b**2 - 4*a*c)
35
- root1 = (-b + discriminant) / (2 * a)
36
- root2 = (-b - discriminant) / (2 * a)
37
- if root1.imag == 0 and root2.imag == 0:
38
- root1 = Fraction(root1.real).limit_denominator()
39
- root2 = Fraction(root2.real).limit_denominator()
40
- return {"roots": (root1, root2), "vertex": vertex}
 
 
 
 
 
 
 
 
41
  if delta > 0:
42
  x1 = (-b + delta**0.5) / (2*a)
43
  x2 = (-b - delta**0.5) / (2*a)
 
3
  """
4
  import cmath
5
  from fractions import Fraction
 
6
  import sympy as sp
7
  import gradio as gr
8
+ import numpy as np
9
 
10
  def solve_quadratic(a: float, b: float, c: float, return_format: str = "string"):
11
  if a == 0:
 
31
  "discriminant": delta
32
  }
33
  if return_format == "dict":
34
+ # Use numpy.roots for quadratic
35
+ roots = np.roots([a, b, c])
36
+ # Ensure two roots (may be complex)
37
+ if len(roots) == 1:
38
+ roots = (roots[0], None)
39
+ else:
40
+ roots = tuple(roots)
41
+ # Try to convert to Fraction if real
42
+ roots_fmt = []
43
+ for r in roots:
44
+ if r is not None and np.isreal(r):
45
+ roots_fmt.append(Fraction(r.real).limit_denominator())
46
+ else:
47
+ roots_fmt.append(r)
48
+ return {"roots": tuple(roots_fmt), "vertex": vertex}
49
  if delta > 0:
50
  x1 = (-b + delta**0.5) / (2*a)
51
  x2 = (-b - delta**0.5) / (2*a)