Skip to content

Commit

Permalink
Trac #29325: Set up permutahedron with both Vrep and Hrep (if backend…
Browse files Browse the repository at this point in the history
… supports it)

We set up the permutahedron with both Vrepresenation and
Hrepresentation, if the backend supports it.

If the backend supports precomputed data (currently `field`) this is
much faster. Otherwise, this is a bit faster, as the Hrepresentation
seems to be the better choice.

Before this ticket:
{{{
sage: %timeit P = polytopes.permutahedron(6)  # using ppl
10 loops, best of 5: 56.5 ms per loop
sage: %timeit P = polytopes.permutahedron(7)  # using ppl
1 loop, best of 5: 1.57 s per loop
}}}

With this ticket:
{{{
sage: %timeit P = polytopes.permutahedron(6)  # using ppl
10 loops, best of 5: 34.2 ms per loop
sage: %timeit P = polytopes.permutahedron(7)  # using ppl
1 loop, best of 5: 1.04 s per loop
sage: %time P = polytopes.permutahedron(8, backend='field')
CPU times: user 587 ms, sys: 20 ms, total: 607 ms
Wall time: 607 ms
sage: %time P = polytopes.permutahedron(9, backend='field')
CPU times: user 5.08 s, sys: 248 ms, total: 5.33 s
Wall time: 5.33 s
}}}

Note that `field` is much slower than `ppl` before this ticket and the
timings are therefore omitted.

As the order of Vrepresentation and Hrepresentation changes, a lot of
tests need to be fixed.

URL: https://trac.sagemath.org/29325
Reported by: gh-kliem
Ticket author(s): Jonathan Kliem
Reviewer(s): Jean-Philippe Labbé, Matthias Koeppe
  • Loading branch information
Release Manager committed Aug 14, 2020
2 parents 9b14ca3 + 577e736 commit b4a8c57
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 123 deletions.
20 changes: 10 additions & 10 deletions src/doc/en/thematic_tutorials/geometry/tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ The `style` parameter allows to change the way to print the `H`-relations:
sage: P = polytopes.permutahedron(3)
sage: print(P.Hrepresentation_str(style='<='))
-x0 - x1 - x2 == -6
x1 + x2 <= 5
x2 <= 3
x1 <= 3
-x0 - x1 <= -3
x0 + x1 <= 5
-x1 <= -1
-x1 - x2 <= -3
-x2 <= -1
x0 <= 3
-x0 <= -1
x1 <= 3
sage: print(P.Hrepresentation_str(style='positive'))
x0 + x1 + x2 == 6
5 >= x1 + x2
3 >= x2
3 >= x1
x0 + x1 >= 3
5 >= x0 + x1
x1 >= 1
x1 + x2 >= 3
x2 >= 1
3 >= x0
x0 >= 1
3 >= x1

.. end of output
62 changes: 31 additions & 31 deletions src/sage/geometry/polyhedron/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1594,50 +1594,50 @@ def Hrepresentation_str(self, separator='\n', latex=False, style='>=', align=Non
sage: P = polytopes.permutahedron(3)
sage: print(P.Hrepresentation_str())
x0 + x1 + x2 == 6
-x1 - x2 >= -5
-x2 >= -3
-x1 >= -3
x0 + x1 >= 3
-x0 - x1 >= -5
x1 >= 1
x1 + x2 >= 3
x2 >= 1
-x0 >= -3
x0 >= 1
-x1 >= -3
sage: print(P.Hrepresentation_str(style='<='))
-x0 - x1 - x2 == -6
x1 + x2 <= 5
x2 <= 3
x1 <= 3
-x0 - x1 <= -3
x0 + x1 <= 5
-x1 <= -1
-x1 - x2 <= -3
-x2 <= -1
x0 <= 3
-x0 <= -1
x1 <= 3
sage: print(P.Hrepresentation_str(style='positive'))
x0 + x1 + x2 == 6
5 >= x1 + x2
3 >= x2
3 >= x1
x0 + x1 >= 3
5 >= x0 + x1
x1 >= 1
x1 + x2 >= 3
x2 >= 1
3 >= x0
x0 >= 1
3 >= x1
sage: print(P.Hrepresentation_str(latex=True))
\begin{array}{rcl}
x_{0} + x_{1} + x_{2} & = & 6 \\
-x_{1} - x_{2} & \geq & -5 \\
-x_{2} & \geq & -3 \\
-x_{1} & \geq & -3 \\
x_{0} + x_{1} & \geq & 3 \\
-x_{0} - x_{1} & \geq & -5 \\
x_{1} & \geq & 1 \\
x_{1} + x_{2} & \geq & 3 \\
x_{2} & \geq & 1
-x_{0} & \geq & -3 \\
x_{0} & \geq & 1 \\
-x_{1} & \geq & -3
\end{array}
sage: print(P.Hrepresentation_str(align=False))
x0 + x1 + x2 == 6
-x1 - x2 >= -5
-x2 >= -3
-x1 >= -3
x0 + x1 >= 3
-x0 - x1 >= -5
x1 >= 1
x1 + x2 >= 3
x2 >= 1
-x0 >= -3
x0 >= 1
-x1 >= -3
sage: c = polytopes.cube()
sage: c.Hrepresentation_str(separator=', ', style='positive')
Expand Down Expand Up @@ -2117,11 +2117,11 @@ def an_affine_basis(self):
sage: P = polytopes.permutahedron(5)
sage: P.an_affine_basis()
[A vertex at (4, 1, 5, 2, 3),
A vertex at (5, 1, 4, 2, 3),
A vertex at (4, 2, 5, 1, 3),
A vertex at (4, 1, 5, 3, 2),
A vertex at (1, 2, 3, 4, 5)]
[A vertex at (1, 2, 3, 5, 4),
A vertex at (2, 1, 3, 5, 4),
A vertex at (1, 3, 2, 5, 4),
A vertex at (4, 1, 3, 5, 2),
A vertex at (4, 2, 5, 3, 1)]
The method is not implemented for unbounded polyhedra::
Expand Down Expand Up @@ -9852,7 +9852,7 @@ def affine_hull_projection(self, as_affine_map=False, orthogonal=False, orthonor
sage: A, b = P.affine_hull_projection(orthonormal=True, as_affine_map=True, extend=True)
sage: Q = P.affine_hull_projection(orthonormal=True, extend=True)
sage: Q.center()
(0.7071067811865475?, 0.7071067811865475?, 2.000000000000000?)
(0.7071067811865475?, 1.224744871391589?, 1.732050807568878?)
sage: A(P.center()) + b == Q.center()
True
Expand Down
46 changes: 25 additions & 21 deletions src/sage/geometry/polyhedron/combinatorial_polyhedron/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -687,12 +687,12 @@ cdef class CombinatorialPolyhedron(SageObject):
sage: C = CombinatorialPolyhedron(P)
sage: C.Hrepresentation()
(An equation (1, 1, 1) x - 6 == 0,
An inequality (0, -1, -1) x + 5 >= 0,
An inequality (0, 0, -1) x + 3 >= 0,
An inequality (0, -1, 0) x + 3 >= 0,
An inequality (1, 1, 0) x - 3 >= 0,
An inequality (-1, -1, 0) x + 5 >= 0,
An inequality (0, 1, 0) x - 1 >= 0,
An inequality (0, 1, 1) x - 3 >= 0,
An inequality (0, 0, 1) x - 1 >= 0)
An inequality (-1, 0, 0) x + 3 >= 0,
An inequality (1, 0, 0) x - 1 >= 0,
An inequality (0, -1, 0) x + 3 >= 0)
sage: points = [(1,0,0), (0,1,0), (0,0,1),
....: (-1,0,0), (0,-1,0), (0,0,-1)]
Expand Down Expand Up @@ -1285,10 +1285,10 @@ cdef class CombinatorialPolyhedron(SageObject):
sage: P = polytopes.permutahedron(2)
sage: C = CombinatorialPolyhedron(P)
sage: C.ridges()
((An inequality (0, -1) x + 2 >= 0, An inequality (0, 1) x - 1 >= 0),)
((An inequality (1, 0) x - 1 >= 0, An inequality (-1, 0) x + 2 >= 0),)
sage: C.ridges(add_equalities=True)
(((An inequality (0, -1) x + 2 >= 0, An equation (1, 1) x - 3 == 0),
(An inequality (0, 1) x - 1 >= 0, An equation (1, 1) x - 3 == 0)),)
(((An inequality (1, 0) x - 1 >= 0, An equation (1, 1) x - 3 == 0),
(An inequality (-1, 0) x + 2 >= 0, An equation (1, 1) x - 3 == 0)),)
sage: P = polytopes.cyclic_polytope(4,5)
sage: C = CombinatorialPolyhedron(P)
Expand Down Expand Up @@ -2296,29 +2296,33 @@ cdef class CombinatorialPolyhedron(SageObject):
sage: face = next(it); face
A 2-dimensional face of a 4-dimensional combinatorial polyhedron
sage: face.ambient_Vrepresentation()
(A vertex at (4, 1, 5, 2, 3),
A vertex at (4, 2, 5, 1, 3),
A vertex at (5, 1, 4, 2, 3),
A vertex at (5, 2, 4, 1, 3))
(A vertex at (1, 3, 2, 5, 4),
A vertex at (2, 3, 1, 5, 4),
A vertex at (3, 1, 2, 5, 4),
A vertex at (3, 2, 1, 5, 4),
A vertex at (2, 1, 3, 5, 4),
A vertex at (1, 2, 3, 5, 4))
sage: face = next(it); face
A 2-dimensional face of a 4-dimensional combinatorial polyhedron
sage: face.ambient_Vrepresentation()
(A vertex at (4, 1, 5, 2, 3),
A vertex at (4, 1, 5, 3, 2),
A vertex at (5, 1, 4, 2, 3),
A vertex at (5, 1, 4, 3, 2))
(A vertex at (2, 1, 4, 5, 3),
A vertex at (3, 2, 4, 5, 1),
A vertex at (3, 1, 4, 5, 2),
A vertex at (1, 3, 4, 5, 2),
A vertex at (1, 2, 4, 5, 3),
A vertex at (2, 3, 4, 5, 1))
sage: face.ambient_Hrepresentation()
(An inequality (0, 1, 0, 0, 0) x - 1 >= 0,
An inequality (0, 1, 0, 1, 1) x - 6 >= 0,
(An inequality (0, 0, -1, -1, 0) x + 9 >= 0,
An inequality (0, 0, 0, -1, 0) x + 5 >= 0,
An equation (1, 1, 1, 1, 1) x - 15 == 0)
sage: face.ambient_H_indices()
(25, 29)
sage: face = next(it); face
A 2-dimensional face of a 4-dimensional combinatorial polyhedron
sage: face.ambient_H_indices()
(12, 29)
(24, 29)
sage: face.ambient_V_indices()
(76, 77, 82, 83, 88, 89)
(32, 89, 90, 94)
sage: C = CombinatorialPolyhedron([[0,1,2],[0,1,3],[0,2,3],[1,2,3]])
sage: it = C.face_iter()
Expand Down Expand Up @@ -2603,7 +2607,7 @@ cdef class CombinatorialPolyhedron(SageObject):
A 1-dimensional face of a 3-dimensional combinatorial polyhedron,
A 2-dimensional face of a 3-dimensional combinatorial polyhedron]
sage: [face.ambient_V_indices() for face in chain]
[(13,), (13, 15), (13, 15, 19, 21)]
[(16,), (15, 16), (8, 9, 14, 15, 16, 17)]
sage: P = Polyhedron(rays=[[1,0]], lines=[[0,1]])
sage: C = P.combinatorial_polyhedron()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,20 @@ cdef class CombinatorialFace(SageObject):
sage: it = C.face_iter(dimension=2)
sage: face = next(it)
sage: face.ambient_Vrepresentation()
(A vertex at (4, 1, 5, 2, 3),
A vertex at (4, 2, 5, 1, 3),
A vertex at (5, 1, 4, 2, 3),
A vertex at (5, 2, 4, 1, 3))
(A vertex at (1, 3, 2, 5, 4),
A vertex at (2, 3, 1, 5, 4),
A vertex at (3, 1, 2, 5, 4),
A vertex at (3, 2, 1, 5, 4),
A vertex at (2, 1, 3, 5, 4),
A vertex at (1, 2, 3, 5, 4))
sage: face = next(it)
sage: face.ambient_Vrepresentation()
(A vertex at (4, 1, 5, 2, 3),
A vertex at (4, 1, 5, 3, 2),
A vertex at (5, 1, 4, 2, 3),
A vertex at (5, 1, 4, 3, 2))
(A vertex at (2, 1, 4, 5, 3),
A vertex at (3, 2, 4, 5, 1),
A vertex at (3, 1, 4, 5, 2),
A vertex at (1, 3, 4, 5, 2),
A vertex at (1, 2, 4, 5, 3),
A vertex at (2, 3, 4, 5, 1))
sage: C = CombinatorialPolyhedron([[0,1,2],[0,1,3],[0,2,3],[1,2,3]])
sage: it = C.face_iter()
Expand Down Expand Up @@ -411,9 +415,9 @@ cdef class CombinatorialFace(SageObject):
sage: it = C.face_iter(dimension=2)
sage: face = next(it)
sage: next(it).ambient_V_indices()
(76, 77, 100, 101)
(32, 91, 92, 93, 94, 95)
sage: next(it).ambient_V_indices()
(76, 77, 82, 83, 88, 89)
(32, 89, 90, 94)
sage: C = CombinatorialPolyhedron([[0,1,2],[0,1,3],[0,2,3],[1,2,3]])
sage: it = C.face_iter()
Expand Down Expand Up @@ -474,10 +478,12 @@ cdef class CombinatorialFace(SageObject):
sage: face.Vrepr()
doctest:...: DeprecationWarning: the method Vrepr of CombinatorialPolyhedron is deprecated; use ambient_V_indices or ambient_Vrepresentation
See https://trac.sagemath.org/28616 for details.
(A vertex at (4, 1, 5, 2, 3),
A vertex at (4, 2, 5, 1, 3),
A vertex at (5, 1, 4, 2, 3),
A vertex at (5, 2, 4, 1, 3))
(A vertex at (1, 3, 2, 5, 4),
A vertex at (2, 3, 1, 5, 4),
A vertex at (3, 1, 2, 5, 4),
A vertex at (3, 2, 1, 5, 4),
A vertex at (2, 1, 3, 5, 4),
A vertex at (1, 2, 3, 5, 4))
"""
from sage.misc.superseded import deprecation
deprecation(28616, "the method Vrepr of CombinatorialPolyhedron is deprecated; use ambient_V_indices or ambient_Vrepresentation", 3)
Expand Down Expand Up @@ -531,12 +537,12 @@ cdef class CombinatorialFace(SageObject):
sage: C = CombinatorialPolyhedron(P)
sage: it = C.face_iter(2)
sage: next(it).ambient_Hrepresentation()
(An inequality (0, 1, 0, 1, 0) x - 3 >= 0,
An inequality (0, 1, 0, 1, 1) x - 6 >= 0,
(An inequality (1, 1, 1, 0, 0) x - 6 >= 0,
An inequality (0, 0, 0, -1, 0) x + 5 >= 0,
An equation (1, 1, 1, 1, 1) x - 15 == 0)
sage: next(it).ambient_Hrepresentation()
(An inequality (0, 1, 0, 0, 0) x - 1 >= 0,
An inequality (0, 1, 0, 1, 1) x - 6 >= 0,
(An inequality (0, 0, -1, -1, 0) x + 9 >= 0,
An inequality (0, 0, 0, -1, 0) x + 5 >= 0,
An equation (1, 1, 1, 1, 1) x - 15 == 0)
sage: P = polytopes.cyclic_polytope(4,6)
Expand Down Expand Up @@ -652,8 +658,8 @@ cdef class CombinatorialFace(SageObject):
sage: next(it).Hrepr()
doctest:...: DeprecationWarning: the method Hrepr of CombinatorialPolyhedron is deprecated; use ambient_H_indices or ambient_Hrepresentation
See https://trac.sagemath.org/28616 for details.
(An inequality (0, 1, 0, 1, 0) x - 3 >= 0,
An inequality (0, 1, 0, 1, 1) x - 6 >= 0,
(An inequality (1, 1, 1, 0, 0) x - 6 >= 0,
An inequality (0, 0, 0, -1, 0) x + 5 >= 0,
An equation (1, 1, 1, 1, 1) x - 15 == 0)
"""
from sage.misc.superseded import deprecation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,19 @@ def incidence_matrix_to_bit_rep_of_facets(Matrix_integer_dense matrix):
sage: for i in range(facets.n_faces): # long time
....: print(bit_rep_to_Vrep_list_wrapper(facets, i))
(18, 19, 20, 21, 22, 23)
(9, 11, 15, 17, 21, 23)
(16, 17, 22, 23)
(0, 1, 2, 3, 4, 5)
(2, 4, 8, 10)
(0, 1, 6, 7)
(6, 7, 12, 13, 18, 19)
(3, 5, 9, 11)
(1, 3, 7, 9, 13, 15)
(0, 2, 6, 8, 12, 14)
(12, 14, 18, 20)
(4, 5, 10, 11, 16, 17)
(8, 10, 14, 16, 20, 22)
(13, 15, 19, 21)
(3, 5, 8, 10, 12, 17)
(2, 7, 11, 13, 20, 21)
(2, 5, 12, 13)
(4, 6, 14, 15, 19, 23)
(3, 4, 8, 14)
(6, 7, 21, 23)
(2, 3, 4, 5, 6, 7)
(0, 1, 9, 16, 18, 22)
(0, 9, 10, 17)
(1, 11, 20, 22)
(0, 1, 10, 11, 12, 13)
(15, 16, 18, 19)
(8, 9, 14, 15, 16, 17)
"""
# Output will be a ``ListOfFaces`` with ``matrix.ncols()`` faces and
# ``matrix.nrows()`` Vrep.
Expand Down Expand Up @@ -377,30 +377,30 @@ def incidence_matrix_to_bit_rep_of_Vrep(Matrix_integer_dense matrix):
14
sage: for i in range(vertices.n_faces):
....: print(bit_rep_to_Vrep_list_wrapper(vertices, i))
(3, 5, 9)
(3, 5, 8)
(3, 4, 9)
(3, 7, 8)
(3, 4, 11)
(3, 7, 11)
(5, 6, 9)
(5, 6, 8)
(4, 9, 12)
(1, 7, 8)
(4, 11, 12)
(1, 7, 11)
(6, 9, 10)
(6, 8, 13)
(9, 10, 12)
(1, 8, 13)
(2, 11, 12)
(1, 2, 11)
(0, 6, 10)
(0, 6, 13)
(0, 10, 12)
(0, 1, 13)
(0, 2, 12)
(0, 1, 2)
(8, 9, 11)
(8, 10, 11)
(2, 3, 7)
(1, 5, 7)
(4, 5, 7)
(1, 3, 7)
(4, 6, 7)
(2, 6, 7)
(1, 5, 13)
(8, 9, 13)
(1, 9, 11)
(2, 10, 11)
(1, 3, 11)
(2, 3, 11)
(4, 5, 13)
(4, 12, 13)
(8, 12, 13)
(1, 9, 13)
(0, 8, 12)
(0, 4, 12)
(0, 2, 10)
(0, 2, 6)
(0, 8, 10)
(0, 4, 6)
"""
return incidence_matrix_to_bit_rep_of_facets(matrix.transpose())
incidence_matrix_to_bit_repr_of_Vrepr = deprecated_function_alias(28608, incidence_matrix_to_bit_rep_of_Vrep)
Expand Down
Loading

0 comments on commit b4a8c57

Please sign in to comment.