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}, Polyhedron.some_elements, …
Browse files Browse the repository at this point in the history
…RelativeInterior.some_elements: New
  • Loading branch information
mkoeppe committed Jun 16, 2021
1 parent 7d3ae5c commit 2b1040c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/sage/geometry/convex_set.py
Expand Up @@ -443,6 +443,45 @@ def affine_hull(self):
TypeError: 'NotImplementedType' object is not callable
"""

def an_element(self):
r"""
Return a point of ``self``.
If ``self`` is empty, an :class:`EmptySetError` 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().an_element()
"""
try:
return next(iter(self.some_elements()))
except StopIteration:
raise EmptySetError

@abstract_method(optional=True)
def some_elements(self):
r"""
Generate some points of ``self``.
If ``self`` is empty, no points are generated; no exception will be raised.
TESTS::
sage: from sage.geometry.convex_set import ConvexSet_base
sage: C = ConvexSet_base()
sage: C.some_elements(C)
Traceback (most recent call last):
...
TypeError: 'NotImplementedType' object is not callable
"""

@abstract_method(optional=True)
def cartesian_product(self, other):
"""
Expand Down
29 changes: 29 additions & 0 deletions src/sage/geometry/polyhedron/base.py
Expand Up @@ -3302,6 +3302,35 @@ def representative_point(self):
accumulator.set_immutable()
return accumulator

def some_elements(self):
r"""
Generate some points of ``self``.
If ``self`` is empty, no points are generated; no exception will be raised.
EXAMPLES::
sage: P = polytopes.simplex()
sage: list(P.some_elements())
[(1/4, 1/4, 1/4, 1/4),
(0, 0, 0, 1),
(0, 0, 1/2, 1/2),
(0, 1/2, 1/4, 1/4),
(1/2, 1/4, 1/8, 1/8)]
"""
if self.is_empty():
return
yield self.representative_point()
vertex_iter = iter(self.vertex_generator())
try:
p = next(vertex_iter).vector()
yield vector(p, immutable=True)
for i in range(4):
p = (p + next(vertex_iter).vector()) / 2
yield vector(p, immutable=True)
except StopIteration:
pass

def a_maximal_chain(self):
r"""
Return a maximal chain of the face lattice in increasing order.
Expand Down
16 changes: 16 additions & 0 deletions src/sage/geometry/relative_interior.py
Expand Up @@ -227,6 +227,22 @@ def is_closed(self):
assert not self._polyhedron.is_relatively_open()
return False

def some_elements(self):
r"""
Generate some points of ``self``.
If ``self`` is empty, no points are generated; no exception will be raised.
EXAMPLES::
sage: P = polytopes.simplex()
sage: list(P.relative_interior().some_elements())
[(1/4, 1/4, 1/4, 1/4), (1/2, 1/4, 1/8, 1/8)]
"""
for p in self._polyhedron.some_elements():
if p in self:
yield p

def _repr_(self):
r"""
Return a description of ``self``.
Expand Down

0 comments on commit 2b1040c

Please sign in to comment.