简体   繁体   English

集中的符号关系符号

[英]Sympy relational symbol in set

I have a FiniteSet and a symbol with which I want to associate a Relation such that the symbol is in the FiniteSet, is it possible with sympy? 我有一个FiniteSet和一个符号,要与它关联一个Relation,以便该符号位于FiniteSet中,sympy是否可能? symbol in FiniteSet does not return an expression, but instead evaluates it: symbol in FiniteSet中的symbol in FiniteSet不返回表达式,而是对其求值:

>>> from sympy import *   
>>> s = FiniteSet(range(0,3))
>>> x = symbols('x')
>>> x in s
False
>>> Eq(x,s)
x == {0, 1, 2}
>>> In(x,s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'In' is not defined

Edit: Thanks to ohe for telling me about Contains . 编辑:感谢ohe告诉我有关Contains I updated my version of sympy, by the way the syntax of FinitSet also changed in the update. 我通过更新中的FinitSet语法来更新了我的sympy版本。 I give the small example that I expected to work in the first place for the record: 我举一个我希望首先进行记录的小例子:

>>> from sympy import *   
>>> x = symbols('x')
>>> s = FiniteSet(*range(0,3))
>>> init_printing()
>>> Contains(x,s)
x ∈ {0, 1, 2}

您正在寻找的可能是Contains函数。

Your code doesn't work for me. 您的代码对我不起作用。 The expression 表达方式

x in s

raises an exception. 引发异常。 You have to assign a value to x first. 您必须先为x分配一个值。 Then you can just use "in". 然后,您可以只使用“ in”。

Like this: 像这样:

s = FiniteSet(range(0,3))
x = symbols('x')
x=3
x in s # False

Here is the complete setup: 这是完整的设置:

>>> from sympy import *
>>> s=FiniteSet(range(0,3))
>>> x=symbols("x")
>>> x in s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\lhk\Anaconda3\lib\site-packages\sympy\sets\sets.py", line 497, in __contains__
    raise TypeError('contains did not evaluate to a bool: %r' % symb)
TypeError: contains did not evaluate to a bool: Contains(x, {range(0, 3)})
>>> x=3
>>> x in s
False
>>> Contains(x,s)
False
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM