如何在numpy数组的笛卡尔积上计算函数?

假设

a = np.random.rand(np.random.randint(10), 4)
b = np.random.rand(np.random.randint(10), 4)

我想向量化以下内容:

from itertools import product
res = np.array([foo(a[i, :], b) for i in range(a.shape[0])])

foo can take a vector and a matrix and yield a correct result.

我想避免列表理解,而只剩下纯粹的numpy

I would like a way to trace back which i,j gave each result.

Is this doable in a general way, or does it require the specific calculation of foo?

如果是这样,如何?