@Test public void testDistance() { System.out.println("distance"); double[] x = {1.0, 2.0, 3.0, 4.0}; double[] y = {4.0, 3.0, 2.0, 1.0}; assertEquals(4.472136, new EuclideanDistance().d(x, y), 1E-6); double[] w = {-2.1968219, -0.9559913, -0.0431738, 1.0567679, 0.3853515}; double[] v = {-1.7781325, -0.6659839, 0.9526148, -0.9460919, -0.3925300}; assertEquals(2.422302, new EuclideanDistance().d(w, v), 1E-6); SparseArray s = new SparseArray(); s.append(1, 1.0); s.append(2, 2.0); s.append(3, 3.0); s.append(4, 4.0); SparseArray t = new SparseArray(); t.append(1, 4.0); t.append(2, 3.0); t.append(3, 2.0); t.append(4, 1.0); assertEquals(4.472136, new SparseEuclideanDistance().d(s, t), 1E-6); s = new SparseArray(); s.append(2, 2.0); s.append(3, 3.0); s.append(4, 4.0); t = new SparseArray(); t.append(1, 4.0); t.append(2, 3.0); t.append(3, 2.0); assertEquals(5.830951, new SparseEuclideanDistance().d(s, t), 1E-6); s = new SparseArray(); s.append(1, 1.0); t = new SparseArray(); t.append(3, 2.0); assertEquals(2.236067, new SparseEuclideanDistance().d(s, t), 1E-6); }
public double d(int[] x, int[] y) { if (x.length != y.length) throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); double dist = 0.0; if (weight == null) { for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += d * d; } } else { if (x.length != weight.length) throw new IllegalArgumentException(String.format("Input vectors and weight vector have different length: %d, %d", x.length, weight.length)); for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += weight[i] * d * d; } } return Math.sqrt(dist); }
EuclideanDistance implements Metric<double[]> { public double d(int[] x, int[] y) { if (x.length != y.length) throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); double dist = 0.0; if (weight == null) { for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += d * d; } } else { if (x.length != weight.length) throw new IllegalArgumentException(String.format("Input vectors and weight vector have different length: %d, %d", x.length, weight.length)); for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += weight[i] * d * d; } } return Math.sqrt(dist); } }
EuclideanDistance implements Metric<double[]> { public double d(int[] x, int[] y) { if (x.length != y.length) throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); double dist = 0.0; if (weight == null) { for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += d * d; } } else { if (x.length != weight.length) throw new IllegalArgumentException(String.format("Input vectors and weight vector have different length: %d, %d", x.length, weight.length)); for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += weight[i] * d * d; } } return Math.sqrt(dist); } EuclideanDistance();  EuclideanDistance(double[] weight); }
EuclideanDistance implements Metric<double[]> { public double d(int[] x, int[] y) { if (x.length != y.length) throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); double dist = 0.0; if (weight == null) { for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += d * d; } } else { if (x.length != weight.length) throw new IllegalArgumentException(String.format("Input vectors and weight vector have different length: %d, %d", x.length, weight.length)); for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += weight[i] * d * d; } } return Math.sqrt(dist); } EuclideanDistance();  EuclideanDistance(double[] weight); @Override String toString(); double d(int[] x, int[] y); double d(float[] x, float[] y); @Override double d(double[] x, double[] y); }
EuclideanDistance implements Metric<double[]> { public double d(int[] x, int[] y) { if (x.length != y.length) throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length)); double dist = 0.0; if (weight == null) { for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += d * d; } } else { if (x.length != weight.length) throw new IllegalArgumentException(String.format("Input vectors and weight vector have different length: %d, %d", x.length, weight.length)); for (int i = 0; i < x.length; i++) { double d = x[i] - y[i]; dist += weight[i] * d * d; } } return Math.sqrt(dist); } EuclideanDistance();  EuclideanDistance(double[] weight); @Override String toString(); double d(int[] x, int[] y); double d(float[] x, float[] y); @Override double d(double[] x, double[] y);  }