Ask Your Question
0

tuples with +-1

asked 2013-07-09 21:55:40 -0500

emiliocba gravatar image

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 flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
3

answered 2013-07-09 23:54:34 -0500

updated 2013-07-09 23:59:48 -0500

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!

edit flag offensive delete link more

Comments

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

tmonteil gravatar imagetmonteil ( 2013-07-10 01:09:43 -0500 )edit

product in itertools is *older* than Sage!

vdelecroix gravatar imagevdelecroix ( 2013-07-10 01:11:40 -0500 )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 !

tmonteil gravatar imagetmonteil ( 2013-07-10 07:06:13 -0500 )edit

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

vdelecroix gravatar imagevdelecroix ( 2013-07-10 07:09:41 -0500 )edit
1

answered 2013-07-09 23:09:31 -0500

tmonteil gravatar image

updated 2013-07-09 23:19:12 -0500

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)
edit flag offensive delete link more

Comments

Nice and simple answers. Thank you!

emiliocba gravatar imageemiliocba ( 2013-07-09 23:58:20 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-07-09 21:55:40 -0500

Seen: 132 times

Last updated: Jul 09 '13