2

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}}... }...{{{...   }}}}
user57467
  • 2,708
  • 6
  • 12

1 Answers1

4

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}}
CA Trevillian
  • 3,342
  • 2
  • 8
  • 26
  • Yes, that's the right solution. Great! But I will need some time to analyze it. But I also look for a solution which enables me to to reiterate it. That is, I want to use your solution and set it as v2new and apply v1new again and again.Later I will try to put bounderies on all values. But that would be the next next after I understood all the other parts. – user57467 Apr 04 '21 at 00:12
  • 1
    @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? – CA Trevillian Apr 04 '21 at 02:32
  • 1
    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. – user57467 Apr 04 '21 at 04:26
  • 1
    @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? – CA Trevillian Apr 04 '21 at 04:36
  • Yes! Great! Though I still do not understand your code completely, how would you reiterate? So what would be the general solution for n dimensions without writing a new command for each step? If it makes easier for you to check results just set u=0 and v=0. – user57467 Apr 04 '21 at 05:46
  • 1
    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. – CA Trevillian Apr 04 '21 at 10:40
  • Ok. Thanks for your effort! I will keep struggling. – user57467 Apr 04 '21 at 11:45
  • 1
    @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... – CA Trevillian Apr 04 '21 at 13:04