Ticket #14291: trac_14291_reviewer.2.patch

File trac_14291_reviewer.2.patch, 7.2 KB (added by vbraun, 8 years ago)

Updated patch

  • sage/groups/perm_gps/permgroup.py

    # HG changeset patch
    # User Volker Braun <vbraun.name@gmail.com>
    # Date 1365704555 -3600
    # Node ID 33ab1a79a9fb83cdc3474457c0c5cf2255199f77
    # Parent  14b4fa8451a21882580a93f9833de994ee46ae48
    Reviewer patch to make things more beautiful
    
    diff --git a/sage/groups/perm_gps/permgroup.py b/sage/groups/perm_gps/permgroup.py
    a b  
    10671067                self._gap_().Orbits(self._domain_gap()).sage()]
    10681068
    10691069    @cached_method
    1070     def orbit(self, point, action = "OnPoints"):
     1070    def orbit(self, point, action="OnPoints"):
    10711071        """
    10721072        Return the orbit of a point under a group action.
    10731073
     
    10871087
    10881088          It is set to ``"OnPoints"`` by default. See below for examples.
    10891089
     1090        OUTPUT:
     1091
     1092        The orbit of ``point`` as a tuple. Each entry is an image
     1093        under the action of the permutation group, if necessary
     1094        converted to the corresponding container. That is, if
     1095        `action='OnSets'` then each entry will be a set even if
     1096        ``point`` was given by a list/tuple/iterable.
     1097
    10901098        EXAMPLES::
    10911099
    10921100            sage: G = PermutationGroup([ [(3,4)], [(1,3)] ])
    10931101            sage: G.orbit(3)
    1094             [3, 4, 1]
     1102            (3, 4, 1)
    10951103            sage: G = PermutationGroup([[(1,2),(3,4)], [(1,2,3,4,10)]])
    10961104            sage: G.orbit(3)
    1097             [3, 4, 10, 1, 2]
     1105            (3, 4, 10, 1, 2)
    10981106            sage: G = PermutationGroup([ [('c','d')], [('a','c')] ])
    10991107            sage: G.orbit('a')
    1100             ['a', 'c', 'd']
     1108            ('a', 'c', 'd')
    11011109
    11021110        Action of `S_3` on sets::
    11031111
    11041112            sage: S3 = groups.permutation.Symmetric(3)
    11051113            sage: S3.orbit((1,2), action = "OnSets")
    1106             [[1, 2], [2, 3], [1, 3]]
     1114            ({1, 2}, {2, 3}, {1, 3})
    11071115
    11081116        On tuples::
    11091117
    11101118            sage: S3.orbit((1,2), action = "OnTuples")
    1111             [[1, 2], [2, 3], [2, 1], [3, 1], [1, 3], [3, 2]]
     1119            ((1, 2), (2, 3), (2, 1), (3, 1), (1, 3), (3, 2))
    11121120
    11131121        Action of `S_4` on sets of disjoint sets::
    11141122
    11151123            sage: S4 = groups.permutation.Symmetric(4)
    11161124            sage: S4.orbit(((1,2),(3,4)), action = "OnSetsDisjointSets")
    1117             [[[1, 2], [3, 4]],
    1118              [[1, 4], [2, 3]],
    1119              [[1, 3], [2, 4]]]
     1125            ({{1, 2}, {3, 4}}, {{2, 3}, {1, 4}}, {{1, 3}, {2, 4}})
    11201126
    11211127        Action of `S_4` (on a nonstandard domain) on tuples of sets::
    11221128
    11231129            sage: S4 = PermutationGroup([ [('c','d')], [('a','c')], [('a','b')] ])
    11241130            sage: S4.orbit((('a','c'),('b','d')),"OnTuplesSets")
    1125             [[['a', 'c'], ['b', 'd']], [['a', 'd'], ['b', 'c']],
    1126              [['b', 'c'], ['a', 'd']], [['b', 'd'], ['a', 'c']],
    1127              [['c', 'd'], ['a', 'b']], [['a', 'b'], ['c', 'd']]]
     1131            (({'a', 'c'}, {'b', 'd'}),
     1132             ({'a', 'd'}, {'c', 'b'}),
     1133             ({'c', 'b'}, {'a', 'd'}),
     1134             ({'b', 'd'}, {'a', 'c'}),
     1135             ({'c', 'd'}, {'a', 'b'}),
     1136             ({'a', 'b'}, {'c', 'd'}))
    11281137
    11291138        Action of `S_4` (on a very nonstandard domain) on tuples of sets::
    11301139
    1131             sage: S4 = PermutationGroup([ [((11,(12,13)),'d')], [((12,(12,11)),(11,(12,13)))], [((12,(12,11)),'b')] ])
     1140            sage: S4 = PermutationGroup([ [((11,(12,13)),'d')],
     1141            ...           [((12,(12,11)),(11,(12,13)))], [((12,(12,11)),'b')] ])
    11321142            sage: S4.orbit((( (11,(12,13)), (12,(12,11))),('b','d')),"OnTuplesSets")
    1133             [[[(11, (12, 13)), (12, (12, 11))], ['b', 'd']],
    1134              [['d', (12, (12, 11))], ['b', (11, (12, 13))]],
    1135              [['b', (11, (12, 13))], ['d', (12, (12, 11))]],
    1136              [['d', (11, (12, 13))], ['b', (12, (12, 11))]],
    1137              [['b', 'd'], [(11, (12, 13)), (12, (12, 11))]],
    1138              [['b', (12, (12, 11))], ['d', (11, (12, 13))]]]
    1139 
     1143            (({(11, (12, 13)), (12, (12, 11))}, {'b', 'd'}),
     1144             ({'d', (12, (12, 11))}, {(11, (12, 13)), 'b'}),
     1145             ({(11, (12, 13)), 'b'}, {'d', (12, (12, 11))}),
     1146             ({(11, (12, 13)), 'd'}, {'b', (12, (12, 11))}),
     1147             ({'b', 'd'}, {(11, (12, 13)), (12, (12, 11))}),
     1148             ({'b', (12, (12, 11))}, {(11, (12, 13)), 'd'}))
    11401149        """
    1141         def input_to_gap(x, depth, sort):
    1142             if depth:
    1143                 ans = [input_to_gap(xx,depth-1,sort) for xx in x]
    1144                 if depth == 1 and sort:
    1145                     ans.sort()
    1146                 return ans
     1150        from sage.sets.set import Set
     1151        actions = {
     1152            "OnPoints"           : [],
     1153            "OnSets"             : [Set],
     1154            "OnTuples"           : [tuple],
     1155            "OnSetsSets"         : [Set, Set],
     1156            "OnSetsDisjointSets" : [Set, Set],
     1157            "OnSetsTuples"       : [Set, tuple],
     1158            "OnTuplesSets"       : [tuple, Set],
     1159            "OnTuplesTuples"     : [tuple, tuple],
     1160            }
     1161
     1162        def input_for_gap(x, depth, container):
     1163            if depth == len(container):
     1164                try:
     1165                    return self._domain_to_gap[x]
     1166                except KeyError:
     1167                    raise ValueError('{0} is not part of the domain'.format(x))
     1168            x = [input_for_gap(xx, depth+1, container) for xx in x]
     1169            if container[depth] is Set:
     1170                x.sort()
     1171            return x
     1172
     1173        def gap_to_output(x, depth, container):
     1174            if depth == len(container):
     1175                return self._domain_from_gap[x]
    11471176            else:
    1148                 return self._domain_to_gap[x]
    1149 
    1150         def gap_to_output(x, depth):
    1151             if depth:
    1152                 return [gap_to_output(xx,depth-1) for xx in x]
    1153             else:
    1154                 return self._domain_from_gap[x]
    1155 
    1156         sort = False
    1157 
    1158         actions = {
    1159             "OnPoints"           : {"depth" : 0, "sort" : False},
    1160             "OnSets"             : {"depth" : 1, "sort" : True},
    1161             "OnTuples"           : {"depth" : 1, "sort" : False},
    1162             "OnTuples"           : {"depth" : 1, "sort" : False},
    1163             "OnSetsSets"         : {"depth" : 2, "sort" : True},
    1164             "OnSetsDisjointSets" : {"depth" : 2, "sort" : True},
    1165             "OnTuplesSets"       : {"depth" : 2, "sort" : True},
    1166             "OnTuplesTuples"     : {"depth" : 2, "sort" : False},
    1167             "OnTuplesTuples"     : {"depth" : 2, "sort" : False}
    1168             }
     1177                x = [gap_to_output(xx, depth+1, container) for xx in x]
     1178                return container[depth](x)
    11691179        try:
    1170             params = actions[action]
     1180            container = actions[action]
    11711181        except KeyError:
    1172             raise ValueError("This action is not implemented (yet?).")
    1173 
    1174         try:
    1175             point = input_to_gap(point, **params)
    1176         except KeyError:
    1177             raise ValueError("One element does not seem to be part of the domain.")
    1178 
    1179         ans = self._gap_().Orbit(point, action).sage()
    1180 
    1181         return gap_to_output(ans, params['depth']+1)
     1182            raise NotImplementedError("This action is not implemented (yet?).")
     1183        point = input_for_gap(point, 0, container)
     1184        result = self._gap_().Orbit(point, action).sage()
     1185        result = [gap_to_output(x, 0, container) for x in result]
     1186        return tuple(result)
    11821187
    11831188    def transversals(self, point):
    11841189        """