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 2009/12/18 00:22:41 UTC

svn commit: r891983 [37/47] - in /lucene/mahout/trunk: ./ core/ core/src/main/java/org/apache/mahout/cf/taste/hadoop/item/ core/src/main/java/org/apache/mahout/clustering/ core/src/main/java/org/apache/mahout/clustering/canopy/ core/src/main/java/org/a...

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseObjectMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseObjectMatrix3D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseObjectMatrix3D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/DenseObjectMatrix3D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,284 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.matrix.impl;
+
+import org.apache.mahout.math.matrix.ObjectMatrix2D;
+import org.apache.mahout.math.matrix.ObjectMatrix3D;
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class DenseObjectMatrix3D extends ObjectMatrix3D {
+
+  /**
+   * The elements of this matrix. elements are stored in slice major, then row major, then column major, in order of
+   * significance, i.e. index==slice*sliceStride+ row*rowStride + column*columnStride i.e. {slice0 row0..m}, {slice1
+   * row0..m}, ..., {sliceN row0..m} with each row storead as {row0 column0..m}, {row1 column0..m}, ..., {rown
+   * column0..m}
+   */
+  protected Object[] elements;
+
+  /**
+   * Constructs a matrix with a copy of the given values. <tt>values</tt> is required to have the form
+   * <tt>values[slice][row][column]</tt> and have exactly the same number of rows in in every slice and exactly the same
+   * number of columns in in every row. <p> The values are copied. So subsequent changes in <tt>values</tt> are not
+   * reflected in the matrix, and vice-versa.
+   *
+   * @param values The values to be filled into the new matrix.
+   * @throws IllegalArgumentException if <tt>for any 1 &lt;= slice &lt; values.length: values[slice].length !=
+   *                                  values[slice-1].length</tt>.
+   * @throws IllegalArgumentException if <tt>for any 1 &lt;= row &lt; values[0].length: values[slice][row].length !=
+   *                                  values[slice][row-1].length</tt>.
+   */
+  public DenseObjectMatrix3D(Object[][][] values) {
+    this(values.length, (values.length == 0 ? 0 : values[0].length),
+        (values.length == 0 ? 0 : values[0].length == 0 ? 0 : values[0][0].length));
+    assign(values);
+  }
+
+  /**
+   * Constructs a matrix with a given number of slices, rows and columns. All entries are initially <tt>0</tt>.
+   *
+   * @param slices  the number of slices the matrix shall have.
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @throws IllegalArgumentException if <tt>(Object)slices*columns*rows > Integer.MAX_VALUE</tt>.
+   * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+   */
+  public DenseObjectMatrix3D(int slices, int rows, int columns) {
+    setUp(slices, rows, columns);
+    this.elements = new Object[slices * rows * columns];
+  }
+
+  /**
+   * Constructs a view with the given parameters.
+   *
+   * @param slices       the number of slices the matrix shall have.
+   * @param rows         the number of rows the matrix shall have.
+   * @param columns      the number of columns the matrix shall have.
+   * @param elements     the cells.
+   * @param sliceZero    the position of the first element.
+   * @param rowZero      the position of the first element.
+   * @param columnZero   the position of the first element.
+   * @param sliceStride  the number of elements between two slices, i.e. <tt>index(k+1,i,j)-index(k,i,j)</tt>.
+   * @param rowStride    the number of elements between two rows, i.e. <tt>index(k,i+1,j)-index(k,i,j)</tt>.
+   * @param columnStride the number of elements between two columns, i.e. <tt>index(k,i,j+1)-index(k,i,j)</tt>.
+   * @throws IllegalArgumentException if <tt>(Object)slices*columns*rows > Integer.MAX_VALUE</tt>.
+   * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+   */
+  protected DenseObjectMatrix3D(int slices, int rows, int columns, Object[] elements, int sliceZero, int rowZero,
+                                int columnZero, int sliceStride, int rowStride, int columnStride) {
+    setUp(slices, rows, columns, sliceZero, rowZero, columnZero, sliceStride, rowStride, columnStride);
+    this.elements = elements;
+    this.isNoView = false;
+  }
+
+  /**
+   * Sets all cells to the state specified by <tt>values</tt>. <tt>values</tt> is required to have the form
+   * <tt>values[slice][row][column]</tt> and have exactly the same number of slices, rows and columns as the receiver.
+   * <p> The values are copied. So subsequent changes in <tt>values</tt> are not reflected in the matrix, and
+   * vice-versa.
+   *
+   * @param values the values to be filled into the cells.
+   * @return <tt>this</tt> (for convenience only).
+   * @throws IllegalArgumentException if <tt>values.length != slices() || for any 0 &lt;= slice &lt; slices():
+   *                                  values[slice].length != rows()</tt>.
+   * @throws IllegalArgumentException if <tt>for any 0 &lt;= column &lt; columns(): values[slice][row].length !=
+   *                                  columns()</tt>.
+   */
+  @Override
+  public ObjectMatrix3D assign(Object[][][] values) {
+    if (this.isNoView) {
+      if (values.length != slices) {
+        throw new IllegalArgumentException(
+            "Must have same number of slices: slices=" + values.length + "slices()=" + slices());
+      }
+      int i = slices * rows * columns - columns;
+      for (int slice = slices; --slice >= 0;) {
+        Object[][] currentSlice = values[slice];
+        if (currentSlice.length != rows) {
+          throw new IllegalArgumentException(
+              "Must have same number of rows in every slice: rows=" + currentSlice.length + "rows()=" + rows());
+        }
+        for (int row = rows; --row >= 0;) {
+          Object[] currentRow = currentSlice[row];
+          if (currentRow.length != columns) {
+            throw new IllegalArgumentException(
+                "Must have same number of columns in every row: columns=" + currentRow.length + "columns()=" +
+                    columns());
+          }
+          System.arraycopy(currentRow, 0, this.elements, i, columns);
+          i -= columns;
+        }
+      }
+    } else {
+      super.assign(values);
+    }
+    return this;
+  }
+
+  /**
+   * Replaces all cell values of the receiver with the values of another matrix. Both matrices must have the same number
+   * of slices, rows and columns. If both matrices share the same cells (as is the case if they are views derived from
+   * the same matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep
+   * copy of <tt>other</tt>.
+   *
+   * @param source the source matrix to copy from (may be identical to the receiver).
+   * @return <tt>this</tt> (for convenience only).
+   * @throws IllegalArgumentException if <tt>slices() != source.slices() || rows() != source.rows() || columns() !=
+   *                                  source.columns()</tt>
+   */
+  @Override
+  public ObjectMatrix3D assign(ObjectMatrix3D source) {
+    // overriden for performance only
+    if (!(source instanceof DenseObjectMatrix3D)) {
+      return super.assign(source);
+    }
+    DenseObjectMatrix3D other = (DenseObjectMatrix3D) source;
+    if (other == this) {
+      return this;
+    }
+    checkShape(other);
+    if (haveSharedCells(other)) {
+      ObjectMatrix3D c = other.copy();
+      if (!(c instanceof DenseObjectMatrix3D)) { // should not happen
+        return super.assign(source);
+      }
+      other = (DenseObjectMatrix3D) c;
+    }
+
+    if (this.isNoView && other.isNoView) { // quickest
+      System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
+      return this;
+    }
+    return super.assign(other);
+  }
+
+  /**
+   * Returns the matrix cell value at coordinate <tt>[slice,row,column]</tt>.
+   *
+   * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>slice&lt;0 || slice&gt;=slices() || row&lt;0 || row&gt;=rows() || column&lt;0 ||
+   * column&gt;=column()</tt>.
+   *
+   * @param slice  the index of the slice-coordinate.
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @return the value at the specified coordinate.
+   */
+  @Override
+  public Object getQuick(int slice, int row, int column) {
+    //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
+    //return elements[index(slice,row,column)];
+    //manually inlined:
+    return elements[sliceZero + slice * sliceStride + rowZero + row * rowStride + columnZero + column * columnStride];
+  }
+
+  /**
+   * Returns <tt>true</tt> if both matrices share common cells. More formally, returns <tt>true</tt> if <tt>other !=
+   * null</tt> and at least one of the following conditions is met <ul> <li>the receiver is a view of the other matrix
+   * <li>the other matrix is a view of the receiver <li><tt>this == other</tt> </ul>
+   */
+  @Override
+  protected boolean haveSharedCellsRaw(ObjectMatrix3D other) {
+    if (other instanceof SelectedDenseObjectMatrix3D) {
+      SelectedDenseObjectMatrix3D otherMatrix = (SelectedDenseObjectMatrix3D) other;
+      return this.elements == otherMatrix.elements;
+    } else if (other instanceof DenseObjectMatrix3D) {
+      DenseObjectMatrix3D otherMatrix = (DenseObjectMatrix3D) other;
+      return this.elements == otherMatrix.elements;
+    }
+    return false;
+  }
+
+  /**
+   * Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array.
+   *
+   * @param slice  the index of the slice-coordinate.
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the third-coordinate.
+   */
+  @Override
+  protected int index(int slice, int row, int column) {
+    //return _sliceOffset(_sliceRank(slice)) + _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
+    //manually inlined:
+    return sliceZero + slice * sliceStride + rowZero + row * rowStride + columnZero + column * columnStride;
+  }
+
+  /**
+   * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified
+   * number of slices, rows and columns. For example, if the receiver is an instance of type
+   * <tt>DenseObjectMatrix3D</tt> the new matrix must also be of type <tt>DenseObjectMatrix3D</tt>, if the receiver is
+   * an instance of type <tt>SparseObjectMatrix3D</tt> the new matrix must also be of type
+   * <tt>SparseObjectMatrix3D</tt>, etc. In general, the new matrix should have internal parametrization as similar as
+   * possible.
+   *
+   * @param slices  the number of slices the matrix shall have.
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @return a new empty matrix of the same dynamic type.
+   */
+  @Override
+  public ObjectMatrix3D like(int slices, int rows, int columns) {
+    return new DenseObjectMatrix3D(slices, rows, columns);
+  }
+
+  /**
+   * Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, sharing the same cells. For
+   * example, if the receiver is an instance of type <tt>DenseObjectMatrix3D</tt> the new matrix must also be of type
+   * <tt>DenseObjectMatrix2D</tt>, if the receiver is an instance of type <tt>SparseObjectMatrix3D</tt> the new matrix
+   * must also be of type <tt>SparseObjectMatrix2D</tt>, etc.
+   *
+   * @param rows         the number of rows the matrix shall have.
+   * @param columns      the number of columns the matrix shall have.
+   * @param rowZero      the position of the first element.
+   * @param columnZero   the position of the first element.
+   * @param rowStride    the number of elements between two rows, i.e. <tt>index(i+1,j)-index(i,j)</tt>.
+   * @param columnStride the number of elements between two columns, i.e. <tt>index(i,j+1)-index(i,j)</tt>.
+   * @return a new matrix of the corresponding dynamic type.
+   */
+  @Override
+  protected ObjectMatrix2D like2D(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) {
+    return new DenseObjectMatrix2D(rows, columns, this.elements, rowZero, columnZero, rowStride, columnStride);
+  }
+
+  /**
+   * Sets the matrix cell at coordinate <tt>[slice,row,column]</tt> to the specified value.
+   *
+   * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>slice&lt;0 || slice&gt;=slices() || row&lt;0 || row&gt;=rows() || column&lt;0 ||
+   * column&gt;=column()</tt>.
+   *
+   * @param slice  the index of the slice-coordinate.
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @param value  the value to be filled into the specified cell.
+   */
+  @Override
+  public void setQuick(int slice, int row, int column, Object value) {
+    //if (debug) if (slice<0 || slice>=slices || row<0 || row>=rows || column<0 || column>=columns) throw new IndexOutOfBoundsException("slice:"+slice+", row:"+row+", column:"+column);
+    //elements[index(slice,row,column)] = value;
+    //manually inlined:
+    elements[sliceZero + slice * sliceStride + rowZero + row * rowStride + columnZero + column * columnStride] = value;
+  }
+
+  /**
+   * Construct and returns a new selection view.
+   *
+   * @param sliceOffsets  the offsets of the visible elements.
+   * @param rowOffsets    the offsets of the visible elements.
+   * @param columnOffsets the offsets of the visible elements.
+   * @return a new view.
+   */
+  @Override
+  protected ObjectMatrix3D viewSelectionLike(int[] sliceOffsets, int[] rowOffsets, int[] columnOffsets) {
+    return new SelectedDenseObjectMatrix3D(this.elements, sliceOffsets, rowOffsets, columnOffsets, 0);
+  }
+}

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

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/Former.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/Former.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/Former.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/Former.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,31 @@
+/*
+Copyright 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.matrix.impl;
+
+/**
+ * Formats a double into a string (like sprintf in C).
+ *
+ * @see java.util.Comparator
+ * @see org.apache.mahout.colt
+ * @see org.apache.mahout.math.Sorting
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public interface Former {
+
+  /**
+   * Formats a double into a string (like sprintf in C).
+   *
+   * @param value the number to format
+   * @return the formatted string
+   * @throws IllegalArgumentException if bad argument
+   */
+  String form(double value);
+}

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

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/FormerFactory.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/FormerFactory.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/FormerFactory.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/FormerFactory.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,44 @@
+package org.apache.mahout.math.matrix.impl;
+
+
+/**
+ * Factory producing implementations of {@link org.apache.mahout.math.matrix.impl.Former} via method create();
+ * Implementations of can use existing libraries such as PrintfFormat or Format or other.
+ * Serves to isolate the interface of String formatting from the actual implementation.
+ * If you want to plug in a different String formatting implementation, simply replace this class with your alternative.
+ *
+ */
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class FormerFactory {
+
+  /**
+   * Constructs and returns a new format instance.
+   *
+   * @param format the format string following printf conventions. The string has a prefix, a format code and a suffix.
+   *               The prefix and suffix become part of the formatted output. The format code directs the formatting of
+   *               the (single) parameter to be formatted. The code has the following structure <ul> <li> a % (required)
+   *               <li> a modifier (optional) <dl> <dt> + <dd> forces display of + for positive numbers <dt> 0 <dd> show
+   *               leading zeroes <dt> - <dd> align left in the field <dt> space <dd> prepend a space in front of
+   *               positive numbers <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal numbers.
+   *               Don't suppress trailing zeroes in general floating point format. </dl> <li> an integer denoting field
+   *               width (optional) <li> a period followed by an integer denoting precision (optional) <li> a format
+   *               descriptor (required) <dl> <dt>f <dd> floating point number in fixed format <dt>e, E <dd> floating
+   *               point number in exponential notation (scientific format). The E format results in an uppercase E for
+   *               the exponent (1.14130E+003), the e format in a lowercase e. <dt>g, G <dd> floating point number in
+   *               general format (fixed format for small numbers, exponential format for large numbers). Trailing
+   *               zeroes are suppressed. The G format results in an uppercase E for the exponent (if any), the g format
+   *               in a lowercase e. <dt>d, i <dd> integer in decimal <dt>x <dd> integer in hexadecimal <dt>o <dd>
+   *               integer in octal <dt>s <dd> string <dt>c <dd> character </dl> </ul>
+   * @throws IllegalArgumentException if bad format
+   */
+  public Former create(String format) {
+    return new Former() {
+      @Override
+      public String form(double value) {
+        return String.valueOf(value);
+      }
+    };
+  }
+}

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

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCDoubleMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCDoubleMatrix2D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCDoubleMatrix2D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCDoubleMatrix2D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,522 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.matrix.impl;
+
+import org.apache.mahout.math.function.DoubleFunction;
+import org.apache.mahout.math.function.IntIntDoubleFunction;
+import org.apache.mahout.math.jet.math.Functions;
+import org.apache.mahout.math.jet.math.Mult;
+import org.apache.mahout.math.jet.math.PlusMult;
+import org.apache.mahout.math.list.DoubleArrayList;
+import org.apache.mahout.math.list.IntArrayList;
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/** @deprecated until unit tests are in place.  Until this time, this class/interface is unsupported. */
+@Deprecated
+public class RCDoubleMatrix2D extends WrapperDoubleMatrix2D {
+  /*
+   * The elements of the matrix.
+   */
+  private IntArrayList indexes;
+  private DoubleArrayList values;
+  private int[] starts;
+
+  //protected int N;
+
+  /**
+   * Constructs a matrix with a copy of the given values. <tt>values</tt> is required to have the form
+   * <tt>values[row][column]</tt> and have exactly the same number of columns in every row. <p> The values are copied.
+   * So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa.
+   *
+   * @param values The values to be filled into the new matrix.
+   * @throws IllegalArgumentException if <tt>for any 1 &lt;= row &lt; values.length: values[row].length !=
+   *                                  values[row-1].length</tt>.
+   */
+  public RCDoubleMatrix2D(double[][] values) {
+    this(values.length, values.length == 0 ? 0 : values[0].length);
+    assign(values);
+  }
+
+  /**
+   * Constructs a matrix with a given number of rows and columns. All entries are initially <tt>0</tt>.
+   *
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt>.
+   */
+  public RCDoubleMatrix2D(int rows, int columns) {
+    super(null);
+    try {
+      setUp(rows, columns);
+    }
+    catch (IllegalArgumentException exc) { // we can hold rows*columns>Integer.MAX_VALUE cells !
+      if (!"matrix too large".equals(exc.getMessage())) {
+        throw exc;
+      }
+    }
+    indexes = new IntArrayList();
+    values = new DoubleArrayList();
+    starts = new int[rows + 1];
+  }
+
+  /**
+   * Sets all cells to the state specified by <tt>value</tt>.
+   *
+   * @param value the value to be filled into the cells.
+   * @return <tt>this</tt> (for convenience only).
+   */
+  @Override
+  public DoubleMatrix2D assign(double value) {
+    // overriden for performance only
+    if (value == 0) {
+      indexes.clear();
+      values.clear();
+      for (int i = starts.length; --i >= 0;) {
+        starts[i] = 0;
+      }
+    } else {
+      super.assign(value);
+    }
+    return this;
+  }
+
+  @Override
+  public DoubleMatrix2D assign(DoubleFunction function) {
+    if (function instanceof Mult) { // x[i] = mult*x[i]
+      double alpha = ((Mult) function).getMultiplicator();
+      if (alpha == 1) {
+        return this;
+      }
+      if (alpha == 0) {
+        return assign(0);
+      }
+      if (alpha != alpha) {
+        return assign(alpha);
+      } // the funny definition of isNaN(). This should better not happen.
+
+      double[] vals = values.elements();
+      for (int j = values.size(); --j >= 0;) {
+        vals[j] *= alpha;
+      }
+
+      /*
+      forEachNonZero(
+        new IntIntDoubleFunction() {
+          public double apply(int i, int j, double value) {
+            return function.apply(value);
+          }
+        }
+      );
+      */
+    } else {
+      super.assign(function);
+    }
+    return this;
+  }
+
+  /**
+   * Replaces all cell values of the receiver with the values of another matrix. Both matrices must have the same number
+   * of rows and columns. If both matrices share the same cells (as is the case if they are views derived from the same
+   * matrix) and intersect in an ambiguous way, then replaces <i>as if</i> using an intermediate auxiliary deep copy of
+   * <tt>other</tt>.
+   *
+   * @param source the source matrix to copy from (may be identical to the receiver).
+   * @return <tt>this</tt> (for convenience only).
+   * @throws IllegalArgumentException if <tt>columns() != source.columns() || rows() != source.rows()</tt>
+   */
+  @Override
+  public DoubleMatrix2D assign(DoubleMatrix2D source) {
+    if (source == this) {
+      return this;
+    } // nothing to do
+    checkShape(source);
+    // overriden for performance only
+    if (!(source instanceof RCDoubleMatrix2D)) {
+      //return super.assign(source);
+
+      assign(0);
+      source.forEachNonZero(
+          new IntIntDoubleFunction() {
+            @Override
+            public double apply(int i, int j, double value) {
+              setQuick(i, j, value);
+              return value;
+            }
+          }
+      );
+      /*
+      indexes.clear();
+      values.clear();
+      int nonZeros=0;
+      for (int row=0; row<rows; row++) {
+        starts[row]=nonZeros;
+        for (int column=0; column<columns; column++) {
+          double v = source.getQuick(row,column);
+          if (v!=0) {
+            values.add(v);
+            indexes.add(column);
+            nonZeros++;
+          }
+        }
+      }
+      starts[rows]=nonZeros;
+      */
+      return this;
+    }
+
+    // even quicker
+    RCDoubleMatrix2D other = (RCDoubleMatrix2D) source;
+
+    System.arraycopy(other.starts, 0, this.starts, 0, this.starts.length);
+    int s = other.indexes.size();
+    this.indexes.setSize(s);
+    this.values.setSize(s);
+    this.indexes.replaceFromToWithFrom(0, s - 1, other.indexes, 0);
+    this.values.replaceFromToWithFrom(0, s - 1, other.values, 0);
+
+    return this;
+  }
+
+  @Override
+  public DoubleMatrix2D assign(DoubleMatrix2D y, org.apache.mahout.math.function.DoubleDoubleFunction function) {
+    checkShape(y);
+
+    if (function instanceof PlusMult) { // x[i] = x[i] + alpha*y[i]
+      final double alpha = ((PlusMult) function).getMultiplicator();
+      if (alpha == 0) {
+        return this;
+      } // nothing to do
+      y.forEachNonZero(
+          new IntIntDoubleFunction() {
+            @Override
+            public double apply(int i, int j, double value) {
+              setQuick(i, j, getQuick(i, j) + alpha * value);
+              return value;
+            }
+          }
+      );
+      return this;
+    }
+
+    if (function == org.apache.mahout.math.jet.math.Functions.mult) { // x[i] = x[i] * y[i]
+      int[] idx = indexes.elements();
+      double[] vals = values.elements();
+
+      for (int i = starts.length - 1; --i >= 0;) {
+        int low = starts[i];
+        for (int k = starts[i + 1]; --k >= low;) {
+          int j = idx[k];
+          vals[k] *= y.getQuick(i, j);
+          if (vals[k] == 0) {
+            remove(i, j);
+          }
+        }
+      }
+      return this;
+    }
+
+    if (function == org.apache.mahout.math.jet.math.Functions.div) { // x[i] = x[i] / y[i]
+      int[] idx = indexes.elements();
+      double[] vals = values.elements();
+
+      for (int i = starts.length - 1; --i >= 0;) {
+        int low = starts[i];
+        for (int k = starts[i + 1]; --k >= low;) {
+          int j = idx[k];
+          vals[k] /= y.getQuick(i, j);
+          if (vals[k] == 0) {
+            remove(i, j);
+          }
+        }
+      }
+      return this;
+    }
+
+    return super.assign(y, function);
+  }
+
+  @Override
+  public DoubleMatrix2D forEachNonZero(IntIntDoubleFunction function) {
+    int[] idx = indexes.elements();
+    double[] vals = values.elements();
+
+    for (int i = starts.length - 1; --i >= 0;) {
+      int low = starts[i];
+      for (int k = starts[i + 1]; --k >= low;) {
+        int j = idx[k];
+        double value = vals[k];
+        double r = function.apply(i, j, value);
+        if (r != value) {
+          vals[k] = r;
+        }
+      }
+    }
+    return this;
+  }
+
+  /**
+   * Returns the content of this matrix if it is a wrapper; or <tt>this</tt> otherwise. Override this method in
+   * wrappers.
+   */
+  @Override
+  protected DoubleMatrix2D getContent() {
+    return this;
+  }
+
+  /**
+   * Returns the matrix cell value at coordinate <tt>[row,column]</tt>.
+   *
+   * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>0 &lt;= column &lt; columns() && 0 &lt;= row &lt; rows()</tt>.
+   *
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @return the value at the specified coordinate.
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    int k = indexes.binarySearchFromTo(column, starts[row], starts[row + 1] - 1);
+    double v = 0;
+    if (k >= 0) {
+      v = values.getQuick(k);
+    }
+    return v;
+  }
+
+  protected void insert(int row, int column, int index, double value) {
+    indexes.beforeInsert(index, column);
+    values.beforeInsert(index, value);
+    for (int i = starts.length; --i > row;) {
+      starts[i]++;
+    }
+  }
+
+  /**
+   * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified
+   * number of rows and columns. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the
+   * new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>, if the receiver is an instance of type
+   * <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc. In general,
+   * the new matrix should have internal parametrization as similar as possible.
+   *
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @return a new empty matrix of the same dynamic type.
+   */
+  @Override
+  public DoubleMatrix2D like(int rows, int columns) {
+    return new RCDoubleMatrix2D(rows, columns);
+  }
+
+  /**
+   * Construct and returns a new 1-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the
+   * receiver. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must be
+   * of type <tt>DenseDoubleMatrix1D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new
+   * matrix must be of type <tt>SparseDoubleMatrix1D</tt>, etc.
+   *
+   * @param size the number of cells the matrix shall have.
+   * @return a new matrix of the corresponding dynamic type.
+   */
+  @Override
+  public DoubleMatrix1D like1D(int size) {
+    return new SparseDoubleMatrix1D(size);
+  }
+
+  protected void remove(int row, int index) {
+    indexes.remove(index);
+    values.remove(index);
+    for (int i = starts.length; --i > row;) {
+      starts[i]--;
+    }
+  }
+
+  /**
+   * Sets the matrix cell at coordinate <tt>[row,column]</tt> to the specified value.
+   *
+   * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>0 &lt;= column &lt; columns() && 0 &lt;= row &lt; rows()</tt>.
+   *
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @param value  the value to be filled into the specified cell.
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    int k = indexes.binarySearchFromTo(column, starts[row], starts[row + 1] - 1);
+    if (k >= 0) { // found
+      if (value == 0) {
+        remove(row, k);
+      } else {
+        values.setQuick(k, value);
+      }
+      return;
+    }
+
+    if (value != 0) {
+      k = -k - 1;
+      insert(row, column, k, value);
+    }
+  }
+
+  @Override
+  public void trimToSize() {
+    indexes.trimToSize();
+    values.trimToSize();
+  }
+
+  @Override
+  public DoubleMatrix1D zMult(DoubleMatrix1D y, DoubleMatrix1D z, double alpha, double beta, boolean transposeA) {
+    int m = rows;
+    int n = columns;
+    if (transposeA) {
+      m = columns;
+      n = rows;
+    }
+
+    boolean ignore = (z == null || !transposeA);
+    if (z == null) {
+      z = new DenseDoubleMatrix1D(m);
+    }
+
+    if (!(y instanceof DenseDoubleMatrix1D && z instanceof DenseDoubleMatrix1D)) {
+      return super.zMult(y, z, alpha, beta, transposeA);
+    }
+
+    if (n != y.size() || m > z.size()) {
+      throw new IllegalArgumentException(
+          "Incompatible args: " + ((transposeA ? viewDice() : this).toStringShort()) + ", " + y.toStringShort() + ", " +
+              z.toStringShort());
+    }
+
+    DenseDoubleMatrix1D zz = (DenseDoubleMatrix1D) z;
+    double[] zElements = zz.elements;
+    int zStride = zz.stride;
+    int zi = z.index(0);
+
+    DenseDoubleMatrix1D yy = (DenseDoubleMatrix1D) y;
+    double[] yElements = yy.elements;
+    int yStride = yy.stride;
+    int yi = y.index(0);
+
+    if (yElements == null || zElements == null) {
+      throw new InternalError();
+    }
+
+    /*
+    forEachNonZero(
+      new IntIntDoubleFunction() {
+        public double apply(int i, int j, double value) {
+          zElements[zi + zStride*i] += value * yElements[yi + yStride*j];
+          //z.setQuick(row,z.getQuick(row) + value * y.getQuick(column));
+          //log.info("["+i+","+j+"]-->"+value);
+          return value;
+        }
+      }
+    );
+    */
+
+
+    int[] idx = indexes.elements();
+    double[] vals = values.elements();
+    int s = starts.length - 1;
+    if (!transposeA) {
+      for (int i = 0; i < s; i++) {
+        int high = starts[i + 1];
+        double sum = 0;
+        for (int k = starts[i]; k < high; k++) {
+          int j = idx[k];
+          sum += vals[k] * yElements[yi + yStride * j];
+        }
+        zElements[zi] = alpha * sum + beta * zElements[zi];
+        zi += zStride;
+      }
+    } else {
+      if (!ignore) {
+        z.assign(Functions.mult(beta));
+      }
+      for (int i = 0; i < s; i++) {
+        int high = starts[i + 1];
+        double yElem = alpha * yElements[yi + yStride * i];
+        for (int k = starts[i]; k < high; k++) {
+          int j = idx[k];
+          zElements[zi + zStride * j] += vals[k] * yElem;
+        }
+      }
+    }
+
+    return z;
+  }
+
+  @Override
+  public DoubleMatrix2D zMult(DoubleMatrix2D B, DoubleMatrix2D C, double alpha, double beta, boolean transposeA,
+                              boolean transposeB) {
+    if (transposeB) {
+      B = B.viewDice();
+    }
+    int m = rows;
+    int n = columns;
+    if (transposeA) {
+      m = columns;
+      n = rows;
+    }
+    int p = B.columns;
+    boolean ignore = (C == null);
+    if (C == null) {
+      C = new DenseDoubleMatrix2D(m, p);
+    }
+
+    if (B.rows != n) {
+      throw new IllegalArgumentException("Matrix2D inner dimensions must agree:" + toStringShort() + ", " +
+          (transposeB ? B.viewDice() : B).toStringShort());
+    }
+    if (C.rows != m || C.columns != p) {
+      throw new IllegalArgumentException(
+          "Incompatibel result matrix: " + toStringShort() + ", " + (transposeB ? B.viewDice() : B).toStringShort() +
+              ", " + C.toStringShort());
+    }
+    if (this == C || B == C) {
+      throw new IllegalArgumentException("Matrices must not be identical");
+    }
+
+    if (!ignore) {
+      C.assign(Functions.mult(beta));
+    }
+
+    // cache views
+    DoubleMatrix1D[] Brows = new DoubleMatrix1D[n];
+    for (int i = n; --i >= 0;) {
+      Brows[i] = B.viewRow(i);
+    }
+    DoubleMatrix1D[] Crows = new DoubleMatrix1D[m];
+    for (int i = m; --i >= 0;) {
+      Crows[i] = C.viewRow(i);
+    }
+
+    PlusMult fun = org.apache.mahout.math.jet.math.PlusMult.plusMult(0);
+
+    int[] idx = indexes.elements();
+    double[] vals = values.elements();
+    for (int i = starts.length - 1; --i >= 0;) {
+      int low = starts[i];
+      for (int k = starts[i + 1]; --k >= low;) {
+        int j = idx[k];
+        fun.setMultiplicator(vals[k] * alpha);
+        if (!transposeA) {
+          Crows[i].assign(Brows[j], fun);
+        } else {
+          Crows[j].assign(Brows[i], fun);
+        }
+      }
+    }
+
+    return C;
+  }
+}

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

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCMDoubleMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCMDoubleMatrix2D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCMDoubleMatrix2D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/RCMDoubleMatrix2D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,224 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.matrix.impl;
+
+import org.apache.mahout.math.jet.math.Functions;
+import org.apache.mahout.math.list.DoubleArrayList;
+import org.apache.mahout.math.list.IntArrayList;
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/**
+ * Sparse row-compressed-modified 2-d matrix holding <tt>double</tt> elements.
+ *
+ * @author wolfgang.hoschek@cern.ch
+ * @version 0.9, 04/14/2000
+ */
+class RCMDoubleMatrix2D extends WrapperDoubleMatrix2D {
+  /*
+   * The elements of the matrix.
+   */
+  private final IntArrayList[] indexes;
+  private final DoubleArrayList[] values;
+
+  /**
+   * Constructs a matrix with a copy of the given values. <tt>values</tt> is required to have the form
+   * <tt>values[row][column]</tt> and have exactly the same number of columns in every row. <p> The values are copied.
+   * So subsequent changes in <tt>values</tt> are not reflected in the matrix, and vice-versa.
+   *
+   * @param values The values to be filled into the new matrix.
+   * @throws IllegalArgumentException if <tt>for any 1 &lt;= row &lt; values.length: values[row].length !=
+   *                                  values[row-1].length</tt>.
+   */
+  RCMDoubleMatrix2D(double[][] values) {
+    this(values.length, values.length == 0 ? 0 : values[0].length);
+    assign(values);
+  }
+
+  /**
+   * Constructs a matrix with a given number of rows and columns. All entries are initially <tt>0</tt>.
+   *
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt>.
+   */
+  RCMDoubleMatrix2D(int rows, int columns) {
+    super(null);
+    setUp(rows, columns);
+    indexes = new IntArrayList[rows];
+    values = new DoubleArrayList[rows];
+  }
+
+  /**
+   * Sets all cells to the state specified by <tt>value</tt>.
+   *
+   * @param value the value to be filled into the cells.
+   * @return <tt>this</tt> (for convenience only).
+   */
+  @Override
+  public DoubleMatrix2D assign(double value) {
+    // overriden for performance only
+    if (value == 0) {
+      for (int row = rows; --row >= 0;) {
+        indexes[row] = null;
+        values[row] = null;
+      }
+    } else {
+      super.assign(value);
+    }
+    return this;
+  }
+
+  /**
+   * Returns the content of this matrix if it is a wrapper; or <tt>this</tt> otherwise. Override this method in
+   * wrappers.
+   */
+  @Override
+  protected DoubleMatrix2D getContent() {
+    return this;
+  }
+
+  /**
+   * Returns the matrix cell value at coordinate <tt>[row,column]</tt>.
+   *
+   * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>0 &lt;= column &lt; columns() && 0 &lt;= row &lt; rows()</tt>.
+   *
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @return the value at the specified coordinate.
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    int k = -1;
+    if (indexes[row] != null) {
+      k = indexes[row].binarySearch(column);
+    }
+    if (k < 0) {
+      return 0;
+    }
+    return values[row].getQuick(k);
+  }
+
+  /**
+   * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified
+   * number of rows and columns. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the
+   * new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>, if the receiver is an instance of type
+   * <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc. In general,
+   * the new matrix should have internal parametrization as similar as possible.
+   *
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @return a new empty matrix of the same dynamic type.
+   */
+  @Override
+  public DoubleMatrix2D like(int rows, int columns) {
+    return new RCMDoubleMatrix2D(rows, columns);
+  }
+
+  /**
+   * Construct and returns a new 1-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the
+   * receiver. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must be
+   * of type <tt>DenseDoubleMatrix1D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new
+   * matrix must be of type <tt>SparseDoubleMatrix1D</tt>, etc.
+   *
+   * @param size the number of cells the matrix shall have.
+   * @return a new matrix of the corresponding dynamic type.
+   */
+  @Override
+  public DoubleMatrix1D like1D(int size) {
+    return new SparseDoubleMatrix1D(size);
+  }
+
+  /**
+   * Sets the matrix cell at coordinate <tt>[row,column]</tt> to the specified value.
+   *
+   * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>0 &lt;= column &lt; columns() && 0 &lt;= row &lt; rows()</tt>.
+   *
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @param value  the value to be filled into the specified cell.
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    int i = row;
+    int j = column;
+
+    int k = -1;
+    IntArrayList indexList = indexes[i];
+    if (indexList != null) {
+      k = indexList.binarySearch(j);
+    }
+
+    if (k >= 0) { // found
+      if (value == 0) {
+        DoubleArrayList valueList = values[i];
+        indexList.remove(k);
+        valueList.remove(k);
+        int s = indexList.size();
+        if (s > 2 && s * 3 < indexList.elements().length) {
+          indexList.setSize(s * 3 / 2);
+          indexList.trimToSize();
+          indexList.setSize(s);
+
+          valueList.setSize(s * 3 / 2);
+          valueList.trimToSize();
+          valueList.setSize(s);
+        }
+      } else {
+        values[i].setQuick(k, value);
+      }
+    } else { // not found
+      if (value == 0) {
+        return;
+      }
+
+      k = -k - 1;
+
+      if (indexList == null) {
+        indexes[i] = new IntArrayList(3);
+        values[i] = new DoubleArrayList(3);
+      }
+      indexes[i].beforeInsert(k, j);
+      values[i].beforeInsert(k, value);
+    }
+  }
+
+  /**
+   * Linear algebraic matrix-vector multiplication; <tt>z = A * y</tt>. <tt>z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i],
+   * i=0..A.rows()-1, j=0..y.size()-1</tt>. Where <tt>A == this</tt>.
+   *
+   * @param y the source vector.
+   * @param z the vector where results are to be stored.
+   * @throws IllegalArgumentException if <tt>A.columns() != y.size() || A.rows() > z.size())</tt>.
+   */
+  protected void zMult(DoubleMatrix1D y, DoubleMatrix1D z, org.apache.mahout.math.list.IntArrayList nonZeroIndexes,
+                       DoubleMatrix1D[] allRows, double alpha, double beta) {
+    if (columns != y.size() || rows > z.size()) {
+      throw new IllegalArgumentException(
+          "Incompatible args: " + toStringShort() + ", " + y.toStringShort() + ", " + z.toStringShort());
+    }
+
+    z.assign(Functions.mult(beta / alpha));
+    for (int i = indexes.length; --i >= 0;) {
+      if (indexes[i] != null) {
+        for (int k = indexes[i].size(); --k >= 0;) {
+          int j = indexes[i].getQuick(k);
+          double value = values[i].getQuick(k);
+          z.setQuick(i, z.getQuick(i) + value * y.getQuick(j));
+        }
+      }
+    }
+
+    z.assign(Functions.mult(alpha));
+  }
+}

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

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix1D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,198 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.matrix.impl;
+
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/**
+ * Selection view on dense 1-d matrices holding <tt>double</tt> elements. First see the <a
+ * href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the
+ * broad picture. <p> <b>Implementation:</b> <p> Objects of this class are typically constructed via
+ * <tt>viewIndexes</tt> methods on some source matrix. The interface introduced in abstract super classes defines
+ * everything a user can do. From a user point of view there is nothing special about this class; it presents the same
+ * functionality with the same signatures and semantics as its abstract superclass(es) while introducing no additional
+ * functionality. Thus, this class need not be visible to users. By the way, the same principle applies to concrete
+ * DenseXXX, SparseXXX classes: they presents the same functionality with the same signatures and semantics as abstract
+ * superclass(es) while introducing no additional functionality. Thus, they need not be visible to users, either.
+ * Factory methods could hide all these concrete types. <p> This class uses no delegation. Its instances point directly
+ * to the data. Cell addressing overhead is 1 additional array index access per get/set. <p> Note that this
+ * implementation is not synchronized. <p> <b>Memory requirements:</b> <p> <tt>memory [bytes] = 4*indexes.length</tt>.
+ * Thus, an index view with 1000 indexes additionally uses 4 KB. <p> <b>Time complexity:</b> <p> Depends on the parent
+ * view holding cells. <p>
+ *
+ * @author wolfgang.hoschek@cern.ch
+ * @version 1.0, 09/24/99
+ */
+class SelectedDenseDoubleMatrix1D extends DoubleMatrix1D {
+
+  /** The elements of this matrix. */
+  protected final double[] elements;
+
+  /** The offsets of visible indexes of this matrix. */
+  private final int[] offsets;
+
+  /** The offset. */
+  private int offset;
+
+  /**
+   * Constructs a matrix view with the given parameters.
+   *
+   * @param elements the cells.
+   * @param offsets  The indexes of the cells that shall be visible.
+   */
+  protected SelectedDenseDoubleMatrix1D(double[] elements, int[] offsets) {
+    this(offsets.length, elements, 0, 1, offsets, 0);
+  }
+
+  /**
+   * Constructs a matrix view with the given parameters.
+   *
+   * @param size     the number of cells the matrix shall have.
+   * @param elements the cells.
+   * @param zero     the index of the first element.
+   * @param stride   the number of indexes between any two elements, i.e. <tt>index(i+1)-index(i)</tt>.
+   * @param offsets  the offsets of the cells that shall be visible.
+   */
+  protected SelectedDenseDoubleMatrix1D(int size, double[] elements, int zero, int stride, int[] offsets, int offset) {
+    setUp(size, zero, stride);
+
+    this.elements = elements;
+    this.offsets = offsets;
+    this.offset = offset;
+    this.isNoView = false;
+  }
+
+  /**
+   * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+   * Default implementation. Override, if necessary.
+   *
+   * @param absRank the absolute rank of the element.
+   * @return the position.
+   */
+  @Override
+  protected int _offset(int absRank) {
+    return offsets[absRank];
+  }
+
+  /**
+   * Returns the matrix cell value at coordinate <tt>index</tt>.
+   *
+   * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>index&lt;0 || index&gt;=size()</tt>.
+   *
+   * @param index the index of the cell.
+   * @return the value of the specified cell.
+   */
+  @Override
+  public double getQuick(int index) {
+    //if (debug) if (index<0 || index>=size) checkIndex(index);
+    //return elements[index(index)];
+    //manually inlined:
+    return elements[offset + offsets[zero + index * stride]];
+  }
+
+  /** Returns <tt>true</tt> if both matrices share at least one identical cell. */
+  @Override
+  protected boolean haveSharedCellsRaw(DoubleMatrix1D other) {
+    if (other instanceof SelectedDenseDoubleMatrix1D) {
+      SelectedDenseDoubleMatrix1D otherMatrix = (SelectedDenseDoubleMatrix1D) other;
+      return this.elements == otherMatrix.elements;
+    } else if (other instanceof DenseDoubleMatrix1D) {
+      DenseDoubleMatrix1D otherMatrix = (DenseDoubleMatrix1D) other;
+      return this.elements == otherMatrix.elements;
+    }
+    return false;
+  }
+
+  /**
+   * Returns the position of the element with the given relative rank within the (virtual or non-virtual) internal
+   * 1-dimensional array. You may want to override this method for performance.
+   *
+   * @param rank the rank of the element.
+   */
+  @Override
+  protected int index(int rank) {
+    //return this.offset + super.index(rank);
+    // manually inlined:
+    return offset + offsets[zero + rank * stride];
+  }
+
+  /**
+   * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified
+   * size. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must also be
+   * of type <tt>DenseDoubleMatrix1D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new
+   * matrix must also be of type <tt>SparseDoubleMatrix1D</tt>, etc. In general, the new matrix should have internal
+   * parametrization as similar as possible.
+   *
+   * @param size the number of cell the matrix shall have.
+   * @return a new empty matrix of the same dynamic type.
+   */
+  @Override
+  public DoubleMatrix1D like(int size) {
+    return new DenseDoubleMatrix1D(size);
+  }
+
+  /**
+   * Construct and returns a new 2-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the
+   * receiver. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix1D</tt> the new matrix must be
+   * of type <tt>DenseDoubleMatrix2D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix1D</tt> the new
+   * matrix must be of type <tt>SparseDoubleMatrix2D</tt>, etc.
+   *
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @return a new matrix of the corresponding dynamic type.
+   */
+  @Override
+  public DoubleMatrix2D like2D(int rows, int columns) {
+    return new DenseDoubleMatrix2D(rows, columns);
+  }
+
+  /**
+   * Sets the matrix cell at coordinate <tt>index</tt> to the specified value.
+   *
+   * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>index&lt;0 || index&gt;=size()</tt>.
+   *
+   * @param index the index of the cell.
+   * @param value the value to be filled into the specified cell.
+   */
+  @Override
+  public void setQuick(int index, double value) {
+    //if (debug) if (index<0 || index>=size) checkIndex(index);
+    //elements[index(index)] = value;
+    //manually inlined:
+    elements[offset + offsets[zero + index * stride]] = value;
+  }
+
+  /**
+   * Sets up a matrix with a given number of cells.
+   *
+   * @param size the number of cells the matrix shall have.
+   */
+  @Override
+  protected void setUp(int size) {
+    super.setUp(size);
+    this.stride = 1;
+    this.offset = 0;
+  }
+
+  /**
+   * Construct and returns a new selection view.
+   *
+   * @param offsets the offsets of the visible elements.
+   * @return a new view.
+   */
+  @Override
+  protected DoubleMatrix1D viewSelectionLike(int[] offsets) {
+    return new SelectedDenseDoubleMatrix1D(this.elements, offsets);
+  }
+}

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

Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix2D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix2D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix2D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,309 @@
+/*
+Copyright � 1999 CERN - European Organization for Nuclear Research.
+Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
+is hereby granted without fee, provided that the above copyright notice appear in all copies and 
+that both that copyright notice and this permission notice appear in supporting documentation. 
+CERN makes no representations about the suitability of this software for any purpose. 
+It is provided "as is" without expressed or implied warranty.
+*/
+package org.apache.mahout.math.matrix.impl;
+
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/**
+ * Selection view on dense 2-d matrices holding <tt>double</tt> elements. First see the <a
+ * href="package-summary.html">package summary</a> and javadoc <a href="package-tree.html">tree view</a> to get the
+ * broad picture. <p> <b>Implementation:</b> <p> Objects of this class are typically constructed via
+ * <tt>viewIndexes</tt> methods on some source matrix. The interface introduced in abstract super classes defines
+ * everything a user can do. From a user point of view there is nothing special about this class; it presents the same
+ * functionality with the same signatures and semantics as its abstract superclass(es) while introducing no additional
+ * functionality. Thus, this class need not be visible to users. By the way, the same principle applies to concrete
+ * DenseXXX and SparseXXX classes: they presents the same functionality with the same signatures and semantics as
+ * abstract superclass(es) while introducing no additional functionality. Thus, they need not be visible to users,
+ * either. Factory methods could hide all these concrete types. <p> This class uses no delegation. Its instances point
+ * directly to the data. Cell addressing overhead is 1 additional int addition and 2 additional array index accesses per
+ * get/set. <p> Note that this implementation is not synchronized. <p> <b>Memory requirements:</b> <p> <tt>memory
+ * [bytes] = 4*(rowIndexes.length+columnIndexes.length)</tt>. Thus, an index view with 1000 x 1000 indexes additionally
+ * uses 8 KB. <p> <b>Time complexity:</b> <p> Depends on the parent view holding cells. <p>
+ *
+ * @author wolfgang.hoschek@cern.ch
+ * @version 1.0, 09/24/99
+ */
+class SelectedDenseDoubleMatrix2D extends DoubleMatrix2D {
+
+  /** The elements of this matrix. */
+  protected final double[] elements;
+
+  /** The offsets of the visible cells of this matrix. */
+  private int[] rowOffsets;
+  private int[] columnOffsets;
+
+  /** The offset. */
+  private int offset;
+
+  /**
+   * Constructs a matrix view with the given parameters.
+   *
+   * @param elements      the cells.
+   * @param rowOffsets    The row offsets of the cells that shall be visible.
+   * @param columnOffsets The column offsets of the cells that shall be visible.
+   */
+  protected SelectedDenseDoubleMatrix2D(double[] elements, int[] rowOffsets, int[] columnOffsets, int offset) {
+    this(rowOffsets.length, columnOffsets.length, elements, 0, 0, 1, 1, rowOffsets, columnOffsets, offset);
+  }
+
+  /**
+   * Constructs a matrix view with the given parameters.
+   *
+   * @param rows          the number of rows the matrix shall have.
+   * @param columns       the number of columns the matrix shall have.
+   * @param elements      the cells.
+   * @param rowZero       the position of the first element.
+   * @param columnZero    the position of the first element.
+   * @param rowStride     the number of elements between two rows, i.e. <tt>index(i+1,j)-index(i,j)</tt>.
+   * @param columnStride  the number of elements between two columns, i.e. <tt>index(i,j+1)-index(i,j)</tt>.
+   * @param rowOffsets    The row offsets of the cells that shall be visible.
+   * @param columnOffsets The column offsets of the cells that shall be visible.
+   */
+  protected SelectedDenseDoubleMatrix2D(int rows, int columns, double[] elements, int rowZero, int columnZero,
+                                        int rowStride, int columnStride, int[] rowOffsets, int[] columnOffsets,
+                                        int offset) {
+    // be sure parameters are valid, we do not check...
+    setUp(rows, columns, rowZero, columnZero, rowStride, columnStride);
+
+    this.elements = elements;
+    this.rowOffsets = rowOffsets;
+    this.columnOffsets = columnOffsets;
+    this.offset = offset;
+
+    this.isNoView = false;
+  }
+
+  /**
+   * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+   * Default implementation. Override, if necessary.
+   *
+   * @param absRank the absolute rank of the element.
+   * @return the position.
+   */
+  @Override
+  protected int _columnOffset(int absRank) {
+    return columnOffsets[absRank];
+  }
+
+  /**
+   * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+   * Default implementation. Override, if necessary.
+   *
+   * @param absRank the absolute rank of the element.
+   * @return the position.
+   */
+  @Override
+  protected int _rowOffset(int absRank) {
+    return rowOffsets[absRank];
+  }
+
+  /**
+   * Returns the matrix cell value at coordinate <tt>[row,column]</tt>.
+   *
+   * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>0 &lt;= column &lt; columns() && 0 &lt;= row &lt; rows()</tt>.
+   *
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @return the value at the specified coordinate.
+   */
+  @Override
+  public double getQuick(int row, int column) {
+    //if (debug) if (column<0 || column>=columns || row<0 || row>=rows) throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
+    //return elements[index(row,column)];
+    //manually inlined:
+    return elements[offset + rowOffsets[rowZero + row * rowStride] + columnOffsets[columnZero + column * columnStride]];
+  }
+
+  /**
+   * Returns <tt>true</tt> if both matrices share common cells. More formally, returns <tt>true</tt> if <tt>other !=
+   * null</tt> and at least one of the following conditions is met <ul> <li>the receiver is a view of the other matrix
+   * <li>the other matrix is a view of the receiver <li><tt>this == other</tt> </ul>
+   */
+  @Override
+  protected boolean haveSharedCellsRaw(DoubleMatrix2D other) {
+    if (other instanceof SelectedDenseDoubleMatrix2D) {
+      SelectedDenseDoubleMatrix2D otherMatrix = (SelectedDenseDoubleMatrix2D) other;
+      return this.elements == otherMatrix.elements;
+    } else if (other instanceof DenseDoubleMatrix2D) {
+      DenseDoubleMatrix2D otherMatrix = (DenseDoubleMatrix2D) other;
+      return this.elements == otherMatrix.elements;
+    }
+    return false;
+  }
+
+  /**
+   * Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array.
+   *
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   */
+  @Override
+  protected int index(int row, int column) {
+    //return this.offset + super.index(row,column);
+    //manually inlined:
+    return this.offset + rowOffsets[rowZero + row * rowStride] + columnOffsets[columnZero + column * columnStride];
+  }
+
+  /**
+   * Construct and returns a new empty matrix <i>of the same dynamic type</i> as the receiver, having the specified
+   * number of rows and columns. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the
+   * new matrix must also be of type <tt>DenseDoubleMatrix2D</tt>, if the receiver is an instance of type
+   * <tt>SparseDoubleMatrix2D</tt> the new matrix must also be of type <tt>SparseDoubleMatrix2D</tt>, etc. In general,
+   * the new matrix should have internal parametrization as similar as possible.
+   *
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @return a new empty matrix of the same dynamic type.
+   */
+  @Override
+  public DoubleMatrix2D like(int rows, int columns) {
+    return new DenseDoubleMatrix2D(rows, columns);
+  }
+
+  /**
+   * Construct and returns a new 1-d matrix <i>of the corresponding dynamic type</i>, entirelly independent of the
+   * receiver. For example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must be
+   * of type <tt>DenseDoubleMatrix1D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new
+   * matrix must be of type <tt>SparseDoubleMatrix1D</tt>, etc.
+   *
+   * @param size the number of cells the matrix shall have.
+   * @return a new matrix of the corresponding dynamic type.
+   */
+  @Override
+  public DoubleMatrix1D like1D(int size) {
+    return new DenseDoubleMatrix1D(size);
+  }
+
+  /**
+   * Construct and returns a new 1-d matrix <i>of the corresponding dynamic type</i>, sharing the same cells. For
+   * example, if the receiver is an instance of type <tt>DenseDoubleMatrix2D</tt> the new matrix must be of type
+   * <tt>DenseDoubleMatrix1D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix2D</tt> the new matrix
+   * must be of type <tt>SparseDoubleMatrix1D</tt>, etc.
+   *
+   * @param size   the number of cells the matrix shall have.
+   * @param zero   the index of the first element.
+   * @param stride the number of indexes between any two elements, i.e. <tt>index(i+1)-index(i)</tt>.
+   * @return a new matrix of the corresponding dynamic type.
+   */
+  @Override
+  protected DoubleMatrix1D like1D(int size, int zero, int stride) {
+    throw new InternalError(); // this method is never called since viewRow() and viewColumn are overridden properly.
+  }
+
+  /**
+   * Sets the matrix cell at coordinate <tt>[row,column]</tt> to the specified value.
+   *
+   * <p>Provided with invalid parameters this method may access illegal indexes without throwing any exception. <b>You
+   * should only use this method when you are absolutely sure that the coordinate is within bounds.</b> Precondition
+   * (unchecked): <tt>0 &lt;= column &lt; columns() && 0 &lt;= row &lt; rows()</tt>.
+   *
+   * @param row    the index of the row-coordinate.
+   * @param column the index of the column-coordinate.
+   * @param value  the value to be filled into the specified cell.
+   */
+  @Override
+  public void setQuick(int row, int column, double value) {
+    //if (debug) if (column<0 || column>=columns || row<0 || row>=rows) throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
+    //elements[index(row,column)] = value;
+    //manually inlined:
+    elements[offset + rowOffsets[rowZero + row * rowStride] + columnOffsets[columnZero + column * columnStride]] =
+        value;
+  }
+
+  /**
+   * Sets up a matrix with a given number of rows and columns.
+   *
+   * @param rows    the number of rows the matrix shall have.
+   * @param columns the number of columns the matrix shall have.
+   * @throws IllegalArgumentException if <tt>(double)columns*rows > Integer.MAX_VALUE</tt>.
+   */
+  @Override
+  protected void setUp(int rows, int columns) {
+    super.setUp(rows, columns);
+    this.rowStride = 1;
+    this.columnStride = 1;
+    this.offset = 0;
+  }
+
+  /** Self modifying version of viewDice(). */
+  @Override
+  protected AbstractMatrix2D vDice() {
+    super.vDice();
+    // swap
+    int[] tmp = rowOffsets;
+    rowOffsets = columnOffsets;
+    columnOffsets = tmp;
+
+    // flips stay unaffected
+
+    this.isNoView = false;
+    return this;
+  }
+
+  /**
+   * Constructs and returns a new <i>slice view</i> representing the rows of the given column. The returned view is
+   * backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. To obtain a
+   * slice view on subranges, construct a sub-ranging view (<tt>viewPart(...)</tt>), then apply this method to the
+   * sub-range view. <p> <b>Example:</b> <table border="0"> <tr nowrap> <td valign="top">2 x 3 matrix: <br> 1, 2, 3<br>
+   * 4, 5, 6 </td> <td>viewColumn(0) ==></td> <td valign="top">Matrix1D of size 2:<br> 1, 4</td> </tr> </table>
+   *
+   * @param column the column to fix.
+   * @return a new slice view.
+   * @throws IllegalArgumentException if <tt>column < 0 || column >= columns()</tt>.
+   * @see #viewRow(int)
+   */
+  @Override
+  public DoubleMatrix1D viewColumn(int column) {
+    checkColumn(column);
+    int viewSize = this.rows;
+    int viewZero = this.rowZero;
+    int viewStride = this.rowStride;
+    int[] viewOffsets = this.rowOffsets;
+    int viewOffset = this.offset + _columnOffset(_columnRank(column));
+    return new SelectedDenseDoubleMatrix1D(viewSize, this.elements, viewZero, viewStride, viewOffsets, viewOffset);
+  }
+
+  /**
+   * Constructs and returns a new <i>slice view</i> representing the columns of the given row. The returned view is
+   * backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa. To obtain a
+   * slice view on subranges, construct a sub-ranging view (<tt>viewPart(...)</tt>), then apply this method to the
+   * sub-range view. <p> <b>Example:</b> <table border="0"> <tr nowrap> <td valign="top">2 x 3 matrix: <br> 1, 2, 3<br>
+   * 4, 5, 6 </td> <td>viewRow(0) ==></td> <td valign="top">Matrix1D of size 3:<br> 1, 2, 3</td> </tr> </table>
+   *
+   * @param row the row to fix.
+   * @return a new slice view.
+   * @throws IndexOutOfBoundsException if <tt>row < 0 || row >= rows()</tt>.
+   * @see #viewColumn(int)
+   */
+  @Override
+  public DoubleMatrix1D viewRow(int row) {
+    checkRow(row);
+    int viewSize = this.columns;
+    int viewZero = columnZero;
+    int viewStride = this.columnStride;
+    int[] viewOffsets = this.columnOffsets;
+    int viewOffset = this.offset + _rowOffset(_rowRank(row));
+    return new SelectedDenseDoubleMatrix1D(viewSize, this.elements, viewZero, viewStride, viewOffsets, viewOffset);
+  }
+
+  /**
+   * Construct and returns a new selection view.
+   *
+   * @param rowOffsets    the offsets of the visible elements.
+   * @param columnOffsets the offsets of the visible elements.
+   * @return a new view.
+   */
+  @Override
+  protected DoubleMatrix2D viewSelectionLike(int[] rowOffsets, int[] columnOffsets) {
+    return new SelectedDenseDoubleMatrix2D(this.elements, rowOffsets, columnOffsets, this.offset);
+  }
+}

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