You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by je...@apache.org on 2008/04/21 19:29:31 UTC

svn commit: r650209 - /lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/

Author: jeastman
Date: Mon Apr 21 10:29:14 2008
New Revision: 650209

URL: http://svn.apache.org/viewvc?rev=650209&view=rev
Log:
Added asFormatString() to Vector and Matrix interfaces and factored methods out of asWritableComparable() implementations. This is in preparation for removing the Point class (MAHOUT-47) which is redundant and only used for similar formatting by canopy and kmeans clustering. In general, other classes needing to produce formatted representations of themselves will benefit from asFormatString, which returns a more useful result than a pre-wrapped WritableComparable.

Classes Modified: AbstractMatrix, AbstractVector, DenseMatrix, DenseVector, Matrix, MatrixView, SparseColumnMatrix, SparseMatrix, SparseRowMatrix, SparseVector, Vector and VectorView.

All unit tests run. Did not add new tests since code is factored from and tested by existing tests.

Modified:
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractVector.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseMatrix.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseVector.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Matrix.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/MatrixView.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseMatrix.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseVector.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Vector.java
    lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/VectorView.java

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java Mon Apr 21 10:29:14 2008
@@ -18,7 +18,6 @@
 
 import org.apache.hadoop.io.WritableComparable;
 
-
 /**
  * A few universal implementations of convenience functions
  * 
@@ -38,9 +37,28 @@
    */
   public abstract WritableComparable asWritableComparable();
 
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#asFormatString()
+   */
+  public abstract String asFormatString();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#assignColumn(int,
+   *      org.apache.mahout.matrix.Vector)
+   */
   public abstract Matrix assignColumn(int column, Vector other)
       throws CardinalityException;
 
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#assignRow(int,
+   *      org.apache.mahout.matrix.Vector)
+   */
   public abstract Matrix assignRow(int row, Vector other)
       throws CardinalityException;
 
@@ -58,6 +76,11 @@
    */
   public abstract Matrix copy();
 
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#getColumn(int)
+   */
   public abstract Vector getColumn(int column) throws IndexException;
 
   /*
@@ -200,42 +223,43 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.Matrix#determinant()
    */
-   public double determinant() throws CardinalityException {
-   int[] card = cardinality();
-   int rowSize = card[ROW];
-   int columnSize = card[COL];
-   if(rowSize!=columnSize) throw new CardinalityException();
-
-   if(rowSize==2)
-       return getQuick(0,0)*getQuick(1,1)-getQuick(0,1) * getQuick(1,0);
-   else {
-       int sign = 1;
-       double ret = 0;
-
-       for(int i = 0; i<columnSize; i++){
-               Matrix minor = new DenseMatrix(rowSize-1,columnSize-1);
-               for(int j = 1; j<rowSize; j++){
-                   boolean flag = false; /* column offset flag */
-                   for(int k = 0; k<columnSize; k++){
-                       if(k==i) {
-                           flag = true;
-                           continue;
-                       }
-                       minor.set(j-1,flag ? k-1 : k, getQuick(j,k));
-                   }
-               }
-               ret += getQuick(0,i)*sign*minor.determinant();
-               sign*=-1;
+  public double determinant() throws CardinalityException {
+    int[] card = cardinality();
+    int rowSize = card[ROW];
+    int columnSize = card[COL];
+    if (rowSize != columnSize)
+      throw new CardinalityException();
+
+    if (rowSize == 2)
+      return getQuick(0, 0) * getQuick(1, 1) - getQuick(0, 1) * getQuick(1, 0);
+    else {
+      int sign = 1;
+      double ret = 0;
+
+      for (int i = 0; i < columnSize; i++) {
+        Matrix minor = new DenseMatrix(rowSize - 1, columnSize - 1);
+        for (int j = 1; j < rowSize; j++) {
+          boolean flag = false; /* column offset flag */
+          for (int k = 0; k < columnSize; k++) {
+            if (k == i) {
+              flag = true;
+              continue;
+            }
+            minor.set(j - 1, flag ? k - 1 : k, getQuick(j, k));
+          }
+        }
+        ret += getQuick(0, i) * sign * minor.determinant();
+        sign *= -1;
 
-           }
+      }
 
-           return ret;
-       }
+      return ret;
+    }
 
-   }
+  }
 
   /*
    * (non-Javadoc)
@@ -325,13 +349,16 @@
     setQuick(row, column, value);
   }
 
-  public void set(int row, double[] data) throws IndexException,CardinalityException {
+  public void set(int row, double[] data) throws IndexException,
+      CardinalityException {
     int[] c = cardinality();
-      if(c[COL] < data.length) throw new CardinalityException();
-      if( (c[ROW] < row) || (row < 0) ) throw new IndexException();
+    if (c[COL] < data.length)
+      throw new CardinalityException();
+    if ((c[ROW] < row) || (row < 0))
+      throw new IndexException();
 
-    for(int i = 0; i<c[COL]; i++)
-        setQuick(row,i,data[i]);
+    for (int i = 0; i < c[COL]; i++)
+      setQuick(row, i, data[i]);
   }
 
   /*

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractVector.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractVector.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/AbstractVector.java Mon Apr 21 10:29:14 2008
@@ -71,8 +71,10 @@
   /**
    * Subclasses must override to return an appropriately sparse or dense result
    * 
-   * @param rows the row cardinality
-   * @param columns the column cardinality
+   * @param rows
+   *            the row cardinality
+   * @param columns
+   *            the column cardinality
    * @return a Matrix
    */
   protected abstract Matrix matrixLike(int rows, int columns);
@@ -106,30 +108,46 @@
   public abstract Vector viewPart(int offset, int length)
       throws CardinalityException, IndexException;
 
-
   /**
-   *  Returns an iterator for traversing the Vector, but not in
-   *  any particular order. The actual implementations may  make 
-   *  some guarantees about the order in which the vector is 
-   *  traversed. Otherwise, the traversal order is undefined.
-   *  @see java.lang.Iterable#iterator()
+   * Returns an iterator for traversing the Vector, but not in any particular
+   * order. The actual implementations may make some guarantees about the order
+   * in which the vector is traversed. Otherwise, the traversal order is
+   * undefined.
+   * 
+   * @see java.lang.Iterable#iterator()
    */
   public abstract java.util.Iterator<Vector.Element> iterator();
 
   /*
    * (non-Javadoc)
+   * 
    * @see org.apache.mahout.matrix.Vector#getElement
    */
-  //@Override JDK 1.6
-  public Vector.Element getElement(int index) { return new Element(index); }
+  // @Override JDK 1.6
+  public Vector.Element getElement(int index) {
+    return new Element(index);
+  }
 
   public class Element implements Vector.Element {
     private int ind;
-    public Element(int ind) { this.ind=ind; }
-    public double get() { return getQuick(ind); }
-    public int index() { return ind; }
-    public void set(double value) { setQuick(ind,value); }
+
+    public Element(int ind) {
+      this.ind = ind;
+    }
+
+    public double get() {
+      return getQuick(ind);
+    }
+
+    public int index() {
+      return ind;
+    }
+
+    public void set(double value) {
+      setQuick(ind, value);
+    }
   }
+
   /*
    * (non-Javadoc)
    * 
@@ -351,5 +369,12 @@
       }
     return result;
   }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#asFormatString()
+   */
+  public abstract String asFormatString();
 
 }

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseMatrix.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseMatrix.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseMatrix.java Mon Apr 21 10:29:14 2008
@@ -38,7 +38,8 @@
   /**
    * Construct a matrix from the given values
    * 
-   * @param values a double[][]
+   * @param values
+   *            a double[][]
    */
   public DenseMatrix(double[][] values) {
     super();
@@ -67,6 +68,16 @@
    */
   @Override
   public WritableComparable asWritableComparable() {
+    return new Text(asFormatString());
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append("[[, ");
     for (int row = 0; row < rowSize(); row++) {
@@ -75,7 +86,7 @@
       out.append("], ");
     }
     out.append("] ");
-    return new Text(out.toString());
+    return out.toString();
   }
 
   /*
@@ -147,8 +158,7 @@
   /*
    * (non-Javadoc)
    * 
-   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
-   *      double)
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int, double)
    */
   @Override
   public void setQuick(int row, int column, double value) {

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseVector.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseVector.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/DenseVector.java Mon Apr 21 10:29:14 2008
@@ -28,7 +28,8 @@
   /**
    * Decode a new instance from the formatted string
    * 
-   * @param formattedString a string produced by the asFormatString method
+   * @param formattedString
+   *            a string produced by the asFormatString method
    * @return a DenseVector
    */
   public static Vector decodeFormat(WritableComparable formattedString) {
@@ -66,11 +67,11 @@
 
   @Override
   public WritableComparable asWritableComparable() {
-    return new Text(toString());
+    return new Text(asFormatString());
   }
 
   @Override
-  public String toString() {
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append("[, ");
     for (int i = 0; i < values.length; i++)
@@ -138,8 +139,9 @@
   }
 
   /**
-   * Returns an iterator that traverses this Vector from 0 
-   * to cardinality-1, in that order.
+   * Returns an iterator that traverses this Vector from 0 to cardinality-1, in
+   * that order.
+   * 
    * @see java.lang.Iterable#iterator
    */
   @Override
@@ -149,9 +151,21 @@
 
   private class Iterator implements java.util.Iterator<Vector.Element> {
     private int ind;
-    public Iterator() { ind=0; }
-    public boolean hasNext() { return ind<values.length; }
-    public Vector.Element next() { return new Element(ind++); }
-    public void remove() { throw new UnsupportedOperationException(); }
+
+    public Iterator() {
+      ind = 0;
+    }
+
+    public boolean hasNext() {
+      return ind < values.length;
+    }
+
+    public Vector.Element next() {
+      return new Element(ind++);
+    }
+
+    public void remove() {
+      throw new UnsupportedOperationException();
+    }
   }
 }

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Matrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Matrix.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Matrix.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Matrix.java Mon Apr 21 10:29:14 2008
@@ -33,6 +33,12 @@
   WritableComparable asWritableComparable();
 
   /**
+   * Return a formatted String suitable for output
+   * 
+   * @return
+   */
+  String asFormatString();
+  /**
    * Assign the value to all elements of the receiver
    * 
    * @param value a double value

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/MatrixView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/MatrixView.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/MatrixView.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/MatrixView.java Mon Apr 21 10:29:14 2008
@@ -34,10 +34,13 @@
 
   /**
    * Construct a view of the matrix with given offset and cardinality
-   *
-   * @param matrix an underlying Matrix
-   * @param offset the int[2] offset into the underlying matrix
-   * @param cardinality the int[2] cardinality of the view
+   * 
+   * @param matrix
+   *            an underlying Matrix
+   * @param offset
+   *            the int[2] offset into the underlying matrix
+   * @param cardinality
+   *            the int[2] cardinality of the view
    */
   public MatrixView(Matrix matrix, int[] offset, int[] cardinality) {
     super();
@@ -48,11 +51,22 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
    */
   @Override
   public WritableComparable asWritableComparable() {
+    String out = asFormatString();
+    return new Text(out);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append("[, ");
     for (int row = 0; row < cardinality[ROW]; row++) {
@@ -62,12 +76,12 @@
       out.append("], ");
     }
     out.append("] ");
-    return new Text(out.toString());
+    return out.toString();
   }
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#cardinality()
    */
   @Override
@@ -77,7 +91,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#copy()
    */
   @Override
@@ -87,7 +101,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#getQuick(int, int)
    */
   @Override
@@ -97,7 +111,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#like()
    */
   @Override
@@ -107,7 +121,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#like(int, int)
    */
   @Override
@@ -118,7 +132,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int, double)
    */
   @Override
@@ -128,7 +142,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#size()
    */
   @Override
@@ -138,7 +152,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#toArray()
    */
   @Override
@@ -167,7 +181,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
    */
   @Override
@@ -180,7 +194,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int,
    *      org.apache.mahout.vector.Vector)
    */
@@ -197,7 +211,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int,
    *      org.apache.mahout.vector.Vector)
    */
@@ -213,7 +227,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#getColumn(int)
    */
   @Override
@@ -226,7 +240,7 @@
 
   /*
    * (non-Javadoc)
-   *
+   * 
    * @see org.apache.mahout.matrix.AbstractMatrix#getRow(int)
    */
   @Override

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java Mon Apr 21 10:29:14 2008
@@ -32,8 +32,10 @@
   /**
    * Construct a matrix of the given cardinality with the given data columns
    * 
-   * @param cardinality the int[2] cardinality
-   * @param columns a SparseVector[] array of columns
+   * @param cardinality
+   *            the int[2] cardinality
+   * @param columns
+   *            a SparseVector[] array of columns
    */
   public SparseColumnMatrix(int[] cardinality, SparseVector[] columns) {
     super();
@@ -46,7 +48,8 @@
   /**
    * Construct a matrix of the given cardinality
    * 
-   * @param cardinality the int[2] cardinality
+   * @param cardinality
+   *            the int[2] cardinality
    */
   public SparseColumnMatrix(int[] cardinality) {
     super();
@@ -63,6 +66,17 @@
    */
   @Override
   public WritableComparable asWritableComparable() {
+    String out = asFormatString();
+    return new Text(out);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append("[[, ");
     for (int row = 0; row < columns[ROW].size(); row++) {
@@ -71,7 +85,7 @@
       out.append("], ");
     }
     out.append("] ");
-    return new Text(out.toString());
+    return out.toString();
   }
 
   /*
@@ -148,8 +162,7 @@
   /*
    * (non-Javadoc)
    * 
-   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
-   *      double)
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int, double)
    */
   @Override
   public void setQuick(int row, int column, double value) {

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseMatrix.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseMatrix.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseMatrix.java Mon Apr 21 10:29:14 2008
@@ -25,6 +25,14 @@
 /**
  * Doubly sparse matrix. Implemented as a Map of SparseVector rows
  */
+/**
+ * @author jeff
+ * 
+ */
+/**
+ * @author jeff
+ * 
+ */
 public class SparseMatrix extends AbstractMatrix {
 
   private int[] cardinality;
@@ -34,8 +42,10 @@
   /**
    * Construct a matrix of the given cardinality with the given row map
    * 
-   * @param cardinality the int[2] cardinality desired
-   * @param rows a Map<Integer, SparseVector> of rows
+   * @param cardinality
+   *            the int[2] cardinality desired
+   * @param rows
+   *            a Map<Integer, SparseVector> of rows
    */
   public SparseMatrix(int[] cardinality, Map<Integer, SparseVector> rows) {
     this.cardinality = cardinality.clone();
@@ -47,7 +57,8 @@
   /**
    * Construct a matrix of the given cardinality
    * 
-   * @param cardinality the int[2] cardinality desired
+   * @param cardinality
+   *            the int[2] cardinality desired
    */
   public SparseMatrix(int[] cardinality) {
     super();
@@ -62,12 +73,23 @@
    */
   @Override
   public WritableComparable asWritableComparable() {
+    String out = asFormatString();
+    return new Text(out);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append("[s").append(cardinality[ROW]).append(", ");
     for (Integer row : rows.keySet())
       out.append(rows.get(row).asWritableComparable());
     out.append("] ");
-    return new Text(out.toString());
+    return out.toString();
   }
 
   /*
@@ -146,8 +168,7 @@
   /*
    * (non-Javadoc)
    * 
-   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
-   *      double)
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int, double)
    */
   @Override
   public void setQuick(int row, int column, double value) {

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java Mon Apr 21 10:29:14 2008
@@ -32,8 +32,10 @@
   /**
    * Construct a matrix of the given cardinality with the given rows
    * 
-   * @param cardinality the int[2] cardinality desired
-   * @param rows a SparseVector[] array of rows
+   * @param cardinality
+   *            the int[2] cardinality desired
+   * @param rows
+   *            a SparseVector[] array of rows
    */
   public SparseRowMatrix(int[] cardinality, SparseVector[] rows) {
     this.cardinality = cardinality.clone();
@@ -45,7 +47,8 @@
   /**
    * Construct a matrix of the given cardinality
    * 
-   * @param cardinality the int[2] cardinality desired
+   * @param cardinality
+   *            the int[2] cardinality desired
    */
   public SparseRowMatrix(int[] cardinality) {
     super();
@@ -62,6 +65,17 @@
    */
   @Override
   public WritableComparable asWritableComparable() {
+    String out = asFormatString();
+    return new Text(out);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append("[[, ");
     for (int row = 0; row < rows.length; row++) {
@@ -70,7 +84,7 @@
       out.append("], ");
     }
     out.append("] ");
-    return new Text(out.toString());
+    return out.toString();
   }
 
   /*
@@ -147,8 +161,7 @@
   /*
    * (non-Javadoc)
    * 
-   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
-   *      double)
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int, double)
    */
   @Override
   public void setQuick(int row, int column, double value) {

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseVector.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseVector.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/SparseVector.java Mon Apr 21 10:29:14 2008
@@ -35,7 +35,8 @@
   /**
    * Decode a new instance from the formatted string
    * 
-   * @param formattedString a string produced by the asFormatString method
+   * @param formattedString
+   *            a string produced by the asFormatString method
    * @return a DenseVector
    */
   public static Vector decodeFormat(WritableComparable formattedString) {
@@ -69,12 +70,18 @@
 
   @Override
   public WritableComparable asWritableComparable() {
+    String out = asFormatString();
+    return new Text(out);
+  }
+
+  @Override
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append("[s").append(cardinality).append(", ");
     for (Integer index : values.keySet())
       out.append(index).append(':').append(values.get(index)).append(", ");
     out.append("] ");
-    return new Text(out.toString());
+    return out.toString();
   }
 
   @Override
@@ -154,33 +161,45 @@
   }
 
   private class Iterator implements java.util.Iterator<Vector.Element> {
-    private java.util.Iterator<Map.Entry<Integer,Double>> it;
-    public Iterator() { it=values.entrySet().iterator(); }
-    public boolean hasNext() { return it.hasNext(); }
-    public Element next() { return new Element(it.next().getKey()); }
-    public void remove() { throw new UnsupportedOperationException(); }
+    private java.util.Iterator<Map.Entry<Integer, Double>> it;
+
+    public Iterator() {
+      it = values.entrySet().iterator();
+    }
+
+    public boolean hasNext() {
+      return it.hasNext();
+    }
+
+    public Element next() {
+      return new Element(it.next().getKey());
+    }
+
+    public void remove() {
+      throw new UnsupportedOperationException();
+    }
   }
-  
+
   @Override
   public double zSum() {
-	  java.util.Iterator<Double> iter = values.values().iterator(); 
-	  double result = 0;
-	  while (iter.hasNext())
-		  result += iter.next();
-	  return result;
+    java.util.Iterator<Double> iter = values.values().iterator();
+    double result = 0;
+    while (iter.hasNext())
+      result += iter.next();
+    return result;
   }
 
   @Override
   public double dot(Vector x) throws CardinalityException {
-	  if (cardinality() != x.cardinality())
-		  throw new CardinalityException();
-	  java.util.Iterator<Integer> iter = values.keySet().iterator(); 
-	  double result = 0;
-	  while (iter.hasNext()){
-		  int nextIndex = iter.next();
-		  result += getQuick(nextIndex) * x.getQuick(nextIndex);
-	  }
-	  return result;
+    if (cardinality() != x.cardinality())
+      throw new CardinalityException();
+    java.util.Iterator<Integer> iter = values.keySet().iterator();
+    double result = 0;
+    while (iter.hasNext()) {
+      int nextIndex = iter.next();
+      result += getQuick(nextIndex) * x.getQuick(nextIndex);
+    }
+    return result;
   }
-  
+
 }

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Vector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Vector.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Vector.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/Vector.java Mon Apr 21 10:29:14 2008
@@ -18,12 +18,11 @@
 
 import org.apache.hadoop.io.WritableComparable;
 
-
 /**
  * The basic interface including numerous convenience functions
  * 
  */
-public interface Vector extends Iterable<Vector.Element>{
+public interface Vector extends Iterable<Vector.Element> {
 
   /**
    * Return a formatted WritableComparable suitable for output
@@ -33,9 +32,17 @@
   WritableComparable asWritableComparable();
 
   /**
+   * Return a formatted String suitable for output
+   * 
+   * @return
+   */
+  String asFormatString();
+
+  /**
    * Assign the value to all elements of the receiver
    * 
-   * @param value a double value
+   * @param value
+   *            a double value
    * @return the modified receiver
    */
   Vector assign(double value);
@@ -43,25 +50,30 @@
   /**
    * Assign the values to the receiver
    * 
-   * @param values a double[] of values
+   * @param values
+   *            a double[] of values
    * @return the modified receiver
-   * @throws CardinalityException if the cardinalities differ
+   * @throws CardinalityException
+   *             if the cardinalities differ
    */
   Vector assign(double[] values) throws CardinalityException;
 
   /**
    * Assign the other matrix values to the receiver
    * 
-   * @param other a Vector
+   * @param other
+   *            a Vector
    * @return the modified receiver
-   * @throws CardinalityException if the cardinalities differ
+   * @throws CardinalityException
+   *             if the cardinalities differ
    */
   Vector assign(Vector other) throws CardinalityException;
 
   /**
    * Apply the function to each element of the receiver
    * 
-   * @param function a DoubleFunction to apply
+   * @param function
+   *            a DoubleFunction to apply
    * @return the modified receiver
    */
   Vector assign(UnaryFunction function);
@@ -70,10 +82,13 @@
    * Apply the function to each element of the receiver and the corresponding
    * element of the other argument
    * 
-   * @param other a Vector containing the second arguments to the function
-   * @param function a DoubleDoubleFunction to apply
+   * @param other
+   *            a Vector containing the second arguments to the function
+   * @param function
+   *            a DoubleDoubleFunction to apply
    * @return the modified receiver
-   * @throws CardinalityException if the cardinalities differ
+   * @throws CardinalityException
+   *             if the cardinalities differ
    */
   Vector assign(Vector other, BinaryFunction function)
       throws CardinalityException;
@@ -93,9 +108,11 @@
   Vector copy();
 
   /**
-   * Return an object of Vector.Element representing an element
-   * of this Vector. Useful when designing new iterator types.
-   * @param index Index of the Vector.Element required
+   * Return an object of Vector.Element representing an element of this Vector.
+   * Useful when designing new iterator types.
+   * 
+   * @param index
+   *            Index of the Vector.Element required
    * @return The Vector.Element Object
    */
   Element getElement(int index);
@@ -105,12 +122,15 @@
      * @return the value of this vector element.
      */
     double get();
+
     /**
      * @return the index of this vector element.
      */
     int index();
+
     /**
-     * @param value Set the current element to value.
+     * @param value
+     *            Set the current element to value.
      */
     void set(double value);
   }
@@ -119,7 +139,8 @@
    * Return a new matrix containing the values of the recipient divided by the
    * argument
    * 
-   * @param x a double value
+   * @param x
+   *            a double value
    * @return a new Vector
    */
   Vector divide(double x);
@@ -127,25 +148,30 @@
   /**
    * Return the dot product of the recipient and the argument
    * 
-   * @param x a Vector
+   * @param x
+   *            a Vector
    * @return a new Vector
-   * @throws CardinalityException if the cardinalities differ
+   * @throws CardinalityException
+   *             if the cardinalities differ
    */
   double dot(Vector x) throws CardinalityException;
 
   /**
    * Return the value at the given index
    * 
-   * @param index an int index
+   * @param index
+   *            an int index
    * @return the double at the index
-   * @throws IndexException if the index is out of bounds
+   * @throws IndexException
+   *             if the index is out of bounds
    */
   double get(int index) throws IndexException;
 
   /**
    * Return the value at the given index, without checking bounds
    * 
-   * @param index an int index
+   * @param index
+   *            an int index
    * @return the double at the index
    */
   double getQuick(int index);
@@ -153,7 +179,8 @@
   /**
    * Return if the other matrix and the receiver share any underlying data cells
    * 
-   * @param other a Vector
+   * @param other
+   *            a Vector
    * @return true if the other matrix has common data cells
    */
   boolean haveSharedCells(Vector other);
@@ -169,7 +196,8 @@
    * Return an empty matrix of the same underlying class as the receiver and of
    * the given cardinality
    * 
-   * @param cardinality an int specifying the desired cardinality
+   * @param cardinality
+   *            an int specifying the desired cardinality
    * @return a Vector
    */
   Vector like(int cardinality);
@@ -178,9 +206,11 @@
    * Return a new matrix containing the element by element difference of the
    * recipient and the argument
    * 
-   * @param x a Vector
+   * @param x
+   *            a Vector
    * @return a new Vector
-   * @throws CardinalityException if the cardinalities differ
+   * @throws CardinalityException
+   *             if the cardinalities differ
    */
   Vector minus(Vector x) throws CardinalityException;
 
@@ -195,7 +225,8 @@
    * Return a new matrix containing the sum of each value of the recipient and
    * the argument
    * 
-   * @param x a double
+   * @param x
+   *            a double
    * @return a new Vector
    */
   Vector plus(double x);
@@ -204,26 +235,33 @@
    * Return a new matrix containing the element by element sum of the recipient
    * and the argument
    * 
-   * @param x a Vector
+   * @param x
+   *            a Vector
    * @return a new Vector
-   * @throws CardinalityException if the cardinalities differ
+   * @throws CardinalityException
+   *             if the cardinalities differ
    */
   Vector plus(Vector x) throws CardinalityException;
 
   /**
    * Set the value at the given index
    * 
-   * @param index an int index into the receiver
-   * @param value a double value to set
-   * @throws IndexException if the index is out of bounds
+   * @param index
+   *            an int index into the receiver
+   * @param value
+   *            a double value to set
+   * @throws IndexException
+   *             if the index is out of bounds
    */
   void set(int index, double value) throws IndexException;
 
   /**
    * Set the value at the given index, without checking bounds
    * 
-   * @param index an int index into the receiver
-   * @param value a double value to set
+   * @param index
+   *            an int index into the receiver
+   * @param value
+   *            a double value to set
    */
   void setQuick(int index, double value);
 
@@ -238,7 +276,8 @@
    * Return a new matrix containing the product of each value of the recipient
    * and the argument
    * 
-   * @param x a double argument
+   * @param x
+   *            a double argument
    * @return a new Vector
    */
   Vector times(double x);
@@ -247,9 +286,11 @@
    * Return a new matrix containing the element-wise product of the recipient
    * and the argument
    * 
-   * @param x a Vector argument
+   * @param x
+   *            a Vector argument
    * @return a new Vector
-   * @throws CardinalityException if the cardinalities differ
+   * @throws CardinalityException
+   *             if the cardinalities differ
    */
   Vector times(Vector x) throws CardinalityException;
 
@@ -263,13 +304,16 @@
   /**
    * Return a new matrix containing the subset of the recipient
    * 
-   * @param offset an int offset into the receiver
-   * @param length the cardinality of the desired result
+   * @param offset
+   *            an int offset into the receiver
+   * @param length
+   *            the cardinality of the desired result
    * @return a new Vector
-   * @throws CardinalityException if the length is greater than the cardinality
-   *         of the receiver
-   * @throws IndexException if the offset is negative or the offset+length is
-   *         outside of the receiver
+   * @throws CardinalityException
+   *             if the length is greater than the cardinality of the receiver
+   * @throws IndexException
+   *             if the offset is negative or the offset+length is outside of
+   *             the receiver
    */
   Vector viewPart(int offset, int length) throws CardinalityException,
       IndexException;
@@ -284,7 +328,8 @@
   /**
    * Return the cross product of the receiver and the other vector
    * 
-   * @param other another Vector
+   * @param other
+   *            another Vector
    * @return a Matrix
    */
   Matrix cross(Vector other);
@@ -300,5 +345,4 @@
   // DoubleDoubleFunction map);
   // NewVector assign(Vector y, DoubleDoubleFunction function, IntArrayList
   // nonZeroIndexes);
-
 }

Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/VectorView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/VectorView.java?rev=650209&r1=650208&r2=650209&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/VectorView.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/matrix/VectorView.java Mon Apr 21 10:29:14 2008
@@ -47,12 +47,18 @@
 
   @Override
   public WritableComparable asWritableComparable() {
+    String out = asFormatString();
+    return new Text(out);
+  }
+
+  @Override
+  public String asFormatString() {
     StringBuilder out = new StringBuilder();
     out.append('[');
     for (int i = offset; i < offset + cardinality; i++)
       out.append(getQuick(i)).append(", ");
     out.append("] ");
-    return new Text(out.toString());
+    return out.toString();
   }
 
   @Override
@@ -117,11 +123,11 @@
   }
 
   /*
-   * (Non-Javadoc)
-   * Returns true if index is a valid index in the underlying Vector
+   * (Non-Javadoc) Returns true if index is a valid index in the underlying
+   * Vector
    */
-  private boolean isInView(int index) { 
-    return index>=offset && index<offset+cardinality;
+  private boolean isInView(int index) {
+    return index >= offset && index < offset + cardinality;
   }
 
   @Override
@@ -159,7 +165,7 @@
           return;
         }
       }
-      el = null;  // No element was found
+      el = null; // No element was found
     }
 
     public Vector.Element next() {
@@ -172,8 +178,9 @@
       return el != null;
     }
 
-    /** @throws UnsupportedOperationException all the time. method not
-     * implemented.
+    /**
+     * @throws UnsupportedOperationException
+     *             all the time. method not implemented.
      */
     public void remove() {
       throw new UnsupportedOperationException();