Signed compositions - Combinatorics

Skip to content
Back to top
View this page
Edit this page

# Signed compositions

class sage.combinat.composition_signed.SignedCompositions(n)

  Bases: `Compositions_n`

  The class of signed compositions of \(n\).

  EXAMPLES:

  Sage

      sage: SC3 = SignedCompositions(3); SC3
      Signed compositions of 3
      sage: SC3.cardinality()
      18
      sage: len(SC3.list())
      18
      sage: SC3.first()
      [1, 1, 1]
      sage: SC3.last()
      [-3]
      sage: SC3.random_element() # random
      [1, -1, 1]
      sage: SC3.list()
      [[1, 1, 1],
       [1, 1, -1],
       [1, -1, 1],
       [1, -1, -1],
       [-1, 1, 1],
       [-1, 1, -1],
       [-1, -1, 1],
       [-1, -1, -1],
       [1, 2],
       [1, -2],
       [-1, 2],
       [-1, -2],
       [2, 1],
       [2, -1],
       [-2, 1],
       [-2, -1],
       [3],
       [-3]]

  Python

      >>> from sage.all import *
      >>> SC3 = SignedCompositions(Integer(3)); SC3
      Signed compositions of 3
      >>> SC3.cardinality()
      18
      >>> len(SC3.list())
      18
      >>> SC3.first()
      [1, 1, 1]
      >>> SC3.last()
      [-3]
      >>> SC3.random_element() # random
      [1, -1, 1]
      >>> SC3.list()
      [[1, 1, 1],
       [1, 1, -1],
       [1, -1, 1],
       [1, -1, -1],
       [-1, 1, 1],
       [-1, 1, -1],
       [-1, -1, 1],
       [-1, -1, -1],
       [1, 2],
       [1, -2],
       [-1, 2],
       [-1, -2],
       [2, 1],
       [2, -1],
       [-2, 1],
       [-2, -1],
       [3],
       [-3]]

  cardinality()

    Return the number of elements in `self`.

    The number of signed compositions of \(n\) is equal to

    \[\sum_{i=1}^{n+1} \binom{n-1}{i-1} 2^i\]

    EXAMPLES:

    Sage

        sage: SC4 = SignedCompositions(4)
        sage: SC4.cardinality() == len(SC4.list())
        True
        sage: SignedCompositions(3).cardinality()
        18

    Python

        >>> from sage.all import *
        >>> SC4 = SignedCompositions(Integer(4))
        >>> SC4.cardinality() == len(SC4.list())
        True
        >>> SignedCompositions(Integer(3)).cardinality()
        18