如何检查SymPy表达式是否具有解析积分 [英] How to check if a SymPy expression has analytical integral

查看:242
本文介绍了如何检查SymPy表达式是否具有解析积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在此处解决我的其他问题,所以我需要sympy在没有积分的解析/符号解决方案时返回错误.

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.

例如,如果我尝试:

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.我也已经在Reddit上的此处问过这个问题.

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

推荐答案

始终存在符号"解决方案:我刚刚发明了一个新函数intcos(x),根据定义,该函数是1/cos(x**2)的反导数.现在,此积分具有符号解决方案!

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!

要严格回答问题,必须限制答案中允许的功能类别.通常,人们会考虑基本功能.正如 SymPy积分参考所述,Risch算法员工可以证明某些功能没有基本的反导数.使用选项risch=True并检查返回值是否为sympy.integrals.risch.NonElementaryIntegral

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

但是,由于Risch算法的实现不完整,因此在许多情况下,例如1/cos(x**2),它都会返回一个普通的Integral对象.这意味着它既不能找到一种基本的反导,也不能证明它不存在.

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.

对于此示例,使用rewrite(cos, exp)可以帮助重写指数函数的三角函数:

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)  

返回True,因此我们知道积分是非基本的.

returns True, so we know the integral is nonelementary.

但是通常我们并不需要真正的基本功能.像Gamma或erf或Bessel函数之类的东西可能还可以;只要是某些已知"功能(当然是模糊术语)即可.问题就变成了:如何判断SymPy是否能够集成特定的表达式?使用.has(Integral)进行检查:

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

(不是isinstance(Integral),因为返回值可以是2*Integral(1/cos(x**2), x).)除了SymPy无法找到反导数之外,这不能证明.反导可能很好地是一种已知功能,甚至是一种基本功能.

(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.

这篇关于如何检查SymPy表达式是否具有解析积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆