I am trying to implement a Matrix.pow(int exp)
method for exp >= 0
.
My current implementation repeatedly calls a Matrix.mul(Matrix m)
method, which works well for small exponent.
For large exponents, however, the solution becomes skewed. The Matrix class is using doubles internally and I think that the repeated calls end up in a loss of precision.
I was looking at http://en.wikipedia.org/wiki/Exponentiation_by_squaring, which will certainly help, but I think it will still be an issue for larger exponents.
Is there a better way to exponentiate a Matrix?