File size: 4,328 Bytes
0d7ff87
4c5b4cf
0d7ff87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c5b4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d29336a
 
 
 
 
 
 
4c5b4cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import math
import gradio as gr

def solve_trig_equations(a: float, b: float, c: float, trig_func: str, interval_degrees: tuple[float, float] = (0, 360)) -> str:
    """
    Solves basic trigonometric equations of the form a * func(x) + b = c.
    Finds solutions for x within a given interval (in degrees).
    Args:
        a: Coefficient of the trigonometric function.
        b: Constant term added to the function part.
        c: Constant term on the other side of the equation.
        trig_func: The trigonometric function ("sin", "cos", "tan").
        interval_degrees: Tuple (min_angle, max_angle) for solutions in degrees.
    Returns:
        A string describing the solutions in degrees.
    """
    if a == 0:
        return "Error: Coefficient 'a' cannot be zero."
    val = (c - b) / a
    func = trig_func.lower()
    solutions_deg = []
    if func == "sin":
        if not (-1 <= val <= 1):
            return f"No solution: sin(x) cannot be {val:.4f}."
        angle_rad_principal = math.asin(val)
    elif func == "cos":
        if not (-1 <= val <= 1):
            return f"No solution: cos(x) cannot be {val:.4f}."
        angle_rad_principal = math.acos(val)
    elif func == "tan":
        angle_rad_principal = math.atan(val)
    else:
        return "Error: Invalid trigonometric function. Choose 'sin', 'cos', or 'tan'."
    angle_deg_principal = math.degrees(angle_rad_principal)
    min_interval_deg, max_interval_deg = interval_degrees
    for n in range(int(min_interval_deg / 360) - 2, int(max_interval_deg / 360) + 3):
        if func == "sin":
            sol1_deg = n * 360 + angle_deg_principal
            sol2_deg = n * 360 + (180 - angle_deg_principal)
            if min_interval_deg <= sol1_deg <= max_interval_deg:
                solutions_deg.append(sol1_deg)
            if min_interval_deg <= sol2_deg <= max_interval_deg:
                solutions_deg.append(sol2_deg)
        elif func == "cos":
            sol1_deg = n * 360 + angle_deg_principal
            sol2_deg = n * 360 - angle_deg_principal
            if min_interval_deg <= sol1_deg <= max_interval_deg:
                solutions_deg.append(sol1_deg)
            if min_interval_deg <= sol2_deg <= max_interval_deg:
                solutions_deg.append(sol2_deg)
        elif func == "tan":
            for n_tan in range(int(min_interval_deg / 180) - 2, int(max_interval_deg / 180) + 3):
                sol_deg = n_tan * 180 + angle_deg_principal
                if min_interval_deg <= sol_deg <= max_interval_deg:
                    solutions_deg.append(sol_deg)
    unique_solutions = sorted(list(set(f"{s:.2f}" for s in solutions_deg)))
    if not unique_solutions:
        return f"No solutions found for {a}*{func}(x) + {b} = {c} in the interval [{min_interval_deg}, {max_interval_deg}] degrees."
    return f"Solutions for x in [{min_interval_deg}, {max_interval_deg}] degrees: {', '.join(unique_solutions)}"

def parse_interval(interval_str: str) -> tuple[float, float]:
    try:
        parts = [float(x.strip()) for x in interval_str.split(',')]
        if len(parts) == 2 and parts[0] <= parts[1]:
            return parts[0], parts[1]
        raise ValueError("Interval must be two numbers, min,max.")
    except Exception:
        raise gr.Error("Invalid interval format. Use 'min,max' (e.g., '0,360').")

solve_trig_equations_interface = gr.Interface(
    fn=lambda a, b, c, func, interval_str: solve_trig_equations(a, b, c, func, parse_interval(interval_str)),
    inputs=[
        gr.Number(label="a (coefficient of function)"),
        gr.Number(label="b (constant added to function part)"),
        gr.Number(label="c (constant on other side of equation)"),
        gr.Radio(["sin", "cos", "tan"], label="Trigonometric Function"),
        gr.Textbox(label="Interval for x (degrees, comma-separated, e.g., 0,360)", value="0,360")
    ],
    outputs="text",
    title="Trigonometric Equation Solver",
    description="Solves equations like a * func(x) + b = c for x in a given interval (degrees). Enter the coefficients, select the function, and specify the interval for x. Example: Solve 2*sin(x) + 1 = 1 for x in [0,360] degrees.",
    examples=[
        [2, 1, 1, "sin", "0,360"],
        [1, 0, 0.5, "cos", "0,360"],
        [1, 0, 1, "tan", "0,360"],
        [1, 0, 2, "sin", "0,360"]
    ]
)