spagestic commited on
Commit
fe0557b
·
1 Parent(s): 54f380d

feat: implement Gradio interfaces for algebra operations and create tabbed interface

Browse files
app.py CHANGED
@@ -4,10 +4,8 @@ from maths.arithmetic.arithmetic_interface import (
4
  array_calc_interface, array_calc_vis_interface,
5
  gcd_interface, lcm_interface, is_prime_interface # Assuming these were added earlier from a subtask
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
13
  )
@@ -51,8 +49,11 @@ arithmetic_tab_names = [
51
  number_theory_interfaces_list = [gcd_interface, lcm_interface, is_prime_interface]
52
  number_theory_tab_names = ["GCD", "LCM", "Prime Check"]
53
 
54
- algebra_interfaces_list = [evaluate_expression_interface, simplify_radical_interface, polynomial_interface]
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
@@ -104,7 +105,6 @@ operations_research_tab_names = ["Branch & Bound", "Dual Simplex", "Simplex Step
104
  # Create topic-based tabbed interfaces
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")
 
4
  array_calc_interface, array_calc_vis_interface,
5
  gcd_interface, lcm_interface, is_prime_interface # Assuming these were added earlier from a subtask
6
  )
7
+ from maths.algebra.algebra_interface import solve_linear_equation_interface
8
+ from maths.algebra.algebra_tabs import algebra_tab
 
 
9
  from maths.geometry.trigonometry_interface import (
10
  trig_interface, inverse_trig_interface, solve_trig_equations_interface, trig_identities_interface # Assuming these
11
  )
 
49
  number_theory_interfaces_list = [gcd_interface, lcm_interface, is_prime_interface]
50
  number_theory_tab_names = ["GCD", "LCM", "Prime Check"]
51
 
52
+ algebra_tab = gr.TabbedInterface(
53
+ algebra_interfaces_list,
54
+ algebra_tab_names,
55
+ title="Algebra"
56
+ )
57
 
58
  equations_interfaces_list = [
59
  solve_linear_equation_interface, solve_quadratic_interface, # keep direct quadratic solver for single tab
 
105
  # Create topic-based tabbed interfaces
106
  arithmetic_tab = gr.TabbedInterface(arithmetic_interfaces_list, arithmetic_tab_names, title="Arithmetic")
107
  number_theory_tab = gr.TabbedInterface(number_theory_interfaces_list, number_theory_tab_names, title="Number Theory")
 
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")
maths/algebra/algebra_interface.py CHANGED
@@ -1,65 +1,8 @@
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
-
7
- # Middle School Math Tab
8
- solve_linear_equation_interface = gr.Interface(
9
- fn=solve_linear_equation,
10
- inputs=[
11
- gr.Number(label="Coefficient (a)"),
12
- gr.Number(label="Constant (b)")
13
- ],
14
- outputs="text",
15
- title="Linear Equation Solver",
16
- description="Solve the equation ax = b for x"
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)"),
24
- gr.Number(label="c (constant)"),
25
- gr.Number(label="x (value)")
26
- ],
27
- outputs="number",
28
- title="Quadratic Expression Evaluator",
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,
35
- inputs=gr.Number(label="Number under radical", precision=0),
36
- outputs="text",
37
- title="Radical Simplifier",
38
- description="Simplify a square root (e.g., √12 → 2√3)"
39
- )
40
-
41
- def parse_coeffs(coeff_str: str) -> list[float]:
42
- """Helper to parse comma-separated coefficients from string input."""
43
- if not coeff_str.strip():
44
- return [0.0]
45
- try:
46
- return [float(x.strip()) for x in coeff_str.split(',') if x.strip() != '']
47
- except ValueError:
48
- # Return a value that indicates error, Gradio will show the function's error message
49
- raise gr.Error("Invalid coefficient input. Ensure all coefficients are numbers.")
50
-
51
- polynomial_interface = gr.Interface(
52
- fn=lambda p1_str, p2_str, op: polynomial_operations(parse_coeffs(p1_str), parse_coeffs(p2_str), op),
53
- inputs=[
54
- gr.Textbox(label="Polynomial 1 Coefficients (comma-separated, highest power first, e.g., 1,-2,3 for x²-2x+3)"),
55
- gr.Textbox(label="Polynomial 2 Coefficients (comma-separated, e.g., 2,5 for 2x+5)"),
56
- gr.Radio(choices=["add", "subtract", "multiply"], label="Operation")
57
- ],
58
- outputs="text",
59
- title="Polynomial Operations",
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(
 
1
  import gradio as gr
2
+ from maths.algebra.solve_linear_equation import solve_linear_equation_interface
3
+ from maths.algebra.evaluate_quadratic_expression import evaluate_expression_interface
4
+ from maths.algebra.simplify_radical import simplify_radical_interface
5
+ from maths.algebra.polynomial_operations import polynomial_interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Create the main Gradio application that combines all interfaces
8
  algebra_app = gr.TabbedInterface(
maths/algebra/algebra_tabs.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from maths.algebra.algebra_interface import (
4
+ evaluate_expression_interface,
5
+ simplify_radical_interface,
6
+ polynomial_interface
7
+ )
8
+
9
+ # Tabbed interface for algebra operations
10
+ algebra_interfaces_list = [
11
+ evaluate_expression_interface,
12
+ simplify_radical_interface,
13
+ polynomial_interface
14
+ ]
15
+ algebra_tab_names = [
16
+ "Evaluate Expressions",
17
+ "Radical Simplifier",
18
+ "Polynomial Ops"
19
+ ]
20
+
21
+ algebra_tab = gr.TabbedInterface(
22
+ algebra_interfaces_list,
23
+ algebra_tab_names,
24
+ title="Algebra"
25
+ )
maths/algebra/evaluate_quadratic_expression.py CHANGED
@@ -1,5 +1,23 @@
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
  Evaluate the expression ax² + bx + c for a given value of x.
3
  """
4
+ import gradio as gr
5
+
6
  def evaluate_quadratic_expression(a, b, c, x):
7
  return a * (x ** 2) + b * x + c
8
+
9
+ evaluate_expression_interface = gr.Interface(
10
+ fn=evaluate_quadratic_expression,
11
+ inputs=[
12
+ gr.Number(label="a (coefficient of x²)"),
13
+ gr.Number(label="b (coefficient of x)"),
14
+ gr.Number(label="c (constant)"),
15
+ gr.Number(label="x (value)")
16
+ ],
17
+ outputs="number",
18
+ title="Quadratic Expression Evaluator",
19
+ description="Evaluate ax² + bx + c for a given value of x"
20
+ )
21
+
22
+ if __name__ == "__main__":
23
+ evaluate_expression_interface.launch()
maths/algebra/polynomial_operations.py CHANGED
@@ -3,6 +3,8 @@ Performs addition, subtraction, or multiplication of two polynomials.
3
  Polynomials are represented by lists of coefficients in descending order of power.
4
  Example: [1, -2, 3] represents x^2 - 2x + 3.
5
  """
 
 
6
  def polynomial_operations(poly1_coeffs: list[float], poly2_coeffs: list[float], operation: str) -> str:
7
  if not all(isinstance(c, (int, float)) for c in poly1_coeffs) or \
8
  not all(isinstance(c, (int, float)) for c in poly2_coeffs):
@@ -62,3 +64,28 @@ def polynomial_operations(poly1_coeffs: list[float], poly2_coeffs: list[float],
62
  else:
63
  result_str += f" + {term}"
64
  return result_str
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  Polynomials are represented by lists of coefficients in descending order of power.
4
  Example: [1, -2, 3] represents x^2 - 2x + 3.
5
  """
6
+ import gradio as gr
7
+
8
  def polynomial_operations(poly1_coeffs: list[float], poly2_coeffs: list[float], operation: str) -> str:
9
  if not all(isinstance(c, (int, float)) for c in poly1_coeffs) or \
10
  not all(isinstance(c, (int, float)) for c in poly2_coeffs):
 
64
  else:
65
  result_str += f" + {term}"
66
  return result_str
67
+
68
+ def parse_coeffs(coeff_str: str) -> list[float]:
69
+ """Helper to parse comma-separated coefficients from string input."""
70
+ if not coeff_str.strip():
71
+ return [0.0]
72
+ try:
73
+ return [float(x.strip()) for x in coeff_str.split(',') if x.strip() != '']
74
+ except ValueError:
75
+ # Return a value that indicates error, Gradio will show the function's error message
76
+ raise gr.Error("Invalid coefficient input. Ensure all coefficients are numbers.")
77
+
78
+ polynomial_interface = gr.Interface(
79
+ fn=lambda p1_str, p2_str, op: polynomial_operations(parse_coeffs(p1_str), parse_coeffs(p2_str), op),
80
+ inputs=[
81
+ gr.Textbox(label="Polynomial 1 Coefficients (comma-separated, highest power first, e.g., 1,-2,3 for x²-2x+3)"),
82
+ gr.Textbox(label="Polynomial 2 Coefficients (comma-separated, e.g., 2,5 for 2x+5)"),
83
+ gr.Radio(choices=["add", "subtract", "multiply"], label="Operation")
84
+ ],
85
+ outputs="text",
86
+ title="Polynomial Operations",
87
+ description="Add, subtract, or multiply two polynomials. Enter coefficients in descending order of power."
88
+ )
89
+
90
+ if __name__ == "__main__":
91
+ polynomial_interface.launch()
maths/algebra/simplify_radical.py CHANGED
@@ -1,6 +1,8 @@
1
  """
2
  Simplifies a radical (square root) to its simplest form (e.g., sqrt(12) -> 2*sqrt(3)).
3
  """
 
 
4
  def simplify_radical(number: int) -> str:
5
  if not isinstance(number, int):
6
  return "Input must be an integer."
@@ -22,3 +24,14 @@ def simplify_radical(number: int) -> str:
22
  if remaining == 1:
23
  return str(factor)
24
  return f"{factor}*sqrt({remaining})"
 
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
  Simplifies a radical (square root) to its simplest form (e.g., sqrt(12) -> 2*sqrt(3)).
3
  """
4
+ import gradio as gr
5
+
6
  def simplify_radical(number: int) -> str:
7
  if not isinstance(number, int):
8
  return "Input must be an integer."
 
24
  if remaining == 1:
25
  return str(factor)
26
  return f"{factor}*sqrt({remaining})"
27
+
28
+ simplify_radical_interface = gr.Interface(
29
+ fn=simplify_radical,
30
+ inputs=gr.Number(label="Number under radical", precision=0),
31
+ outputs="text",
32
+ title="Radical Simplifier",
33
+ description="Simplify a square root (e.g., √12 → 2√3)"
34
+ )
35
+
36
+ if __name__ == "__main__":
37
+ simplify_radical_interface.launch()
maths/algebra/solve_linear_equation.py CHANGED
@@ -1,9 +1,25 @@
1
  """
2
  Solve the equation ax = b for x.
3
  """
 
 
4
  def solve_linear_equation(a, b):
5
  if a == 0:
6
  if b == 0:
7
  return "Infinite solutions"
8
  return "No solution"
9
  return b / a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
  Solve the equation ax = b for x.
3
  """
4
+ import gradio as gr
5
+
6
  def solve_linear_equation(a, b):
7
  if a == 0:
8
  if b == 0:
9
  return "Infinite solutions"
10
  return "No solution"
11
  return b / a
12
+
13
+ solve_linear_equation_interface = gr.Interface(
14
+ fn=solve_linear_equation,
15
+ inputs=[
16
+ gr.Number(label="Coefficient (a)"),
17
+ gr.Number(label="Constant (b)")
18
+ ],
19
+ outputs="text",
20
+ title="Linear Equation Solver",
21
+ description="Solve the equation ax = b for x"
22
+ )
23
+
24
+ if __name__ == "__main__":
25
+ solve_linear_equation_interface.launch()