假设 A A A是一个 n × n n\times n n×n的方阵,通过利用python实现了 A n A^{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))