You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by gs...@apache.org on 2008/03/16 22:24:51 UTC

svn commit: r637664 [1/3] - in /lucene/mahout/trunk/src: main/java/org/apache/mahout/matrix/ test/java/org/apache/mahout/matrix/

Author: gsingers
Date: Sun Mar 16 14:24:48 2008
New Revision: 637664

URL: http://svn.apache.org/viewvc?rev=637664&view=rev
Log:
MAHOUT-6:  Matrix/Vector initial implementation

Added:
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/BinaryFunction.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/CardinalityException.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseMatrix.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/IndexException.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/NegateFunction.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/PlusFunction.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseMatrix.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/UnaryFunction.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Vector.java   (with props)
    lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/VectorView.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseMatrix.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestDenseVector.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseColumnMatrix.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseMatrix.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseRowMatrix.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestSparseVector.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/TestVectorView.java   (with props)
    lucene/mahout/trunk/src/test/java/org/apache/mahout/matrix/VectorTest.java   (with props)

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,352 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.WritableComparable;
+
+
+/**
+ * A few universal implementations of convenience functions
+ * 
+ */
+public abstract class AbstractMatrix implements Matrix {
+
+  // index into int[2] for column value
+  public static final int COL = 1;
+
+  // index into int[2] for row value
+  public static final int ROW = 0;
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#asFormatString()
+   */
+  public abstract WritableComparable asWritableComparable();
+
+  public abstract Matrix assignColumn(int column, Vector other)
+      throws CardinalityException;
+
+  public abstract Matrix assignRow(int row, Vector other)
+      throws CardinalityException;
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#cardinality()
+   */
+  public abstract int[] cardinality();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#copy()
+   */
+  public abstract Matrix copy();
+
+  public abstract Vector getColumn(int column) throws IndexException;
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#getQuick(int, int)
+   */
+  public abstract double getQuick(int row, int column);
+
+  public abstract Vector getRow(int row) throws IndexException;
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
+   */
+  public abstract boolean haveSharedCells(Matrix other);
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#like()
+   */
+  public abstract Matrix like();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#like(int, int)
+   */
+  public abstract Matrix like(int rows, int columns);
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#setQuick(int, int, double)
+   */
+  public abstract void setQuick(int row, int column, double value);
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#size()
+   */
+  public abstract int[] size();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#toArray()
+   */
+  public abstract double[][] toArray();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#viewPart(int[], int[])
+   */
+  public abstract Matrix viewPart(int[] offset, int[] length)
+      throws CardinalityException, IndexException;
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#assign(double)
+   */
+  public Matrix assign(double value) {
+    int[] c = cardinality();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        setQuick(row, col, value);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#assign(double[][])
+   */
+  public Matrix assign(double[][] values) throws CardinalityException {
+    int[] c = cardinality();
+    if (c[ROW] != values.length)
+      throw new CardinalityException();
+    for (int row = 0; row < c[ROW]; row++)
+      if (c[COL] != values[row].length)
+        throw new CardinalityException();
+      else
+        for (int col = 0; col < c[COL]; col++)
+          setQuick(row, col, values[row][col]);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#assign(org.apache.mahout.matrix.Matrix,
+   *      org.apache.mahout.function.BinaryFunction)
+   */
+  public Matrix assign(Matrix other, BinaryFunction function)
+      throws CardinalityException {
+    int[] c = cardinality();
+    int[] o = other.cardinality();
+    if (c[ROW] != o[ROW] || c[COL] != o[COL])
+      throw new CardinalityException();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        setQuick(row, col, function.apply(getQuick(row, col), other.getQuick(
+            row, col)));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#assign(org.apache.mahout.matrix.Matrix)
+   */
+  public Matrix assign(Matrix other) throws CardinalityException {
+    int[] c = cardinality();
+    int[] o = other.cardinality();
+    if (c[ROW] != o[ROW] || c[COL] != o[COL])
+      throw new CardinalityException();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        setQuick(row, col, other.getQuick(row, col));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#assign(org.apache.mahout.function.UnaryFunction)
+   */
+  public Matrix assign(UnaryFunction function) {
+    int[] c = cardinality();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        setQuick(row, col, function.apply(getQuick(row, col)));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#divide(double)
+   */
+  public Matrix divide(double x) {
+    Matrix result = copy();
+    int[] c = cardinality();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        result.setQuick(row, col, result.getQuick(row, col) / x);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#get(int, int)
+   */
+  public double get(int row, int column) throws IndexException {
+    int[] c = cardinality();
+    if (row < 0 || column < 0 || row >= c[ROW] || column >= c[COL])
+      throw new IndexException();
+    return getQuick(row, column);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#minus(org.apache.mahout.matrix.Matrix)
+   */
+  public Matrix minus(Matrix other) throws CardinalityException {
+    int[] c = cardinality();
+    int[] o = other.cardinality();
+    if (c[ROW] != o[ROW] || c[COL] != o[COL])
+      throw new CardinalityException();
+    Matrix result = copy();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        result.setQuick(row, col, result.getQuick(row, col)
+            - other.getQuick(row, col));
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#plus(double)
+   */
+  public Matrix plus(double x) {
+    Matrix result = copy();
+    int[] c = cardinality();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        result.setQuick(row, col, result.getQuick(row, col) + x);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#plus(org.apache.mahout.matrix.Matrix)
+   */
+  public Matrix plus(Matrix other) throws CardinalityException {
+    int[] c = cardinality();
+    int[] o = other.cardinality();
+    if (c[ROW] != o[ROW] || c[COL] != o[COL])
+      throw new CardinalityException();
+    Matrix result = copy();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        result.setQuick(row, col, result.getQuick(row, col)
+            + other.getQuick(row, col));
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#set(int, int, double)
+   */
+  public void set(int row, int column, double value) throws IndexException {
+    int[] c = cardinality();
+    if (row < 0 || column < 0 || row >= c[ROW] || column >= c[COL])
+      throw new IndexException();
+    setQuick(row, column, value);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#times(double)
+   */
+  public Matrix times(double x) {
+    Matrix result = copy();
+    int[] c = cardinality();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        result.setQuick(row, col, result.getQuick(row, col) * x);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#times(org.apache.mahout.matrix.Matrix)
+   */
+  public Matrix times(Matrix other) throws CardinalityException {
+    int[] c = cardinality();
+    int[] o = other.cardinality();
+    if (c[COL] != o[ROW])
+      throw new CardinalityException();
+    Matrix result = like(c[ROW], o[COL]);
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++) {
+        double sum = 0;
+        for (int k = 0; k < c[COL]; k++)
+          sum += getQuick(row, k) * other.getQuick(k, col);
+        result.setQuick(row, col, sum);
+      }
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#transpose()
+   */
+  public Matrix transpose() {
+    int[] card = cardinality();
+    Matrix result = like(card[COL], card[ROW]);
+    for (int row = 0; row < card[ROW]; row++)
+      for (int col = 0; col < card[COL]; col++)
+        result.setQuick(col, row, getQuick(row, col));
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Matrix#zSum()
+   */
+  public double zSum() {
+    double result = 0;
+    int[] c = cardinality();
+    for (int row = 0; row < c[ROW]; row++)
+      for (int col = 0; col < c[COL]; col++)
+        result += getQuick(row, col);
+    return result;
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractMatrix.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,331 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * Implementations of generic capabilities like sum of elements and dot products
+ * 
+ */
+public abstract class AbstractVector implements Vector {
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#asFormatString()
+   */
+  public abstract WritableComparable asWritableComparable();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#cardinality()
+   */
+  public abstract int cardinality();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#copy()
+   */
+  public abstract Vector copy();
+
+  public abstract boolean haveSharedCells(Vector other);
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#getQuick(int)
+   */
+  public abstract double getQuick(int index);
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#like()
+   */
+  public abstract Vector like();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#like(int)
+   */
+  public abstract Vector like(int cardinality);
+
+  /**
+   * Subclasses must override to return an appropriately sparse or dense result
+   * 
+   * @param rows the row cardinality
+   * @param columns the column cardinality
+   * @return a Matrix
+   */
+  protected abstract Matrix matrixLike(int rows, int columns);
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#setQuick(int, double)
+   */
+  public abstract void setQuick(int index, double value);
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#size()
+   */
+  public abstract int size();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#toArray()
+   */
+  public abstract double[] toArray();
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#viewPart(int, int)
+   */
+  public abstract Vector viewPart(int offset, int length)
+      throws CardinalityException, IndexException;
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#normalize(double)
+   */
+  public Vector divide(double x) {
+    Vector result = copy();
+    for (int i = 0; i < result.cardinality(); i++)
+      result.setQuick(i, getQuick(i) / x);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#dot(org.apache.mahout.matrix.Vector)
+   */
+  public double dot(Vector x) throws CardinalityException {
+    if (cardinality() != x.cardinality())
+      throw new CardinalityException();
+    double result = 0;
+    for (int i = 0; i < cardinality(); i++)
+      result += getQuick(i) * x.getQuick(i);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#get(int)
+   */
+  public double get(int index) throws IndexException {
+    if (index >= 0 && index < cardinality())
+      return getQuick(index);
+    else
+      throw new IndexException();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#minus(org.apache.mahout.matrix.Vector)
+   */
+  public Vector minus(Vector x) throws CardinalityException {
+    if (cardinality() != x.cardinality())
+      throw new CardinalityException();
+    Vector result = copy();
+    for (int i = 0; i < result.cardinality(); i++)
+      result.setQuick(i, getQuick(i) - x.getQuick(i));
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#normalize()
+   */
+  public Vector normalize() {
+    double divSq = 0;
+    try {
+      divSq = Math.sqrt(dot(this));
+    } catch (CardinalityException e) {
+      // cannot occur with dot(this)
+    }
+    return divide(divSq);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#plus(double)
+   */
+  public Vector plus(double x) {
+    Vector result = copy();
+    for (int i = 0; i < result.cardinality(); i++)
+      result.setQuick(i, getQuick(i) + x);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#plus(org.apache.mahout.matrix.Vector)
+   */
+  public Vector plus(Vector x) throws CardinalityException {
+    if (cardinality() != x.cardinality())
+      throw new CardinalityException();
+    Vector result = copy();
+    for (int i = 0; i < result.cardinality(); i++)
+      result.setQuick(i, getQuick(i) + x.getQuick(i));
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#set(int, double)
+   */
+  public void set(int index, double value) throws IndexException {
+    if (index >= 0 && index < cardinality())
+      setQuick(index, value);
+    else
+      throw new IndexException();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#times(double)
+   */
+  public Vector times(double x) {
+    Vector result = copy();
+    for (int i = 0; i < result.cardinality(); i++)
+      result.setQuick(i, getQuick(i) * x);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#times(org.apache.mahout.matrix.Vector)
+   */
+  public Vector times(Vector x) throws CardinalityException {
+    if (cardinality() != x.cardinality())
+      throw new CardinalityException();
+    Vector result = copy();
+    for (int i = 0; i < result.cardinality(); i++)
+      result.setQuick(i, getQuick(i) * x.getQuick(i));
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#zSum()
+   */
+  public double zSum() {
+    double result = 0;
+    for (int i = 0; i < cardinality(); i++)
+      result += getQuick(i);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#assign(double)
+   */
+  public Vector assign(double value) {
+    for (int i = 0; i < cardinality(); i++)
+      setQuick(i, value);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#assign(double[])
+   */
+  public Vector assign(double[] values) throws CardinalityException {
+    if (values.length != cardinality())
+      throw new CardinalityException();
+    for (int i = 0; i < cardinality(); i++)
+      setQuick(i, values[i]);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#assign(org.apache.mahout.matrix.Vector)
+   */
+  public Vector assign(Vector other) throws CardinalityException {
+    if (other.cardinality() != cardinality())
+      throw new CardinalityException();
+    for (int i = 0; i < cardinality(); i++)
+      setQuick(i, other.getQuick(i));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#assign(org.apache.mahout.utils.matrix.DoubleFunction)
+   */
+  public Vector assign(UnaryFunction function) {
+    for (int i = 0; i < cardinality(); i++)
+      setQuick(i, function.apply(getQuick(i)));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#assign(org.apache.mahout.matrix.Vector,
+   *      org.apache.mahout.utils.matrix.DoubleDoubleFunction)
+   */
+  public Vector assign(Vector other, BinaryFunction function)
+      throws CardinalityException {
+    if (other.cardinality() != cardinality())
+      throw new CardinalityException();
+    for (int i = 0; i < cardinality(); i++)
+      setQuick(i, function.apply(getQuick(i), other.getQuick(i)));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.Vector#cross(org.apache.mahout.matrix.Vector)
+   */
+  public Matrix cross(Vector other) {
+    Matrix result = matrixLike(cardinality(), other.cardinality());
+    for (int row = 0; row < cardinality(); row++)
+      try {
+        result.assignRow(row, other.times(getQuick(row)));
+      } catch (CardinalityException e) {
+        // cannot happen since result is other's cardinality
+      }
+    return result;
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/AbstractVector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/BinaryFunction.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/BinaryFunction.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/BinaryFunction.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/BinaryFunction.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,35 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+/**
+ * This interface allows the formulation of binary functions to be applied to
+ * matrices inside the inner loops of their implementations.
+ * 
+ */
+public interface BinaryFunction {
+
+  /**
+   * Apply the function to the arguments and return the result
+   * 
+   * @param arg1 a double for the first argument
+   * @param arg2 a double for the second argument
+   * @return the result of applying the function
+   */
+  public double apply(double arg1, double arg2);
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/BinaryFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/CardinalityException.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/CardinalityException.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/CardinalityException.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/CardinalityException.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,27 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+/**
+ * Exception thrown when there is a cardinality mismatch in matrix operations
+ * 
+ */
+public class CardinalityException extends Exception {
+
+  private static final long serialVersionUID = 1L;
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/CardinalityException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseMatrix.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,252 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * Matrix of doubles implemented using a 2-d array
+ * 
+ */
+public class DenseMatrix extends AbstractMatrix {
+
+  private double[][] values;
+
+  private int columnSize() {
+    return values[0].length;
+  }
+
+  private int rowSize() {
+    return values.length;
+  }
+
+  /**
+   * Construct a matrix from the given values
+   * 
+   * @param values a double[][]
+   */
+  public DenseMatrix(double[][] values) {
+    super();
+    // clone the rows
+    this.values = values.clone();
+    // be careful, need to clone the columns too
+    for (int i = 0; i < values.length; i++)
+      this.values[i] = this.values[i].clone();
+  }
+
+  /**
+   * Construct an empty matrix of the given size
+   * 
+   * @param rows
+   * @param columns
+   */
+  public DenseMatrix(int rows, int columns) {
+    super();
+    this.values = new double[rows][columns];
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public WritableComparable asWritableComparable() {
+    StringBuilder out = new StringBuilder();
+    out.append("[[, ");
+    for (int row = 0; row < rowSize(); row++) {
+      for (int col = 0; col < values[row].length; col++)
+        out.append(getQuick(row, col)).append(", ");
+      out.append("], ");
+    }
+    out.append("] ");
+    return new Text(out.toString());
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#cardinality()
+   */
+  @Override
+  public int[] cardinality() {
+    int[] result = new int[2];
+    result[ROW] = rowSize();
+    result[COL] = columnSize();
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#copy()
+   */
+  @Override
+  public Matrix copy() {
+    return new DenseMatrix(values);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getQuick(int, int)
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    return values[row][column];
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
+   */
+  @Override
+  public boolean haveSharedCells(Matrix other) {
+    if (other instanceof DenseMatrix)
+      return other == this;
+    else
+      return other.haveSharedCells(this);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like()
+   */
+  @Override
+  public Matrix like() {
+    return like(rowSize(), columnSize());
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like(int, int)
+   */
+  @Override
+  public Matrix like(int rows, int columns) {
+    return new DenseMatrix(rows, columns);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
+   *      double)
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    values[row][column] = value;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#size()
+   */
+  @Override
+  public int[] size() {
+    return cardinality();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#toArray()
+   */
+  @Override
+  public double[][] toArray() {
+    DenseMatrix result = new DenseMatrix(values);
+    return result.values;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#viewPart(int[], int[])
+   */
+  @Override
+  public Matrix viewPart(int[] offset, int[] size) throws CardinalityException,
+      IndexException {
+    if (size[ROW] > rowSize() || size[COL] > columnSize())
+      throw new CardinalityException();
+    if (offset[ROW] < 0 || offset[ROW] + size[ROW] > rowSize()
+        || offset[COL] < 0 || offset[COL] + size[COL] > columnSize())
+      throw new IndexException();
+    return new MatrixView(this, offset, size);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignColumn(int column, Vector other)
+      throws CardinalityException {
+    if (other.cardinality() != rowSize() || column >= columnSize())
+      throw new CardinalityException();
+    for (int row = 0; row < rowSize(); row++)
+      values[row][column] = other.getQuick(row);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignRow(int row, Vector other) throws CardinalityException {
+    if (row >= rowSize() || other.cardinality() != columnSize())
+      throw new CardinalityException();
+    values[row] = other.toArray();
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getColumn(int)
+   */
+  @Override
+  public Vector getColumn(int column) throws IndexException {
+    if (column < 0 || column >= columnSize())
+      throw new IndexException();
+    double[] col = new double[rowSize()];
+    for (int row = 0; row < rowSize(); row++)
+      col[row] = values[row][column];
+    return new DenseVector(col);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getRow(int)
+   */
+  @Override
+  public Vector getRow(int row) throws IndexException {
+    if (row < 0 || row >= rowSize())
+      throw new IndexException();
+    return new DenseVector(values[row]);
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseMatrix.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,134 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * Implements vector as an array of doubles
+ */
+public class DenseVector extends AbstractVector {
+  private double[] values;
+
+  /**
+   * Decode a new instance from the formatted string
+   * 
+   * @param formattedString a string produced by the asFormatString method
+   * @return a DenseVector
+   */
+  public static Vector decodeFormat(WritableComparable formattedString) {
+    String[] pts = formattedString.toString().split(",");
+    double[] point = new double[pts.length - 2];
+    for (int i = 1; i < pts.length - 1; i++)
+      point[i - 1] = new Double(pts[i]);
+    return new DenseVector(point);
+  }
+
+  /**
+   * Construct a new instance using provided values
+   * 
+   * @param values
+   */
+  public DenseVector(double[] values) {
+    super();
+    this.values = values.clone();
+  }
+
+  /**
+   * Construct a new instance of the given cardinality
+   * 
+   * @param cardinality
+   */
+  public DenseVector(int cardinality) {
+    super();
+    this.values = new double[cardinality];
+  }
+
+  @Override
+  protected Matrix matrixLike(int rows, int columns) {
+    return new DenseMatrix(rows, columns);
+  }
+
+  @Override
+  public WritableComparable asWritableComparable() {
+    StringBuilder out = new StringBuilder();
+    out.append("[, ");
+    for (int i = 0; i < values.length; i++)
+      out.append(values[i]).append(", ");
+    out.append("] ");
+    return new Text(out.toString());
+  }
+
+  @Override
+  public int cardinality() {
+    return values.length;
+  }
+
+  @Override
+  public Vector copy() {
+    return new DenseVector(values);
+  }
+
+  @Override
+  public double getQuick(int index) {
+    return values[index];
+  }
+
+  @Override
+  public Vector like() {
+    return new DenseVector(cardinality());
+  }
+
+  @Override
+  public Vector like(int cardinality) {
+    return new DenseVector(cardinality);
+  }
+
+  @Override
+  public void setQuick(int index, double value) {
+    values[index] = value;
+  }
+
+  @Override
+  public int size() {
+    return values.length;
+  }
+
+  @Override
+  public double[] toArray() {
+    return values.clone();
+  }
+
+  @Override
+  public Vector viewPart(int offset, int length) throws CardinalityException,
+      IndexException {
+    if (length > values.length)
+      throw new CardinalityException();
+    if (offset < 0 || offset + length > values.length)
+      throw new IndexException();
+    return new VectorView(this, offset, length);
+  }
+
+  @Override
+  public boolean haveSharedCells(Vector other) {
+    if (other instanceof DenseVector)
+      return other == this;
+    else
+      return other.haveSharedCells(this);
+  }
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/DenseVector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/IndexException.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/IndexException.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/IndexException.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/IndexException.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,27 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+/**
+ * Exception thrown when there is an index outside of the cardinality
+ * 
+ */
+public class IndexException extends Exception {
+
+  private static final long serialVersionUID = 1L;
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/IndexException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,308 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.WritableComparable;
+
+
+/**
+ * The basic interface including numerous convenience functions
+ * 
+ */
+public interface Matrix {
+
+  /**
+   * Return a formatted WritableComparable suitable for output
+   * 
+   * @return formatted WritableComparable
+   */
+  WritableComparable asWritableComparable();
+
+  /**
+   * Assign the value to all elements of the receiver
+   * 
+   * @param value a double value
+   * @return the modified receiver
+   */
+  Matrix assign(double value);
+
+  /**
+   * Assign the values to the receiver
+   * 
+   * @param values a double[] of values
+   * @return the modified receiver
+   * @throws CardinalityException if the cardinalities differ
+   */
+  Matrix assign(double[][] values) throws CardinalityException;
+
+  /**
+   * Assign the other vector values to the receiver
+   * 
+   * @param other a Matrix
+   * @return the modified receiver
+   * @throws CardinalityException if the cardinalities differ
+   */
+  Matrix assign(Matrix other) throws CardinalityException;
+
+  /**
+   * Apply the function to each element of the receiver
+   * 
+   * @param function a UnaryFunction to apply
+   * @return the modified receiver
+   */
+  Matrix assign(UnaryFunction function);
+
+  /**
+   * Apply the function to each element of the receiver and the corresponding
+   * element of the other argument
+   * 
+   * @param other a Matrix containing the second arguments to the function
+   * @param function a BinaryFunction to apply
+   * @return the modified receiver
+   * @throws CardinalityException if the cardinalities differ
+   */
+  Matrix assign(Matrix other, BinaryFunction function)
+      throws CardinalityException;
+
+  /**
+   * Assign the other vector values to the column of the receiver
+   * 
+   * @param column the int row to assign
+   * @param other a Vector
+   * 
+   * @return the modified receiver
+   * @throws CardinalityException if the cardinalities differ
+   */
+  Matrix assignColumn(int column, Vector other) throws CardinalityException;
+
+  /**
+   * Assign the other vector values to the row of the receiver
+   * 
+   * @param row the int row to assign
+   * @param other a Vector
+   * 
+   * @return the modified receiver
+   * @throws CardinalityException if the cardinalities differ
+   */
+  Matrix assignRow(int row, Vector other) throws CardinalityException;
+
+  /**
+   * Return the cardinality of the recipient (the maximum number of values)
+   * 
+   * @return an int[2]
+   */
+  int[] cardinality();
+
+  /**
+   * Return a copy of the recipient
+   * 
+   * @return a new Matrix
+   */
+  Matrix copy();
+
+  /**
+   * Return a new matrix containing the values of the recipient divided by the
+   * argument
+   * 
+   * @param x a double value
+   * @return a new Matrix
+   */
+  Matrix divide(double x);
+
+  /**
+   * Return the value at the given indexes
+   * 
+   * @param row an int row index
+   * @param column an int column index
+   * @return the double at the index
+   * @throws IndexException if the index is out of bounds
+   */
+  double get(int row, int column) throws IndexException;
+
+  /**
+   * Return the column at the given index
+   * 
+   * @param column an int column index
+   * @return a Vector at the index
+   * @throws IndexException if the index is out of bounds
+   */
+  Vector getColumn(int column) throws IndexException;
+
+  /**
+   * Return the row at the given index
+   * 
+   * @param row an int row index
+   * @return a Vector at the index
+   * @throws IndexException if the index is out of bounds
+   */
+  Vector getRow(int row) throws IndexException;
+
+  /**
+   * Return the value at the given indexes, without checking bounds
+   * 
+   * @param row an int row index
+   * @param column an int column index
+   * @return the double at the index
+   */
+  double getQuick(int row, int column);
+
+  /**
+   * Return if the other matrix and the receiver share any underlying data cells
+   * 
+   * @param other a Matrix
+   * @return true if the other matrix has common data cells
+   */
+  boolean haveSharedCells(Matrix other);
+
+  /**
+   * Return an empty matrix of the same underlying class as the receiver
+   * 
+   * @return a Matrix
+   */
+  Matrix like();
+
+  /**
+   * Return an empty matrix of the same underlying class as the receiver and of
+   * the given cardinality
+   * 
+   * @param rows the int number of rows
+   * @param columns the int number of columns
+   * @return
+   */
+  Matrix like(int rows, int columns);
+
+  /**
+   * Return a new matrix containing the element by element difference of the
+   * recipient and the argument
+   * 
+   * @param x a Matrix
+   * @return a new Matrix
+   * @throws CardinalityException if the cardinalities differ
+   */
+  Matrix minus(Matrix x) throws CardinalityException;
+
+  /**
+   * Return a new matrix containing the sum of each value of the recipient and
+   * the argument
+   * 
+   * @param x a double
+   * @return a new Matrix
+   */
+  Matrix plus(double x);
+
+  /**
+   * Return a new matrix containing the element by element sum of the recipient
+   * and the argument
+   * 
+   * @param x a Matrix
+   * @return a new Matrix
+   * @throws CardinalityException if the cardinalities differ
+   */
+  Matrix plus(Matrix x) throws CardinalityException;
+
+  /**
+   * Set the value at the given index
+   * 
+   * @param row an int row index into the receiver
+   * @param column an int column index into the receiver
+   * @param value a double value to set
+   * @throws IndexException if the index is out of bounds
+   */
+  void set(int row, int column, double value) throws IndexException;
+
+  /**
+   * Set the value at the given index, without checking bounds
+   * 
+   * @param row an int row index into the receiver
+   * @param column an int column index into the receiver
+   * @param value a double value to set
+   */
+  void setQuick(int row, int column, double value);
+
+  /**
+   * Return the number of values in the recipient
+   * 
+   * @return an int[2] containing [row, column] count
+   */
+  int[] size();
+
+  /**
+   * Return a new matrix containing the product of each value of the recipient
+   * and the argument
+   * 
+   * @param x a double argument
+   * @return a new Matrix
+   */
+  Matrix times(double x);
+
+  /**
+   * Return a new matrix containing the product of the recipient and the
+   * argument
+   * 
+   * @param x a Matrix argument
+   * @return a new Matrix
+   * @throws CardinalityException if the cardinalities are incompatible
+   */
+  Matrix times(Matrix x) throws CardinalityException;
+
+  /**
+   * Return a new matrix that is the transpose of the receiver
+   * 
+   * @return the transpose
+   */
+  Matrix transpose();
+
+  /**
+   * Return the element of the recipient as a double[]
+   * 
+   * @return a double[][]
+   */
+  double[][] toArray();
+
+  /**
+   * Return a new matrix containing the subset of the recipient
+   * 
+   * @param offset an int[2] offset into the receiver
+   * @param size the int[2] size of the desired result
+   * @return a new Matrix
+   * @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
+   */
+  Matrix viewPart(int[] offset, int[] size) throws CardinalityException,
+      IndexException;
+
+  /**
+   * Return the sum of all the elements of the receiver
+   * 
+   * @return a double
+   */
+  double zSum();
+
+  /*
+   * Need stories for these but keeping them here for now.
+   * 
+   */
+  // void getNonZeros(IntArrayList jx, DoubleArrayList values);
+  // void foreachNonZero(IntDoubleFunction f);
+  // double aggregate(DoubleDoubleFunction aggregator, DoubleFunction map);
+  // double aggregate(Matrix other, DoubleDoubleFunction aggregator,
+  // DoubleDoubleFunction map);
+  // NewMatrix assign(Matrix y, DoubleDoubleFunction function, IntArrayList
+  // nonZeroIndexes);
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/Matrix.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,203 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+
+/**
+ * Implements subset view of a Matrix
+ */
+public class MatrixView extends AbstractMatrix {
+
+  private Matrix matrix;
+
+  // the offset into the Matrix
+  private int[] offset;
+
+  // the cardinality of the view
+  private int[] cardinality;
+
+  /**
+   * 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
+   */
+  public MatrixView(Matrix matrix, int[] offset, int[] cardinality) {
+    super();
+    this.matrix = matrix;
+    this.offset = offset;
+    this.cardinality = cardinality;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public WritableComparable asWritableComparable() {
+    StringBuilder out = new StringBuilder();
+    out.append("[[, ");
+    for (int row = offset[ROW]; row < offset[ROW] + cardinality[ROW]; row++) {
+      for (int col = offset[COL]; col < offset[COL] + cardinality[COL]; row++)
+        out.append(getQuick(row, col)).append(", ");
+      out.append("], ");
+    }
+    out.append("] ");
+    return new Text(out.toString());
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#cardinality()
+   */
+  @Override
+  public int[] cardinality() {
+    return cardinality;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#copy()
+   */
+  @Override
+  public Matrix copy() {
+    return new MatrixView(matrix.copy(), offset, cardinality);
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#getQuick(int, int)
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    return matrix.getQuick(offset[ROW] + row, offset[COL] + column);
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#like()
+   */
+  @Override
+  public Matrix like() {
+    return matrix.like();
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#like(int, int)
+   */
+  @Override
+  public Matrix like(int rows, int columns) {
+
+    return matrix.like(rows, columns);
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int, double)
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    matrix.setQuick(offset[ROW] + row, offset[COL] + column, value);
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#size()
+   */
+  @Override
+  public int[] size() {
+    return cardinality;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#toArray()
+   */
+  @Override
+  public double[][] toArray() {
+    double[][] result = new double[cardinality[ROW]][cardinality[COL]];
+    for (int row = ROW; row < cardinality[ROW]; row++)
+      for (int col = ROW; col < cardinality[COL]; col++)
+        result[row][col] = matrix
+            .getQuick(offset[ROW] + row, offset[COL] + col);
+    return result;
+  }
+
+  @Override
+  public Matrix viewPart(int offset[], int[] size) throws CardinalityException,
+      IndexException {
+    if (size[ROW] > cardinality[ROW] || size[COL] > cardinality[COL])
+      throw new CardinalityException();
+    if ((offset[ROW] < ROW || offset[ROW] + size[ROW] > cardinality[ROW])
+        || (offset[COL] < ROW || offset[COL] + size[COL] > cardinality[COL]))
+      throw new IndexException();
+    int[] origin = offset.clone();
+    origin[ROW] += offset[ROW];
+    origin[COL] += offset[COL];
+    Matrix result = new MatrixView(matrix, origin, size);
+    return result;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
+   */
+  @Override
+  public boolean haveSharedCells(Matrix other) {
+    if (other instanceof MatrixView)
+      return other == this || matrix.haveSharedCells(other);
+    else
+      return other.haveSharedCells(matrix);
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int, org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignColumn(int column, Vector other)
+      throws CardinalityException {
+    if (cardinality[ROW] != other.cardinality())
+      throw new CardinalityException();
+    matrix.assignColumn(column + offset[COL], other);
+    return this;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int, org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignRow(int row, Vector other) throws CardinalityException {
+    if (cardinality[COL] != other.cardinality())
+      throw new CardinalityException();
+    matrix.assignRow(row + offset[ROW], other);
+    return this;
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#getColumn(int)
+   */
+  @Override
+  public Vector getColumn(int column) throws IndexException {
+    if (column < 0 || column >= cardinality[COL])
+      throw new IndexException();
+    return matrix.getColumn(column + offset[COL]);
+  }
+
+  /* (non-Javadoc)
+   * @see org.apache.mahout.matrix.AbstractMatrix#getRow(int)
+   */
+  @Override
+  public Vector getRow(int row) throws IndexException {
+    if (row < 0 || row >= cardinality[ROW])
+      throw new IndexException();
+    return matrix.getRow(row + offset[ROW]);
+  }
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/MatrixView.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/NegateFunction.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/NegateFunction.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/NegateFunction.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/NegateFunction.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,27 @@
+package org.apache.mahout.matrix;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+public class NegateFunction implements UnaryFunction {
+
+  public double apply(double arg1) {
+    return -arg1;
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/NegateFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/PlusFunction.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/PlusFunction.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/PlusFunction.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/PlusFunction.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,27 @@
+package org.apache.mahout.matrix;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+public class PlusFunction implements BinaryFunction {
+
+  public double apply(double arg1, double arg2) {
+    return arg1 + arg2;
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/PlusFunction.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,263 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * sparse matrix with general element values whose columns are accessible
+ * quickly. Implemented as a column array of SparseVectors.
+ */
+public class SparseColumnMatrix extends AbstractMatrix {
+
+  int[] cardinality;
+
+  Vector[] columns;
+
+  /**
+   * 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
+   */
+  public SparseColumnMatrix(int[] cardinality, SparseVector[] columns) {
+    super();
+    this.cardinality = cardinality.clone();
+    this.columns = columns.clone();
+    for (int col = 0; col < cardinality[COL]; col++)
+      this.columns[col] = columns[col].copy();
+  }
+
+  /**
+   * Construct a matrix of the given cardinality
+   * 
+   * @param cardinality the int[2] cardinality
+   */
+  public SparseColumnMatrix(int[] cardinality) {
+    super();
+    this.cardinality = cardinality.clone();
+    this.columns = new SparseVector[cardinality[COL]];
+    for (int col = 0; col < cardinality[COL]; col++)
+      this.columns[col] = new SparseVector(cardinality[ROW]);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public WritableComparable asWritableComparable() {
+    StringBuilder out = new StringBuilder();
+    out.append("[[, ");
+    for (int row = 0; row < columns[ROW].size(); row++) {
+      for (int col = 0; col < columns.length; col++)
+        out.append(getQuick(row, col)).append(", ");
+      out.append("], ");
+    }
+    out.append("] ");
+    return new Text(out.toString());
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#cardinality()
+   */
+  @Override
+  public int[] cardinality() {
+    return cardinality;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#copy()
+   */
+  @Override
+  public Matrix copy() {
+    SparseColumnMatrix copy = new SparseColumnMatrix(cardinality);
+    for (int col = 0; col < cardinality[COL]; col++)
+      copy.columns[col] = columns[col].copy();
+    return copy;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getQuick(int, int)
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    if (columns[column] == null)
+      return 0.0;
+    else
+      return columns[column].getQuick(row);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
+   */
+  @Override
+  public boolean haveSharedCells(Matrix other) {
+    if (other instanceof SparseColumnMatrix)
+      return other == this;
+    return other.haveSharedCells(this);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like()
+   */
+  @Override
+  public Matrix like() {
+    return new SparseColumnMatrix(cardinality);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like(int, int)
+   */
+  @Override
+  public Matrix like(int rows, int columns) {
+    int[] c = new int[2];
+    c[ROW] = rows;
+    c[COL] = columns;
+    return new SparseColumnMatrix(c);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
+   *      double)
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    if (columns[column] == null)
+      columns[column] = new SparseVector(cardinality[ROW]);
+    columns[column].setQuick(row, value);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#size()
+   */
+  @Override
+  public int[] size() {
+    int[] result = new int[2];
+    result[COL] = columns.length;
+    for (int col = 0; col < cardinality[COL]; col++)
+      result[ROW] = Math.max(result[ROW], columns[col].size());
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#toArray()
+   */
+  @Override
+  public double[][] toArray() {
+    double[][] result = new double[cardinality[ROW]][cardinality[COL]];
+    for (int row = 0; row < cardinality[ROW]; row++)
+      for (int col = 0; col < cardinality[COL]; col++)
+        result[row][col] = getQuick(row, col);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#viewPart(int[], int[])
+   */
+  @Override
+  public Matrix viewPart(int[] offset, int[] size) throws CardinalityException,
+      IndexException {
+    if (size[COL] > columns.length || size[ROW] > columns[COL].cardinality())
+      throw new CardinalityException();
+    if (offset[COL] < 0 || offset[COL] + size[COL] > columns.length
+        || offset[ROW] < 0
+        || offset[ROW] + size[ROW] > columns[COL].cardinality())
+      throw new IndexException();
+    return new MatrixView(this, offset, size);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignColumn(int column, Vector other)
+      throws CardinalityException {
+    if (other.cardinality() != cardinality[ROW] || column >= cardinality[COL])
+      throw new CardinalityException();
+    columns[column].assign(other);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignRow(int row, Vector other) throws CardinalityException {
+    if (row >= cardinality[ROW] || other.cardinality() != cardinality[COL])
+      throw new CardinalityException();
+    for (int col = 0; col < cardinality[COL]; col++)
+      columns[col].setQuick(row, other.getQuick(col));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getColumn(int)
+   */
+  @Override
+  public Vector getColumn(int column) throws IndexException {
+    if (column < 0 || column >= cardinality[COL])
+      throw new IndexException();
+    return columns[column];
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getRow(int)
+   */
+  @Override
+  public Vector getRow(int row) throws IndexException {
+    if (row < 0 || row >= cardinality[ROW])
+      throw new IndexException();
+    double[] d = new double[cardinality[COL]];
+    for (int col = 0; col < cardinality[COL]; col++)
+      d[col] = getQuick(row, col);
+    return new DenseVector(d);
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseColumnMatrix.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseMatrix.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,272 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * Doubly sparse matrix. Implemented as a Map of SparseVector rows
+ */
+public class SparseMatrix extends AbstractMatrix {
+
+  int[] cardinality;
+
+  private Map<Integer, Vector> rows;
+
+  /**
+   * 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
+   */
+  public SparseMatrix(int[] cardinality, Map<Integer, SparseVector> rows) {
+    this.cardinality = cardinality.clone();
+    this.rows = new HashMap<Integer, Vector>();
+    for (Integer row : rows.keySet())
+      this.rows.put(row, (SparseVector) rows.get(row).copy());
+  }
+
+  /**
+   * Construct a matrix of the given cardinality
+   * 
+   * @param cardinality the int[2] cardinality desired
+   */
+  public SparseMatrix(int[] cardinality) {
+    super();
+    this.cardinality = cardinality.clone();
+    this.rows = new HashMap<Integer, Vector>();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public WritableComparable asWritableComparable() {
+    StringBuilder out = new StringBuilder();
+    out.append("[s" + cardinality[ROW] + ", ");
+    for (Integer row : rows.keySet())
+      out.append(rows.get(row).asWritableComparable());
+    out.append("] ");
+    return new Text(out.toString());
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#cardinality()
+   */
+  @Override
+  public int[] cardinality() {
+    return cardinality;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#copy()
+   */
+  @Override
+  public Matrix copy() {
+    SparseMatrix copy = new SparseMatrix(cardinality);
+    for (Integer row : rows.keySet())
+      copy.rows.put(row, (SparseVector) rows.get(row).copy());
+    return copy;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getQuick(int, int)
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    Vector r = rows.get(row);
+    if (r == null)
+      return 0.0;
+    else
+      return r.getQuick(column);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
+   */
+  @Override
+  public boolean haveSharedCells(Matrix other) {
+    if (other instanceof SparseMatrix)
+      return other == this;
+    else
+      return other.haveSharedCells(this);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like()
+   */
+  @Override
+  public Matrix like() {
+    return new SparseMatrix(cardinality);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like(int, int)
+   */
+  @Override
+  public Matrix like(int rows, int columns) {
+    int[] c = new int[2];
+    c[ROW] = rows;
+    c[COL] = columns;
+    return new SparseMatrix(c);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
+   *      double)
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    if (rows.get(row) == null)
+      rows.put(row, new SparseVector(cardinality[COL]));
+    rows.get(row).setQuick(column, value);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#size()
+   */
+  @Override
+  public int[] size() {
+    int[] result = new int[2];
+    result[ROW] = rows.size();
+    for (Integer row : rows.keySet())
+      result[COL] = Math.max(result[COL], rows.get(row).size());
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#toArray()
+   */
+  @Override
+  public double[][] toArray() {
+    double[][] result = new double[cardinality[ROW]][cardinality[COL]];
+    for (int row = 0; row < cardinality[ROW]; row++)
+      for (int col = 0; col < cardinality[COL]; col++)
+        result[row][col] = getQuick(row, col);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#viewPart(int[], int[])
+   */
+  @Override
+  public Matrix viewPart(int[] offset, int[] size) throws CardinalityException,
+      IndexException {
+    if (size[ROW] > cardinality[ROW] || size[COL] > cardinality[COL])
+      throw new CardinalityException();
+    if (offset[ROW] < 0 || offset[ROW] + size[ROW] > cardinality[ROW]
+        || offset[COL] < 0 || offset[COL] + size[COL] > cardinality[COL])
+      throw new IndexException();
+    return new MatrixView(this, offset, size);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignColumn(int column, Vector other)
+      throws CardinalityException {
+    if (other.cardinality() != cardinality[ROW] || column >= cardinality[COL])
+      throw new CardinalityException();
+    for (int row = 0; row < cardinality[ROW]; row++) {
+      double val = other.getQuick(row);
+      if (val != 0.0) {
+        Vector r = rows.get(row);
+        if (r == null) {
+          r = new SparseVector(cardinality[ROW]);
+          rows.put(row, r);
+        }
+        r.setQuick(column, val);
+      }
+    }
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignRow(int row, Vector other) throws CardinalityException {
+    if (row >= cardinality[ROW] || other.cardinality() != cardinality[COL])
+      throw new CardinalityException();
+    rows.put(new Integer(row), other);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getColumn(int)
+   */
+  @Override
+  public Vector getColumn(int column) throws IndexException {
+    if (column < 0 || column >= cardinality[COL])
+      throw new IndexException();
+    double[] d = new double[cardinality[ROW]];
+    for (int row = 0; row < cardinality[ROW]; row++)
+      d[row] = getQuick(row, column);
+    return new DenseVector(d);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getRow(int)
+   */
+  @Override
+  public Vector getRow(int row) throws IndexException {
+    if (row < 0 || row >= cardinality[ROW])
+      throw new IndexException();
+    Vector res = rows.get(row);
+    if (res == null)
+      res = new SparseVector(cardinality[ROW]);
+    return res;
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseMatrix.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,259 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * sparse matrix with general element values whose rows are accessible quickly.
+ * Implemented as a row array of SparseVectors.
+ */
+public class SparseRowMatrix extends AbstractMatrix {
+
+  int[] cardinality;
+
+  Vector[] rows;
+
+  /**
+   * 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
+   */
+  public SparseRowMatrix(int[] cardinality, SparseVector[] rows) {
+    this.cardinality = cardinality.clone();
+    this.rows = rows.clone();
+    for (int row = 0; row < cardinality[ROW]; row++)
+      this.rows[row] = rows[row].copy();
+  }
+
+  /**
+   * Construct a matrix of the given cardinality
+   * 
+   * @param cardinality the int[2] cardinality desired
+   */
+  public SparseRowMatrix(int[] cardinality) {
+    super();
+    this.cardinality = cardinality.clone();
+    this.rows = new SparseVector[cardinality[ROW]];
+    for (int row = 0; row < cardinality[ROW]; row++)
+      this.rows[row] = new SparseVector(cardinality[COL]);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#asFormatString()
+   */
+  @Override
+  public WritableComparable asWritableComparable() {
+    StringBuilder out = new StringBuilder();
+    out.append("[[, ");
+    for (int row = 0; row < rows.length; row++) {
+      for (int col = 0; col < rows[ROW].size(); col++)
+        out.append(getQuick(row, col)).append(", ");
+      out.append("], ");
+    }
+    out.append("] ");
+    return new Text(out.toString());
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#cardinality()
+   */
+  @Override
+  public int[] cardinality() {
+    return cardinality;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#copy()
+   */
+  @Override
+  public Matrix copy() {
+    SparseRowMatrix copy = new SparseRowMatrix(cardinality);
+    for (int row = 0; row < cardinality[ROW]; row++)
+      copy.rows[row] = rows[row].copy();
+    return copy;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getQuick(int, int)
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    if (rows[row] == null)
+      return 0.0;
+    else
+      return rows[row].getQuick(column);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#haveSharedCells(org.apache.mahout.matrix.Matrix)
+   */
+  @Override
+  public boolean haveSharedCells(Matrix other) {
+    if (other instanceof SparseRowMatrix)
+      return other == this;
+    return other.haveSharedCells(this);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like()
+   */
+  @Override
+  public Matrix like() {
+    return new SparseRowMatrix(cardinality);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#like(int, int)
+   */
+  @Override
+  public Matrix like(int rows, int columns) {
+    int[] c = new int[2];
+    c[ROW] = rows;
+    c[COL] = columns;
+    return new SparseRowMatrix(c);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#setQuick(int, int,
+   *      double)
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    rows[row].setQuick(column, value);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#size()
+   */
+  @Override
+  public int[] size() {
+    int[] result = new int[2];
+    result[ROW] = rows.length;
+    for (int row = 0; row < cardinality[ROW]; row++)
+      result[COL] = Math.max(result[COL], rows[row].size());
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#toArray()
+   */
+  @Override
+  public double[][] toArray() {
+    double[][] result = new double[cardinality[ROW]][cardinality[COL]];
+    for (int row = 0; row < cardinality[ROW]; row++)
+      for (int col = 0; col < cardinality[COL]; col++)
+        result[row][col] = getQuick(row, col);
+    return result;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#viewPart(int[], int[])
+   */
+  @Override
+  public Matrix viewPart(int[] offset, int[] size) throws CardinalityException,
+      IndexException {
+    if (size[ROW] > rows.length || size[COL] > rows[ROW].cardinality())
+      throw new CardinalityException();
+    if (offset[ROW] < 0 || offset[ROW] + size[ROW] > rows.length
+        || offset[COL] < 0 || offset[COL] + size[COL] > rows[ROW].cardinality())
+      throw new IndexException();
+    return new MatrixView(this, offset, size);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignColumn(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignColumn(int column, Vector other)
+      throws CardinalityException {
+    if (other.cardinality() != cardinality[ROW] || column >= cardinality[COL])
+      throw new CardinalityException();
+    for (int row = 0; row < cardinality[ROW]; row++)
+      rows[row].setQuick(column, other.getQuick(row));
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#assignRow(int,
+   *      org.apache.mahout.vector.Vector)
+   */
+  @Override
+  public Matrix assignRow(int row, Vector other) throws CardinalityException {
+    if (row >= cardinality[ROW] || other.cardinality() != cardinality[COL])
+      throw new CardinalityException();
+    rows[row].assign(other);
+    return this;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getColumn(int)
+   */
+  @Override
+  public Vector getColumn(int column) throws IndexException {
+    if (column < 0 || column >= cardinality[COL])
+      throw new IndexException();
+    double[] d = new double[cardinality[ROW]];
+    for (int row = 0; row < cardinality[ROW]; row++)
+      d[row] = getQuick(row, column);
+    return new DenseVector(d);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.mahout.matrix.AbstractMatrix#getRow(int)
+   */
+  @Override
+  public Vector getRow(int row) throws IndexException {
+    if (row < 0 || row >= cardinality[ROW])
+      throw new IndexException();
+    return rows[row];
+  }
+
+}

Propchange: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseRowMatrix.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java?rev=637664&view=auto
==============================================================================
--- lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java (added)
+++ lucene/mahout/trunk/src/main/java/org/apache/mahout/matrix/SparseVector.java Sun Mar 16 14:24:48 2008
@@ -0,0 +1,151 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.mahout.matrix;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.WritableComparable;
+
+/**
+ * Implements vector that only stores non-zero doubles
+ * 
+ */
+public class SparseVector extends AbstractVector {
+
+  private Map<Integer, Double> values = new HashMap<Integer, Double>();
+
+  private int cardinality;
+
+  /**
+   * Decode a new instance from the formatted string
+   * 
+   * @param formattedString a string produced by the asFormatString method
+   * @return a DenseVector
+   */
+  public static Vector decodeFormat(WritableComparable formattedString) {
+    String[] pts = formattedString.toString().split(",");
+    SparseVector result = null;
+    for (int i = 0; i < pts.length; i++) {
+      String pt = pts[i].trim();
+      if (pt.startsWith("[s")) {
+        int c = new Integer(pts[i].substring(2));
+        result = new SparseVector(c);
+      } else if (!pt.startsWith("]")) {
+        int ix = pt.indexOf(':');
+        Integer index = new Integer(pt.substring(0, ix).trim());
+        Double value = new Double(pt.substring(ix + 1));
+        result.setQuick(index, value);
+      }
+    }
+    return result;
+  }
+
+  public SparseVector(int cardinality) {
+    super();
+    this.cardinality = cardinality;
+  }
+
+  @Override
+  protected Matrix matrixLike(int rows, int columns) {
+    int[] cardinality = { rows, columns };
+    return new SparseRowMatrix(cardinality);
+  }
+
+  @Override
+  public WritableComparable asWritableComparable() {
+    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());
+  }
+
+  @Override
+  public int cardinality() {
+    return cardinality;
+  }
+
+  @Override
+  public Vector copy() {
+    Vector result = like();
+    for (Integer index : values.keySet())
+      result.setQuick(index, values.get(index));
+    return result;
+  }
+
+  @Override
+  public double getQuick(int index) {
+    Double value = values.get(index);
+    if (value == null)
+      return 0;
+    else
+      return value;
+  }
+
+  @Override
+  public void setQuick(int index, double value) {
+    if (value == 0)
+      values.remove(index);
+    else
+      values.put(index, value);
+  }
+
+  @Override
+  public int size() {
+    return values.size();
+  }
+
+  @Override
+  public double[] toArray() {
+    double[] result = new double[cardinality];
+    for (int i = 0; i < cardinality; i++)
+      result[i] = getQuick(i);
+    return result;
+  }
+
+  @Override
+  public Vector viewPart(int offset, int length) throws CardinalityException,
+      IndexException {
+    if (length > cardinality)
+      throw new CardinalityException();
+    if (offset < 0 || offset + length > cardinality)
+      throw new IndexException();
+    return new VectorView(this, offset, length);
+  }
+
+  @Override
+  public boolean haveSharedCells(Vector other) {
+    if (other instanceof SparseVector)
+      return other == this;
+    else
+      return other.haveSharedCells(this);
+  }
+
+  @Override
+  public Vector like() {
+    return new SparseVector(cardinality);
+  }
+
+  @Override
+  public Vector like(int newCardinality) {
+    return new SparseVector(newCardinality);
+  }
+
+}