plasmascience的个人博客分享 http://blog.sciencenet.cn/u/plasmascience

博文

符号计算Sympy

已有 3282 次阅读 2018-11-28 05:05 |系统分类:科研笔记

用了mathematica 与 maxima 很多年, 这几年开始用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

上一篇:托卡马卡磁力线可视化
下一篇:托卡马克中的粒子轨道动画1
收藏 IP: 128.138.109.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

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

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部