How to check if a SymPy expression has analytical integral

谁都会走 提交于 2019-12-11 09:08:46

问题


I want to solve my other question here so I need sympy to return an error whenever there is no analytical/symbolic solution for and integral.

For example if I try :

from sympy import *
init_printing(use_unicode=False, wrap_line=False, no_global=True)
x = Symbol('x')
integrate(1/cos(x**2), x)

It just [pretty] prints the integral itself

without solving and/or giving an error about not being able to solve it!

P.S. I have also asked this question here on Reddit.


回答1:


A "symbolic" solution always exists: I just invented a new function intcos(x), which by definition is the antiderivative of 1/cos(x**2). Now this integral has a symbolic solution!

For the question to be rigorously answerable, one has to restrict the class of functions allowed in the answer. Typically one considers elementary functions. As SymPy integral reference explains, the Risch algorithm it employs can prove that some functions do not have elementary antiderivatives. Use the option risch=True and check whether the return value is an instance of sympy.integrals.risch.NonElementaryIntegral

from sympy.integrals.risch import NonElementaryIntegral
isinstance(integrate(1/exp(x**2), x, risch=True), NonElementaryIntegral)  # True

However, since Risch algorithm implementation is incomplete, in many cases like 1/cos(x**2) it returns an ordinary Integral object. This means it was not able to either find an elementary antiderivative or prove that one does not exist.

For this example, it helps to rewrite the trigonometric function in terms of exponential, with rewrite(cos, exp):

isinstance(integrate((1/cos(x**2)).rewrite(cos, exp), x, risch=True), NonElementaryIntegral)  

returns True, so we know the integral is nonelementary.

Non-elementary antiderivatives

But often we don't really need an elementary function; something like Gamma or erf or Bessel functions may be okay; as long as it's some "known" function (which of course is a fuzzy term). The question becomes: how to tell if SymPy was able to integrate a specific expression or not? Use .has(Integral) check for that:

integrate(2/cos(x**2), x).has(Integral)   # True

(not isinstance(Integral) because the return value can be, like here, 2*Integral(1/cos(x**2), x).) This does not prove anything other than SymPy's failure to find the antiderivative. The antiderivative may well be a known function, even an elementary one.



来源:https://stackoverflow.com/questions/50039222/how-to-check-if-a-sympy-expression-has-analytical-integral

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!