# A lot of matrix multiplication

So I have a set of 15 $4\times 4$ matrices which I call $X_i, (i=1,2..15)$ and a set of 6 $4\times 4$ matrices which I call $y_j, (j=1,2...6)$. Now I have to calculate $(X_i y_j)-(y_j X_i^*)$ for different values of $i$ and $j$, where $*$ stands for Complex conjugate. Is there a simple command (with perhaps loops) in mathematica using which I can calculate all the 90 results instead of multiplying these matrices individually 90 times?

• Have a look at Outer. – b.gates.you.know.what Dec 3 '12 at 8:29
• Try Outer[#1.#2 - #2.Conjugate[#1] &, X, y] – E.O. Dec 3 '12 at 9:08

Define some matrices.

Xs = RandomInteger[{1, 4}, {2, 4, 4}];
ys = RandomInteger[{1, 4}, {3, 4, 4}];


Use Outer applied to the indices to produce your derived matrices:

Flatten[
Outer[#1.#2 - #2.Conjugate@#1 &, Xs, ys]
, 1]


Flatten provides your result matrices in a single level list.

Define the number of Xs and ys:

numXs = 2;
numys = 3;

Flatten[Outer[
Subscript[X, #1].Subscript[y, #2] -
Subscript[y, #2].Conjugate@Subscript[X, #1] &, Range@numXs,
Range@numys], 1]


X is made of sixteen 4by4 matrix. X[[i]]] is your Xi.

X = Table[RandomReal[1, {4, 4}], {i, 1, 16}];


Y is made of six 4by4 matrix. Y[[j]]] is your Yi.

Y = Table[RandomReal[1, {4, 4}], {i, 1, 6}];


I define a function for the multiplication you want:

fun[i_, j_] := X[[i]].Y[[j]] - Y[[j]].X[[i]]\[Conjugate]


Now I make a table out of this function. Each i and j gives one of the 16*4 multiplications.

result = Table[fun[i, j], {i, 1, 16}, {j, 1, 4}];
result[[13, 2]] // Dimensions
{4, 4}


Each element of result as you see is a 4by4 matrix.