0

This question relates to Add a vector to a list of vectors whereas here I want to add list of vectors to list of vectors recursively. Given:

m = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
k = m

Adding each vector of m to each vector of k is done by

k = Transpose[# + Transpose[m]] & /@ k

resulting in

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

This is my new k and should go back into my code, so I can add the vectors m to each element of this new k. The new result starts with {{{{3,0},{2,1},{1,0),{2,-1}}... I tried

Table[k = (Transpose[# + Transpose[m]] & /@ k), {i, 2}]

But it does not work and then I get too confused. Any idea for this tree-like structure?

kglr
  • 394,356
  • 18
  • 477
  • 896
user57467
  • 2,708
  • 6
  • 12

1 Answers1

2

This can be done with Nest and Outer. If you need intermediate results, you would use NestList:

n = 1; (*number of recursions*)
m = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
Nest[Flatten[Outer[Plus, #, m, 1], 1] &, m, n]

enter image description here

An for n=2:

enter image description here

Daniel Huber
  • 51,463
  • 1
  • 23
  • 57