I have X (n x d), Y (m x d), and positive-definite L (d x d). I want to calculate D where D_ij is (X_i - Y_i) * L * (X_i - Y_i).T. n and m are around 250; d is around 10^4.
I can use scipy.spatial.distance.cdist
, but this is very slow.
scipy.spatial.distance.cdist(X, Y, metric='mahalanobis', VI=L)
Looking at Dougal's answer to this question, I tried
diff = X[np.newaxis, :, :] - Y[:, np.newaxis, :]
D = np.einsum('jik,kl,jil->ij', diff, L, diff)
Which is also very slow.
Is there a more efficient way to vectorize this computation?