def products_2(x, func, k=0):
"""Computes func(xi, xj) over all possible indices i and j constrained to j >= i+k.
func is an arbitrary function, and k >= 0 is an integer
"""
x_height, x_width = x.shape
mask = numpy.triu(numpy.ones((x_width, x_width)), k) > 0.5
z1 = x.reshape(x_height, x_width, 1)
z2 = x.reshape(x_height, 1, x_width)
yexp = func(z1, z2) # twice computation, but performance gain due to lack of loops
out = yexp[:, mask]
return out
评论列表
文章目录