SymPy 是用于符号数学的 Python 库。 它旨在成为功能齐全的计算机代数系统。 SymPy 包括从基本符号算术到微积分,代数,离散数学和量子物理学的功能。 它可以在 LaTeX 中显示结果。

打印普通公式

from sympy import Symbol
x = Symbol('x')

a = sqrt(2)
a

2\displaystyle \sqrt{2}2

from sympy.abc import a, b

expr = b*a*a + -4*a + b + a*b + 4*a + (a + b)*3
expr

a2b+ab+3a+4b\displaystyle a^{2} b + a b + 3 a + 4 ba2b+ab+3a+4b

计算

公式化简

from sympy import sin, cos, simplify
from sympy.abc import x

expr = sin(x) / cos(x)
simplify(expr)

tan⁡(x)\displaystyle \tan{\left(x \right)}tan(x)

解普通方程

from sympy import Symbol, solve

x = Symbol('x')
sol = solve(x**2 - x, x)
sol

[0, 1]\displaystyle \left[ 0, \ 1\right][0, 1]

求极限

from sympy import sin, limit, oo
from sympy.abc import x

l1 = limit(1/x, x, oo)
l1

0\displaystyle 00

线性代数

矩阵运算

A=(a11a12a21a22a31a32)A=\begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \\ a_{31} & a_{32} \end{pmatrix}A= a11a21a31a12a22a32 ,B=(b100b2)B=\begin{pmatrix} b_1 & 0 \\ 0 & b_2 \end{pmatrix}B=(b100b2),C=(c1000c2000c3)C=\begin{pmatrix} c_1 & 0 & 0 \\ 0 & c_2 & 0 \\ 0 & 0 & c_3 \end{pmatrix}C= c1000c2000c3 ,求ABABABCACACA.

import numpy as np
import sympy as sp
A = np.array([['a11','a12'],['a21','a22'],['a21','a32']])
B = np.array([['b1',0],[0,'b2']])
C = np.array([['c1',0,0],[0,'c2',0],[0,0,'c3']])

A=sp.Matrix(A)
B=sp.Matrix(B)
C=sp.Matrix(C)
A*B

[a11b1a12b2a21b1a22b2a21b1a32b2]\displaystyle \left[\begin{matrix}a_{11} b_{1} & a_{12} b_{2}\\a_{21} b_{1} & a_{22} b_{2}\\a_{21} b_{1} & a_{32} b_{2}\end{matrix}\right] a11b1a21b1a21b1a12b2a22b2a32b2

解线性方程组

用消元法解下列方程组:

{x1−2x2+3x3−x4+2x5=23x1−x2+5x3−3x4+x5=62x1+x2+2x3−2x4−x5=8 \left \{ \begin{array}{c} x_1-2x_2+3x_3-x_4+2x_5=2 \\ 3x_1-x_2+5x_3-3x_4+x_5=6 \\ 2x_1+x_2+2x_3-2x_4-x_5=8 \end{array} \right. x12x2+3x3x4+2x5=23x1x2+5x33x4+x5=62x1+x2+2x32x4x5=8

import numpy as np
import sympy as sp

x1,x2,x3,x4,x5 = sp.symbols("x1 x2 x3 x4 x5")
A = sp.Matrix([[1,-2,3,-1,2],[3,-1,4,-3,1],[2,1,2,-2,-1]])
b = sp.Matrix([[2],[6],[8]])
b0 = sp.Matrix([[0],[0],[0]])

system1 = A,b
system0 = A,b0

result0 = sp.linsolve(system0,x1,x2,x3,x4,x5)
result = sp.linsolve(system1,x1,x2,x3,x4,x5)
print('特解:')
print(result)


特解:
{(x4 - 2, x5 + 4, 4, x4, x5)}
print('基础解:')
result0
基础解:

{(x4, x5, 0, x4, x5)}\displaystyle \left\{\left( x_{4}, \ x_{5}, \ 0, \ x_{4}, \ x_{5}\right)\right\}{(x4, x5, 0, x4, x5)}

绘图

import sympy
from sympy.abc import x
from sympy.plotting import plot

plot(x*x)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DSwn1lj0-1667988483063)(output_17_0.png)]

<sympy.plotting.plot.Plot at 0x7fb1cc6d9f30>

其它

欧拉公式

eix=cos⁡x+isin⁡xe^{ix}=\cos{x}+i\sin{x}eix=cosx+isinx

from sympy import *

# 若x为复数
expand(exp(I*x), complex=True)

ie−im⁡(x)sin⁡(re⁡(x))+e−im⁡(x)cos⁡(re⁡(x))\displaystyle i e^{- \operatorname{im}{\left(x\right)}} \sin{\left(\operatorname{re}{\left(x\right)} \right)} + e^{- \operatorname{im}{\left(x\right)}} \cos{\left(\operatorname{re}{\left(x\right)} \right)}ieim(x)sin(re(x))+eim(x)cos(re(x))

# 若x为实数
x = Symbol("x", real=True)
expand(exp(I*x), complex=True)

isin⁡(x)+cos⁡(x)\displaystyle i \sin{\left(x \right)} + \cos{\left(x \right)}isin(x)+cos(x)

# 泰勒公式展开
series(exp(I*x), x, 0, 10)

1+ix−x22−ix36+x424+ix5120−x6720−ix75040+x840320+ix9362880+O(x10)\displaystyle 1 + i x - \frac{x^{2}}{2} - \frac{i x^{3}}{6} + \frac{x^{4}}{24} + \frac{i x^{5}}{120} - \frac{x^{6}}{720} - \frac{i x^{7}}{5040} + \frac{x^{8}}{40320} + \frac{i x^{9}}{362880} + O\left(x^{10}\right)1+ix2x26ix3+24x4+120ix5720x65040ix7+40320x8+362880ix9+O(x10)

分数计算

12+13\frac{1}{2}+\frac{1}{3}21+31

S(1)/2 + 1/S(3)

56\displaystyle \frac{5}{6}65

官方例子

微分

求导sin⁡(x)ex\sin(x)e^xsin(x)ex

from sympy import *
x, t, z, nu = symbols('x t z nu')

diff(sin(x)*exp(x), x)

exsin⁡(x)+excos⁡(x)\displaystyle e^{x} \sin{\left(x \right)} + e^{x} \cos{\left(x \right)}exsin(x)+excos(x)

计算∂7∂x∂y2∂z4exyz\frac{\partial^7}{\partial x\partial y^2\partial z^4}e^{xyz}xy2z47exyz

x,y,z = symbols('x y z')
expr = exp(x*y*z)
diff(expr, x, y, y, z, z, z, z)

x3y2(x3y3z3+14x2y2z2+52xyz+48)exyz\displaystyle x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}x3y2(x3y3z3+14x2y2z2+52xyz+48)exyz

积分

cos⁡(x)\cos(x)cos(x)的积分

integrate(cos(x), x)

sin⁡(x)\displaystyle \sin{\left(x \right)}sin(x)

定积分
∫0∞e−xdx\int^\infty_0e^{-x}dx0exdx

integrate(exp(-x), (x, 0, oo))

1\displaystyle 11

二重积分
∫−∞∞∫−∞∞e−x2−y2dxdy\int^\infty_{-\infty}\int^\infty_{-\infty}e^{-x^2-y^2}dxdyex2y2dxdy

integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))

π\displaystyle \piπ

极限

lim⁡x→x0f(x)\lim_{x\to x_{0}}f(x)xx0limf(x)

limit(sin(x)/x,x,0)

1\displaystyle 11

有限差分

f = Function('f')
d2fdx2 = f(x).diff(x, 2)
h = Symbol('h')
d2fdx2.as_finite_difference([-3*h,-h,2*h])

f(−3h)5h2−f(−h)3h2+2f(2h)15h2\displaystyle \frac{f{\left(- 3 h \right)}}{5 h^{2}} - \frac{f{\left(- h \right)}}{3 h^{2}} + \frac{2 f{\left(2 h \right)}}{15 h^{2}}5h2f(3h)3h2f(h)+15h22f(2h)

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐