首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sympy在解决“[.]不可判定的集合成员资格在每个候选人中都发现”时的错误是否意味着不存在解决方案?

Sympy在解决“[.]不可判定的集合成员资格在每个候选人中都发现”时的错误是否意味着不存在解决方案?
EN

Stack Overflow用户
提问于 2022-04-27 10:17:43
回答 1查看 122关注 0票数 0

我试图为变量x (依赖于其他符号b, c, d)求解以下方程:

代码语言:javascript
复制
from sympy import symbols, sin, cos, solve, solveset, S
  
b, c, d, x = symbols('b c d x', real=True)

expr = sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x)  # find `x` such that `expr == 0`

还有适用于各种符号的其他限制因素,即:

代码语言:javascript
复制
0 < d < b < d+x < c

因此,我尝试使用solve来合并这些不等式约束:

代码语言:javascript
复制
result = solve(
    [
        sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x),
        0 < d,
        d < b,
        b < d+x,
        d+x < c,
    ],
    x,
)

但是,我得到以下错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 211, in _parallel_dict_from_expr_if_gens
    monom[indices[base]] = exp
KeyError: sin(c - x)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 818, in _solve_inequality
    p = Poly(expr, s)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 181, in __new__
    return cls._from_expr(rep, opt)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 310, in _from_expr
    rep, opt = _dict_from_expr(rep, opt)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 368, in _dict_from_expr
    rep, gens = _dict_from_expr_if_gens(expr, opt)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 307, in _dict_from_expr_if_gens
    (poly,), gens = _parallel_dict_from_expr_if_gens((expr,), opt)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
    raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: sin(c - x) contains an element of the set of generators.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 211, in _parallel_dict_from_expr_if_gens
    monom[indices[base]] = exp
KeyError: sin(c - x)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 246, in reduce_rational_inequalities
    (numer, denom), opt = parallel_poly_from_expr(
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 4414, in parallel_poly_from_expr
    return _parallel_poly_from_expr(exprs, opt)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polytools.py", line 4467, in _parallel_poly_from_expr
    reps, opt = _parallel_dict_from_expr(exprs, opt)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 332, in _parallel_dict_from_expr
    reps, gens = _parallel_dict_from_expr_if_gens(exprs, opt)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/polys/polyutils.py", line 216, in _parallel_dict_from_expr_if_gens
    raise PolynomialError("%s contains an element of "
sympy.polys.polyerrors.PolynomialError: sin(c - x) contains an element of the set of generators.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 827, in _solve_inequality
    rv = reduce_rational_inequalities([[ie]], s)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 249, in reduce_rational_inequalities
    raise PolynomialError(filldedent('''
sympy.polys.polyerrors.PolynomialError: 
only polynomials and rational functions are supported in this context.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/test2.py", line 5, in <module>
    result = solve(
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/solvers.py", line 920, in solve
    return reduce_inequalities(f, symbols=symbols)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 996, in reduce_inequalities
    rv = _reduce_inequalities(inequalities, symbols)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 912, in _reduce_inequalities
    other.append(_solve_inequality(Relational(expr, 0, rel), gen))
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 829, in _solve_inequality
    rv = solve_univariate_inequality(ie, s)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/solvers/inequalities.py", line 500, in solve_univariate_inequality
    frange = function_range(e, gen, domain)
  File "/path/to/venv/lib/python3.9/site-packages/sympy/calculus/util.py", line 208, in function_range
    for critical_point in critical_points:
  File "/path/to/venv/lib/python3.9/site-packages/sympy/utilities/iterables.py", line 2881, in roundrobin
    yield nxt()
  File "/path/to/venv/lib/python3.9/site-packages/sympy/sets/sets.py", line 1439, in __iter__
    raise TypeError(
TypeError: The computation had not completed because of the undecidable set membership is found in every candidates.

我不知道那个错误意味着什么。这是否意味着对于给定的不等式约束没有解?

(同情版本: 1.10.1)

索尔夫塞

我还尝试使用solveset (但是,这里似乎不可能包含不平等约束):

代码语言:javascript
复制
result = solveset(
    sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x),
    x,
    domain=S.Reals,
)

但是,经过长时间的计算后,它只返回原始表达式:

代码语言:javascript
复制
ConditionSet(x, Eq(-sin(x)*sin(-c + 2*d + x) + sin(c - x)*sin(-2*b + 2*d + x), 0), Reals)

matlab

我还试着用Matlab来求解这个方程:

代码语言:javascript
复制
syms b c d x

eq1 = sin(x)*sin(c - 2*d - x) - sin(2*b - 2*d - x)*sin(c - x) == 0;
eq2 = 0 < d;
eq3 = d < b;
eq4 = b < d+x;
eq5 = d+x < c;
result = solve([eq1 eq2 eq3 eq4 eq5], x, "Real",true, "ReturnConditions",true);

然而,结果是完全不同的,涉及一个四阶多项式,我无法理解:

代码语言:javascript
复制
>> result.x
 
ans =
 
2*pi*k + atan2(y, 1) - atan2(-y, 1)

>> result.conditions
 
ans =
 
d < b & b + atan2(-y, 1) < d + atan2(y, 1) + 2*pi*k & d + atan2(y, 1) + 2*pi*k < c + atan2(-y, 1) & cos(b)*sin(b) + 2*cos(b)^2*cos(d)*sin(d) ~= cos(d)*sin(d) + 2*cos(b)*cos(d)^2*sin(b) & in(k, 'integer') & ~in(c/pi, 'integer') & in(y, 'real') & 4*y^2*cos(c - 2*d) + 2*y^3*sin(c - 2*d) + sin(2*b - 2*d)*sin(c) + y^2*cos(c - 2*b + 2*d) + 3*y^2*cos(2*b + c - 2*d) + 2*y^3*sin(2*b + c - 2*d) + y^4*sin(2*b - 2*d)*sin(c) == 2*y*sin(2*b + c - 2*d) + 2*y*sin(c - 2*d) & 0 < d
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-27 12:43:18

下面是如何使用SymPy获得类似于Matlab解决方案的内容(注意,我重新定义了符号以简化输出):

代码语言:javascript
复制
In [19]: a, b, c, d, e, x, y = symbols('a, b, c, d, e, x, y', real=True)

In [20]: expr = sin(x)*sin(a - x) - sin(e - x)*sin(c - x)

In [21]: expr.expand(trig=True)
Out[21]: 
                                        2                                                          
sin(a)⋅sin(x)⋅cos(x) - sin(c)⋅sin(e)⋅cos (x) + sin(c)⋅sin(x)⋅cos(e)⋅cos(x) + sin(e)⋅sin(x)⋅cos(c)⋅c

           2                2                 
os(x) - sin (x)⋅cos(a) - sin (x)⋅cos(c)⋅cos(e)

In [22]: expr2 = resultant(expr.expand(trig=True), cos(x)**2 + sin(x)**2 - 1, cos(x))

In [23]: expr2.collect(sin(x))
Out[23]: 
⎛     2                                                             2       2         2       2    
⎝- sin (a) - 2⋅sin(a)⋅sin(c)⋅cos(e) - 2⋅sin(a)⋅sin(e)⋅cos(c) - 2⋅sin (c)⋅sin (e) - sin (c)⋅cos (e) 

                              2       2   ⎞    2      ⎛   2                                        
+ 2⋅sin(c)⋅sin(e)⋅cos(a) - sin (e)⋅cos (c)⎠⋅sin (x) + ⎝sin (a) + 2⋅sin(a)⋅sin(c)⋅cos(e) + 2⋅sin(a)⋅

                   2       2         2       2                                  2       2         2
sin(e)⋅cos(c) + sin (c)⋅sin (e) + sin (c)⋅cos (e) - 2⋅sin(c)⋅sin(e)⋅cos(a) + sin (e)⋅cos (c) + cos 

                                  2       2   ⎞    4         2       2   
(a) + 2⋅cos(a)⋅cos(c)⋅cos(e) + cos (c)⋅cos (e)⎠⋅sin (x) + sin (c)⋅sin (e)

所以我们看到,在sin(x)中,我们有一个4次多项式。这个问题比Matlab所发现的要好一些,因为它只有sin(x)的能力。可以使用根找到sin(x)的显式解决方案,例如,第一个是:

代码语言:javascript
复制
In [26]: r1, r2, r3, r4 = roots(expr2.subs(sin(x), y), y)

In [27]: r1
Out[27]: 
          _________________________________________________________________________________________
         ╱                         ________________________________________________________________
        ╱                         ╱                                                                
       ╱                         ╱                               2       2      (cos(2⋅a) + cos(2⋅c
      ╱                         ╱   - 8⋅(cos(-a + c + e) + 1)⋅sin (c)⋅sin (e) + ───────────────────
     ╱                        ╲╱                                                                   
-   ╱      - ──────────────────────────────────────────────────────────────────────────────────────
   ╱           ⎛   2                                                           2       2         2 
 ╲╱          2⋅⎝sin (a) + 2⋅sin(a)⋅sin(c)⋅cos(e) + 2⋅sin(a)⋅sin(e)⋅cos(c) + sin (c)⋅sin (e) + sin (

___________________________________________________________________________________________________
__________________________________________________________________________________________         
                                                                                        2          
) + cos(2⋅e) - 3⋅cos(-a + c + e) + cos(a - c + e) + cos(a + c - e) + cos(a + c + e) - 3)           
─────────────────────────────────────────────────────────────────────────────────────────          
                                  4                                                                
───────────────────────────────────────────────────────────────────────────────────────────────────
      2                                  2       2         2                                  2    
c)⋅cos (e) - 2⋅sin(c)⋅sin(e)⋅cos(a) + sin (e)⋅cos (c) + cos (a) + 2⋅cos(a)⋅cos(c)⋅cos(e) + cos (c)⋅

___________________________________________________________________________________________________
                                                                                                   
                                                                                                   
                                                                                                   
                                                                                                   
           -cos(2⋅a) - cos(2⋅c) - cos(2⋅e) + 3⋅cos(-a + c + e) - cos(a - c + e) - cos(a + c - e) - 
──────── + ────────────────────────────────────────────────────────────────────────────────────────
   2   ⎞                                            8⋅(cos(-a + c + e) + 1)                        
cos (e)⎠                                                                                           

___________________
                   
                   
                   
                   
cos(a + c + e) + 3 
────────────────── 

这是否是真实的或满足的不等式将取决于符号参数的值,所以我怀疑这些不等式是否可以用来选择根的4个表达式。也许可以使用多项式expr2回答不同的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72027073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档