ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 14 Dec 2016 16:52:25 -0600How to define WordMorphism on an infinite alphabet ?http://ask.sagemath.org/question/35622/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. Wed, 16 Nov 2016 20:24:55 -0600http://ask.sagemath.org/question/35622/how-to-define-wordmorphism-on-an-infinite-alphabet/Answer by tmonteil for <p>Let imagine I want to do a substitution on the set of all words written with natural numbers.</p>
<p>Example 1: replace every number with its square.</p>
<p>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). </p>
<p>It's not very clear for me from the doc whether this is actually allowed with the command <code>WordMorphism</code>, but if it is, I would like to understand how to do it. </p>
http://ask.sagemath.org/question/35622/how-to-define-wordmorphism-on-an-infinite-alphabet/?answer=35624#post-id-35624Your 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.Thu, 17 Nov 2016 03:20:44 -0600http://ask.sagemath.org/question/35622/how-to-define-wordmorphism-on-an-infinite-alphabet/?answer=35624#post-id-35624Comment by lb for <p>Your first example is unfortunately currently impossible with <code>WordMorphism</code>, since the map is supposed to be a <code>dict</code>. I quickly tried to hack <code>self._morph</code> with lambda functions and ad_hoc objects with <code>__getitem__</code> method, but the implications with the other methods are too intricate to work easily (in particular, some of them iterate over all the items).</p>
<p>For your second example, you can trick with <code>defaultdict</code> as follows, by starting with a trivial wordmorphism and change its <code>_morph</code> attribute by hand:</p>
<pre><code>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->
</code></pre>
<p>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.</p>
http://ask.sagemath.org/question/35622/how-to-define-wordmorphism-on-an-infinite-alphabet/?comment=36017#post-id-36017Thank 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).Wed, 14 Dec 2016 16:52:25 -0600http://ask.sagemath.org/question/35622/how-to-define-wordmorphism-on-an-infinite-alphabet/?comment=36017#post-id-36017