I have the following code which calculates the number of pairs 1 <= i < j <= n such that xs[i] == ys[j]:
def f(xs, ys):
s = 0
for j in range(xs.size):
s += np.sum(xs[:j] == ys[j])
return s
This is called a couple of times from some other procedure, so I need it to be as fast as possible (at some memory cost).
The sizes of the arrays are > 1e6.
Is there a faster equivalent that makes use of some numpy magic which gets rid of the for loop?
np.broadcast_to(xs, (xs.shape[0], xs.shape[0]))to get a square matrix of repeated xs, thennp.trilto zero-out elements above the diagonal, thennp.sum(zeroed_matrix == ys)... not sure about the last part, but play around with itnp.sum(zeroed_matrix==ys[:, np.newaxis])as in this answer