Spaces:
Runtime error
Runtime error
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]], | |
) | |