DenseVectorSummarizer extends BaseVectorSummarizer { @Override public BaseVectorSummarizer visit(Vector vec) { if (vec instanceof DenseVector) { DenseVector dv = (DenseVector) vec; int n = dv.size(); if (count == 0) { init(n); } count++; if (sum.size() >= n) { for (int i = 0; i < n; i++) { double value = dv.get(i); if (!Double.isNaN(value)) { if (value < min.get(i)) { min.set(i, value); } if (value > max.get(i)) { max.set(i, value); } if (0 != value) { sum.add(i, value); normL1.add(i, Math.abs(value)); squareSum.add(i, value * value); numNonZero.add(i, 1); } } } if (calculateOuterProduct) { if (outerProduct == null) { outerProduct = dv.outer(); } else { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { outerProduct.add(i, j, dv.get(i) * dv.get(j)); } } } } return this; } else { DenseVectorSummarizer summarizer = new DenseVectorSummarizer(calculateOuterProduct); summarizer.visit(vec); return VectorSummarizerUtil.merge(summarizer, this); } } else { SparseVectorSummarizer sparseSrt = new SparseVectorSummarizer(calculateOuterProduct); sparseSrt.visit(vec); return VectorSummarizerUtil.merge(sparseSrt, this); } } DenseVectorSummarizer();  DenseVectorSummarizer(boolean calculateOuterProduction); @Override BaseVectorSummarizer visit(Vector vec); @Override String toString(); @Override BaseVectorSummary toSummary(); public DenseVector sum; public DenseVector squareSum; public DenseVector min; public DenseVector max; public DenseVector normL1; public DenseVector numNonZero; }
@Test public void visit() { DenseVectorSummarizer summarizer = summarizer(); assertEquals(5, summarizer.count); assertArrayEquals(new double[]{15.0, -15.0, 15.0}, summarizer.sum.getData(), 10e-6); assertArrayEquals(new double[]{55.0, 55.0, 45.0}, summarizer.squareSum.getData(), 10e-6); assertArrayEquals(new double[]{1.0, -5.0, 3.0}, summarizer.min.getData(), 10e-6); assertArrayEquals(new double[]{5.0, -1.0, 3.0}, summarizer.max.getData(), 10e-6); assertArrayEquals(new double[]{15.0, 15.0, 15.0}, summarizer.normL1.getData(), 10e-6); }