Search code examples
pythonsympy

Integral that Sympy cannot solve but Wolfram Alpha can


I am using the following code, which returns the integral itself and not its result:

x = sp.Symbol('x')
integrand = (sp.sqrt(1 - x**2) / (1 + x**2))
sp.integrate(integrand, (x, -1, 1), manual=True)

Wolfram Alpha can find a closed-form result for this integral.

So my question is: can get the same result by using a different approach than mine?


Solution

  • Sympy can't get a closed-form solution so you will have to resort to numerical integration. You can simply do that by adding .evalf() to the end of your integration.

    res = sp.integrate(integrand, (x, -1, 1), manual=True).evalf()
    print(res)  # 1.30129028456857