File size: 1,905 Bytes
5543e04
95151f6
5543e04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95151f6
 
 
 
 
 
 
 
 
 
 
db31576
 
95151f6
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
import sympy
import gradio as gr

def taylor_series_expansion(expression_str: str, variable_str: str = 'x', point: float = 0, order: int = 5) -> str:
    """
    Computes the Taylor series expansion of an expression around a point.

    Args:
        expression_str: The mathematical expression (e.g., "exp(x)").
        variable_str: The variable (default 'x').
        point: The point around which to expand (default 0).
        order: The order of the Taylor polynomial (default 5).

    Returns:
        String representation of the Taylor series or an error message.
    """
    try:
        var = sympy.Symbol(variable_str)
        expr = sympy.parse_expr(expression_str, local_dict={variable_str: var}, transformations='all')
        series = expr.series(var, x0=point, n=order).removeO()
        return str(series)
    except Exception as e:
        return f"Error calculating Taylor series: {e}"

taylor_series_interface = gr.Interface(
    fn=taylor_series_expansion,
    inputs=[
        gr.Textbox(label="Expression (e.g., exp(x), cos(x))", value="exp(x)"),
        gr.Textbox(label="Variable (e.g., x)", value="x"),
        gr.Number(label="Point of Expansion (x0)", value=0),
        gr.Number(label="Order of Taylor Polynomial", value=5, precision=0)
    ],
    outputs="text",
    title="Taylor Series Expansion",
    description="Compute the Taylor series of a function around a point.\n\n**Description:**\nThis tool computes the Taylor series expansion of a function around a specified point and order.\n\n**Examples:**\n- Expression: exp(x), Variable: x, Point: 0, Order: 5 → Output: 1 + x + x**2/2 + x**3/6 + x**4/24 + x**5/120\n- Expression: cos(x), Variable: x, Point: 0, Order: 4 → Output: 1 - x**2/2 + x**4/24\n- Expression: sin(x), Variable: x, Point: 0, Order: 3 → Output: x - x**3/6",
    examples=[["exp(x)", "x", 0, 5], ["cos(x)", "x", 0, 4], ["sin(x)", "x", 0, 3]],
)