MAHOUT-1693: Override .toString() in AbstractMatrix using VectorView closes apache/mahout#121
Project: http://git-wip-us.apache.org/repos/asf/mahout/repo Commit: http://git-wip-us.apache.org/repos/asf/mahout/commit/6a3f93e6 Tree: http://git-wip-us.apache.org/repos/asf/mahout/tree/6a3f93e6 Diff: http://git-wip-us.apache.org/repos/asf/mahout/diff/6a3f93e6 Branch: refs/heads/mahout-0.10.x Commit: 6a3f93e6f695d12f63ef48c5e33f0d24658bd596 Parents: 8be44ab Author: Andrew Palumbo <[email protected]> Authored: Tue Apr 21 23:11:50 2015 -0400 Committer: Andrew Palumbo <[email protected]> Committed: Tue Apr 21 23:11:50 2015 -0400 ---------------------------------------------------------------------- .../org/apache/mahout/math/AbstractMatrix.java | 29 +++++++++++++++--- .../mahout/math/FunctionalMatrixView.java | 4 --- .../apache/mahout/math/SparseColumnMatrix.java | 31 ++++++++++++++++++-- 3 files changed, 53 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mahout/blob/6a3f93e6/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java b/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java index 0dd084e..bb363b8 100644 --- a/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java +++ b/math/src/main/java/org/apache/mahout/math/AbstractMatrix.java @@ -782,13 +782,34 @@ public abstract class AbstractMatrix implements Matrix { @Override public String toString() { + int row = 0; + int maxRowsToDisplay = 10; + int maxColsToDisplay = 20; + int colsToDisplay = maxColsToDisplay; + + if(maxColsToDisplay > columnSize()){ + colsToDisplay = columnSize(); + } + + StringBuilder s = new StringBuilder("{\n"); Iterator<MatrixSlice> it = iterator(); - while (it.hasNext()) { + while ((it.hasNext()) && (row < maxRowsToDisplay)) { MatrixSlice next = it.next(); - s.append(" ").append(next.index()).append(" =>\t").append(next.vector()).append('\n'); + s.append(" ").append(next.index()) + .append(" =>\t") + .append(new VectorView(next.vector(), 0, colsToDisplay)) + .append('\n'); + row ++; + } + String returnString = s.toString(); + if (maxColsToDisplay <= columnSize()) { + returnString = returnString.replace("}", " ... } "); + } + if(maxRowsToDisplay <= rowSize()) + return returnString + ("... }"); + else{ + return returnString + ("}"); } - s.append("}"); - return s.toString(); } } http://git-wip-us.apache.org/repos/asf/mahout/blob/6a3f93e6/math/src/main/java/org/apache/mahout/math/FunctionalMatrixView.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/FunctionalMatrixView.java b/math/src/main/java/org/apache/mahout/math/FunctionalMatrixView.java index e955a5e..2a13611 100644 --- a/math/src/main/java/org/apache/mahout/math/FunctionalMatrixView.java +++ b/math/src/main/java/org/apache/mahout/math/FunctionalMatrixView.java @@ -87,8 +87,4 @@ class FunctionalMatrixView extends AbstractMatrix { return new MatrixVectorView(this, 0, column, 1, 0, denseLike); } - @Override - public String toString(){ - return "org.apache.mahout.math.FunctionalMatrixView"; - } } http://git-wip-us.apache.org/repos/asf/mahout/blob/6a3f93e6/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java ---------------------------------------------------------------------- diff --git a/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java b/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java index b852026..d847dea 100644 --- a/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java +++ b/math/src/main/java/org/apache/mahout/math/SparseColumnMatrix.java @@ -172,12 +172,37 @@ public class SparseColumnMatrix extends AbstractMatrix { @Override public String toString() { + int row = 0; + int maxRowsToDisplay = 10; + int maxColsToDisplay = 20; + int colsToDisplay = maxColsToDisplay; + + if(maxColsToDisplay > columnSize()){ + colsToDisplay = columnSize(); + } + StringBuilder s = new StringBuilder("{\n"); for (MatrixSlice next : this.transpose()) { - s.append(" ").append(next.index()).append(" =>\t").append(next.vector()).append('\n'); + if (row < maxRowsToDisplay) { + s.append(" ") + .append(next.index()) + .append(" =>\t") + .append(new VectorView(next.vector(), 0, colsToDisplay)) + .append('\n'); + row++; + } + } + + String returnString = s.toString(); + if (maxColsToDisplay <= columnSize()) { + returnString = returnString.replace("}", " ... }"); + } + + if (maxRowsToDisplay <= rowSize()) { + return returnString + "... }"; + } else { + return returnString + "}"; } - s.append("}"); - return s.toString(); } }
