Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/categories/examples/posets.py
4057 views
1
"""
2
Examples of posets
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2011 Nicolas M. Thiery <nthiery at users.sf.net>
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# http://www.gnu.org/licenses/
9
#******************************************************************************
10
11
from sage.structure.parent import Parent
12
from sage.structure.unique_representation import UniqueRepresentation
13
from sage.categories.all import Posets
14
from sage.structure.element_wrapper import ElementWrapper
15
from sage.sets.set import Set, Set_object_enumerated
16
from sage.sets.positive_integers import PositiveIntegers
17
18
class FiniteSetsOrderedByInclusion(UniqueRepresentation, Parent):
19
r"""
20
An example of a poset: finite sets ordered by inclusion
21
22
This class provides a minimal implementation of a poset
23
24
EXAMPLES::
25
26
sage: P = Posets().example(); P
27
An example of a poset: sets ordered by inclusion
28
29
We conclude by running systematic tests on this poset::
30
31
sage: TestSuite(P).run(verbose = True)
32
running ._test_an_element() . . . pass
33
running ._test_category() . . . pass
34
running ._test_elements() . . .
35
Running the test suite of self.an_element()
36
running ._test_category() . . . pass
37
running ._test_eq() . . . pass
38
running ._test_not_implemented_methods() . . . pass
39
running ._test_pickling() . . . pass
40
pass
41
running ._test_elements_eq() . . . pass
42
running ._test_eq() . . . pass
43
running ._test_not_implemented_methods() . . . pass
44
running ._test_pickling() . . . pass
45
running ._test_some_elements() . . . pass
46
"""
47
48
def __init__(self):
49
r"""
50
EXAMPLES::
51
52
sage: P = Posets().example(); P
53
An example of a poset: sets ordered by inclusion
54
sage: P.category()
55
Category of posets
56
sage: type(P)
57
<class 'sage.categories.examples.posets.FiniteSetsOrderedByInclusion_with_category'>
58
sage: TestSuite(P).run()
59
"""
60
Parent.__init__(self, category = Posets())
61
62
def _repr_(self):
63
r"""
64
TESTS::
65
66
sage: S = Posets().example()
67
sage: S._repr_()
68
'An example of a poset: sets ordered by inclusion'
69
"""
70
return "An example of a poset: sets ordered by inclusion"
71
72
def le(self, x, y):
73
r"""
74
Returns whether `x` is a subset of `y`
75
76
EXAMPLES::
77
78
sage: P = Posets().example()
79
sage: P.le( P(Set([1,3])), P(Set([1,2,3])) )
80
True
81
sage: P.le( P(Set([1,3])), P(Set([1,3])) )
82
True
83
sage: P.le( P(Set([1,2])), P(Set([1,3])) )
84
False
85
"""
86
return x.value.issubset(y.value)
87
88
def an_element(self):
89
r"""
90
Returns an element of this poset
91
92
EXAMPLES::
93
94
sage: B = Posets().example()
95
sage: B.an_element()
96
{1, 4, 6}
97
"""
98
return self(Set([1,4,6]))
99
100
class Element(ElementWrapper):
101
102
wrapped_class = Set_object_enumerated
103
104
class PositiveIntegersOrderedByDivisibilityFacade(UniqueRepresentation, Parent):
105
r"""
106
An example of a facade poset: the positive integers ordered by divisibility
107
108
This class provides a minimal implementation of a facade poset
109
110
EXAMPLES::
111
112
sage: P = Posets().example("facade"); P
113
An example of a facade poset: the positive integers ordered by divisibility
114
115
sage: P(5)
116
5
117
sage: P(0)
118
Traceback (most recent call last):
119
...
120
ValueError: Can't coerce `0` in any parent `An example of a facade poset: the positive integers ordered by divisibility` is a facade for
121
122
sage: 3 in P
123
True
124
sage: 0 in P
125
False
126
"""
127
128
element_class = type(Set([]))
129
130
def __init__(self):
131
r"""
132
EXAMPLES::
133
134
sage: P = Posets().example("facade"); P
135
An example of a facade poset: the positive integers ordered by divisibility
136
sage: P.category()
137
Category of facade posets
138
sage: type(P)
139
<class 'sage.categories.examples.posets.PositiveIntegersOrderedByDivisibilityFacade_with_category'>
140
sage: TestSuite(P).run()
141
"""
142
Parent.__init__(self, facade = (PositiveIntegers(),), category = Posets())
143
144
def _repr_(self):
145
r"""
146
TESTS::
147
148
sage: S = Posets().example("facade")
149
sage: S._repr_()
150
'An example of a facade poset: the positive integers ordered by divisibility'
151
"""
152
return "An example of a facade poset: the positive integers ordered by divisibility"
153
154
def le(self, x, y):
155
r"""
156
Returns whether `x` is divisible by `y`
157
158
EXAMPLES::
159
160
sage: P = Posets().example("facade")
161
sage: P.le(3, 6)
162
True
163
sage: P.le(3, 3)
164
True
165
sage: P.le(3, 7)
166
False
167
"""
168
return x.divides(y)
169
170