用于符号数学的 Python 库——sympy(一)

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

a 2 b + a b + 3 a + 4 b \displaystyle a^{2} b + a b + 3 a + 4 b a2b+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 0 0

线性代数

矩阵运算

A = ( a 11 a 12 a 21 a 22 a 31 a 32 ) A=\begin{pmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \\ a_{31} & a_{32} \end{pmatrix} A= a11a21a31a12a22a32 , B = ( b 1 0 0 b 2 ) B=\begin{pmatrix} b_1 & 0 \\ 0 & b_2 \end{pmatrix} B=(b100b2), C = ( c 1 0 0 0 c 2 0 0 0 c 3 ) C=\begin{pmatrix} c_1 & 0 & 0 \\ 0 & c_2 & 0 \\ 0 & 0 & c_3 \end{pmatrix} C= c1000c2000c3 ,求 A B AB AB C A CA CA.

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

[ a 11 b 1 a 12 b 2 a 21 b 1 a 22 b 2 a 21 b 1 a 32 b 2 ] \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

解线性方程组

用消元法解下列方程组:

{ x 1 − 2 x 2 + 3 x 3 − x 4 + 2 x 5 = 2 3 x 1 − x 2 + 5 x 3 − 3 x 4 + x 5 = 6 2 x 1 + x 2 + 2 x 3 − 2 x 4 − x 5 = 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
基础解:

{ ( x 4 ,   x 5 ,   0 ,   x 4 ,   x 5 ) } \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)]


其它

欧拉公式

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

from sympy import *

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

i e − 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)

i sin ⁡ ( 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 + i x − x 2 2 − i x 3 6 + x 4 24 + i x 5 120 − x 6 720 − i x 7 5040 + x 8 40320 + i x 9 362880 + O ( x 10 ) \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)

分数计算

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

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

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

官方例子

微分

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

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

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

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

计算 ∂ 7 ∂ x ∂ y 2 ∂ z 4 e x y z \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)

x 3 y 2 ( x 3 y 3 z 3 + 14 x 2 y 2 z 2 + 52 x y z + 48 ) e x y z \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 − x d x \int^\infty_0e^{-x}dx 0exdx

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

1 \displaystyle 1 1

二重积分
∫ − ∞ ∞ ∫ − ∞ ∞ e − x 2 − y 2 d x d y \int^\infty_{-\infty}\int^\infty_{-\infty}e^{-x^2-y^2}dxdy ex2y2dxdy

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

π \displaystyle \pi π

极限

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

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

1 \displaystyle 1 1

有限差分

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

f ( − 3 h ) 5 h 2 − f ( − h ) 3 h 2 + 2 f ( 2 h ) 15 h 2 \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)

你可能感兴趣的:(科研,python,开发语言)