9
$\begingroup$

How does one multiply a list of matrices by a list of vectors, elementwise? For example, multiplying

A = {IdentityMatrix[2], 2*IdentityMatrix[2]}
x = {{1, 1}, {2, -2}}

should return

{{1, 1}, {4, -4}}

Neither Dot nor Times accomplishes this; both have the wrong dimensions. A cumbersome way would be

result = {{0, 0}, {0, 0}}
Do[result[[i]] = A[[i]].x[[i]], {i, 2}]

but surely there is a cleaner way.

$\endgroup$

3 Answers 3

10
$\begingroup$
MapThread[Dot, {A, x}]   // TeXForm

$\left( \begin{array}{cc} 1 & 1 \\ 4 & -4 \\ \end{array} \right)$

$\endgroup$
4
$\begingroup$

Perhaps just:

Dot @@@ Transpose[{A, x}]
$\endgroup$
2
$\begingroup$

Following Belasarius in a somewhat more transparent fashion, you Apply[ ] the Dot function to A and x. The Transpose is used to get the A and x into a single list and the "1" causes the Apply to to work at the correct level.

 Apply[Dot, Transpose[{A, x}], 1]
$\endgroup$

Your Answer

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

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