# MatrixPower in 11.0.1

For a square matrix $M$, consider $M^8$.

Does MatrixPower[M, 8] do seven matrix multiplications? Or is it smart enough to compute $((M^2)^2)^2$ that involves only three?

Or does Mathematica do something else?

• The "Details" section of the documentation for MatrixPower states that "MatrixPower[m, n] effectively evaluates the product of a matrix with itself $n$ times." Dec 7, 2016 at 6:51
• @MarcoB I think a key word there is "effectively". It should give the same result, but I doubt that it does that since it wouldn't be the most efficient. I don't know what it does, but what makes sense to me is to switch methods based on whether the matrix is symbolic, exact numeric, inexact numeric or machine precision numeric, as well as based on the matrix size and the exponent. Dec 7, 2016 at 11:11
• If you only want to know which method is faster, just benchmark it. Dec 7, 2016 at 11:13
• This is just a guess, but for machine precision matrices a major concern could be numerical stability and hence a different method might be needed to ensure the answer is even correct. Now whether the 'nested power' method is as numerically stable as say diagonalization, I'm not sure... Dec 9, 2016 at 18:18

Benchmark:

M = RandomReal[10, {10, 10}];
RepeatedTiming[M.M.M.M.M.M.M.M;, 10]
RepeatedTiming[MatrixPower[M, 8];, 10]
RepeatedTiming[temp = M.M;
temp = temp.temp;
temp.temp;, 10]


{7.7*10^-6, Null}

{3.1*10^-6, Null}

{3.60*10^-6, Null}

So it appears some optimization does take place, as the timing is much closer to the $((M^2)^2)^2$ method.

• Repeat Feyre's benchmark with M = Array[x, {5, 5}]; and there's an even more dramatic difference. Dec 7, 2016 at 13:53
• Especially for high powers I could imagine it might be faster to diagonalize first if that's possible Dec 7, 2016 at 14:19
• @JulesLamers The exact case e.g. integer or rational input would be ill suited for diagonalizing since that involves working in an algebraic extension field. For the inexact case this might make sense. For real input there will be fuzzy nonzero imaginary parts and offhand I don't know if they should be left alone or chopped off (probably the latter is my guess). Dec 7, 2016 at 15:57
• @Jules, in addition to Daniel's comments, recall that not all matrices are diagonalizable; one can ostensibly use Schur before powering in the inexact case, tho. In the exact and symbolic cases, one could use Jordan, but then you hit the problem alluded in Daniel's first sentence. Dec 9, 2016 at 17:37
• @J.M. Certainly, hence the "if that's possible" :) Dec 9, 2016 at 19:18