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

Dr. Colombes DrColombes at yahoo.com
Tue Jan 3 20:21:48 EST 2006


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.




More information about the Python-list mailing list