Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

""" 

Generate cdd ``.ext`` / ``.ine`` file format 

""" 

 

######################################################################## 

# Copyright (C) 2008 Marshall Hampton <hamptonio@gmail.com> 

# Copyright (C) 2011 Volker Braun <vbraun.name@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# http://www.gnu.org/licenses/ 

######################################################################## 

from __future__ import print_function 

from __future__ import absolute_import 

 

from .misc import _set_to_None_if_empty, _common_length_of, _to_space_separated_string 

 

######################################################################### 

def cdd_Vrepresentation(cdd_type, vertices, rays, lines, file_output=None): 

r""" 

Return a string containing the V-representation in cddlib's ext format. 

 

INPUT: 

 

- ``file_output`` (string; optional) -- a filename to which the 

representation should be written. If set to ``None`` (default), 

representation is returned as a string. 

 

.. NOTE:: 

 

If there is no vertex given, then the origin will be implicitly 

added. You cannot write the empty V-representation (which cdd would 

refuse to process). 

 

EXAMPLES:: 

 

sage: from sage.geometry.polyhedron.cdd_file_format import cdd_Vrepresentation 

sage: print(cdd_Vrepresentation('rational', [[0,0]], [[1,0]], [[0,1]])) 

V-representation 

linearity 1 1 

begin 

3 3 rational 

0 0 1 

0 1 0 

1 0 0 

end 

 

TESTS:: 

 

sage: from sage.misc.temporary_file import tmp_filename 

sage: filename = tmp_filename(ext='.ext') 

sage: cdd_Vrepresentation('rational', [[0,0]], [[1,0]], [[0,1]], file_output=filename) 

""" 

vertices = _set_to_None_if_empty(vertices) 

rays = _set_to_None_if_empty(rays) 

lines = _set_to_None_if_empty(lines) 

 

num, ambient_dim = _common_length_of(vertices, rays, lines) 

 

# cdd implicitly assumes that the origin is a vertex if none is given 

if vertices is None: 

vertices = [[0]*ambient_dim] 

num += 1 

 

s = 'V-representation\n' 

if lines is not None: 

n = len(lines) 

s += "linearity " + repr(n) + ' ' 

s += _to_space_separated_string(range(1,n+1)) + '\n' 

s += 'begin\n' 

s += ' ' + repr(num) + ' ' + repr(ambient_dim+1) + ' ' + cdd_type + '\n' 

if lines is not None: 

for l in lines: 

s += ' 0 ' + _to_space_separated_string(l) + '\n' 

if rays is not None: 

for r in rays: 

s += ' 0 ' + _to_space_separated_string(r) + '\n' 

if vertices is not None: 

for v in vertices: 

s += ' 1 ' + _to_space_separated_string(v) + '\n' 

s += 'end\n' 

 

if file_output is not None: 

in_file = open(file_output, 'w') 

in_file.write(s) 

in_file.close() 

else: 

return s 

 

######################################################################### 

def cdd_Hrepresentation(cdd_type, ieqs, eqns, file_output=None): 

r""" 

Return a string containing the H-representation in cddlib's ine format. 

 

INPUT: 

 

- ``file_output`` (string; optional) -- a filename to which the 

representation should be written. If set to ``None`` (default), 

representation is returned as a string. 

 

EXAMPLES:: 

 

sage: from sage.geometry.polyhedron.cdd_file_format import cdd_Hrepresentation 

sage: cdd_Hrepresentation('rational', None, [[0,1]]) 

'H-representation\nlinearity 1 1\nbegin\n 1 2 rational\n 0 1\nend\n' 

 

TESTS:: 

 

sage: from sage.misc.temporary_file import tmp_filename 

sage: filename = tmp_filename(ext='.ine') 

sage: cdd_Hrepresentation('rational', None, [[0,1]], file_output=filename) 

""" 

ieqs = _set_to_None_if_empty(ieqs) 

eqns = _set_to_None_if_empty(eqns) 

 

num, ambient_dim = _common_length_of(ieqs, eqns) 

ambient_dim -= 1 

 

s = 'H-representation\n' 

if eqns is not None: 

assert len(eqns)>0 

n = len(eqns) 

s += "linearity " + repr(n) + ' ' 

s += _to_space_separated_string(range(1,n+1)) + '\n' 

s += 'begin\n' 

s += ' ' + repr(num) + ' ' + repr(ambient_dim+1) + ' ' + cdd_type + '\n' 

if eqns is not None: 

for e in eqns: 

s += ' ' + _to_space_separated_string(e) + '\n' 

if ieqs is not None: 

for i in ieqs: 

s += ' ' + _to_space_separated_string(i) + '\n' 

s += 'end\n' 

 

if file_output is not None: 

in_file = open(file_output, 'w') 

in_file.write(s) 

in_file.close() 

else: 

return s