假设AAA是一个n×nn\times nn×n的方阵,通过利用python实现了AnA^{n}An的结果。其代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
def matrixPow(Matrix,n):
if(type(Matrix)==list):
Matrix=np.array(Matrix)
if(n==1):
return Matrix
else:
return np.matmul(Matrix,matrixPow(Matrix,n-1))
print(matrixPow([[5,2,32],[1,2,2],[1,2,3]],5))