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

Commit

Permalink
src/sage/sets/set.py: Accept and handle 'category' arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoeppe committed Aug 17, 2022
1 parent 12be2d9 commit b19a1ff
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions src/sage/sets/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

from sage.categories.sets_cat import Sets
from sage.categories.enumerated_sets import EnumeratedSets
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets

import sage.rings.infinity

Expand Down Expand Up @@ -85,7 +86,7 @@ def has_finite_length(obj):
return True


def Set(X=None):
def Set(X=None, category=None):
r"""
Create the underlying set of ``X``.
Expand Down Expand Up @@ -187,22 +188,22 @@ def Set(X=None):
if X is None:
X = []
elif isinstance(X, CategoryObject):
if isinstance(X, Set_generic):
if isinstance(X, Set_generic) and category is None:
return X
elif X in Sets().Finite():
return Set_object_enumerated(X)
return Set_object_enumerated(X, category=category)
else:
return Set_object(X)
return Set_object(X, category=category)

if isinstance(X, Element) and not isinstance(X, Set_base):
raise TypeError("Element has no defined underlying set")

try:
X = frozenset(X)
except TypeError:
return Set_object(X)
return Set_object(X, category=category)
else:
return Set_object_enumerated(X)
return Set_object_enumerated(X, category=category)


class Set_base():
Expand Down Expand Up @@ -474,7 +475,7 @@ def __init__(self, X, category=None):
sage: type(Set(QQ))
<class 'sage.sets.set.Set_object_with_category'>
sage: Set(QQ).category()
Category of sets
Category of infinite sets
TESTS::
Expand All @@ -492,6 +493,15 @@ def __init__(self, X, category=None):

if category is None:
category = Sets()

if isinstance(X, CategoryObject):
if X in Sets().Finite():
category = category.Finite()
elif X in Sets().Infinite():
category = category.Infinite()
if X in Sets().Enumerated():
category = category.Enumerated()

Parent.__init__(self, category=category)
self.__object = X

Expand Down Expand Up @@ -830,7 +840,7 @@ class Set_object_enumerated(Set_object):
"""
A finite enumerated set.
"""
def __init__(self, X):
def __init__(self, X, category=None):
r"""
Initialize ``self``.
Expand All @@ -839,12 +849,12 @@ def __init__(self, X):
sage: S = Set(GF(19)); S
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}
sage: S.category()
Category of finite sets
Category of finite enumerated sets
sage: print(latex(S))
\left\{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18\right\}
sage: TestSuite(S).run()
"""
Set_object.__init__(self, X, category=Sets().Finite())
Set_object.__init__(self, X, category=FiniteEnumeratedSets().or_subcategory(category))

def random_element(self):
r"""
Expand Down Expand Up @@ -1267,7 +1277,7 @@ def __classcall__(cls, X, Y, *args, **kwds):
Y = Set(Y)
return type.__call__(cls, X, Y, *args, **kwds)

def __init__(self, X, Y, op, latex_op):
def __init__(self, X, Y, op, latex_op, category=None):
r"""
Initialization.
Expand All @@ -1284,7 +1294,7 @@ def __init__(self, X, Y, op, latex_op):
self._Y = Y
self._op = op
self._latex_op = latex_op
Set_object.__init__(self, self)
Set_object.__init__(self, self, category=category)

def _repr_(self):
r"""
Expand Down Expand Up @@ -1338,7 +1348,7 @@ class Set_object_union(Set_object_binary):
"""
A formal union of two sets.
"""
def __init__(self, X, Y):
def __init__(self, X, Y, category=None):
r"""
Initialize ``self``.
Expand All @@ -1354,7 +1364,7 @@ def __init__(self, X, Y):
sage: TestSuite(X).run()
"""
Set_object_binary.__init__(self, X, Y, "union", "\\cup")
Set_object_binary.__init__(self, X, Y, "union", "\\cup", category=category)

def is_finite(self):
r"""
Expand Down Expand Up @@ -1481,7 +1491,7 @@ class Set_object_intersection(Set_object_binary):
"""
Formal intersection of two sets.
"""
def __init__(self, X, Y):
def __init__(self, X, Y, category=None):
r"""
Initialize ``self``.
Expand All @@ -1499,7 +1509,7 @@ def __init__(self, X, Y):
True
sage: TestSuite(X).run()
"""
Set_object_binary.__init__(self, X, Y, "intersection", "\\cap")
Set_object_binary.__init__(self, X, Y, "intersection", "\\cap", category=category)

def is_finite(self):
r"""
Expand Down Expand Up @@ -1643,7 +1653,7 @@ class Set_object_difference(Set_object_binary):
"""
Formal difference of two sets.
"""
def __init__(self, X, Y):
def __init__(self, X, Y, category=None):
r"""
Initialize ``self``.
Expand All @@ -1658,7 +1668,7 @@ def __init__(self, X, Y):
sage: TestSuite(X).run()
"""
Set_object_binary.__init__(self, X, Y, "difference", "-")
Set_object_binary.__init__(self, X, Y, "difference", "-", category=category)

def is_finite(self):
r"""
Expand Down Expand Up @@ -1807,7 +1817,7 @@ class Set_object_symmetric_difference(Set_object_binary):
"""
Formal symmetric difference of two sets.
"""
def __init__(self, X, Y):
def __init__(self, X, Y, category=None):
r"""
Initialize ``self``.
Expand All @@ -1822,7 +1832,7 @@ def __init__(self, X, Y):
sage: TestSuite(X).run()
"""
Set_object_binary.__init__(self, X, Y, "symmetric difference", "\\bigtriangleup")
Set_object_binary.__init__(self, X, Y, "symmetric difference", "\\bigtriangleup", category=category)

def is_finite(self):
r"""
Expand Down

0 comments on commit b19a1ff

Please sign in to comment.