# Revision history [back]

It 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).

What do you want to do more precisely with such morphisms ? Perhaps do you only need a few features that can be implemented as workarounds.

It 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 as workarounds.

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 as workarounds.workarounds, without using wordmorphism at all.

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 as workarounds, automomously, without using wordmorphism at all.