3
$\begingroup$

I have two lists of 3x3 matrices, of equal length. I would like to make a list of their pairwise matrix products. Is there a more elegant way to do it than this ?

rotationmatrices={{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 0, -1}, {0, 1, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}}};
scalematrices={{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 2, 0}, {0, 0, 1}}, {{2, 0, 0}, {0, 1, 0}, {0, 0, 2}}};
Map[Apply[Dot, Transpose[{rotationmatrices, scalematrices}][[#]]] &, Range[Length[rotationmatrices]]]

I also tried

Inner[Dot, rotationmatrices, scalematrices, List]

and

Inner[Dot[#1, #2] &, rotationmatrices, scalematrices, List]

but neither of these worked.

$\endgroup$
2
  • 2
    $\begingroup$ MapThread[Dot, {rotationmatrices, scalematrices}]? $\endgroup$ Aug 14, 2017 at 11:33
  • $\begingroup$ Brilliant ! Thank you J. M. !! $\endgroup$
    – Simon
    Aug 14, 2017 at 11:39

2 Answers 2

2
$\begingroup$

If you want all products between rotation and scaling matrices you want Outer.

(You know you have some repeated elements in your scale matrices, right?)

Here's one way of seeing all the Dots between each rotation and each scale matrix, and understanding where they came from:

 allDots= ((Outer[dot[rot[Position[rotationmatrices, #1] // Flatten], 
        scale[Position[scalematrices, #2] // Flatten]] -> 
       Dot[#1, #2] &, rotationmatrices, scalematrices, 1] // 
    Flatten) // Union );

If you just want the dot of matrices between matrices with the same index in your lists, you should use J.M.'s suggestion of MapThread;

orderedDots1= MapThread[Dot, {rotationmatrices, scalematrices}];

This is equivalent to (but slightly faster than)

orderedDots2= MapIndexed[Dot[#, scalematrices[[#2[[1]]]]] &, rotationmatrices];
$\endgroup$
1
$\begingroup$
Dot @@@ Transpose[{rotationmatrices, scalematrices}]

{{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, 0, -1}, {0, 2, 0}}, {{0, 1, 0}, {-2, 0, 0}, {0, 0, 2}}}

$\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.