name: math-compute description: | Calculate, compute, evaluate mathematical expressions. Use for: compute integral, calculate derivative, evaluate limit, find sum of series, solve equation, matrix operations, eigenvalues, determinant, ODE solution, plot function, graph equation, numerical computation, symbolic math, SymPy, NumPy, Wolfram Alpha check. Triggers: "compute", "calculate", "evaluate", "what is the value of", "find the integral", "derivative of", "limit of", "sum of", "plot", "graph", "matrix", "eigenvalue" allowed-tools: Bash(python:), Bash(pip:), WebFetch, WebSearch
Mathematical Computation Engine
Execute symbolic and numerical computations with verification.
Anti-Hallucination Rules
- NEVER state a result without running code - every number must come from executed Python
- ALWAYS verify: symbolic result + numerical check + (optional) Wolfram Alpha
- If SymPy fails: say "SymPy cannot compute this" and try numerical methods
- If numerical fails: report the error, do not guess
Quick Start
cd /tmp && python3 -c "import sympy" 2>/dev/null || (python3 -m venv math_env && source math_env/bin/activate && pip install sympy numpy scipy matplotlib)
Computation Patterns
Derivatives
diff(x3 * exp(-x2), x)
Integrals (definite and indefinite)
integrate(exp(-x2), x) # indefinite integrate(exp(-x2), (x, -oo, oo)) # = sqrt(pi)
Limits
limit(sin(x)/x, x, 0) # = 1 limit((1 + 1/x)**x, x, oo) # = e
Series expansion
series(exp(x), x, 0, n=6)
</pattern>
<pattern name="Linear Algebra">
```python
from sympy import Matrix, symbols, simplify
A = Matrix([[4, 2], [1, 3]])
# Basic operations
A.det() # determinant
A.inv() # inverse
A.rank() # rank
# Eigenvalues/eigenvectors
eigenvals = A.eigenvals() # {eigenvalue: multiplicity}
eigenvects = A.eigenvects()
# Diagonalization
P, D = A.diagonalize()
# VERIFY: simplify(P * D * P.inv() - A) should be zero matrix
Solve ODE
ode = Eq(y(x).diff(x) + 2*y(x), exp(-x)) sol = dsolve(ode, y(x))
With initial conditions
ics = {y(0): 1} sol_ivp = dsolve(ode, y(x), ics=ics)
VERIFY by substitution
lhs = sol_ivp.rhs.diff(x) + 2*sol_ivp.rhs assert simplify(lhs - exp(-x)) == 0
</pattern>
<pattern name="Numerical Verification">
```python
import numpy as np
from scipy.integrate import quad
# Compare symbolic result with numerical
symbolic_result = float(sqrt(pi)) # from SymPy
numerical, error = quad(lambda x: np.exp(-x**2), -50, 50)
assert abs(symbolic_result - numerical) < 1e-6, "Mismatch!"
Wolfram Alpha Cross-Check
For critical results, verify with Wolfram Alpha:
import urllib.parse
query = "integrate x^2 exp(-x^2) from -infinity to infinity"
url = f"https://www.wolframalpha.com/input?i={urllib.parse.quote(query)}"
# Then use WebFetch to verify
Visualization
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 1000)
plt.figure(figsize=(10, 6))
plt.plot(x, np.sin(x), 'b-', linewidth=2, label='sin(x)')
plt.axhline(y=0, color='k', linewidth=0.5)
plt.axvline(x=0, color='k', linewidth=0.5)
plt.grid(True, alpha=0.3)
plt.legend()
plt.title('Function Plot')
plt.savefig('/tmp/plot.png', dpi=150, bbox_inches='tight')
print("Saved to /tmp/plot.png")
Output Format
- Symbolic result: Show SymPy output
- Numerical check: Verify with NumPy/SciPy
- Wolfram check: (if result is surprising or critical)
- Plot: (if visualization helps)
- Final answer: $$ \text{result in LaTeX} $$