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 sympy get the same result by using a different approach than mine?
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
2
The substitution t=x/sqrt(1-x^2), or, equivalently, x=t/sqrt(1+t^2) allows you to do the integral manually (after a lot of algebra!).
This substitution came from successive substitutions x=sin(theta), then t=tan(theta), then a monumental amount of paper! (For tan(theta)-like techniques see https://en.wikipedia.org/wiki/Tangent_half-angle_substitution – in this case the first substitution leaves a sine-squared in the denominator, but this can be written in terms of cos(2.theta), so we end up with a substitution for tan(theta) and not the more familiar tan(theta/2).)
The integrand is an even function, so I have “doubled the integral over half range”.
Note that x=1 translates to t=infinity.
import sympy as sp
x, t = sp.symbols( "x t" )
integrand = sp.sqrt( 1 - x ** 2 ) / ( 1 + x ** 2 )
substitution = t / sp.sqrt( 1 + t ** 2 )
ex = (integrand.subs( x, substitution ) * sp.diff( substitution, t) ).simplify()
res = 2 * sp.integrate( ex, ( t, 0, sp.oo ) )
print( res )
Output:
-pi + sqrt(2)*pi
1
Tried the following approach, but got zero as result, which is wrong (correct result is (sqrt(2) - 1) * pi
):
import sympy as sp
x, u = sp.symbols('x u')
e1 = sp.sqrt(1 - x**2)
e2 = 1 + x**2
expr = e1 / e2
I = sp.Integral(expr, (x, -1, 1))
expr_s = (expr.subs(x, sp.sin(u))* sp.diff(sp.sin(u), u)).simplify()
sp.integrate(expr_s, (u, -sp.pi/2, sp.pi/2)).simplify()
Filed an issue at GitHub.
3