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

r""" 

Elements of posets, lattices, semilattices, etc. 

""" 

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

# Copyright (C) 2008 Peter Jipsen <jipsen@chapman.edu>, 

# Franco Saliola <saliola@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 sage.structure.element import Element 

from sage.structure.element import have_same_parent 

 

 

class PosetElement(Element): 

 

def __init__(self, poset, element, vertex): 

r""" 

Establish the parent-child relationship between ``poset`` 

and ``element``, where ``element`` is associated to the 

vertex ``vertex`` of the Hasse diagram of the poset. 

 

INPUT: 

 

- ``poset`` -- a poset object 

 

- ``element`` -- any object 

 

- ``vertex`` -- a vertex of the Hasse diagram of the poset 

 

TESTS:: 

 

sage: from sage.combinat.posets.elements import PosetElement 

sage: P = Poset([[1,2],[4],[3],[4],[]], facade = False) 

sage: e = P(0) 

sage: e.parent() is P 

True 

sage: TestSuite(e).run() 

""" 

Element.__init__(self, poset) 

if isinstance(element, self.parent().element_class): 

self.element = element.element 

else: 

self.element = element 

self.vertex = vertex 

 

def __hash__(self): 

r""" 

TESTS:: 

 

sage: P = Poset([[1,2],[4],[3],[4],[]], facade = False) 

sage: e = P(0) 

sage: hash(e) 

0 

""" 

return hash(self.element) 

 

def _repr_(self): 

""" 

TESTS:: 

 

sage: Poset([[1,2],[4],[3],[4],[]], facade = False)(0)._repr_() 

'0' 

""" 

return "%s" % str(self.element) 

 

def _latex_(self): 

r""" 

Return the latex code of the poset element. 

 

EXAMPLES:: 

 

sage: m = matrix(2,[1,2,3,4]) 

sage: m.set_immutable() 

sage: P = Poset(([m],[]), facade = False) 

sage: [e] = P 

sage: type(e) 

<class 'sage.combinat.posets.posets.FinitePoset_with_category.element_class'> 

sage: latex(e) #indirect doctest 

\left(\begin{array}{rr} 

1 & 2 \\ 

3 & 4 

\end{array}\right) 

""" 

from sage.misc.latex import latex 

return latex(self.element) 

 

def __eq__(self, other): 

""" 

TESTS:: 

 

sage: P = Poset([["a","b"],["d"],["c"],["d"],[]], facade = False) 

sage: Q = Poset([["a","b"],["d"],["c"],[],[]], facade = False) 

sage: P(0).__eq__(P(4)) 

False 

sage: from sage.combinat.posets.elements import PosetElement 

sage: PosetElement(P,0,"c") == PosetElement(P,0,"c") 

True 

sage: PosetElement(P,0,"c") == PosetElement(Q,0,"c") 

False 

sage: PosetElement(P,0,"b") == PosetElement(P,0,"c") 

False 

 

.. warning:: as an optimization, this only compares the parent 

and vertex, using the invariant that, in a proper poset 

element, ``self.element == other.element`` if and only 

``self.vertex == other.vertex``:: 

 

sage: PosetElement(P,1,"c") == PosetElement(P,0,"c") 

True 

 

Test that :trac:`12351` is fixed:: 

 

sage: P(0) == int(0) 

False 

""" 

# This should instead exploit unique representation, using 

# self is other, or best inherit __eq__ from there. But there 

# are issues around pickling and rich comparison functions. 

return have_same_parent(self, other) \ 

and self.vertex == other.vertex 

 

def __ne__(self, other): 

r""" 

TESTS:: 

 

sage: P = Poset([[1,2],[4],[3],[4],[]]) 

sage: P = Poset([["a","b"],["d"],["c"],["d"],[]]) 

sage: P(0).__ne__(P(4)) 

True 

sage: from sage.combinat.posets.elements import PosetElement 

sage: PosetElement(P,0,"c") != PosetElement(P,0,"c") 

False 

sage: PosetElement(P,0,"b") != PosetElement(P,0,"c") 

True 

 

For this one, see comment in :meth:`__eq__`:: 

 

sage: PosetElement(P,1,"c") != PosetElement(P,0,"c") 

False 

""" 

return not self == other 

 

def _cmp(self, other): 

""" 

TESTS:: 

 

sage: P = Poset([[1,2],[4],[3],[4],[]], facade = False) 

sage: P(0)._cmp(P(4)) 

-1 

sage: P(4)._cmp(P(0)) 

1 

sage: P(0)._cmp(P(0)) 

0 

sage: P(1)._cmp(P(2)) 

 

""" 

return self.parent().compare_elements(self, other) 

 

def __lt__(self, other): 

""" 

TESTS 

 

:: 

 

sage: dag = DiGraph({0:[2,3], 1:[3,4], 2:[5], 3:[5], 4:[5]}) 

sage: P = Poset(dag, facade = False) 

sage: P(0) < P(1) 

False 

sage: P(4) < P(1) 

False 

sage: P(0) < P(0) 

False 

""" 

return self._cmp(other) == -1 or False 

 

def __le__(self, other): 

""" 

TESTS 

 

:: 

 

sage: dag = DiGraph({0:[2,3], 1:[3,4], 2:[5], 3:[5], 4:[5]}) 

sage: P = Poset(dag, facade = False) 

sage: P(1) <= P(0) 

False 

sage: P(0) <= P(1) 

False 

sage: P(0) <= P(3) 

True 

sage: P(0) <= P(0) 

True 

""" 

return self == other or self._cmp(other) == -1 or False 

 

def __gt__(self, other): 

""" 

TESTS 

 

:: 

 

sage: dag = DiGraph({0:[2,3], 1:[3,4], 2:[5], 3:[5], 4:[5]}) 

sage: P = Poset(dag) 

sage: P(0).__gt__(P(5)) 

False 

sage: P(5).__gt__(P(0)) 

True 

sage: P(0).__gt__(P(0)) 

False 

""" 

return self._cmp(other) == 1 or False 

 

def __ge__(self, other): 

""" 

TESTS 

 

:: 

 

sage: dag = DiGraph({0:[2,3], 1:[3,4], 2:[5], 3:[5], 4:[5]}) 

sage: P = Poset(dag) 

sage: P(0).__ge__(P(5)) 

False 

sage: P(5).__ge__(P(0)) 

True 

sage: P(0).__ge__(P(0)) 

True 

""" 

return self == other or self._cmp(other) == 1 or False 

 

 

class MeetSemilatticeElement(PosetElement): 

def __mul__(self, other): 

r""" 

Return the meet of ``self`` and ``other`` in the lattice. 

 

EXAMPLES:: 

 

sage: D = posets.DiamondPoset(5,facade=False) 

sage: D(1) * D(2) 

0 

sage: D(1) * D(1) 

1 

sage: D(1) * D(0) 

0 

sage: D(1) * D(4) 

1 

""" 

return self.parent().meet(self, other) 

 

 

class JoinSemilatticeElement(PosetElement): 

def __add__(self, other): 

r""" 

Return the join of ``self`` and ``other`` in the lattice. 

 

EXAMPLES:: 

 

sage: D = posets.DiamondPoset(5,facade=False) 

sage: D(1) + D(2) 

4 

sage: D(1) + D(1) 

1 

sage: D(1) + D(4) 

4 

sage: D(1) + D(0) 

1 

""" 

return self.parent().join(self, other) 

 

 

class LatticePosetElement(MeetSemilatticeElement, JoinSemilatticeElement): 

pass