HaversineDistance extends FastDistance { @Override public void updateLabel(FastDistanceData data) { if (data instanceof FastDistanceVectorData) { FastDistanceVectorData vectorData = (FastDistanceVectorData)data; Vector vec = vectorData.getVector(); Preconditions.checkState(vec.size() == VECTOR_SIZE, "HaversineDistance only supports vector size 2, the first value is latitude and the second value is " + "longitude"); if (vectorData.label == null || vectorData.label.size() != LABEL_SIZE) { vectorData.label = new DenseVector(LABEL_SIZE); } vectorData.label = new DenseVector(vectorLabel(vec.get(0), vec.get(1))); } else { FastDistanceMatrixData matrix = (FastDistanceMatrixData)data; if (matrix.label == null || matrix.label.numRows() != LABEL_SIZE || matrix.label.numCols() != matrix.vectors .numCols()) { matrix.label = new DenseMatrix(LABEL_SIZE, matrix.vectors.numCols()); } double[] matrixData = matrix.vectors.getData(); Preconditions.checkState(matrixData.length % VECTOR_SIZE == 0, "HaversineDistance only supports vector size 2, the first value is latitude and the second value is " + "longitude"); double[] normData = matrix.label.getData(); int labelCnt = 0; for (int i = 0; i < matrixData.length; i += VECTOR_SIZE) { double[] norm = vectorLabel(matrixData[i], matrixData[i + 1]); normData[labelCnt++] = norm[0]; normData[labelCnt++] = norm[1]; normData[labelCnt++] = norm[2]; } } }  double calc(double latitude1, double longitude1, double latitude2, double longitude2); @Override double calc(double[] array1, double[] array2); @Override double calc(Vector vec1, Vector vec2); @Override void updateLabel(FastDistanceData data);  }
@Test public void testUpdateLabel() { DenseVector label = distance.prepareVectorData(Tuple2.of(denseVector1, null)).getLabel(); Assert.assertEquals(label.size(), 3); Assert.assertEquals(label.get(0), 0.642, 0.001); Assert.assertEquals(label.get(1), 0.719, 0.001); Assert.assertEquals(label.get(2), 0.262, 0.001); FastDistanceMatrixData matrixData = initMatrixData(); Assert.assertEquals(matrixData.label.numCols(), 2); Assert.assertEquals(matrixData.label.numRows(), 3); double[] expect = new double[]{0.642, 0.719, 0.262, 0.173, 0.492, 0.852}; double[] predict = matrixData.getLabel().getData(); for (int i = 0; i < expect.length; i++) { Assert.assertEquals(expect[i], predict[i], 0.001); } }