1
$\begingroup$

The matrix multiplication of square matrices of different order is often claimed to be impossible. Yet, if the order of one matrix is divisible by the order of the other, a natural multiplication rule is visible. The bigger matrix simply should be considered a "matrix of matrices" or, alternatively, in small matrix all elements should be replaced with equivalent diagonal $m\times m$ (in this case, $2\times2$) square matrices:

$\left( \begin{array}{cc} a & b \\ c & d \\ \end{array} \right).\left( \begin{array}{cc} \left( \begin{array}{cc} {a_1} & {b_1} \\ {c_1} & {d_1} \\ \end{array} \right) & \left( \begin{array}{cc} {a_2} & {b_2} \\ {c_2} & {d_2} \\ \end{array} \right) \\ \left( \begin{array}{cc} {a_3} & {b_3} \\ {c_3} & {d_3} \\ \end{array} \right) & \left( \begin{array}{cc} {a_4} & {b_4} \\ {c_4} & {d_4} \\ \end{array} \right) \\ \end{array} \right)=\left( \begin{array}{cccc} a & 0 & b & 0 \\ 0 & a & 0 & b \\ c & 0 & d & 0 \\ 0 & c & 0 & d \\ \end{array} \right).\left( \begin{array}{cccc} {a_1} & {b_1} & {a_2} & {b_2} \\ {c_1} & {d_1} & {c_2} & {d_2} \\ {a_3} & {b_3} & {a_4} & {b_4} \\ {c_3} & {d_3} & {c_4} & {d_4} \\ \end{array} \right)$ $=\left( \begin{array}{cc} \left( \begin{array}{cc} a {a_1}+{a_3} b & a {b_1}+b {b_3} \\ a {c_1}+b {c_3} & a {d_1}+b {d_3} \\ \end{array} \right) & \left( \begin{array}{cc} a {a_2}+{a_4} b & a {b_2}+b {b_4} \\ a {c_2}+b {c_4} & a {d_2}+b {d_4} \\ \end{array} \right) \\ \left( \begin{array}{cc} {a_1} c+{a_3} d & {b_1} c+{b_3} d \\ c {c_1}+{c_3} d & c {d_1}+d {d_3} \\ \end{array} \right) & \left( \begin{array}{cc} {a_2} c+{a_4} d & {b_2} c+{b_4} d \\ c {c_2}+{c_4} d & c {d_2}+d {d_4} \\ \end{array} \right) \\ \end{array} \right)$

So, in certain circumstances this is possible. People on Math.Stackexchange commented that this is essentially tensor product, but TensorProduct in Mathematica gives something different.

So, can such product be somehow realized in Mathematica? Maybe I shoud use TensorProduct in some combination with other transformation?

Also, is there a way to "expand" the matrix increasing its order this way: $\left( \begin{array}{cc} a & b \\ c & d \\ \end{array} \right)\to \left( \begin{array}{cccc} a & 0 & b & 0 \\ 0 & a & 0 & b \\ c & 0 & d & 0 \\ 0 & c & 0 & d \\ \end{array} \right)$

That is to an equivalent matrix of higher order (replace all elements with equivalent matrices)?

$\endgroup$
7
  • 1
    $\begingroup$ It looks to me you just want to take the Kronecker product of the smaller matrix with an appropriately-sized identity matrix, e.g. KroneckerProduct[{{a, b}, {c, d}}, IdentityMatrix[2]]. $\endgroup$ Jun 30 at 8:41
  • $\begingroup$ @J.M. well, this looks like a first step, yes. I just wanted to ask a separate question on how to expand a matrix this way. $\endgroup$
    – Anixx
    Jun 30 at 8:50
  • $\begingroup$ @J.M. I wonder whethwer there is an opposite operation that would shrink the matrix on this pattern? And, also, optionally shrink a matrix of the pattern $\left( \begin{array}{cc} a & b \\ -b & a \\ \end{array} \right)$ to a complex number. $\endgroup$
    – Anixx
    Jun 30 at 8:52
  • $\begingroup$ @J.M. I made a complete solution to the question based on your advice. See my answer, thanks! $\endgroup$
    – Anixx
    Jun 30 at 10:15
  • $\begingroup$ Your question about the "opposite operation" should perhaps be a separate question altogether. $\endgroup$ Jun 30 at 13:43

3 Answers 3

0
$\begingroup$

Here is a solution using ReplaceAll:

We first create a matrix of matrices and flatten this:

m1 = {{a, b}, {c, d}};
m2 = m1 /. {x_?(MemberQ[{a, b, c, d}, #] &) :> {{x, 0}, {0, 
      x}}}; m2 // MatrixForm
(m3 = ArrayFlatten[m2]) // MatrixForm

enter image description here

Then we do the same using indexed matrix elements:

fun[j_] = 
  m1 /. {x_?(MemberQ[{a, b, c, d}, #] &) :> (Subscript[x, j] )};
(m4 = {{fun[1], fun[2]}, {fun[3], fun[4]}}) // MatrixForm
(m5 = ArrayFlatten[m4]) // MatrixForm

enter image description here

Finally we multiply the matrices and partition them into the required form:

(m6 = m3 . m5) // MatrixForm
(m7 = Partition[m6, {2, 2}]) // MatrixForm

enter image description here

$\endgroup$
1
  • $\begingroup$ KronekerProduct (as @J.M. advised) makes the "matrix of matrices" easier. See my answer to this question, it gives a complete solution for this kind of multiplication for any orders (yours works only for one order combination). For instance, one can multiply $2\times2$ and $3\times3$ matrices. $\endgroup$
    – Anixx
    Jun 30 at 10:07
0
$\begingroup$

So, with the hint of @J.M.'sslightlylessbusy I made the code:

DotG[A_, B_] := 
 KroneckerProduct[A, 
   IdentityMatrix[LCM[Length[A], Length[B]]/Length[A]]] . 
  KroneckerProduct[B, 
   IdentityMatrix[LCM[Length[A], Length[B]]/Length[B]]]

This can multiply square matrices of any orders!

$\endgroup$
9
  • $\begingroup$ Since your matrices are assumed square, 1. you can use SquareMatrixQ[] as an argument check; and 2. you can use Length[] to get the dimension. You might also consider computing the LCM[] only once. $\endgroup$ Jun 30 at 14:12
  • $\begingroup$ @J.M. thanks! $\phantom{}$ $\endgroup$
    – Anixx
    Jun 30 at 14:15
  • $\begingroup$ @J.M. only once - this way? KroneckerProduct[A, IdentityMatrix[l/Length[A]]] . KroneckerProduct[B, IdentityMatrix[l/Length[B]]] /. l -> LCM[Length[A], Length[B]] $\endgroup$
    – Anixx
    Jun 30 at 14:33
  • $\begingroup$ Well, that's what Module[] is useful for: DotG[A_?SquareMatrixQ, B_?SquareMatrixQ] := Module[{na = Length[A], nb = Length[A], lc}, l = LCM[na, nb]; (* stuff *)] $\endgroup$ Jun 30 at 14:46
  • $\begingroup$ @J.M., thanks! But this would be a bit longer. $\endgroup$
    – Anixx
    Jun 30 at 14:50
0
$\begingroup$

A simple matrix product seems to do the trick.

A = {{a, b},
     {c, d}};
B = {{{{a1, b1}, {c1, d1}}, {{a2, b2}, {c2, d2}}},
     {{{a3, b3}, {c3, d3}}, {{a4, b4}, {c4, d4}}}};

In the front end these matrices look like you want:

A // MatrixForm

$$ \left( \begin{array}{cc} a & b \\ c & d \\ \end{array} \right) $$

B // MatrixForm

$$ \left( \begin{array}{cc} \left( \begin{array}{cc} a_1 & b_1 \\ c_1 & d_1 \\ \end{array} \right) & \left( \begin{array}{cc} a_2 & b_2 \\ c_2 & d_2 \\ \end{array} \right) \\ \left( \begin{array}{cc} a_3 & b_3 \\ c_3 & d_3 \\ \end{array} \right) & \left( \begin{array}{cc} a_4 & b_4 \\ c_4 & d_4 \\ \end{array} \right) \\ \end{array} \right) $$

Just matrix-multiplying them with Dot does what you need:

A . B // MatrixForm

$$ \left( \begin{array}{cc} \left( \begin{array}{cc} a a_1+a_3 b & a b_1+b b_3 \\ a c_1+b c_3 & a d_1+b d_3 \\ \end{array} \right) & \left( \begin{array}{cc} a a_2+a_4 b & a b_2+b b_4 \\ a c_2+b c_4 & a d_2+b d_4 \\ \end{array} \right) \\ \left( \begin{array}{cc} a_1 c+a_3 d & b_1 c+b_3 d \\ c c_1+c_3 d & c d_1+d d_3 \\ \end{array} \right) & \left( \begin{array}{cc} a_2 c+a_4 d & b_2 c+b_4 d \\ c c_2+c_4 d & c d_2+d d_4 \\ \end{array} \right) \\ \end{array} \right) $$

$\endgroup$
6
  • $\begingroup$ This question is already answered, see my own answer. $\endgroup$
    – Anixx
    Jul 30 at 17:49
  • $\begingroup$ You're welcome! $\endgroup$
    – Roman
    Jul 30 at 17:50
  • $\begingroup$ If simple matrix product did the trick, there would be no need for the question. $\endgroup$
    – Anixx
    Jul 30 at 17:50
  • $\begingroup$ Multiply an order 2 matrix (2 x 2) and order 3 matrix (3 x 3) using your method. $\endgroup$
    – Anixx
    Jul 30 at 17:52
  • $\begingroup$ Maybe give some more examples? The one example you gave is calculated with a simple dot product, and I don't know what you mean by multiplying 2x2 with 3x3. $\endgroup$
    – Roman
    Jul 30 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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