# tuples with +-1

Is there a simple command that for any $n\in \mathbb N$ gives the list of all $n$-tuples with coefficients $\pm1$? Thus, the list has $2^m$ elements. Thanks.-.

edit retag close merge delete

Sort by » oldest newest most voted

The fastest is

sage: from itertools import product
sage: for p in product((1,-1), repeat=4): print p
(1, 1, 1, 1)
(1, 1, 1, -1)
...
(-1, -1, -1, -1)


and if you want the list

sage: list(product((1,-1), repeat=4))
[(1, 1, 1, 1), (1, 1, 1, -1), ..., (-1, -1, -1, -1)]


If you care about performances, you can evaluate the timings

sage: timeit('for p in Words(length=8, alphabet=[-1,1]): pass')
25 loops, best of 3: 7.77 ms per loop
sage: timeit('for p in cartesian_product_iterator([[-1,1] for i in range(8)]): pass')
625 loops, best of 3: 868 µs per loop
sage: timeit('for p in product((1,-1), repeat=8): pass')
625 loops, best of 3: 26.6 µs per loop


So product is fastest, then comes cartesian_product_iterator and then words. The difference is non non-negligible!

more

Nice, comparison. So, it seems we should update sage/misc/mrange.py to be based on the more recent itertools.product.

( 2013-07-10 08:09:43 +0200 )edit

product in itertools is *older* than Sage!

( 2013-07-10 08:11:40 +0200 )edit

Definitely not: itertools.product [appears in Python 2.6](http://docs.python.org/2/library/itertools.html#itertools.product) which was [released in October 2008](http://www.python.org/download/releases/2.6/). According to hg blame -d and the copyright notice, most of the code (not the doc) of sage/misc/mrange.py was [written in July 2006](http://hg.sagemath.org/sage-main/src/tip/sage/misc/mrange.py?at=default), more than two years before !

( 2013-07-10 14:06:13 +0200 )edit

I see. Thank you! I always thought that we do not see evolution of Python inside Sage but I was definitely wrong!

( 2013-07-10 14:09:41 +0200 )edit

You can use the words of length $n$ over the alphabet $\{-1,1\}$, and the transform each of them into a tuple:

sage: n = 4
sage: W = Words(length=n, alphabet=[-1,1])
sage: W
Words of length 4 over {-1, 1}
sage: L = [tuple(w) for w in W]
sage: L
[(-1, -1, -1, -1),
(-1, -1, -1, 1),
(-1, -1, 1, -1),
(-1, -1, 1, 1),
(-1, 1, -1, -1),
(-1, 1, -1, 1),
(-1, 1, 1, -1),
(-1, 1, 1, 1),
(1, -1, -1, -1),
(1, -1, -1, 1),
(1, -1, 1, -1),
(1, -1, 1, 1),
(1, 1, -1, -1),
(1, 1, -1, 1),
(1, 1, 1, -1),
(1, 1, 1, 1)]


If $n$ is large and you want to save memory, you can creat an iterator instead of a list, by writing, instead of L:

sage: It = (tuple(w) for w in W)
sage: for i in It:
....:     print i
....:
(-1, -1, -1, -1)
(-1, -1, -1, 1)
(-1, -1, 1, -1)
(-1, -1, 1, 1)
(-1, 1, -1, -1)
(-1, 1, -1, 1)
(-1, 1, 1, -1)
(-1, 1, 1, 1)
(1, -1, -1, -1)
(1, -1, -1, 1)
(1, -1, 1, -1)
(1, -1, 1, 1)
(1, 1, -1, -1)
(1, 1, -1, 1)
(1, 1, 1, -1)
(1, 1, 1, 1)


You can also use the cartesian products:

sage: A = cartesian_product_iterator([[-1,1] for i in range(n)])
sage: for i in A:
....:     print i
....:
(-1, -1, -1, -1)
(-1, -1, -1, 1)
(-1, -1, 1, -1)
(-1, -1, 1, 1)
(-1, 1, -1, -1)
(-1, 1, -1, 1)
(-1, 1, 1, -1)
(-1, 1, 1, 1)
(1, -1, -1, -1)
(1, -1, -1, 1)
(1, -1, 1, -1)
(1, -1, 1, 1)
(1, 1, -1, -1)
(1, 1, -1, 1)
(1, 1, 1, -1)
(1, 1, 1, 1)

more

Nice and simple answers. Thank you!

( 2013-07-10 06:58:20 +0200 )edit