How to implement a fast nxn integer matrix exponentiation algorithm M^n
The matrices are not diagonalizable and not symetric.
I have implemented the algo Exponentiation by Squaring but I know there are faster algos.
I don't want to use a numerical library.
The eigenvectors/values answer is usable for all symetric matrices but not for all matrices, I need a general algo. So my question need other answers than the previous one you pointed out and mark my question as duplicate.
Here is my code for Expo by Squaring but I need a faster algo: ww is the size if the matrix
void expo(int ww, ll n)
{
int i,j,k;
memset(ret,0,SZ);
for(i=0;i<ww;i++)ret[i][i]=1;
while(n){
if(n&1){
memset(tmp,0,SZ);
for(i=0;i<ww;i++)
for(j=0;j<ww;j++)
for(k=0;k<ww;k++)
tmp[i][j]+=ret[i][k]*mat[k][j];
memcpy(ret,tmp,SZ);
}
memset(tmp,0,SZ);
for(i=0;i<ww;i++)
for(j=0;j<ww;j++)
for(k=0;k<ww;k++)
tmp[i][j]+=mat[i][k]*mat[k][j];
memcpy(mat,tmp,SZ);
n>>=1;
}
}