'Return the commutator of self and x: ``~x*~self*x*self``
If f and g are part of a group, G, then the commutator of f and g
is the group identity iff f and g commute, i.e. fg == gf.
Examples
>>> from sympy.combinatorics.permutations import Permutation
>>> Permutation.print_cyclic = False
>>> p = Permutation([0, 2, 3, 1])
>>> x = Permutation([2, 0, 3, 1])
>>> c = p.commutator(x); c
Permutation([2, 1, 3, 0])
>>> c == ~x*~p*x*p
True
>>> I = Permutation(3)
>>> p = [I + i for i in range(6)]
>>> for i in range(len(p)):
...     for j in range(len(p)):
...         c = p[i].commutator(p[j])
...         if p[i]*p[j] == p[j]*p[i]:
...             assert c == I
...         else:
...             assert c != I
References
http://en.wikipedia.org/wiki/Commutator'

def commutator(self, x):

    a = self.array_form
    b = x.array_form
    n = len(a)
    if (len(b) != n):
        raise ValueError('The    permutations    must    be    of    equal    size.')
    inva = ([None] * n)
    for i in range(n):
        inva[a[i]] = i
    invb = ([None] * n)
    for i in range(n):
        invb[b[i]] = i
    return self._af_new([a[b[inva[i]]] for i in invb])
