Combinatorial classes of words.

To define a new class of words, please refer to the documentation file: sage/combinat/words/notes/word_inheritance_howto.txt

AUTHORS:

  • Franco Saliola (2008-12-17): merged into sage
  • Sebastien Labbe (2008-12-17): merged into sage
  • Arnaud Bergeron (2008-12-17): merged into sage
  • Sebastien Labbe (2009-07-21): Improved morphism iterator (#6571).

EXAMPLES:

sage: Words()
Words
sage: Words(5)
Words over Ordered Alphabet [1, 2, 3, 4, 5]
sage: Words('ab')
Words over Ordered Alphabet ['a', 'b']
sage: Words('natural numbers')
Words over Ordered Alphabet of Natural Numbers
class sage.combinat.words.words.FiniteWords_length_k_over_OrderedAlphabet(alphabet, length)

Bases: sage.combinat.words.words.FiniteWords_over_OrderedAlphabet

cardinality()

Returns the number of words of length n from alphabet.

EXAMPLES:

sage: Words(['a','b','c'], 4).cardinality()
81
sage: Words(3, 4).cardinality()
81
sage: Words(0,0).cardinality()
1
sage: Words(5,0).cardinality()
1
sage: Words(['a','b','c'],0).cardinality()
1
sage: Words(0,1).cardinality()
0
sage: Words(5,1).cardinality()
5
sage: Words(['a','b','c'],1).cardinality()
3
sage: Words(7,13).cardinality()
96889010407
sage: Words(['a','b','c','d','e','f','g'],13).cardinality()
96889010407
iterate_by_length(length)

All words in this class are of the same length, so use iterator instead.

TESTS:

sage: W = Words(['a', 'b'], 2)
sage: list(W.iterate_by_length(2))
[word: aa, word: ab, word: ba, word: bb]
sage: list(W.iterate_by_length(1))
[]
list()

Returns a list of all the words contained in self.

EXAMPLES:

sage: Words(0,0).list()
[word: ]
sage: Words(5,0).list()
[word: ]
sage: Words(['a','b','c'],0).list()
[word: ]
sage: Words(5,1).list()
[word: 1, word: 2, word: 3, word: 4, word: 5]
sage: Words(['a','b','c'],2).list()
[word: aa, word: ab, word: ac, word: ba, word: bb, word: bc, word: ca, word: cb, word: cc]
class sage.combinat.words.words.FiniteWords_over_OrderedAlphabet(alphabet)
Bases: sage.combinat.words.words.Words_over_OrderedAlphabet
class sage.combinat.words.words.InfiniteWords_over_OrderedAlphabet(alphabet)
Bases: sage.combinat.words.words.Words_over_OrderedAlphabet
sage.combinat.words.words.Words(alphabet=None, length=None, finite=True, infinite=True)

Returns the combinatorial class of words of length k over an ordered alphabet.

EXAMPLES:

sage: Words()
Words
sage: Words(length=7)
Finite Words of length 7
sage: Words(5)
Words over Ordered Alphabet [1, 2, 3, 4, 5]
sage: Words(5, 3)
Finite Words of length 3 over Ordered Alphabet [1, 2, 3, 4, 5]
sage: Words(5, infinite=False)
Finite Words over Ordered Alphabet [1, 2, 3, 4, 5]
sage: Words(5, finite=False)
Infinite Words over Ordered Alphabet [1, 2, 3, 4, 5]
sage: Words('ab')
Words over Ordered Alphabet ['a', 'b']
sage: Words('ab', 2)
Finite Words of length 2 over Ordered Alphabet ['a', 'b']
sage: Words('ab', infinite=False)
Finite Words over Ordered Alphabet ['a', 'b']
sage: Words('ab', finite=False)
Infinite Words over Ordered Alphabet ['a', 'b']
sage: Words('positive integers', finite=False)
Infinite Words over Ordered Alphabet of Positive Integers
sage: Words('natural numbers')
Words over Ordered Alphabet of Natural Numbers
class sage.combinat.words.words.Words_all(category=None, *keys, **opts)

Bases: sage.combinat.combinat.InfiniteAbstractCombinatorialClass

TESTS:

sage: from sage.combinat.words.words import Words_all
sage: list(Words_all())
...
NotImplementedError
sage: Words_all().list()
...
NotImplementedError: infinite list
sage: Words_all().cardinality()
+Infinity
alphabet()

EXAMPLES:

sage: from sage.combinat.words.words import Words_over_Alphabet
sage: W = Words_over_Alphabet([1,2,3])
sage: W.alphabet()
[1, 2, 3]
sage: from sage.combinat.words.words import OrderedAlphabet
sage: W = Words_over_Alphabet(OrderedAlphabet('ab'))
sage: W.alphabet()
Ordered Alphabet ['a', 'b']
has_letter(letter)

Returns True if the alphabet of self contains the given letter.

INPUT:

  • letter - a letter

EXAMPLES:

sage: W = Words()
sage: W.has_letter('a')
True
sage: W.has_letter(1)
True
sage: W.has_letter({})
True
sage: W.has_letter([])
True
sage: W.has_letter(range(5))
True
sage: W.has_letter(Permutation([]))
True

sage: from sage.combinat.words.words import Words_over_Alphabet
sage: W = Words_over_Alphabet(['a','b','c'])
sage: W.has_letter('a')
True
sage: W.has_letter('d')
False
sage: W.has_letter(8)
False
size_of_alphabet()

Returns the size of the alphabet.

EXAMPLES:

sage: Words().size_of_alphabet()
+Infinity
class sage.combinat.words.words.Words_n(n)
Bases: sage.combinat.words.words.Words_all
class sage.combinat.words.words.Words_over_Alphabet(alphabet)

Bases: sage.combinat.words.words.Words_all

identity_morphism()

Returns the identity morphism from self to itself.

EXAMPLES:

sage: W = Words('ab')
sage: print W.identity_morphism()
WordMorphism: a->a, b->b
sage: W = Words(range(3))
sage: print W.identity_morphism()
WordMorphism: 0->0, 1->1, 2->2

There is no support yet for infinite alphabet:

sage: W = Words(alphabet=Alphabet(name='NN'))
sage: W
Words over Ordered Alphabet of Natural Numbers
sage: W.identity_morphism()
...
NotImplementedError: size of alphabet must be finite
size_of_alphabet()

Returns the size of the alphabet.

EXAMPLES:

sage: Words('abcdef').size_of_alphabet()
6
sage: Words('').size_of_alphabet()
0
class sage.combinat.words.words.Words_over_OrderedAlphabet(alphabet)

Bases: sage.combinat.words.words.Words_over_Alphabet

cmp_letters(letter1, letter2)

Returns a negative number, zero or a positive number if letter1 < letter2, letter1 == letter2 or letter1 > letter2 respectively.

INPUT:

  • letter1 - a letter in the alphabet
  • letter2 - a letter in the alphabet

EXAMPLES:

sage: from sage.combinat.words.words import Words_over_OrderedAlphabet
sage: from sage.combinat.words.words import OrderedAlphabet
sage: A = OrderedAlphabet('woa')
sage: W = Words_over_OrderedAlphabet(A)
sage: W.cmp_letters('w','a')
-2
sage: W.cmp_letters('w','o')
-1
sage: W.cmp_letters('w','w')
0
iter_morphisms(l=None, codomain=None, min_length=1)

Iterate over all morphisms with domain self and the given codmain.

INPUT:

  • l – list of nonnegative integers (default: None). The length of the list must be the number of letters in the alphabet, and the i-th integer of l determines the length of the word mapped to by i-th letter of the (ordered) alphabet. If l is None, then the method iterates through all morphisms.
  • codomain – (default: None) a combinatorial class of words. By default, codomain is self.
  • min_length – (default: 1) nonnegative integer. If l is not specified, then iterate through all the morphisms where the length of the images of each letter in the alphabet is at least min_length. This is ignored if l is not None.

OUTPUT:

iterator

EXAMPLES:

Iterator over all non-erasing morphisms:

sage: W = Words('ab')
sage: it = W.iter_morphisms()
sage: for _ in range(7): print it.next()
WordMorphism: a->a, b->a
WordMorphism: a->a, b->b
WordMorphism: a->b, b->a
WordMorphism: a->b, b->b
WordMorphism: a->aa, b->a
WordMorphism: a->aa, b->b
WordMorphism: a->ab, b->a

Iterator over all morphisms including erasing morphisms:

sage: W = Words('ab')
sage: it = W.iter_morphisms(min_length=0)
sage: for _ in range(7): print it.next()
WordMorphism: a->, b->
WordMorphism: a->a, b->
WordMorphism: a->b, b->
WordMorphism: a->, b->a
WordMorphism: a->, b->b
WordMorphism: a->aa, b->
WordMorphism: a->ab, b->

Iterator over morphisms with specific image lengths:

sage: for m in W.iter_morphisms([0, 0]): print m
WordMorphism: a->, b->
sage: for m in W.iter_morphisms([0, 1]): print m
WordMorphism: a->, b->a
WordMorphism: a->, b->b
sage: for m in W.iter_morphisms([2, 1]): print m
WordMorphism: a->aa, b->a
WordMorphism: a->aa, b->b
WordMorphism: a->ab, b->a
WordMorphism: a->ab, b->b
WordMorphism: a->ba, b->a
WordMorphism: a->ba, b->b
WordMorphism: a->bb, b->a
WordMorphism: a->bb, b->b
sage: for m in W.iter_morphisms([2, 2]): print m
WordMorphism: a->aa, b->aa
WordMorphism: a->aa, b->ab
WordMorphism: a->aa, b->ba
WordMorphism: a->aa, b->bb
WordMorphism: a->ab, b->aa
WordMorphism: a->ab, b->ab
WordMorphism: a->ab, b->ba
WordMorphism: a->ab, b->bb
WordMorphism: a->ba, b->aa
WordMorphism: a->ba, b->ab
WordMorphism: a->ba, b->ba
WordMorphism: a->ba, b->bb
WordMorphism: a->bb, b->aa
WordMorphism: a->bb, b->ab
WordMorphism: a->bb, b->ba
WordMorphism: a->bb, b->bb

The codomain may be specified as well:

sage: Y = Words('xyz')
sage: for m in W.iter_morphisms([0, 2], codomain=Y): print m
WordMorphism: a->, b->xx
WordMorphism: a->, b->xy
WordMorphism: a->, b->xz
WordMorphism: a->, b->yx
WordMorphism: a->, b->yy
WordMorphism: a->, b->yz
WordMorphism: a->, b->zx
WordMorphism: a->, b->zy
WordMorphism: a->, b->zz
sage: for m in Y.iter_morphisms([0,2,1], codomain=W): print m
WordMorphism: x->, y->aa, z->a
WordMorphism: x->, y->aa, z->b
WordMorphism: x->, y->ab, z->a
WordMorphism: x->, y->ab, z->b
WordMorphism: x->, y->ba, z->a
WordMorphism: x->, y->ba, z->b
WordMorphism: x->, y->bb, z->a
WordMorphism: x->, y->bb, z->b
sage: it = W.iter_morphisms(codomain=Y)
sage: for _ in range(10): print it.next()
WordMorphism: a->x, b->x
WordMorphism: a->x, b->y
WordMorphism: a->x, b->z
WordMorphism: a->y, b->x
WordMorphism: a->y, b->y
WordMorphism: a->y, b->z
WordMorphism: a->z, b->x
WordMorphism: a->z, b->y
WordMorphism: a->z, b->z
WordMorphism: a->xx, b->x

TESTS:

sage: list(W.iter_morphisms([1,0]))
[Morphism from Words over Ordered Alphabet ['a', 'b'] to Words over Ordered Alphabet ['a', 'b'], Morphism from Words over Ordered Alphabet ['a', 'b'] to Words over Ordered Alphabet ['a', 'b']]
sage: list(W.iter_morphisms([0,0], codomain=Y))   
[Morphism from Words over Ordered Alphabet ['a', 'b'] to Words over Ordered Alphabet ['x', 'y', 'z']]
sage: list(W.iter_morphisms([0, 1, 2]))
...
TypeError: l (=[0, 1, 2]) must be an iterable of 2 integers
sage: list(W.iter_morphisms([0, 'a'])) 
...
TypeError: l (=[0, 'a']) must be an iterable of 2 integers
sage: list(W.iter_morphisms([0, 1], codomain='a')) 
...
TypeError: codomain (=a) must be an instance of Words_over_OrderedAlphabet
iterate_by_length(l=1)

Returns an iterator over all the words of self of length l.

INPUT:

  • l - integer (default: 1), the length of the desired words

EXAMPLES:

sage: W = Words('ab')
sage: list(W.iterate_by_length(1)) 
[word: a, word: b]
sage: list(W.iterate_by_length(2))
[word: aa, word: ab, word: ba, word: bb]
sage: list(W.iterate_by_length(3))
[word: aaa,
 word: aab,
 word: aba,
 word: abb,
 word: baa,
 word: bab,
 word: bba,
 word: bbb]
sage: list(W.iterate_by_length('a'))
...
TypeError: the parameter l (='a') must be an integer
sage.combinat.words.words.is_Words(obj)

Returns True if obj is a word set and False otherwise.

EXAMPLES:

sage: from sage.combinat.words.words import is_Words
sage: is_Words(33)
doctest:1: DeprecationWarning: is_Words is deprecated, use isinstance(your_object, Words_all) instead!
False
sage: is_Words(Words('ab'))
True

Previous topic

A collection of constructors of common words

Next topic

Shuffle product of words

This Page