Decomposing integral into elliptic integrals - python

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.

Related

Symbolic matrix multiplication on GPU using sympy?

I have a matrix M with approximately 300 rows and columns. Each entry contains a symbolic expression, and there are around 40 symbols in total. Doing something like M*M can take a long time (hours). Is there a way to do this symbolic matrix multiplication on a gpu using sympy, or more generally in python?
You're in luck! I just levelled up on this a few minutes ago. The optimal solution for parallelizing SymPy expressions on [G|T]PU hardware is (as of mid-2021) Aesara, a well-maintained fork of the now-defunct Theano package foolishly discontinued in the mistaken belief that Tensorflow was better. Spoiler alert: it wasn't.
Since the OP sadly failed to supply a minimal length example (MLE) for us to mutilate, we'll just copypasta a mostly useless example demonstrating the core API concepts:
# SymPy -> Aesara translater, bundled with SymPy.
from sympy.printing.aesaracode import aesara_function
# Arbitrary SymPy expression.
from sympy import sin
from sympy.abc import x
expr = sin(x)/x
# GPU-parallelized Aesara function compiled from that expression! Yup.
f = aesara_function([x], [expr])
Generally speaking, the optimal solution is whatever SymPy's "Numeric Computation" documentation says it is. Currently, that means Aesara; since data science in Python changes faster than a King Cobra biting the village hand that feeds it, everything has probably already changed by the time you read this. Since the aforementioned documentation is surprisingly intelligible, go there first instead. That's your optimum landing page for all things optimizing SymPy.

sympy Integration stopped for particular polinomial function grow-up

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.

Solving homogeneous linear systems using Sympy

I'm teaching myself linear algebra, and I'm trying to learn the corresponding Numpy and Sympy code alongside it.
My book presented the following matrix:
example1 = Matrix([[3,5,-4,0],[-3,-2,4,0],[6,1,-8,0]])
with the instructions to determine if there is a nontrivial solution. The final solution would be x = x3 * Matrix([[4\3],[0],[1]]). (Using Jupyter's math mode, I used the following to represent the solution:)
$$\pmb{x} =
\begin{bmatrix}x_1\\x_2\\x_3\end{bmatrix} =
\begin{bmatrix}\frac{4}{3}x_3\\0\\x_3\end{bmatrix} =
x_3\begin{bmatrix}\frac{4}{3}\\0\\1\end{bmatrix} \\
= x_3\pmb{v} \text{, where }\pmb{v} = \begin{bmatrix}\frac{4}{3}\\0\\1\end{bmatrix}$$
How can I now solve this in Sympy? I've looked through the documentation, but I didn't see anything, and I'm at a bit of a loss. I know that errors tend to be thrown for free variables. Is there a way to determine nontrivial solutions and the corresponding general solution using Sympy, considering that nontrivial solutions are reliant upon free variables? Or is np.linalg generally more preferred for this type of problem?
This is a linear system so
>>> linsolve(Matrix([[3,5,-4,0],[-3,-2,4,0],[6,1,-8,0]]))
FiniteSet((4*tau0/3, 0, tau0))
The tau0 is the free parameter that you refer to as x3.

Symbolic integration of Modified Bessel Functions using sympy and Mathematica

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.

Is there a workaround to avoid SymPy hanging for high order trigonometic integrands?

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.

Categories

Resources