SymPy - Symbolic Mathematics in Python
Detailed Guide
Read the detailed guide before executing this skill. It retains the complete procedure and reference material. Treat its safety, prerequisites, and validation requirements as mandatory. For focused work, load the relevant sections; for end-to-end work, read the guide completely.
When to Use This Skill
Use this skill when:
- Solving equations symbolically (algebraic, differential, systems of equations)
- Performing calculus operations (derivatives, integrals, limits, series)
- Manipulating and simplifying algebraic expressions
- Working with matrices and linear algebra symbolically
- Doing physics calculations (mechanics, quantum mechanics, vector analysis)
- Number theory computations (primes, factorization, modular arithmetic)
- Geometric calculations (2D/3D geometry, analytic geometry)
- Converting mathematical expressions to executable code (Python, C, Fortran)
- Generating LaTeX or other formatted mathematical output
- Needing exact mathematical results (e.g.,
sqrt(2) not 1.414...)
Getting Started Examples
Example 1: Solve Quadratic Equation
from sympy import symbols, solve, sqrt
x = symbols('x')
solution = solve(x**2 - 5*x + 6, x)
# [2, 3]
Example 2: Calculate Derivative
from sympy import symbols, diff, sin
x = symbols('x')
f = sin(x**2)
df_dx = diff(f, x)
# 2*x*cos(x**2)
Example 3: Evaluate Integral
from sympy import symbols, integrate, exp
x = symbols('x')
integral = integrate(x * exp(-x**2), (x, 0, oo))
# 1/2
Example 4: Matrix Eigenvalues
from sympy import Matrix
M = Matrix([[1, 2], [2, 1]])
eigenvals = M.eigenvals()
# {3: 1, -1: 1}
Example 5: Generate Python Function
from sympy import symbols, lambdify
import numpy as np
x = symbols('x')
expr = x**2 + 2*x + 1
f = lambdify(x, expr, 'numpy')
f(np.array([1, 2, 3]))
# array([ 4, 9, 16])
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
1---2name: sympy-23description: SymPy is a Python library for symbolic mathematics that enables exact computation using mathematical symbols rather than numerical approximations.4license: https://github.com/sympy/sympy/blob/master/LICENSE5---67# SymPy - Symbolic Mathematics in Python89## Detailed Guide1011Read [the detailed guide](references/detailed-guide.md) before executing this skill. It retains the complete procedure and reference material. Treat its safety, prerequisites, and validation requirements as mandatory. For focused work, load the relevant sections; for end-to-end work, read the guide completely.1213## When to Use This Skill1415Use this skill when:16- Solving equations symbolically (algebraic, differential, systems of equations)17- Performing calculus operations (derivatives, integrals, limits, series)18- Manipulating and simplifying algebraic expressions19- Working with matrices and linear algebra symbolically20- Doing physics calculations (mechanics, quantum mechanics, vector analysis)21- Number theory computations (primes, factorization, modular arithmetic)22- Geometric calculations (2D/3D geometry, analytic geometry)23- Converting mathematical expressions to executable code (Python, C, Fortran)24- Generating LaTeX or other formatted mathematical output25- Needing exact mathematical results (e.g., `sqrt(2)` not `1.414...`)2627## Getting Started Examples2829### Example 1: Solve Quadratic Equation30```python31from sympy import symbols, solve, sqrt32x = symbols('x')33solution = solve(x**2 - 5*x + 6, x)34# [2, 3]35```3637### Example 2: Calculate Derivative38```python39from sympy import symbols, diff, sin40x = symbols('x')41f = sin(x**2)42df_dx = diff(f, x)43# 2*x*cos(x**2)44```4546### Example 3: Evaluate Integral47```python48from sympy import symbols, integrate, exp49x = symbols('x')50integral = integrate(x * exp(-x**2), (x, 0, oo))51# 1/252```5354### Example 4: Matrix Eigenvalues55```python56from sympy import Matrix57M = Matrix([[1, 2], [2, 1]])58eigenvals = M.eigenvals()59# {3: 1, -1: 1}60```6162### Example 5: Generate Python Function63```python64from sympy import symbols, lambdify65import numpy as np66x = symbols('x')67expr = x**2 + 2*x + 168f = lambdify(x, expr, 'numpy')69f(np.array([1, 2, 3]))70# array([ 4, 9, 16])71```7273## Limitations74- Use this skill only when the task clearly matches the scope described above.75- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.76- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.