26
$\begingroup$

What is the syntax to add a vector v1 to each vector in a list of vectors v2?

I know it has to be simple, but I really have searched and not found it.

v1 = {a, b, c}
v2 = {{d, e, f}, {g, h, i}, {j, k, l}}

i.e., sum them in a way to give:

{{a + d, b + e,c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}
$\endgroup$
0

10 Answers 10

30
$\begingroup$

I recommend using Transpose twice since it is more efficient than other approaches. Moreover Plus has the Listable attribute, thus one need not map Plus over a list (vector).

Transpose[v1 + Transpose[v2]]
{{a + d, b + e, c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}

Having said that remember that one can rewrite it very concisely in the Front-End: Esc tr Esc :

enter image description here

$\endgroup$
4
  • $\begingroup$ Hah, clearly my knowledge of Inner isn't well enough entrenched yet. I failed to notice that my Inner effectively simplified to Transpose @* Plus! $\endgroup$ Sep 19, 2015 at 22:28
  • $\begingroup$ @ciao Thanks, unfortunately someone unupvoted my answer (it was edited) so I guess it is inadvisable to improve posts. $\endgroup$
    – Artes
    Sep 20, 2015 at 0:23
  • 3
    $\begingroup$ @Artes Of course it is advisable to improve posts! $\endgroup$ Sep 20, 2015 at 18:24
  • 1
    $\begingroup$ In similar contexts ... While I use Transpose all the time - for intuitive clarity and readability, sometimes one can get faster performance using Part and All in its place ... just FYI testData = Table[{{i, 2 i}, 3 i}, {i, 1000000}]; Print@Timing[ Length[ Last[ Transpose[testData]]]]; Print@Timing[ Length[ Part[ testData, All, 2]]]; Print@Timing[ Length[ testData[[ All, 2]]]]; $\endgroup$ Sep 23, 2015 at 13:32
17
$\begingroup$

To achieve what you need requires to distribute the sum over v2:

(v1 + # &) /@ v2

which is a short form of:

Map[ v1 + # &, v2 ]
$\endgroup$
1
  • $\begingroup$ This is a good one for my list of "expressions that help me understand mathematica syntax"! Thanks. $\endgroup$
    – DrBubbles
    Sep 19, 2015 at 22:17
10
$\begingroup$

Alternative method using the magic that is Inner:

Inner[Plus, {a, b, c}, Transpose@{{d, e, f}, {g, h, i}, {j, k, l}}, List]
$\endgroup$
10
$\begingroup$

Also:

Plus @@@ Thread[{v1, v2}, List, {2}]
$\endgroup$
6
$\begingroup$

I couldn't resist adding this:

TranslationTransform[v1] /@ v2
$\endgroup$
2
  • 3
    $\begingroup$ More terse and universal version: TranslationTransform[v1][v2]. $\endgroup$ Oct 12, 2015 at 13:39
  • 3
    $\begingroup$ This function is for that purpose by far the slowest solution ... $\endgroup$
    – mrz
    May 27, 2016 at 21:10
6
$\begingroup$

Using Outer is also an efficient approach, in some cases faster than using Transpose twice, the undocumented function Statistics`Library`MatrixRowTranslate may be the fastest

v1 = RandomReal[1, 10^5];
v2 = RandomReal[1, {10^3, 10^5}];

r1 = Transpose[v1 + Transpose[v2]]; // RepeatedTiming
r2 = Outer[Plus, {v1}, v2, 1][[1]]; // RepeatedTiming
r3 = Block[{res=v2}, Statistics`Library`MatrixRowTranslate[res, v1]; res]; // RepeatedTiming

r1===r2===r3

Output

{0.541983, Null}
{0.446791, Null}
{0.241258, Null}
True

Related link:
Improving Map Function on Lists

Elegant operations on matrix rows and columns

$\endgroup$
4
$\begingroup$
Total[Tuples[{{v1}, v2}], {2}]
 {{a + d, b + e, c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}
Distribute[{{v1}, v2}, List, List, List, Plus]
 {{a + d, b + e, c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}
$\endgroup$
4
$\begingroup$

In versions 13.1+ you can wrap v1 with Threaded:

Threaded[v1] + v2
{{a + d, b + e, c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}
$\endgroup$
1
  • $\begingroup$ Interesting new command! $\endgroup$
    – bill s
    Apr 11 at 22:27
2
$\begingroup$

In versions 13.1+ you can use ReplaceAt:

ReplaceAt[{v2}, _ -> Map[#1 + v1 &], 0]

(*{{a + d, b + e, c + f}, {a + g, b + h, c + i}, {a + j, b + k, c + l}}*)
$\endgroup$
2
$\begingroup$

Here was my first thought...

ConstantArray[v1, 3] + v2

or more generally

ConstantArray[v1, Length[v2]] + v2
$\endgroup$

Your Answer

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

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