0
$\begingroup$

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

$\endgroup$

1 Answer 1

1
$\begingroup$

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.
$\endgroup$
2
  • $\begingroup$ Wow! Great! I yet don't understand your code.It will give me a long time to chew. Thanks for your effort! $\endgroup$
    – user57467
    Commented Apr 7, 2021 at 15:39
  • 1
    $\begingroup$ Hi, I added an explanation to the answer. $\endgroup$ Commented Apr 7, 2021 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.