Spaces:
Runtime error
Runtime error
feat: add Gradio interfaces for calculus operations and refactor existing code
Browse files- maths/calculus/calculate_limit.py +15 -0
- maths/calculus/calculus_interface.py +0 -127
- maths/calculus/calculus_tab.py +7 -5
- maths/calculus/derivative_polynomial.py +9 -0
- maths/calculus/fourier_series_example.py +12 -0
- maths/calculus/integral_polynomial.py +12 -0
- maths/calculus/multiple_integral.py +44 -0
- maths/calculus/partial_derivative.py +12 -0
- maths/calculus/taylor_series_expansion.py +14 -0
maths/calculus/calculate_limit.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import sympy
|
|
|
|
| 2 |
|
| 3 |
def calculate_limit(expression_str: str, variable_str: str, point_str: str, direction: str = '+-') -> str:
|
| 4 |
"""
|
|
@@ -37,3 +38,17 @@ def calculate_limit(expression_str: str, variable_str: str, point_str: str, dire
|
|
| 37 |
return f"Error parsing expression or point: {e}. Ensure valid Sympy syntax (e.g. use 'oo' for infinity, ensure variables match)."
|
| 38 |
except Exception as e:
|
| 39 |
return f"An unexpected error occurred: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sympy
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
def calculate_limit(expression_str: str, variable_str: str, point_str: str, direction: str = '+-') -> str:
|
| 5 |
"""
|
|
|
|
| 38 |
return f"Error parsing expression or point: {e}. Ensure valid Sympy syntax (e.g. use 'oo' for infinity, ensure variables match)."
|
| 39 |
except Exception as e:
|
| 40 |
return f"An unexpected error occurred: {e}"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
limit_interface = gr.Interface(
|
| 44 |
+
fn=calculate_limit,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.Textbox(label="Expression (e.g., sin(x)/x, x**2, exp(-x))", value="sin(x)/x"),
|
| 47 |
+
gr.Textbox(label="Variable (e.g., x)", value="x"),
|
| 48 |
+
gr.Textbox(label="Point (e.g., 0, 1, oo, -oo, pi)", value="0"),
|
| 49 |
+
gr.Radio(choices=["+-", "+", "-"], label="Direction", value="+-")
|
| 50 |
+
],
|
| 51 |
+
outputs="text",
|
| 52 |
+
title="Limit Calculator",
|
| 53 |
+
description="Calculate the limit of an expression. Use 'oo' for infinity. Ensure variable in expression matches variable input."
|
| 54 |
+
)
|
maths/calculus/calculus_interface.py
DELETED
|
@@ -1,127 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from maths.calculus.derivative_polynomial import derivative_polynomial
|
| 3 |
-
from maths.calculus.integral_polynomial import integral_polynomial
|
| 4 |
-
from maths.calculus.calculate_limit import calculate_limit
|
| 5 |
-
from maths.calculus.taylor_series_expansion import taylor_series_expansion
|
| 6 |
-
from maths.calculus.fourier_series_example import fourier_series_example
|
| 7 |
-
from maths.calculus.partial_derivative import partial_derivative
|
| 8 |
-
from maths.calculus.multiple_integral import multiple_integral
|
| 9 |
-
import json # For parsing list of tuples for multiple integrals
|
| 10 |
-
|
| 11 |
-
# University Math Tab
|
| 12 |
-
derivative_interface = gr.Interface(
|
| 13 |
-
fn=lambda coefficients: derivative_polynomial([float(c) for c in coefficients.split(',')]),
|
| 14 |
-
inputs=gr.Textbox(label="Polynomial Coefficients (comma-separated, highest degree first)"),
|
| 15 |
-
outputs="json",
|
| 16 |
-
title="Polynomial Derivative",
|
| 17 |
-
description="Find the derivative of a polynomial function"
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
integral_interface = gr.Interface(
|
| 21 |
-
fn=lambda coefficients, c: integral_polynomial([float(x) for x in coefficients.split(',')], float(c)),
|
| 22 |
-
inputs=[
|
| 23 |
-
gr.Textbox(label="Polynomial Coefficients (comma-separated, highest degree first)"),
|
| 24 |
-
gr.Number(label="Integration Constant (c)", value=0)
|
| 25 |
-
],
|
| 26 |
-
outputs="json",
|
| 27 |
-
title="Polynomial Integration",
|
| 28 |
-
description="Find the indefinite integral of a polynomial function"
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
limit_interface = gr.Interface(
|
| 32 |
-
fn=calculate_limit,
|
| 33 |
-
inputs=[
|
| 34 |
-
gr.Textbox(label="Expression (e.g., sin(x)/x, x**2, exp(-x))", value="sin(x)/x"),
|
| 35 |
-
gr.Textbox(label="Variable (e.g., x)", value="x"),
|
| 36 |
-
gr.Textbox(label="Point (e.g., 0, 1, oo, -oo, pi)", value="0"),
|
| 37 |
-
gr.Radio(choices=["+-", "+", "-"], label="Direction", value="+-")
|
| 38 |
-
],
|
| 39 |
-
outputs="text",
|
| 40 |
-
title="Limit Calculator",
|
| 41 |
-
description="Calculate the limit of an expression. Use 'oo' for infinity. Ensure variable in expression matches variable input."
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
taylor_series_interface = gr.Interface(
|
| 45 |
-
fn=taylor_series_expansion,
|
| 46 |
-
inputs=[
|
| 47 |
-
gr.Textbox(label="Expression (e.g., exp(x), cos(x))", value="exp(x)"),
|
| 48 |
-
gr.Textbox(label="Variable (e.g., x)", value="x"),
|
| 49 |
-
gr.Number(label="Point of Expansion (x0)", value=0),
|
| 50 |
-
gr.Number(label="Order of Taylor Polynomial", value=5, precision=0)
|
| 51 |
-
],
|
| 52 |
-
outputs="text",
|
| 53 |
-
title="Taylor Series Expansion",
|
| 54 |
-
description="Compute the Taylor series of a function around a point."
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
fourier_series_interface = gr.Interface(
|
| 58 |
-
fn=fourier_series_example,
|
| 59 |
-
inputs=[
|
| 60 |
-
gr.Radio(choices=["sawtooth", "square"], label="Function Type", value="sawtooth"),
|
| 61 |
-
gr.Number(label="Number of Terms (n)", value=5, precision=0)
|
| 62 |
-
],
|
| 63 |
-
outputs="text",
|
| 64 |
-
title="Fourier Series Examples",
|
| 65 |
-
description="Generate Fourier series for predefined periodic functions (e.g., sawtooth wave, square wave)."
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
partial_derivative_interface = gr.Interface(
|
| 69 |
-
fn=lambda expr_str, vars_str: partial_derivative(expr_str, [v.strip() for v in vars_str.split(',') if v.strip()]),
|
| 70 |
-
inputs=[
|
| 71 |
-
gr.Textbox(label="Expression (e.g., x**2*y + z*sin(x))", value="x**2*y**3 + y*sin(z)"),
|
| 72 |
-
gr.Textbox(label="Variables to differentiate by (comma-separated, in order, e.g., x,y or x,x for d²f/dx²)", value="x,y")
|
| 73 |
-
],
|
| 74 |
-
outputs="text",
|
| 75 |
-
title="Partial Derivative Calculator",
|
| 76 |
-
description="Compute partial derivatives. For d/dy(d/dx(f)), enter 'x,y'. For d²f/dx², enter 'x,x'."
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
def parse_integration_variables(vars_json_str: str):
|
| 81 |
-
"""
|
| 82 |
-
Parses a JSON string representing a list of integration variables and their bounds.
|
| 83 |
-
Expected format: '[["var1", "lower1", "upper1"], ["var2", "lower2", "upper2"]]'
|
| 84 |
-
"""
|
| 85 |
-
try:
|
| 86 |
-
parsed_list = json.loads(vars_json_str)
|
| 87 |
-
if not isinstance(parsed_list, list):
|
| 88 |
-
raise ValueError("Input must be a JSON list.")
|
| 89 |
-
|
| 90 |
-
integration_vars = []
|
| 91 |
-
for item in parsed_list:
|
| 92 |
-
if not (isinstance(item, list) and len(item) == 3):
|
| 93 |
-
raise ValueError("Each item in the list must be a sub-list of three elements: [variable_name, lower_bound, upper_bound].")
|
| 94 |
-
|
| 95 |
-
var, low, upp = item
|
| 96 |
-
if not isinstance(var, str):
|
| 97 |
-
raise ValueError(f"Variable name '{var}' must be a string.")
|
| 98 |
-
|
| 99 |
-
# Bounds can be numbers or strings (for symbolic bounds like 'x')
|
| 100 |
-
if not (isinstance(low, (str, int, float))) or not (isinstance(upp, (str, int, float))):
|
| 101 |
-
raise ValueError(f"Bounds for variable '{var}' must be numbers or strings. Got '{low}' and '{upp}'.")
|
| 102 |
-
|
| 103 |
-
integration_vars.append((str(var), str(low), str(upp))) # Ensure all elements are strings for sympy processing later if needed
|
| 104 |
-
|
| 105 |
-
return integration_vars
|
| 106 |
-
except json.JSONDecodeError:
|
| 107 |
-
raise gr.Error("Invalid JSON format for integration variables.")
|
| 108 |
-
except ValueError as ve:
|
| 109 |
-
raise gr.Error(str(ve))
|
| 110 |
-
except Exception as e:
|
| 111 |
-
raise gr.Error(f"Unexpected error parsing integration variables: {str(e)}")
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
multiple_integral_interface = gr.Interface(
|
| 115 |
-
fn=lambda expr_str, vars_json_str: multiple_integral(expr_str, parse_integration_variables(vars_json_str)),
|
| 116 |
-
inputs=[
|
| 117 |
-
gr.Textbox(label="Expression (e.g., x*y**2, 1)", value="x*y"),
|
| 118 |
-
gr.Textbox(
|
| 119 |
-
label="Integration Variables and Bounds (JSON list of [var, low, upp] tuples, inner to outer)",
|
| 120 |
-
value='[["y", "0", "x"], ["x", "0", "1"]]' ,
|
| 121 |
-
info="Example: integral_0^1 dx integral_0^x dy (x*y) is [['y', '0', 'x'], ['x', '0', '1']]"
|
| 122 |
-
)
|
| 123 |
-
],
|
| 124 |
-
outputs="text",
|
| 125 |
-
title="Definite Multiple Integral Calculator",
|
| 126 |
-
description="Compute multiple integrals. Order of variables in list: inner-most integral first."
|
| 127 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maths/calculus/calculus_tab.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from maths.calculus.
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
calculus_interfaces_list = [
|
| 9 |
derivative_interface, integral_interface, limit_interface,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from maths.calculus.derivative_polynomial import derivative_interface
|
| 3 |
+
from maths.calculus.integral_polynomial import integral_interface
|
| 4 |
+
from maths.calculus.calculate_limit import limit_interface
|
| 5 |
+
from maths.calculus.taylor_series_expansion import taylor_series_interface
|
| 6 |
+
from maths.calculus.fourier_series_example import fourier_series_interface
|
| 7 |
+
from maths.calculus.partial_derivative import partial_derivative_interface
|
| 8 |
+
from maths.calculus.multiple_integral import multiple_integral_interface
|
| 9 |
|
| 10 |
calculus_interfaces_list = [
|
| 11 |
derivative_interface, integral_interface, limit_interface,
|
maths/calculus/derivative_polynomial.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import sympy
|
|
|
|
| 2 |
|
| 3 |
def derivative_polynomial(coefficients):
|
| 4 |
"""
|
|
@@ -19,3 +20,11 @@ def derivative_polynomial(coefficients):
|
|
| 19 |
result.append(coef * power)
|
| 20 |
|
| 21 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sympy
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
def derivative_polynomial(coefficients):
|
| 5 |
"""
|
|
|
|
| 20 |
result.append(coef * power)
|
| 21 |
|
| 22 |
return result
|
| 23 |
+
|
| 24 |
+
derivative_interface = gr.Interface(
|
| 25 |
+
fn=lambda coefficients: derivative_polynomial([float(c) for c in coefficients.split(',')]),
|
| 26 |
+
inputs=gr.Textbox(label="Polynomial Coefficients (comma-separated, highest degree first)"),
|
| 27 |
+
outputs="json",
|
| 28 |
+
title="Polynomial Derivative",
|
| 29 |
+
description="Find the derivative of a polynomial function"
|
| 30 |
+
)
|
maths/calculus/fourier_series_example.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import sympy
|
|
|
|
| 2 |
|
| 3 |
def fourier_series_example(function_type: str = "sawtooth", n_terms: int = 5) -> str:
|
| 4 |
"""
|
|
@@ -31,3 +32,14 @@ def fourier_series_example(function_type: str = "sawtooth", n_terms: int = 5) ->
|
|
| 31 |
return "Error: Unknown function type for Fourier series. Choose 'sawtooth' or 'square'."
|
| 32 |
except Exception as e:
|
| 33 |
return f"Error generating Fourier series: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sympy
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
def fourier_series_example(function_type: str = "sawtooth", n_terms: int = 5) -> str:
|
| 5 |
"""
|
|
|
|
| 32 |
return "Error: Unknown function type for Fourier series. Choose 'sawtooth' or 'square'."
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error generating Fourier series: {e}"
|
| 35 |
+
|
| 36 |
+
fourier_series_interface = gr.Interface(
|
| 37 |
+
fn=fourier_series_example,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Radio(choices=["sawtooth", "square"], label="Function Type", value="sawtooth"),
|
| 40 |
+
gr.Number(label="Number of Terms (n)", value=5, precision=0)
|
| 41 |
+
],
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="Fourier Series Examples",
|
| 44 |
+
description="Generate Fourier series for predefined periodic functions (e.g., sawtooth wave, square wave)."
|
| 45 |
+
)
|
maths/calculus/integral_polynomial.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import sympy
|
|
|
|
| 2 |
|
| 3 |
def integral_polynomial(coefficients, c=0):
|
| 4 |
"""
|
|
@@ -18,3 +19,14 @@ def integral_polynomial(coefficients, c=0):
|
|
| 18 |
|
| 19 |
result.append(c) # Add integration constant
|
| 20 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sympy
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
def integral_polynomial(coefficients, c=0):
|
| 5 |
"""
|
|
|
|
| 19 |
|
| 20 |
result.append(c) # Add integration constant
|
| 21 |
return result
|
| 22 |
+
|
| 23 |
+
integral_interface = gr.Interface(
|
| 24 |
+
fn=lambda coefficients, c: integral_polynomial([float(x) for x in coefficients.split(',')], float(c)),
|
| 25 |
+
inputs=[
|
| 26 |
+
gr.Textbox(label="Polynomial Coefficients (comma-separated, highest degree first)"),
|
| 27 |
+
gr.Number(label="Integration Constant (c)", value=0)
|
| 28 |
+
],
|
| 29 |
+
outputs="json",
|
| 30 |
+
title="Polynomial Integration",
|
| 31 |
+
description="Find the indefinite integral of a polynomial function"
|
| 32 |
+
)
|
maths/calculus/multiple_integral.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import sympy
|
| 2 |
from typing import List, Tuple, Union
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def multiple_integral(expression_str: str, integration_vars: List[Tuple[str, Union[str, float], Union[str, float]]]) -> str:
|
| 5 |
"""
|
|
@@ -34,3 +36,45 @@ def multiple_integral(expression_str: str, integration_vars: List[Tuple[str, Uni
|
|
| 34 |
return f"Error parsing expression, bounds, or variables: {e}. Ensure bounds are numbers or valid expressions."
|
| 35 |
except Exception as e:
|
| 36 |
return f"An unexpected error occurred during integration: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sympy
|
| 2 |
from typing import List, Tuple, Union
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
def multiple_integral(expression_str: str, integration_vars: List[Tuple[str, Union[str, float], Union[str, float]]]) -> str:
|
| 7 |
"""
|
|
|
|
| 36 |
return f"Error parsing expression, bounds, or variables: {e}. Ensure bounds are numbers or valid expressions."
|
| 37 |
except Exception as e:
|
| 38 |
return f"An unexpected error occurred during integration: {e}"
|
| 39 |
+
|
| 40 |
+
def parse_integration_variables(vars_json_str: str):
|
| 41 |
+
"""
|
| 42 |
+
Parses a JSON string representing a list of integration variables and their bounds.
|
| 43 |
+
Expected format: '[["var1", "lower1", "upper1"], ["var2", "lower2", "upper2"]]
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
parsed_list = json.loads(vars_json_str)
|
| 47 |
+
if not isinstance(parsed_list, list):
|
| 48 |
+
raise ValueError("Input must be a JSON list.")
|
| 49 |
+
integration_vars = []
|
| 50 |
+
for item in parsed_list:
|
| 51 |
+
if not (isinstance(item, list) and len(item) == 3):
|
| 52 |
+
raise ValueError("Each item in the list must be a sub-list of three elements: [variable_name, lower_bound, upper_bound].")
|
| 53 |
+
var, low, upp = item
|
| 54 |
+
if not isinstance(var, str):
|
| 55 |
+
raise ValueError(f"Variable name '{var}' must be a string.")
|
| 56 |
+
if not (isinstance(low, (str, int, float))) or not (isinstance(upp, (str, int, float))):
|
| 57 |
+
raise ValueError(f"Bounds for variable '{var}' must be numbers or strings. Got '{low}' and '{upp}'.")
|
| 58 |
+
integration_vars.append((str(var), str(low), str(upp)))
|
| 59 |
+
return integration_vars
|
| 60 |
+
except json.JSONDecodeError:
|
| 61 |
+
raise gr.Error("Invalid JSON format for integration variables.")
|
| 62 |
+
except ValueError as ve:
|
| 63 |
+
raise gr.Error(str(ve))
|
| 64 |
+
except Exception as e:
|
| 65 |
+
raise gr.Error(f"Unexpected error parsing integration variables: {str(e)}")
|
| 66 |
+
|
| 67 |
+
multiple_integral_interface = gr.Interface(
|
| 68 |
+
fn=lambda expr_str, vars_json_str: multiple_integral(expr_str, parse_integration_variables(vars_json_str)),
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Textbox(label="Expression (e.g., x*y**2, 1)", value="x*y"),
|
| 71 |
+
gr.Textbox(
|
| 72 |
+
label="Integration Variables and Bounds (JSON list of [var, low, upp] tuples, inner to outer)",
|
| 73 |
+
value='[["y", "0", "x"], ["x", "0", "1"]]',
|
| 74 |
+
info="Example: integral_0^1 dx integral_0^x dy (x*y) is [['y', '0', 'x'], ['x', '0', '1']]"
|
| 75 |
+
)
|
| 76 |
+
],
|
| 77 |
+
outputs="text",
|
| 78 |
+
title="Definite Multiple Integral Calculator",
|
| 79 |
+
description="Compute multiple integrals. Order of variables in list: inner-most integral first."
|
| 80 |
+
)
|
maths/calculus/partial_derivative.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import sympy
|
| 2 |
from typing import List
|
|
|
|
| 3 |
|
| 4 |
def partial_derivative(expression_str: str, variables_str: List[str]) -> str:
|
| 5 |
"""
|
|
@@ -34,3 +35,14 @@ def partial_derivative(expression_str: str, variables_str: List[str]) -> str:
|
|
| 34 |
return f"Error parsing expression or variables: {e}"
|
| 35 |
except Exception as e:
|
| 36 |
return f"An unexpected error occurred: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sympy
|
| 2 |
from typing import List
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
def partial_derivative(expression_str: str, variables_str: List[str]) -> str:
|
| 6 |
"""
|
|
|
|
| 35 |
return f"Error parsing expression or variables: {e}"
|
| 36 |
except Exception as e:
|
| 37 |
return f"An unexpected error occurred: {e}"
|
| 38 |
+
|
| 39 |
+
partial_derivative_interface = gr.Interface(
|
| 40 |
+
fn=lambda expr_str, vars_str: partial_derivative(expr_str, [v.strip() for v in vars_str.split(',') if v.strip()]),
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.Textbox(label="Expression (e.g., x**2*y + z*sin(x))", value="x**2*y**3 + y*sin(z)"),
|
| 43 |
+
gr.Textbox(label="Variables to differentiate by (comma-separated, in order, e.g., x,y or x,x for d²f/dx²)", value="x,y")
|
| 44 |
+
],
|
| 45 |
+
outputs="text",
|
| 46 |
+
title="Partial Derivative Calculator",
|
| 47 |
+
description="Compute partial derivatives. For d/dy(d/dx(f)), enter 'x,y'. For d²f/dx², enter 'x,x'."
|
| 48 |
+
)
|
maths/calculus/taylor_series_expansion.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import sympy
|
|
|
|
| 2 |
|
| 3 |
def taylor_series_expansion(expression_str: str, variable_str: str = 'x', point: float = 0, order: int = 5) -> str:
|
| 4 |
"""
|
|
@@ -20,3 +21,16 @@ def taylor_series_expansion(expression_str: str, variable_str: str = 'x', point:
|
|
| 20 |
return str(series)
|
| 21 |
except Exception as e:
|
| 22 |
return f"Error calculating Taylor series: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import sympy
|
| 2 |
+
import gradio as gr
|
| 3 |
|
| 4 |
def taylor_series_expansion(expression_str: str, variable_str: str = 'x', point: float = 0, order: int = 5) -> str:
|
| 5 |
"""
|
|
|
|
| 21 |
return str(series)
|
| 22 |
except Exception as e:
|
| 23 |
return f"Error calculating Taylor series: {e}"
|
| 24 |
+
|
| 25 |
+
taylor_series_interface = gr.Interface(
|
| 26 |
+
fn=taylor_series_expansion,
|
| 27 |
+
inputs=[
|
| 28 |
+
gr.Textbox(label="Expression (e.g., exp(x), cos(x))", value="exp(x)"),
|
| 29 |
+
gr.Textbox(label="Variable (e.g., x)", value="x"),
|
| 30 |
+
gr.Number(label="Point of Expansion (x0)", value=0),
|
| 31 |
+
gr.Number(label="Order of Taylor Polynomial", value=5, precision=0)
|
| 32 |
+
],
|
| 33 |
+
outputs="text",
|
| 34 |
+
title="Taylor Series Expansion",
|
| 35 |
+
description="Compute the Taylor series of a function around a point."
|
| 36 |
+
)
|