Why is numpy's kron so fast?
问题 I was trying to implement a kronecker product function. Below are three ideas that I have: def kron(arr1, arr2): """columnwise outer product, avoiding relocate elements. """ r1, c1 = arr1.shape r2, c2 = arr2.shape nrows, ncols = r1 * r2, c1 * c2 res = np.empty((nrows, ncols)) for idx1 in range(c1): for idx2 in range(c2): new_c = idx1 * c2 + idx2 temp = np.zeros((r2, r1)) temp_kron = scipy.linalg.blas.dger( alpha=1.0, x=arr2[:, idx2], y=arr1[:, idx1], incx=1, incy=1, a=temp) res[:, new_c] = np