473,473 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to generate (enumerate) 2**N tuples representing all vertices of unit hypercube in N-dimensional hyperspace ?

I'm looking for a good Python way to generate (enumerate) the 2**N
tuples representing all vertices of the unit hypercube in N-dimensional
hyperspace.

For example, for N=4 the Python code should generate the following 2**N
= 16 tuples:

(1,1,1,1), (1,1,1,-1),
(1,1,-1, 1), (1,1,-1,-1),
(1,-1,1,1), (1,-1,1,-1),
(1,-1,-1, 1), (1,-1,-1,-1),
(-1,1,1,1), (-1,1,1,-1),
(-1,1,-1, 1), (-1,1,-1,-1),
(-1,-1,1,1), (-1,-1,1,-1),
(-1,-1,-1, 1), (-1,-1,-1,-1)

Maybe converting each integer in the range(2**N) to binary, then
converting to bit string, then applying the "tuple" function to each
bit string?

Thanks for your help.

Jan 4 '06 #1
9 2472
"Dr. Colombes" <Dr********@yahoo.com> writes:
I'm looking for a good Python way to generate (enumerate) the 2**N
tuples representing all vertices of the unit hypercube in N-dimensional
hyperspace.
For example, for N=4 the Python code should generate the following 2**N
= 16 tuples:
Here's a recursive generator:

def hypercube(ndims):
if ndims == 0:
yield ()
return
for h in 1, -1:
for y in hypercube(ndims-1):
return (h,)+y

print list(hypercube(4))
[(1, 1, 1, 1), (1, 1, 1, -1), (1, 1, -1, 1), (1, 1, -1, -1), (1, -1,
1, 1), (1, -1, 1, -1), (1, -1, -1, 1), (1, -1, -1, -1), (-1, 1, 1, 1),
(-1, 1, 1, -1), (-1, 1, -1, 1), (-1, 1, -1, -1), (-1, -1, 1, 1), (-1,
-1, 1, -1), (-1, -1, -1, 1), (-1, -1, -1, -1)]

Maybe converting each integer in the range(2**N) to binary, then
converting to bit string, then applying the "tuple" function to each
bit string?


Yeah you could do that too.
Jan 4 '06 #2
Dr. Colombes wrote:
Maybe converting each integer in the range(2**N) to binary, then
converting to bit string, then applying the "tuple" function to each
bit string?


A direct translation of that:

def perm(n):
rv = []
for i in xrange(2L**n):
cur = []
for j in range(n):
cur.append(1-2*(bool(i & (1<<j))))
# cur is in reversed order LSB first, but as you seemingly don't
# care about order of the returned tuples, this is irrelevant.
rv.append(tuple(cur))
return rv

modelnine@phoenix ~ $ python
Python 2.4.2 (#1, Dec 22 2005, 17:27:39)
[GCC 4.0.2 (Gentoo 4.0.2-r2, pie-8.7.8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from test1 import perm
perm(5) [(1, 1, 1, 1, 1), (-1, 1, 1, 1, 1), (1, -1, 1, 1, 1), (-1, -1, 1, 1, 1), (1,
1, -1, 1, 1), (-1, 1, -1, 1, 1), (1, -1, -1, 1, 1), (-1, -1, -1, 1, 1), (1,
1, 1, -1, 1), (-1, 1, 1, -1, 1), (1, -1, 1, -1, 1), (-1, -1, 1, -1, 1), (1,
1, -1, -1, 1), (-1, 1, -1, -1, 1), (1, -1, -1, -1, 1), (-1, -1, -1, -1, 1),
(1, 1, 1, 1, -1), (-1, 1, 1, 1, -1), (1, -1, 1, 1, -1), (-1, -1, 1, 1, -1),
(1, 1, -1, 1, -1), (-1, 1, -1, 1, -1), (1, -1, -1, 1, -1), (-1, -1, -1, 1,
-1), (1, 1, 1, -1, -1), (-1, 1, 1, -1, -1), (1, -1, 1, -1, -1), (-1, -1, 1,
-1, -1), (1, 1, -1, -1, -1), (-1, 1, -1, -1, -1), (1, -1, -1, -1, -1), (-1,
-1, -1, -1, -1)]

modelnine@phoenix ~ $

--- Heiko.
Jan 4 '06 #3
Heiko Wundram <mo*******@bit-bukket.org> writes:
def perm(n):
rv = []
for i in xrange(2L**n):
cur = []
for j in range(n):
cur.append(1-2*(bool(i & (1<<j))))
# cur is in reversed order LSB first, but as you seemingly don't
# care about order of the returned tuples, this is irrelevant.
rv.append(tuple(cur))
return rv


def perm(n):
return [tuple([(1,-1)[(t>>i)%2] for i in xrange(n)])
for t in xrange(2L**n)]
perm(4) [(1, 1, 1, 1), (-1, 1, 1, 1), (1, -1, 1, 1), (-1, -1, 1, 1), (1, 1,
-1, 1), (-1, 1, -1, 1), (1, -1, -1, 1), (-1, -1, -1, 1), (1, 1, 1,
-1), (-1, 1, 1, -1), (1, -1, 1, -1), (-1, -1, 1, -1), (1, 1, -1, -1),
(-1, 1, -1, -1), (1, -1, -1, -1), (-1, -1, -1, -1)]

Jan 4 '06 #4
Paul Rubin wrote:
def perm(n):
return [tuple([(1,-1)[(t>>i)%2] for i in xrange(n)])
for t in xrange(2L**n)]


or replace that with:

def perm(n):
return (tuple(((1,-1)[(t>>i)%2] for i in xrange(n)))
for t in xrange(2L**n))

to get a generator like in Paul's first example. Only works with Python 2.4+
though.

--- Heiko.
Jan 4 '06 #5
Heiko Wundram wrote:
Paul Rubin wrote:
def perm(n):
return [tuple([(1,-1)[(t>>i)%2] for i in xrange(n)])
for t in xrange(2L**n)]

or replace that with:

def perm(n):
return (tuple(((1,-1)[(t>>i)%2] for i in xrange(n)))
for t in xrange(2L**n))

to get a generator like in Paul's first example. Only works with Python 2.4+
though.

--- Heiko.


Isn't this kind of coding beeing the result of suffering from the
post-pyContest illness syndrom?

Or is there another reason behind writing it that way sacrificing
readability like usage of less CPU time to run the code?

Or am I alone having trouble to read this kind of code because not
experienced enough in writing Python scripts?

Claudio
Jan 4 '06 #6

Dr. Colombes wrote:
I'm looking for a good Python way to generate (enumerate) the 2**N
tuples representing all vertices of the unit hypercube in N-dimensional
hyperspace.

For example, for N=4 the Python code should generate the following 2**N
= 16 tuples:

(1,1,1,1), (1,1,1,-1),
(1,1,-1, 1), (1,1,-1,-1),
(1,-1,1,1), (1,-1,1,-1),
(1,-1,-1, 1), (1,-1,-1,-1),
(-1,1,1,1), (-1,1,1,-1),
(-1,1,-1, 1), (-1,1,-1,-1),
(-1,-1,1,1), (-1,-1,1,-1),
(-1,-1,-1, 1), (-1,-1,-1,-1)

Maybe converting each integer in the range(2**N) to binary, then
converting to bit string, then applying the "tuple" function to each
bit string?

Thanks for your help.


Is this just a special case for the list of list combine() function
posted not long ago ?

def combine_lol(seq):
return reduce(lambda x,y: (a+(b,) for a in x for b in y), seq,
[()])

list(combine_lol([(1,-1)]*4))

Jan 4 '06 #7
Claudio Grondi wrote:
Heiko Wundram wrote:
def perm(n):
return (tuple(((1,-1)[(t>>i)%2] for i in xrange(n)))
for t in xrange(2L**n))


Isn't this kind of coding beeing the result of suffering from the
post-pyContest illness syndrom?


I don't think what Paul Rubin posted is the sign of pyContest illness, as I
use two-level generator expressions such as this quite often in my code.
Why I didn't give this as the reponse to the OP was because it seems that
he's not that familiar with Python to be able to read this efficiently and
grap the concept behind the implementation immediately, that's why I
thought an explicit loop was in order.

But for any sufficiently advanced Python coder, I don't think this is
unreadable in the least.

--- Heiko.
Jan 4 '06 #8
Paul, Heiko:

Thank you for the quality, parsimony and promptness of your
excellent suggestions.

I wasn't familiar with the Python "yield" function.

Dr. Colombes

Jan 4 '06 #9
Heiko Wundram wrote:
Claudio Grondi wrote:
Heiko Wundram wrote:
def perm(n):
return (tuple(((1,-1)[(t>>i)%2] for i in xrange(n)))
for t in xrange(2L**n))


Isn't this kind of coding beeing the result of suffering from the
post-pyContest illness syndrom?

I don't think what Paul Rubin posted is the sign of pyContest illness, as I
use two-level generator expressions such as this quite often in my code.
Why I didn't give this as the reponse to the OP was because it seems that
he's not that familiar with Python to be able to read this efficiently and
grap the concept behind the implementation immediately, that's why I
thought an explicit loop was in order.

But for any sufficiently advanced Python coder, I don't think this is
unreadable in the least.

--- Heiko.

List comprehension is a nice thing when standalone.
I am usually able to read it from the left to the right and understand
directly.
But with nested list comprehensions I don't see directly what is going
on, because there are more than one identifier I don't know about at the
time of reading the expression and I am forced to read the entire
nestings with eventually some more code spread over it. The trouble is,
that there are no visual hints toward understanding of the used
algorithm like it is the case in the non-list-comprehension version and
I usually have to re-read the nested list comprehension from the right
to the the left in order to get the idea. I don't want to be forced to
read from the right to the left as I am used to read left-right,
top-down and I expect any text (also source code) to be that way.
Similar critics comes sometimes up when someone learns the German
language, because you are forced to read also a very, very long sentence
up to its end, before it is clear what is the action. I consider it as a
problem which would be nice to get rid of if possible, but it is much
easier to try to influence coding style than the usage of a language.

Can usage of deep (i.e. more than two levels) nested list comprehensions
be considered bad coding style?

Claudio
Jan 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Jeff Wagner | last post by:
I've spent most of the day playing around with lists and tuples to get a really good grasp on what you can do with them. I am still left with a question and that is, when should you choose a list or...
11
by: vdavidster | last post by:
Hello everyone, I want to convert a tuple to a list, and I expected this behavior: list(('abc','def')) -> list(('abc')) -> But Python gave me this behavior: list(('abc','def')) ->
5
by: Popoxinhxan | last post by:
HI guy I am working on the web application that required to display the chart image on the page. The process is clicking the button and browser will popup another windows that displayed...
8
by: Bill Rust | last post by:
I've created an "Add Item" wizard for VB.NET 2003 that allows a user to add a specialized class that works with my application framework. In the wizard, the user can select the interfaces they...
0
by: taylorjonl | last post by:
I am having a problem generating some soap proxies dynamically using almost the exact same code as in the MSDN sample. ...
10
by: Will McGugan | last post by:
Hi, I'd like a generator that takes a sequence and yields tuples containing n items of the sqeuence, but ignoring the 'odd' items. For example take_group(range(9), 3) -(0,1,2) (3,4,5) (6,7,8)...
1
by: George Sakkis | last post by:
I've just started toying with the python bindings of BGL and I'm puzzled from the following: True False It seems that the vertices iterator creates new vertex objects every time instead of...
122
by: C.L. | last post by:
I was looking for a function or method that would return the index to the first matching element in a list. Coming from a C++ STL background, I thought it might be called "find". My first stop was...
5
Atran
by: Atran | last post by:
Discover The 4D. The Impossible Hypercube: http://www.metacafe.com/watch/624539/discover_the_4d_the_impossible_hypercube/
10
by: Debajit Adhikary | last post by:
I'm writing this little Python program which will pull values from a database and generate some XHTML. I'm generating a <tablewhere I would like the alternate <tr>'s to be <tr class="Even">...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.