# A Product function for matrix products

I would like to use an equivalent of the product-function in mathematica, but where instead of multiplying numbers I multiply matrices?

• Apply[Dot,Table[matrixFunctionOfj,{j,n}]] should do this. – Daniel Lichtblau Oct 26 '14 at 22:38
• Sometimes defining a new function for a simple task can make the code less readable. In this instance, I think that is the case because the syntax as shown by @DanielLichtblau is concise and sufficiently expressive to be self-explanatory. – Jens Oct 27 '14 at 0:09

Matrix multiplication is built in in Mathematica. Just use the dot for multiplication.

Here are two 2x2 matrices

a = PauliMatrix[1]
b = PauliMatrix[3]

(* Out[49]= {{0, 1}, {1, 0}} *)
(* Out[50]= {{1, 0}, {0, -1}} *)


Here's a product

a.b

(* Out[53]= {{0, -1}, {1, 0}} *)


and here is the product of the same factors in reverse order

b.a

(* Out[52]= {{0, 1}, {-1, 0}} *)


There are also two interesting functions of matrices

MatrixPower[a, 2]

(* Out[55]= {{1, 0}, {0, 1}} *)

MatrixExp[b]

(* Out[56]= {{E, 0}, {0, 1/E}} *)


Hope this helps,
Wolfgang

EDIT in response to the comment

I'm not aware of a generalization of Product[] to matrices in Mathematica. But you could easily define it yourself

matrixProduct[listOfMatrices_] := Dot @@ listOfMatrices


Then

m = Table[PauliMatrix[i], {i, 1, 3}];

matrixProduct[m]

(* Out[31]= {{I, 0}, {0, I}} *)


Regards,
Wolfgang

• It is not exactly what I asked: I would like to make an iterated product but with matrices. E.g. Product[PauliMatrix[i],{i,1,3}], where the multiplication would be the dot . instead of the star *. – Anne O'Nyme Oct 25 '14 at 19:03
• Sorry, Anne. But see the EDIT in my answer. I hope it covers now what you want. – Dr. Wolfgang Hintze Oct 25 '14 at 20:02

Another method is to use Array, the fourth parameter of which sets the function that combines expressions:

m = RandomReal[9, {3, 3, 3}];

Array[m[[#]] &, 3, 1, Dot]

{{606.041, 638.877, 525.972},
{1011.5, 1068.12, 856.671},
{532.56, 556.236, 435.836}}


Equivalent to:

Dot @@ m

{{606.041, 638.877, 525.972},
{1011.5, 1068.12, 856.671},
{532.56, 556.236, 435.836}}

productF = Block[{Times = Dot}, Product[#, #2]] &;


Examples:

m = Table[PauliMatrix[i], {i, 1, 3}];

productF[j, {j, m}]
(* {{I, 0},{0, I}} *)

productF[PauliMatrix[j], {j, 3}]
(* {{I, 0},{0, I}} *)


For better emulation of Product one may include the HoldAll attribute and accept additional Product iterators with:

productF2 = Function[, Block[{Times = Dot}, Product @ ##], HoldAll];