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

Commit

Permalink
SageSet: Finish docstrings; handle symbolic _contains
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Jun 9, 2021
1 parent 3cac256 commit eef604e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/sage/categories/sets_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1690,12 +1690,17 @@ def _sympy_(self):
"""
Return an instance of a subclass of SymPy ``Set`` corresponding to ``self``.
The default implementation creates an instance of
:class:`~sage.interfaces.sympy_wrapper`.
EXAMPLES::
sage: F = FiniteEnumeratedSets().example(); F
An example of a finite enumerated set: {1,2,3}
sage: sF = F._sympy_(); sF
SageSet(An example of a finite enumerated set: {1,2,3})
sage: sF.is_finite_set
True
sage: bool(sF)
True
sage: len(sF)
Expand All @@ -1708,6 +1713,14 @@ def _sympy_(self):
sage: RR._sympy_().is_finite_set
False
sage: F = Set([1, 2])
sage: F is Set([1, 2])
False
sage: sF = F._sympy_(); sF
SageSet({1, 2})
sage: sF._sage_() is F
True
"""
from sage.interfaces.sympy_wrapper import SageSet
return SageSet(self)
Expand Down
32 changes: 30 additions & 2 deletions src/sage/interfaces/sympy_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class SageSet(Set):
"""

def __new__(cls, sage_set):
r"""
Construct a wrapper for a Sage set.
"""
return Basic.__new__(cls, sage_set)

def _sage_(self):
Expand All @@ -67,6 +70,8 @@ def _sage_(self):
@property
def is_empty(self):
r"""
Return whether the set ``self`` is empty.
EXAMPLES::
sage: Empty = Set([])
Expand All @@ -79,6 +84,8 @@ def is_empty(self):
@property
def is_finite_set(self):
r"""
Return whether the set ``self`` is finite.
EXAMPLES::
sage: W = WeylGroup(["A",1,1])
Expand All @@ -92,6 +99,8 @@ def is_finite_set(self):
@property
def is_iterable(self):
r"""
Return whether the set ``self`` is iterable.
EXAMPLES::
sage: W = WeylGroup(["A",1,1])
Expand All @@ -105,6 +114,8 @@ def is_iterable(self):

def __iter__(self):
r"""
Iterator for the set ``self``.
EXAMPLES::
sage: sPrimes = Primes()._sympy_(); sPrimes
Expand All @@ -116,19 +127,36 @@ def __iter__(self):
for element in self._sage_():
yield sympify(element)

def _contains(self, other):
def _contains(self, element):
"""
Return whether ``element`` is an element of the set ``self``.
EXAMPLES::
sage: sPrimes = Primes()._sympy_(); sPrimes
SageSet(Set of all prime numbers: 2, 3, 5, 7, ...)
sage: 91 in sPrimes
False
sage: from sympy.abc import p
sage: sPrimes.contains(p)
Contains(p, SageSet(Set of all prime numbers: 2, 3, 5, 7, ...))
sage: p in sPrimes
Traceback (most recent call last):
...
TypeError: did not evaluate to a bool: None
"""
return other in self._sage_()
if element.is_symbol:
# keep symbolic
return None
return element in self._sage_()

def __len__(self):
"""
Return the cardinality of the finite set ``self``.
EXAMPLES::
sage: sB3 = WeylGroup(["B", 3])._sympy_(); sB3
Expand Down

0 comments on commit eef604e

Please sign in to comment.