EuclideanDistance extends FastDistance { @Override public void updateLabel(FastDistanceData data) { if (data instanceof FastDistanceVectorData) { FastDistanceVectorData vectorData = (FastDistanceVectorData)data; double d = MatVecOp.dot(vectorData.vector, vectorData.vector); if (vectorData.label == null || vectorData.label.size() != LABEL_SIZE) { vectorData.label = new DenseVector(LABEL_SIZE); } vectorData.label.set(0, d); } else { FastDistanceMatrixData matrix = (FastDistanceMatrixData)data; int vectorSize = matrix.vectors.numRows(); int numVectors = matrix.vectors.numCols(); if (matrix.label == null || matrix.label.numCols() != numVectors || matrix.label.numRows() != LABEL_SIZE) { matrix.label = new DenseMatrix(LABEL_SIZE, numVectors); } double[] label = matrix.label.getData(); double[] matrixData = matrix.vectors.getData(); Arrays.fill(label, 0.0); int labelCnt = 0; int cnt = 0; while(cnt < matrixData.length){ int endIndex = cnt + vectorSize; while(cnt < endIndex){ label[labelCnt] += matrixData[cnt] * matrixData[cnt]; cnt++; } labelCnt++; } } }  @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(), 1); Assert.assertEquals(label.get(0), denseVector1.normL2Square(), 0.001); FastDistanceMatrixData matrixData = initMatrixData(); Assert.assertEquals(matrixData.label.numCols(), 2); Assert.assertEquals(matrixData.label.numRows(), 1); for(int i = 0; i< matrixData.getVectors().numCols(); i++){ double[] data = matrixData.getVectors().getColumn(i); Assert.assertEquals(matrixData.label.getData()[i], BLAS.dot(data, data), 0.01); } }