计算尚未完成,因为在每个候选者中都发现了不可确定的集合成员资格

问题描述 投票:0回答:1

使用 IPython 中的 Sympy,我收到一个我无法理解的错误。

下面的

Y1
函数是 x 中的多项式,其最大值在 [0,1] 范围内(参见绘图,请注意 y 轴是反转的!),我已经检查过使用更简单的多项式是可行的,例如
maximum(x*(1-x), x, Interval(0, 1)) → 1/4
,但是对于
Y1
,重复一遍,我收到一条我无法理解的错误消息。有谁愿意帮助我吗?

In [8]: plot((Y1, interval1), (Y2, interval2), backend=InvertYAxis);

In [9]: Y1
Out[9]: x**4/24 - 5*x**3/64 + 7*x/192

In [10]: maximum(Y1, x, Interval(0, 1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 maximum(Y1, x, Interval(0, 1))

File /usr/lib/python3.11/site-packages/sympy/calculus/util.py:792, in maximum(f, symbol, domain)
    789     if domain is S.EmptySet:
    790         raise ValueError("Maximum value not defined for empty domain.")
--> 792     return function_range(f, symbol, domain).sup
    793 else:
    794     raise ValueError("%s is not a valid symbol." % symbol)

File /usr/lib/python3.11/site-packages/sympy/calculus/util.py:208, in function_range(f, symbol, domain)
    203     raise NotImplementedError(
    204             'Infinite number of critical points for {}'.format(f))
    206 critical_points += solution
--> 208 for critical_point in critical_points:
    209     vals += FiniteSet(f.subs(symbol, critical_point))
    211 left_open, right_open = False, False

File /usr/lib/python3.11/site-packages/sympy/sets/sets.py:1561, in Intersection.__iter__(self)
   1559 if not candidates:
   1560     raise TypeError("None of the constituent sets are iterable")
-> 1561 raise TypeError(
   1562     "The computation had not completed because of the "
   1563     "undecidable set membership is found in every candidates.")

TypeError: The computation had not completed because of the undecidable set membership is found in every candidates.
python sympy
1个回答
0
投票

这是 SymPy 最大值函数中的一个错误。不过,其他 SymPy 函数可用于计算此最大值:

In [16]: x, y = symbols('x, y')

In [17]: p = x**4/24 - 5*x**3/64 + 7*x/192

In [18]: p
Out[18]: 
 4      3      
x    5⋅x    7⋅x
── - ──── + ───
24    64    192

In [19]: py = resultant(y - p, p.diff(x), x)

In [20]: py
Out[20]: 
  3           2                                 
 y      8515⋅y        1225⋅y           4459     
──── + ────────── - ─────────── - ──────────────
1296   3623878656   14495514624   11132555231232

In [21]: max(real_roots(py) + [p.subs(x, 0), p.subs(x, 1)])
Out[21]: 
       ⎛            3             2                     ⎞
CRootOf⎝8589934592⋅x  + 26158080⋅x  - 940800⋅x - 4459, 2⎠

In [22]: _.n(50)
Out[22]: 0.011075450895889113601230819303513051420756055228604
© www.soinside.com 2019 - 2024. All rights reserved.