I would like to use sympy to solve a definite integral that I know that Mathematica can solve. In Mathematica the following line
Integrate[z^2 (BesselI[0, z^2] - BesselI[1, z^2]) Exp[-z^2], {z, 0, x}]
yields
1/3 x^3 HypergeometricPFQ[{1/2,3/2},{1,5/2},-2 x^2]-1/10 x^5 HypergeometricPFQ[{3/2,5/2},{3,7/2},-2 x^2]
It would prefer to use python with sympy for this and I try to with the following code
import sympy;
x, z = sympy.var('x z');
sympy.integrate( z**2*(sympy.besseli(0,z**2)-sympy.besseli(1,z**2))*sympy.exp(-z**2) ,(z,0,x));
Unfortunately, the computation just hangs. I give up after about 30-40 minutes of waiting. In Mathematica, it takes less than a second. If I change the integrand, then I can get sympy to solve it. Such as
sympy.integrate( z**2*sympy.besseli(0,z**2) ,(z,0,x));
yields
x**3*gamma(3/4)*hyper((3/4,), (1, 7/4), -x**4/4)/(4*gamma(7/4))
I am a long time Mathematica user and have a pretty good sense of how to get it to solve tricky integrals. As a new sympy users I lack that experience.
Are there any flags I could add?
Are there other ways to solve this problem in sympy?
If this integral is not possible with sympy, is there a way to understand sympy's limitations? Such as how sympy does integration versus Mathematica.
Related
Noted that in one of the integrals that I put into sympy the tool stopped.
integrate((sinhx)/(exp(5*x)-1),x)
I tried to rewrite the sinh-function and I put exp(x) = y.
Well, I obtained a product of polinomial function. On the denominator, one factor was:
y^4+y^3+y^2+y+1
The tool found the roots(4) but the previous poly can write on this way:
(y^2+ay+b)(y^2+cy+d)
The solution of the integral go for this step (factorize the polinomial on the way above) and sympy stopped here.
On various sites I see the final symbolic solution.
Is there a method on sympy to solve this kind of integrals?Thanks.A.
So, I'm trying to solve this nonlinear system of equations and I am getting the error "cannot create mpf". Here's the sympy link . Anyone know how to solve thsi?
mpmath is made for doing numerical calculations and l is a symbol, thus the error (you used an mpmath tanh in your second attempt, but SymPy's tanh in the first example). You already have the solution for k from your first attempt. Solving for l requires solving a 12th order polynomial in exp(1.401*l) which you won't be able to do symbolically in general.
I'm using sympy to solve a simple linear system of equations.
It's a coupled ODE, there are time-derivatives of variables and I need to solve the system of equations for the highest derivatives.
Since sympy doesn't allow me to solve for statements like phi_1.diff(t), I've replaced all derivatives with placeholder symbols.
For example:
phi.diff(t).diff(t) + phi(t) =0
becomes
ddphi + phi(t) = 0
This works fine. The solutions are correct and I can simulate the system - it's a pendulum: https://youtu.be/Gc_V2FussNk
The problem is that solving the system of equations (with linsolve) takes very long.
For just 2 equations, it takes 2 seconds.
For 3 equations, it's still calculating (after over 10 minutes).
EDIT: #asmeurer advised me to try out solve instead.
For n=3, linsolve took about 34 minutes - I only made one measurement.
solve takes 31 seconds (averages over 3 runs).
Still, I believe that a linear 3x3 system should be solved in fractions of a second.
And for n=4 solve becomes unbearably slow, too (still calculating)
I've formatted the code and created an iPython notebook: http://nbviewer.jupyter.org/gist/lhk/bec52b222d1d8d28e0d1baf77d545ec5
If you scroll down a little, you can see the formatted output of the system of equations and directly below that the call to linsolve
The equations are rather long but strictly linear in the second derivatives.
I'm sure that this system can be solved.
All I need to do is solve a 3x3 system of linear equations where the coefficients might be symbols.
Is there a more performant way to do this ?
solve (not linsolve) has some flags that you can set which can make it faster:
simplify=False: disables simplification of the result.
rational=False: disables the automatic conversion of floats to rationals.
There is a warning in the solve docstring that rational=False can lead to some equations not being solvable due to issues in the polys, so be aware that that's a potential issue.
I have found that solve can be very slow in jupyter notebook if you have run sp.init_printing() before your equations. I have a module "equations" where I write my equations and solve them.
This is faster:
import sympy as sp
import equations
sp.init_printing()
Than this:
import sympy as sp
sp.init_printing()
import equations
I tried the following integral in SymPy 0.7.3:
import sympy
from sympy import cos, sin, pi
sympy.var('x, L')
sympy.var('i, k', integer=True)
integrand = cos(pi*i*x/L)**2*cos(pi*k*x/L)**2*sin(pi*i*x/L)*sin(pi*k*x/L)
print(sympy.integrate(integrand, (x, 0, L), conds='none'))
I already tried not using the conds argument and not using integer=True when declaring i, k without success.
Is there any other workaround to solve this integral?
The solution for this integral is 0.
NOTE:
There are other similar integrands for which SymPy hangs if someone is interested.
I've noticed a trend of SymPy to hang when the simplifications along the integrations lead to 0 terms, so maybe the problem is in a simplification algorithms applied along the integration.
Unfortunately, currently SymPy's ability to deal with trigonometric integrals is not that great. There are maybe some forms that it will deal with better than others (you can play with trigsimp'ing the input to see if it helps).
If you don't mind dealing with the output, you can rewrite the integrand in terms of complex exponentials. SymPy is much better at integrating those. Use .rewrite(exp). You'll have to use .rewrite(sin) and simplify at the end to get it back in terms of trig functions, though. Unfortunately, it doesn't really help for this particular integral :(
Better trigonometric integration is coming. The work from https://github.com/sympy/sympy/pull/2380 needs to be finished, and one more algorithm implemented.
This integral cannot be evaluated.
>>> from sympy import integrate, sin, cos,sqrt, Symbol
>>> x = Symbol('x')
>>> integrate(1/sqrt(1+sin(x)**2),x)
Integral(1/sqrt(sin(x)**2 + 1), x)
Does sympy not support decomposing integrals into elliptic integrals as mathematica, wolfram alpha, or maple do? (wolfram answer)
I tried looking in the sympy buglist under the integration label, but nothing jumped out at me (there was something on recognizing elliptic integrals, but the bug description wasn't clear to me).
Thanks!
SymPy's integrators are always improving, but there are still several classes of integrals that they cannot do yet. This is apparently one of them. Feel free to open new issues in the bug tracker for things like this. It's definitely something that we want to be able to do.