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

Commit

Permalink
set up the hypercube with both Vrep and Hrep
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Kliem committed Feb 24, 2020
1 parent 268f41a commit f9958f3
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/sage/geometry/polyhedron/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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::
Expand All @@ -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"""
Expand Down

0 comments on commit f9958f3

Please sign in to comment.