Symbolic integration in Python using Sympy

I want to integrate exp (- (x ^ 2 + y ^ 2)) in python using the sympy library. I could find the integral of 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 integrating 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 cannot take the integral!

+5
source share
2 answers

sympy does not always recognize each form, so sometimes you need to help a little:

>>> 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))
+3
source

(I am the lead developer of SymPy)

DSM is correct that you can make it work by calling an extension, and that there is no general way to do this (because, in general, integrals do not have closed forms).

, SymPy , , , http://code.google.com/p/sympy/issues.

+6

All Articles