Path: blob/master/src/sage/groups/conjugacy_classes.py
8814 views
# -*- coding: utf-8 -*-1r"""2Conjugacy classes of groups34This module implements a wrapper of GAP's ``ConjugacyClass`` function.56There are two main classes, :class:`ConjugacyClass` and7:class:`ConjugacyClassGAP`. All generic methods should go into8:class:`ConjugacyClass`, whereas :class:`ConjugacyClassGAP` should only9contain wrappers for GAP functions. :class:`ConjugacyClass` contains some10fallback methods in case some group cannot be defined as a GAP object.1112.. TODO::1314- Implement a non-naive fallback method for computing all the elements of15the conjugacy class when the group is not defined in GAP, as the one in16Butler's paper.17- Define a sage method for gap matrices so that groups of matrices can18use the quicker GAP algorithm rather than the naive one.1920EXAMPLES:2122Conjugacy classes for groups of permutations::2324sage: G = SymmetricGroup(4)25sage: g = G((1,2,3,4))26sage: G.conjugacy_class(g)27Conjugacy class of (1,2,3,4) in Symmetric group of order 4! as a permutation group2829Conjugacy classes for groups of matrices::3031sage: F = GF(5)32sage: gens = [matrix(F,2,[1,2, -1, 1]), matrix(F,2, [1,1, 0,1])]33sage: H = MatrixGroup(gens)34sage: h = H(matrix(F,2,[1,2, -1, 1]))35sage: H.conjugacy_class(h)36Conjugacy class of [1 2]37[4 1] in Matrix group over Finite Field of size 5 with 2 generators (38[1 2] [1 1]39[4 1], [0 1]40)4142TESTS::4344sage: G = SymmetricGroup(3)45sage: g = G((1,2,3))46sage: C = ConjugacyClass(G,g)47sage: TestSuite(C).run()48"""4950#****************************************************************************51# Copyright (C) 2011 Javier López Peña <[email protected]>52#53# Distributed under the terms of the GNU General Public License (GPL)54# http://www.gnu.org/licenses/55#****************************************************************************5657from sage.structure.parent import Parent58from sage.misc.cachefunc import cached_method59from sage.categories.enumerated_sets import EnumeratedSets60from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets6162# TODO63#64# Don't derive from Parent if there are no elements65#66# @cached_method must not return mutable containers (lists), use67# tuples instead.68697071class ConjugacyClass(Parent):72r"""73Generic conjugacy classes for elements in a group.7475This is the default fall-back implementation to be used whenever76GAP cannot handle the group.7778EXAMPLES::7980sage: G = SymmetricGroup(4)81sage: g = G((1,2,3,4))82sage: ConjugacyClass(G,g)83Conjugacy class of (1,2,3,4) in Symmetric group of order 4! as a84permutation group85"""86def __init__(self, group, element):87r"""88Generic conjugacy classes for elements in a group.89This is the default fall-back implementation to be used whenever90GAP cannot handle the group.9192EXAMPLES::9394sage: G = SymmetricGroup(4)95sage: g = G((1,2,3,4))96sage: ConjugacyClass(G,g)97Conjugacy class of (1,2,3,4) in Symmetric group of order 4! as a98permutation group99sage: TestSuite(G).run()100"""101self._parent = group102self._representative = element103try:104finite = group.is_finite()105except (AttributeError, NotImplementedError):106finite = False107if finite:108Parent.__init__(self, category=FiniteEnumeratedSets())109else: # If the group is not finite, then we do not know if we are finite or not110Parent.__init__(self, category=EnumeratedSets())111112def __repr__(self):113r"""114EXAMPLES::115116sage: G = SymmetricGroup(4)117sage: g = G((1,2,3,4))118sage: C = ConjugacyClass(G,g)119sage: C120Conjugacy class of (1,2,3,4) in Symmetric group of order 4! as a121permutation group122123"""124return "Conjugacy class of %s in %s"%(self._representative,self._parent)125126def __cmp__(self, other):127r"""128Comparison of conjugacy classes is done by comparing the129underlying sets.130131EXAMPLES::132133sage: F = GF(5)134sage: gens = [matrix(F,2,[1,2, -1, 1]), matrix(F,2, [1,1, 0,1])]135sage: H = MatrixGroup(gens)136sage: h = H(matrix(F,2,[1,2, -1, 1]))137sage: h2 = H(matrix(F,2,[1,1, 0, 1]))138sage: g = h2*h*h2^(-1)139sage: C = ConjugacyClass(H,h)140sage: D = ConjugacyClass(H,g)141sage: C == D142True143"""144c = cmp(type(self),type(other))145if c:146return c147return cmp(self.set(), other.set())148149def __contains__(self, element):150r"""151Checks if ``element`` belongs to the conjugacy class ``self``.152153EXAMPLES::154155sage: G = SymmetricGroup(4)156sage: g = G((1,2,3,4))157sage: C = ConjugacyClass(G,g)158sage: g in C159True160"""161return element in self.set()162163@cached_method164def set(self):165r"""166Naive algorithm to give a set with all the elements of the conjugacy167class.168169.. TODO::170171Implement a non-naive algorithm, cf. for instance172G. Butler: "An Inductive Schema for Computing Conjugacy Classes173in Permutation Groups", Math. of Comp. Vol. 62, No. 205 (1994)174175EXAMPLES:176177Groups of permutations::178179sage: G = SymmetricGroup(3)180sage: g = G((1,2))181sage: C = ConjugacyClass(G,g)182sage: S = [(2,3), (1,2), (1,3)]183sage: C.set() == Set(G(x) for x in S)184True185186Groups of matrices over finite fields::187188sage: F = GF(5)189sage: gens = [matrix(F,2,[1,2, -1, 1]), matrix(F,2, [1,1, 0,1])]190sage: H = MatrixGroup(gens)191sage: h = H(matrix(F,2,[1,2, -1, 1]))192sage: C = ConjugacyClass(H,h)193sage: S = [[[3, 2], [2, 4]], [[0, 1], [2, 2]], [[3, 4], [1, 4]],\194[[0, 3], [4, 2]], [[1, 2], [4, 1]], [[2, 1], [2, 0]],\195[[4, 1], [4, 3]], [[4, 4], [1, 3]], [[2, 4], [3, 0]],\196[[1, 4], [2, 1]], [[3, 3], [3, 4]], [[2, 3], [4, 0]],\197[[0, 2], [1, 2]], [[1, 3], [1, 1]], [[4, 3], [3, 3]],\198[[4, 2], [2, 3]], [[0, 4], [3, 2]], [[1, 1], [3, 1]],\199[[2, 2], [1, 0]], [[3, 1], [4, 4]]]200sage: C.set() == Set(H(x) for x in S)201True202"""203from sage.sets.set import Set204from sage.combinat.backtrack import TransitiveIdeal205if self._parent.is_finite():206g = self._representative207G = self._parent208gens = G.gens()209return Set(x for x in TransitiveIdeal(lambda y: [c*y*c**-1 for c in gens], [g]))210else:211raise NotImplementedError("Listing the elements of conjugacy classes is not implemented for infinite groups!")212213@cached_method214def list(self):215r"""216Return a list with all the elements of ``self``.217218EXAMPLES:219220Groups of permutations::221222sage: G = SymmetricGroup(3)223sage: g = G((1,2,3))224sage: c = ConjugacyClass(G,g)225sage: L = c.list()226sage: Set(L) == Set([G((1,3,2)), G((1,2,3))])227True228"""229return list(self.set())230231def is_real(self):232"""233Checks if ``self`` is real (closed for inverses).234235EXAMPLES::236237sage: G = SymmetricGroup(4)238sage: g = G((1,2,3,4))239sage: c = ConjugacyClass(G,g)240sage: c.is_real()241True242243"""244return self._representative**(-1) in self245246def is_rational(self):247"""248Checks if ``self`` is rational (closed for powers).249250EXAMPLES::251252sage: G = SymmetricGroup(4)253sage: g = G((1,2,3,4))254sage: c = ConjugacyClass(G,g)255sage: c.is_rational()256False257"""258g = self._representative259return all(g**k in self.set() for k in range(1,g.order()))260261def representative(self):262"""263Return a representative of ``self``.264265EXAMPLES::266267sage: G = SymmetricGroup(3)268sage: g = G((1,2,3))269sage: C = ConjugacyClass(G,g)270sage: C.representative()271(1,2,3)272"""273return self._representative274275276class ConjugacyClassGAP(ConjugacyClass):277r"""278Class for a conjugacy class for groups defined over GAP.279Intended for wrapping GAP methods on conjugacy classes.280281INPUT:282283- ``group`` -- the group in which the conjugacy class is taken284285- ``element`` -- the element generating the conjugacy class286287EXAMPLES::288289sage: G = SymmetricGroup(4)290sage: g = G((1,2,3,4))291sage: ConjugacyClassGAP(G,g)292Conjugacy class of (1,2,3,4) in Symmetric group of order 4! as a293permutation group294"""295296def __init__(self, group, element):297r"""298Constructor for the class.299300EXAMPLES:301302Conjugacy classes for groups of permutations::303304sage: G = SymmetricGroup(4)305sage: g = G((1,2,3,4))306sage: ConjugacyClassGAP(G,g)307Conjugacy class of (1,2,3,4) in Symmetric group of order 4! as a permutation group308309Conjugacy classes for groups of matrices::310311sage: F = GF(5)312sage: gens = [matrix(F,2,[1,2, -1, 1]), matrix(F,2, [1,1, 0,1])]313sage: H = MatrixGroup(gens)314sage: h = H(matrix(F,2,[1,2, -1, 1]))315sage: ConjugacyClassGAP(H,h)316Conjugacy class of [1 2]317[4 1] in Matrix group over Finite Field of size 5 with 2 generators (318[1 2] [1 1]319[4 1], [0 1]320)321"""322try:323# GAP interface324self._gap_group = group._gap_()325self._gap_representative = element._gap_()326except (AttributeError, TypeError):327try:328# LibGAP329self._gap_group = group.gap()330self._gap_representative = element.gap()331except (AttributeError, TypeError):332raise TypeError("The group %s cannot be defined as a GAP group"%group)333334self._gap_conjugacy_class = self._gap_group.ConjugacyClass(self._gap_representative)335ConjugacyClass.__init__(self, group, element)336337def _gap_(self):338r"""339Return the gap object corresponding to ``self``.340341EXAMPLES::342343sage: G = SymmetricGroup(3)344sage: g = G((1,2,3))345sage: C = ConjugacyClassGAP(G,g)346sage: C._gap_()347ConjugacyClass( SymmetricGroup( [ 1 .. 3 ] ), (1,2,3) )348"""349return self._gap_conjugacy_class350351@cached_method352def set(self):353r"""354Return a Sage ``Set`` with all the elements of the conjugacy class.355356By default attempts to use GAP construction of the conjugacy class.357If GAP method is not implemented for the given group, and the group358is finite, falls back to a naive algorithm.359360.. WARNING::361362The naive algorithm can be really slow and memory intensive.363364EXAMPLES:365366Groups of permutations::367368sage: G = SymmetricGroup(4)369sage: g = G((1,2,3,4))370sage: C = ConjugacyClassGAP(G,g)371sage: S = [(1,3,2,4), (1,4,3,2), (1,3,4,2), (1,2,3,4), (1,4,2,3), (1,2,4,3)]372sage: C.set() == Set(G(x) for x in S)373True374375"""376from sage.sets.set import Set377try:378cc = self._gap_conjugacy_class.AsList().sage()379return Set([self._parent(x) for x in cc])380except NotImplementedError: # If GAP doesn't work, fall back to naive method381return ConjugacyClass.set.f(self) # Need the f because the base-class mehod is also cached382383384385