3
$\begingroup$

I have a program and I need to take the Dot product of many matrices, which is quite effectively working for small NStep. But for large(=30000), it is failing and saying. Either I use ParallelTable or not, problem remains.

$IterationLimit::itlim: Iteration limit of 4096 exceeded.

This is my program,

a = 1;
tI = 0.0001;
dt = 0.0001;
NStep=30000;
T1[t_] = j1 (Cos[t]); 

T2[t_] = j2 ;

cond = {j1 -> 0.9, j2 -> 1.};
HSm[t_] = ({{0, -(T1[t] + T2[t] Cos[k])}, {-(T1[t] + T2[t] Cos[k]),0}})//. cond;
HStDig[t_] = MatrixExp[t *DiagonalMatrix[Eigenvalues[HSm[t]]]]


us = ParallelTable[HStDig[tI + j dt], {j, 0, NStep}];
us1 = Apply[Dot, us];

How to go about it? Is there a way to speed up the program?

$\endgroup$
4
  • 3
    $\begingroup$ Isn't the MatrixExp of a diagonal matrix just the diagonal matrix made up of the elements of that matrix exponentiated? You might save some time putting that in explicitly rather than using MatrixExp. $\endgroup$
    – march
    Commented Apr 6, 2017 at 17:57
  • $\begingroup$ @march great idea. Thanks $\endgroup$
    – L.K.
    Commented Apr 6, 2017 at 17:59
  • $\begingroup$ I think the big issue is that you're trying to multiply 30000 expressions rather than 3000 numbers (since k is left as a symbol). This is going to be tough for Mathematica's symbolic processing. I would recommend choosing a value for k, and then doing the multiplication. Finally, since matrix multiplication of diagonal matrices is the same as element-wise multiplication of the matrices, do Times@@us rather than Dot@@us. If you insist on using Dot, you can do Block[{$IterationLimit = 30002}, Dot @@ us], but it will take longer than the Times method. $\endgroup$
    – march
    Commented Apr 6, 2017 at 18:06
  • $\begingroup$ Once again thanks @march. Let me go through it meticulously. k is supposed to be given value from a list of numbers $\endgroup$
    – L.K.
    Commented Apr 6, 2017 at 18:17

1 Answer 1

4
$\begingroup$

To answer the specific question, I think that instead of Apply[Dot, l], Fold[Dot, l] will not run into any problems. Of course, all the other comments are valid.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.