'Sympy returns Integrate when integrating with a constant sometimes

I am trying to integrate a seemingly simple equation but can't. This is the equation:

sin(x)
------
x + n·pi

So when I do

import sympy as sym
x = sym.Symbol("x")
n = sym.Symbol("n", real = True, constant = True)
sym.integrate(sym.sin(x)/(x+sym.pi*n),x)

I get Integral(sin(x)/(pi*n + x), x). I have tried other solutions like .doit() and .evalf() but to no avail.

  • If I declare n = 3 or other random number then it works.
  • It also works if n is outside as in sym.integrate(sym.sin(x)/(x+sym.pi)/n, x). This is obviuosly not the same formula but still
  • When inputting the formula into wolframalpha it gets computed instantly.

How to make sympy calculate this?



Solution 1:[1]

Sympy's integration algorithms appear to be very sensitive to assumptions. For example, assuming n to be a positive integer, then SymPy is able to compute the result:

import sympy as sym
x = sym.Symbol("x")
n = sym.Symbol("n", integer=True, positive=True)
res = sym.integrate(sym.sin(x)/(x+sym.pi*n),x)
res
# out: exp(I*pi*n)*Si(pi*n + x)

You can then simplify it:

res.simplify()
# out: (-1)**n*Si(pi*n + x)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Davide_sd