# A problem with List and Vector

GROUPS:
 First, a=1;b=2;c=3; {a,b}+c will be {a+c,b+c}, i.e.{4,5}. This is the Distributive property of List.Second, a={1,1}; b={1,2}; a+b will be {1+1, 1+2} i.e. {2, 3}. This is Plus between Vectors.But the problem comes when I try to combine these two features.a = {1, 1}; b = {1, 2}; c = {1, 2};{a + c, b + c}{a, b} + cResult:{{2, 3}, {2, 4}} (* Right *){{2, 2}, {3, 4}} (* Wrong *)it seems that Mathematica interprets {a, b} as a matrix here.How can I fix this problem?I can fix this simple problem by Plus[#, c]&/@{a,b}But I want a universal solution. Anyone can help?
4 years ago
8 Replies
 Nasser M. Abbasi 1 Vote I see no problem at all. It is all consistent behavior. It is based on element by element operation.When you write{a, b} + {x, y}Then the first element "a" is added to the first element "x". Then the second element "b" is added to the second element "y".{a + x, b + y}This you first case, which you say is right. (simply let a=1,b=1,x=1,y=2.Now lets look at the second case, where now "a" happend to be a list of 2 elements. No problem. The same thing will happen{{a1, a2}, {b1, b2}} + {x, y}Mathematica will now do the same thing. It will add "x" to the first element which happend to be {a1, a2}.  Giving {a1 + x, a2 + x}which I am sure you agree is correct. Next it go to the next element, and adds "y" to {b1, b2} giving {b1 + y, b2 + y} so you end up with{{a1 + x, a2 + x}, {b1 + y, b2 + y}}Replace a1=1, a2=1,b1=1,b2=2,x=1,x=2 and you get the second case.And if you had 3 elements in the list, the same thing will happen. Each term is added to each other term, one by one. {{a1, a2, a2}, {b1, b2, b3}} + {x, y}(*   {    {a1 + x, a2 + x, a2 + x}    ,      {b1 + y, b2 + y, b3 + y}    }  *)
4 years ago
 Moon Liu 1 Vote Thanks for your reply. You're right.{{a1, a2}, {b1, b2}} + {x, y} == {{a1 + x, a2 + x}, {b1 + y, b2 + y}}and this is indeed a consistent behavior.However, in my case, a different task is concerned.{{a1, a2}, {b1, b2}} [op] {x, y} == {{a1 + x, a2 + y}, {b1 + x, b2 + y}}How can I deal with it? Which is the right ?More precisely, in the first caseMathematica regards {a1, a2} and x in the same level.In my case, however, I want {a1, a2} and {x, y} in the same level.The following code may illustrate my problem OffsetPoint[a_, b_] := a + b; a = {1, 1}; b = {1, 2}; c = {1, 2}; A = OffsetPoint[a, c] B = OffsetPoint[b, c] {A, B} = OffsetPoint[{a, b}, c]; (* I just want to simplify my code here *) A B (* Result: *) {2, 3}{2, 4}{2, 2}(* But I got wrong answer here *){3, 4}
4 years ago
 Not sure if this is exactly what you're looking for, but you can use Thread:a = {1, 1}; b = {1, 2}; c = {1, 2};Thread[{a, b} + c]
4 years ago
 Thanks for your reply. I've tried Thread, but it seems not to be the answer.a = {a1, a2}; b = {b1, b2}; c = {c1, c2};Thread[{a, b} + c](*Result:{{a1 + c1, b1 + c2}, {a2 + c1, b2 + c2}}But I want {{a1 + c1, a2 + c2}, {b1 + c1, b2 + c2}}*)Actually the closest answer I've tried is Table. a = {a1, a2}; b = {b1, b2}; c = {c1, c2}; ans1 = Table[Plus[x, y], {x, {a}}, {y, {c}}]   (* First discrepancy: apparently listed input {a,b} needs curly braces, but the simply input "a" ought not to need them. *) ans2 = Table[Plus[x, y], {x, {a, b}}, {y, {c}}] (* Result: {{{a1 + c1, a2 + c2}}} {{{a1 + c1, a2 + c2}}, {{b1 + c1, b2 + c2}}} One step further, Flatten is needed to get the right answer. *)Flatten[ans1, 2] (* Second discrepency: flatten levels differ *)Flatten[ans2, 1](*Result:{a1 + c1, a2 + c2}{{a1 + c1, a2 + c2}, {b1 + c1, b2 + c2}}*)The reason why I don't see Table as the best answer is that, as you can see, I'm not satisfied with the discrepancy between simple input and Listed input.I think my purpose is simple. I just want {a, b} + c always equals {a + c, b + c}. But this relation doesn't hold if a, b and c are vectors. TAT
4 years ago
 Clear["*"];  a = {1, 1}; b = {3, 2}; ref1 = {1, 2}; ref2 = {3, 2}; OffsetPoint[a_, b_] := a + b; OffsetPointEx[a_, b_] := Module[{bool1, bool2, para1, para2, ans},    para1 = a; para2 = b;    bool1 = If[Depth[a] == 2, 1, 0];    bool2 = If[Depth[b] == 2, 1, 0];    If[bool1 == 1, para1 = {a}]; (* fix the first discrepancy *)   If[bool2 == 1, para2 = {b}];   ans = Table[Plus[x, y], {y, para2}, {x, para1}]; (* Notice the sequence here *)   Flatten[ans, bool1 + bool2] (* fix the second discrepancy *)   ];oA = OffsetPoint[a, ref1];oB = OffsetPoint[b, ref1];oC = OffsetPoint[a, ref2];oD = OffsetPoint[b, ref2];{{oA, oB}, {oC, oD}}OffsetPointEx[a, ref1]OffsetPointEx[{a, b}, ref1]OffsetPointEx[{a, b}, {ref1, ref2}]OffsetPointEx[{a, b}, #] & /@ {ref1, ref2}(*Result:{{{2, 3}, {4, 4}}, {{4, 3}, {6, 4}}}{2, 3}{{2, 3}, {4, 4}}{{{2, 3}, {4, 4}}, {{4, 3}, {6, 4}}}{{{2, 3}, {4, 4}}, {{4, 3}, {6, 4}}}*)I wrote this. It's right but really complicated and unpractical!