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

-
+1. But Total[#]&/@Tuples[h[1], k] is much faster than Plus @@@ Tuples[h[1], k]. Comparing 3 methods: data = Table[RandomReal[{0, 1}, 1000], {1000}]; First@Timing[Plus @@@ data]; First@Timing[Total[#] & /@ data]; First@Timing[Total[Transpose[data]]] and it gives 0.202801, 0, .015600 screen shot: !Mathematica graphics – Nasser Dec 25 '12 at 2:19
@Nasser you don't need Function: Total /@ ... will do. – Mr.Wizard Dec 25 '12 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. – Royce Dec 25 '12 at 10:46
@Royce, you say Is there a more general key point to take away from this? I am no expert in Mathematica. Not even close. But for me, this is how it goes: 1) Find at least 5 different ways to do it (in Mathematica, this is almost always possible if you keep trying). 2) Find which is the fastest way by timing these different methods. 3) Select the fastest method and use that. To try to reason why one method is faster, is hard, (but you can try) as implementation of these commands is internal and can not easily be guessed. So easier to just treat them as black boxes, but test for speed – Nasser Dec 25 '12 at 17:13

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

-