I'm new to Python. I'm trying to implement Strassen's Algorithm. The size of the matrix will always be a power of 2 in my implementation. So, how do I divide the matrix into 4 equal sized quadrants? Thanks
Asked
Active
Viewed 5,658 times
1 Answers
3
>>> 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]]
replace 2, with len(xs) // 2
.

behzad.nouri
- 74,723
- 18
- 126
- 124
-
Thanks for your answer. Can you please explain the ":" notation when computing a, b, c, d? Should it be before or after the value(2 in the above example)? – so908 Sep 16 '14 at 02:02