简体   繁体   English

使用Sympy进行Python中的符号集成

[英]Symbolic Integration in Python using Sympy

I want to integrate exp(-(x^2 + y^2)) in python using sympy library. 我想使用sympy库在python中集成exp( - (x ^ 2 + y ^ 2))。 I could find the integral of exp(-(x^2)) 我可以找到exp的积分( - (x ^ 2))

>>> B1 = sympy.exp(-alpha1 * (r1_x**2))
>>> p = integrate(B1,r1_x)
>>> p
pi**(1/2)*erf(alpha1**(1/2)*r1_x)/(2*alpha1**(1/2))

But when I want to try integrate exp(-(x^2 + y^2)) 但是当我想尝试整合exp( - (x ^ 2 + y ^ 2))

>>> B1 = sympy.exp(-alpha1 * (r1_x**2 + r1_y**2))
>>> p = integrate(B1,r1_x)
>>> p
Integral(exp(-alpha1*(r1_x**2 + r1_y**2)), r1_x)

There is no output and python can't take the integral! 没有输出和python不能采取积分!

(I am the lead developer of SymPy) (我是SymPy的首席开发人员)

DSM is correct that you can get this to work by calling expand, and that there is no general way to do this (because in general, integrals don't have closed forms). DSM是正确的,您可以通过调用expand来使其工作,并且没有通用的方法来执行此操作(因为通常,积分没有封闭的表单)。

I just wanted to point out that if SymPy cannot do an integral that does have a closed form, we consider this a bug, and you should feel free to report it at http://code.google.com/p/sympy/issues . 我只是想指出,如果SymPy不能做一个封闭形式的积分,我们认为这是一个错误,您可以随时在http://code.google.com/p/sympy/issues上报告它。 。

sympy doesn't always recognize every form, and so sometimes you have to give it a little help: sympy并不总能识别出每一种形式,所以有时你必须给它一些帮助:

>>> import sympy
>>> alpha1, r1_x, r1_y = sympy.var("alpha1 r1_x r1_y")
>>> B1 = sympy.exp(-alpha1 * (r1_x**2 + r1_y**2))
>>> B1.integrate(r1_x)
Integral(exp(-alpha1*(r1_x**2 + r1_y**2)), r1_x)
>>> B1.expand(alpha1)
exp(-alpha1*r1_x**2)*exp(-alpha1*r1_y**2)
>>> B1.expand(alpha1).integrate(r1_x)
sqrt(pi)*exp(-alpha1*r1_y**2)*erf(sqrt(alpha1)*r1_x)/(2*sqrt(alpha1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM