Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/combinat/finite_class.py
4045 views
1
r"""
2
Finite combinatorial classes
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2007 Mike Hansen <[email protected]>,
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
#
9
# This code is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# General Public License for more details.
13
#
14
# The full text of the GPL is available at:
15
#
16
# http://www.gnu.org/licenses/
17
#*****************************************************************************
18
from combinat import CombinatorialClass
19
20
class FiniteCombinatorialClass(CombinatorialClass):
21
"""
22
INPUT:
23
- l a list or iterable
24
25
Returns l, wrapped as a combinatorial class
26
27
EXAMPLES::
28
29
sage: F = FiniteCombinatorialClass([1,2,3])
30
sage: F.list()
31
[1, 2, 3]
32
sage: F.cardinality()
33
3
34
sage: F.random_element()
35
1
36
sage: F.first()
37
1
38
sage: F.last()
39
3
40
"""
41
def __init__(self, l):
42
"""
43
TESTS::
44
45
sage: F = FiniteCombinatorialClass([1,2,3])
46
sage: F == loads(dumps(F))
47
True
48
"""
49
self.l = list(l) # Probably would be better to use a tuple
50
51
def _element_constructor_(self, x):
52
"""
53
EXAMPLES::
54
55
sage: F = FiniteCombinatorialClass([1,2,3])
56
sage: F._element_constructor_(1)
57
1
58
sage: F(1)
59
1
60
"""
61
return x
62
63
def __repr__(self):
64
"""
65
TESTS::
66
67
sage: F = FiniteCombinatorialClass([1,2,3])
68
sage: repr(F)
69
'Combinatorial class with elements in [1, 2, 3]'
70
"""
71
return "Combinatorial class with elements in %s"%self.l
72
73
def __contains__(self, x):
74
"""
75
EXAMPLES::
76
77
sage: F = FiniteCombinatorialClass([1,2,3])
78
sage: 1 in F
79
True
80
sage: 2 in F
81
True
82
sage: 4 in F
83
False
84
sage: ZZ in F
85
False
86
"""
87
return x in self.l
88
89
def list(self):
90
"""
91
TESTS::
92
93
sage: F = FiniteCombinatorialClass([1,2,3])
94
sage: F.list()
95
[1, 2, 3]
96
"""
97
return self.l
98
99
def cardinality(self):
100
"""
101
EXAMPLES::
102
103
sage: F = FiniteCombinatorialClass([1,2,3])
104
sage: F.cardinality()
105
3
106
"""
107
return len(self.l)
108
109
110
def __getitem__(self, i): # TODO: optimize
111
"""
112
EXAMPLES::
113
114
sage: F = FiniteCombinatorialClass(["a", "b", "c"])
115
sage: F[2]
116
'c'
117
"""
118
return self.l[i]
119
120
def keys(self):
121
"""
122
EXAMPLES::
123
124
sage: F = FiniteCombinatorialClass([1,2,3])
125
sage: F.keys()
126
[0, 1, 2]
127
"""
128
return range(len(self.l))
129
130
# Backward compatibility pointer
131
# Needed for unpickling.
132
FiniteCombinatorialClass_l = FiniteCombinatorialClass
133
134