Search code examples
pythonsympysymbolic-integration

Integrate expression more effectively with sympy


I'm trying to do the following integration with Sympy:

import sympy as sp
sp.var('a e nu lamb omeg')
r = a*(1.-e**2)/(1. + e*sp.cos(lamb-omeg))
I = sp.integrate(r**2, lamb)

And what happens is that the processor stays at 100% for some minutes, my laptop gets hot and then finally I get tired of waiting and give up.

Surprisingly, when I do the same integration with Maxima, I get the result almost instantly. The only difference is that Maxima asks me if e**2-1 is positive or negative.

How do I get the same functionality with Sympy? And why is that not happening automatically?


Solution

  • It can be done, apparently, by making assumptions as for the constants, although that's not the same assumptions that Maxima used:

    import sympy as sp
    sp.var('a e nu lamb omeg', positive=True, real=True)
    r = a*(1.-e**2)/(1. + e*sp.cos(lamb-omeg))
    I = sp.integrate(r**2, lamb)