【发布时间】:2022-01-01 03:01:27
【问题描述】:
我正在尝试确定函数的范围,在本例中是多项式。我的程序已经使用了很多 SymPy 函数,所以我想我可以使用 sympy.calculus.util 模块中的 function_range 函数。
对于域 R 上的多项式 1/2*x**4-2*x**2-1/4*x 不起作用,并输出此错误:
>>> from sympy import S
>>> from sympy.abc import x
>>> expr = S(1)/2*x**4-2*x**2-S(1)/4*x
>>> function_range(expr, x, S.Reals)
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/base/data/home/apps/s~sympy-live-hrd/66.426491309333028408/sympy/sympy/calculus/util.py", line 223, in function_range
for critical_point in critical_points:
File "/base/data/home/apps/s~sympy-live-hrd/66.426491309333028408/sympy/sympy/sets/sets.py", line 1375, in __iter__
"The computation had not completed because of the "
TypeError: The computation had not completed because of the undecidable set membership is found in every candidates.
考虑到这个函数是完全连续的并且不是很复杂,我真的不明白为什么 SymPy 不能确定 Range,因为它是 a rather simple one。我对输入做错了吗?是否有 SymPy 的替代品来执行此操作?
[编辑为使用 Rationals 而不是 Floats]
【问题讨论】:
-
为我工作。您使用的是什么版本的 SymPy?
-
你对
x的定义是什么?请提供a minimal reproducible example。 -
当
x定义为x = Symbol('x')结果为Interval(-2.35737693115205, oo)。所以 SymPy 有效。 -
好吧,所以我测试了更多。当我只是在本地的基本 python shell 中运行此代码时,它可以工作,并给出与@YuriGinsburg 提到的相同的间隔。但是,在我自己的个人代码和 SymPy live shell 上,它不起作用。 SymPy live 和我的代码都解析输入,将
expr转换为x**4/2 - 2*x**2 - x/4。同时,python shell 将输入解析为0.5*x**4 - 2*x**2 - 0.25*x。当我按照 shell 的方式输入 expr 时,该函数就起作用了。 -
我可以用
expr = S(1)/2*x**4-2*x**2-S(1)/4*x重现这个。我认为这可能是 Live 自动执行的操作。看起来应该向github报告的错误:github.com/sympy/sympy/issues
标签: python math sympy calculus