@Test public void testCalDistanceVecMatrix() { FastDistanceMatrixData matrixData = initMatrixData(); FastDistanceVectorData vectorData = distance.prepareVectorData(Row.of(0, sparseVector1), 1); double[] predict = distance.calc(matrixData, vectorData).getData(); double[] expect = new double[] {0.73, 0.60}; for (int i = 0; i < expect.length; i++) { Assert.assertEquals(expect[i], predict[i], 0.01); } predict = distance.calc(vectorData, matrixData).getData(); for (int i = 0; i < expect.length; i++) { Assert.assertEquals(expect[i], predict[i], 0.01); } }
@Override public double calc(double[] array1, double[] array2) { double dot = BLAS.dot(array1, array2); double cross = Math.sqrt(BLAS.dot(array1, array1) * BLAS.dot(array2, array2)); return 1.0 - (cross > 0.0 ? dot / cross : 0.0); }
CosineDistance extends FastDistance { @Override public double calc(double[] array1, double[] array2) { double dot = BLAS.dot(array1, array2); double cross = Math.sqrt(BLAS.dot(array1, array1) * BLAS.dot(array2, array2)); return 1.0 - (cross > 0.0 ? dot / cross : 0.0); } }
CosineDistance extends FastDistance { @Override public double calc(double[] array1, double[] array2) { double dot = BLAS.dot(array1, array2); double cross = Math.sqrt(BLAS.dot(array1, array1) * BLAS.dot(array2, array2)); return 1.0 - (cross > 0.0 ? dot / cross : 0.0); }  }
CosineDistance extends FastDistance { @Override public double calc(double[] array1, double[] array2) { double dot = BLAS.dot(array1, array2); double cross = Math.sqrt(BLAS.dot(array1, array1) * BLAS.dot(array2, array2)); return 1.0 - (cross > 0.0 ? dot / cross : 0.0); }  @Override double calc(double[] array1, double[] array2); @Override double calc(Vector vec1, Vector vec2); @Override void updateLabel(FastDistanceData data); }
CosineDistance extends FastDistance { @Override public double calc(double[] array1, double[] array2) { double dot = BLAS.dot(array1, array2); double cross = Math.sqrt(BLAS.dot(array1, array1) * BLAS.dot(array2, array2)); return 1.0 - (cross > 0.0 ? dot / cross : 0.0); }  @Override double calc(double[] array1, double[] array2); @Override double calc(Vector vec1, Vector vec2); @Override void updateLabel(FastDistanceData data);  }