| name | math-computation |
| description | Mathematical computation including symbolic math, numerical methods, linear algebra, calculus, differential equations, optimization, and mathematical modeling. Uses Python with SymPy, NumPy, SciPy. Use when user asks to solve equations, compute integrals/derivatives, do matrix operations, solve ODEs/PDEs, optimize functions, or build mathematical models. Triggers on "solve equation", "integral", "derivative", "matrix", "eigenvalue", "differential equation", "optimization", "linear algebra", "symbolic math", "proof". |
Mathematical Computation
Symbolic and numerical mathematics. Venv: source /Users/zhangmingda/clawd/.venv/bin/activate
Symbolic Math (SymPy)
from sympy import *
x, y, z, t = symbols('x y z t')
a, b, c = symbols('a b c', real=True)
n, k = symbols('n k', integer=True, positive=True)
solve(x**2 - 5*x + 6, x)
solve([x + y - 5, x - y - 1], [x, y])
diff(sin(x)*exp(x), x)
integrate(x**2 * exp(-x), (x, 0, oo))
limit(sin(x)/x, x, 0)
series(exp(x), x, 0, 5)
M = Matrix([[1, 2], [3, 4]])
M.eigenvals()
M.eigenvects()
M.det()
M.inv()
f = Function('f')
dsolve(f(x).diff(x, 2) + f(x), f(x))
simplify(sin(x)**2 + cos(x)**2)
trigsimp(expr)
factor(expr)
expand(expr)
latex(expr)
Numerical Methods (SciPy)
from scipy import optimize, integrate, linalg, interpolate
import numpy as np
root = optimize.brentq(lambda x: x**3 - 2*x - 5, 2, 3)
result = optimize.minimize(lambda x: (x[0]-1)**2 + (x[1]-2.5)**2,
x0=[0, 0], method='Nelder-Mead')
from scipy.optimize import linprog, minimize
result = minimize(objective, x0, constraints=constraints, bounds=bounds)
val, err = integrate.quad(lambda x: np.exp(-x**2), -np.inf, np.inf)
from scipy.integrate import solve_ivp
def lorenz(t, state, sigma=10, rho=28, beta=8/3):
x, y, z = state
return [sigma*(y-x), x*(rho-z)-y, x*y-beta*z]
sol = solve_ivp(lorenz, [0, 50], [1, 1, 1], dense_output=True, max_step=0.01)
f_interp = interpolate.interp1d(x_data, y_data, kind='cubic')
from scipy.fft import fft, fftfreq
yf = fft(signal)
xf = fftfreq(N, 1/sample_rate)
Linear Algebra
A = np.array([[1, 2], [3, 4]])
np.linalg.eig(A)
np.linalg.svd(A)
np.linalg.solve(A, b)
np.linalg.norm(A)
np.linalg.matrix_rank(A)
from scipy.sparse import csr_matrix, linalg as sparse_linalg
Mathematical Modeling Workflow
- Define the system and variables
- Formulate equations (conservation laws, constitutive relations)
- Non-dimensionalize if appropriate
- Solve analytically (SymPy) or numerically (SciPy)
- Validate against known solutions or data
- Sensitivity analysis on parameters
- Visualize results
Common Models
- Population dynamics: Lotka-Volterra, SIR/SEIR epidemiological
- Diffusion: Heat equation, Fick's law
- Mechanics: Newton's laws, Lagrangian/Hamiltonian
- Economics: Supply-demand, game theory, optimal control
- Networks: Graph theory, flow optimization
Tips
- Use SymPy for exact solutions, SciPy for numerical
- Always verify numerical solutions against analytical when possible
- Check units and dimensional consistency
- Use
latex() to generate paper-ready equations
- For large systems, consider sparse matrix methods