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=SparseArray`SparseBlockMatrix[{{i_, i_} -> B}, Dimensions[B]];



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@
      Erow =Flatten@Flatten[{#, ConstantArray[0, Dimensions[B]-{0,1}]}, {2, 1}];
             Erow = RotateRight[Erow];
     &, Range@Last@Dimensions[B]];
   &, B];



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.

  • 1
    $\begingroup$ KronecketProduct B with a 2 by 2 Identity matrix perhaps? In both orders? $\endgroup$ – march Dec 8 '15 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

enter image description here


enter image description here

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 KroneckerProducts 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.

  • $\begingroup$ 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! $\endgroup$ – lalmei Dec 11 '15 at 12:40

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.