Spaces:
Running
Running
kabudadada commited on
Commit ·
ac96648
1
Parent(s): b2a7907
Add advanced math tools
Browse files- README.md +86 -3
- sympy/mcp_output/mcp_plugin/mcp_service.py +333 -8
README.md
CHANGED
|
@@ -1,10 +1,93 @@
|
|
| 1 |
---
|
| 2 |
-
title: Code2MCP Sympy
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Code2MCP Sympy - Advanced Mathematical Computing
|
| 3 |
+
emoji: 🧮
|
| 4 |
colorFrom: purple
|
| 5 |
colorTo: yellow
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# Code2MCP Sympy - Advanced Mathematical Computing Service
|
| 11 |
+
|
| 12 |
+
A powerful MCP (Model Context Protocol) service based on SymPy, providing advanced symbolic mathematics and numerical computing capabilities.
|
| 13 |
+
|
| 14 |
+
## 🚀 Key Features
|
| 15 |
+
|
| 16 |
+
### Basic Mathematical Operations
|
| 17 |
+
- Symbolic expression creation and manipulation
|
| 18 |
+
- Expression expansion and simplification
|
| 19 |
+
- Equation solving (linear and nonlinear)
|
| 20 |
+
- Differentiation and integration
|
| 21 |
+
- Polynomial factorization
|
| 22 |
+
|
| 23 |
+
### Advanced Numerical Computing
|
| 24 |
+
- **Riemann Sum** - Riemann sum calculation (left, right, midpoint, trapezoid methods)
|
| 25 |
+
- **Darboux Sum** - Darboux sum calculation (upper and lower integrals)
|
| 26 |
+
- **Limit Calculation** - Support for finite and infinite limits
|
| 27 |
+
- **Area Calculation** - Area under curves (trapezoid method)
|
| 28 |
+
- **Volume Calculation** - Volume of revolution around x-axis (disk method)
|
| 29 |
+
|
| 30 |
+
### Transform Calculations
|
| 31 |
+
- **Fourier Transform** - Fourier transform (numerical approximation)
|
| 32 |
+
- **Laplace Transform** - Laplace transform (numerical approximation)
|
| 33 |
+
- **Z Transform** - Z transform (discrete-time systems)
|
| 34 |
+
|
| 35 |
+
### Financial Mathematics
|
| 36 |
+
- Compound interest calculation
|
| 37 |
+
- Present value calculation
|
| 38 |
+
- Net present value (NPV) calculation
|
| 39 |
+
- Logarithmic and exponential functions
|
| 40 |
+
|
| 41 |
+
## 🛠️ Technical Features
|
| 42 |
+
|
| 43 |
+
- Built on FastMCP framework
|
| 44 |
+
- SymPy symbolic computing support
|
| 45 |
+
- Integrated NumPy and SciPy numerical computing
|
| 46 |
+
- Comprehensive error handling
|
| 47 |
+
- Unified API response format
|
| 48 |
+
|
| 49 |
+
## 📦 Dependencies
|
| 50 |
+
|
| 51 |
+
- fastapi
|
| 52 |
+
- fastmcp >= 2.12.0
|
| 53 |
+
- sympy >= 1.12
|
| 54 |
+
- numpy >= 1.24.0
|
| 55 |
+
- scipy >= 1.10.0
|
| 56 |
+
- mpmath >= 1.3.0
|
| 57 |
+
|
| 58 |
+
## 🔧 Usage
|
| 59 |
+
|
| 60 |
+
The service provides the following MCP tools:
|
| 61 |
+
|
| 62 |
+
### Basic Tools
|
| 63 |
+
- `create_symbol` - Create symbolic variables
|
| 64 |
+
- `expand_expression` - Expand expressions
|
| 65 |
+
- `simplify_expression` - Simplify expressions
|
| 66 |
+
- `solve_equation` - Solve equations
|
| 67 |
+
- `differentiate` - Calculate derivatives
|
| 68 |
+
- `integrate_expression` - Calculate integrals
|
| 69 |
+
|
| 70 |
+
### Advanced Computing Tools
|
| 71 |
+
- `riemann_sum` - Riemann sum calculation
|
| 72 |
+
- `darboux_sum` - Darboux sum calculation
|
| 73 |
+
- `calculate_limit` - Limit calculation
|
| 74 |
+
- `calculate_area` - Area calculation
|
| 75 |
+
- `calculate_volume` - Volume calculation
|
| 76 |
+
- `fourier_transform` - Fourier transform
|
| 77 |
+
- `laplace_transform` - Laplace transform
|
| 78 |
+
- `z_transform` - Z transform
|
| 79 |
+
|
| 80 |
+
### Financial Tools
|
| 81 |
+
- `compound_interest` - Compound interest calculation
|
| 82 |
+
- `present_value` - Present value calculation
|
| 83 |
+
- `npv` - Net present value calculation
|
| 84 |
+
- `logarithm` - Logarithmic calculation
|
| 85 |
+
- `exponential` - Exponential calculation
|
| 86 |
+
|
| 87 |
+
## 🚀 Deployment
|
| 88 |
+
|
| 89 |
+
This service is configured to run on Hugging Face Spaces using Docker containerization.
|
| 90 |
+
|
| 91 |
+
---
|
| 92 |
+
|
| 93 |
+
Built with [SymPy](https://www.sympy.org/) and [FastMCP](https://github.com/pydantic/fastmcp).
|
sympy/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -12,8 +12,13 @@ from sympy.simplify import simplify
|
|
| 12 |
from sympy.solvers import solve, linsolve
|
| 13 |
from sympy.integrals import integrate
|
| 14 |
from sympy.polys import Poly, factor
|
| 15 |
-
from sympy.functions import sin, cos, exp
|
| 16 |
-
from sympy import sympify
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
mcp = FastMCP("sympy_service")
|
| 18 |
|
| 19 |
def _ser(x):
|
|
@@ -29,6 +34,9 @@ def _ser(x):
|
|
| 29 |
|
| 30 |
@mcp.tool(name="create_symbol")
|
| 31 |
def create_symbol(name: str):
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
res = symbols(name)
|
| 34 |
return {"success": True, "result": _ser(res), "error": None}
|
|
@@ -37,6 +45,9 @@ def create_symbol(name: str):
|
|
| 37 |
|
| 38 |
@mcp.tool(name="expand_expression")
|
| 39 |
def expand_expression(expr: str):
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
res = expand(sympify(expr))
|
| 42 |
return {"success": True, "result": _ser(res), "error": None}
|
|
@@ -45,6 +56,9 @@ def expand_expression(expr: str):
|
|
| 45 |
|
| 46 |
@mcp.tool(name="simplify_expression")
|
| 47 |
def simplify_expression(expr: str):
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
res = simplify(sympify(expr))
|
| 50 |
return {"success": True, "result": _ser(res), "error": None}
|
|
@@ -53,14 +67,22 @@ def simplify_expression(expr: str):
|
|
| 53 |
|
| 54 |
@mcp.tool(name="solve_equation")
|
| 55 |
def solve_equation(equation: str, variable: str):
|
|
|
|
|
|
|
|
|
|
| 56 |
try:
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
return {"success": True, "result": _ser(res), "error": None}
|
| 59 |
except Exception as e:
|
| 60 |
return {"success": False, "result": None, "error": str(e)}
|
| 61 |
|
| 62 |
@mcp.tool(name="solve_linear_system")
|
| 63 |
def solve_linear_system(system: list, variables: list):
|
|
|
|
|
|
|
|
|
|
| 64 |
try:
|
| 65 |
eqs = [sympify(e) for e in system]
|
| 66 |
vars_sym = [symbols(v) for v in variables]
|
|
@@ -71,38 +93,63 @@ def solve_linear_system(system: list, variables: list):
|
|
| 71 |
|
| 72 |
@mcp.tool(name="differentiate")
|
| 73 |
def differentiate(expr: str, variable: str):
|
|
|
|
|
|
|
|
|
|
| 74 |
try:
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
return {"success": True, "result": _ser(res), "error": None}
|
| 77 |
except Exception as e:
|
| 78 |
return {"success": False, "result": None, "error": str(e)}
|
| 79 |
|
| 80 |
@mcp.tool(name="integrate_expression")
|
| 81 |
def integrate_expression(expr: str, variable: str):
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
return {"success": True, "result": _ser(res), "error": None}
|
| 85 |
except Exception as e:
|
| 86 |
return {"success": False, "result": None, "error": str(e)}
|
| 87 |
|
| 88 |
@mcp.tool(name="create_polynomial")
|
| 89 |
-
def create_polynomial(
|
|
|
|
|
|
|
|
|
|
| 90 |
try:
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
return {"success": True, "result": _ser(res), "error": None}
|
| 93 |
except Exception as e:
|
| 94 |
return {"success": False, "result": None, "error": str(e)}
|
| 95 |
|
| 96 |
@mcp.tool(name="factor_polynomial")
|
| 97 |
def factor_polynomial(poly: str):
|
|
|
|
|
|
|
|
|
|
| 98 |
try:
|
| 99 |
-
|
|
|
|
| 100 |
return {"success": True, "result": _ser(res), "error": None}
|
| 101 |
except Exception as e:
|
| 102 |
return {"success": False, "result": None, "error": str(e)}
|
| 103 |
|
| 104 |
@mcp.tool(name="calculate_sin")
|
| 105 |
def calculate_sin(angle: str):
|
|
|
|
|
|
|
|
|
|
| 106 |
try:
|
| 107 |
res = sin(sympify(angle))
|
| 108 |
return {"success": True, "result": _ser(res), "error": None}
|
|
@@ -111,6 +158,9 @@ def calculate_sin(angle: str):
|
|
| 111 |
|
| 112 |
@mcp.tool(name="calculate_cos")
|
| 113 |
def calculate_cos(angle: str):
|
|
|
|
|
|
|
|
|
|
| 114 |
try:
|
| 115 |
res = cos(sympify(angle))
|
| 116 |
return {"success": True, "result": _ser(res), "error": None}
|
|
@@ -119,12 +169,287 @@ def calculate_cos(angle: str):
|
|
| 119 |
|
| 120 |
@mcp.tool(name="calculate_exp")
|
| 121 |
def calculate_exp(value: str):
|
|
|
|
|
|
|
|
|
|
| 122 |
try:
|
| 123 |
res = exp(sympify(value))
|
| 124 |
return {"success": True, "result": _ser(res), "error": None}
|
| 125 |
except Exception as e:
|
| 126 |
return {"success": False, "result": None, "error": str(e)}
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
def create_app():
|
|
|
|
| 12 |
from sympy.solvers import solve, linsolve
|
| 13 |
from sympy.integrals import integrate
|
| 14 |
from sympy.polys import Poly, factor
|
| 15 |
+
from sympy.functions import sin, cos, exp, log
|
| 16 |
+
from sympy import sympify, limit, oo
|
| 17 |
+
from sympy.calculus.util import continuous_domain
|
| 18 |
+
import numpy as np
|
| 19 |
+
from scipy import integrate as scipy_integrate
|
| 20 |
+
from scipy.fft import fft, ifft
|
| 21 |
+
from scipy.signal import lti
|
| 22 |
mcp = FastMCP("sympy_service")
|
| 23 |
|
| 24 |
def _ser(x):
|
|
|
|
| 34 |
|
| 35 |
@mcp.tool(name="create_symbol")
|
| 36 |
def create_symbol(name: str):
|
| 37 |
+
"""
|
| 38 |
+
Create a symbolic variable
|
| 39 |
+
"""
|
| 40 |
try:
|
| 41 |
res = symbols(name)
|
| 42 |
return {"success": True, "result": _ser(res), "error": None}
|
|
|
|
| 45 |
|
| 46 |
@mcp.tool(name="expand_expression")
|
| 47 |
def expand_expression(expr: str):
|
| 48 |
+
"""
|
| 49 |
+
Expand mathematical expression
|
| 50 |
+
"""
|
| 51 |
try:
|
| 52 |
res = expand(sympify(expr))
|
| 53 |
return {"success": True, "result": _ser(res), "error": None}
|
|
|
|
| 56 |
|
| 57 |
@mcp.tool(name="simplify_expression")
|
| 58 |
def simplify_expression(expr: str):
|
| 59 |
+
"""
|
| 60 |
+
Simplify mathematical expression
|
| 61 |
+
"""
|
| 62 |
try:
|
| 63 |
res = simplify(sympify(expr))
|
| 64 |
return {"success": True, "result": _ser(res), "error": None}
|
|
|
|
| 67 |
|
| 68 |
@mcp.tool(name="solve_equation")
|
| 69 |
def solve_equation(equation: str, variable: str):
|
| 70 |
+
"""
|
| 71 |
+
Solve equation for variable
|
| 72 |
+
"""
|
| 73 |
try:
|
| 74 |
+
expr = sympify(equation)
|
| 75 |
+
var = symbols(variable)
|
| 76 |
+
res = solve(expr, var)
|
| 77 |
return {"success": True, "result": _ser(res), "error": None}
|
| 78 |
except Exception as e:
|
| 79 |
return {"success": False, "result": None, "error": str(e)}
|
| 80 |
|
| 81 |
@mcp.tool(name="solve_linear_system")
|
| 82 |
def solve_linear_system(system: list, variables: list):
|
| 83 |
+
"""
|
| 84 |
+
Solve system of linear equations
|
| 85 |
+
"""
|
| 86 |
try:
|
| 87 |
eqs = [sympify(e) for e in system]
|
| 88 |
vars_sym = [symbols(v) for v in variables]
|
|
|
|
| 93 |
|
| 94 |
@mcp.tool(name="differentiate")
|
| 95 |
def differentiate(expr: str, variable: str):
|
| 96 |
+
"""
|
| 97 |
+
Calculate derivative of expression with respect to variable
|
| 98 |
+
"""
|
| 99 |
try:
|
| 100 |
+
expr_sym = sympify(expr)
|
| 101 |
+
var = symbols(variable)
|
| 102 |
+
res = diff(expr_sym, var)
|
| 103 |
return {"success": True, "result": _ser(res), "error": None}
|
| 104 |
except Exception as e:
|
| 105 |
return {"success": False, "result": None, "error": str(e)}
|
| 106 |
|
| 107 |
@mcp.tool(name="integrate_expression")
|
| 108 |
def integrate_expression(expr: str, variable: str):
|
| 109 |
+
"""
|
| 110 |
+
Calculate integral of expression with respect to variable
|
| 111 |
+
"""
|
| 112 |
try:
|
| 113 |
+
expr_sym = sympify(expr)
|
| 114 |
+
var = symbols(variable)
|
| 115 |
+
res = integrate(expr_sym, var)
|
| 116 |
return {"success": True, "result": _ser(res), "error": None}
|
| 117 |
except Exception as e:
|
| 118 |
return {"success": False, "result": None, "error": str(e)}
|
| 119 |
|
| 120 |
@mcp.tool(name="create_polynomial")
|
| 121 |
+
def create_polynomial(expr: str, variable: str = None):
|
| 122 |
+
"""
|
| 123 |
+
Create polynomial from expression
|
| 124 |
+
"""
|
| 125 |
try:
|
| 126 |
+
expr_sym = sympify(expr)
|
| 127 |
+
if variable:
|
| 128 |
+
var = symbols(variable)
|
| 129 |
+
res = Poly(expr_sym, var)
|
| 130 |
+
else:
|
| 131 |
+
res = Poly(expr_sym)
|
| 132 |
return {"success": True, "result": _ser(res), "error": None}
|
| 133 |
except Exception as e:
|
| 134 |
return {"success": False, "result": None, "error": str(e)}
|
| 135 |
|
| 136 |
@mcp.tool(name="factor_polynomial")
|
| 137 |
def factor_polynomial(poly: str):
|
| 138 |
+
"""
|
| 139 |
+
Factor polynomial expression
|
| 140 |
+
"""
|
| 141 |
try:
|
| 142 |
+
poly_sym = sympify(poly)
|
| 143 |
+
res = factor(poly_sym)
|
| 144 |
return {"success": True, "result": _ser(res), "error": None}
|
| 145 |
except Exception as e:
|
| 146 |
return {"success": False, "result": None, "error": str(e)}
|
| 147 |
|
| 148 |
@mcp.tool(name="calculate_sin")
|
| 149 |
def calculate_sin(angle: str):
|
| 150 |
+
"""
|
| 151 |
+
Calculate sine of angle
|
| 152 |
+
"""
|
| 153 |
try:
|
| 154 |
res = sin(sympify(angle))
|
| 155 |
return {"success": True, "result": _ser(res), "error": None}
|
|
|
|
| 158 |
|
| 159 |
@mcp.tool(name="calculate_cos")
|
| 160 |
def calculate_cos(angle: str):
|
| 161 |
+
"""
|
| 162 |
+
Calculate cosine of angle
|
| 163 |
+
"""
|
| 164 |
try:
|
| 165 |
res = cos(sympify(angle))
|
| 166 |
return {"success": True, "result": _ser(res), "error": None}
|
|
|
|
| 169 |
|
| 170 |
@mcp.tool(name="calculate_exp")
|
| 171 |
def calculate_exp(value: str):
|
| 172 |
+
"""
|
| 173 |
+
Calculate exponential function e^x
|
| 174 |
+
"""
|
| 175 |
try:
|
| 176 |
res = exp(sympify(value))
|
| 177 |
return {"success": True, "result": _ser(res), "error": None}
|
| 178 |
except Exception as e:
|
| 179 |
return {"success": False, "result": None, "error": str(e)}
|
| 180 |
|
| 181 |
+
@mcp.tool(name="riemann_sum")
|
| 182 |
+
def riemann_sum(expression: str, variable: str, a: float, b: float, n: int, method: str = "midpoint"):
|
| 183 |
+
"""
|
| 184 |
+
Calculate Riemann sum of a function
|
| 185 |
+
method: 'left', 'right', 'midpoint', 'trapezoid'
|
| 186 |
+
"""
|
| 187 |
+
try:
|
| 188 |
+
# Parse expression
|
| 189 |
+
expr = sympify(expression)
|
| 190 |
+
var = symbols(variable)
|
| 191 |
+
|
| 192 |
+
# Calculate step size
|
| 193 |
+
delta_x = (b - a) / n
|
| 194 |
+
sum_result = 0
|
| 195 |
+
|
| 196 |
+
if method == "left":
|
| 197 |
+
for i in range(n):
|
| 198 |
+
x_val = a + i * delta_x
|
| 199 |
+
sum_result += float(expr.subs(var, x_val)) * delta_x
|
| 200 |
+
elif method == "right":
|
| 201 |
+
for i in range(1, n + 1):
|
| 202 |
+
x_val = a + i * delta_x
|
| 203 |
+
sum_result += float(expr.subs(var, x_val)) * delta_x
|
| 204 |
+
elif method == "midpoint":
|
| 205 |
+
for i in range(n):
|
| 206 |
+
x_val = a + (i + 0.5) * delta_x
|
| 207 |
+
sum_result += float(expr.subs(var, x_val)) * delta_x
|
| 208 |
+
elif method == "trapezoid":
|
| 209 |
+
for i in range(n + 1):
|
| 210 |
+
x_val = a + i * delta_x
|
| 211 |
+
weight = 0.5 if (i == 0 or i == n) else 1.0
|
| 212 |
+
sum_result += weight * float(expr.subs(var, x_val)) * delta_x
|
| 213 |
+
else:
|
| 214 |
+
return {"success": False, "result": None, "error": "Invalid method. Use 'left', 'right', 'midpoint', or 'trapezoid'"}
|
| 215 |
+
|
| 216 |
+
return {"success": True, "result": sum_result, "error": None}
|
| 217 |
+
except Exception as e:
|
| 218 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 219 |
+
|
| 220 |
+
@mcp.tool(name="darboux_sum")
|
| 221 |
+
def darboux_sum(expression: str, variable: str, a: float, b: float, n: int, sum_type: str = "upper"):
|
| 222 |
+
"""
|
| 223 |
+
Calculate Darboux sum (upper or lower integral)
|
| 224 |
+
sum_type: 'upper' or 'lower'
|
| 225 |
+
"""
|
| 226 |
+
try:
|
| 227 |
+
expr = sympify(expression)
|
| 228 |
+
var = symbols(variable)
|
| 229 |
+
|
| 230 |
+
delta_x = (b - a) / n
|
| 231 |
+
sum_result = 0
|
| 232 |
+
|
| 233 |
+
for i in range(n):
|
| 234 |
+
x1 = a + i * delta_x
|
| 235 |
+
x2 = x1 + delta_x
|
| 236 |
+
y1 = float(expr.subs(var, x1))
|
| 237 |
+
y2 = float(expr.subs(var, x2))
|
| 238 |
+
|
| 239 |
+
if sum_type == "upper":
|
| 240 |
+
value = max(y1, y2)
|
| 241 |
+
else: # lower
|
| 242 |
+
value = min(y1, y2)
|
| 243 |
+
|
| 244 |
+
sum_result += value * delta_x
|
| 245 |
+
|
| 246 |
+
return {"success": True, "result": sum_result, "error": None}
|
| 247 |
+
except Exception as e:
|
| 248 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 249 |
+
|
| 250 |
+
@mcp.tool(name="calculate_limit")
|
| 251 |
+
def calculate_limit(expression: str, variable: str, approach: str):
|
| 252 |
+
"""
|
| 253 |
+
Calculate function limit
|
| 254 |
+
approach: can be a number or 'oo' (infinity) or '-oo' (negative infinity)
|
| 255 |
+
"""
|
| 256 |
+
try:
|
| 257 |
+
expr = sympify(expression)
|
| 258 |
+
var = symbols(variable)
|
| 259 |
+
|
| 260 |
+
if approach == 'oo':
|
| 261 |
+
approach_val = oo
|
| 262 |
+
elif approach == '-oo':
|
| 263 |
+
approach_val = -oo
|
| 264 |
+
else:
|
| 265 |
+
approach_val = float(approach)
|
| 266 |
+
|
| 267 |
+
res = limit(expr, var, approach_val)
|
| 268 |
+
return {"success": True, "result": _ser(res), "error": None}
|
| 269 |
+
except Exception as e:
|
| 270 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 271 |
+
|
| 272 |
+
@mcp.tool(name="calculate_area")
|
| 273 |
+
def calculate_area(expression: str, start: float, end: float, n: int = 1000):
|
| 274 |
+
"""
|
| 275 |
+
Calculate area under curve (using trapezoid method)
|
| 276 |
+
"""
|
| 277 |
+
try:
|
| 278 |
+
expr = sympify(expression)
|
| 279 |
+
var = symbols('x')
|
| 280 |
+
|
| 281 |
+
# Calculate area using trapezoid method
|
| 282 |
+
delta_x = (end - start) / n
|
| 283 |
+
area = 0
|
| 284 |
+
|
| 285 |
+
for i in range(n + 1):
|
| 286 |
+
x_val = start + i * delta_x
|
| 287 |
+
weight = 0.5 if (i == 0 or i == n) else 1.0
|
| 288 |
+
area += weight * float(expr.subs(var, x_val)) * delta_x
|
| 289 |
+
|
| 290 |
+
return {"success": True, "result": area, "error": None}
|
| 291 |
+
except Exception as e:
|
| 292 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 293 |
+
|
| 294 |
+
@mcp.tool(name="calculate_volume")
|
| 295 |
+
def calculate_volume(expression: str, start: float, end: float, n: int = 1000):
|
| 296 |
+
"""
|
| 297 |
+
Calculate volume of revolution around x-axis (disk method)
|
| 298 |
+
"""
|
| 299 |
+
try:
|
| 300 |
+
expr = sympify(expression)
|
| 301 |
+
var = symbols('x')
|
| 302 |
+
|
| 303 |
+
delta_x = (end - start) / n
|
| 304 |
+
volume = 0
|
| 305 |
+
|
| 306 |
+
for i in range(n):
|
| 307 |
+
x_val = start + i * delta_x
|
| 308 |
+
y_val = float(expr.subs(var, x_val))
|
| 309 |
+
volume += np.pi * y_val * y_val * delta_x
|
| 310 |
+
|
| 311 |
+
return {"success": True, "result": volume, "error": None}
|
| 312 |
+
except Exception as e:
|
| 313 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 314 |
+
|
| 315 |
+
@mcp.tool(name="fourier_transform")
|
| 316 |
+
def fourier_transform(expression: str, time_var: str, freq_var: str):
|
| 317 |
+
"""
|
| 318 |
+
Calculate Fourier transform (numerical approximation)
|
| 319 |
+
"""
|
| 320 |
+
try:
|
| 321 |
+
expr = sympify(expression)
|
| 322 |
+
t = symbols(time_var)
|
| 323 |
+
omega = symbols(freq_var)
|
| 324 |
+
|
| 325 |
+
# Use numerical integration to approximate Fourier transform
|
| 326 |
+
def integrand(t_val):
|
| 327 |
+
return float(expr.subs(t, t_val))
|
| 328 |
+
|
| 329 |
+
# Define integration range and frequency range
|
| 330 |
+
t_range = (-50, 50) # Time range
|
| 331 |
+
omega_vals = np.linspace(-10, 10, 100) # Frequency range
|
| 332 |
+
|
| 333 |
+
result = []
|
| 334 |
+
for omega_val in omega_vals:
|
| 335 |
+
def f(t_val):
|
| 336 |
+
return integrand(t_val) * np.exp(-1j * omega_val * t_val)
|
| 337 |
+
|
| 338 |
+
# Use numerical integration
|
| 339 |
+
integral_result, _ = scipy_integrate.quad(f, t_range[0], t_range[1], limit=100)
|
| 340 |
+
result.append(complex(integral_result))
|
| 341 |
+
|
| 342 |
+
return {"success": True, "result": [str(complex(r)) for r in result], "error": None}
|
| 343 |
+
except Exception as e:
|
| 344 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 345 |
+
|
| 346 |
+
@mcp.tool(name="laplace_transform")
|
| 347 |
+
def laplace_transform(expression: str, time_var: str, laplace_var: str):
|
| 348 |
+
"""
|
| 349 |
+
Calculate Laplace transform (numerical approximation)
|
| 350 |
+
"""
|
| 351 |
+
try:
|
| 352 |
+
expr = sympify(expression)
|
| 353 |
+
t = symbols(time_var)
|
| 354 |
+
s = symbols(laplace_var)
|
| 355 |
+
|
| 356 |
+
def integrand(t_val):
|
| 357 |
+
return float(expr.subs(t, t_val))
|
| 358 |
+
|
| 359 |
+
# Define integration range and Laplace variable range
|
| 360 |
+
t_range = (0, 100) # Time range
|
| 361 |
+
s_vals = np.linspace(0.1, 10, 50) # Laplace variable range
|
| 362 |
+
|
| 363 |
+
result = []
|
| 364 |
+
for s_val in s_vals:
|
| 365 |
+
def f(t_val):
|
| 366 |
+
return integrand(t_val) * np.exp(-s_val * t_val)
|
| 367 |
+
|
| 368 |
+
# Use numerical integration
|
| 369 |
+
integral_result, _ = scipy_integrate.quad(f, t_range[0], t_range[1], limit=100)
|
| 370 |
+
result.append(complex(integral_result))
|
| 371 |
+
|
| 372 |
+
return {"success": True, "result": [str(complex(r)) for r in result], "error": None}
|
| 373 |
+
except Exception as e:
|
| 374 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 375 |
+
|
| 376 |
+
@mcp.tool(name="z_transform")
|
| 377 |
+
def z_transform(expression: str, time_var: str, z_var: str, limit: int = 100):
|
| 378 |
+
"""
|
| 379 |
+
Calculate Z transform
|
| 380 |
+
"""
|
| 381 |
+
try:
|
| 382 |
+
expr = sympify(expression)
|
| 383 |
+
n = symbols(time_var)
|
| 384 |
+
z = symbols(z_var)
|
| 385 |
+
|
| 386 |
+
result = 0
|
| 387 |
+
for k in range(limit + 1):
|
| 388 |
+
term = expr.subs(n, k) * (z ** (-k))
|
| 389 |
+
result += term
|
| 390 |
+
|
| 391 |
+
return {"success": True, "result": _ser(result), "error": None}
|
| 392 |
+
except Exception as e:
|
| 393 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 394 |
+
|
| 395 |
+
@mcp.tool(name="compound_interest")
|
| 396 |
+
def compound_interest(principal: float, rate: float, time: float, compounds: int = 12):
|
| 397 |
+
"""
|
| 398 |
+
Calculate compound interest
|
| 399 |
+
"""
|
| 400 |
+
try:
|
| 401 |
+
result = principal * (1 + rate / compounds) ** (compounds * time)
|
| 402 |
+
return {"success": True, "result": result, "error": None}
|
| 403 |
+
except Exception as e:
|
| 404 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 405 |
+
|
| 406 |
+
@mcp.tool(name="present_value")
|
| 407 |
+
def present_value(future_value: float, rate: float, time: float):
|
| 408 |
+
"""
|
| 409 |
+
Calculate present value
|
| 410 |
+
"""
|
| 411 |
+
try:
|
| 412 |
+
result = future_value / (1 + rate) ** time
|
| 413 |
+
return {"success": True, "result": result, "error": None}
|
| 414 |
+
except Exception as e:
|
| 415 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 416 |
+
|
| 417 |
+
@mcp.tool(name="npv")
|
| 418 |
+
def npv(cash_flows: list, rate: float):
|
| 419 |
+
"""
|
| 420 |
+
Calculate net present value
|
| 421 |
+
"""
|
| 422 |
+
try:
|
| 423 |
+
result = sum(cf / (1 + rate) ** t for t, cf in enumerate(cash_flows))
|
| 424 |
+
return {"success": True, "result": result, "error": None}
|
| 425 |
+
except Exception as e:
|
| 426 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 427 |
+
|
| 428 |
+
@mcp.tool(name="logarithm")
|
| 429 |
+
def logarithm(value: float, base: float = None):
|
| 430 |
+
"""
|
| 431 |
+
Calculate logarithm
|
| 432 |
+
"""
|
| 433 |
+
try:
|
| 434 |
+
if base is None:
|
| 435 |
+
result = np.log(value)
|
| 436 |
+
else:
|
| 437 |
+
result = np.log(value) / np.log(base)
|
| 438 |
+
return {"success": True, "result": result, "error": None}
|
| 439 |
+
except Exception as e:
|
| 440 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 441 |
+
|
| 442 |
+
@mcp.tool(name="exponential")
|
| 443 |
+
def exponential(power: float):
|
| 444 |
+
"""
|
| 445 |
+
Calculate exponential function e^x
|
| 446 |
+
"""
|
| 447 |
+
try:
|
| 448 |
+
result = np.exp(power)
|
| 449 |
+
return {"success": True, "result": result, "error": None}
|
| 450 |
+
except Exception as e:
|
| 451 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 452 |
+
|
| 453 |
|
| 454 |
|
| 455 |
def create_app():
|