# Decrease computation time of module [closed]

I need to compute the exponential of a matrix (in this caseR) by the summation method where R is a square matrix with symbolic entries and n is the number of summation terms. I've written a function, which works as intended, but is quite slow - however faster than MatrixExp[]. Is there a way to increase the computational efficiency (decrease computational time) of the below function? Thanks in advance!

Qfun[n0_, R0_] :=
Module[{n = n0, R = R0},
temp = IdentityMatrix[Length[R]];
sumR = IdentityMatrix[Length[R]];

For[k = 1, k <= n, k++,
temp = temp.R;
sumR = sumR + ((t^k)/Factorial[k]) temp;
];
]

• Your Module is quite a mess. It would be helpful if you can add a simple example to your question, which shows how you use your function and that it is slower than MatrixExp. Additionally, are there any restrictions to the parameters? For instance seems n0 > Length[R0] useless, right? – halirutan Mar 16 '15 at 4:37
• If you ask, why your Module is a mess: you copy the input parameter n0 and R0 although you don't have to because you never assign them any new values. On the other hand, you don't localise your local variables temp, sumR, k and t and use global variables instead. They should go into the Module. – halirutan Mar 16 '15 at 4:40
• I don't believe I understand, can you explain how local variables will decreased computation speed? How can I make my Module less messy? – gKirkland Mar 16 '15 at 18:33
• T is undefined. – Sjoerd C. de Vries Mar 17 '15 at 7:22