You can use LeviCivita.  From the docstring:

>>> LeviCivita(1, 2, 3)
1
>>> LeviCivita(1, 3, 2)
-1
>>> LeviCivita(1, 2, 2)
0

It also works with symbolic parameters:

>>> LeviCivita(i, j, k)
LeviCivita(i, j, k)
>>> LeviCivita(i, j, i)
0

Internally, it's computing the product and dividing by the factorial
(see sympy/functions/special/tensor_functions.py), which is actually
probably not the most efficient way to do it.

There is a function in sympy/combinatorics/permutations.py that
computes the parity that is more efficient.  This is more efficient
for numeric parameters because it just uses the fact that the parity
is the even/odd parity of the number of terms such that x < y and p[x]
> p[y] (see e.g., permutation.signature()).

So this code should actually be changed to use that instead, with an
additional check if a number is repeated (or maybe that could happen
in Permutation.__init__).

Aaron Meurer

On Tue, Feb 14, 2012 at 12:11 PM, Alan Bromborsky <[email protected]> wrote:
> I am trying not to reinvent the wheel, hence this question.  Strictly
> speaking this is more a python than sympy question, but I think the
> mathematical abilities of the sympy group would lead to a more optimized
> answer.
>
> The question is if I have a list (tuple, etc.) of integers what is the most
> optimal (short list < 20) way of calculating the sign of the permutation.
>  Return 1 if even, Return -1 if odd, and return 0 if any members occur more
> that once.
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to