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 [39/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/SelectedSparseDoubleMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix3D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix3D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix3D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,384 @@
+/*
+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.map.AbstractIntDoubleMap;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+import org.apache.mahout.math.matrix.DoubleMatrix3D;
+
+/**
+ * Selection view on sparse 3-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 is 1 additional int addition and 3 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*(sliceIndexes.length+rowIndexes.length+columnIndexes.length)</tt>. Thus, an index view with 100 x 100 x
+ * 100 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 SelectedSparseDoubleMatrix3D extends DoubleMatrix3D {
+
+ /** The elements of this matrix. */
+ protected final AbstractIntDoubleMap elements;
+
+ /** The offsets of the visible cells of this matrix. */
+ protected int[] sliceOffsets;
+ protected int[] rowOffsets;
+ protected int[] columnOffsets;
+
+ /** The offset. */
+ protected int offset;
+
+ /**
+ * Constructs a matrix view with the given parameters.
+ *
+ * @param elements the cells.
+ * @param sliceOffsets The slice offsets of the cells that shall be visible.
+ * @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 SelectedSparseDoubleMatrix3D(AbstractIntDoubleMap elements, int[] sliceOffsets, int[] rowOffsets,
+ int[] columnOffsets, int offset) {
+ // be sure parameters are valid, we do not check...
+ int slices = sliceOffsets.length;
+ int rows = rowOffsets.length;
+ int columns = columnOffsets.length;
+ setUp(slices, rows, columns);
+
+ this.elements = elements;
+
+ this.sliceOffsets = sliceOffsets;
+ 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 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 _sliceOffset(int absRank) {
+ return sliceOffsets[absRank];
+ }
+
+ /**
+ * 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<0 || slice>=slices() || row<0 || row>=rows() || column<0 ||
+ * column>=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 double 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.get(index(slice,row,column));
+ //manually inlined:
+ return elements.get(offset + sliceOffsets[sliceZero + slice * sliceStride] + 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(DoubleMatrix3D other) {
+ if (other instanceof SelectedSparseDoubleMatrix3D) {
+ SelectedSparseDoubleMatrix3D otherMatrix = (SelectedSparseDoubleMatrix3D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof SparseDoubleMatrix3D) {
+ SparseDoubleMatrix3D otherMatrix = (SparseDoubleMatrix3D) 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 this.offset + super.index(slice,row,column);
+ //manually inlined:
+ return this.offset + sliceOffsets[sliceZero + slice * sliceStride] + 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 slices, rows and columns. For example, if the receiver is an instance of type
+ * <tt>DenseDoubleMatrix3D</tt> the new matrix must also be of type <tt>DenseDoubleMatrix3D</tt>, if the receiver is
+ * an instance of type <tt>SparseDoubleMatrix3D</tt> the new matrix must also be of type
+ * <tt>SparseDoubleMatrix3D</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 DoubleMatrix3D like(int slices, int rows, int columns) {
+ return new SparseDoubleMatrix3D(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>DenseDoubleMatrix3D</tt> the new matrix must also be of type
+ * <tt>DenseDoubleMatrix2D</tt>, if the receiver is an instance of type <tt>SparseDoubleMatrix3D</tt> the new matrix
+ * must also 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.
+ * @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 DoubleMatrix2D like2D(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) {
+ throw new InternalError(); // this method is never called since viewRow() and viewColumn are overridden properly.
+ }
+
+ /**
+ * 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<0 || slice>=slices() || row<0 || row>=rows() || column<0 ||
+ * column>=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, double 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);
+ //int index = index(slice,row,column);
+ //manually inlined:
+ int index = offset + sliceOffsets[sliceZero + slice * sliceStride] + rowOffsets[rowZero + row * rowStride] +
+ columnOffsets[columnZero + column * columnStride];
+ if (value == 0) {
+ this.elements.removeKey(index);
+ } else {
+ this.elements.put(index, value);
+ }
+ }
+
+ /**
+ * Sets up a matrix with a given number of slices and rows.
+ *
+ * @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>(double)rows*slices > Integer.MAX_VALUE</tt>.
+ */
+ @Override
+ protected void setUp(int slices, int rows, int columns) {
+ super.setUp(slices, rows, columns);
+ this.sliceStride = 1;
+ this.rowStride = 1;
+ this.columnStride = 1;
+ this.offset = 0;
+ }
+
+ /**
+ * Self modifying version of viewDice().
+ *
+ * @throws IllegalArgumentException if some of the parameters are equal or not in range 0..2.
+ */
+ @Override
+ protected AbstractMatrix3D vDice(int axis0, int axis1, int axis2) {
+ super.vDice(axis0, axis1, axis2);
+
+ // swap offsets
+ int[][] offsets = new int[3][];
+ offsets[0] = this.sliceOffsets;
+ offsets[1] = this.rowOffsets;
+ offsets[2] = this.columnOffsets;
+
+ this.sliceOffsets = offsets[axis0];
+ this.rowOffsets = offsets[axis1];
+ this.columnOffsets = offsets[axis2];
+
+ return this;
+ }
+
+ /**
+ * Constructs and returns a new 2-dimensional <i>slice view</i> representing the slices and 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. <p> To obtain a slice view on subranges, construct a sub-ranging view (<tt>view().part(...)</tt>), then
+ * apply this method to the sub-range view. To obtain 1-dimensional views, apply this method, then apply another slice
+ * view (methods <tt>viewColumn</tt>, <tt>viewRow</tt>) on the intermediate 2-dimensional view. To obtain
+ * 1-dimensional views on subranges, apply both steps.
+ *
+ * @param column the index of the column to fix.
+ * @return a new 2-dimensional slice view.
+ * @throws IndexOutOfBoundsException if <tt>column < 0 || column >= columns()</tt>.
+ * @see #viewSlice(int)
+ * @see #viewRow(int)
+ */
+ @Override
+ public DoubleMatrix2D viewColumn(int column) {
+ checkColumn(column);
+
+ int viewRows = this.slices;
+ int viewColumns = this.rows;
+
+ int viewRowZero = sliceZero;
+ int viewColumnZero = rowZero;
+ int viewOffset = this.offset + _columnOffset(_columnRank(column));
+
+ int viewRowStride = this.sliceStride;
+ int viewColumnStride = this.rowStride;
+
+ int[] viewRowOffsets = this.sliceOffsets;
+ int[] viewColumnOffsets = this.rowOffsets;
+
+ return new SelectedSparseDoubleMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
+ viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
+ }
+
+ /**
+ * Constructs and returns a new 2-dimensional <i>slice view</i> representing the slices and 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. <p> To obtain a slice view on subranges, construct a sub-ranging view (<tt>view().part(...)</tt>), then
+ * apply this method to the sub-range view. To obtain 1-dimensional views, apply this method, then apply another slice
+ * view (methods <tt>viewColumn</tt>, <tt>viewRow</tt>) on the intermediate 2-dimensional view. To obtain
+ * 1-dimensional views on subranges, apply both steps.
+ *
+ * @param row the index of the row to fix.
+ * @return a new 2-dimensional slice view.
+ * @throws IndexOutOfBoundsException if <tt>row < 0 || row >= row()</tt>.
+ * @see #viewSlice(int)
+ * @see #viewColumn(int)
+ */
+ @Override
+ public DoubleMatrix2D viewRow(int row) {
+ checkRow(row);
+
+ int viewRows = this.slices;
+ int viewColumns = this.columns;
+
+ int viewRowZero = sliceZero;
+ int viewColumnZero = columnZero;
+ int viewOffset = this.offset + _rowOffset(_rowRank(row));
+
+ int viewRowStride = this.sliceStride;
+ int viewColumnStride = this.columnStride;
+
+ int[] viewRowOffsets = this.sliceOffsets;
+ int[] viewColumnOffsets = this.columnOffsets;
+
+ return new SelectedSparseDoubleMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
+ viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
+ }
+
+ /**
+ * 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 DoubleMatrix3D viewSelectionLike(int[] sliceOffsets, int[] rowOffsets, int[] columnOffsets) {
+ return new SelectedSparseDoubleMatrix3D(this.elements, sliceOffsets, rowOffsets, columnOffsets, this.offset);
+ }
+
+ /**
+ * Constructs and returns a new 2-dimensional <i>slice view</i> representing the rows and columns of the given slice.
+ * The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and
+ * vice-versa. <p> To obtain a slice view on subranges, construct a sub-ranging view (<tt>view().part(...)</tt>), then
+ * apply this method to the sub-range view. To obtain 1-dimensional views, apply this method, then apply another slice
+ * view (methods <tt>viewColumn</tt>, <tt>viewRow</tt>) on the intermediate 2-dimensional view. To obtain
+ * 1-dimensional views on subranges, apply both steps.
+ *
+ * @param slice the index of the slice to fix.
+ * @return a new 2-dimensional slice view.
+ * @throws IndexOutOfBoundsException if <tt>slice < 0 || slice >= slices()</tt>.
+ * @see #viewRow(int)
+ * @see #viewColumn(int)
+ */
+ @Override
+ public DoubleMatrix2D viewSlice(int slice) {
+ checkSlice(slice);
+
+ int viewRows = this.rows;
+ int viewColumns = this.columns;
+
+ int viewRowZero = rowZero;
+ int viewColumnZero = columnZero;
+ int viewOffset = this.offset + _sliceOffset(_sliceRank(slice));
+
+ int viewRowStride = this.rowStride;
+ int viewColumnStride = this.columnStride;
+
+ int[] viewRowOffsets = this.rowOffsets;
+ int[] viewColumnOffsets = this.columnOffsets;
+
+ return new SelectedSparseDoubleMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
+ viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix3D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix1D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,206 @@
+/*
+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.map.AbstractIntObjectMap;
+import org.apache.mahout.math.matrix.ObjectMatrix1D;
+import org.apache.mahout.math.matrix.ObjectMatrix2D;
+
+/**
+ * Selection view on sparse 1-d matrices holding <tt>Object</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 SelectedSparseObjectMatrix1D extends ObjectMatrix1D {
+ /*
+ * The elements of the matrix.
+ */
+ protected final AbstractIntObjectMap elements;
+
+ /** The offsets of visible indexes of this matrix. */
+ protected final int[] offsets;
+
+ /** The offset. */
+ protected int offset;
+
+ /**
+ * 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 SelectedSparseObjectMatrix1D(int size, AbstractIntObjectMap 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;
+ }
+
+ /**
+ * Constructs a matrix view with the given parameters.
+ *
+ * @param elements the cells.
+ * @param offsets The indexes of the cells that shall be visible.
+ */
+ protected SelectedSparseObjectMatrix1D(AbstractIntObjectMap elements, int[] offsets) {
+ this(offsets.length, elements, 0, 1, offsets, 0);
+ }
+
+ /**
+ * 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<0 || index>=size()</tt>.
+ *
+ * @param index the index of the cell.
+ * @return the value of the specified cell.
+ */
+ @Override
+ public Object getQuick(int index) {
+ //if (debug) if (index<0 || index>=size) checkIndex(index);
+ //return elements.get(index(index));
+ //manually inlined:
+ return elements.get(offset + offsets[zero + index * stride]);
+ }
+
+ /** Returns <tt>true</tt> if both matrices share at least one identical cell. */
+ @Override
+ protected boolean haveSharedCellsRaw(ObjectMatrix1D other) {
+ if (other instanceof SelectedSparseObjectMatrix1D) {
+ SelectedSparseObjectMatrix1D otherMatrix = (SelectedSparseObjectMatrix1D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof SparseObjectMatrix1D) {
+ SparseObjectMatrix1D otherMatrix = (SparseObjectMatrix1D) 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>DenseObjectMatrix1D</tt> the new matrix must also be
+ * of type <tt>DenseObjectMatrix1D</tt>, if the receiver is an instance of type <tt>SparseObjectMatrix1D</tt> the new
+ * matrix must also be of type <tt>SparseObjectMatrix1D</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 ObjectMatrix1D like(int size) {
+ return new SparseObjectMatrix1D(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>DenseObjectMatrix1D</tt> the new matrix must be
+ * of type <tt>DenseObjectMatrix2D</tt>, if the receiver is an instance of type <tt>SparseObjectMatrix1D</tt> the new
+ * matrix must 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.
+ * @return a new matrix of the corresponding dynamic type.
+ */
+ @Override
+ public ObjectMatrix2D like2D(int rows, int columns) {
+ return new SparseObjectMatrix2D(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<0 || index>=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, Object value) {
+ //if (debug) if (index<0 || index>=size) checkIndex(index);
+ //int i = index(index);
+ //manually inlined:
+ int i = offset + offsets[zero + index * stride];
+ if (value == null) {
+ this.elements.removeKey(i);
+ } else {
+ this.elements.put(i, 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 ObjectMatrix1D viewSelectionLike(int[] offsets) {
+ return new SelectedSparseObjectMatrix1D(this.elements, offsets);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix2D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix2D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix2D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,318 @@
+/*
+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.map.AbstractIntObjectMap;
+import org.apache.mahout.math.matrix.ObjectMatrix1D;
+import org.apache.mahout.math.matrix.ObjectMatrix2D;
+
+/**
+ * Selection view on sparse 2-d matrices holding <tt>Object</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 SelectedSparseObjectMatrix2D extends ObjectMatrix2D {
+ /*
+ * The elements of the matrix.
+ */
+ protected final AbstractIntObjectMap elements;
+
+ /** The offsets of the visible cells of this matrix. */
+ protected int[] rowOffsets;
+ protected int[] columnOffsets;
+
+ /** The offset. */
+ protected int 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 SelectedSparseObjectMatrix2D(int rows, int columns, AbstractIntObjectMap 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;
+ }
+
+ /**
+ * 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 SelectedSparseObjectMatrix2D(AbstractIntObjectMap elements, int[] rowOffsets, int[] columnOffsets,
+ int offset) {
+ this(rowOffsets.length, columnOffsets.length, elements, 0, 0, 1, 1, rowOffsets, columnOffsets, offset);
+ }
+
+ /**
+ * 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 <= column < columns() && 0 <= row < 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 Object 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.get(index(row,column));
+ //manually inlined:
+ return elements
+ .get(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(ObjectMatrix2D other) {
+ if (other instanceof SelectedSparseObjectMatrix2D) {
+ SelectedSparseObjectMatrix2D otherMatrix = (SelectedSparseObjectMatrix2D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof SparseObjectMatrix2D) {
+ SparseObjectMatrix2D otherMatrix = (SparseObjectMatrix2D) 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>DenseObjectMatrix2D</tt> the
+ * new matrix must also be of type <tt>DenseObjectMatrix2D</tt>, if the receiver is an instance of type
+ * <tt>SparseObjectMatrix2D</tt> the new matrix must also be of type <tt>SparseObjectMatrix2D</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 ObjectMatrix2D like(int rows, int columns) {
+ return new SparseObjectMatrix2D(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>DenseObjectMatrix2D</tt> the new matrix must be
+ * of type <tt>DenseObjectMatrix1D</tt>, if the receiver is an instance of type <tt>SparseObjectMatrix2D</tt> the new
+ * matrix must be of type <tt>SparseObjectMatrix1D</tt>, etc.
+ *
+ * @param size the number of cells the matrix shall have.
+ * @return a new matrix of the corresponding dynamic type.
+ */
+ @Override
+ public ObjectMatrix1D like1D(int size) {
+ return new SparseObjectMatrix1D(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>DenseObjectMatrix2D</tt> the new matrix must be of type
+ * <tt>DenseObjectMatrix1D</tt>, if the receiver is an instance of type <tt>SparseObjectMatrix2D</tt> the new matrix
+ * must be of type <tt>SparseObjectMatrix1D</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 ObjectMatrix1D 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 <= column < columns() && 0 <= row < 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, Object value) {
+ //if (debug) if (column<0 || column>=columns || row<0 || row>=rows) throw new IndexOutOfBoundsException("row:"+row+", column:"+column);
+ //int index = index(row,column);
+ //manually inlined:
+ int index = offset + rowOffsets[rowZero + row * rowStride] + columnOffsets[columnZero + column * columnStride];
+
+ if (value == null) {
+ this.elements.removeKey(index);
+ } else {
+ this.elements.put(index, 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>(Object)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 ObjectMatrix1D 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 SelectedSparseObjectMatrix1D(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 ObjectMatrix1D 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 SelectedSparseObjectMatrix1D(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 ObjectMatrix2D viewSelectionLike(int[] rowOffsets, int[] columnOffsets) {
+ return new SelectedSparseObjectMatrix2D(this.elements, rowOffsets, columnOffsets, this.offset);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix2D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix3D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix3D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix3D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,384 @@
+/*
+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.map.AbstractIntObjectMap;
+import org.apache.mahout.math.matrix.ObjectMatrix2D;
+import org.apache.mahout.math.matrix.ObjectMatrix3D;
+
+/**
+ * Selection view on sparse 3-d matrices holding <tt>Object</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 is 1 additional int addition and 3 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*(sliceIndexes.length+rowIndexes.length+columnIndexes.length)</tt>. Thus, an index view with 100 x 100 x
+ * 100 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 SelectedSparseObjectMatrix3D extends ObjectMatrix3D {
+
+ /** The elements of this matrix. */
+ protected final AbstractIntObjectMap elements;
+
+ /** The offsets of the visible cells of this matrix. */
+ protected int[] sliceOffsets;
+ protected int[] rowOffsets;
+ protected int[] columnOffsets;
+
+ /** The offset. */
+ protected int offset;
+
+ /**
+ * Constructs a matrix view with the given parameters.
+ *
+ * @param elements the cells.
+ * @param sliceOffsets The slice offsets of the cells that shall be visible.
+ * @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 SelectedSparseObjectMatrix3D(AbstractIntObjectMap elements, int[] sliceOffsets, int[] rowOffsets,
+ int[] columnOffsets, int offset) {
+ // be sure parameters are valid, we do not check...
+ int slices = sliceOffsets.length;
+ int rows = rowOffsets.length;
+ int columns = columnOffsets.length;
+ setUp(slices, rows, columns);
+
+ this.elements = elements;
+
+ this.sliceOffsets = sliceOffsets;
+ 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 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 _sliceOffset(int absRank) {
+ return sliceOffsets[absRank];
+ }
+
+ /**
+ * 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<0 || slice>=slices() || row<0 || row>=rows() || column<0 ||
+ * column>=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.get(index(slice,row,column));
+ //manually inlined:
+ return elements.get(offset + sliceOffsets[sliceZero + slice * sliceStride] + 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(ObjectMatrix3D other) {
+ if (other instanceof SelectedSparseObjectMatrix3D) {
+ SelectedSparseObjectMatrix3D otherMatrix = (SelectedSparseObjectMatrix3D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof SparseObjectMatrix3D) {
+ SparseObjectMatrix3D otherMatrix = (SparseObjectMatrix3D) 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 this.offset + super.index(slice,row,column);
+ //manually inlined:
+ return this.offset + sliceOffsets[sliceZero + slice * sliceStride] + 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 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 SparseObjectMatrix3D(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) {
+ throw new InternalError(); // this method is never called since viewRow() and viewColumn are overridden properly.
+ }
+
+ /**
+ * 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<0 || slice>=slices() || row<0 || row>=rows() || column<0 ||
+ * column>=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);
+ //int index = index(slice,row,column);
+ //manually inlined:
+ int index = offset + sliceOffsets[sliceZero + slice * sliceStride] + rowOffsets[rowZero + row * rowStride] +
+ columnOffsets[columnZero + column * columnStride];
+ if (value == null) {
+ this.elements.removeKey(index);
+ } else {
+ this.elements.put(index, value);
+ }
+ }
+
+ /**
+ * Sets up a matrix with a given number of slices and rows.
+ *
+ * @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)rows*slices > Integer.MAX_VALUE</tt>.
+ */
+ @Override
+ protected void setUp(int slices, int rows, int columns) {
+ super.setUp(slices, rows, columns);
+ this.sliceStride = 1;
+ this.rowStride = 1;
+ this.columnStride = 1;
+ this.offset = 0;
+ }
+
+ /**
+ * Self modifying version of viewDice().
+ *
+ * @throws IllegalArgumentException if some of the parameters are equal or not in range 0..2.
+ */
+ @Override
+ protected AbstractMatrix3D vDice(int axis0, int axis1, int axis2) {
+ super.vDice(axis0, axis1, axis2);
+
+ // swap offsets
+ int[][] offsets = new int[3][];
+ offsets[0] = this.sliceOffsets;
+ offsets[1] = this.rowOffsets;
+ offsets[2] = this.columnOffsets;
+
+ this.sliceOffsets = offsets[axis0];
+ this.rowOffsets = offsets[axis1];
+ this.columnOffsets = offsets[axis2];
+
+ return this;
+ }
+
+ /**
+ * Constructs and returns a new 2-dimensional <i>slice view</i> representing the slices and 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. <p> To obtain a slice view on subranges, construct a sub-ranging view (<tt>view().part(...)</tt>), then
+ * apply this method to the sub-range view. To obtain 1-dimensional views, apply this method, then apply another slice
+ * view (methods <tt>viewColumn</tt>, <tt>viewRow</tt>) on the intermediate 2-dimensional view. To obtain
+ * 1-dimensional views on subranges, apply both steps.
+ *
+ * @param column the index of the column to fix.
+ * @return a new 2-dimensional slice view.
+ * @throws IndexOutOfBoundsException if <tt>column < 0 || column >= columns()</tt>.
+ * @see #viewSlice(int)
+ * @see #viewRow(int)
+ */
+ @Override
+ public ObjectMatrix2D viewColumn(int column) {
+ checkColumn(column);
+
+ int viewRows = this.slices;
+ int viewColumns = this.rows;
+
+ int viewRowZero = sliceZero;
+ int viewColumnZero = rowZero;
+ int viewOffset = this.offset + _columnOffset(_columnRank(column));
+
+ int viewRowStride = this.sliceStride;
+ int viewColumnStride = this.rowStride;
+
+ int[] viewRowOffsets = this.sliceOffsets;
+ int[] viewColumnOffsets = this.rowOffsets;
+
+ return new SelectedSparseObjectMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
+ viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
+ }
+
+ /**
+ * Constructs and returns a new 2-dimensional <i>slice view</i> representing the slices and 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. <p> To obtain a slice view on subranges, construct a sub-ranging view (<tt>view().part(...)</tt>), then
+ * apply this method to the sub-range view. To obtain 1-dimensional views, apply this method, then apply another slice
+ * view (methods <tt>viewColumn</tt>, <tt>viewRow</tt>) on the intermediate 2-dimensional view. To obtain
+ * 1-dimensional views on subranges, apply both steps.
+ *
+ * @param row the index of the row to fix.
+ * @return a new 2-dimensional slice view.
+ * @throws IndexOutOfBoundsException if <tt>row < 0 || row >= row()</tt>.
+ * @see #viewSlice(int)
+ * @see #viewColumn(int)
+ */
+ @Override
+ public ObjectMatrix2D viewRow(int row) {
+ checkRow(row);
+
+ int viewRows = this.slices;
+ int viewColumns = this.columns;
+
+ int viewRowZero = sliceZero;
+ int viewColumnZero = columnZero;
+ int viewOffset = this.offset + _rowOffset(_rowRank(row));
+
+ int viewRowStride = this.sliceStride;
+ int viewColumnStride = this.columnStride;
+
+ int[] viewRowOffsets = this.sliceOffsets;
+ int[] viewColumnOffsets = this.columnOffsets;
+
+ return new SelectedSparseObjectMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
+ viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
+ }
+
+ /**
+ * 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 SelectedSparseObjectMatrix3D(this.elements, sliceOffsets, rowOffsets, columnOffsets, this.offset);
+ }
+
+ /**
+ * Constructs and returns a new 2-dimensional <i>slice view</i> representing the rows and columns of the given slice.
+ * The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and
+ * vice-versa. <p> To obtain a slice view on subranges, construct a sub-ranging view (<tt>view().part(...)</tt>), then
+ * apply this method to the sub-range view. To obtain 1-dimensional views, apply this method, then apply another slice
+ * view (methods <tt>viewColumn</tt>, <tt>viewRow</tt>) on the intermediate 2-dimensional view. To obtain
+ * 1-dimensional views on subranges, apply both steps.
+ *
+ * @param slice the index of the slice to fix.
+ * @return a new 2-dimensional slice view.
+ * @throws IndexOutOfBoundsException if <tt>slice < 0 || slice >= slices()</tt>.
+ * @see #viewRow(int)
+ * @see #viewColumn(int)
+ */
+ @Override
+ public ObjectMatrix2D viewSlice(int slice) {
+ checkSlice(slice);
+
+ int viewRows = this.rows;
+ int viewColumns = this.columns;
+
+ int viewRowZero = rowZero;
+ int viewColumnZero = columnZero;
+ int viewOffset = this.offset + _sliceOffset(_sliceRank(slice));
+
+ int viewRowStride = this.rowStride;
+ int viewColumnStride = this.columnStride;
+
+ int[] viewRowOffsets = this.rowOffsets;
+ int[] viewColumnOffsets = this.columnOffsets;
+
+ return new SelectedSparseObjectMatrix2D(viewRows, viewColumns, this.elements, viewRowZero, viewColumnZero,
+ viewRowStride, viewColumnStride, viewRowOffsets, viewColumnOffsets, viewOffset);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseObjectMatrix3D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SparseDoubleMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SparseDoubleMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SparseDoubleMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SparseDoubleMatrix1D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,243 @@
+/*
+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.map.AbstractIntDoubleMap;
+import org.apache.mahout.math.map.OpenIntDoubleHashMap;
+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 SparseDoubleMatrix1D extends DoubleMatrix1D {
+ /*
+ * The elements of the matrix.
+ */
+ protected final AbstractIntDoubleMap elements;
+
+ /**
+ * Constructs a matrix with a copy of the given values. 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.
+ */
+ public SparseDoubleMatrix1D(double[] values) {
+ this(values.length);
+ assign(values);
+ }
+
+ /**
+ * Constructs a matrix with a given number of cells. All entries are initially <tt>0</tt>.
+ *
+ * @param size the number of cells the matrix shall have.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+ public SparseDoubleMatrix1D(int size) {
+ this(size, size / 1000, 0.2, 0.5);
+ }
+
+ /**
+ * Constructs a matrix with a given number of parameters. All entries are initially <tt>0</tt>. For details related to
+ * memory usage see {@link org.apache.mahout.math.map.OpenIntDoubleHashMap}.
+ *
+ * @param size the number of cells the matrix shall have.
+ * @param initialCapacity the initial capacity of the hash map. If not known, set <tt>initialCapacity=0</tt> or
+ * small.
+ * @param minLoadFactor the minimum load factor of the hash map.
+ * @param maxLoadFactor the maximum load factor of the hash map.
+ * @throws IllegalArgumentException if <tt>initialCapacity < 0 || (minLoadFactor < 0.0 || minLoadFactor >= 1.0) ||
+ * (maxLoadFactor <= 0.0 || maxLoadFactor >= 1.0) || (minLoadFactor >=
+ * maxLoadFactor)</tt>.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+ public SparseDoubleMatrix1D(int size, int initialCapacity, double minLoadFactor, double maxLoadFactor) {
+ setUp(size);
+ this.elements = new OpenIntDoubleHashMap(initialCapacity, minLoadFactor, maxLoadFactor);
+ }
+
+ /**
+ * Constructs a matrix view with a given number of parameters.
+ *
+ * @param size the number of cells the matrix shall have.
+ * @param elements the cells.
+ * @param offset 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>.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+ protected SparseDoubleMatrix1D(int size, AbstractIntDoubleMap elements, int offset, int stride) {
+ setUp(size, offset, stride);
+ this.elements = elements;
+ this.isNoView = false;
+ }
+
+ /**
+ * 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 DoubleMatrix1D assign(double value) {
+ // overriden for performance only
+ if (this.isNoView && value == 0) {
+ this.elements.clear();
+ } else {
+ super.assign(value);
+ }
+ return this;
+ }
+
+ /** Returns the number of cells having non-zero values. */
+ @Override
+ public int cardinality() {
+ if (this.isNoView) {
+ return this.elements.size();
+ } else {
+ return super.cardinality();
+ }
+ }
+
+ /**
+ * Ensures that the receiver can hold at least the specified number of non-zero cells without needing to allocate new
+ * internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver. <p> This
+ * method never need be called; it is for performance tuning only. Calling this method before tt>set()</tt>ing a large
+ * number of non-zero values boosts performance, because the receiver will grow only once instead of potentially many
+ * times and hash collisions get less probable.
+ *
+ * @param minCapacity the desired minimum number of non-zero cells.
+ */
+ @Override
+ public void ensureCapacity(int minCapacity) {
+ this.elements.ensureCapacity(minCapacity);
+ }
+
+ /**
+ * 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<0 || index>=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 this.elements.get(index(index));
+ // manually inlined:
+ return elements.get(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 SelectedSparseDoubleMatrix1D) {
+ SelectedSparseDoubleMatrix1D otherMatrix = (SelectedSparseDoubleMatrix1D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof SparseDoubleMatrix1D) {
+ SparseDoubleMatrix1D otherMatrix = (SparseDoubleMatrix1D) 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) {
+ // overriden for manual inlining only
+ //return _offset(_rank(rank));
+ return 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 SparseDoubleMatrix1D(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 SparseDoubleMatrix2D(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<0 || index>=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);
+ //int i = index(index);
+ // manually inlined:
+ int i = zero + index * stride;
+ if (value == 0) {
+ this.elements.removeKey(i);
+ } else {
+ this.elements.put(i, value);
+ }
+ }
+
+ /**
+ * Releases any superfluous memory created by explicitly putting zero values into cells formerly having non-zero
+ * values; An application can use this operation to minimize the storage of the receiver. <p> <b>Background:</b> <p>
+ * Cells that <ul> <li>are never set to non-zero values do not use any memory. <li>switch from zero to non-zero state
+ * do use memory. <li>switch back from non-zero to zero state also do use memory. However, their memory can be
+ * reclaimed by calling <tt>trimToSize()</tt>. </ul> A sequence like <tt>set(i,5); set(i,0);</tt> sets a cell to
+ * non-zero state and later back to zero state. Such as sequence generates obsolete memory that is automatically
+ * reclaimed from time to time or can manually be reclaimed by calling <tt>trimToSize()</tt>. Putting zeros into cells
+ * already containing zeros does not generate obsolete memory since no memory was allocated to them in the first
+ * place.
+ */
+ @Override
+ public void trimToSize() {
+ this.elements.trimToSize();
+ }
+
+ /**
+ * 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 SelectedSparseDoubleMatrix1D(this.elements, offsets);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SparseDoubleMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native