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 equations
solve(x**2 - 5*x + 6, x) # [2, 3]
solve([x + y - 5, x - y - 1], [x, y]) # {x: 3, y: 2}
# Calculus
diff(sin(x)*exp(x), x) # derivative
integrate(x**2 * exp(-x), (x, 0, oo)) # definite integral
limit(sin(x)/x, x, 0) # limit
series(exp(x), x, 0, 5) # Taylor series
# Linear algebra
M = Matrix([[1, 2], [3, 4]])
M.eigenvals() # eigenvalues
M.eigenvects() # eigenvectors
M.det() # determinant
M.inv() # inverse
# Differential equations
f = Function('f')
dsolve(f(x).diff(x, 2) + f(x), f(x)) # y'' + y = 0
# Simplification
simplify(sin(x)**2 + cos(x)**2) # 1
trigsimp(expr)
factor(expr)
expand(expr)
# LaTeX output
latex(expr) # for paper-ready equations
Numerical Methods (SciPy)
from scipy import optimize, integrate, linalg, interpolate
import numpy as np
# Root finding
root = optimize.brentq(lambda x: x**3 - 2*x - 5, 2, 3)
# Optimization
result = optimize.minimize(lambda x: (x[0]-1)**2 + (x[1]-2.5)**2,
x0=[0, 0], method='Nelder-Mead')
# Constrained optimization
from scipy.optimize import linprog, minimize
result = minimize(objective, x0, constraints=constraints, bounds=bounds)
# Numerical integration
val, err = integrate.quad(lambda x: np.exp(-x**2), -np.inf, np.inf) # √π
# ODE solving
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)
# Interpolation
f_interp = interpolate.interp1d(x_data, y_data, kind='cubic')
# FFT
from scipy.fft import fft, fftfreq
yf = fft(signal)
xf = fftfreq(N, 1/sample_rate)
Linear Algebra
# NumPy
A = np.array([[1, 2], [3, 4]])
np.linalg.eig(A) # eigendecomposition
np.linalg.svd(A) # SVD
np.linalg.solve(A, b) # solve Ax = b
np.linalg.norm(A) # matrix norm
np.linalg.matrix_rank(A)
# Sparse matrices (SciPy)
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
1---2name: math-computation3description: 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".4---5
6# Mathematical Computation
7
8Symbolic and numerical mathematics. Venv: `source /Users/zhangmingda/clawd/.venv/bin/activate`
9
10## Symbolic Math (SymPy)
11
12```python
13from sympy import *
14x, y, z, t = symbols('x y z t')
15a, b, c = symbols('a b c', real=True)
16n, k = symbols('n k', integer=True, positive=True)
17
18# Solve equations
19solve(x**2 - 5*x + 6, x) # [2, 3]
20solve([x + y - 5, x - y - 1], [x, y]) # {x: 3, y: 2}
21
22# Calculus
23diff(sin(x)*exp(x), x) # derivative
24integrate(x**2 * exp(-x), (x, 0, oo)) # definite integral
25limit(sin(x)/x, x, 0) # limit
26series(exp(x), x, 0, 5) # Taylor series
27
28# Linear algebra
29M = Matrix([[1, 2], [3, 4]])
30M.eigenvals() # eigenvalues
31M.eigenvects() # eigenvectors
32M.det() # determinant
33M.inv() # inverse
34
35# Differential equations
36f = Function('f')
37dsolve(f(x).diff(x, 2) + f(x), f(x)) # y'' + y = 0
38
39# Simplification
40simplify(sin(x)**2 + cos(x)**2) # 1
41trigsimp(expr)
42factor(expr)
43expand(expr)
44
45# LaTeX output
46latex(expr) # for paper-ready equations
47```
48
49## Numerical Methods (SciPy)
50
51```python
52from scipy import optimize, integrate, linalg, interpolate
53import numpy as np
54
55# Root finding
56root = optimize.brentq(lambda x: x**3 - 2*x - 5, 2, 3)
57
58# Optimization
59result = optimize.minimize(lambda x: (x[0]-1)**2 + (x[1]-2.5)**2,
60 x0=[0, 0], method='Nelder-Mead')
61
62# Constrained optimization
63from scipy.optimize import linprog, minimize
64result = minimize(objective, x0, constraints=constraints, bounds=bounds)
65
66# Numerical integration
67val, err = integrate.quad(lambda x: np.exp(-x**2), -np.inf, np.inf) # √π
68
69# ODE solving
70from scipy.integrate import solve_ivp
71def lorenz(t, state, sigma=10, rho=28, beta=8/3):
72 x, y, z = state
73 return [sigma*(y-x), x*(rho-z)-y, x*y-beta*z]
74sol = solve_ivp(lorenz, [0, 50], [1, 1, 1], dense_output=True, max_step=0.01)
75
76# Interpolation
77f_interp = interpolate.interp1d(x_data, y_data, kind='cubic')
78
79# FFT
80from scipy.fft import fft, fftfreq
81yf = fft(signal)
82xf = fftfreq(N, 1/sample_rate)
83```
84
85## Linear Algebra
86
87```python
88# NumPy
89A = np.array([[1, 2], [3, 4]])
90np.linalg.eig(A) # eigendecomposition
91np.linalg.svd(A) # SVD
92np.linalg.solve(A, b) # solve Ax = b
93np.linalg.norm(A) # matrix norm
94np.linalg.matrix_rank(A)
95
96# Sparse matrices (SciPy)
97from scipy.sparse import csr_matrix, linalg as sparse_linalg
98```
99
100## Mathematical Modeling Workflow
101
1021. **Define** the system and variables
1032. **Formulate** equations (conservation laws, constitutive relations)
1043. **Non-dimensionalize** if appropriate
1054. **Solve** analytically (SymPy) or numerically (SciPy)
1065. **Validate** against known solutions or data
1076. **Sensitivity analysis** on parameters
1087. **Visualize** results
109
110## Common Models
111
112- **Population dynamics**: Lotka-Volterra, SIR/SEIR epidemiological
113- **Diffusion**: Heat equation, Fick's law
114- **Mechanics**: Newton's laws, Lagrangian/Hamiltonian
115- **Economics**: Supply-demand, game theory, optimal control
116- **Networks**: Graph theory, flow optimization
117
118## Tips
119- Use SymPy for exact solutions, SciPy for numerical
120- Always verify numerical solutions against analytical when possible
121- Check units and dimensional consistency
122- Use `latex()` to generate paper-ready equations
123- For large systems, consider sparse matrix methods