How to split a matrix into 4 blocks using numpy?

孤街浪徒 提交于 2019-12-18 04:45:07

问题


I'm implementing Strassen's Matrix Multiplication using python. In divide step, we divide a larger matrix into smaller sub-matrices. Is there a built-in numpy function to split a matrix?


回答1:


Not exactly, but using array slicing notation you should be able to do it yourself pretty easily.

>>> A = np.linspace(0,24,25).reshape([5,5,])
>>> A
array([[  0.,   1.,   2.,   3.,   4.],
       [  5.,   6.,   7.,   8.,   9.],
       [ 10.,  11.,  12.,  13.,  14.],
       [ 15.,  16.,  17.,  18.,  19.],
       [ 20.,  21.,  22.,  23.,  24.]])

Make B the top-left 2x2 in A:

>>> B = A[0:2,0:2]

Note that B is a view, it shares data with A

>>> B[1,1] = 60
>>> print A
[[  0.   1.   2.   3.   4.]
 [  5.  60.   7.   8.   9.]
 [ 10.  11.  12.  13.  14.]
 [ 15.  16.  17.  18.  19.]
 [ 20.  21.  22.  23.  24.]]

If you need to copy the data from A, use the copy method:

>>> B = A[0:2,0:2].copy()
>>> B
array([[  0.,   1.],
       [  5.,  60.]])
>>> B[1,1] = 600
>>> B
array([[   0.,    1.],
       [   5.,  600.]])
>>> A
array([[  0.,   1.,   2.,   3.,   4.],
       [  5.,  60.,   7.,   8.,   9.],
       [ 10.,  11.,  12.,  13.,  14.],
       [ 15.,  16.,  17.,  18.,  19.],
       [ 20.,  21.,  22.,  23.,  24.]])



回答2:


I ran into the same problem and found some built-in numpy functions to split my matrix into 4 submatrices (my matrices are of size 2^N*2^N)

Here is the code i wrote:

upper_half = np.hsplit(np.vsplit(my_matrix, 2)[0], 2)
lower_half = np.hsplit(np.vsplit(my_matrix, 2)[1], 2)

upper_left = upper_half[0]
upper_right = upper_half[1]
lower_left = lower_half[0]
lower_right = lower_half[1]

Bonus to recombine them using numpy:

C=np.vstack([np.hstack([c11, c12]), np.hstack([c21, c22])])

vsplit hsplit hstack and vstack seem to be made for that purpose




回答3:


According to this answer, you might use the swapaxes:

You can create a helper method as:

def split(array, nrows, ncols):
    """Split a matrix into sub-matrices."""

    r, h = array.shape
    return (array.reshape(h//nrows, nrows, -1, ncols)
                 .swapaxes(1, 2)
                 .reshape(-1, nrows, ncols))

Here is an example of using it

import numpy as np

array = np.array([
    [1, 1, 2, 2],
    [3, 3, 4, 4],
    [5, 5, 6, 6],
    [7, 7, 8, 8]])

A, B, C, D =  split(array, 2, 2)
# A = 
# [[1 1]
#  [3 3]]

# B = 
# [[2 2]
#  [4 4]]

# C = 
# [[5 5]
#  [7 7]]

# D =
# [[6 6]
#  [8 8]]
print('A = \n{}\n\n'
      'B = \n{}\n\n'
      'C = \n{}\n\n'
      'D =\n{}'.format(A, B, C, D))


来源:https://stackoverflow.com/questions/11105375/how-to-split-a-matrix-into-4-blocks-using-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!