2
$\begingroup$

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}}... }...{{{...   }}}}
$\endgroup$
9
  • $\begingroup$ The rules of this operation are unclear to me: can you explain? $\endgroup$
    – MarcoB
    Apr 3, 2021 at 6:31
  • 2
    $\begingroup$ why not Map[v1 + # &]@v2? $\endgroup$
    – kglr
    Apr 3, 2021 at 6:34
  • $\begingroup$ 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. $\endgroup$ Apr 3, 2021 at 6:39
  • 1
    $\begingroup$ Well, yes, but I try to rephrase my question better. I'll be back. $\endgroup$
    – user57467
    Apr 3, 2021 at 7:26
  • 1
    $\begingroup$ 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? $\endgroup$ Apr 3, 2021 at 18:42

1 Answer 1

4
$\begingroup$

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}}
$\endgroup$
8
  • 1
    $\begingroup$ @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? $\endgroup$ Apr 4, 2021 at 2:32
  • 1
    $\begingroup$ 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. $\endgroup$
    – user57467
    Apr 4, 2021 at 4:26
  • 1
    $\begingroup$ @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? $\endgroup$ Apr 4, 2021 at 4:36
  • 1
    $\begingroup$ 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. $\endgroup$ Apr 4, 2021 at 10:40
  • 1
    $\begingroup$ @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... $\endgroup$ Apr 4, 2021 at 13:04

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