sympy
它能做什么
这个工具让你用符号进行精确的数学计算,而不是使用近似数字。它可以用于代数、微积分、解方程,甚至物理相关的计算。
适用场景
- ✓求解像“x + 5 = 10”这样的方程的精确解。
- ✓计算数学函数的导数或积分。
- ✓化简复杂的数学表达式。
技能链接
https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/sympy/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“求解方程 x^2 - 4 = 0 中的 x。”
每周 5 个新技能,1 分钟读完。
工作原理
你先告诉 AI 每个符号代表什么(比如“x”或“y”),然后写出数学表达式或方程。AI 会调用 SymPy 工具进行计算,给出精确结果。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
SymPy - 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
Reviewed against current official documentation and executed with SymPy 1.14.0
on Python 3.13.3 (2026-10-01). Core SymPy requires Python 3.9+; the tested
NumPy 2.5.3 / SciPy 1.18.1 stack needs Python 3.12+. SymPy 1.14.0 requires
mpmath>=1.1,<1.4; use the compatible 1.3.0, not the newer 1.4.x release.
See verification and official sources for coverage.
# Install SymPy using uv
uv pip install "sympy==1.14.0"
# 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
from sympy import symbols, sqrt
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
# Approximate (appropriate for measured/numerical inputs):
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) # Request 50 decimal digits; cannot recover precision lost in inputs
5. Convert to NumPy for Performance
from sympy import symbols, lambdify
import numpy as np
x = symbols("x")
expr = x**2 + 1
# 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; supports some problemssolvesetdoes not
…
常见问题
这个工具和普通计算器有什么区别?
普通计算器使用近似数字(比如 3.14159)。这个工具使用精确的符号(比如 π 或 2 的平方根),因此能给出精确的答案。
我可以同时使用数字和符号吗?
可以,数字和符号可以混合使用。比如你可以让它化简“2*x + 3*x”。
它能解决所有类型的数学问题吗?
它在代数、微积分以及部分物理领域非常强大,但对于极其复杂或非常专业的数学问题,可能无法求解。
详情
作者的原始描述
Performs exact symbolic mathematics with SymPy for algebra, calculus, equation solving, symbolic linear algebra, physics, and lambdify or LaTeX code generation. Use when a task needs symbolic results, explicit assumptions, or exact arithmetic; use NumPy or SciPy for purely numerical workloads.
热度
- GitHub 星标
- 51k
- 排名
- #3579
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …