What SymPy does
SymPy is a powerful Python library designed for symbolic mathematics, allowing users to perform exact computations using mathematical symbols instead of numerical approximations. This skill provides comprehensive resources for tackling a wide range of mathematical tasks, including symbolic algebra, calculus, linear algebra, equation solving, and even physics calculations. With SymPy, you can manipulate expressions, solve equations symbolically, and generate LaTeX or executable code, making it an essential tool for developers and researchers who require precision in their computations.
The skill is particularly useful for tasks such as solving algebraic and differential equations, performing derivatives and integrals, and handling complex matrix operations. It also supports number theory and geometric calculations, which are beneficial for advanced mathematical applications. The ability to convert mathematical expressions into code using lambdify allows for seamless integration into larger projects, enhancing productivity and efficiency.
SymPy is best utilized when exact results are necessary, such as when working with irrational numbers or when the precision of floating-point arithmetic is insufficient. By leveraging its core capabilities, users can achieve accurate results in various scientific and engineering domains. The skill's modular reference files cover specific areas of mathematics, allowing users to access targeted information as needed, whether they are dealing with basic algebra or advanced calculus.
For those who frequently engage in mathematical modeling or scientific research, this skill provides the tools to document results in LaTeX format, facilitating clear communication of findings. Overall, SymPy is a versatile and essential skill for anyone involved in mathematical computations in Python.
When to use it
Use this skill when you need to perform symbolic computations, such as solving equations, performing calculus, or generating formatted mathematical output.
When not to use it
Avoid this skill if you primarily need floating-point approximations, as libraries like NumPy or SciPy are better suited for those tasks.
What you can build with it
Solving a Quadratic Equation
Use SymPy to symbolically solve a quadratic equation, obtaining exact roots instead of numerical approximations.
Calculating Derivatives
Utilize SymPy to compute derivatives of functions symbolically, enabling precise manipulation of mathematical expressions.
Generating Code from Expressions
Convert symbolic expressions into executable Python code using SymPy's lambdify function for efficient numerical evaluation.
How to install SymPy
View source1. Install with the skills CLI
npx skills add k-dense-ai/scientific-agent-skills/sympy --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by k-dense-aiSymPy - Symbolic Mathematics in Python
Overview
SymPy is a Python library for symbolic mathematics that enables exact computation using mathematical symbols rather than numerical approximations. This skill provides comprehensive guidance for performing symbolic algebra, calculus, linear algebra, equation solving, physics calculations, and code generation using SymPy.
Installation
Tested against SymPy 1.14.0 (stable; April 2025). Requires Python 3.9+.
# Install SymPy using uv
uv pip install "sympy>=1.14"
# Optional: for lambdify and plotting examples
uv pip install numpy scipy matplotlib
Check your version:
import sympy
print(sympy.__version__)
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)not1.414...)
Core Capabilities
Seven capability areas are documented in references/core_capabilities.md:
- Symbolic computation basics — symbols, expressions, simplification, substitution.
- Calculus — differentiation, integration, limits, series.
- Equation solving —
solve,solveset, linear and nonlinear systems, ODEs. - Matrices and linear algebra — see references/matrices-linear-algebra.md.
- Physics and mechanics — see references/physics-mechanics.md.
- Advanced mathematics — see references/advanced-topics.md.
- Code generation and output — see references/code-generation-printing.md.
Deeper treatment of the first three is in references/core-capabilities.md.
Working with SymPy: Best Practices
1. Always Define Symbols First
from sympy import symbols
x, y, z = symbols('x y z')
# Now x, y, z can be used in expressions
2. Use Assumptions for Better Simplification
x = symbols('x', positive=True, real=True)
sqrt(x**2) # Returns x (not Abs(x)) due to positive assumption
Common assumptions: real, positive, negative, integer, rational, complex, even, odd
3. Use Exact Arithmetic
from sympy import Rational, S
# Correct (exact):
expr = Rational(1, 2) * x
expr = S(1)/2 * x
# Incorrect (floating-point):
expr = 0.5 * x # Creates approximate value
4. Numerical Evaluation When Needed
from sympy import pi, sqrt
result = sqrt(8) + pi
result.evalf() # 5.96371554103586
result.evalf(50) # 50 digits of precision
5. Convert to NumPy for Performance
# Slow for many evaluations:
for x_val in range(1000):
result = expr.subs(x, x_val).evalf()
# Fast:
f = lambdify(x, expr, 'numpy')
results = f(np.arange(1000))
6. Use Appropriate Solvers
solveset: Algebraic equations (primary)linsolve: Linear systemsnonlinsolve: Nonlinear systemsdsolve: Differential equationssolve: General purpose (legacy, but flexible)
Reference Files Structure
This skill uses modular reference files for different capabilities:
-
core-capabilities.md: Symbols, algebra, calculus, simplification, equation solving- Load when: Basic symbolic computation, calculus, or solving equations
-
matrices-linear-algebra.md: Matrix operations, eigenvalues, linear systems- Load when: Working with matrices or linear algebra problems
-
physics-mechanics.md: Classical mechanics, quantum mechanics, vectors, units- Load when: Physics calculations or mechanics problems
-
advanced-topics.md: Geometry, number theory, combinatorics, logic, statistics- Load when: Advanced mathematical topics beyond basic algebra and calculus
-
code-generation-printing.md: Lambdify, codegen, LaTeX output, printing- Load when: Converting expressions to code or generating formatted output
Common Use Case Patterns
Pattern 1: Solve and Verify
from sympy import symbols, solve, simplify
x = symbols('x')
# Solve equation
equation = x**2 - 5*x + 6
solutions = solve(equation, x) # [2, 3]
# Verify solutions
for sol in solutions:
result = simplify(equation.subs(x, sol))
assert result == 0
Pattern 2: Symbolic to Numeric Pipeline
# 1. Define symbolic problem
x, y = symbols('x y')
expr = sin(x) + cos(y)
# 2. Manipulate symbolically
simplified = simplify(expr)
derivative = diff(simplified, x)
# 3. Convert to numerical function
f = lambdify((x, y), derivative, 'numpy')
# 4. Evaluate numerically
results = f(x_data, y_data)
Pattern 3: Document Mathematical Results
# Compute result symbolically
integral_expr = Integral(x**2, (x, 0, 1))
result = integral_expr.doit()
# Generate documentation
print(f"LaTeX: {latex(integral_expr)} = {latex(result)}")
print(f"Pretty: {pretty(integral_expr)} = {pretty(result)}")
print(f"Numerical: {result.evalf()}")
Integration with Scientific Workflows
With NumPy
import numpy as np
from sympy import symbols, lambdify
x = symbols('x')
expr = x**2 + 2*x + 1
f = lambdify(x, expr, 'numpy')
x_array = np.linspace(-5, 5, 100)
y_array = f(x_array)
With Matplotlib
import matplotlib.pyplot as plt
import numpy as np
from sympy import symbols, lambdify, sin
x = symbols('x')
expr = sin(x) / x
f = lambdify(x, expr, 'numpy')
x_vals = np.linspace(-10, 10, 1000)
y_vals = f(x_vals)
plt.plot(x_vals, y_vals)
plt.show()
With SciPy
from scipy.optimize import fsolve
from sympy import symbols, lambdify
# Define equation symbolically
x = symbols('x')
equation = x**3 - 2*x - 5
# Convert to numerical function
f = lambdify(x, equation, 'numpy')
# Solve numerically with initial guess
solution = fsolve(f, 2)
Quick Reference: Most Common Functions
# Symbols
from sympy import symbols, Symbol
x, y = symbols('x y')
# Basic operations
from sympy import simplify, expand, factor, collect, cancel
from sympy import sqrt, exp, log, sin, cos, tan, pi, E, I, oo
# Calculus
from sympy import diff, integrate, limit, series, Derivative, Integral
# Solving
from sympy import solve, solveset, linsolve, nonlinsolve, dsolve
# Matrices
from sympy import Matrix, eye, zeros, ones, diag
# Logic and sets
from sympy import And, Or, Not, Implies, FiniteSet, Interval, Union
# Output
from sympy import latex, pprint, lambdify, init_printing
# Utilities
from sympy import evalf, N, nsimplify
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])
Troubleshooting Common Issues
-
"NameError: name 'x' is not defined"
- Solution: Always define symbols using
symbols()before use
- Solution: Always define symbols using
-
Unexpected numerical results
- Issue: Using floating-point numbers like
0.5instead ofRational(1, 2) - Solution: Use
Rational()orS()for exact arithmetic
- Issue: Using floating-point numbers like
-
Slow performance in loops
- Issue: Using
subs()andevalf()repeatedly - Solution: Use
lambdify()to create a fast numerical function
- Issue: Using
-
"Can't solve this equation"
- Try different solvers:
solve,solveset,nsolve(numerical) - Check if the equation is solvable algebraically
- Use numerical methods if no closed-form solution exists
- Try different solvers:
-
Simplification not working as expected
- Try different simplification functions:
simplify,factor,expand,trigsimp - Add assumptions to symbols (e.g.,
positive=True) - Use
simplify(expr, force=True)for aggressive simplification
- Try different simplification functions:
Additional Resources
- Official Documentation: https://docs.sympy.org/
- Tutorial: https://docs.sympy.org/latest/tutorials/intro-tutorial/index.html
- API Reference: https://docs.sympy.org/latest/reference/index.html
- Examples: https://github.com/sympy/sympy/tree/master/examples
Frequently asked questions about SymPy
Similar skills
Rhino 3D Scripting
Streamline your Rhinoceros 3D scripting tasks.
MVVM Toolkit
Streamline ViewModel development with source generators.
FreeCAD Scripts
Generate Python scripts for FreeCAD automation and modeling.
Azure Architecture Builder
Design and deploy Azure infrastructure using natural language.
Command Development
Streamline your command creation for Claude Code.
Create Cowork Plugin
Easily build and package plugins through guided sessions.

