FastDistance implements ContinuousDistance { public List<FastDistanceData> prepareMatrixData(Iterable<Row> rows, int vectorIdx, int... keepIdxs) { Iterable<Tuple2<Vector, Row>> newItearble = Iterables.transform(rows, (row) -> { Vector vec = VectorUtil.getVector(row.getField(vectorIdx)); row = TableUtil.getRow(row, keepIdxs); return Tuple2.of(vec, row); }); return prepareMatrixData(newItearble); }  List<FastDistanceData> prepareMatrixData(Iterable<Row> rows, int vectorIdx, int... keepIdxs); FastDistanceVectorData prepareVectorData(Row row, int vectorIdx, int... keepIdxs); List<FastDistanceData> prepareMatrixData(Iterable<Tuple2<Vector, Row>> tuples); FastDistanceVectorData prepareVectorData(Tuple2<Vector, Row> tuple); DenseMatrix calc(FastDistanceData left, FastDistanceData right); DenseMatrix calc(FastDistanceData left, FastDistanceData right, DenseMatrix res); abstract void updateLabel(FastDistanceData data);  }
@Test public void testDenseIterableRowInput(){ int len = 10000; int size = 100; List<Vector> vectorList = initDenseData(len, size); List<Row> rows = new ArrayList<>(); for(int i = 0; i < vectorList.size(); i++){ rows.add(Row.of(i, vectorList.get(i))); } EuclideanDistance distance = new EuclideanDistance(); List<FastDistanceData> list = distance.prepareMatrixData(rows, 1, 0); assertIterableDenseInput(list, len, size, vectorList); }
@Test public void testDenseIterableTupleInput(){ int len = 10000; int size = 100; List<Vector> vectorList = initDenseData(len, size); List<Tuple2<Vector, Row>> rows = new ArrayList<>(); for(int i = 0; i < vectorList.size(); i++){ rows.add(Tuple2.of(vectorList.get(i), Row.of(i))); } EuclideanDistance distance = new EuclideanDistance(); List<FastDistanceData> list = distance.prepareMatrixData(rows); assertIterableDenseInput(list, len, size, vectorList); }
@Test public void testSparseIterableRowInput(){ int len = 20; int size = 50; List<Vector> vectorList = initSparseData(len, size); List<Row> rows = new ArrayList<>(); for(int i = 0; i < vectorList.size(); i++){ rows.add(Row.of(i, vectorList.get(i))); } EuclideanDistance distance = new EuclideanDistance(); List<FastDistanceData> list = distance.prepareMatrixData(rows, 1, 0); assertIterableSparseInput(list, len, vectorList); }
@Test public void testSparseIterableTupleInput(){ int len = 20; int size = 50; List<Vector> vectorList = initSparseData(len, size); List<Tuple2<Vector, Row>> rows = new ArrayList<>(); for(int i = 0; i < vectorList.size(); i++){ rows.add(Tuple2.of(vectorList.get(i), Row.of(i))); } EuclideanDistance distance = new EuclideanDistance(); List<FastDistanceData> list = distance.prepareMatrixData(rows); assertIterableSparseInput(list, len, vectorList); }