Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How would I best express the following in Mathematica: $\begin{pmatrix}2 & 4\end{pmatrix} \begin{pmatrix}r_1 & r_2\\r_3 & r_4\end{pmatrix} \begin{pmatrix}6 \\ 8\end{pmatrix}$, where $r_i$ are 3-element vectors?

Is there a more elegant way than this?

r1 := {1, 2, 3}
r2 := {3, 2, 1}
r3 := {2, 3, 1}
r4 := {2, 1, 3}
res := {2, 4} . {{r1//Hold, r2//Hold}, {r3//Hold, r4//Hold}} . {{6}, {8}} // ReleaseHold
res[[1]]

Out= {172, 160, 172}
share|improve this question
2  
{2, 4}.Transpose[{{{1, 2, 3}, {3, 2, 1}}, {{2, 3, 1}, {2, 1, 3}}}, {1, 3, 2}].{6, 8} –  J. M. Apr 25 '13 at 15:20
    
@J.M.: Nice, thanks. Shall I delete this question or do you want to add it as the answer? –  Frank Apr 25 '13 at 15:34
    
I can write an answer with some elaboration, if you don't mind a bit of a wait, or unless some other enterprising user beats me to it. –  J. M. Apr 25 '13 at 15:41
    
@J.M. I have something minor to post but I certainly don't intend to "beat you to it." Shall I wait to post? –  Mr.Wizard Apr 25 '13 at 15:43
    
@Mr. Wizard, you know I'm a slow typist. :) Nevertheless, I've long been used to getting scooped by the quicker young-ins... so I was already half-expecting somebody had written an answer in the interim. –  J. M. Apr 25 '13 at 16:36

3 Answers 3

up vote 3 down vote accepted

As it stands, the dimensions of the tensor {{{1, 2, 3}, {3, 2, 1}}, {{2, 3, 1}, {2, 1, 3}}} are compatible with the vector on the left, but not on the right. We thus have

{3, 4}.{{{1, 2, 3}, {3, 2, 1}}, {{2, 3, 1}, {2, 1, 3}}}
   {{11, 18, 13}, {17, 10, 15}}

and we now want to form the product of this with {6, 8} in some way. One could certainly do Transpose[{{11, 18, 13}, {17, 10, 15}}].{6, 8}, but the more appropriate way would be to match the dimensions of the original tensor with not only the left vector, but with the right vector as well.

Thus, one might initially do

Transpose /@ {{{1, 2, 3}, {3, 2, 1}}, {{2, 3, 1}, {2, 1, 3}}}
   {{{1, 3}, {2, 2}, {3, 1}}, {{2, 2}, {3, 1}, {1, 3}}}

and now it is clear one can take left- and right-products of this tensor with $2$-vectors. A better route, though, is to note that the previous operation was exactly equivalent to swapping out levels $2$ and $3$, and thus we have the more compact

Transpose[{{{1, 2, 3}, {3, 2, 1}}, {{2, 3, 1}, {2, 1, 3}}}, {1, 3, 2}]
   {{{1, 3}, {2, 2}, {3, 1}}, {{2, 2}, {3, 1}, {1, 3}}}

With this, we finally have

{2, 4}.Transpose[{{{1, 2, 3}, {3, 2, 1}}, {{2, 3, 1}, {2, 1, 3}}}, {1, 3, 2}].{6, 8}
   {172, 160, 172}
share|improve this answer

This seems simpler than your method with Hold:

Block[{r1, r2, r3, r4},
 {2, 4}.{{r1, r2}, {r3, r4}}.{{6}, {8}}
]
{{172, 160, 172}}

Also see:

Ways to compute inner products of tensors

share|improve this answer

This really is an inner product, and can be written straightforwardly using the Inner command:

Inner[Times, Transpose[Inner[Times,{2, 4},{{r1, r2}, {r3, r4}}, Plus]], {6, 8}, Plus]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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