我是Python的新手。我试图实施Strassen的算法。在我的实现中,矩阵的大小总是2的幂。那么,我如何将矩阵划分为4个相等大小的象限?感谢
答案 0 :(得分:2)
>>> xs = np.arange(16)
>>> xs
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
>>> xs.reshape(4, 4)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
>>> xs = xs.reshape(4, 4)
>>> a, b, c, d = xs[:2, :2], xs[2:, :2], xs[:2, 2:], xs[2:, 2:]
>>> print(a, b, c, d, sep='\n')
[[0 1]
[4 5]]
[[ 8 9]
[12 13]]
[[2 3]
[6 7]]
[[10 11]
[14 15]]
用len(xs) // 2
替换2。