@Test public void testDistance() { System.out.println("distance"); double[] x = {-2.1968219, -0.9559913, -0.0431738, 1.0567679, 0.3853515}; double[] y = {-1.7781325, -0.6659839, 0.9526148, -0.9460919, -0.3925300}; MinkowskiDistance m3 = new MinkowskiDistance(3); MinkowskiDistance m4 = new MinkowskiDistance(4); assertEquals(2.124599, m3.d(x, y), 1E-6); assertEquals(2.044696, m4.d(x, y), 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 = Math.abs(x[i] - y[i]); dist += Math.pow(d, p); } } 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 = Math.abs(x[i] - y[i]); dist += weight[i] * Math.pow(d, p); } } return Math.pow(dist, 1.0/p); }
MinkowskiDistance 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 = Math.abs(x[i] - y[i]); dist += Math.pow(d, p); } } 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 = Math.abs(x[i] - y[i]); dist += weight[i] * Math.pow(d, p); } } return Math.pow(dist, 1.0/p); } }
MinkowskiDistance 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 = Math.abs(x[i] - y[i]); dist += Math.pow(d, p); } } 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 = Math.abs(x[i] - y[i]); dist += weight[i] * Math.pow(d, p); } } return Math.pow(dist, 1.0/p); } MinkowskiDistance(int p);  MinkowskiDistance(int p, double[] weight); }
MinkowskiDistance 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 = Math.abs(x[i] - y[i]); dist += Math.pow(d, p); } } 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 = Math.abs(x[i] - y[i]); dist += weight[i] * Math.pow(d, p); } } return Math.pow(dist, 1.0/p); } MinkowskiDistance(int p);  MinkowskiDistance(int p, double[] weight); @Override String toString(); double d(int[] x, int[] y); double d(float[] x, float[] y); @Override double d(double[] x, double[] y); }
MinkowskiDistance 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 = Math.abs(x[i] - y[i]); dist += Math.pow(d, p); } } 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 = Math.abs(x[i] - y[i]); dist += weight[i] * Math.pow(d, p); } } return Math.pow(dist, 1.0/p); } MinkowskiDistance(int p);  MinkowskiDistance(int p, double[] weight); @Override String toString(); double d(int[] x, int[] y); double d(float[] x, float[] y); @Override double d(double[] x, double[] y);  }