Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/combinat/composition_signed.py
8815 views
1
r"""
2
Signed Compositions
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
19
from composition import Compositions_n, Composition
20
import cartesian_product
21
from sage.rings.all import binomial, Integer
22
import __builtin__
23
24
class SignedCompositions(Compositions_n):
25
"""
26
The class of signed compositions of `n`.
27
28
EXAMPLES::
29
30
sage: SC3 = SignedCompositions(3); SC3
31
Signed compositions of 3
32
sage: SC3.cardinality()
33
18
34
sage: len(SC3.list())
35
18
36
sage: SC3.first()
37
[1, 1, 1]
38
sage: SC3.last()
39
[-3]
40
sage: SC3.random_element()
41
[1, -1, 1]
42
sage: SC3.list()
43
[[1, 1, 1],
44
[1, 1, -1],
45
[1, -1, 1],
46
[1, -1, -1],
47
[-1, 1, 1],
48
[-1, 1, -1],
49
[-1, -1, 1],
50
[-1, -1, -1],
51
[1, 2],
52
[1, -2],
53
[-1, 2],
54
[-1, -2],
55
[2, 1],
56
[2, -1],
57
[-2, 1],
58
[-2, -1],
59
[3],
60
[-3]]
61
62
TESTS::
63
64
sage: SC = SignedCompositions(3)
65
sage: TestSuite(SC).run()
66
"""
67
def __repr__(self):
68
"""
69
TESTS::
70
71
sage: SignedCompositions(3)
72
Signed compositions of 3
73
"""
74
return "Signed compositions of %s"%self.n
75
76
def __contains__(self, x):
77
"""
78
TESTS::
79
80
sage: [] in SignedCompositions(0)
81
True
82
sage: [0] in SignedCompositions(0)
83
False
84
sage: [2,1,3] in SignedCompositions(6)
85
True
86
sage: [-2, 1, -3] in SignedCompositions(6)
87
True
88
"""
89
if isinstance(x, __builtin__.list):
90
for i in range(len(x)):
91
if (not isinstance(x[i], (int, Integer))) and x[i] not in ZZ:
92
return False
93
if x[i] == 0:
94
return False
95
elif not isinstance(x, Composition):
96
return False
97
return sum([abs(i) for i in x]) == self.n
98
99
def cardinality(self):
100
r"""
101
Return the number of elements in ``self``.
102
103
The number of signed compositions of `n` is equal to
104
105
.. MATH::
106
107
\sum_{i=1}^{n+1} \binom{n-1}{i-1} 2^i
108
109
TESTS::
110
111
sage: SC4 = SignedCompositions(4)
112
sage: SC4.cardinality() == len(SC4.list())
113
True
114
sage: SignedCompositions(3).cardinality()
115
18
116
"""
117
return sum([binomial(self.n-1, i-1)*2**(i) for i in range(1, self.n+1)])
118
119
def __iter__(self):
120
"""
121
TESTS::
122
123
sage: SignedCompositions(0).list() #indirect doctest
124
[[]]
125
sage: SignedCompositions(1).list() #indirect doctest
126
[[1], [-1]]
127
sage: SignedCompositions(2).list() #indirect doctest
128
[[1, 1], [1, -1], [-1, 1], [-1, -1], [2], [-2]]
129
"""
130
for comp in Compositions_n.__iter__(self):
131
l = len(comp)
132
a = [[1,-1] for i in range(l)]
133
for sign in cartesian_product.CartesianProduct(*a):
134
yield [ sign[i]*comp[i] for i in range(l)]
135
136
from sage.structure.sage_object import register_unpickle_override
137
register_unpickle_override('sage.combinat.composition_signed', 'SignedCompositions_n', SignedCompositions)
138
139
140