efficiency – Fast computing of a matrix power for large integer values in C++

I’m working with squared matrices that can be quite large, for instance, a M = 50 x 50 matrix.

My objective is to compute the power of the squared matrix M^t for very large t values (for example t = 4000).

I work in R and I have used the R function matrix.power from the matrixcalc R package.

I’m exploring the possibility to write a code for matrix power in C++ and then import it in R through the R package Rcpp.

One alternative would be to use the matrix multiplication approach (as in the matrix.power function in R), but looking around I have understood that there might be faster approaches to calculate a matrix power.

Do you have any experience on that? Does anyone know a library in C++ that does matrix power calculation fast and efficiently?

Consider that I’m working on a Mac0s laptop with 16 GB of RAM and 4 CPUs.