6
$\begingroup$

I have a list of vectors and a list of matrices for each vector, what i need is to multiply each list of matrices to respective vector.

v = {{a1, a2}, {b1, b2}, {c1, c2}};

U = {
{{{U1a11, U1a12}, {U1a21, U1a22}}, 
 {{U2a11, U2a12}, {U2a21, U2a22}},
 {{U3a11, U3a12}, {U3a21, U3a22}},
 {{U4a11, U4a12}, {U4a21, U4a22}}}},
{{{U1b11, U1b12}, {U1b21, U1b22}},
 {{U2b11, U2b12}, {U2b21, U2b22}}, 
 {{U3b11, U3b12}, {U3b21, U3b22}}, 
 {{U4b11, U4b12}, {U4b21, U4b22}}}, 
{{{U1c11, U1c12}, {U1c21, U1c22}}, 
 {{U2c11, U2c12}, {U2c21, U2c22}},
 {{U3c11, U3c12}, {U3c21, U3c22}}, 
 {{U4c11, U4c12}, {U4c21, U4c22}}}
};

I know that I can use a Do

vt = ConstantArray[0, {Length[U], Length[v]}];

Do[vt[[it]] = (#.v[[it]]) & /@ U[[it]], {it, 1, 3}]

vt

But I don't want to use a Do cycle. Is there a way to do it in an intelligent way?

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ Mar 20, 2016 at 23:35

2 Answers 2

4
$\begingroup$

A few alternatives to MapThread, with res = MapThread[Dot, {U, v}] from Dr. belisarius' answer,

res1 = Dot @@@ Transpose[{U, v}];
res2 = Dot @@@ Thread[{U, v}];
res3 = Thread[dot[U, v]] /. dot -> Dot;
res4 = Activate[Thread[Inactive[Dot][U,v]]] (* if you have version 10 *);
Equal @@ {res1, res2, res3, res4, res}

True

$\endgroup$
2
  • $\begingroup$ +1 I particularly hate the Thread[dot[U, v]] /. dot -> Dot thing. It is needed in a lot of situations (like the testxxxQ functions) and it makes me feel soooo uncomfortable $\endgroup$ Mar 21, 2016 at 10:52
  • $\begingroup$ @Dr.belisarius I can't agree more. Thank you for the vote. $\endgroup$
    – kglr
    Mar 21, 2016 at 11:11
6
$\begingroup$
v = {{a1, a2}, {b1, b2}, {c1, c2}};

U = {
   {{{U1a11, U1a12}, {U1a21, U1a22}}, {{U2a11, U2a12}, {U2a21, U2a22}},  
    {{U3a11, U3a12}, {U3a21, U3a22}}, {{U4a11, U4a12}, {U4a21, U4a22}}}, 
   {{{U1b11, U1b12}, {U1b21, U1b22}}, {{U2b11, U2b12}, {U2b21, U2b22}},
    {{U3b11, U3b12}, {U3b21, U3b22}}, {{U4b11, U4b12}, {U4b21,U4b22}}}, 
   {{{U1c11, U1c12}, {U1c21, U1c22}}, {{U2c11, U2c12}, {U2c21, U2c22}}, 
    {{U3c11, U3c12}, {U3c21, U3c22}}, {{U4c11, U4c12}, {U4c21, U4c22}}}};

res = MapThread[#1.#2 &, {U, v}];

Map[MatrixForm, res, {2}]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Why not just Dot instead of wrapping it in an additional pure function call with #1.#2&? $\endgroup$ Mar 21, 2016 at 11:16
  • $\begingroup$ @MartinBüttner No special reason. I feel more comfortable with it, and the problem is small enough to hardly justify trying to measure performance subtleties $\endgroup$ Mar 21, 2016 at 11:41
  • $\begingroup$ @MartinBüttner Now, thinking again about why I tend to use it like this, see kglr's answer with the Thread[dot[U, v]] /. dot -> Dot horrible construction. Thread and Dot are a nasty marriage and I guess that one develops allergies to the simple sight of them together. I'm using MapThread here, but probably my running nose doesn't discriminate $\endgroup$ Mar 21, 2016 at 11:46

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.