# 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? – Michael E2 Sep 27 '16 at 18:05
• x={1,2,3,4} and then Tuples[{-#, #}&/@x] or Tuples[Transpose[{-x, x}]] – Coolwater Sep 27 '16 at 18:08
• You have repeated set {-1,-2}!? – Algohi Sep 27 '16 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]] ;-) – mgamer Sep 27 '16 at 20:54
• Slot-free: Tuples[Outer[Times, Range[n], {-1, 1}]]. – J. M. will be back soon Dec 11 '16 at 12:31