4

What function in sympy.combinatorics.permutations can return inverse permutation of a given permutation? Searches in Google don't give results. I can write this function, but if such has already been implemented in sympy, it will be unnecessary.

Thanks for any help!

Gert
  • 3,839
  • 19
  • 22
User9123
  • 45
  • 3

1 Answers1

6

You're looking for ~:

In [5]: print Permutation.__invert__.__doc__
        Return the inverse of the permutation.
        A permutation multiplied by its inverse is the identity permutation.
        Examples
        ========
        >>> from sympy.combinatorics.permutations import Permutation
        >>> p = Permutation([[2,0], [3,1]])
        >>> ~p
        Permutation([2, 3, 0, 1])
        >>> _ == p**-1
        True
        >>> p*~p == ~p*p == Permutation([0, 1, 2, 3])
        True

In [6]: ~Permutation(1, 2, 0)
Out[6]: Permutation(0, 2, 1)

** -1 also works. The online documentation literally never explains this, so I can see how you didn't find it. ~ is only mentioned in the explanations of the commutator and mul_inv methods.

user2357112
  • 260,549
  • 28
  • 431
  • 505