Compute the distance matrix between each pair from a feature array X and Y.
For efficiency reasons, the euclidean distance between a pair of row
vector x and y is computed as:
dist(x,y)=sqrt(dot(x,x)-2*dot(x,y)+dot(y,y))
This formulation has two advantages over other ways of computing distances.
First, it is computationally efficient when dealing with sparse data.
Second, if one argument varies but the other remains unchanged, then
dot(x,x) and/or dot(y,y) can be pre-computed.
However, this is not the most precise way of doing this computation,
because this equation potentially suffers from “catastrophic cancellation”.
Also, the distance matrix returned by this function may not be exactly
symmetric as required by, e.g., scipy.spatial.distance functions.