I'm currently trying to compute matrix exponentiation, and for that I use the well known algorithm of exponentiation by squaring.
def mat_mul(a, b):
n = len(a)
c = []
for i in range(n):
c.append([0]*n)
for j in range(n):
for k in range(n) :
c[i][j] += (a[i][k]*b[k][j])
return c
def mat_pow(a, n):
if n<=0:
return None
if n==1:
return a
if n==2:
return mat_mul(a, a)
t1 = mat_pow(a, n//2)
if n%2 == 0:
return mat_mul(t1, t1)
return mat_mul(t1, mat_mul(a, t1))
The problem is that my algorithm is still too slow, and after some research, I found out that it was because contrary to what I thought, the time of matrix multiplication depends on the matrix size and on the numbers in the matrix.
In fact, the numbers in my matrix become very large, so after some time, the multiplication becomes way slower. Typically, I have M, a 13*13 matrix filled with random 1 and 0, and I want to compute M(108). The integers in the matrix can have hundreds of digits. I'd like to know if there is a way to avoid that problem.
I've seen that I could use matrix diagonalization, but the problem is I can't use external libraries (like numpy). So the diagonalization algorithm seems a bit too complicated.