# How to define WordMorphism on an infinite alphabet ?

Let imagine I want to do a substitution on the set of all words written with natural numbers.

Example 1: replace every number with its square.

Example 2: make precise rules for substitution of letter in some finite subset of the alphabet and decide that all other letters will be deleted (replaced with empty).

It's not very clear for me from the doc whether this is actually allowed with the command WordMorphism, but if it is, I would like to understand how to do it.

edit retag close merge delete

Sort by » oldest newest most voted

Your first example is unfortunately currently impossible with WordMorphism, since the map is supposed to be a dict. I quickly tried to hack self._morph with lambda functions and ad_hoc objects with __getitem__ method, but the implications with the other methods are too intricate to work easily (in particular, some of them iterate over all the items).

For your second example, you can trick with defaultdict as follows, by starting with a trivial wordmorphism and change its _morph attribute by hand:

sage: from collections import defaultdict
sage: W = Words(NN)
sage: m = WordMorphism(data={},domain=W,codomain=W)
sage: m
WordMorphism:
sage: m._morph = defaultdict(W)
sage: m._morph[1] = W([12,34])
sage: m._morph[3] = W([42,42,42])
sage: m
WordMorphism: 1->12,34, 3->42,42,42
sage: m([1,2,3,4,5,7])
word: 12,34,42,42,42
sage: m
WordMorphism: 1->12,34, 2->, 3->42,42,42, 4->, 5->, 7->


What do you want to do more precisely with such morphisms ? Perhaps do you only need a few features that can be implemented automomously, without using wordmorphism at all.

more

Thank you very much for the detailed answer. I am sorry I did not replied earlier. Actually I expected an email would tell me when my question is answered.
I was using word and WordMorphism for my teaching, as an example, I am not planning to use it further for the moment, but will keep in mind the trick with default dictionary for my course next year (if I am lucky enough to have the same course again next year).

( 2016-12-14 16:52:25 -0500 )edit