import sympy import gradio as gr def fourier_series_example(function_type: str = "sawtooth", n_terms: int = 5) -> str: """ Provides an example of a Fourier series for a predefined function. Args: function_type: "sawtooth" or "square" wave. n_terms: Number of terms to compute in the series. Returns: String representation of the Fourier series. """ try: L = sympy.pi x = sympy.Symbol('x') if function_type == "sawtooth": series = sympy.S(0) for i in range(1, n_terms + 1): bn_coeff = -2 * ((-1)**i) / i series += bn_coeff * sympy.sin(i * x) return f"Fourier series for sawtooth wave (f(x)=x on [-pi,pi]): {str(series)}" elif function_type == "square": series = sympy.S(0) for i in range(1, n_terms + 1): if i % 2 != 0: bn_coeff = 4 / (i * sympy.pi) series += bn_coeff * sympy.sin(i * x) return f"Fourier series for square wave (f(x)=1 on [0,pi], -1 on [-pi,0]): {str(series)}" else: return "Error: Unknown function type for Fourier series. Choose 'sawtooth' or 'square'." except Exception as e: return f"Error generating Fourier series: {e}" fourier_series_interface = gr.Interface( fn=fourier_series_example, inputs=[ gr.Radio(choices=["sawtooth", "square"], label="Function Type", value="sawtooth"), gr.Number(label="Number of Terms (n)", value=5, precision=0) ], outputs="text", title="Fourier Series Examples", description="Generate Fourier series for predefined periodic functions.\n\n**Description:**\nThis tool generates the Fourier series for a sawtooth or square wave with a specified number of terms.\n\n**Examples:**\n- Function Type: sawtooth, n=3 → Output: Fourier series for sawtooth wave (f(x)=x on [-pi,pi]): ...\n- Function Type: square, n=5 → Output: Fourier series for square wave (f(x)=1 on [0,pi], -1 on [-pi,0]): ...", examples=[["sawtooth", 3], ["square", 5]], )