5
$\begingroup$

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

$\endgroup$
  • 2
    $\begingroup$ Apply[Dot,Table[matrixFunctionOfj,{j,n}]] should do this. $\endgroup$ – Daniel Lichtblau Oct 26 '14 at 22:38
  • $\begingroup$ 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. $\endgroup$ – Jens Oct 27 '14 at 0:09
5
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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 *. $\endgroup$ – Anne O'Nyme Oct 25 '14 at 19:03
  • 2
    $\begingroup$ Sorry, Anne. But see the EDIT in my answer. I hope it covers now what you want. $\endgroup$ – Dr. Wolfgang Hintze Oct 25 '14 at 20:02
5
$\begingroup$

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}}
$\endgroup$
1
$\begingroup$
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];
$\endgroup$

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.