Data library science sympy

Copyright Notice: Copyright: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_42658739/article/details/90417727

sympy is a Python library for scientific computing, such as polynomial evaluation is completed, find the limit with a powerful symbolic computation system, solving equations, quadrature, differential equations, series expansion, matrix operations and so on computational problems.
Python for its syntax is simple, approachable, rich eco-party libraries, personally think that a more elegant solution to everyday life, a variety of computational problems encountered in the work.
installation:

pip install sympy

Imaginary unit i

sympy.I
#i平方
sympy.I**2

Square root of -1

sympy.sqrt(-1

Natural logarithm of the end e:

print(sympy.log(sympy.E))

Infinity oo:

print(sympy.oo)

PI:

print(sympy.pi)

Determine n th root root (8,4) that is to finding the fourth root 8:

rst = sympy.root(8,4)
print(rst)

K seek power and factorial

print(2**3) #2的3次方
print(sympy.factorial(5)) #求5的阶乘

Seeking trigonometric functions

print(sympy.sin(sympy.pi/2))

Expression and expression evaluation
sympy set of symbols may be represented by an expression system, such as functions, polynomial, and can be evaluated.

x = sympy.Symbol('x')
fx = 2*x + 1
print(type(fx)) #查看fx的类型
f = fx.evalf(subs={x:6}) #传入一个x的值,求出fx
print(f)

Multivariate equations are solved:

x1,y = sympy.symbols('x1 y')
f1 = 2 * x1 + y
f1 = f1.evalf(subs = {x1:1,y:2}) #传入自变量值求出因变量值
print(f1)

Solving equation: solve ()
using the solution of the equation sympy.solve function, this function is typically two arguments,
the first parameter is a (formula all the items to the same side of the equal sign of equation is formed) expression equation ,
the second parameter is the unknowns in the equations. The return value is a list of all the roots of equation representative of (possibly complex roots)

Ordinary equation solution:
First define xa symbol, representing an unknown

x = sympy.Symbol('x')

Solving the equation: x - 6 = 0 returns a list consisting of a solution of

print(sympy.solve(x - 6,x)) 

Solution of equations

x,y = sympy.symbols('x y')
print(sympy.solve([x-y-8,x+y-2],[x,y]))
#第一个参数传入的方程组(也就是表达式),
#第二个参数传入的是方程组的变量,两个参数都是以列表形式传入

Summation : summation (a, b)
the first argument of the summation expression, the second parameter related information in the form of tuples incoming summing
the first parameter the second parameter tuple is variable The second parameter is the initial variables, the third parameter is a variable limit.

n = sympy.Symbol('n')
print(sympy.summation(2 * n, (n,1,100)))

Solutions with summation equation

x = sympy.Symbol('x')  #声明符号变量
i = sympy.Symbol('i',integer = True)
f =  sympy.summation(x,(i,1,5)) + 10 * x - 15  #构造表达式
reslute = sympy.solve(f,x) #第一个参数是表达式,第二个是变量;求解结果
print(reslute)

Evaluating Limit:

x = sympy.Symbol('x')
f1 = sympy.sin(x)/x    #声明表达式
sympy.limit(f1,x,0)     #求解表达式是f1,变量x趋于0的极限

Derivation:

x= sympy.Symbol('x')
f = x**2+2*x+1
print(sympy.diff(f,x))
#第一个参数是表达式,第二个参数是符号变量、返回一个<class 'sympy.core.add.Add'>类型的结果

Variable function deflector

y = sympy.Symbol('y')
f3 = x**2+2*x+y**4
print(sympy.diff(f3,x)) #对x求偏导
print(sympy.diff(f3,y)) #对y求偏导

Seeking the definite integral: sympy.integrate functions:
The first parameter is filled in the expression product, a second parameter tuple {fill the first parameter tuple is a variable, the second argument is the lower limit, the second parameter is the upper limit]
# solving complex integration time, follow the principle of variable nested

x = sympy.Symbol('x')
f = 2 * x
print(sympy.integrate(f,(x,0,1)))

Solving indefinite integral: integration is not simple to maybe upper and lower limits of integration:

x = sympy.Symbol('x')
f = sympy.exp(x)+3**2+sympy.log(x)
print(sympy.integrate(f,x)) 
#求不定积分,此时第二个参数填入的不再是元组,而是直接传入变量,因为没有上下限。当然也可以作为元组形式传入元组

Solving the differential equation y '= 4xy [i.e. (Dy / DX). 4 = X y]:

#定义符号变量
x=sympy.Symbol('x') 
 #定义符号函数、f可以与x构成函数方程f(x)、因为x就是自变量、f是因变量。
f = sympy.Function('f')
fx = sympy.diff(f(x),x)
print(sympy.dsolve(fx-2*x*f(x),f(x)))
#dsolve函数第一个参数传入微分方程表达式,第二个参数传入函数【f与x构成的函数f(x)】

Matrix operations to numpy quite accustomed numpy.

Guess you like

Origin blog.csdn.net/qq_42658739/article/details/90417727