# Matrix Multiplication after "Flatten"

Say we originally had two matrices, $A$ and $B$, both $n \times n$, whose product

$$C= A.B$$

Now I flatten $A$. I can obtain a flatten $C$, from the following multiplication

$$C_{flat}=A_{flat}.D$$

The question here is how to build a $D$ from the original B, for both products; $$C_1=A.B \qquad \qquad C_2=B.A$$ $$C_{1,flat}=A_{flat}.D \qquad \qquad C_{2,flat}=E.A_{flat}$$

For the first case, this easily done since the B is just blocks in D, and there are quite a few answers about building block matrices in this site.

For example, let's take a specific case, a set of 2x2 matrices

A={{a11, a12}, {a21, a22}} ={{1,1},{1,0}}
B={{b11, b12}, {b21, b22}} ={{0,1},{0,1}}


In the first case, D is now a 4x4 matrix and has blocks of $B$'s

$$\left( \begin{array}{cccc} a11 & a12 & a2 1& a22 \end{array} \right) \cdot\left( \begin{array}{cccc} b11 & b12 & 0 & 0 \\ b21 & b22 & 0 & 0 \\ 0 & 0 & b11 & b12 \\ 0 & 0 & b21 & b22 \\ \end{array} \right)$$

D can be built using previous answers with

D=SparseArraySparseBlockMatrix[{{i_, i_} -> B}, Dimensions[B]];

Flatten[A.B]===Flatten[A].D


True

The second case is also straightforward to write down explicitly $$\left( \begin{array}{cccc} b11 &0 & b12 & 0 \\ 0 & b11 & 0 & b12 \\ b21 & 0 & b22 & 0 \\ 0 & b21 & 0 & b22 \\ \end{array} \right) \cdot \left( \begin{array}{cccc} a11 \\ a12 \\ a21 \\ a22 \end{array} \right)$$

my implementation though seems like monstrosity when compared to the previous case, I simply resorted to building it row by row with essentially a riffle of zeros and rotating till I reached the next row in the original B.

E= Last@Last@Reap@
Scan[
Erow =Flatten@Flatten[{#, ConstantArray[0, Dimensions[B]-{0,1}]}, {2, 1}];
Scan[
Sow[Erow];
Erow = RotateRight[Erow];
&, Range@Last@Dimensions[B]];
&, B];

Flatten[B.A]===E.Flatten[A]


True

My main question is: Is there are any way to simplify this last case ?

Maybe both of these problems have a simpler solution than what I am doing.

A second more ambiguous question is how do these results generalize for higher tensors multiplication ? any insight into that would be great.

• KronecketProduct B with a 2 by 2 Identity matrix perhaps? In both orders? Commented Dec 8, 2015 at 15:25

Here is a nice way to do things:

Note that

KroneckerProduct[IdentityMatrix[2], B] // MatrixForm
KroneckerProduct[B, IdentityMatrix[2]] // MatrixForm


respectively yield

and

Therefore, if you evaluate

Flatten[A.B] - Flatten[A].KroneckerProduct[IdentityMatrix[2], B]
Flatten[B.A] - KroneckerProduct[B, IdentityMatrix[2]].Flatten[A]


you get {0, 0, 0, 0}.

This method of using KroneckerProduct`s of the matrices with identity matrices should generalize to tensors with arbitrary numbers of indices, although I always need specific examples to re-figure that stuff out.

• I was expecting something like this, but what I wasn't expecting is that this answer is still faster for larger and larger vectors/matrices, in both cases! Commented Dec 11, 2015 at 12:40