Python - Matrix outer product

后端 未结 4 1406
时光取名叫无心
时光取名叫无心 2020-12-14 10:54

Given two matrices

A: m * r
B: n * r

I want to generate another matrix C: m * n, with each entry C_ij being a mat

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 11:50

    Simple Solution with Numpy Array Broadcasting

    Since, you want C_ij = A_i * B_j, this can be achieved simply by numpy broadcasting on element-wise-product of column-vector-A and row-vector-B, as shown below:

    # import numpy as np
    # A = [[1, 2], [3, 4]]
    # B = [[3, 1], [1, 2]]
    A, B = np.array(A), np.array(B)
    C = A.reshape(-1,1) * B.reshape(1,-1)
    # same as: 
    # C = np.einsum('i,j->ij', A.flatten(), B.flatten())
    print(C)
    

    Output:

    array([[ 3,  1,  1,  2],
           [ 6,  2,  2,  4],
           [ 9,  3,  3,  6],
           [12,  4,  4,  8]])
    

    You could then get your desired four sub-matrices by using numpy.dsplit() or numpy.array_split() as follows:

    np.dsplit(C.reshape(2, 2, 4), 2)
    # same as:
    # np.array_split(C.reshape(2,2,4), 2, axis=2)
    

    Output:

    [array([[[ 3,  1],
             [ 6,  2]],
    
            [[ 9,  3],
             [12,  4]]]), 
    array([[[1, 2],
             [2, 4]],
    
            [[3, 6],
             [4, 8]]])]
    

提交回复
热议问题