Path: blob/master/src/sage/geometry/polyhedron/misc.py
8817 views
r"""1Miscellaneous helper functions2"""345########################################################################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########################################################################13141516171819#########################################################################20def _to_space_separated_string(l):21"""22Converts a container to a space-separated string.2324INPUT:2526- ``l`` -- anything iterable.2728OUTPUT:2930String.3132EXAMPLES::3334sage: import sage.geometry.polyhedron.misc as P35sage: P._to_space_separated_string([2,3])36'2 3'37"""38s = '';39for x in l:40if len(s)>0: s += ' '41s += repr(x)42return s434445#########################################################################46def _set_to_None_if_empty(x):47"""48Helper function to clean up arguments: Returns None if x==None or49x is an empty container.5051EXAMPLES::5253sage: import sage.geometry.polyhedron.misc as P54sage: None == P._set_to_None_if_empty([])55True56sage: P._set_to_None_if_empty([1])57[1]58"""59if x is None: return x60x = list(x)61if len(x)==0: return None62return x636465#########################################################################66def _make_listlist(x):67"""68Helper function to clean up arguments.6970INPUT:7172- ``x`` -- ``None`` or an iterable of iterables.7374OUTPUT7576A list of lists.7778EXAMPLES::7980sage: import sage.geometry.polyhedron.misc as P81sage: [] == P._make_listlist(tuple())82True83sage: [] == P._make_listlist(None)84True85sage: P._make_listlist([(1,2),[3,4]])86[[1, 2], [3, 4]]87"""88if x is None: return []89return [list(y) for y in x]909192#########################################################################93def _common_length_of(l1, l2=None, l3=None):94"""95The arguments are containers or ``None``. The function applies96``len()`` to each element, and returns the common length. If the97length differs, ``ValueError`` is raised. Used to check arguments.9899OUTPUT:100101A tuple (number of entries, common length of the entries)102103EXAMPLES::104105sage: import sage.geometry.polyhedron.misc as P106sage: P._common_length_of([[1,2,3],[1,3,34]])107(2, 3)108"""109args = [];110if l1 is not None: args.append(l1)111if l2 is not None: args.append(l2)112if l3 is not None: args.append(l3)113114length = None115num = 0116for l in args:117for i in l:118num += 1119length_i = len(i)120if length is not None and length_i != length:121raise ValueError, "Argument lengths differ!"122length = length_i123124return num, length125126127