2
votes

Whenever I try and use Sympy to Integrate my Function, it doesn't evaluate it but simply returns my input multiplied by 1.0.

Here is my code:

import sympy as sp

a = sp.Symbol('a')

Ωm = 0.31
Ωr = 9*(10**(-5))
ΩΛ = 0.69
Ω0 = Ωm + Ωr + ΩΛ


I = sp.integrate(((Ωm*a**(-1)) + (Ωr*a**(-2)) + (ΩΛ*a**(2)) + (1-Ω0))**(-0.5),a)
print(I)

Here is the answer I get:

1.0*Integral((0.69*a**2 - 8.99999999999235e-5 + 0.31/a + 9.0e-5/a**2)**(-0.5), a)

Where am I going wrong? Is this an integral beyond the capacity of Sympy? If so does anyone know any better ways to evaluate that integral. The limits are 0 and a by the way.

3

3 Answers

1
votes

When SymPy returns an integral unevaluated, that means that it doesn't know how to compute it.

In this case, the integrand is an algebraic function, which SymPy often has a difficult time with integrating.

-1
votes

Your code works fine. Result of the integration is non-convergent integral. You could print it in more beautiful form (in jupyter) with following code

import sympy as sp
sp.init_printing(wrap_line=False, no_global=True)

a = sp.Symbol('a')

Ωm = 0.31
Ωr = 9*(10**(-5))
ΩΛ = 0.69
Ω0 = Ωm + Ωr + ΩΛ


I = sp.integrate(((Ωm*a**(-1)) + (Ωr*a**(-2)) + (ΩΛ*a**(2)) + (1-Ω0))**(-0.5),a)
I

results

enter image description here

-1
votes

I ran the integral on wolfram alpha and it says it doesn't converge (the computation takes a while, so unless you have a wolfram premium account it might not complete):

Try out an integral you know converges.

So try out something like

import sympy as sp

sp.init_printing(wrap_line=False, no_global=True)
a = sp.Symbol('a')

I = sp.integrate(a**3,a)

You should get a**4/4