0

This question refers to an extension of Add list of vectors to list of vectors recursively . I tried hard to complete the problem but now I got stuck. I have vectors

lis0 = {u, v}
m = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

Adding each component of m to lis0 gives me

lis1={{{u, v}, {1 + u, v}}, {{u, v}, {u, 1 + v}}, {{u, v}, {-1 + u, v}}, {{u, v}, {u, -1 + v}}}

Now I want to add each vector of m to each last component of lis1 resulting in 16 sublists

lis2={{{{u, v}, {1 + u, v}, {2 + u, v}}, {{u, v}, {1 + u, v}, {1 + u, 1 + v}},{{u, v}, {1 + u, v}, {u, v}}...

So now I want to continue adding m to each last element in lis2 and so on. Lists will grow exponentially in size. Here is my code so far:

lis1 = {u, v} + # & /@ m;
lis1 = List[lis0, #] & /@ lis1

x2 = Map[Function[z, z + # & /@ m], Last /@ lis1]

here x2 is part of next lis2

user57467
  • 2,708
  • 6
  • 12

1 Answers1

1

This seems a case for recursive programming. Call n then number of additions:

n = 2;
add[dat_] := Module[{},
  If [Length[Flatten[dat]] >= 8 n + 1, Return[dat]];
  Switch[dat
   , {{u, v}, ___}, add[ Append[dat, Last[dat] + #] & /@ m ]
   , {__}, add /@ dat
   ] ]
res = add[{{u, v}}]

enter image description here

Explanation:

  1. First check if we already reached the end (recursion stop).
  2. If we have {{u,v},...}, we need to create 4 new lists, each with an element of m added to the original list.
  3. If the structure of data is not {{u,v},..} we need to dig deeper, that is we need to apply add to the sub lists.
Daniel Huber
  • 51,463
  • 1
  • 23
  • 57