英文:
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('x')
In [14]: ConditionSet(x, x >1, S.Reals).is_disjoint(ConditionSet(x, x < 1, S.Reals))
Out[14]: False
In [15]: ConditionSet(x, x >1, S.Reals).intersect(ConditionSet(x, x < 1, S.Reals))
Out[15]: Intersection(ConditionSet(x, x > 1, Reals), ConditionSet(x, x < 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论