# How can I realize this matrix product (dot product of matrices of different orders)?

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)?

• 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]]. Jun 30 at 8:41
• @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. Jun 30 at 8:50
• @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. Jun 30 at 8:52
• Your question about the "opposite operation" should perhaps be a separate question altogether. Jun 30 at 13:43

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


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


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

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


• 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. Jun 30 at 10:07

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!

• 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. Jun 30 at 14:12
• @J.M. thanks! $\phantom{}$ Jun 30 at 14:15
• @J.M. only once - this way? KroneckerProduct[A, IdentityMatrix[l/Length[A]]] . KroneckerProduct[B, IdentityMatrix[l/Length[B]]] /. l -> LCM[Length[A], Length[B]] Jun 30 at 14:33
• 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 *)] Jun 30 at 14:46
• @J.M., thanks! But this would be a bit longer. Jun 30 at 14:50

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)$$