Python 是一种广泛使用的解释型、高级和通用的编程语言。除了 numpy 提供科学计算外,还有 sympy 用于数学符号计算。

安装

sympy 的安装非常简单,使用 pip install sympy 即可完成。更详细的安装教程请参考 sympy Installation 官网

下面直接进入使用介绍

使用

命令行

如果您已经安装了 sympy,那么在命令行就可以直接使用,就行使用 python 或 ipython 一样,方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ isympy
No event loop hook running.
IPython console for SymPy 1.10.1 (Python 3.9.17-64-bit) (ground types: python)

These commands were executed:
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at https://docs.sympy.org/1.10.1/


In [1]:

jupyter

建议使用 jupyterlab 进行更方便的交互式使用 sympy,同时可以撰写 markdown 说明文档。

1
import sympy

计算数值

sympy 作为一种数学符合计算工具,它同时也能够进行科学计算。

1
2
pi = sympy.pi
pi

$\displaystyle \pi$

1
2
# 显示为实数
pi.evalf(n=100)

$\displaystyle 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$

1
2
3
# 无穷的表示
infty = sympy.oo
infty

$\displaystyle \infty$

1
infty > 9999999

$\displaystyle \text{True}$

1
infty + 1

$\displaystyle \infty$

1
2
rational_a = sympy.Rational(1, 2)
rational_a

$\displaystyle \frac{1}{2}$

1
2
rational_b = sympy.Rational(1, 3)
rational_b

$\displaystyle \frac{1}{3}$

1
rational_a + rational_b

$\displaystyle \frac{5}{6}$

符号表示

1
2
x = sympy.Symbol("x")
x

$\displaystyle x$

1
2
x, y = sympy.symbols("x y")
x

$\displaystyle x$

1
y

$\displaystyle y$

1
2
# abc 模块下记录有所有的 拉丁字母和希腊字母
from sympy import abc
1
2
x = abc.x
x

$\displaystyle x$

1
2
3
# 注意 Python 关键字冲突
lambda_var = abc.lamda
lambda_var

$\displaystyle \lambda$

1
sympy.I

$\displaystyle i$

1
2
xi = sympy.symbols("xi", real=True)
xi

$\displaystyle \xi$

1
sympy.expand_complex(sympy.exp(sympy.I * xi))

$\displaystyle i \sin{\left(\xi \right)} + \cos{\left(\xi \right)}$

1
x + y * 2 + x - y * 2

$\displaystyle 2 x$

输出格式

sympy 允许很多种形式的输出,可以通过如下方式设置

1
sympy.init_printing(use_unicode=False, wrap_line=True)

该函数有很多自定义参数,可以使用 sympy.init_printing? 在 jupyter 中查看。

1
2
# latex
sympy.print_latex(1 / x + 1 / y)
\frac{1}{y} + \frac{1}{x}
1
sympy.latex(1 / x)
'\\frac{1}{x}'
1
2
# dotprint
sympy.printing.dot.dotprint(1 / x)
'digraph{\n\n# Graph style\n"ordering"="out"\n"rankdir"="TD"\n\n#########\n# Nodes #\n#########\n\n"Pow(Symbol(\'x\'), Integer(-1))_()" ["color"="black", "label"="Pow", "shape"="ellipse"];\n"Symbol(\'x\')_(0,)" ["color"="black", "label"="x", "shape"="ellipse"];\n"Integer(-1)_(1,)" ["color"="black", "label"="-1", "shape"="ellipse"];\n\n#########\n# Edges #\n#########\n\n"Pow(Symbol(\'x\'), Integer(-1))_()" -> "Symbol(\'x\')_(0,)";\n"Pow(Symbol(\'x\'), Integer(-1))_()" -> "Integer(-1)_(1,)";\n}'
1
2
# mathml
sympy.print_mathml(1 / x)
<apply>
    <power/>
    <ci>x</ci>
    <cn>-1</cn>
</apply>
1
sympy.print_python(1 / x)
x = Symbol('x')
e = 1/x
1
sympy.print_ccode(1 / x)
1.0/x
1
sympy.print_rcode(1 / x)
1.0/x

更多打印相关的知识请参考:sympy文档 print

代数

展开

1
sympy.expand((x + y) ** 5)

$\displaystyle x^{5} + 5 x^{4} y + 10 x^{3} y^{2} + 10 x^{2} y^{3} + 5 x y^{4} + y^{5}$

1
sympy.expand(sympy.cos(x + y), trig=True)

$\displaystyle - \sin{\left(x \right)} \sin{\left(y \right)} + \cos{\left(x \right)} \cos{\left(y \right)}$

1
sympy.expand(sympy.cos(x - y))

$\displaystyle \cos{\left(x - y \right)}$

1
sympy.expand(x + y, complex=True)

$\displaystyle \operatorname{re}{\left(x\right)} + \operatorname{re}{\left(y\right)} + i \operatorname{im}{\left(x\right)} + i \operatorname{im}{\left(y\right)}$

化简

1
sympy.simplify((x**2 + x * y + 3) / x)

$\displaystyle x + y + \frac{3}{x}$

1
sympy.simplify((x + y) / (x - y) + (x - y) / (x + y))

$\displaystyle \frac{\left(x - y\right)^{2} + \left(x + y\right)^{2}}{\left(x - y\right) \left(x + y\right)}$

矩阵

1
sympy.Matrix([[1, 2], [1, 2]])

$\displaystyle \left[\begin{matrix}1 & 2\\ 1 & 2\end{matrix}\right]$

1
sympy.Matrix([[1, 2], [1, 2]]).eigenvals()

$\displaystyle { 0 : 1, \ 3 : 1}$

1
sympy.Matrix([[1, x], [y, 1]])

$\displaystyle \left[\begin{matrix}1 & x\\ y & 1\end{matrix}\right]$

微分方程

1
f, g = sympy.symbols("f g", cls=sympy.Function)
1
f(x).diff(x, x) + f(x)

$\displaystyle f{\left(x \right)} + \frac{d^{2}}{d x^{2}} f{\left(x \right)}$

1
sympy.dsolve(f(x).diff(x, x) + f(x), f(x))

$\displaystyle f{\left(x \right)} = C_{1} \sin{\left(x \right)} + C_{2} \cos{\left(x \right)}$

1
2
3
4
5
sympy.dsolve(
sympy.sin(x) * sympy.cos(f(x)) + sympy.cos(x) * sympy.sin(f(x)) * f(x).diff(x),
f(x),
hint="separable",
)

$\displaystyle \left[ f{\left(x \right)} = - \operatorname{acos}{\left(\frac{C_{1}}{\cos{\left(x \right)}} \right)} + 2 \pi, \ f{\left(x \right)} = \operatorname{acos}{\left(\frac{C_{1}}{\cos{\left(x \right)}} \right)}\right]$

微积分

极限

1
sympy.limit(sympy.sin(x) / x, x, 0, dir="+-")

$\displaystyle 1$

1
sympy.limit(1 / x**2, x, sympy.oo)

$\displaystyle 0$

1
2
func = sympy.Limit(sympy.sin(x) / x, x, 0, dir="+-")
func

$\displaystyle \lim_{x \to 0}\left(\frac{\sin{\left(x \right)}}{x}\right)$

1
sympy.Eq(func, func.doit())

$\displaystyle \lim_{x \to 0}\left(\frac{\sin{\left(x \right)}}{x}\right) = 1$

求导

1
sympy.diff(sympy.sin(x), x)

$\displaystyle \cos{\left(x \right)}$

1
sympy.diff(sympy.exp(1 + x**2), x)

$\displaystyle 2 x e^{x^{2} + 1}$

1
sympy.Eq(sympy.exp(1 + x**2), sympy.diff(sympy.exp(1 + x**2), x))

$\displaystyle e^{x^{2} + 1} = 2 x e^{x^{2} + 1}$

级数

1
sympy.series(sympy.sin(x), x)

$\displaystyle x - \frac{x^{3}}{6} + \frac{x^{5}}{120} + O\left(x^{6}\right)$

1
sympy.series(sympy.ln(1 + x), x)

$\displaystyle x - \frac{x^{2}}{2} + \frac{x^{3}}{3} - \frac{x^{4}}{4} + \frac{x^{5}}{5} + O\left(x^{6}\right)$

积分

1
sympy.integrate(3 * x**2, x)

$\displaystyle x^{3}$

1
sympy.integrate(sympy.sin(x), x)

$\displaystyle - \cos{\left(x \right)}$

1
sympy.integrate(sympy.exp(-(x**2)) * sympy.erf(x), x)

$\displaystyle \frac{\sqrt{\pi} \operatorname{erf}^{2}{\left(x \right)}}{4}$

1
sympy.integrate(sympy.cos(x), (x, 0, sympy.pi / 2))

$\displaystyle 1$

1
sympy.integrate(sympy.exp(-(x**2)), (x, -sympy.oo, sympy.oo))

$\displaystyle \sqrt{\pi}$

1
2
func = sympy.Integral(sympy.exp(-(x**2)), (x, -sympy.oo, sympy.oo))
func

$\displaystyle \int\limits_{-\infty}^{\infty} e^{- x^{2}} dx$

1
sympy.Eq(func, func.doit())

$\displaystyle \int\limits_{-\infty}^{\infty} e^{- x^{2}} dx = \sqrt{\pi}$

解方程

1
sympy.solveset(x**3 - 1, x)

$\displaystyle {1, - \frac{1}{2} - \frac{\sqrt{3} i}{2}, - \frac{1}{2} + \frac{\sqrt{3} i}{2} }$

1
sympy.solve((x + 2 * y - 2, -3 * x + 3 * y - 5), (x, y))

$\displaystyle { x : - \frac{4}{9}, \ y : \frac{11}{9} }$

因式分解

1
2
func = x**4 - 3 * x**2 + 1
func

$\displaystyle x^{4} - 3 x^{2} + 1$

1
sympy.factor(func)

$\displaystyle \left(x^{2} - x - 1\right) \left(x^{2} + x - 1\right)$

1
sympy.Eq(func, sympy.factor(func))

$\displaystyle x^{4} - 3 x^{2} + 1 = \left(x^{2} - x - 1\right) \left(x^{2} + x - 1\right)$

1
sympy.factor(func, modulus=5)

$\displaystyle \left(x - 2\right)^{2} \left(x + 2\right)^{2}$

参考文献

  1. sympy 文档 print
  2. Sympy : Symbolic Mathematics in Python