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

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

""" 

Rankers 

""" 

#***************************************************************************** 

# Copyright (C) 2007 Mike Hansen <mhansen@gmail.com>, 

# Nicolas M. Thiery <nthiery at users.sf.net> 

# Ported from MuPAD-Combinat (combinat::rankers) 

# 

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

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

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

#***************************************************************************** 

from six.moves import range 

 

from collections import Iterable, Sequence 

from sage.misc.cachefunc import cached_function 

from sage.misc.callable_dict import CallableDict 

from sage.structure.parent import Parent 

from sage.categories.enumerated_sets import EnumeratedSets 

 

def from_list(l): 

""" 

Returns a ranker from the list l. 

 

INPUT: 

 

- ``l`` - a list 

 

OUTPUT: 

 

- ``[rank, unrank]`` - functions 

 

EXAMPLES:: 

 

sage: import sage.combinat.ranker as ranker 

sage: l = [1,2,3] 

sage: r,u = ranker.from_list(l) 

sage: r(1) 

0 

sage: r(3) 

2 

sage: u(2) 

3 

sage: u(0) 

1 

""" 

return [rank_from_list(l), unrank_from_list(l)] 

 

 

def rank_from_list(l): 

r""" 

Return a rank function for the elements of ``l``. 

 

INPUT: 

 

- ``l`` -- a duplicate free list (or iterable) of hashable objects 

 

OUTPUT: 

 

- a function from the elements of ``l`` to ``0,...,len(l)`` 

 

EXAMPLES:: 

 

sage: import sage.combinat.ranker as ranker 

sage: l = ['a', 'b', 'c'] 

sage: r = ranker.rank_from_list(l) 

sage: r('a') 

0 

sage: r('c') 

2 

 

For non elements a ``ValueError`` is raised, as with the usual 

``index`` method of lists:: 

 

sage: r('blah') 

Traceback (most recent call last): 

... 

ValueError: 'blah' is not in dict 

 

Currently, the rank function is a 

:class:`~sage.misc.callable_dict.CallableDict`; but this is an 

implementation detail:: 

 

sage: type(r) 

<... 'sage.misc.callable_dict.CallableDict'> 

sage: r 

{'a': 0, 'c': 2, 'b': 1} 

 

With the current implementation, no error is issued in case of 

duplicate value in ``l``. Instead, the rank function returns the 

position of some of the duplicates:: 

 

sage: r = ranker.rank_from_list(['a', 'b', 'a', 'c']) 

sage: r('a') 

2 

 

Constructing the rank function itself is of complexity 

``O(len(l))``. Then, each call to the rank function consists of an 

essentially constant time dictionary lookup. 

 

TESTS:: 

 

sage: TestSuite(r).run() 

""" 

return CallableDict((x,i) for i,x in enumerate(l)) 

 

def unrank_from_list(l): 

""" 

Returns an unrank function from a list. 

 

EXAMPLES:: 

 

sage: import sage.combinat.ranker as ranker 

sage: l = [1,2,3] 

sage: u = ranker.unrank_from_list(l) 

sage: u(2) 

3 

sage: u(0) 

1 

""" 

unrank = lambda j: l[j] 

return unrank 

 

def on_fly(): 

""" 

Returns a pair of enumeration functions rank / unrank. 

 

rank assigns on the fly an integer, starting from 0, to any object 

passed as argument. The object should be hashable. unrank is the 

inverse function; it returns None for indices that have not yet 

been assigned. 

 

EXAMPLES:: 

 

sage: [rank, unrank] = sage.combinat.ranker.on_fly() 

sage: rank('a') 

0 

sage: rank('b') 

1 

sage: rank('c') 

2 

sage: rank('a') 

0 

sage: unrank(2) 

'c' 

sage: unrank(3) 

sage: rank('d') 

3 

sage: unrank(3) 

'd' 

 

.. todo:: add tests as in combinat::rankers 

""" 

def count(): 

i = 0 

while True: 

yield i 

i+=1 

 

counter = count() 

 

@cached_function 

def rank(x): 

i = next(counter) 

unrank.set_cache(x, i) 

return i 

 

@cached_function 

def unrank(i): 

return None 

 

return [rank, unrank] 

 

def unrank(L, i): 

r""" 

Return the `i`-th element of `L`. 

 

INPUT: 

 

- ``L`` -- a list, tuple, finite enumerated set, ... 

- ``i`` -- an int or :class:`Integer` 

 

The purpose of this utility is to give a uniform idiom to recover 

the `i`-th element of an object ``L``, whether ``L`` is a list, 

tuple (or more generally a :class:`collections.Sequence`), an 

enumerated set, some old parent of Sage still implementing 

unranking in the method ``__getitem__``, or an iterable (see 

:class:`collections.Iterable`). See :trac:`15919`. 

 

EXAMPLES: 

 

Lists, tuples, and other :class:`sequences <collections.Sequence>`:: 

 

sage: from sage.combinat.ranker import unrank 

sage: unrank(['a','b','c'], 2) 

'c' 

sage: unrank(('a','b','c'), 1) 

'b' 

sage: unrank(range(3,13,2), 1) 

5 

 

Enumerated sets:: 

 

sage: unrank(GF(7), 2) 

2 

sage: unrank(IntegerModRing(29), 10) 

10 

 

An iterable:: 

 

sage: unrank(NN,4) 

4 

 

An iterator:: 

 

sage: unrank(('a{}'.format(i) for i in range(20)), 0) 

'a0' 

sage: unrank(('a{}'.format(i) for i in range(20)), 2) 

'a2' 

 

.. WARNING:: 

 

When unranking an iterator, it returns the ``i``-th element 

beyond where it is currently at:: 

 

sage: from sage.combinat.ranker import unrank 

sage: it = iter(range(20)) 

sage: unrank(it, 2) 

2 

sage: unrank(it, 2) 

5 

 

TESTS:: 

 

sage: from sage.combinat.ranker import unrank 

sage: unrank(list(range(3)), 10) 

Traceback (most recent call last): 

... 

IndexError: list index out of range 

 

sage: unrank(('a{}'.format(i) for i in range(20)), 22) 

Traceback (most recent call last): 

... 

IndexError: index out of range 

""" 

if L in EnumeratedSets: 

return L.unrank(i) 

if isinstance(L, Sequence): 

return L[i] 

if isinstance(L, Parent): 

# handle parents still implementing unranking in __getitem__ 

try: 

return L[i] 

except (AttributeError, TypeError, ValueError): 

pass 

if isinstance(L, Iterable): 

try: 

it = iter(L) 

for _ in range(i): 

next(it) 

return next(it) 

except StopIteration as e: 

raise IndexError("index out of range") 

raise ValueError("Don't know how to unrank on {}".format(L))