SymPy equivalent of itertools.permutations

409 views
Skip to first unread message

Amit Saha

unread,
Jun 11, 2014, 10:35:19 AM6/11/14
to sy...@googlegroups.com
Hello,

I am looking for the simplest SymPy alternative to this:

>>> import itertools
>>> for p in itertools.permutations([1, 2, 4]):
... print(p)
...
(1, 2, 4)
(1, 4, 2)
(2, 1, 4)
(2, 4, 1)
(4, 1, 2)
(4, 2, 1)


I have a feeling this may be obvious and i am asking here without
spending sufficient time to search myself.

Thanks for the help in advance.

Best,
Amit.
--
http://echorand.me

Christophe Bal

unread,
Jun 11, 2014, 1:49:08 PM6/11/14
to sympy-list
Hello.

What do you want to do ?

Christophe BAL


--
http://echorand.me

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CANODV3n1OOyTQcO0Q6s9VpSse-zWrDHy_k5_OQQR8tt4ztewsw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Amit Saha

unread,
Jun 12, 2014, 9:07:38 PM6/12/14
to sy...@googlegroups.com
Hello,

On Thu, Jun 12, 2014 at 3:49 AM, Christophe Bal <proj...@gmail.com> wrote:
> Hello.
>
> What do you want to do ?

I want to know if there are any SymPy methods to find the permutations
as I can do with itertools.permutations.
> https://groups.google.com/d/msgid/sympy/CAAb4jG%3DEUJdqqHM4jSYCp0DwT9QRUPX%2Bfo0u%2BED9BbXYXMtsRg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://echorand.me

Aaron Meurer

unread,
Jun 12, 2014, 9:22:04 PM6/12/14
to sy...@googlegroups.com
What behavior are you looking for that isn't offered by itertools.permutations?

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CANODV3%3DNsx1Ex1n3vGPgPL%3D%3DZGPc4Ap51KabpPi4H7DKErwwYQ%40mail.gmail.com.

Amit Saha

unread,
Jun 12, 2014, 10:38:18 PM6/12/14
to sy...@googlegroups.com


On 13/06/2014 11:22 AM, "Aaron Meurer" <asme...@gmail.com> wrote:
>
> What behavior are you looking for that isn't offered by itertools.permutations?

No, my query was: is it possible to do what itertools.permutations() does using SymPy's Permutation() class for example without writing any more code than with itertools.permutations().

> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6LYcfp0H8%2Br4XVOJE_hnVVv%2BQ0z%3DvpUMtiXo2htzxTsDA%40mail.gmail.com.

Rathmann

unread,
Jun 13, 2014, 12:18:46 AM6/13/14
to sy...@googlegroups.com
I think in general Smpy's goal is to extend rather than duplicate what is already available in the Python Standard Library.   (There are a few exceptions in core.compatibility, but that is for internal use, and only comes into play with older versions of Python.) 

If you want to enumerate only those permutations in some restricted subclass (derangements, involutions) there are a number of useful routines in sympy.utilities.iterables.

The generate_bell() function will generate the permuations of integers 0 to n-1, but in a very specific order.

Similarly, the next_lex() method of the sympy.combinatorics.permutations.Permutation object could be used to enumerate all the permutations of a list of objects, but it adds a fair amount of overhead (and lots of additional functionality.)

Hence, if all you need is what is provided by itertools, use itertools.

Aaron Meurer

unread,
Jun 13, 2014, 11:26:55 AM6/13/14
to sy...@googlegroups.com
These are different things. The Permutation class represents a single
permutation as an element of an algebraic group.
itertools.permutations enumerates all possible applications of
permutations on a given list.

For instance, the permutation that swaps the first and second and
swaps the third and fourth items in a four item list is
Permutation([[0, 1], [2, 3]]). This applied to a specific list, say,
['a', 'b', 'c, 'd'], would be

In [10]: Permutation([[0, 1], [2, 3]])(['a', 'b', 'c', 'd'])
Out[10]: ['b', 'a', 'd', 'c']

You could use one of the group objects, which represents a set of
permutations, and apply all the elements to a list, like say:

In [19]: from sympy.combinatorics.named_groups import *

In [20]: S4 = SymmetricGroup(4)

In [21]: for P in S4.generate():
print(P(['a', 'b', 'c', 'd']))
....:
['a', 'b', 'c', 'd']
['b', 'c', 'd', 'a']
['c', 'd', 'a', 'b']
['d', 'b', 'c', 'a']
['a', 'c', 'd', 'b']
['b', 'd', 'a', 'c']
['c', 'a', 'b', 'd']
['d', 'c', 'a', 'b']
['a', 'd', 'b', 'c']
['b', 'a', 'c', 'd']
['c', 'b', 'd', 'a']
['d', 'a', 'b', 'c']
['a', 'b', 'd', 'c']
['b', 'c', 'a', 'd']
['c', 'd', 'b', 'a']
['d', 'b', 'a', 'c']
['a', 'c', 'b', 'd']
['b', 'd', 'c', 'a']
['c', 'a', 'd', 'b']
['d', 'c', 'b', 'a']
['a', 'd', 'c', 'b']
['b', 'a', 'd', 'c']
['c', 'b', 'a', 'd']
['d', 'a', 'c', 'b']

(the symmetric group of order n is the group with all permutations of length n)

This is perhaps useful if you only want certain kinds of permutations
(e.g., a different group than the symmetric group). For instance, all
cycles would be

In [22]: C4 = CyclicGroup(4)

In [23]: for P in C4.generate():
print(P(['a', 'b', 'c', 'd']))
....:
['a', 'b', 'c', 'd']
['b', 'c', 'd', 'a']
['c', 'd', 'a', 'b']
['d', 'a', 'b', 'c']

And of course, you can do all kinds of computations on the groups
themselves using the group theory module.

But if you just want all permutations of a given list, stick with
itertools.permutations. It is faster, and less work to use.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/CANODV3kSoUG%3D7_awbRj3_kGX1sd09d-9e_dR2-t97i%3DexUQsEA%40mail.gmail.com.

Chris Smith

unread,
Jun 27, 2014, 10:33:23 AM6/27/14
to sy...@googlegroups.com
>>> from sympy.utilities.iterables import permutations
>>> for i in permutations(range(3)):
...       print i
... 
(0, 1, 2)
(0, 2, 1)
(1, 0, 2)
(1, 2, 0)
(2, 0, 1)
(2, 1, 0)

;-) but if you help(permutations) you will see that we have just imported permutations from itertools.

Also note that we have multiset_permutations (and one for combinations and partitions) for generating unique permutations of lists that contain repeated elements

>>> for i in permutations('aab'):
...  print i
...
('a', 'a', 'b')
('a', 'b', 'a')
('a', 'a', 'b')
('a', 'b', 'a')
('b', 'a', 'a')
('b', 'a', 'a')

>>> for i in multiset_permutations('aab'):
...   print i
...
['a', 'a', 'b']
['a', 'b', 'a']
['b', 'a', 'a']

Reply all
Reply to author
Forward
0 new messages