ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 12 Dec 2018 12:35:08 +0100Overloading the binary operation of symmetric groups to reverse the orderhttps://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/ I believe the Sage implementation of symmetric groups uses GAP which composes permutations in the opposite order to what I am used to.
E.g. if (1,2) denotes the permutations switching 1 and 2, and (2,3) switches 2 and 3, then I would say
(1,2) * (2,3) = (1,2,3),
because I would first evaluate (2,3) and then (1,2) (the order in which one evaluates functions). Sage on the other hand produces
(1,2) * (2,3) = (1,3,2).
About a year ago, a friend helped me overload the definition of the binary operation to have the composition of permutations evaluated in the order I am used to. The code that worked was
class NewSymmetricGroupElement(sage.groups.perm_gps.permgroup_element.SymmetricGroupElement):
def _mul_(self, other):
"""Calling the original class method with order inverted"""
return sage.groups.perm_gps.permgroup_element.SymmetricGroupElement._mul_(other, self)
class NewSymmetricGroup(SymmetricGroup):
def _element_class(self):
return NewSymmetricGroupElement
I recently updated to Sage 8.4 and the code no longer works. Here is what happens, after executing the code above, I run:
NS3=NewSymmetricGroup(3); NS3
> Symmetric group of order 3! as a permutation group
a=NS3((1,2)); b=NS3((2,3));
a*b = (1,3,2) # I would expect (1,2,3)
Can anyone spot what I am doing wrong?Tue, 11 Dec 2018 16:49:40 +0100https://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/Answer by FrédéricC for <p>I believe the Sage implementation of symmetric groups uses GAP which composes permutations in the opposite order to what I am used to.
E.g. if (1,2) denotes the permutations switching 1 and 2, and (2,3) switches 2 and 3, then I would say
(1,2) * (2,3) = (1,2,3),
because I would first evaluate (2,3) and then (1,2) (the order in which one evaluates functions). Sage on the other hand produces
(1,2) * (2,3) = (1,3,2).</p>
<p>About a year ago, a friend helped me overload the definition of the binary operation to have the composition of permutations evaluated in the order I am used to. The code that worked was</p>
<pre><code>class NewSymmetricGroupElement(sage.groups.perm_gps.permgroup_element.SymmetricGroupElement):
def _mul_(self, other):
"""Calling the original class method with order inverted"""
return sage.groups.perm_gps.permgroup_element.SymmetricGroupElement._mul_(other, self)
class NewSymmetricGroup(SymmetricGroup):
def _element_class(self):
return NewSymmetricGroupElement
</code></pre>
<p>I recently updated to Sage 8.4 and the code no longer works. Here is what happens, after executing the code above, I run:</p>
<pre><code>NS3=NewSymmetricGroup(3); NS3
> Symmetric group of order 3! as a permutation group
a=NS3((1,2)); b=NS3((2,3));
a*b = (1,3,2) # I would expect (1,2,3)
</code></pre>
<p>Can anyone spot what I am doing wrong?</p>
https://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/?answer=44641#post-id-44641Well, this is sort of a known problem.. https://trac.sagemath.org/ticket/14885
There is something for permutations:
sage: Permutations.options(mult='r2l',display='cycle')
sage: a=Permutation([(1,2),(3,)]);a
(1,2)
sage: b=Permutation([(2,3),(1,)]);b
(2,3)
sage: a*b
(1,2,3)
But this does not seem to be available for SymmetricGroup.Tue, 11 Dec 2018 20:09:15 +0100https://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/?answer=44641#post-id-44641Comment by Gonneman for <p>Well, this is sort of a known problem.. <a href="https://trac.sagemath.org/ticket/14885">https://trac.sagemath.org/ticket/14885</a></p>
<p>There is something for permutations:</p>
<pre><code>sage: Permutations.options(mult='r2l',display='cycle')
sage: a=Permutation([(1,2),(3,)]);a
(1,2)
sage: b=Permutation([(2,3),(1,)]);b
(2,3)
sage: a*b
(1,2,3)
</code></pre>
<p>But this does not seem to be available for SymmetricGroup.</p>
https://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/?comment=44649#post-id-44649Thanks for the link to the bug report.
That report also mentions setting multiplication to right-to-left (r2l), as you do, but then notes that this breaks code elsewhere. Quite unsatisfying.
I'm surprised that the _mul_ method is so hard to overload.Wed, 12 Dec 2018 10:03:41 +0100https://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/?comment=44649#post-id-44649Answer by slelievre for <p>I believe the Sage implementation of symmetric groups uses GAP which composes permutations in the opposite order to what I am used to.
E.g. if (1,2) denotes the permutations switching 1 and 2, and (2,3) switches 2 and 3, then I would say
(1,2) * (2,3) = (1,2,3),
because I would first evaluate (2,3) and then (1,2) (the order in which one evaluates functions). Sage on the other hand produces
(1,2) * (2,3) = (1,3,2).</p>
<p>About a year ago, a friend helped me overload the definition of the binary operation to have the composition of permutations evaluated in the order I am used to. The code that worked was</p>
<pre><code>class NewSymmetricGroupElement(sage.groups.perm_gps.permgroup_element.SymmetricGroupElement):
def _mul_(self, other):
"""Calling the original class method with order inverted"""
return sage.groups.perm_gps.permgroup_element.SymmetricGroupElement._mul_(other, self)
class NewSymmetricGroup(SymmetricGroup):
def _element_class(self):
return NewSymmetricGroupElement
</code></pre>
<p>I recently updated to Sage 8.4 and the code no longer works. Here is what happens, after executing the code above, I run:</p>
<pre><code>NS3=NewSymmetricGroup(3); NS3
> Symmetric group of order 3! as a permutation group
a=NS3((1,2)); b=NS3((2,3));
a*b = (1,3,2) # I would expect (1,2,3)
</code></pre>
<p>Can anyone spot what I am doing wrong?</p>
https://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/?answer=44653#post-id-44653As a workaround, instead of multiplying with `*`, one can
explicitly use right multiplication with `__rmul__`:
sage: S3 = SymmetricGroup(3)
sage: a = S3((1, 2))
sage: b = S3((2, 3))
sage: a * b
(1,3,2)
sage: a.__rmul__(b)
(1,2,3)Wed, 12 Dec 2018 12:35:08 +0100https://ask.sagemath.org/question/44637/overloading-the-binary-operation-of-symmetric-groups-to-reverse-the-order/?answer=44653#post-id-44653