sympy基础知识
作者:互联网
sympy基础知识
1.sympy
import sympy
print(sympy.pi.evalf())#圆周率π
print(sympy.E)#e
print(sympy.I)#虚数i
print(sympy.oo)#无穷大
print(sympy.E.evalf(3))#保留e的三位小数
2.三角函数
import sympy
print(sympy.sin(sympy.pi/4).evalf())
print(sympy.cos(sympy.pi/3).evalf())
print(sympy.tan(sympy.pi/2).evalf())
3.指数函数
import sympy
x=sympy.Symbol('x')
print(sympy.E ** x)#e**x
print(sympy.exp(x))#e**x
print(sympy.exp(x).evalf(subs={x:2}))#求e**2的值
print(10 ** x.evalf(subs={x:2}))
4.对数函数
import sympy
print(sympy.log(100, 10))#以10为底100的对数
print(sympy.log(sympy.E**3,sympy.E))#以e为底e**3的对数
print(sympy.log(8, 2))#以2为底8的对数,即log2(8)
5.幂函数
import sympy
print(sympy.sqrt(16))#平方根
print(sympy.root(16,4))#4次方根
print(sympy.factorial(3))#求阶乘
a=x**(1/4)
print(a.evalf(subs={x: 16}))
b=x**2
print(b.evalf(subs={x: 4}))
6.展开与折叠
import sympy
y1=x**2+2*x+1
y2=x*(x+2)
print(sympy.expand(y2))#展开
print(sympy.factor(y1))#提取公因式(折叠)
y3=1/x+1/x**2
print(sympy.apart(y3))#展开,用于分数
print(sympy.together(y3))#聚合,用于分数
7.化简
import sympy
print(sympy.simplify((x ** 3 + x ** 2 - x - 1) / (x ** 2 + 2 * x + 1)))#化简
print(sympy.trigsimp(sympy.sin(x) / sympy.cos(x)))#三角函数化简
print(sympy.powsimp(x ** 2 * x ** 4))#指数化简
8.求解
import sympy
x=sympy.Symbol('x')
y=sympy.Symbol('y')
f=x**2+6*x+9
print(sympy.solve(f,x))#有限解
f1=3*x+y**2
print(sympy.solve(f1,x,y))#无限解
f2=x+y-6
f3=2*x-y+5
print(sympy.solve([f2, f3], x, y))#解方程组
#求和
f=2*x
print(sympy.summation(f, (x, 1, 100)))
9.求极限
import sympy
fb=sympy.sin(x)/x
print(sympy.limit(fb, x, 0))#极限x趋于0时的值
print(sympy.limit(fb, x, sympy.oo))#极限x趋于无穷时的值
10.求导
import sympy
fa=3*sympy.exp(x)+x**3+2*x+17
print(sympy.diff(fa,x))#一阶求导
print(sympy.diff(fa, x, 2))#二阶求导
print(sympy.diff(fa, x, 3))#三阶求导
11.求偏导
import sympy
x,y=sympy.Symbol('x'),sympy.Symbol('y')
x,y=sympy.symbols('x y')
fc=2*x**2+3*y**4+2*y*x
print(sympy.diff(fc, x))#对x求一阶偏导
print(sympy.diff(fc, y))#对y求一阶偏导
print(sympy.diff(fc, y,y))#对y求二阶偏导
print(sympy.diff(fc, x,y))#先对x求一阶偏导,在对y求一阶偏导
print(sympy.diff(fc, x,x))#对x求二阶偏导
12.求积分
import sympy
fd=sympy.E**x+3*x**4+x**2
print(sympy.integrate(fd, x))#求不定积分
print(sympy.integrate(fd, (x,1,2)))#求定积分
t=sympy.Symbol('t')
ff=2*t
ff1=sympy.integrate(ff,(t,0,x))
ff2=sympy.integrate(ff1,(x,1,3))#求多重积分
print(ff)
print(ff1)
print(ff2)
z=y*x**2+x*y**2
z1=sympy.integrate(z,(x,0,x))
z2=sympy.integrate(z1,(y,0,1))#求多重积分
print(z2)
标签:基础知识,偏导,evalf,import,print,sympy,diff 来源: https://blog.csdn.net/m0_47405013/article/details/120585743