numpy中矩阵元素使用分数表示

In [1]: from sympy import Rational

In [2]: a = Rational('1/3')

In [3]: p = np.array([[0,1,0,0,0],[a,a,a,0,0],[0,a,a,a,0],[0,0,a,a,a],[0,0,0,1,0]])

In [4]: p
Out[4]:
array([[0, 1, 0, 0, 0],
       [1/3, 1/3, 1/3, 0, 0],
       [0, 1/3, 1/3, 1/3, 0],
       [0, 0, 1/3, 1/3, 1/3],
       [0, 0, 0, 1, 0]], dtype=object)


# n个矩阵进行点乘
In [70]: def matric_dots(a,n):
             i = 1
             c = a
             while i<n:
                 i += 1
                 c = c.dot(a)
             return c

In [75]: matric_dots(p,3) == p.dot(p).dot(p)
Out[75]:
array([[ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True]])