counting / maths /calculus /integral_polynomial.py
spagestic's picture
feat: simplify descriptions and examples in Gradio interfaces for arithmetic and calculus tools
db31576
import sympy
import gradio as gr
def integral_polynomial(coefficients, c=0):
"""
Calculate the indefinite integral of a polynomial function.
Args:
coefficients (list): List of coefficients from highest to lowest degree
c (float): Integration constant
Returns:
list: Coefficients of the integral polynomial including constant term
"""
result = []
for i, coef in enumerate(coefficients):
power = len(coefficients) - i
result.append(coef / power)
result.append(c) # Add integration constant
return result
integral_interface = gr.Interface(
fn=lambda coefficients, c: integral_polynomial([float(x) for x in coefficients.split(',')], float(c)),
inputs=[
gr.Textbox(label="Polynomial Coefficients (comma-separated, highest degree first)"),
gr.Number(label="Integration Constant (c)", value=0)
],
outputs="json",
title="Polynomial Integration",
description="Find the indefinite integral of a polynomial function.\n\n**Description:**\nThis tool computes the indefinite integral of a polynomial given its coefficients (highest degree first) and an optional constant.\n\n**Examples:**\n- Input: 3,2,1 (represents 3x^2 + 2x + 1), c=0 → Output: [1.0, 1.0, 1.0, 0.0] (represents x^3 + x^2 + x)\n- Input: 4,0,-2, c=5 → Output: [1.333..., 0.0, -2.0, 5.0] (represents (4/3)x^3 - 2x + 5)\n- Input: 7, c=2 → Output: [7.0, 2.0] (represents 7x + 2)",
examples=[["3,2,1",0],["4,0,-2",5],["7",2]],
)