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

r""" 

Miscellaneous helper functions 

""" 

 

 

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

# 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/ 

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

 

 

 

 

 

 

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

def _to_space_separated_string(l): 

""" 

Converts a container to a space-separated string. 

 

INPUT: 

 

- ``l`` -- anything iterable. 

 

OUTPUT: 

 

String. 

 

EXAMPLES:: 

 

sage: import sage.geometry.polyhedron.misc as P 

sage: P._to_space_separated_string([2,3]) 

'2 3' 

""" 

s = ''; 

for x in l: 

if len(s)>0: s += ' ' 

s += repr(x) 

return s 

 

 

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

def _set_to_None_if_empty(x): 

""" 

Helper function to clean up arguments: Returns None if x==None or 

x is an empty container. 

 

EXAMPLES:: 

 

sage: import sage.geometry.polyhedron.misc as P 

sage: None == P._set_to_None_if_empty([]) 

True 

sage: P._set_to_None_if_empty([1]) 

[1] 

""" 

if x is None: return x 

x = list(x) 

if len(x)==0: return None 

return x 

 

 

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

def _make_listlist(x): 

""" 

Helper function to clean up arguments. 

 

INPUT: 

 

- ``x`` -- ``None`` or an iterable of iterables. 

 

OUTPUT: 

 

A list of lists. 

 

EXAMPLES:: 

 

sage: import sage.geometry.polyhedron.misc as P 

sage: [] == P._make_listlist(tuple()) 

True 

sage: [] == P._make_listlist(None) 

True 

sage: P._make_listlist([(1,2),[3,4]]) 

[[1, 2], [3, 4]] 

""" 

if x is None: return [] 

return [list(y) for y in x] 

 

 

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

def _common_length_of(l1, l2=None, l3=None): 

""" 

The arguments are containers or ``None``. The function applies 

``len()`` to each element, and returns the common length. If the 

length differs, ``ValueError`` is raised. Used to check arguments. 

 

OUTPUT: 

 

A tuple (number of entries, common length of the entries) 

 

EXAMPLES:: 

 

sage: import sage.geometry.polyhedron.misc as P 

sage: P._common_length_of([[1,2,3],[1,3,34]]) 

(2, 3) 

""" 

args = []; 

if l1 is not None: args.append(l1) 

if l2 is not None: args.append(l2) 

if l3 is not None: args.append(l3) 

 

length = None 

num = 0 

for l in args: 

for i in l: 

num += 1 

length_i = len(i) 

if length is not None and length_i != length: 

raise ValueError("Argument lengths differ!") 

length = length_i 

 

return num, length