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

Commit

Permalink
ConvexSet_base.an_element, some_elements: Go through generator method…
Browse files Browse the repository at this point in the history
… _some_elements_, make some_elements return a list
  • Loading branch information
Matthias Koeppe committed Jun 17, 2021
1 parent 1c2bef5 commit ed898e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
31 changes: 26 additions & 5 deletions src/sage/geometry/convex_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,24 +449,45 @@ def an_element(self):
If ``self`` is empty, an :class:`EmptySetError` will be raised.
The default implementation delegates to :meth:`some_elements`.
The default implementation delegates to :meth:`_some_elements_`.
EXAMPLES::
sage: from sage.geometry.convex_set import ConvexSet_compact
sage: class BlueBox(ConvexSet_compact):
....: def some_elements(self):
....: def _some_elements_(self):
....: yield 'blue'
....: yield 'cyan'
sage: BlueBox().an_element()
'blue'
"""
try:
return next(iter(self.some_elements()))
return next(iter(self._some_elements_()))
except StopIteration:
raise EmptySetError

@abstract_method(optional=True)
def some_elements(self):
r"""
Return a list of some points of ``self``.
If ``self`` is empty, an empty list is returned; no exception will be raised.
The default implementation delegates to :meth:`_some_elements_`.
EXAMPLES::
sage: from sage.geometry.convex_set import ConvexSet_compact
sage: class BlueBox(ConvexSet_compact):
....: def _some_elements_(self):
....: yield 'blue'
....: yield 'cyan'
sage: BlueBox().some_elements()
['blue', 'cyan']
"""
return list(self._some_elements_())

@abstract_method(optional=True)
def _some_elements_(self):
r"""
Generate some points of ``self``.
Expand All @@ -476,7 +497,7 @@ def some_elements(self):
sage: from sage.geometry.convex_set import ConvexSet_base
sage: C = ConvexSet_base()
sage: C.some_elements(C)
sage: C._some_elements_(C)
Traceback (most recent call last):
...
TypeError: 'NotImplementedType' object is not callable
Expand Down
6 changes: 4 additions & 2 deletions src/sage/geometry/polyhedron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3302,7 +3302,7 @@ def representative_point(self):
accumulator.set_immutable()
return accumulator

def some_elements(self):
def _some_elements_(self):
r"""
Generate some points of ``self``.
Expand All @@ -3311,7 +3311,9 @@ def some_elements(self):
EXAMPLES::
sage: P = polytopes.simplex()
sage: list(P.some_elements())
sage: P.an_element() # indirect doctest
(1/4, 1/4, 1/4, 1/4)
sage: P.some_elements() # indirect doctest
[(1/4, 1/4, 1/4, 1/4),
(0, 0, 0, 1),
(0, 0, 1/2, 1/2),
Expand Down
6 changes: 4 additions & 2 deletions src/sage/geometry/polyhedron/face.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ def as_polyhedron(self):
Vrep = (self.vertices(), self.rays(), self.lines())
return P.__class__(parent, Vrep, None)

def some_elements(self):
def _some_elements_(self):
r"""
Generate some points of ``self``.
Expand All @@ -779,7 +779,9 @@ def some_elements(self):
A 2-dimensional face of a Polyhedron in ZZ^3 defined as the convex hull of 3 vertices
sage: face.as_polyhedron().vertices()
(A vertex at (0, -1, 0), A vertex at (0, 0, -1), A vertex at (1, 0, 0))
sage: list(face.some_elements())
sage: face.an_element() # indirect doctest
(1/3, -1/3, -1/3)
sage: face.some_elements() # indirect doctest
[(1/3, -1/3, -1/3), (0, -1, 0), (0, -1/2, -1/2), (1/2, -1/4, -1/4)]
"""
yield from self.as_polyhedron().some_elements()
Expand Down
7 changes: 5 additions & 2 deletions src/sage/geometry/relative_interior.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def is_closed(self):
assert not self._polyhedron.is_relatively_open()
return False

def some_elements(self):
def _some_elements_(self):
r"""
Generate some points of ``self``.
Expand All @@ -238,7 +238,10 @@ def some_elements(self):
EXAMPLES::
sage: P = polytopes.simplex()
sage: list(P.relative_interior().some_elements())
sage: ri_P = P.relative_interior()
sage: ri_P.an_element() # indirect doctest
(1/4, 1/4, 1/4, 1/4)
sage: ri_P.some_elements() # indirect doctest
[(1/4, 1/4, 1/4, 1/4), (1/2, 1/4, 1/8, 1/8)]
"""
for p in self._polyhedron.some_elements():
Expand Down

0 comments on commit ed898e6

Please sign in to comment.