# I am generating n number of 3*3 matrices and taking product of it over large n. How to reduce time of matrix multiplication

I am written a code in which there is large no.of matrix are multiplying I want to reduce it time. Ux[1,1]=F[1,1] and Ux[1,2]=F[1,1].F[1,2] similarly Ux[1,3]=F[1,1].F[1,2].F[1,3]. I have to do it over 20 j values.

 Ax[j_] := Part[RandomReal[{0, 2 \[Pi]}, 20], j];
Bx[j_] := Part[RandomReal[{0, 2 \[Pi]}, 20], j];
Cx[j_] := Part[RandomReal[{0, 2 \[Pi]}, 20], j];
Dx[j_] := Part[RandomReal[{0, 2 \[Pi]}, 20], j];
X[j_] := Flatten[{Ax[j], Bx[j]}];
Y[j_] := Flatten[{Cx[j], Dx[j]}];
n = 100; nr = 4; g = 1;
\[Theta]1 = N@\[Pi]/4; \[Theta]2 = N@\[Pi]/2; rmax =
N[2 \[Pi]]; T = 1; w0 = 10;
With[{Kx1 = 05},
fxy[{\[Theta]_, r_}] := {Mod[\[Theta] + r - Kx1 Sin[\[Theta]],
2 \[Pi]], Mod[r - Kx1 Sin[\[Theta]], 2 \[Pi]]}]
KY[j_] :=
Transpose[
Catenate[
Table[Join[NestList[fxy, X[j], n], NestList[fxy, Y[j], n]], {i, 1,
nr}]]];
TYx[j_] := MapThread[{#1, #2} &, KY[j]];
XX1[j_] := MapThread[(Sqrt[(10 #2)] Cos[#1]) &, KY[j]];
KX[j_, ix_] := Quiet[Part[KY[j], ix]];
SZ = {{2, 0, 0}, {0, 0, 0}, {0, 0, -2}};
SX = {{0, Sqrt[2], 0}, {Sqrt[2], 0, Sqrt[2]}, {0, Sqrt[2], 0}};
H[j_] := MapThread[1/2 w0 SZ + g Sqrt[10 #2] Cos[#1] SX &,
KY[j]];
HF[j_, ix_] := Quiet[Part[H[j], ix]];
F[j_, ix_] := MatrixExp[-I HF[j, ix]];
Ux[b_, c_] :=
Table[Apply[Dot, Table[F[j, ix], {ix, 1, c}]], {j, 1, b}];
psi0 = {{1}, {0}, {0}};
psit[b_, c_] := Ux[b, c][[b]].psi0;
SZ = {{2, 0, 0}, {0, 0, 0}, {0, 0, -2}};
SX = {{0, Sqrt[2], 0}, {Sqrt[2], 0, Sqrt[2]}, {0, Sqrt[2], 0}};
Sxz[b_, c_] := ConjugateTranspose[psit[b, c]].SZ.psit[b, c];
SXX[b_, c_] := ConjugateTranspose[psit[b, c]].SX.psit[b, c];

data11c = Total[Table[Table[Sxz[b, c], {c, 1, 100}], {b, 1, 20}]]/20

• ˋDot@@listOfMatricesˋ. Feb 7, 2022 at 6:35
• Where I have to use it ? For Ux you are saying b is for different initial condition of ix is over which I have to multiply the matrices. Feb 7, 2022 at 6:45
• I have not tried to understand your code (and I won't try). All I am saying is that you could form the list (e.g., by using Table) of matrices that you want to multiply and then Apply Dot to it. Feb 7, 2022 at 7:43
• Btw., how is Ax[j_] := Part[RandomReal[{0, 2 \[Pi]}, 20], j]; supposed to make sense? The random number will be regenated each time you call Ax. Feb 7, 2022 at 7:45
• You could try to utilize ParallelTable instead of Table . Feb 7, 2022 at 7:47