Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/geometry/polyhedron/cdd_file_format.py
4079 views
1
"""
2
Generate cdd ``.ext`` / ``.ine`` file format
3
"""
4
5
########################################################################
6
# Copyright (C) 2008 Marshall Hampton <[email protected]>
7
# Copyright (C) 2011 Volker Braun <[email protected]>
8
#
9
# Distributed under the terms of the GNU General Public License (GPL)
10
#
11
# http://www.gnu.org/licenses/
12
########################################################################
13
14
15
from misc import _set_to_None_if_empty, _common_length_of, _to_space_separated_string
16
17
18
19
#########################################################################
20
def cdd_Vrepresentation(cdd_type, vertices, rays, lines):
21
r"""
22
Return a string containing the V-representation in cddlib's ext format.
23
24
NOTE:
25
26
If there is no vertex given, then the origin will be implicitly
27
added. You cannot write the empty V-representation (which cdd
28
would refuse to process).
29
30
EXAMPLES::
31
32
sage: from sage.geometry.polyhedron.cdd_file_format import cdd_Vrepresentation
33
sage: print cdd_Vrepresentation('rational', [[0,0]], [[1,0]], [[0,1]])
34
V-representation
35
linearity 1 1
36
begin
37
3 3 rational
38
0 0 1
39
0 1 0
40
1 0 0
41
end
42
"""
43
vertices = _set_to_None_if_empty(vertices)
44
rays = _set_to_None_if_empty(rays)
45
lines = _set_to_None_if_empty(lines)
46
47
num, ambient_dim = _common_length_of(vertices, rays, lines)
48
49
# cdd implicitly assumes that the origin is a vertex if none is given
50
if vertices==None:
51
vertices = [[0]*ambient_dim]
52
num += 1
53
54
s = 'V-representation\n'
55
if lines!=None:
56
n = len(lines)
57
s += "linearity " + repr(n) + ' '
58
s += _to_space_separated_string(range(1,n+1)) + '\n'
59
s += 'begin\n'
60
s += ' ' + repr(num) + ' ' + repr(ambient_dim+1) + ' ' + cdd_type + '\n'
61
if lines!=None:
62
for l in lines:
63
s += ' 0 ' + _to_space_separated_string(l) + '\n'
64
if rays!=None:
65
for r in rays:
66
s += ' 0 ' + _to_space_separated_string(r) + '\n'
67
if vertices!=None:
68
for v in vertices:
69
s += ' 1 ' + _to_space_separated_string(v) + '\n'
70
s += 'end\n'
71
return s
72
73
#########################################################################
74
def cdd_Hrepresentation(cdd_type, ieqs, eqns):
75
r"""
76
Return a string containing the H-representation in cddlib's ine format.
77
78
EXAMPLES::
79
80
sage: from sage.geometry.polyhedron.cdd_file_format import cdd_Hrepresentation
81
sage: cdd_Hrepresentation('rational', None, [[0,1]])
82
'H-representation\nlinearity 1 1\nbegin\n 1 2 rational\n 0 1\nend\n'
83
"""
84
ieqs = _set_to_None_if_empty(ieqs)
85
eqns = _set_to_None_if_empty(eqns)
86
87
num, ambient_dim = _common_length_of(ieqs, eqns)
88
ambient_dim -= 1
89
90
s = 'H-representation\n'
91
if eqns!=None:
92
assert len(eqns)>0
93
n = len(eqns)
94
s += "linearity " + repr(n) + ' '
95
s += _to_space_separated_string(range(1,n+1)) + '\n'
96
s += 'begin\n'
97
s += ' ' + repr(num) + ' ' + repr(ambient_dim+1) + ' ' + cdd_type + '\n'
98
if eqns!=None:
99
for e in eqns:
100
s += ' ' + _to_space_separated_string(e) + '\n'
101
if ieqs!=None:
102
for i in ieqs:
103
s += ' ' + _to_space_separated_string(i) + '\n'
104
s += 'end\n'
105
return s
106
107