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

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

""" 

Totally Ordered Finite Sets 

 

AUTHORS: 

 

- Stepan Starosta (2012): Initial version 

""" 

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

# Copyright (C) 2012 Stepan Starosta <stepan.starosta@gmail.com> 

# 

# 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 __future__ import print_function 

 

from sage.structure.element import Element 

from sage.structure.parent import Parent 

from sage.structure.richcmp import richcmp, rich_to_bool 

from sage.sets.finite_enumerated_set import FiniteEnumeratedSet 

from sage.categories.posets import Posets 

from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets 

 

 

class TotallyOrderedFiniteSetElement(Element): 

""" 

Element of a finite totally ordered set. 

 

EXAMPLES:: 

 

sage: S = TotallyOrderedFiniteSet([2,7], facade=False) 

sage: x = S(2) 

sage: print(x) 

2 

sage: x.parent() 

{2, 7} 

""" 

def __init__(self, parent, data): 

r""" 

TESTS:: 

 

sage: T = TotallyOrderedFiniteSet([3,2,1],facade=False) 

sage: TestSuite(T.an_element()).run() 

""" 

Element.__init__(self, parent) 

self.value = data 

 

def __eq__(self, other): 

r""" 

Equality. 

 

EXAMPLES:: 

 

sage: A = TotallyOrderedFiniteSet(['gaga',1], facade=False) 

sage: A('gaga') == 'gaga' #indirect doctest 

False 

sage: 'gaga' == A('gaga') 

False 

sage: A('gaga') == A('gaga') 

True 

""" 

try: 

same_parent = self.parent() is other.parent() 

except AttributeError: 

return False 

 

if not same_parent: 

return False 

return other.value == self.value 

 

def __ne__(self, other): 

r""" 

Non-equality. 

 

EXAMPLES:: 

 

sage: A = TotallyOrderedFiniteSet(['gaga',1], facade=False) 

sage: A('gaga') != 'gaga' #indirect doctest 

True 

""" 

return not (self == other) 

 

def _richcmp_(self, other, op): 

r""" 

Comparison. 

 

For ``self`` and ``other`` that have the same parent the method compares 

their rank. 

 

TESTS:: 

 

sage: A = TotallyOrderedFiniteSet([3,2,7], facade=False) 

sage: A(3) < A(2) and A(3) <= A(2) and A(2) <= A(2) 

True 

sage: A(2) > A(3) and A(2) >= A(3) and A(7) >= A(7) 

True 

sage: A(3) >= A(7) or A(2) > A(2) 

False 

sage: A(7) < A(2) or A(2) <= A(3) or A(2) < A(2) 

False 

""" 

if self.value == other.value: 

return rich_to_bool(op, 0) 

return richcmp(self.rank(), other.rank(), op) 

 

def _repr_(self): 

r""" 

String representation. 

 

TESTS:: 

 

sage: A = TotallyOrderedFiniteSet(['gaga',1], facade=False) 

sage: repr(A('gaga')) #indirect doctest 

"'gaga'" 

 

""" 

return repr(self.value) 

 

def __str__(self): 

r""" 

String that represents self. 

 

EXAMPLES:: 

 

sage: A = TotallyOrderedFiniteSet(['gaga',1], facade=False) 

sage: str(A('gaga')) #indirect doctest 

'gaga' 

""" 

return str(self.value) 

 

 

class TotallyOrderedFiniteSet(FiniteEnumeratedSet): 

""" 

Totally ordered finite set. 

 

This is a finite enumerated set assuming that the elements are 

ordered based upon their rank (i.e. their position in the set). 

 

INPUT: 

 

- ``elements`` -- A list of elements in the set 

 

- ``facade`` -- (default: ``True``) if ``True``, a facade is used; it 

should be set to ``False`` if the elements do not inherit from 

:class:`~sage.structure.element.Element` or if you want a funny order. See 

examples for more details. 

 

.. SEEALSO:: 

 

:class:`FiniteEnumeratedSet` 

 

EXAMPLES:: 

 

sage: S = TotallyOrderedFiniteSet([1,2,3]) 

sage: S 

{1, 2, 3} 

sage: S.cardinality() 

3 

 

By default, totally ordered finite set behaves as a facade:: 

 

sage: S(1).parent() 

Integer Ring 

 

It makes comparison fails when it is not the standard order:: 

 

sage: T1 = TotallyOrderedFiniteSet([3,2,5,1]) 

sage: T1(3) < T1(1) 

False 

sage: T2 = TotallyOrderedFiniteSet([3,var('x')]) 

sage: T2(3) < T2(var('x')) 

3 < x 

 

To make the above example work, you should set the argument facade to 

``False`` in the constructor. In that case, the elements of the set have a 

dedicated class:: 

 

sage: A = TotallyOrderedFiniteSet([3,2,0,'a',7,(0,0),1], facade=False) 

sage: A 

{3, 2, 0, 'a', 7, (0, 0), 1} 

sage: x = A.an_element() 

sage: x 

3 

sage: x.parent() 

{3, 2, 0, 'a', 7, (0, 0), 1} 

sage: A(3) < A(2) 

True 

sage: A('a') < A(7) 

True 

sage: A(3) > A(2) 

False 

sage: A(1) < A(3) 

False 

sage: A(3) == A(3) 

True 

 

But then, the equality comparison is always False with elements outside of 

the set:: 

 

sage: A(1) == 1 

False 

sage: 1 == A(1) 

False 

sage: 'a' == A('a') 

False 

sage: A('a') == 'a' 

False 

 

Since :trac:`16280`, totally ordered sets support elements that do 

not inherit from :class:`sage.structure.element.Element`, whether 

they are facade or not:: 

 

sage: S = TotallyOrderedFiniteSet(['a','b']) 

sage: S('a') 

'a' 

sage: S = TotallyOrderedFiniteSet(['a','b'], facade = False) 

sage: S('a') 

'a' 

 

Multiple elements are automatically deleted:: 

 

sage: TotallyOrderedFiniteSet([1,1,2,1,2,2,5,4]) 

{1, 2, 5, 4} 

""" 

Element = TotallyOrderedFiniteSetElement 

 

@staticmethod 

def __classcall__(cls, iterable, facade=True): 

""" 

Standard trick to expand the iterable upon input, and 

guarantees unique representation, independently of the type of 

the iterable. See ``UniqueRepresentation``. 

 

TESTS:: 

 

sage: S1 = TotallyOrderedFiniteSet([1, 2, 3]) 

sage: S2 = TotallyOrderedFiniteSet((1, 2, 3)) 

sage: S3 = TotallyOrderedFiniteSet((x for x in range(1,4))) 

sage: S1 is S2 

True 

sage: S2 is S3 

True 

""" 

elements = [] 

seen = set() 

for x in iterable: 

if x not in seen: 

elements.append(x) 

seen.add(x) 

return super(FiniteEnumeratedSet, cls).__classcall__( 

cls, 

tuple(elements), 

facade) 

 

def __init__(self, elements, facade=True): 

""" 

Initialize ``self``. 

 

TESTS:: 

 

sage: TestSuite(TotallyOrderedFiniteSet([1,2,3])).run() 

sage: TestSuite(TotallyOrderedFiniteSet([1,2,3],facade=False)).run() 

sage: TestSuite(TotallyOrderedFiniteSet([1,3,2],facade=False)).run() 

sage: TestSuite(TotallyOrderedFiniteSet([])).run() 

""" 

Parent.__init__(self, facade = facade, category = (Posets(),FiniteEnumeratedSets())) 

self._elements = elements 

if facade: 

self._facade_elements = None 

else: 

self._facade_elements = self._elements 

self._elements = [self.element_class(self,x) for x in elements] 

 

def _element_constructor_(self, data): 

r""" 

Build an element of that set from ``data``. 

 

EXAMPLES:: 

 

sage: S1 = TotallyOrderedFiniteSet([1,2,3]) 

sage: x = S1(1); x # indirect doctest 

1 

sage: x.parent() 

Integer Ring 

 

 

sage: S2 = TotallyOrderedFiniteSet([3,2,1], facade=False) 

sage: y = S2(1); y # indirect doctest 

1 

sage: y.parent() 

{3, 2, 1} 

sage: y in S2 

True 

sage: S2(y) is y 

True 

""" 

if self._facade_elements is None: 

return FiniteEnumeratedSet._element_constructor_(self, data) 

 

try: 

i = self._facade_elements.index(data) 

except ValueError: 

raise ValueError("%s not in %s"%(data, self)) 

 

return self._elements[i] 

 

def le(self, x, y): 

r""" 

Return ``True`` if `x \le y` for the order of ``self``. 

 

EXAMPLES:: 

 

sage: T = TotallyOrderedFiniteSet([1,3,2], facade=False) 

sage: T1, T3, T2 = T.list() 

sage: T.le(T1,T3) 

True 

sage: T.le(T3,T2) 

True 

""" 

try: 

return self._elements.index(x) <= self._elements.index(y) 

except Exception: 

raise ValueError("arguments must be elements of the set")