# How to add a vector list to a multiple dimension list of vectors [closed]

This problem is related to Add a vector to a list of vectors. Given:

v1 = {{1,0},{0,1},{-1,0},{0,-1}}
v2 = {{{u, v}, {1 + u, v}}, {{u, v}, {u, 1 + v}}, {{u, v}, {-1 + u, v}}, {{u,v}, {u, -1 + v}}};

I want to add each part of v1 to all last parts of subvectors of v2, so I would get a list with 4 x 4 parts starting with

{{{{u, v}, {1 + u, v},{2 + u, v}}, {{u, v}, {1 + u, v},{1 + u, 1 + v}}... }...{{{...   }}}}
• The rules of this operation are unclear to me: can you explain? Apr 3, 2021 at 6:31
• why not Map[v1 + # &]@v2?
– kglr
Apr 3, 2021 at 6:34
• Oop just saw what you posted after I had written my answer @kglr, but chances are OP wants something more intricate & the solution may not work in a general use case. Apr 3, 2021 at 6:39
• Well, yes, but I try to rephrase my question better. I'll be back. Apr 3, 2021 at 7:26
• Ah, I will try to come up with a solution to your real requirements you have edited in recently. This stands to be a completely different question than before, just fyi. To clarify, you are to change the dimensions completely of v2 when you add in v1? Apr 3, 2021 at 18:42

Wrapping the current solution into a function can easily be done:

v3[v1_,v2_]:=Table[{#[[ ;; ]]~Join~#[[{-1}]]} & /@ v2// #[[j]] & // Table[{#[[;; -2]]~Join~{v1[[i]] + #[[-1]]}} & @@ #, {i, Length@v1}] &, {j, Length@v2}] // Flatten[#, 1] & /@ # &

And you would apply it like so:

v3[v1new,#]&/@v3[v1new,v2new];
%//Dimensions
{4,4,4,4,2}

To apply it yet again:

v3[v1new,#]&/@#&/@(v3[v1new,#]&/@v3[v1new,v2new]);
%//Dimensions
{4,4,4,4,5,2}

And again:

v3[v1new,#]&/@#&/@#&/@(v3[v1new,#]&/@#&/@(v3[v1new,#]&/@v3[v1new,v2new]));
%//Dimensions
{4,4,4,4,4,6,2}

Given the new definitions:

v1new = {{1,0},{0,1},{-1,0},{0,-1}};
v2new = {{{u, v}, {1 + u, v}}, {{u, v}, {u, 1 + v}}, {{u, v}, {-1 + u, v}}, {{u,v}, {u, -1 + v}}};

This can likely be cleaned up, and I’ll consider how a bit later, but this seems do what you want:

Table[{#[[ ;; ]]~Join~#[[{-1}]]} & /@ v2new // #[[j]] & // Table[{#[[;; -2]]~Join~{v1new[[i]] + #[[-1]]}} & @@ #, {i, Length@v1new}] &, {j, Length@v2new}] // Flatten[#, 1] & /@ # &
{{{{u, v}, {1 + u, v}, {2 + u, v}}, {{u, v}, {1 + u, v}, {1 + u, 1 + v}}, {{u, v}, {1 + u, v}, {u, v}}, {{u, v}, {1 + u, v}, {1 + u, -1 + v}}}, {{{u, v}, {u, 1 + v}, {1 + u, 1 + v}}, {{u, v}, {u, 1 + v}, {u, 2 + v}}, {{u, v}, {u, 1 + v}, {-1 + u, 1 + v}}, {{u, v}, {u, 1 + v}, {u, v}}}, {{{u, v}, {-1 + u, v}, {u, v}}, {{u, v}, {-1 + u, v}, {-1 + u, 1 + v}}, {{u, v}, {-1 + u, v}, {-2 + u, v}}, {{u, v}, {-1 + u, v}, {-1 + u, -1 + v}}}, {{{u, v}, {u, -1 + v}, {1 + u, -1 + v}}, {{u, v}, {u, -1 + v}, {u, v}}, {{u, v}, {u, -1 + v}, {-1 + u, -1 + v}}, {{u, v}, {u, -1 + v}, {u, -2 + v}}}}

Old answer with the initial definitions given:

v1 = {a, b};
v2 = {{{d, e}, {{g, h}, {r, s}}}, {j, k}};

This seems to do what you want:

v1+#&/@v2
{{{a+d,a+e},{{b+g,b+h},{b+r,b+s}}},{a+j,b+k}}
• @user57467 this can easily be wrapped up into a function, but just so I understand, if you apply the same operation again & again, won’t you continue to increase the dimensions again & again? What is your expected result? Apr 4, 2021 at 2:32
• Yes, you are right. Dimensions will increase exponentially. But later I want to put restraints after each step. I will delete negative values and also values greater than a certain value. This will limit the amount of memory but not not the depth of dimension. Apr 4, 2021 at 4:26
• @user57467 please see the updates, I think this is what you are going for? If not, please, let me know what is not as intended? Apr 4, 2021 at 4:36
• I think that’s a bit outside the scope of this question...I show how to reiterate the application of the function. I believe that finding a general solution should either be up to you, or if you have some failings, another question may be asked...it almost seems as though that would be a question best left for mathematics SE, rather than here. Apr 4, 2021 at 10:40
• @user57467 to be clear, if you want your new question to be answered, you should ask a new question. I think that your question here has been answered... Apr 4, 2021 at 13:04