2
$\begingroup$

I am trying to do some matrix multiplication in Mathematica but I just cannot figure out the correct syntax for my problem.

I want to write the following: $$ A\pmatrix{a & b\\c & d}+B\pmatrix{e & f\\g & h}=\pmatrix{Aa+Be & Ab+Bf\\Ac+Bg & Ad+Bh} $$ as $$ \pmatrix{\pmatrix{a & b\\c & d}\\\pmatrix{e & f\\g & h}}\pmatrix{A\\B}=\pmatrix{Aa+Be & Ab+Bf\\Ac+Bg & Ad+Bh} $$

So far I found out that this generates

{{{{a, b}, {c, d}}}, {{{g, h}, {i, j}}}};

MatrixForm[%]

the first tensor. But when I multiply this by {e,f} the result is incorrect. This

{{{{a, b}, {c, d}}}, {{{e, f}, {g, h}}}}.{A, B};

MatrixForm[%]

gives me this as a result: $$ \pmatrix{\pmatrix{Aa & Bb\\Ac & Bd}\\\pmatrix{Ae & Bf\\Ag & Bh}} $$

I think that my problem has an easy fix... probably just some brackets that need to be added, but I just didn't manage to figure it out.

Thanks

Philipp

$\endgroup$
2
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 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$
    – Michael E2
    Nov 17, 2015 at 0:27
  • 1
    $\begingroup$ You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. You may also find this this meta Q&A helpful $\endgroup$
    – Michael E2
    Nov 17, 2015 at 0:28

2 Answers 2

4
$\begingroup$

You almost had it, with just two things going wrong. First, you had a higher-rank tensor than you wanted, which can be solved by peeling off a layer of curly braces, and the two $ 2 \times 2 $ matrices are effectively transposed, suggesting we should reverse the order of multiplication. You want:

{A, B}.{{{a, b}, {c, d}}, {{e, f}, {g, h}}} 

which gives

$$ \left( \begin{array}{cc} a A+B e & A b+B f \\ A c+B g & A d+B h \\ \end{array} \right) $$

as desired.

$\endgroup$
3
$\begingroup$

You definitely want to look up the functions Inner and Outer. Outer gives you a generalized outer product, and it is extremely useful. Inner gives you a generalized inner product. See this question and the answers below it for how to think about these functions.

It can be tricky to use and figure out the syntax, so here's a start. For your problem, let's define the matrices and vector as

mat1 = Array[a, {2, 2}];
mat2 = Array[b, {2, 2}];
vec = {A, B};

Then, the direct translation of what you're doing is

(combo1 = A Array[a, {2, 2}] + B Array[b, {2, 2}]) // MatrixForm

resulting in

enter image description here

Using Inner:

combo2 = Inner[Times, {Array[a, {2, 2}], Array[b, {2, 2}]}, {A, B}, Plus, 1];

You can verify that they are the same:

combo1 === combo2
(* True *)

Note that the last (optional) argument to Inner is necessary here. From the documentation:

Inner[f, Subscript[list, 1], Subscript[list, 2], g, n] contracts index n of the first tensor with the first index of the second tensor.

I guess in this case, n == 1 is not the default.


If you would like to make a simple function that does this without all of the extra typing, do this:

dot[twoTensor_ /; Dimensions@twoTensor === {2, 2, 2}, oneTensor_ /; Dimensions@oneTensor == {2}] := Inner[Times, twoTensor, oneTensor, Plus, 1]

You can also take direct advantage of Dot using replacement rules:

Clear[aMat, bMat]
Dot[{aMat, bMat}, {A, B}] /. {aMat -> Array[a, {2, 2}], bMat -> Array[b, {2, 2}]}
$\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.