'Return the commutator of two subgroups.
For a permutation group ``K`` and subgroups ``G``, ``H``, the
commutator of ``G`` and ``H`` is defined as the group generated
by all the commutators `[g, h] = hgh^{-1}g^{-1}` for ``g`` in ``G`` and
``h`` in ``H``. It is naturally a subgroup of ``K`` ([1], p.27).
Examples
>>> from sympy.combinatorics.named_groups import (SymmetricGroup,
... AlternatingGroup)
>>> S = SymmetricGroup(5)
>>> A = AlternatingGroup(5)
>>> G = S.commutator(S, A)
>>> G.is_subgroup(A)
True
See Also
derived_subgroup
Notes
The commutator of two subgroups `H, G` is equal to the normal closure
of the commutators of all the generators, i.e. `hgh^{-1}g^{-1}` for `h`
a generator of `H` and `g` a generator of `G` ([1], p.28)'

def commutator(self, G, H):

    ggens = G.generators
    hgens = H.generators
    commutators = []
    for ggen in ggens:
        for hgen in hgens:
            commutator = rmul(hgen, ggen, (~ hgen), (~ ggen))
            if (commutator not in commutators):
                commutators.append(commutator)
    res = self.normal_closure(commutators)
    return res
