arrays - A strange dot product in Python -
so have 2 matrices, 1 i×h , other i×i, h = m*i. take dot product of first m rows of first matrix first row of second, next m rows next row of second, etc.
does know easy way in numpy? i'm trying avoid loop.
import numpy np # examples = 5 m = 3 h = m * first = np.arange(h * i).reshape(h, i) # note dimension h×i, not i×h second = np.arange(i * i).reshape(i, i) # let's compute dot product of # every column of `first` # every column of `second` (i.e. every row of `second` transposed): # full_matrix_product = np.dot(first, second.transpose()) # no (explicit) loops, # more # multiplications # need in end. # extract specific dot products want: wanted_rows = np.arange(h) wanted_columns = np.arange(m).repeat(i) result = full_matrix_product[wanted_rows, wanted_columns].reshape(m, i)
Comments
Post a Comment