Sympy 简明教程

SymPy - Sets

在数学中,一个集合是有序离散对象 مجموعة منظمة من كائنات منفصلة مرتبة جيدًا 的集合,这些对象可能是数字、人、字母或甚至是其他集合。集合也是 Python 中的内置类型之一。SymPy 提供集合模块。它包含不同类型的集合的定义,并具有执行交集、并集等集合运算的功能。

In mathematics, a set is a well-defined collection of distinct objects which may be numbers, people, letters of the alphabet, or even other sets. Set is also one of the built-in types in Python. SymPy provides sets module. It contains definitions of different types of set and has functionality to perform set operations such as intersection, union, etc.

集合是 SymPy 中其他任何类型的集合的基本类。请注意,它与 Python 的内置集合数据类型不同。区间类别表示实区间,其边界属性返回一个 FiniteSet 对象。

Set is a base class for any other type of set in SymPy. Note that it is different from built-in set data type of Python. Interval class represents real intervals and its boundary property returns a FiniteSet object.

>>> from sympy import Interval
>>> s=Interval(1,10).boundary
>>> type(s)

sympy.sets.sets.FiniteSet

sympy.sets.sets.FiniteSet

有限集合是离散数字的集合。它可以从列表或字符串等任何序列对象中获取。

FiniteSet is a collection of discrete numbers. It can be obtained from any sequence object such as list or string.

>>> from sympy import FiniteSet
>>> FiniteSet(range(5))

Output

$\lbrace\lbrace0,1,…​,4\rbrace\rbrace$

$\lbrace\lbrace0,1,…​,4\rbrace\rbrace$

>>> numbers=[1,3,5,2,8]
>>> FiniteSet(*numbers)

Output

$\lbrace1,2,3,5,8\rbrace$

$\lbrace1,2,3,5,8\rbrace$

>>> s="HelloWorld"
>>> FiniteSet(*s)

Output

{H,W,d,e,l,o,r}

{H,W,d,e,l,o,r}

请注意,在内置集合中,SymPy 的集合也是离散对象的集合。

Note that, as in built-in set, SymPy’s Set is also a collection of distinct objects.

ConditionSet 是满足给定条件的元素的集合

ConditionSet is a set of elements that satisfy a given condition

>>> from sympy import ConditionSet, Eq, Symbol
>>> x=Symbol('x')
>>> s=ConditionSet(x, Eq(x**2-2*x,0), Interval(1,10)) >>> s

Output

$\lbrace x\mid x\in[1,10]∧x^2 - 2x =0\rbrace$

$\lbrace x\mid x\in[1,10]∧x^2 - 2x =0\rbrace$

Union 是一个复合集。它包含两个集合中的所有元素。请注意,同时出现在这两个集合中的元素只在并集当中出现一次。

Union is a compound set. It includes all elements in two sets. Note that elements that are found in both, will appear only once in the Union.

>>> from sympy import Union
>>> l1=[3,1,5,7]
>>> l2=[9,7,2,1]
>>> a=FiniteSet(*l1)
>>> b=FiniteSet(*l2)
>>> Union(a,b)

Intersection 相反,只包含同时出现在两个集合中的那些元素。

Intersection on the other hand contains only those elements that are present in both.

>>> from sympy import Intersection
>>> Intersection(a,b)

ProductSet 对象表示两个集合中元素的笛卡尔积。

ProductSet object represents Cartesian product of elements in both sets.

>>> from sympy import ProductSet
>>> l1=[1,2]
>>> l2=[2,3]
>>> a=FiniteSet(*l1)
>>> b=FiniteSet(*l2)
>>> set(ProductSet(a,b))

Complement(a,b) 保留了集合 a 中的元素,排除了 b 集合中存在的元素。

Complement(a,b) retains elements in a excluding elements that are common with b set.

>>> from sympy import Complement
>>> l1=[3,1,5,7]
>>> l2=[9,7,2,1]
>>> a=FiniteSet(*l1)
>>> b=FiniteSet(*l2)
>>> Complement(a,b), Complement(b,a)

SymmetricDifference 集合只包含两个集合中不常见的元素。

SymmetricDifference set contains only uncommon elements in both sets.

>>> from sympy import SymmetricDifference
>>> l1=[3,1,5,7]
>>> l2=[9,7,2,1]
>>> a=FiniteSet(*l1)
>>> b=FiniteSet(*l2)
>>> SymmetricDifference(a,b)

Output

{2,3,5,9}

{2,3,5,9}