SparseVectorSummarizer extends BaseVectorSummarizer { @Override public BaseVectorSummarizer visit(Vector vec) { SparseVector sv; if (vec instanceof DenseVector) { DenseVector dv = (DenseVector) vec; int[] indices = new int[dv.size()]; for (int i = 0; i < dv.size(); i++) { indices[i] = i; } sv = new SparseVector(dv.size(), indices, dv.getData()); } else { sv = (SparseVector) vec; } count++; this.colNum = Math.max(this.colNum, sv.size()); if (sv.numberOfValues() != 0) { VectorIterator iter = sv.iterator(); while (iter.hasNext()) { int index = iter.getIndex(); double value = iter.getValue(); if (cols.containsKey(index)) { cols.get(index).visit(value); } else { VectorStatCol statCol = new VectorStatCol(); statCol.visit(value); cols.put(index, statCol); } iter.next(); } if (calculateOuterProduct) { int size = sv.getIndices()[sv.getIndices().length - 1] + 1; if (outerProduct == null) { outerProduct = DenseMatrix.zeros(size, size); } else { if (size > outerProduct.numRows()) { DenseMatrix dpNew = DenseMatrix.zeros(size, size); if (outerProduct != null) { outerProduct = VectorSummarizerUtil.plusEqual(dpNew, outerProduct); } } } for (int i = 0; i < sv.getIndices().length; i++) { double val = sv.getValues()[i]; int iIdx = sv.getIndices()[i]; for (int j = 0; j < sv.getIndices().length; j++) { outerProduct.add(iIdx, sv.getIndices()[j], val * sv.getValues()[j]); } } } } return this; } SparseVectorSummarizer();  SparseVectorSummarizer(boolean calculateOuterProduction); @Override BaseVectorSummarizer visit(Vector vec); @Override String toString(); @Override BaseVectorSummary toSummary(); SparseVectorSummarizer copy(); public Map<Integer, VectorStatCol> cols; }
@Test public void testVisit() { SparseVectorSummarizer summarizer = summarizer(); Assert.assertEquals(5, summarizer.colNum); Assert.assertEquals(5, summarizer.count); Assert.assertEquals(5, summarizer.cols.size()); Assert.assertEquals(10, summarizer.cols.get(0).sum, 10e-6); Assert.assertEquals(5, summarizer.cols.get(0).max, 10e-6); Assert.assertEquals(1, summarizer.cols.get(0).min, 10e-6); Assert.assertEquals(3, summarizer.cols.get(0).numNonZero, 10e-6); Assert.assertEquals(10, summarizer.cols.get(0).normL1, 10e-6); Assert.assertEquals(42, summarizer.cols.get(0).squareSum, 10e-6); }
@Test public void testVisitDenseVector() { SparseVectorSummarizer summarizer = summarizer(); SparseVectorSummarizer addDenseSummarizer = (SparseVectorSummarizer) summarizer.visit(new DenseVector(new double[]{-1.0, 2.0, 3.0})); Assert.assertEquals(5, addDenseSummarizer.colNum); Assert.assertEquals(6, addDenseSummarizer.count); Assert.assertEquals(5, addDenseSummarizer.cols.size()); Assert.assertEquals(9, addDenseSummarizer.cols.get(0).sum, 10e-6); Assert.assertEquals(5, addDenseSummarizer.cols.get(0).max, 10e-6); Assert.assertEquals(-1, addDenseSummarizer.cols.get(0).min, 10e-6); Assert.assertEquals(4, addDenseSummarizer.cols.get(0).numNonZero, 10e-6); Assert.assertEquals(11, addDenseSummarizer.cols.get(0).normL1, 10e-6); Assert.assertEquals(43, addDenseSummarizer.cols.get(0).squareSum, 10e-6); }