*/
public static double euclideanDistance(DoubleVector a, DoubleVector b) {
check(a, b);
if (a instanceof SparseVector && b instanceof SparseVector) {
SparseVector svA = (SparseVector)a;
SparseVector svB = (SparseVector)b;
int[] aNonZero = svA.getNonZeroIndices();
int[] bNonZero = svB.getNonZeroIndices();
TIntSet union = new TIntHashSet(aNonZero);
union.addAll(bNonZero);
double sum = 0;
int[] nzIndices = union.toArray();
for (int nz : nzIndices) {
double x = a.get(nz);
double y = b.get(nz);
double diff = x - y;
sum += diff * diff;
}
return Math.sqrt(sum);
} else if (b instanceof SparseVector) {
// If b is sparse, use a special case where we use the cached
// magnitude of a and the sparsity of b to avoid most of the
// computations.
SparseVector sb = (SparseVector) b;
int[] bNonZero = sb.getNonZeroIndices();
double sum = 0;
// Get the magnitude for a. This value will often only be computed
// once for the first vector once since the DenseVector caches the
// magnitude, thus saving a large amount of computation.