From 357fcd1f30f90c406b2fcbc4bf8433ff2faf592a Mon Sep 17 00:00:00 2001 From: Jonathan Kliem Date: Fri, 14 Feb 2020 11:36:06 +0100 Subject: [PATCH] set up the hypercube with both Vrep and Hrep --- src/sage/geometry/polyhedron/library.py | 34 ++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/sage/geometry/polyhedron/library.py b/src/sage/geometry/polyhedron/library.py index 1757e194e26..c2c7a26810f 100644 --- a/src/sage/geometry/polyhedron/library.py +++ b/src/sage/geometry/polyhedron/library.py @@ -86,6 +86,7 @@ from sage.combinat.permutation import Permutations from sage.groups.perm_gps.permgroup_named import AlternatingGroup from .constructor import Polyhedron +from .parent import Polyhedra from sage.graphs.digraph import DiGraph from sage.combinat.root_system.associahedron import Associahedron @@ -2792,6 +2793,13 @@ def hypercube(self, dim, intervals=None, backend=None): ... ValueError: the dimension of the hypercube must match the number of intervals + The intervals must be pairs `(a, b)` with `a < b`:: + + sage: w_cube = polytopes.hypercube(3, intervals = [[0,1],[3,2],[0,3]]) + Traceback (most recent call last): + ... + ValueError: each interval must be a pair `(a, b)` with `a < b` + If a string besides 'zero_one' is passed to ``intervals``, return an error:: @@ -2800,18 +2808,42 @@ def hypercube(self, dim, intervals=None, backend=None): ... ValueError: the only allowed string is 'zero_one' """ + parent = Polyhedra(ZZ, dim, backend=backend) + + # Preparing the inequalities: + # If the intervals are (a_1,b_1), ..., (a_dim, b_dim), + # then the inequalites correspond to + # b_1,b_2,...,b_dim, a_1,a_2,...,a_dim + # in that order. + ieqs = [[0]*(dim+1) for _ in range(2*dim)] + for i in range(dim): + ieqs[i][i+1] = -1 + ieqs[dim+i][i+1] = 1 + if intervals is None: cp = list(itertools.product([-1,1], repeat=dim)) + for i in range(dim): + ieqs[i][0] = 1 # An inequality -x_i + 1 >= 0 + ieqs[i+dim][0] = 1 # An inequality x_i + 1 >= 0 elif isinstance(intervals, str): if intervals == 'zero_one': cp = list(itertools.product([0,1], repeat=dim)) + for i in range(dim): + ieqs[i][0] = 1 # An inequality -x_i + 1 >= 0 else: raise ValueError("the only allowed string is 'zero_one'") elif len(intervals) == dim: + if not all(a < b for a,b in intervals): + raise ValueError("each interval must be a pair `(a, b)` with `a < b`") + parent = parent.base_extend(intervals) cp = list(itertools.product(*intervals)) + for i in range(dim): + ieqs[i][0] = intervals[i][1] # An inequality -x_i + b_i >= 0 + ieqs[i+dim][0] = -intervals[i][0] # An inequality x_i - a_i >= 0 + else: raise ValueError("the dimension of the hypercube must match the number of intervals") - return Polyhedron(vertices=cp, backend=backend) + return parent.element_class(parent, [cp, [], []], [ieqs, []], Vrep_minimal=True, Hrep_minimal=True ) def cube(self, intervals=None, backend=None): r"""