Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/geometry/polyhedron/misc.py
8817 views
1
r"""
2
Miscellaneous helper functions
3
"""
4
5
6
########################################################################
7
# Copyright (C) 2008 Marshall Hampton <[email protected]>
8
# Copyright (C) 2011 Volker Braun <[email protected]>
9
#
10
# Distributed under the terms of the GNU General Public License (GPL)
11
#
12
# http://www.gnu.org/licenses/
13
########################################################################
14
15
16
17
18
19
20
#########################################################################
21
def _to_space_separated_string(l):
22
"""
23
Converts a container to a space-separated string.
24
25
INPUT:
26
27
- ``l`` -- anything iterable.
28
29
OUTPUT:
30
31
String.
32
33
EXAMPLES::
34
35
sage: import sage.geometry.polyhedron.misc as P
36
sage: P._to_space_separated_string([2,3])
37
'2 3'
38
"""
39
s = '';
40
for x in l:
41
if len(s)>0: s += ' '
42
s += repr(x)
43
return s
44
45
46
#########################################################################
47
def _set_to_None_if_empty(x):
48
"""
49
Helper function to clean up arguments: Returns None if x==None or
50
x is an empty container.
51
52
EXAMPLES::
53
54
sage: import sage.geometry.polyhedron.misc as P
55
sage: None == P._set_to_None_if_empty([])
56
True
57
sage: P._set_to_None_if_empty([1])
58
[1]
59
"""
60
if x is None: return x
61
x = list(x)
62
if len(x)==0: return None
63
return x
64
65
66
#########################################################################
67
def _make_listlist(x):
68
"""
69
Helper function to clean up arguments.
70
71
INPUT:
72
73
- ``x`` -- ``None`` or an iterable of iterables.
74
75
OUTPUT
76
77
A list of lists.
78
79
EXAMPLES::
80
81
sage: import sage.geometry.polyhedron.misc as P
82
sage: [] == P._make_listlist(tuple())
83
True
84
sage: [] == P._make_listlist(None)
85
True
86
sage: P._make_listlist([(1,2),[3,4]])
87
[[1, 2], [3, 4]]
88
"""
89
if x is None: return []
90
return [list(y) for y in x]
91
92
93
#########################################################################
94
def _common_length_of(l1, l2=None, l3=None):
95
"""
96
The arguments are containers or ``None``. The function applies
97
``len()`` to each element, and returns the common length. If the
98
length differs, ``ValueError`` is raised. Used to check arguments.
99
100
OUTPUT:
101
102
A tuple (number of entries, common length of the entries)
103
104
EXAMPLES::
105
106
sage: import sage.geometry.polyhedron.misc as P
107
sage: P._common_length_of([[1,2,3],[1,3,34]])
108
(2, 3)
109
"""
110
args = [];
111
if l1 is not None: args.append(l1)
112
if l2 is not None: args.append(l2)
113
if l3 is not None: args.append(l3)
114
115
length = None
116
num = 0
117
for l in args:
118
for i in l:
119
num += 1
120
length_i = len(i)
121
if length is not None and length_i != length:
122
raise ValueError, "Argument lengths differ!"
123
length = length_i
124
125
return num, length
126
127