在sympy中评估两个ConditionSets是否不相交

huangapple go评论55阅读模式
英文:

Evaluating if two ConditionSets are disjoint in sympy

问题

在sympy中,要评估两个区间是否不相交很简单:

from sympy import Interval

# 示例1
Interval(0, 2).is_disjoint(Interval(-2, -1))
# 返回:True

# 示例2
Interval(0, 2).is_disjoint(Interval(1, 2))
# 返回:False

# 示例3
Interval(0, 2).intersect(Interval(-2, -1))
# 返回:EmptySet

然而,在处理两个ConditionSet对象的情况下,如何执行is_disjoint检查呢?

from sympy import ConditionSet, Symbol, S

x = Symbol('x')

# 示例4
ConditionSet(x, x > 1, S.Reals).is_disjoint(ConditionSet(x, x < 1, S.Reals))
# 返回:False

# 示例5
ConditionSet(x, x > 1, S.Reals).intersect(ConditionSet(x, x < 1, S.Reals))
# 返回:Intersection(ConditionSet(x, x > 1, Reals), ConditionSet(x, x < 1, Reals))

is_disjoint仅仅检查intersect是否等于EmptySet,而在ConditionSet的情况下,intersect仍然没有被计算。解决这个问题的最佳方法是什么呢?

英文:

In sympy, it is trivial to evaluate whether two Intervals are disjoint

In [3]: from sympy import Interval

In [4]: Interval(0, 2).is_disjoint(Interval(-2, -1))
Out[4]: True

In [5]: Interval(0, 2).is_disjoint(Interval(1, 2))
Out[5]: False

In [6]: Interval(0, 2).intersect(Interval(-2, -1))
Out[6]: EmptySet

However in the case when I have two ConditionSet objects, how can I perform the is_disjoint check ?

In [12]: from sympy import ConditionSet, Symbol, S

In [13]: x = Symbol(&#39;x&#39;)

In [14]: ConditionSet(x, x &gt;1, S.Reals).is_disjoint(ConditionSet(x, x &lt; 1, S.Reals))
Out[14]: False

In [15]: ConditionSet(x, x &gt;1, S.Reals).intersect(ConditionSet(x, x &lt; 1, S.Reals))
Out[15]: Intersection(ConditionSet(x, x &gt; 1, Reals), ConditionSet(x, x &lt; 1, Reals))

is_disjoint simply checks if intersect equates to EmptySet and in the case of the ConditionSet the intersect is still not evaluated.

What is the best way to resolve this?

答案1

得分: 1

这是一种方法
```python
在 [4] 中s = ConditionSet(x, x > 1, S.Reals).intersect(ConditionSet(x, x < 1, S.Reals))

在 [5] 中s.as_relational(y)
Out[5]: y > 1  - < y  y < 1  y < 

在 [6] 中s.as_relational(y).simplify()
Out[6]: False

<details>
<summary>英文:</summary>

This is one way:

In [4]: s = ConditionSet(x, x >1, S.Reals).intersect(ConditionSet(x, x < 1, S.Reals))

In [5]: s.as_relational(y)
Out[5]: y > 1 ∧ -∞ < y ∧ y < 1 ∧ y < ∞

In [6]: s.as_relational(y).simplify()
Out[6]: False


</details>



huangapple
  • 本文由 发表于 2023年6月11日 23:28:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定