spagestic commited on
Commit
b16cdfc
·
1 Parent(s): d5d735f

Add array calculation interfaces with visualization to arithmetic module

Browse files
app.py CHANGED
@@ -1,14 +1,17 @@
1
  import gradio as gr
2
  from utils.text_utils_interface import letter_counter_interface
3
- from maths.elementary.arithmetic_interface import add_interface, subtract_interface, multiply_interface, divide_interface
 
 
 
4
  from maths.middleschool.algebra_interface import solve_linear_equation_interface, evaluate_expression_interface
5
  from maths.highschool.trigonometry_interface import trig_interface
6
  from maths.university.calculus_interface import derivative_interface, integral_interface
7
 
8
  # Group interfaces by education level
9
  elementary_tab = gr.TabbedInterface(
10
- [add_interface, subtract_interface, multiply_interface, divide_interface],
11
- ["Addition", "Subtraction", "Multiplication", "Division"],
12
  title="Elementary School Math"
13
  )
14
 
 
1
  import gradio as gr
2
  from utils.text_utils_interface import letter_counter_interface
3
+ from maths.elementary.arithmetic_interface import (
4
+ add_interface, subtract_interface, multiply_interface, divide_interface,
5
+ array_calc_interface, array_calc_vis_interface
6
+ )
7
  from maths.middleschool.algebra_interface import solve_linear_equation_interface, evaluate_expression_interface
8
  from maths.highschool.trigonometry_interface import trig_interface
9
  from maths.university.calculus_interface import derivative_interface, integral_interface
10
 
11
  # Group interfaces by education level
12
  elementary_tab = gr.TabbedInterface(
13
+ [add_interface, subtract_interface, multiply_interface, divide_interface, array_calc_interface, array_calc_vis_interface],
14
+ ["Addition", "Subtraction", "Multiplication", "Division", "Array Calculation", "Array Calculation with Visualization"],
15
  title="Elementary School Math"
16
  )
17
 
maths/elementary/arithmetic.py CHANGED
@@ -129,3 +129,63 @@ def create_multiplication_table(number: int, max_multiplier: int = 12) -> plt.Fi
129
  ax.set_xticks(multipliers)
130
 
131
  return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  ax.set_xticks(multipliers)
130
 
131
  return fig
132
+
133
+ def calculate_array(numbers: list, operations: list) -> float:
134
+ """
135
+ Perform a sequence of arithmetic operations on an array of numbers.
136
+ Example: numbers=[2, 3, 4], operations=['add', 'multiply'] means (2 + 3) * 4
137
+ Supported operations: 'add', 'subtract', 'multiply', 'divide'
138
+ """
139
+ if not numbers or not operations:
140
+ raise ValueError("Both numbers and operations must be provided and non-empty.")
141
+ if len(operations) != len(numbers) - 1:
142
+ raise ValueError("Number of operations must be one less than number of numbers.")
143
+
144
+ result = float(numbers[0])
145
+ for i, op in enumerate(operations):
146
+ num = float(numbers[i+1])
147
+ if op == 'add':
148
+ result = add(result, num)
149
+ elif op == 'subtract':
150
+ result = subtract(result, num)
151
+ elif op == 'multiply':
152
+ result = multiply(result, num)
153
+ elif op == 'divide':
154
+ result = divide(result, num)
155
+ if isinstance(result, str): # Error case
156
+ return result
157
+ else:
158
+ raise ValueError(f"Unsupported operation: {op}")
159
+ return result
160
+
161
+
162
+ def calculate_array_with_visualization(numbers: list, operations: list) -> tuple:
163
+ """
164
+ Perform a sequence of arithmetic operations on an array of numbers and return result with visualization.
165
+ Visualization shows the step-by-step calculation on a number line.
166
+ """
167
+ if not numbers or not operations:
168
+ return "Error: Both numbers and operations must be provided and non-empty.", None
169
+ if len(operations) != len(numbers) - 1:
170
+ return "Error: Number of operations must be one less than number of numbers.", None
171
+
172
+ step_results = [float(numbers[0])]
173
+ result = float(numbers[0])
174
+ for i, op in enumerate(operations):
175
+ num = float(numbers[i+1])
176
+ if op == 'add':
177
+ result = add(result, num)
178
+ elif op == 'subtract':
179
+ result = subtract(result, num)
180
+ elif op == 'multiply':
181
+ result = multiply(result, num)
182
+ elif op == 'divide':
183
+ result = divide(result, num)
184
+ if isinstance(result, str): # Error case
185
+ return result, None
186
+ else:
187
+ return f"Error: Unsupported operation: {op}", None
188
+ step_results.append(result)
189
+ # Visualization: show all intermediate results on the number line
190
+ fig = create_number_line_visualization(numbers, ' -> '.join(operations), result)
191
+ return result, fig
maths/elementary/arithmetic_interface.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from maths.elementary.arithmetic import add, subtract, multiply, divide
3
 
4
  # Elementary Math Tab
5
  add_interface = gr.Interface(
@@ -33,3 +33,41 @@ divide_interface = gr.Interface(
33
  title="Division",
34
  description="Divide two numbers"
35
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from maths.elementary.arithmetic import add, subtract, multiply, divide, calculate_array, calculate_array_with_visualization
3
 
4
  # Elementary Math Tab
5
  add_interface = gr.Interface(
 
33
  title="Division",
34
  description="Divide two numbers"
35
  )
36
+
37
+ # Advanced Array Calculation Interface
38
+
39
+ def parse_number_list(s):
40
+ """Helper to parse comma-separated numbers from string input."""
41
+ try:
42
+ return [float(x.strip()) for x in s.split(',') if x.strip() != '']
43
+ except Exception:
44
+ return []
45
+
46
+ def parse_operation_list(s):
47
+ """Helper to parse comma-separated operations from string input."""
48
+ ops = [x.strip().lower() for x in s.split(',') if x.strip() != '']
49
+ valid_ops = {'add', 'subtract', 'multiply', 'divide'}
50
+ return [op for op in ops if op in valid_ops]
51
+
52
+ array_calc_interface = gr.Interface(
53
+ fn=lambda numbers, operations: calculate_array(numbers, operations),
54
+ inputs=[
55
+ gr.Textbox(label="Numbers (comma-separated, e.g. 2, 3, 4)"),
56
+ gr.Textbox(label="Operations (comma-separated, e.g. add, multiply)")
57
+ ],
58
+ outputs="text",
59
+ title="Array Calculation",
60
+ description="Calculate a sequence of numbers with specified operations. Example: Numbers: 2, 3, 4; Operations: add, multiply → (2 + 3) * 4"
61
+ )
62
+
63
+ array_calc_vis_interface = gr.Interface(
64
+ fn=lambda numbers, operations: calculate_array_with_visualization(
65
+ parse_number_list(numbers), parse_operation_list(operations)),
66
+ inputs=[
67
+ gr.Textbox(label="Numbers (comma-separated, e.g. 2, 3, 4)"),
68
+ gr.Textbox(label="Operations (comma-separated, e.g. add, multiply)")
69
+ ],
70
+ outputs=[gr.Textbox(label="Result"), gr.Plot(label="Visualization")],
71
+ title="Array Calculation with Visualization",
72
+ description="Calculate a sequence of numbers with specified operations and see a number line visualization."
73
+ )