Skip to content

Commit

Permalink
Trac #29838: Implement slack matrix for polyhedra
Browse files Browse the repository at this point in the history
We implement the slack matrix for polyhedra.

This matrix contains all the evaluations of Hrepresentation elements on
Vrepresentation elements.

This method can be used to obtain the incidence matrix much quicker (at
least for base ring rationals or integers).

URL: https://trac.sagemath.org/29838
Reported by: gh-kliem
Ticket author(s): Jonathan Kliem
Reviewer(s): Matthias Koeppe
  • Loading branch information
Release Manager committed Jul 4, 2020
2 parents e916ed5 + 47a19c6 commit 6c495f4
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Expand Up @@ -133,6 +133,7 @@ List of Polyhedron methods
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.vertex_facet_graph` | bipartite digraph given vertex-facet adjacency
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.adjacency_matrix` | adjacency matrix
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.incidence_matrix` | incidence matrix
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.slack_matrix` | slack matrix
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.facet_adjacency_matrix` | adjacency matrix of the facets
:meth:`~sage.geometry.polyhedron.base.Polyhedron_base.vertex_adjacency_matrix` | adjacency matrix of the vertices

Expand Down
84 changes: 84 additions & 0 deletions src/sage/geometry/polyhedron/base.py
Expand Up @@ -2586,6 +2586,10 @@ def incidence_matrix(self):
vertices/rays/lines in the order
:meth:`Vrepresentation`.
.. SEEALSO::
:meth:`slack_matrix`.
EXAMPLES::
sage: p = polytopes.cuboctahedron()
Expand Down Expand Up @@ -2701,6 +2705,86 @@ def incidence_matrix(self):
incidence_matrix.set_immutable()
return incidence_matrix

@cached_method
def slack_matrix(self):
r"""
Return the slack matrix.
The entries correspond to the evaluation of the Hrepresentation
elements on the Vrepresentation elements.
.. NOTE::
The columns correspond to inequalities/equations in the
order :meth:`Hrepresentation`, the rows correspond to
vertices/rays/lines in the order
:meth:`Vrepresentation`.
.. SEEALSO::
:meth:`incidence_matrix`.
EXAMPLES::
sage: P = polytopes.cube()
sage: P.slack_matrix()
[0 2 2 2 0 0]
[0 0 2 2 0 2]
[0 0 0 2 2 2]
[0 2 0 2 2 0]
[2 2 0 0 2 0]
[2 2 2 0 0 0]
[2 0 2 0 0 2]
[2 0 0 0 2 2]
sage: P = polytopes.cube(intervals='zero_one')
sage: P.slack_matrix()
[0 1 1 1 0 0]
[0 0 1 1 0 1]
[0 0 0 1 1 1]
[0 1 0 1 1 0]
[1 1 0 0 1 0]
[1 1 1 0 0 0]
[1 0 1 0 0 1]
[1 0 0 0 1 1]
sage: P = polytopes.dodecahedron().faces(2)[0].as_polyhedron()
sage: P.slack_matrix()
[1/2*sqrt5 - 1/2 0 0 1 1/2*sqrt5 - 1/2 0]
[ 0 0 1/2*sqrt5 - 1/2 1/2*sqrt5 - 1/2 1 0]
[ 0 1/2*sqrt5 - 1/2 1 0 1/2*sqrt5 - 1/2 0]
[ 1 1/2*sqrt5 - 1/2 0 1/2*sqrt5 - 1/2 0 0]
[1/2*sqrt5 - 1/2 1 1/2*sqrt5 - 1/2 0 0 0]
sage: P = Polyhedron(rays=[[1, 0], [0, 1]])
sage: P.slack_matrix()
[0 0]
[0 1]
[1 0]
TESTS::
sage: Polyhedron().slack_matrix()
[]
sage: Polyhedron(base_ring=QuadraticField(2)).slack_matrix().base_ring()
Number Field in a with defining polynomial x^2 - 2 with a = 1.41...
"""
if not self.n_Vrepresentation() or not self.n_Hrepresentation():
slack_matrix = matrix(self.base_ring(), self.n_Vrepresentation(),
self.n_Hrepresentation(), 0)
else:
Vrep_matrix = matrix(self.base_ring(), self.Vrepresentation())
Hrep_matrix = matrix(self.base_ring(), self.Hrepresentation())

# Getting homogenous coordinates of the Vrepresentation.
hom_helper = matrix(self.base_ring(), [1 if v.is_vertex() else 0 for v in self.Vrepresentation()])
hom_Vrep = hom_helper.stack(Vrep_matrix.transpose())

slack_matrix = (Hrep_matrix * hom_Vrep).transpose()

slack_matrix.set_immutable()
return slack_matrix

def base_ring(self):
"""
Return the base ring.
Expand Down

0 comments on commit 6c495f4

Please sign in to comment.