# How to create a Table of Tables with indexed variables

I want to use concise code to generate a table of values that list the addition of a set of vectors. What I am looking for is essentially a Table of Tables.

I will show here the kind of lists I want, which I am able to generate individually.

First, let's define a set of say 6 vectors (an ordered pair):

H = Table[{Cos[n*Pi/3], Sin[n*Pi/3]}, {n, 0, 5, 1}];


This will be our first Table. Next, I want to get all vectors that result from adding any two vectors in H together:

H2 = Table[H[[i1]] + H[[i2]], {i1, 1, 6, 1}, {i2, 1, 6, 1}];


Now we create another Table, but this time with all vectors that result from choosing three vectors from the set H and adding them together:

H3 = Table[H[[i1]] + H[[i2]] + H[[i3]], {i1, 1, 6, 1}, {i2, 1, 6, 1}, {i3, 1, 6, 1}];


Suppose we were to continue creating tables like this with arbitrarily many sums of this type. How can I create one single Table that would combine them all?

I am aware that I can use some function like Append to join them all together after each one has been generated, but I am looking for something more efficient that can be done a single cell.

I have something in mind along the following lines (note that this is not properly functioning code, but I am writing it for illustrative purposes). It would be nice to be able to generalize these Tables using something like this, where I've attempted to index the variables (incorrectly?):

H[N_] =
Table[
Sum[ H[[i_j]] , {j, 1, N, 1}],
{i_1, 1, 6, 1}, {i_2, 1, 6, 1}, ... {i_N, 1, 6, 1}] ;


Then I could create a single table with say 10 iterations using:

Table[ H[N],{N,1,10,1}]


Unfortunately, I am unable to index variables in this way because Mathematica tells me the variable names are "Protected". How can I get around this?

I wasn't sure exactly what terms were included in your summations - here are some ideas though:

Define h[1] = Table[{Cos[n*Pi/3], Sin[n*Pi/3]}, {n, 0, 5, 1}]; as you have.

Then try h[k_] := Plus @@@ Tuples[h[1], k] if the sums you are referring to can include the same element multiple times, or try h[k_] := Plus @@@ Subsets[h[1], {k}]if you're summing over distinct terms only.

• @Nasser you don't need Function: Total /@ ... will do. Commented Dec 25, 2012 at 4:53
• @Nasser thanks for the tip! That's a significant difference indeed. Interesting that Total/@data is noticeably faster than Apply[Plus,#]&/@data, too. Is there a more general key point to take away from this? I'm not sure I understand exactly why there's such a speed difference. Commented Dec 25, 2012 at 10:46

Using

  H = Table[{Cos[n*Pi/3], Sin[n*Pi/3]}, {n, 0, 5, 1}];


define

 hF[n_] := With[{arg1 = ConstantArray[H, n], dims = Join[ConstantArray[6, n], {2}]},
ArrayReshape[Tuples[\[FormalA][Sequence @@ arg1]] /. \[FormalA] -> Plus, dims]]


Usage:

 hF[1]
(* {{1, 0}, {1/2, Sqrt[3]/2}, {-(1/2), Sqrt[3]/2}, {-1, 0},
{-(1/2), -(Sqrt[3]/2)},   {1/2, -(Sqrt[3]/2)}} *)

hF[2] == H2
(* True *)

hF[3] == H3
(* True *)