Path: blob/master/src/sage/combinat/composition_signed.py
8815 views
r"""1Signed Compositions2"""3#*****************************************************************************4# Copyright (C) 2007 Mike Hansen <[email protected]>,5#6# Distributed under the terms of the GNU General Public License (GPL)7#8# This code is distributed in the hope that it will be useful,9# but WITHOUT ANY WARRANTY; without even the implied warranty of10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11# General Public License for more details.12#13# The full text of the GPL is available at:14#15# http://www.gnu.org/licenses/16#*****************************************************************************1718from composition import Compositions_n, Composition19import cartesian_product20from sage.rings.all import binomial, Integer21import __builtin__2223class SignedCompositions(Compositions_n):24"""25The class of signed compositions of `n`.2627EXAMPLES::2829sage: SC3 = SignedCompositions(3); SC330Signed compositions of 331sage: SC3.cardinality()321833sage: len(SC3.list())341835sage: SC3.first()36[1, 1, 1]37sage: SC3.last()38[-3]39sage: SC3.random_element()40[1, -1, 1]41sage: SC3.list()42[[1, 1, 1],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, 2],51[1, -2],52[-1, 2],53[-1, -2],54[2, 1],55[2, -1],56[-2, 1],57[-2, -1],58[3],59[-3]]6061TESTS::6263sage: SC = SignedCompositions(3)64sage: TestSuite(SC).run()65"""66def __repr__(self):67"""68TESTS::6970sage: SignedCompositions(3)71Signed compositions of 372"""73return "Signed compositions of %s"%self.n7475def __contains__(self, x):76"""77TESTS::7879sage: [] in SignedCompositions(0)80True81sage: [0] in SignedCompositions(0)82False83sage: [2,1,3] in SignedCompositions(6)84True85sage: [-2, 1, -3] in SignedCompositions(6)86True87"""88if isinstance(x, __builtin__.list):89for i in range(len(x)):90if (not isinstance(x[i], (int, Integer))) and x[i] not in ZZ:91return False92if x[i] == 0:93return False94elif not isinstance(x, Composition):95return False96return sum([abs(i) for i in x]) == self.n9798def cardinality(self):99r"""100Return the number of elements in ``self``.101102The number of signed compositions of `n` is equal to103104.. MATH::105106\sum_{i=1}^{n+1} \binom{n-1}{i-1} 2^i107108TESTS::109110sage: SC4 = SignedCompositions(4)111sage: SC4.cardinality() == len(SC4.list())112True113sage: SignedCompositions(3).cardinality()11418115"""116return sum([binomial(self.n-1, i-1)*2**(i) for i in range(1, self.n+1)])117118def __iter__(self):119"""120TESTS::121122sage: SignedCompositions(0).list() #indirect doctest123[[]]124sage: SignedCompositions(1).list() #indirect doctest125[[1], [-1]]126sage: SignedCompositions(2).list() #indirect doctest127[[1, 1], [1, -1], [-1, 1], [-1, -1], [2], [-2]]128"""129for comp in Compositions_n.__iter__(self):130l = len(comp)131a = [[1,-1] for i in range(l)]132for sign in cartesian_product.CartesianProduct(*a):133yield [ sign[i]*comp[i] for i in range(l)]134135from sage.structure.sage_object import register_unpickle_override136register_unpickle_override('sage.combinat.composition_signed', 'SignedCompositions_n', SignedCompositions)137138139140