# Create vector set

I would want to create the vectors $(\pm x_1, \pm x_2, \ldots,\pm x_n), \; \{x_1,x_2,x_3 \in \mathbb R\}, \{n\in \mathbb N \}$.

For small $n$, this can be done manually (here $n=2, \, x_1 = 1, \, x_2 = 2$),

$(\pm 1, \pm 2) = \{(-1, -2), \;(1,-2), \; (-1,2), \;(1,2) \}$

but the number of vectors increases exponentially. Is there a neat built-in syntax for this kind of list manipulation?

• Perhaps Tuples? Commented Sep 27, 2016 at 18:05
• x={1,2,3,4} and then Tuples[{-#, #}&/@x] or Tuples[Transpose[{-x, x}]] Commented Sep 27, 2016 at 18:08
• You have repeated set {-1,-2}!? Commented Sep 27, 2016 at 18:40

Like Coolwater pointed out in the comments you could use the built-in Tuples command:

Tuples[{-#,#}&/@Range[1,n]]

which works pretty fast.

Another (slow) but natural approach is using PatternMatching:

n=2;
Flatten[Table[PlusMinus[i],{i,1,n}]//.{a___,PlusMinus[b_],c___}:>{{a,b,c},{a,-b,c}},n-1]

{{1,2},{1,-2},{-1,2},{-1,-2}}

• If one needs performance Distribute[{-#, #} & /@ Range[1, 5], List] is slightly faster than Tuples[{-#, #} & /@ Range[1, 5]] ;-) Commented Sep 27, 2016 at 20:54
• Slot-free: Tuples[Outer[Times, Range[n], {-1, 1}]]. Commented Dec 11, 2016 at 12:31