spagestic commited on
Commit
0d7ff87
·
1 Parent(s): 279610a

modularized

Browse files
maths/geometry/cos_degrees.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import math
2
+
3
+ def cos_degrees(angle):
4
+ """Calculate the cosine of an angle in degrees."""
5
+ return math.cos(math.radians(angle))
maths/geometry/inverse_trig_functions.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ def inverse_trig_functions(value: float, function_name: str) -> str:
4
+ """
5
+ Calculates inverse trigonometric functions (asin, acos, atan) in degrees.
6
+ Args:
7
+ value: The value to calculate the inverse trigonometric function for.
8
+ For asin and acos, must be between -1 and 1.
9
+ function_name: "asin", "acos", or "atan".
10
+ Returns:
11
+ A string representing the result in degrees, or an error message.
12
+ """
13
+ if not isinstance(value, (int, float)):
14
+ return "Error: Input value must be a number."
15
+ func_name = function_name.lower()
16
+ result_rad = 0.0
17
+ if func_name == "asin":
18
+ if -1 <= value <= 1:
19
+ result_rad = math.asin(value)
20
+ else:
21
+ return "Error: Input for asin must be between -1 and 1."
22
+ elif func_name == "acos":
23
+ if -1 <= value <= 1:
24
+ result_rad = math.acos(value)
25
+ else:
26
+ return "Error: Input for acos must be between -1 and 1."
27
+ elif func_name == "atan":
28
+ result_rad = math.atan(value)
29
+ else:
30
+ return "Error: Invalid function name. Choose 'asin', 'acos', or 'atan'."
31
+ return f"{math.degrees(result_rad):.4f} degrees"
maths/geometry/sin_degrees.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import math
2
+
3
+ def sin_degrees(angle):
4
+ """Calculate the sine of an angle in degrees."""
5
+ return math.sin(math.radians(angle))
maths/geometry/{trigonometry.py → solve_trig_equations.py} RENAMED
@@ -1,80 +1,23 @@
1
- """
2
- Trigonometry operations for high school level.
3
- """
4
  import math
5
 
6
- def sin_degrees(angle):
7
- """Calculate the sine of an angle in degrees."""
8
- return math.sin(math.radians(angle))
9
-
10
- def cos_degrees(angle):
11
- """Calculate the cosine of an angle in degrees."""
12
- return math.cos(math.radians(angle))
13
-
14
- def tan_degrees(angle):
15
- """Calculate the tangent of an angle in degrees."""
16
- return math.tan(math.radians(angle))
17
-
18
-
19
- def inverse_trig_functions(value: float, function_name: str) -> str:
20
- """
21
- Calculates inverse trigonometric functions (asin, acos, atan) in degrees.
22
-
23
- Args:
24
- value: The value to calculate the inverse trigonometric function for.
25
- For asin and acos, must be between -1 and 1.
26
- function_name: "asin", "acos", or "atan".
27
-
28
- Returns:
29
- A string representing the result in degrees, or an error message.
30
- """
31
- if not isinstance(value, (int, float)):
32
- return "Error: Input value must be a number."
33
-
34
- func_name = function_name.lower()
35
- result_rad = 0.0
36
-
37
- if func_name == "asin":
38
- if -1 <= value <= 1:
39
- result_rad = math.asin(value)
40
- else:
41
- return "Error: Input for asin must be between -1 and 1."
42
- elif func_name == "acos":
43
- if -1 <= value <= 1:
44
- result_rad = math.acos(value)
45
- else:
46
- return "Error: Input for acos must be between -1 and 1."
47
- elif func_name == "atan":
48
- result_rad = math.atan(value)
49
- else:
50
- return "Error: Invalid function name. Choose 'asin', 'acos', or 'atan'."
51
-
52
- return f"{math.degrees(result_rad):.4f} degrees"
53
-
54
-
55
  def solve_trig_equations(a: float, b: float, c: float, trig_func: str, interval_degrees: tuple[float, float] = (0, 360)) -> str:
56
  """
57
  Solves basic trigonometric equations of the form a * func(x) + b = c.
58
  Finds solutions for x within a given interval (in degrees).
59
-
60
  Args:
61
  a: Coefficient of the trigonometric function.
62
  b: Constant term added to the function part.
63
  c: Constant term on the other side of the equation.
64
  trig_func: The trigonometric function ("sin", "cos", "tan").
65
  interval_degrees: Tuple (min_angle, max_angle) for solutions in degrees.
66
-
67
  Returns:
68
  A string describing the solutions in degrees.
69
  """
70
  if a == 0:
71
  return "Error: Coefficient 'a' cannot be zero."
72
-
73
- # Rearrange to func(x) = (c - b) / a
74
  val = (c - b) / a
75
  func = trig_func.lower()
76
  solutions_deg = []
77
-
78
  if func == "sin":
79
  if not (-1 <= val <= 1):
80
  return f"No solution: sin(x) cannot be {val:.4f}."
@@ -87,19 +30,9 @@ def solve_trig_equations(a: float, b: float, c: float, trig_func: str, interval_
87
  angle_rad_principal = math.atan(val)
88
  else:
89
  return "Error: Invalid trigonometric function. Choose 'sin', 'cos', or 'tan'."
90
-
91
- # Convert principal solution to degrees
92
  angle_deg_principal = math.degrees(angle_rad_principal)
93
-
94
  min_interval_deg, max_interval_deg = interval_degrees
95
-
96
- # Find solutions within the interval
97
- # General solutions:
98
- # sin(x) = sin(alpha) => x = n*360 + alpha OR x = n*360 + (180 - alpha)
99
- # cos(x) = cos(alpha) => x = n*360 + alpha OR x = n*360 - alpha
100
- # tan(x) = tan(alpha) => x = n*180 + alpha
101
-
102
- for n in range(int(min_interval_deg / 360) - 2, int(max_interval_deg / 360) + 3): # Check a few cycles around the interval
103
  if func == "sin":
104
  sol1_deg = n * 360 + angle_deg_principal
105
  sol2_deg = n * 360 + (180 - angle_deg_principal)
@@ -115,82 +48,11 @@ def solve_trig_equations(a: float, b: float, c: float, trig_func: str, interval_
115
  if min_interval_deg <= sol2_deg <= max_interval_deg:
116
  solutions_deg.append(sol2_deg)
117
  elif func == "tan":
118
- # For tan, general solution is n*180 + alpha
119
- for n_tan in range(int(min_interval_deg / 180) - 2, int(max_interval_deg / 180) + 3):
120
  sol_deg = n_tan * 180 + angle_deg_principal
121
  if min_interval_deg <= sol_deg <= max_interval_deg:
122
  solutions_deg.append(sol_deg)
123
-
124
- # Remove duplicates and sort
125
- unique_solutions = sorted(list(set(f"{s:.2f}" for s in solutions_deg))) # Format to avoid floating point issues
126
-
127
  if not unique_solutions:
128
  return f"No solutions found for {a}*{func}(x) + {b} = {c} in the interval [{min_interval_deg}, {max_interval_deg}] degrees."
129
-
130
  return f"Solutions for x in [{min_interval_deg}, {max_interval_deg}] degrees: {', '.join(unique_solutions)}"
131
-
132
-
133
- def trig_identities(angle_degrees: float, identity_name: str = "pythagorean1") -> str:
134
- """
135
- Demonstrates common trigonometric identities for a given angle (in degrees).
136
-
137
- Args:
138
- angle_degrees: The angle in degrees to evaluate the identities for.
139
- identity_name: Name of the identity to demonstrate.
140
- "pythagorean1": sin^2(x) + cos^2(x) = 1
141
- "pythagorean2": 1 + tan^2(x) = sec^2(x)
142
- "pythagorean3": 1 + cot^2(x) = csc^2(x)
143
- "all": Show all Pythagorean identities.
144
- More can be added.
145
-
146
- Returns:
147
- A string demonstrating the identity.
148
- """
149
- x_rad = math.radians(angle_degrees)
150
- sinx = math.sin(x_rad)
151
- cosx = math.cos(x_rad)
152
-
153
- # Avoid division by zero for tan, sec, cot, csc
154
- # Check if cosx is very close to zero
155
- if abs(cosx) < 1e-9: # cos(90), cos(270) etc.
156
- tanx = float('inf') if sinx > 0 else float('-inf') if sinx < 0 else 0 # tan is undefined or 0 if sinx is also 0
157
- secx = float('inf') if cosx >= 0 else float('-inf') # sec is undefined
158
- else:
159
- tanx = sinx / cosx
160
- secx = 1 / cosx
161
-
162
- # Check if sinx is very close to zero
163
- if abs(sinx) < 1e-9: # sin(0), sin(180) etc.
164
- cotx = float('inf') if cosx > 0 else float('-inf') if cosx < 0 else 0 # cot is undefined or 0 if cosx is also 0
165
- cscx = float('inf') if sinx >= 0 else float('-inf') # csc is undefined
166
- else:
167
- cotx = cosx / sinx
168
- cscx = 1 / sinx
169
-
170
- results = []
171
- name = identity_name.lower()
172
-
173
- if name == "pythagorean1" or name == "all":
174
- lhs = sinx**2 + cosx**2
175
- results.append(f"Pythagorean Identity 1: sin^2({angle_degrees}) + cos^2({angle_degrees}) = {sinx**2:.4f} + {cosx**2:.4f} = {lhs:.4f} (Expected: 1)")
176
-
177
- if name == "pythagorean2" or name == "all":
178
- if abs(cosx) < 1e-9:
179
- results.append(f"Pythagorean Identity 2 (1 + tan^2(x) = sec^2(x)): Not well-defined for x={angle_degrees} degrees as cos(x) is near zero (tan(x) and sec(x) are undefined or infinite).")
180
- else:
181
- lhs = 1 + tanx**2
182
- rhs = secx**2
183
- results.append(f"Pythagorean Identity 2: 1 + tan^2({angle_degrees}) = 1 + {tanx**2:.4f} = {lhs:.4f}. sec^2({angle_degrees}) = {secx**2:.4f}. (Expected LHS = RHS)")
184
-
185
- if name == "pythagorean3" or name == "all":
186
- if abs(sinx) < 1e-9:
187
- results.append(f"Pythagorean Identity 3 (1 + cot^2(x) = csc^2(x)): Not well-defined for x={angle_degrees} degrees as sin(x) is near zero (cot(x) and csc(x) are undefined or infinite).")
188
- else:
189
- lhs = 1 + cotx**2
190
- rhs = cscx**2
191
- results.append(f"Pythagorean Identity 3: 1 + cot^2({angle_degrees}) = 1 + {cotx**2:.4f} = {lhs:.4f}. csc^2({angle_degrees}) = {cscx**2:.4f}. (Expected LHS = RHS)")
192
-
193
- if not results:
194
- return f"Unknown identity: {identity_name}. Available: pythagorean1, pythagorean2, pythagorean3, all."
195
-
196
- return "\n".join(results)
 
 
 
 
1
  import math
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  def solve_trig_equations(a: float, b: float, c: float, trig_func: str, interval_degrees: tuple[float, float] = (0, 360)) -> str:
4
  """
5
  Solves basic trigonometric equations of the form a * func(x) + b = c.
6
  Finds solutions for x within a given interval (in degrees).
 
7
  Args:
8
  a: Coefficient of the trigonometric function.
9
  b: Constant term added to the function part.
10
  c: Constant term on the other side of the equation.
11
  trig_func: The trigonometric function ("sin", "cos", "tan").
12
  interval_degrees: Tuple (min_angle, max_angle) for solutions in degrees.
 
13
  Returns:
14
  A string describing the solutions in degrees.
15
  """
16
  if a == 0:
17
  return "Error: Coefficient 'a' cannot be zero."
 
 
18
  val = (c - b) / a
19
  func = trig_func.lower()
20
  solutions_deg = []
 
21
  if func == "sin":
22
  if not (-1 <= val <= 1):
23
  return f"No solution: sin(x) cannot be {val:.4f}."
 
30
  angle_rad_principal = math.atan(val)
31
  else:
32
  return "Error: Invalid trigonometric function. Choose 'sin', 'cos', or 'tan'."
 
 
33
  angle_deg_principal = math.degrees(angle_rad_principal)
 
34
  min_interval_deg, max_interval_deg = interval_degrees
35
+ for n in range(int(min_interval_deg / 360) - 2, int(max_interval_deg / 360) + 3):
 
 
 
 
 
 
 
36
  if func == "sin":
37
  sol1_deg = n * 360 + angle_deg_principal
38
  sol2_deg = n * 360 + (180 - angle_deg_principal)
 
48
  if min_interval_deg <= sol2_deg <= max_interval_deg:
49
  solutions_deg.append(sol2_deg)
50
  elif func == "tan":
51
+ for n_tan in range(int(min_interval_deg / 180) - 2, int(max_interval_deg / 180) + 3):
 
52
  sol_deg = n_tan * 180 + angle_deg_principal
53
  if min_interval_deg <= sol_deg <= max_interval_deg:
54
  solutions_deg.append(sol_deg)
55
+ unique_solutions = sorted(list(set(f"{s:.2f}" for s in solutions_deg)))
 
 
 
56
  if not unique_solutions:
57
  return f"No solutions found for {a}*{func}(x) + {b} = {c} in the interval [{min_interval_deg}, {max_interval_deg}] degrees."
 
58
  return f"Solutions for x in [{min_interval_deg}, {max_interval_deg}] degrees: {', '.join(unique_solutions)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
maths/geometry/tan_degrees.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ import math
2
+
3
+ def tan_degrees(angle):
4
+ """Calculate the tangent of an angle in degrees."""
5
+ return math.tan(math.radians(angle))
maths/geometry/trig_identities.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ def trig_identities(angle_degrees: float, identity_name: str = "pythagorean1") -> str:
4
+ """
5
+ Demonstrates common trigonometric identities for a given angle (in degrees).
6
+ Args:
7
+ angle_degrees: The angle in degrees to evaluate the identities for.
8
+ identity_name: Name of the identity to demonstrate.
9
+ "pythagorean1": sin^2(x) + cos^2(x) = 1
10
+ "pythagorean2": 1 + tan^2(x) = sec^2(x)
11
+ "pythagorean3": 1 + cot^2(x) = csc^2(x)
12
+ "all": Show all Pythagorean identities.
13
+ More can be added.
14
+ Returns:
15
+ A string demonstrating the identity.
16
+ """
17
+ x_rad = math.radians(angle_degrees)
18
+ sinx = math.sin(x_rad)
19
+ cosx = math.cos(x_rad)
20
+ if abs(cosx) < 1e-9:
21
+ tanx = float('inf') if sinx > 0 else float('-inf') if sinx < 0 else 0
22
+ secx = float('inf') if cosx >= 0 else float('-inf')
23
+ else:
24
+ tanx = sinx / cosx
25
+ secx = 1 / cosx
26
+ if abs(sinx) < 1e-9:
27
+ cotx = float('inf') if cosx > 0 else float('-inf') if cosx < 0 else 0
28
+ cscx = float('inf') if sinx >= 0 else float('-inf')
29
+ else:
30
+ cotx = cosx / sinx
31
+ cscx = 1 / sinx
32
+ results = []
33
+ name = identity_name.lower()
34
+ if name == "pythagorean1" or name == "all":
35
+ lhs = sinx**2 + cosx**2
36
+ results.append(f"Pythagorean Identity 1: sin^2({angle_degrees}) + cos^2({angle_degrees}) = {sinx**2:.4f} + {cosx**2:.4f} = {lhs:.4f} (Expected: 1)")
37
+ if name == "pythagorean2" or name == "all":
38
+ if abs(cosx) < 1e-9:
39
+ results.append(f"Pythagorean Identity 2 (1 + tan^2(x) = sec^2(x)): Not well-defined for x={angle_degrees} degrees as cos(x) is near zero (tan(x) and sec(x) are undefined or infinite).")
40
+ else:
41
+ lhs = 1 + tanx**2
42
+ rhs = secx**2
43
+ results.append(f"Pythagorean Identity 2: 1 + tan^2({angle_degrees}) = 1 + {tanx**2:.4f} = {lhs:.4f}. sec^2({angle_degrees}) = {secx**2:.4f}. (Expected LHS = RHS)")
44
+ if name == "pythagorean3" or name == "all":
45
+ if abs(sinx) < 1e-9:
46
+ results.append(f"Pythagorean Identity 3 (1 + cot^2(x) = csc^2(x)): Not well-defined for x={angle_degrees} degrees as sin(x) is near zero (cot(x) and csc(x) are undefined or infinite).")
47
+ else:
48
+ lhs = 1 + cotx**2
49
+ rhs = cscx**2
50
+ results.append(f"Pythagorean Identity 3: 1 + cot^2({angle_degrees}) = 1 + {cotx**2:.4f} = {lhs:.4f}. csc^2({angle_degrees}) = {cscx**2:.4f}. (Expected LHS = RHS)")
51
+ if not results:
52
+ return f"Unknown identity: {identity_name}. Available: pythagorean1, pythagorean2, pythagorean3, all."
53
+ return "\n".join(results)
maths/geometry/trigonometry_interface.py CHANGED
@@ -1,5 +1,10 @@
1
  import gradio as gr
2
- from maths.geometry.trigonometry import sin_degrees, cos_degrees, tan_degrees, inverse_trig_functions, solve_trig_equations, trig_identities
 
 
 
 
 
3
 
4
  # High School Math Tab
5
  trig_interface = gr.Interface(
 
1
  import gradio as gr
2
+ from maths.geometry.sin_degrees import sin_degrees
3
+ from maths.geometry.cos_degrees import cos_degrees
4
+ from maths.geometry.tan_degrees import tan_degrees
5
+ from maths.geometry.inverse_trig_functions import inverse_trig_functions
6
+ from maths.geometry.solve_trig_equations import solve_trig_equations
7
+ from maths.geometry.trig_identities import trig_identities
8
 
9
  # High School Math Tab
10
  trig_interface = gr.Interface(