Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic Integration in Python using Sympy

I want to integrate exp(-(x^2 + y^2)) in python using 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 integrate 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!

like image 915
Roy Avatar asked Aug 29 '12 01:08

Roy


People also ask

How do you define a symbol in SymPy?

SymPy also has a Symbols() function that can define multiple symbols at once. String contains names of variables separated by comma or space. In SymPy's abc module, all Latin and Greek alphabets are defined as symbols. Hence, instead of instantiating Symbol object, this method is convenient.

How do you define a symbolic variable in Python?

Symbolic math variables are declared using SymPy's symbols() function. Note, the arguments passed to the symbols() function (symbol names) are separated by a space, no comma, and surrounded by quotes. The output of the symbols() function are SymPy symbols objects.

What is SymPy used for?

SymPy stands for Symbolic Mathematics in Python and is a Python library for dealing with mathematics. It is one of the core libraries of the SciPy Ecosystem among other giants like NumPy, Pandas, and Matplotlib. With SymPy you can manipulate mathematical expressions.


1 Answers

(I am the lead developer of 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).

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.

like image 103
asmeurer Avatar answered Nov 14 '22 21:11

asmeurer