# 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.-.

tuples with +-1

add a comment

3

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!

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 !

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

1

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)
```

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

Asked: ** 2013-07-10 04:55:40 +0200 **

Seen: **244 times**

Last updated: **Jul 10 '13**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.