Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Combinatorics

Reply
Thread Tools

Combinatorics

 
 
Michael Robertson
Guest
Posts: n/a
 
      02-12-2008
Where is the python equivalent of:

http://search.cpan.org/~fxn/Algorith...mbinatorics.pm

combinations (with and without repetition)
variations (with and without repetition)
permutations
partitions
derangements
etc

I'm guessing sage has this, but shouldn't something like this be part of
the standard library (perhaps in C)? I'd understand if derangements and
partitions were excluded, but the standard combinatorics (replacement
on/off, repetition on/off) would be quite nice. It would also be
helpful to have a general cartesian product function which combined
elements from an arbitrary number of lists.

It seems that questions for these algorithms occur too frequently.

Am I wishing on a star?

 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      02-12-2008
Michael Robertson:
> I'm guessing sage has this, but shouldn't something like this be part of
> the standard library (perhaps in C)?


My answer is positive. As a reference point you can look at the
combinatorics module of Mathematica.

Bye,
bearophile
 
Reply With Quote
 
 
 
 
pataphor
Guest
Posts: n/a
 
      02-12-2008
On Mon, 11 Feb 2008 23:52:31 -0800
Michael Robertson <> wrote:

> Am I wishing on a star?


for i in xrange(10**10):
print i
OverflowError: long int too large to convert to int

The problem seems to be that although python supports arbitrary long
integers, all the internal loop counters still use limited size integers.

I'm not arguing that any program would conceivably finish the above
loop in a reasonable time, but I think it should be possible to use
itertools.islice to get a smaller slice of this iterator (somewhere in
the middle) and iterate on that. Maybe it could be done with something
like "from __future__ import use_long_integers".

P.

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-12-2008
On Tue, 12 Feb 2008 10:38:53 +0100, pataphor wrote:

> On Mon, 11 Feb 2008 23:52:31 -0800
> Michael Robertson <> wrote:
>
>> Am I wishing on a star?

>
> for i in xrange(10**10):
> print i
> OverflowError: long int too large to convert to int
>
> The problem seems to be that although python supports arbitrary long
> integers, all the internal loop counters still use limited size
> integers.


I'm curious what you think this has to do with the Original Poster's
question, which was about combinatorics (as the subject says),
specifically asking where he could find a library to deal with them.


--
Steven
 
Reply With Quote
 
Cameron Laird
Guest
Posts: n/a
 
      02-12-2008
In article <6690be63-aad6-4d55-8043->,
<> wrote:
>Michael Robertson:
>> I'm guessing sage has this, but shouldn't something like this be part of
>> the standard library (perhaps in C)?

>
>My answer is positive. As a reference point you can look at the
>combinatorics module of Mathematica.

.
.
.
Should combinatorics be part of the standard library? That's
an aesthetic-pragmatic question I don't feel competent to
answer; I look to timbot and Guido and so on for judgment there.
It does occur to me, though, that even more widely applicable
than the combinatorics module of Mathematica (if only because of
its licensing) might be such resources as
A. http://aspn.activestate.com/ASPN/Coo.../Recipe/190465
B. http://www.sagemath.org/doc/html/ref/node181.html
C. http://web.archive.org/web/200703061...10089/ur0606j/
 
Reply With Quote
 
Robert Dodier
Guest
Posts: n/a
 
      02-12-2008
Cameron Laird wrote:

> Should combinatorics be part of the standard library? That's
> an aesthetic-pragmatic question I don't feel competent to
> answer; I look to timbot and Guido and so on for judgment there.
> It does occur to me, though, that even more widely applicable
> than the combinatorics module of Mathematica (if only because of
> its licensing) might be such resources as

[...]

Well, since Mathematica has been mentioned, I'll go ahead and
mention Maxima as well. Maxima is an open source (GPL) symbolic
computation system, which includes some functions for
combinatorics, among many other things. The relevant code is:
http://maxima.cvs.sourceforge.net/ma.../src/nset.lisp
(Oh, btw Maxima is written in Lisp.) The relevant documentation:
http://maxima.sourceforge.net/docs/m...maxima_37.html

Sage can presumably access Maxima's combinatoric functions
(since Sage bundles Maxima along with other programs).
I don't know if there are combinatoric functions from other
packages in Sage.

FWIW

Robert Dodier
 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      02-12-2008
Cameron Laird:
> It does occur to me, though, that even more widely applicable
> than the combinatorics module of Mathematica (if only because of
> its licensing) might be such resources as


What I was trying to say is that that Mathematica combinatorics module
contains lots and lots and lots of things, so people that want to
create a combinatorics module for the Python std lib may read that
huge list of good ideas as a starting point.

Bye,
bearophile
 
Reply With Quote
 
mensanator@aol.com
Guest
Posts: n/a
 
      02-12-2008
On Feb 12, 1:52*am, Michael Robertson <mcrobert...@hotmail.com> wrote:
> Where is the python equivalent of:
>
> http://search.cpan.org/~fxn/Algorith...6/Combinatoric...
>
> combinations (with and without repetition)
> variations (with and without repetition)
> permutations
> partitions
> derangements
> etc
>
> I'm guessing sage has this, but shouldn't something like this be part of
> the standard library (perhaps in C)? *I'd understand if derangements and
> partitions were excluded, but the standard combinatorics (replacement
> on/off, repetition on/off) would be quite nice. *It would also be
> helpful to have a general cartesian product function which combined
> elements from an arbitrary number of lists.
>
> It seems that questions for these algorithms occur too frequently.
>
> Am I wishing on a star?


Did you know that you can do a Cartesian Product
(permutations with replacement) using SQL?

And that things like Combinations with Replacement,
Permutaions withour Replacement and Combinations
without Replacement are simple Cartesian Product
subsets which can be seleceted via a WHERE clause?

For example:

# unjoined tables create a Cartesian Product

import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.executescript("""
create table letter(n);
""")

letters = [('a'),('b'),('c'),('d'),('e'),('f'),('g'),('h')]

cur.executemany("""
INSERT INTO letter(n)
VALUES (?);"""
, letters)

# note: no JOIN clause
cur.execute("""
SELECT letter.*,
letter1.*
FROM letter, letter AS letter1
ORDER BY letter.n;
""")

cartesian_product = cur.fetchall()

for i in cartesian_product:
print i[0]+i[1],

## aa ab ac ad ae af ag ah
## ba bb bc bd be bf bg bh
## ca cb cc cd ce cf cg ch
## da db dc dd de df dg dh
## ea eb ec ed ee ef eg eh
## fa fb fc fd fe ff fg fh
## ga gb gc gd ge gf gg gh
## ha hb hc hd he hf hg hh
 
Reply With Quote
 
Jaap Spies
Guest
Posts: n/a
 
      02-12-2008
Robert Dodier wrote:
> Cameron Laird wrote:
>
>> Should combinatorics be part of the standard library? That's
>> an aesthetic-pragmatic question I don't feel competent to
>> answer; I look to timbot and Guido and so on for judgment there.
>> It does occur to me, though, that even more widely applicable
>> than the combinatorics module of Mathematica (if only because of
>> its licensing) might be such resources as

> [...]
>
> Well, since Mathematica has been mentioned, I'll go ahead and
> mention Maxima as well. Maxima is an open source (GPL) symbolic
> computation system, which includes some functions for
> combinatorics, among many other things. The relevant code is:
> http://maxima.cvs.sourceforge.net/ma.../src/nset.lisp
> (Oh, btw Maxima is written in Lisp.) The relevant documentation:
> http://maxima.sourceforge.net/docs/m...maxima_37.html
>
> Sage can presumably access Maxima's combinatoric functions
> (since Sage bundles Maxima along with other programs).
> I don't know if there are combinatoric functions from other
> packages in Sage.


There is a native combinatorics module in Sage. See:
http://www.sagemath.org/doc/html/ref/node181.html

Sage: http://www.sagemath.org/


Jaap




>
> FWIW
>
> Robert Dodier

 
Reply With Quote
 
Raymond Hettinger
Guest
Posts: n/a
 
      02-13-2008
On Feb 11, 11:52*pm, Michael Robertson <mcrobert...@hotmail.com>
wrote:
> Where is the python equivalent of:
>
> http://search.cpan.org/~fxn/Algorith...6/Combinatoric...
>
> combinations (with and without repetition)
> variations (with and without repetition)
> permutations
> partitions
> derangements
> etc
>
> I'm guessing sage has this, but shouldn't something like this be part of
> the standard library (perhaps in C)? *I'd understand if derangements and
> partitions were excluded, but the standard combinatorics (replacement
> on/off, repetition on/off) would be quite nice. *It would also be
> helpful to have a general cartesian product function which combined
> elements from an arbitrary number of lists.
>
> It seems that questions for these algorithms occur too frequently.
>
> Am I wishing on a star?


FWIW, I'm adding cartesian product to the itertools module in Py2.6.


Raymond

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
combinatorics via __future__ generators Phlip Python 4 12-02-2009 05:35 AM
Python and Combinatorics none Python 4 10-26-2007 07:41 AM
Python and Combinatorics Nic Python 4 05-16-2006 09:41 AM
[perl-python] combinatorics fun Xah Lee Python 7 02-11-2005 10:05 AM
combinatorics question Carnosaur C Programming 17 10-31-2003 03:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57