# 符号计算Sympy

In [1]: from sympy import *

In [2]: x=symbols('x') #define a variable of symbol type

In [3]: f=Function('f')(x) #define a function of a variable x

In [4]: integrate(sin(x)) #calculat infinite integration

In [5]: integrate(x*x*exp(-x*x),(x,-oo,oo)) #compute definite integration

In [6]: diff(x*x,x) #derivative

When defining a variable, we can specify some properties of the variable, For example:

In [27]: a=symbols('a', real=True, zero=False)

Here we specify that the variable 'a' is a nonzero real number。

Without specifying 'a' is nonzero, then it will take much longer time for sympy to figure  out the following besselj integration.

In [28]: integrate(besselj(0,a*x)**2*exp(-x*x/2)*x,(x,0,oo))
Out[28]: exp(-a**2)*besseli(0, a**2)

This is one important integration used in deriving the polarization density in gyrokinetic theory.

Here besselj is the bessel function of the first kind, besseli is the modified bessel function of the first kind, see wikepedia.

https://blog.sciencenet.cn/blog-620148-1148657.html

## 相关博文

GMT+8, 2022-8-18 13:36

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社