集合#
基本集合#
- class sympy.sets.sets.Set(*args)[源代码]#
任何类型集合的基类。
解释
这不是要直接用作物品的容器。它的行为不像内置的
set
见FiniteSet
为此。实际间隔由
Interval
类和集合的并集Union
上课。空集由EmptySet
类,并作为单一实例提供S.EmptySet
.- property boundary#
The boundary or frontier of a set.
解释
点x在集合S的边界上如果
x是S的闭包,即x的每个邻域都包含S中的一个点。
x不在S的内部,即不存在完全包含在S中的以x为中心的开集。
在S的外缘有点。如果S是开的,那么这些点实际上不需要包含在S中。
例如,区间的边界是其起点和终点。无论间隔是否打开,这都是正确的。
实例
>>> from sympy import Interval >>> Interval(0, 1).boundary {0, 1} >>> Interval(0, 1, True, False).boundary {0, 1}
- property closure#
属性方法,返回集合的闭包。闭包被定义为集合本身与其边界的并集。
实例
>>> from sympy import S, Interval >>> S.Reals.closure Reals >>> Interval(0, 1).closure Interval(0, 1)
- complement(universe)[源代码]#
在给定的宇宙中,自我的补足。
实例
>>> from sympy import Interval, S >>> Interval(0, 1).complement(S.Reals) Union(Interval.open(-oo, 0), Interval.open(1, oo))
>>> Interval(0, 1).complement(S.UniversalSet) Complement(UniversalSet, Interval(0, 1))
- contains(other)[源代码]#
Returns a SymPy value indicating whether
other
is contained inself
:true
if it is,false
if it is not, else an unevaluatedContains
expression (or, as in the case of ConditionSet and a union of FiniteSet/Intervals, an expression indicating the conditions for containment).实例
>>> from sympy import Interval, S >>> from sympy.abc import x
>>> Interval(0, 1).contains(0.5) True
As a shortcut it is possible to use the
in
operator, but that will raise an error unless an affirmative true or false is not obtained.>>> Interval(0, 1).contains(x) (0 <= x) & (x <= 1) >>> x in Interval(0, 1) Traceback (most recent call last): ... TypeError: did not evaluate to a bool: None
“in”的结果是bool,而不是SymPy值
>>> 1 in Interval(0, 2) True >>> _ is S.true False
- property inf#
The infimum of
self
.实例
>>> from sympy import Interval, Union >>> Interval(0, 1).inf 0 >>> Union(Interval(0, 1), Interval(2, 3)).inf 0
- property interior#
返回集合内部的属性方法。集合S的内部由不属于S边界的所有点组成。
实例
>>> from sympy import Interval >>> Interval(0, 1).interior Interval.open(0, 1) >>> Interval(0, 1).boundary.interior EmptySet
- intersect(other)[源代码]#
返回“self”和“other”的交集。
实例
>>> from sympy import Interval
>>> Interval(1, 3).intersect(Interval(1, 2)) Interval(1, 2)
>>> from sympy import imageset, Lambda, symbols, S >>> n, m = symbols('n m') >>> a = imageset(Lambda(n, 2*n), S.Integers) >>> a.intersect(imageset(Lambda(m, 2*m + 1), S.Integers)) EmptySet
- intersection(other)[源代码]#
Alias
intersect()
- property is_closed#
检查集合是否关闭的属性方法。
解释
如果一个集的补是开集,则它是闭集。reals子集的闭性是根据R及其标准拓扑确定的。
实例
>>> from sympy import Interval >>> Interval(0, 1).is_closed True
- is_disjoint(other)[源代码]#
Returns True if
self
andother
are disjoint.实例
>>> from sympy import Interval >>> Interval(0, 2).is_disjoint(Interval(1, 2)) False >>> Interval(0, 2).is_disjoint(Interval(3, 4)) True
工具书类
- property is_open#
属性方法来检查集合是否打开。
解释
当且仅当一个集合与其边界有一个空交集时,它才是开放的。特别地,实数的子集a是开的当且仅当它的每一个点都包含在a的一个子集的开区间中。
实例
>>> from sympy import S >>> S.Reals.is_open True >>> S.Rationals.is_open False
- is_proper_subset(other)[源代码]#
Returns True if
self
is a proper subset ofother
.实例
>>> from sympy import Interval >>> Interval(0, 0.5).is_proper_subset(Interval(0, 1)) True >>> Interval(0, 1).is_proper_subset(Interval(0, 1)) False
- is_proper_superset(other)[源代码]#
Returns True if
self
is a proper superset ofother
.实例
>>> from sympy import Interval >>> Interval(0, 1).is_proper_superset(Interval(0, 0.5)) True >>> Interval(0, 1).is_proper_superset(Interval(0, 1)) False
- is_subset(other)[源代码]#
Returns True if
self
is a subset ofother
.实例
>>> from sympy import Interval >>> Interval(0, 0.5).is_subset(Interval(0, 1)) True >>> Interval(0, 1).is_subset(Interval(0, 1, left_open=True)) False
- is_superset(other)[源代码]#
Returns True if
self
is a superset ofother
.实例
>>> from sympy import Interval >>> Interval(0, 0.5).is_superset(Interval(0, 1)) False >>> Interval(0, 1).is_superset(Interval(0, 1, left_open=True)) True
- isdisjoint(other)[源代码]#
Alias
is_disjoint()
- issubset(other)[源代码]#
Alias
is_subset()
- issuperset(other)[源代码]#
Alias
is_superset()
- property kind#
The kind of a Set
解释
Any
Set
will have kindSetKind
which is parametrised by the kind of the elements of the set. For example most sets are sets of numbers and will have kindSetKind(NumberKind)
. If elements of sets are different in kind than their kind willSetKind(UndefinedKind)
. Seesympy.core.kind.Kind
for an explanation of the kind system.实例
>>> from sympy import Interval, Matrix, FiniteSet, EmptySet, ProductSet, PowerSet
>>> FiniteSet(Matrix([1, 2])).kind SetKind(MatrixKind(NumberKind))
>>> Interval(1, 2).kind SetKind(NumberKind)
>>> EmptySet.kind SetKind()
A
sympy.sets.powerset.PowerSet
is a set of sets:>>> PowerSet({1, 2, 3}).kind SetKind(SetKind(NumberKind))
A
ProductSet
represents the set of tuples of elements of other sets. Its kind issympy.core.containers.TupleKind
parametrised by the kinds of the elements of those sets:>>> p = ProductSet(FiniteSet(1, 2), FiniteSet(3, 4)) >>> list(p) [(1, 3), (2, 3), (1, 4), (2, 4)] >>> p.kind SetKind(TupleKind(NumberKind, NumberKind))
When all elements of the set do not have same kind, the kind will be returned as
SetKind(UndefinedKind)
:>>> FiniteSet(0, Matrix([1, 2])).kind SetKind(UndefinedKind)
The kind of the elements of a set are given by the
element_kind
attribute ofSetKind
:>>> Interval(1, 2).kind.element_kind NumberKind
参见
NumberKind
,sympy.core.kind.UndefinedKind
,sympy.core.containers.TupleKind
,MatrixKind
,sympy.matrices.expressions.sets.MatrixSet
,sympy.sets.conditionset.ConditionSet
,Rationals
,Naturals
,Integers
,sympy.sets.fancysets.ImageSet
,sympy.sets.fancysets.Range
,sympy.sets.fancysets.ComplexRegion
,sympy.sets.powerset.PowerSet
,sympy.sets.sets.ProductSet
,sympy.sets.sets.Interval
,sympy.sets.sets.Union
,sympy.sets.sets.Intersection
,sympy.sets.sets.Complement
,sympy.sets.sets.EmptySet
,sympy.sets.sets.UniversalSet
,sympy.sets.sets.FiniteSet
,sympy.sets.sets.SymmetricDifference
,sympy.sets.sets.DisjointUnion
- property measure#
The (Lebesgue) measure of
self
.实例
>>> from sympy import Interval, Union >>> Interval(0, 1).measure 1 >>> Union(Interval(0, 1), Interval(2, 3)).measure 2
- powerset()[源代码]#
Find the Power set of
self
.实例
>>> from sympy import EmptySet, FiniteSet, Interval
一个空的动力装置:
>>> A = EmptySet >>> A.powerset() {EmptySet}
有限集的幂集:
>>> A = FiniteSet(1, 2) >>> a, b, c = FiniteSet(1), FiniteSet(2), FiniteSet(1, 2) >>> A.powerset() == FiniteSet(a, b, c, EmptySet) True
间隔功率装置:
>>> Interval(1, 2).powerset() PowerSet(Interval(1, 2))
工具书类
- property sup#
The supremum of
self
.实例
>>> from sympy import Interval, Union >>> Interval(0, 1).sup 1 >>> Union(Interval(0, 1), Interval(2, 3)).sup 3
- symmetric_difference(other)[源代码]#
Returns symmetric difference of
self
andother
.实例
>>> from sympy import Interval, S >>> Interval(1, 3).symmetric_difference(S.Reals) Union(Interval.open(-oo, 1), Interval.open(3, oo)) >>> Interval(1, 10).symmetric_difference(S.Reals) Union(Interval.open(-oo, 1), Interval.open(10, oo))
>>> from sympy import S, EmptySet >>> S.Reals.symmetric_difference(EmptySet) Reals
工具书类
- union(other)[源代码]#
Returns the union of
self
andother
.实例
As a shortcut it is possible to use the
+
operator:>>> from sympy import Interval, FiniteSet >>> Interval(0, 1).union(Interval(2, 3)) Union(Interval(0, 1), Interval(2, 3)) >>> Interval(0, 1) + Interval(2, 3) Union(Interval(0, 1), Interval(2, 3)) >>> Interval(1, 2, True, True) + FiniteSet(2, 3) Union({3}, Interval.Lopen(1, 2))
Similarly it is possible to use the
-
operator for set differences:>>> Interval(0, 2) - Interval(0, 1) Interval.Lopen(1, 2) >>> Interval(1, 3) - FiniteSet(2) Union(Interval.Ropen(1, 2), Interval.Lopen(2, 3))
- sympy.sets.sets.imageset(*args)[源代码]#
返回正在转换的集合的图像
f
.解释
If this function cannot compute the image, it returns an unevaluated ImageSet object.
\[\{f(x)\mid x\在\mathrm{self}\}\]实例
>>> from sympy import S, Interval, imageset, sin, Lambda >>> from sympy.abc import x
>>> imageset(x, 2*x, Interval(0, 2)) Interval(0, 4)
>>> imageset(lambda x: 2*x, Interval(0, 2)) Interval(0, 4)
>>> imageset(Lambda(x, sin(x)), Interval(-2, 1)) ImageSet(Lambda(x, sin(x)), Interval(-2, 1))
>>> imageset(sin, Interval(-2, 1)) ImageSet(Lambda(x, sin(x)), Interval(-2, 1)) >>> imageset(lambda y: x + y, Interval(-2, 1)) ImageSet(Lambda(y, x + y), Interval(-2, 1))
应用于整数集合的表达式被简化,以尽可能少地显示负数,并且线性表达式被转换为规范形式。如果这不可取,则应使用未评估的图像集。
>>> imageset(x, -2*x + 5, S.Integers) ImageSet(Lambda(x, 2*x + 1), Integers)
初等集合#
- class sympy.sets.sets.Interval(start, end, left_open=False, right_open=False)[源代码]#
将实间隔表示为一个集合。
- 用途:
Returns an interval with end points
start
andend
.For
left_open=True
(defaultleft_open
isFalse
) the interval will be open on the left. Similarly, forright_open=True
the interval will be open on the right.
实例
>>> from sympy import Symbol, Interval >>> Interval(0, 1) Interval(0, 1) >>> Interval.Ropen(0, 1) Interval.Ropen(0, 1) >>> Interval.Ropen(0, 1) Interval.Ropen(0, 1) >>> Interval.Lopen(0, 1) Interval.Lopen(0, 1) >>> Interval.open(0, 1) Interval.open(0, 1)
>>> a = Symbol('a', real=True) >>> Interval(0, a) Interval(0, a)
笔记
只支持真正的端点
Interval(a, b)
with \(a > b\) will return the empty setUse the
evalf()
method to turn an Interval into an mpmathmpi
interval instance
工具书类
- property end#
The right end point of the interval.
This property takes the same value as the
sup
property.实例
>>> from sympy import Interval >>> Interval(0, 1).end 1
- property is_left_unbounded#
返回
True
如果左端点是负无穷大。
- property is_right_unbounded#
返回
True
如果右端点是正无穷大。
- property left_open#
True if interval is left-open.
实例
>>> from sympy import Interval >>> Interval(0, 1, left_open=True).left_open True >>> Interval(0, 1, left_open=False).left_open False
- property right_open#
True if interval is right-open.
实例
>>> from sympy import Interval >>> Interval(0, 1, right_open=True).right_open True >>> Interval(0, 1, right_open=False).right_open False
- property start#
The left end point of the interval.
This property takes the same value as the
inf
property.实例
>>> from sympy import Interval >>> Interval(0, 1).start 0
- class sympy.sets.sets.FiniteSet(*args, **kwargs)[源代码]#
Represents a finite set of Sympy expressions.
实例
>>> from sympy import FiniteSet, Symbol, Interval, Naturals0 >>> FiniteSet(1, 2, 3, 4) {1, 2, 3, 4} >>> 3 in FiniteSet(1, 2, 3, 4) True >>> FiniteSet(1, (1, 2), Symbol('x')) {1, x, (1, 2)} >>> FiniteSet(Interval(1, 2), Naturals0, {1, 2}) FiniteSet({1, 2}, Interval(1, 2), Naturals0) >>> members = [1, 2, 3, 4] >>> f = FiniteSet(*members) >>> f {1, 2, 3, 4} >>> f - FiniteSet(2) {1, 3, 4} >>> f + FiniteSet(2, 5) {1, 2, 3, 4, 5}
工具书类
复合集#
- class sympy.sets.sets.Union(*args, **kwargs)[源代码]#
将集合的并集表示为
Set
.实例
>>> from sympy import Union, Interval >>> Union(Interval(1, 2), Interval(3, 4)) Union(Interval(1, 2), Interval(3, 4))
如果可能,联合构造函数将始终尝试合并重叠的间隔。例如:
>>> Union(Interval(1, 2), Interval(2, 3)) Interval(1, 3)
参见
工具书类
- class sympy.sets.sets.Intersection(*args, evaluate=None)[源代码]#
将集合的交集表示为
Set
.实例
>>> from sympy import Intersection, Interval >>> Intersection(Interval(1, 3), Interval(2, 4)) Interval(2, 3)
我们经常使用.intersect方法
>>> Interval(1,3).intersect(Interval(2,4)) Interval(2, 3)
参见
工具书类
- class sympy.sets.sets.ProductSet(*sets, **assumptions)[源代码]#
表示集合的笛卡尔积。
解释
返回给定多个集合作为iterable或单个参数的笛卡尔积。
Can use
*
operator on any sets for convenient shorthand.实例
>>> from sympy import Interval, FiniteSet, ProductSet >>> I = Interval(0, 5); S = FiniteSet(1, 2, 3) >>> ProductSet(I, S) ProductSet(Interval(0, 5), {1, 2, 3})
>>> (2, 2) in ProductSet(I, S) True
>>> Interval(0, 1) * Interval(0, 1) # The unit square ProductSet(Interval(0, 1), Interval(0, 1))
>>> coin = FiniteSet('H', 'T') >>> set(coin**2) {(H, H), (H, T), (T, H), (T, T)}
笛卡尔积不是交换或结合的,例如:
>>> I*S == S*I False >>> (I*I)*I == I*(I*I) False
笔记
将大多数操作传递给参数集
工具书类
- property is_iterable#
一种属性方法,用于测试集合是否可编辑。如果set是iterable,则返回True,否则返回False。
实例
>>> from sympy import FiniteSet, Interval >>> I = Interval(0, 1) >>> A = FiniteSet(1, 2, 3, 4, 5) >>> I.is_iterable False >>> A.is_iterable True
- class sympy.sets.sets.Complement(a, b, evaluate=True)[源代码]#
表示一个集合与另一个集合的集合差或相对补码。
\[A - B = \{x \in A \mid x \notin B\}\]实例
>>> from sympy import Complement, FiniteSet >>> Complement(FiniteSet(0, 1, 2), FiniteSet(1)) {0, 2}
参见
工具书类
- static reduce(A, B)[源代码]#
简化a
Complement
.
- class sympy.sets.sets.SymmetricDifference(a, b, evaluate=True)[源代码]#
表示元素集,这些元素位于任一集合中,而不在它们的交集中。
实例
>>> from sympy import SymmetricDifference, FiniteSet >>> SymmetricDifference(FiniteSet(1, 2, 3), FiniteSet(3, 4, 5)) {1, 2, 4, 5}
参见
工具书类
- class sympy.sets.sets.DisjointUnion(*sets)[源代码]#
Represents the disjoint union (also known as the external disjoint union) of a finite number of sets.
实例
>>> from sympy import DisjointUnion, FiniteSet, Interval, Union, Symbol >>> A = FiniteSet(1, 2, 3) >>> B = Interval(0, 5) >>> DisjointUnion(A, B) DisjointUnion({1, 2, 3}, Interval(0, 5)) >>> DisjointUnion(A, B).rewrite(Union) Union(ProductSet({1, 2, 3}, {0}), ProductSet(Interval(0, 5), {1})) >>> C = FiniteSet(Symbol('x'), Symbol('y'), Symbol('z')) >>> DisjointUnion(C, C) DisjointUnion({x, y, z}, {x, y, z}) >>> DisjointUnion(C, C).rewrite(Union) ProductSet({x, y, z}, {0, 1})
工具书类
单粒子集#
特殊套装#
- class sympy.sets.fancysets.Rationals[源代码]#
Represents the rational numbers. This set is also available as the singleton
S.Rationals
.实例
>>> from sympy import S >>> S.Half in S.Rationals True >>> iterable = iter(S.Rationals) >>> [next(iterable) for i in range(12)] [0, 1, -1, 1/2, 2, -1/2, -2, 1/3, 3, -1/3, -3, 2/3]
- class sympy.sets.fancysets.Naturals[源代码]#
Represents the natural numbers (or counting numbers) which are all positive integers starting from 1. This set is also available as the singleton
S.Naturals
.实例
>>> from sympy import S, Interval, pprint >>> 5 in S.Naturals True >>> iterable = iter(S.Naturals) >>> next(iterable) 1 >>> next(iterable) 2 >>> next(iterable) 3 >>> pprint(S.Naturals.intersect(Interval(0, 10))) {1, 2, ..., 10}
- class sympy.sets.fancysets.Integers[源代码]#
Represents all integers: positive, negative and zero. This set is also available as the singleton
S.Integers
.实例
>>> from sympy import S, Interval, pprint >>> 5 in S.Naturals True >>> iterable = iter(S.Integers) >>> next(iterable) 0 >>> next(iterable) 1 >>> next(iterable) -1 >>> next(iterable) 2
>>> pprint(S.Integers.intersect(Interval(-4, 4))) {-4, -3, ..., 4}
- class sympy.sets.fancysets.Reals[源代码]#
Represents all real numbers from negative infinity to positive infinity, including all integer, rational and irrational numbers. This set is also available as the singleton
S.Reals
.实例
>>> from sympy import S, Rational, pi, I >>> 5 in S.Reals True >>> Rational(-1, 2) in S.Reals True >>> pi in S.Reals True >>> 3*I in S.Reals False >>> S.Reals.contains(pi) True
- class sympy.sets.fancysets.Complexes[源代码]#
The
Set
of all complex numbers实例
>>> from sympy import S, I >>> S.Complexes Complexes >>> 1 + I in S.Complexes True
参见
- class sympy.sets.fancysets.ImageSet(flambda, *sets)[源代码]#
数学函数下集合的象。转换必须以Lambda函数的形式给出,该函数的参数与它所操作的集合中的元素的数目相同,例如,当作用于整数集时,有1个参数,当作用于复杂区域时,有2个参数。
This function is not normally called directly, but is called from
imageset
.实例
>>> from sympy import Symbol, S, pi, Dummy, Lambda >>> from sympy import FiniteSet, ImageSet, Interval
>>> x = Symbol('x') >>> N = S.Naturals >>> squares = ImageSet(Lambda(x, x**2), N) # {x**2 for x in N} >>> 4 in squares True >>> 5 in squares False
>>> FiniteSet(0, 1, 2, 3, 4, 5, 6, 7, 9, 10).intersect(squares) {1, 4, 9}
>>> square_iterable = iter(squares) >>> for i in range(4): ... next(square_iterable) 1 4 9 16
If you want to get value for \(x\) = 2, 1/2 etc. (Please check whether the \(x\) value is in
base_set
or not before passing it as args)>>> squares.lamda(2) 4 >>> squares.lamda(S(1)/2) 1/4
>>> n = Dummy('n') >>> solutions = ImageSet(Lambda(n, n*pi), S.Integers) # solutions of sin(x) = 0 >>> dom = Interval(-1, 1) >>> dom.intersect(solutions) {0}
- class sympy.sets.fancysets.Range(*args)[源代码]#
Represents a range of integers. Can be called as
Range(stop)
,Range(start, stop)
, orRange(start, stop, step)
; whenstep
is not given it defaults to 1.Range(stop)
is the same asRange(0, stop, 1)
and the stop value (just as for Python ranges) is not included in the Range values.>>> from sympy import Range >>> list(Range(3)) [0, 1, 2]
步骤也可以是负数:
>>> list(Range(10, 0, -2)) [10, 8, 6, 4, 2]
停止值是规范的,因此等效范围始终具有相同的参数:
>>> Range(0, 10, 3) Range(0, 12, 3)
允许无限范围。
oo
和-oo
不包含在集合中 (Range
始终是Integers
). 如果起点是无限的,那么最终值是stop - step
. 要迭代这样的范围,需要反转:>>> from sympy import oo >>> r = Range(-oo, 1) >>> r[-1] 0 >>> next(iter(r)) Traceback (most recent call last): ... TypeError: Cannot iterate over Range with infinite start >>> next(iter(r.reversed)) 0
Although
Range
is aSet
(and supports the normal set operations) it maintains the order of the elements and can be used in contexts whererange
would be used.>>> from sympy import Interval >>> Range(0, 10, 2).intersect(Interval(3, 7)) Range(4, 8, 2) >>> list(_) [4, 6]
尽管对一个范围的切片总是返回一个范围(可能是空的),但是从任何一个空的交集返回一个空集:
>>> Range(3)[:0] Range(0, 0, 1) >>> Range(3).intersect(Interval(4, oo)) EmptySet >>> Range(3).intersect(Range(4, oo)) EmptySet
Range将接受符号参数,但对除显示范围之外的任何操作的支持非常有限:
>>> from sympy import Symbol, pprint >>> from sympy.abc import i, j, k >>> Range(i, j, k).start i >>> Range(i, j, k).inf Traceback (most recent call last): ... ValueError: invalid method for symbolic range
使用整数符号将获得更好的成功:
>>> n = Symbol('n', integer=True) >>> r = Range(n, n + 20, 3) >>> r.inf n >>> pprint(r) {n, n + 3, ..., n + 18}
- property reversed#
以相反的顺序返回一个相等的范围。
实例
>>> from sympy import Range >>> Range(10).reversed Range(9, -1, -1)
- class sympy.sets.fancysets.ComplexRegion(sets, polar=False)[源代码]#
表示所有复数的集合。它可以用标准的极坐标和直角坐标表示复杂平面的区域。
Polar Form Input is in the form of the ProductSet or Union of ProductSets of the intervals of
r
andtheta
, and use the flagpolar=True
.\[Z = \{z \in \mathbb{C} \mid z = r\times (\cos(\theta) + I\sin(\theta)), r \in [\texttt{r}], \theta \in [\texttt{theta}]\}\]Rectangular Form Input is in the form of the ProductSet or Union of ProductSets of interval of x and y, the real and imaginary parts of the Complex numbers in a plane. Default input type is in rectangular form.
\[Z = \{z \in \mathbb{C} \mid z = x + Iy, x \in [\operatorname{re}(z)], y \in [\operatorname{im}(z)]\}\]实例
>>> from sympy import ComplexRegion, Interval, S, I, Union >>> a = Interval(2, 3) >>> b = Interval(4, 6) >>> c1 = ComplexRegion(a*b) # Rectangular Form >>> c1 CartesianComplexRegion(ProductSet(Interval(2, 3), Interval(4, 6)))
c1表示复平面上由四个顶点的坐标(2,4),(3,4),(3,6)和(2,6)包围的矩形区域。
>>> c = Interval(1, 8) >>> c2 = ComplexRegion(Union(a*b, b*c)) >>> c2 CartesianComplexRegion(Union(ProductSet(Interval(2, 3), Interval(4, 6)), ProductSet(Interval(4, 6), Interval(1, 8))))
c2表示复平面上两个矩形区域的并集。其中一个被c1的坐标包围,另一个被坐标(4,1),(6,1),(6,8)和(4,8)包围。
>>> 2.5 + 4.5*I in c1 True >>> 2.5 + 6.5*I in c1 False
>>> r = Interval(0, 1) >>> theta = Interval(0, 2*S.Pi) >>> c2 = ComplexRegion(r*theta, polar=True) # Polar Form >>> c2 # unit Disk PolarComplexRegion(ProductSet(Interval(0, 1), Interval.Ropen(0, 2*pi)))
c2表示单位圆盘内以原点为中心的复平面区域。
>>> 0.5 + 0.5*I in c2 True >>> 1 + 2*I in c2 False
>>> unit_disk = ComplexRegion(Interval(0, 1)*Interval(0, 2*S.Pi), polar=True) >>> upper_half_unit_disk = ComplexRegion(Interval(0, 1)*Interval(0, S.Pi), polar=True) >>> intersection = unit_disk.intersect(upper_half_unit_disk) >>> intersection PolarComplexRegion(ProductSet(Interval(0, 1), Interval(0, pi))) >>> intersection == upper_half_unit_disk True
- property a_interval#
返回间隔的并集 \(x\) 当自我是矩形的时候,或者是间隔的并集 \(r\) 当自我处于极性状态时。
实例
>>> from sympy import Interval, ComplexRegion, Union >>> a = Interval(2, 3) >>> b = Interval(4, 5) >>> c = Interval(1, 7) >>> C1 = ComplexRegion(a*b) >>> C1.a_interval Interval(2, 3) >>> C2 = ComplexRegion(Union(a*b, b*c)) >>> C2.a_interval Union(Interval(2, 3), Interval(4, 5))
- property b_interval#
返回间隔的并集 \(y\) 当自我是矩形的时候,或者是间隔的并集 \(theta\) 当自我处于极性状态时。
实例
>>> from sympy import Interval, ComplexRegion, Union >>> a = Interval(2, 3) >>> b = Interval(4, 5) >>> c = Interval(1, 7) >>> C1 = ComplexRegion(a*b) >>> C1.b_interval Interval(4, 5) >>> C2 = ComplexRegion(Union(a*b, b*c)) >>> C2.b_interval Interval(1, 7)
- classmethod from_real(sets)[源代码]#
将给定的实数子集转换为复数区域。
实例
>>> from sympy import Interval, ComplexRegion >>> unit = Interval(0,1) >>> ComplexRegion.from_real(unit) CartesianComplexRegion(ProductSet(Interval(0, 1), {0}))
- property psets#
返回self的set(ProductSets)输入的元组。
实例
>>> from sympy import Interval, ComplexRegion, Union >>> a = Interval(2, 3) >>> b = Interval(4, 5) >>> c = Interval(1, 7) >>> C1 = ComplexRegion(a*b) >>> C1.psets (ProductSet(Interval(2, 3), Interval(4, 5)),) >>> C2 = ComplexRegion(Union(a*b, b*c)) >>> C2.psets (ProductSet(Interval(2, 3), Interval(4, 5)), ProductSet(Interval(4, 5), Interval(1, 7)))
- property sets#
将原始输入集返回给自身。
实例
>>> from sympy import Interval, ComplexRegion, Union >>> a = Interval(2, 3) >>> b = Interval(4, 5) >>> c = Interval(1, 7) >>> C1 = ComplexRegion(a*b) >>> C1.sets ProductSet(Interval(2, 3), Interval(4, 5)) >>> C2 = ComplexRegion(Union(a*b, b*c)) >>> C2.sets Union(ProductSet(Interval(2, 3), Interval(4, 5)), ProductSet(Interval(4, 5), Interval(1, 7)))
- class sympy.sets.fancysets.CartesianComplexRegion(sets)[源代码]#
表示复平面的正方形区域的集合。
\[Z = \{z \in \mathbb{C} \mid z = x + Iy, x \in [\operatorname{re}(z)], y \in [\operatorname{im}(z)]\}\]实例
>>> from sympy import ComplexRegion, I, Interval >>> region = ComplexRegion(Interval(1, 3) * Interval(4, 6)) >>> 2 + 5*I in region True >>> 5*I in region False
- class sympy.sets.fancysets.PolarComplexRegion(sets)[源代码]#
表示复平面极性区域的集合。
\[Z = \{z \in \mathbb{C} \mid z = r\times (\cos(\theta) + I\sin(\theta)), r \in [\texttt{r}], \theta \in [\texttt{theta}]\}\]实例
>>> from sympy import ComplexRegion, Interval, oo, pi, I >>> rset = Interval(0, oo) >>> thetaset = Interval(0, pi) >>> upper_half_plane = ComplexRegion(rset * thetaset, polar=True) >>> 1 + I in upper_half_plane True >>> 1 - I in upper_half_plane False
- sympy.sets.fancysets.normalize_theta_set(theta)[源代码]#
Normalize a Real Set \(theta\) in the interval \([0, 2\pi)\). It returns a normalized value of theta in the Set. For Interval, a maximum of one cycle \([0, 2\pi]\), is returned i.e. for theta equal to \([0, 10\pi]\), returned normalized value would be \([0, 2\pi)\). As of now intervals with end points as non-multiples of
pi
is not supported.- 加薪:
NotImplementedError
规范化theta集的算法尚未实现。
ValueError
输入是无效的。
RuntimeError
这是一个bug,请向github问题跟踪者报告。
实例
>>> from sympy.sets.fancysets import normalize_theta_set >>> from sympy import Interval, FiniteSet, pi >>> normalize_theta_set(Interval(9*pi/2, 5*pi)) Interval(pi/2, pi) >>> normalize_theta_set(Interval(-3*pi/2, pi/2)) Interval.Ropen(0, 2*pi) >>> normalize_theta_set(Interval(-pi/2, pi/2)) Union(Interval(0, pi/2), Interval.Ropen(3*pi/2, 2*pi)) >>> normalize_theta_set(Interval(-4*pi, 3*pi)) Interval.Ropen(0, 2*pi) >>> normalize_theta_set(Interval(-3*pi/2, -pi/2)) Interval(pi/2, 3*pi/2) >>> normalize_theta_set(FiniteSet(0, pi, 3*pi)) {0, pi}
动力装置#
- class sympy.sets.powerset.PowerSet(arg, evaluate=None)[源代码]#
表示动力装置的符号对象。
- 参数:
arg :设置
要掌权的人。
评价 布尔
控制计算的标志。
如果对有限集禁用求值,则可以利用子集测试作为成员测试。
笔记
动力装置 \(\mathcal{{P}}(S)\) 定义为包含 \(S\) .
如果 \(S\) 是一个有限集,它的幂集 \(2^{{\left| S \right|}}\) 元素,其中 \(\left| S \right|\) 表示的基数 \(S\) .
实例
>>> from sympy import PowerSet, S, FiniteSet
有限集的幂集:
>>> PowerSet(FiniteSet(1, 2, 3)) PowerSet({1, 2, 3})
一个空的动力装置:
>>> PowerSet(S.EmptySet) PowerSet(EmptySet) >>> PowerSet(PowerSet(S.EmptySet)) PowerSet(PowerSet(EmptySet))
无穷集的幂集:
>>> PowerSet(S.Reals) PowerSet(Reals)
用显式形式计算有限集的幂集:
>>> PowerSet(FiniteSet(1, 2, 3)).rewrite(FiniteSet) FiniteSet(EmptySet, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3})
工具书类
条件集#
- class sympy.sets.conditionset.ConditionSet(sym, condition, base_set=UniversalSet)[源代码]#
满足给定条件的一组元素。
\[\{x \mid \textrm{condition}(x) = \texttt{True}, x \in S\}\]实例
>>> from sympy import Symbol, S, ConditionSet, pi, Eq, sin, Interval >>> from sympy.abc import x, y, z
>>> sin_sols = ConditionSet(x, Eq(sin(x), 0), Interval(0, 2*pi)) >>> 2*pi in sin_sols True >>> pi/2 in sin_sols False >>> 3*pi in sin_sols False >>> 5 in ConditionSet(x, x**2 > 4, S.Reals) True
如果该值不在基集中,则结果为false:
>>> 5 in ConditionSet(x, x**2 > 4, Interval(2, 4)) False
笔记
应避免使用带有假设的符号,否则可在不考虑集合的情况下评估条件:
>>> n = Symbol('n', negative=True) >>> cond = (n > 0); cond False >>> ConditionSet(n, cond, S.Integers) EmptySet
使用只能更改自由符号 \(subs\) :
>>> c = ConditionSet(x, x < 1, {x, z}) >>> c.subs(x, y) ConditionSet(x, x < 1, {y, z})
检查是否
pi
是在c
使用:>>> pi in c False
如果未指定基集,则表示通用集:
>>> ConditionSet(x, x < 1).base_set UniversalSet
只能使用符号或类似符号的表达式:
>>> ConditionSet(x + 1, x + 1 < 1, S.Integers) Traceback (most recent call last): ... ValueError: non-symbol dummy not recognized in condition
当基集是条件集时,如果可能,将统一符号,并优先选择最外层的符号:
>>> ConditionSet(x, x < y, ConditionSet(z, z + y < 2, S.Integers)) ConditionSet(x, (x < y) & (x + y < 2), Integers)
- class sympy.sets.conditionset.Contains(x, s, evaluate=None)[源代码]#
Asserts that x is an element of the set S.
实例
>>> from sympy import Symbol, Integer, S, Contains >>> Contains(Integer(2), S.Integers) True >>> Contains(Integer(-2), S.Naturals) False >>> i = Symbol('i', integer=True) >>> Contains(i, S.Naturals) Contains(i, Naturals)
工具书类
SetKind#
- class sympy.sets.conditionset.SetKind(element_kind=None)[源代码]#
SetKind is kind for all Sets
Every instance of Set will have kind
SetKind
parametrised by the kind of the elements of theSet
. The kind of the elements might beNumberKind
, orTupleKind
or something else. When not all elements have the same kind then the kind of the elements will be given asUndefinedKind
.- 参数:
element_kind: Kind (optional)
The kind of the elements of the set. In a well defined set all elements will have the same kind. Otherwise the kind should
sympy.core.kind.UndefinedKind
. Theelement_kind
argument is optional but should only be omitted in the case ofEmptySet
whose kind is simplySetKind()
实例
>>> from sympy import Interval >>> Interval(1, 2).kind SetKind(NumberKind) >>> Interval(1,2).kind.element_kind NumberKind
集合上的迭代#
对于集合工会, \(\{{a, b\}} \cup \{{x, y\}}\) 可以当作 \(\{{a, b, x, y\}}\) 不管元素的显著性如何,对于集合交集,假设 \(\{{a, b\}} \cap \{{x, y\}}\) 是 \(\varnothing\) 或 \(\{{a, b \}}\) 不会总是有效的,因为 \(a\) , \(b\) , \(x\) 或 \(y\) 可能是也可能不是交叉点的元素。
迭代包含交集、补码或对称差分的集合的元素会产生(可能是重复的)集合元素,前提是所有元素都是集合的元素。如果任何元素不能被确定为集合的成员,那么迭代将给出 TypeError
. 在相同的情况下 x in y
会出错。
这样的实现有一些原因,即使它破坏了与python set iterator工作方式的一致性。我们把理解力放在心上 FiniteSet(*s)
从现有的sympy集可能是一个常见的用法。这种方法会使 FiniteSet(*s)
与任何符号集处理方法一致,如 FiniteSet(*simplify(s))
.