Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/geometry/polyhedron/misc.py
4068 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
if len(x)==0: return None
62
return x
63
64
65
#########################################################################
66
def _set_to_empty_if_None(x):
67
"""
68
Helper function to clean up arguments: Returns the empty list if
69
x==None or x is an empty container.
70
71
EXAMPLES::
72
73
sage: import sage.geometry.polyhedron.misc as P
74
sage: [] == P._set_to_empty_if_None(tuple())
75
True
76
sage: [] == P._set_to_empty_if_None(None)
77
True
78
sage: P._set_to_empty_if_None([1])
79
[1]
80
"""
81
if x is None: return []
82
if len(x)==0: return []
83
return x
84
85
86
#########################################################################
87
def _common_length_of(l1, l2=None, l3=None):
88
"""
89
The arguments are containers or ``None``. The function applies
90
``len()`` to each element, and returns the common length. If the
91
length differs, ``ValueError`` is raised. Used to check arguments.
92
93
OUTPUT:
94
95
A tuple (number of entries, common length of the entries)
96
97
EXAMPLES::
98
99
sage: import sage.geometry.polyhedron.misc as P
100
sage: P._common_length_of([[1,2,3],[1,3,34]])
101
(2, 3)
102
"""
103
args = [];
104
if l1 is not None: args.append(l1)
105
if l2 is not None: args.append(l2)
106
if l3 is not None: args.append(l3)
107
108
length = None
109
num = 0
110
for l in args:
111
for i in l:
112
num += 1
113
length_i = len(i)
114
if length is not None and length_i != length:
115
raise ValueError, "Argument lengths differ!"
116
length = length_i
117
118
return num, length
119
120