Search code examples
pythonsympysymbolic-math

Symbolic Integration returns garbage


I have the following integral, which I have computed via other sources to match a given solution. However, SymPy returns something that appears to be garbage. Why is this happening? Have I not explicitly established something that Mathematica and Integral Calculator assume?

from sympy import symbols, integrate, oo
x, z = symbols('x,z')
expr = 1/((x**2 + z**2)**(3/2))
integrate(expr, (x,-oo,oo))

Gives the following result:

enter image description here

I know the result to be: 2/(z^2)

As I don't know how (or if it's even possible) to enter LaTeX here, below is the operation attempted and desired result

enter image description here


Solution

  • You have **(3 / 2) which is a float. This is an issue that SymPy struggles with and is one of the issues mentioned here under Gotchas and Pitfalls. I found this from the GitHub page integrate((x-t)**(-1/2)*t,(t,0,x)) raises ValueError.

    You need to make sure that your exponent is a rational number. There are a few ways to do this. Below, we use S (sympify):

    from sympy import symbols, integrate, oo, S
    x, z = symbols('x,z')
    expr = 1/((x**2 + z**2)**(S(3)/2))
    integrate(expr, (x,-oo,oo))
    

    Which gives the desired output.