Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit b67092c

Browse files
author
Matthias Koeppe
committed
Set_base.union, intersection, difference, symmetric_difference: Convert to Set if necessary
1 parent b8f5978 commit b67092c

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/sage/geometry/convex_set.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
# ****************************************************************************
1414

1515
from sage.structure.sage_object import SageObject
16+
from sage.sets.set import Set_base
1617
from sage.categories.sets_cat import EmptySetError
1718
from sage.misc.abstract_method import abstract_method
19+
from sage.rings.infinity import infinity
20+
from sage.rings.integer_ring import ZZ
1821

19-
class ConvexSet_base(SageObject):
22+
class ConvexSet_base(SageObject, Set_base):
2023
"""
2124
Abstract base class for convex sets.
2225
"""

src/sage/sets/set.py

+31-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from sage.structure.element import Element
4444
from sage.structure.parent import Parent, Set_generic
4545
from sage.structure.richcmp import richcmp_method, richcmp, rich_to_bool
46+
from sage.misc.classcall_metaclass import ClasscallMetaclass
4647

4748
from sage.categories.sets_cat import Sets
4849
from sage.categories.enumerated_sets import EnumeratedSets
@@ -229,7 +230,7 @@ def union(self, X):
229230
sage: sorted(Set(GF(7)) + Set(GF(3)), key=int)
230231
[0, 0, 1, 1, 2, 2, 3, 4, 5, 6]
231232
"""
232-
if isinstance(X, Set_generic):
233+
if isinstance(X, (Set_generic, Set_base)):
233234
if self is X:
234235
return self
235236
return Set_object_union(self, X)
@@ -258,7 +259,7 @@ def intersection(self, X):
258259
sage: X
259260
{}
260261
"""
261-
if isinstance(X, Set_generic):
262+
if isinstance(X, (Set_generic, Set_base)):
262263
if self is X:
263264
return self
264265
return Set_object_intersection(self, X)
@@ -287,7 +288,7 @@ def difference(self, X):
287288
sage: X
288289
{0, 1, 2, b, b + 1, b + 2, 2*b, 2*b + 1, 2*b + 2}
289290
"""
290-
if isinstance(X, Set_generic):
291+
if isinstance(X, (Set_generic, Set_base)):
291292
if self is X:
292293
return Set([])
293294
return Set_object_difference(self, X)
@@ -303,7 +304,7 @@ def symmetric_difference(self, X):
303304
sage: X
304305
{1, 2, 4}
305306
"""
306-
if isinstance(X, Set_generic):
307+
if isinstance(X, (Set_generic, Set_base)):
307308
if self is X:
308309
return Set([])
309310
return Set_object_symmetric_difference(self, X)
@@ -1179,7 +1180,7 @@ def symmetric_difference(self, other):
11791180
return Set_object_enumerated(self.set().symmetric_difference(other.set()))
11801181

11811182

1182-
class Set_object_binary(Set_object):
1183+
class Set_object_binary(Set_object, metaclass=ClasscallMetaclass):
11831184
r"""
11841185
An abstract common base class for sets defined by a binary operation (ex.
11851186
:class:`Set_object_union`, :class:`Set_object_intersection`,
@@ -1200,9 +1201,32 @@ class Set_object_binary(Set_object):
12001201
sage: Y = Set(ZZ)
12011202
sage: from sage.sets.set import Set_object_binary
12021203
sage: S = Set_object_binary(X, Y, "union", "\\cup"); S
1203-
Set-theoretic union of Set of elements of Vector space of dimension 2
1204-
over Rational Field and Set of elements of Integer Ring
1204+
Set-theoretic union of
1205+
Set of elements of Vector space of dimension 2 over Rational Field and
1206+
Set of elements of Integer Ring
12051207
"""
1208+
1209+
@staticmethod
1210+
def __classcall__(cls, X, Y, *args, **kwds):
1211+
r"""
1212+
Convert the operands to instances of :class:`Set_object` if necessary.
1213+
1214+
TESTS::
1215+
1216+
sage: from sage.sets.set import Set_object_binary
1217+
sage: X = QQ^2
1218+
sage: Y = ZZ
1219+
sage: Set_object_binary(X, Y, "union", "\\cup")
1220+
Set-theoretic union of
1221+
Set of elements of Vector space of dimension 2 over Rational Field and
1222+
Set of elements of Integer Ring
1223+
"""
1224+
if not isinstance(X, Set_object):
1225+
X = Set(X)
1226+
if not isinstance(Y, Set_object):
1227+
Y = Set(Y)
1228+
return type.__call__(cls, X, Y, *args, **kwds)
1229+
12061230
def __init__(self, X, Y, op, latex_op):
12071231
r"""
12081232
Initialization.

0 commit comments

Comments
 (0)