SymPy是一个符号计算的Python库。它的目标是成为一个全功能的计算机代数系统,同时保持代码简洁、易于理解和扩展。它完全由Python写成,不依赖于外部库。
以下使用导入并开启漂亮的打印
import sympy
sympy.init_printing(pretty_print=True)
有理数 Rationals
1 |
sympy.Rational(1, 3) |
out: $\frac{1}{3}$
特殊的无理数
1 |
sympy.pi, sympy.E, sympy.oo |
out:$\displaystyle \left( \pi, \ e, \ \infty\right)$
代数运算
声明一个代数符号
1 |
x = sympy.Symbol('x') # 注意,声明一个代数的时候 S 为大写 |
out: $𝑥$
声明多个代数符号
1 |
x, y = sympy.symbols('x y') |
out: $\displaystyle \left( x, \ y\right)$
声明带下标的代数符号
1 |
x1, x2 = sympy.symbols('x1 x2') |
out:$\displaystyle \left( x_{1}, \ x_{2}\right)$
简单的代数运算
1 |
x ** 2 + 2 * x + 1 |
out:$\displaystyle x^{2} + 2 x + 1$
1 |
x1 + x2 ** 2 - x2 + x1 /2 |
out:$\displaystyle \frac{3 x_{1}}{2} + x_{2}^{2} - x_{2}$
展开和分解
1 |
x, y = sympy.symbols('x y') |
展开多项式
1 |
sympy.expand((x + y) ** 2) |
out:$\displaystyle x^{2} + 2 x y + y^{2}$
展开三角函数
1 |
sympy.expand(sympy.sin(x + y), trig=True) |
out:$\displaystyle \sin{\left(x \right)} \cos{\left(y \right)} + \sin{\left(y \right)} \cos{\left(x \right)}$
化简
1 |
sympy.simplify((x + x*y) / x) |
out:$\displaystyle y + 1$
求和运算Sum
$$
\frac{1}{1^2+2\cdot 1} + \frac{1}{2^2+2\cdot 2} + \cdots + \frac{1}{10^2+2\cdot 10}
$$
1 |
expr = sympy.Sum(1/(x**2 + 2*x), (x, 1, 10)) |
expr:$\displaystyle \sum_{x=1}^{10} \frac{1}{x^{2} + 2 x}$ out: $\displaystyle \frac{175}{264}$
乘积运算 Product
$$
\frac{1}{1^2+2\cdot 1} \times \frac{1}{2^2+2\cdot 2} \times \cdots \times \frac{1}{10^2+2\cdot
10}
$$
1 |
expr = sympy.Product(1/(x**2 + 2*x), (x, 1, 10)) |
expr:$\displaystyle \prod_{x=1}^{10} \frac{1}{x^{2} + 2 x}$ out:$\displaystyle \frac{1}{869100503040000}$
极限计算
$$
\lim_{x \rightarrow 0}\frac{\sin x}{x}
$$
1 |
sympy.limit(sympy.sin(x)/x, x, 0) # 1 |
$$
\lim_{x \rightarrow \infty} x
$$
1 |
sympy.limit(x, x, sympy.oo) # ∞ |
$$
\lim_{x \rightarrow \infty} \frac{1}{x}
$$
1 |
sympy.limit(1/x, x, sympy.oo) # ∞ |
左极限和右极限
$$
\lim_{x \rightarrow 0^{-}} \frac{1}{x},\lim_{x \rightarrow 0^{+}} \frac{1}{x}
$$
1 |
sympy.limit(1/x, x, 0, dir='+') # ∞ |
导数
1 |
sympy.diff(x**2, x) |
out:$\displaystyle 2 x$
1 |
sympy.diff(sympy.sin(2*x), x) |
out:$\displaystyle 2 \cos{\left(2 x \right)}$
1 |
sympy.diff(sympy.sin(x**2+2*x), x) |
out:$\displaystyle \left(2 x + 2\right) \cos{\left(x^{2} + 2 x \right)}$
多阶导数
1 |
sympy.diff(x**2, x, 2) # 2(x**2对 x 求二阶导数) |
积分
不定积分
$$
\int_{ -\infty }^{ \infty } 6x^5
$$
1 |
sympy.integrate(6 * x**5, x) |
out:$x^6$
定积分
$$
\int_{0}^{\frac{\pi}{2}} \sin x
$$
1 |
sympy.integrate(sympy.sin(x), (x, 0, sympy.pi/2)) |
out:1
解方程
解一元方程
1 |
sympy.solve(x**2-3*x+2, x) |
out:$\displaystyle \left[ 1, \ 2\right]$
解二元方程
1 |
sympy.solve([x+5*y-2, -3*x+6*y-15], [x, y]) |
out:$\displaystyle \left{ x : -3, \ y : 1\right}$
代数运算
1 |
expr = x**2 + 2*x + 1 |
out:$\displaystyle x^{2} + 2 x + 1$
- 令 x = 2
1 |
expr.subs(x, 2) |
out:9
- 令 x = y + 1
1 |
expr.subs(x, y+1) |
out:$\displaystyle 2 y + \left(y + 1\right)^{2} + 3$
阶乘
1 |
n = sympy.Symbol('n') |
out:120
多元函数的代数
1 |
expr = x ** 3 + 4 * x * y - z |
out:$\displaystyle x^{3} + 4 x y - z$
1 |
expr.subs([(x, 2), (y, 4), (z, 0)]) |
out:40
字符串转 SymPy 表达式
1 |
str_expr = "x**2 + 3*x - 1/2" |
out:$\displaystyle x^{2} + 3 x - \frac{1}{2}$
概率论问题
导入框架import sympy.stats
骰子问题
- 创建一个有 6 个面的骰子
1 |
x = sympy.stats.Die('x', 6) |
- 查看每个面出现的概率
1 |
sympy.stats.density(x).dict |
out:$\displaystyle \left{ 1 : \frac{1}{6}, \ 2 : \frac{1}{6}, \ 3 : \frac{1}{6}, \ 4 : \frac{1}{6}, \ 5 : \frac{1}{6}, \ 6 : \frac{1}{6}\right}$
- 随机丢一个骰子
1 |
sympy.stats.sample(x) # out: 5 |
- 丢出骰子大于 3 的概率
1 |
sympy.stats.P(x > 3) |
硬币问题
- 创建一个硬币
1 |
c = sympy.stats.Coin('c') |
- 查看每个面出现的概率
1 |
sympy.stats.density(c).dict |
out:$\displaystyle \left{ H : \frac{1}{2}, \ T : \frac{1}{2}\right}$
与创建两个面的骰子类似
正态分布
- 创建一个标准正态分布
1 |
z = sympy.stats.Normal('z', 0, 1) |
- 标准正态分布中数据大于 1 的概率
1 |
sympy.stats.P(z > 1).evalf() |