I have a function that I want to integrate symbolically using the Sympy library. I followed the steps outlined in the website: https://docs.sympy.org/latest/modules/integrals/integrals.html
However, when I try to integrate, I just get back the integral in symbolic form: the Sympy integrate function doesn't seem to have integrated at all.
I am not sure what is wrong with my code:
s = Symbol('s')
s0 = Symbol('s0')
k = Symbol('k')
u = Symbol('u')
E = Symbol('E')
init_printing(use_unicode=False, wrap_line=False)
func = (np.pi - 2)*s/s0*(1/((1/s0**2)*(1 - k*u/E - s**2*u**2))**0.5)
integrate(func,(u,0,1/a))
I believe that I followed everything correctly, but I'm not getting the output that should show up like in the website when integrating simple functions. I'm guessing its this np.pi
. But isn't this just a number? Shouldn't SymPy still be able to integrate?
When in SymPy-land, use SymPy. Like @kikon said, no need to use a pi other than the one that SymPy provides. Since you are dividing by s0
and a
it would be nice to tell SymPy that you know those are not zero. The following works for me:
from sympy import symbols, integrate
s, k, u, E = Symbol('s k u E')
a,s0 = symbols('a s0', nonzero=True)
func = (pi - 2)*s/s0*(1/((1/s0**2)*(1 - k*u/E - s**2*u**2))**0.5)
integrate(func,(u,0,1/a))
Piecewise((-s*(-2 + pi)*log(2*sqrt(-s**2) - k/E)/sqrt(-s**2) + s*(-2 + pi)*log(2*sqrt(-s**2)*sqrt(1 - s**2/a**2 - k/(E*a)) - 2*s**2/a - k/E)/sqrt(-s**2), s0 >= 0), (-s*(-s0)**1.0*(-2 + pi)*log(2*sqrt(-s**2) - k/E)/(s0*sqrt(-s**2)) + s*(-s0)**1.0*(-2 + pi)*log(2*sqrt(-s**2)*sqrt(1 - s**2/a**2 - k/(E*a)) - 2*s**2/a - k/E)/(s0*sqrt(-s**2)), True))
If you use sqrt
instead of **0.5
you will get a different form:
>>> from sympy import nsimplify
>>> integrate(nsimplify(func), (u,0,1/a))
Piecewise((-s*(-2 + pi)*log(2*sqrt(-s**2) - k/E)/sqrt(-s**2) + s*(-2 + pi)*log(2*sqrt(-s**2)*sqrt(1 - s**2/a**2 - k/(E*a)) - 2*s**2/a - k/E)/sqrt(-s**2), ((s0 >= 0) & Ne(s**2, 0)) | ((s0 >= 0) & Ne(s**2, 0) & Ne(k/E, 0))), (-2*E*s*(-2 + pi)*sqrt(1 - k/(E*a))/k + 2*E*s*(-2 + pi)/k, (s0 >= 0) & Ne(k/E, 0)), (s*(-2 + pi)/a, s0 >= 0), (s*(-2 + pi)*log(2*sqrt(-s**2) - k/E)/sqrt(-s**2) - s*(-2 + pi)*log(2*sqrt(-s**2)*sqrt(1 - s**2/a**2 - k/(E*a)) - 2*s**2/a - k/E)/sqrt(-s**2), ((s > -oo) & (s < oo) & Ne(s, 0)) | (Ne(s**2, 0) & Ne(k/E, 0))), (2*E*s*(-2 + pi)*sqrt(1 - k/(E*a))/k - 2*E*s*(-2 + pi)/k, Ne(k/E, 0)), (-s*(-2 + pi)/a, True))