counting / maths /calculus /derivative_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 derivative_polynomial(coefficients):
"""
Calculate the derivative of a polynomial function.
Args:
coefficients (list): List of coefficients from highest to lowest degree
Returns:
list: Coefficients of the derivative polynomial
"""
if len(coefficients) <= 1:
return [0]
result = []
for i, coef in enumerate(coefficients[:-1]):
power = len(coefficients) - i - 1
result.append(coef * power)
return result
derivative_interface = gr.Interface(
fn=lambda coefficients: derivative_polynomial([float(c) for c in coefficients.split(',')]),
inputs=gr.Textbox(label="Polynomial Coefficients (comma-separated, highest degree first)"),
outputs="json",
title="Polynomial Derivative",
description="Find the derivative of a polynomial function.\n\n**Description:**\nThis tool computes the derivative of a polynomial given its coefficients (highest degree first).\n\n**Examples:**\n- Input: 3,2,1 (represents 3x^2 + 2x + 1) → Output: [6,2] (represents 6x + 2)\n- Input: 5,0,-4 (represents 5x^2 - 4) → Output: [10,0] (represents 10x)\n- Input: 7 (represents constant 7) → Output: [0]",
examples=[["3,2,1"],["5,0,-4"],["7"]],
)