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 [38/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/SelectedDenseDoubleMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix3D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix3D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseDoubleMatrix3D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,378 @@
+/*
+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.DoubleMatrix2D;
+import org.apache.mahout.math.matrix.DoubleMatrix3D;
+
+/**
+ * Selection view on dense 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 SelectedDenseDoubleMatrix3D extends DoubleMatrix3D {
+
+ /** The elements of this matrix. */
+ protected final double[] elements;
+
+ /** The offsets of the visible cells of this matrix. */
+ private int[] sliceOffsets;
+ private int[] rowOffsets;
+ private int[] columnOffsets;
+
+ /** The offset. */
+ private 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 SelectedDenseDoubleMatrix3D(double[] 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[index(slice,row,column)];
+ //manually inlined:
+ return elements[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 SelectedDenseDoubleMatrix3D) {
+ SelectedDenseDoubleMatrix3D otherMatrix = (SelectedDenseDoubleMatrix3D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof DenseDoubleMatrix3D) {
+ DenseDoubleMatrix3D otherMatrix = (DenseDoubleMatrix3D) 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 DenseDoubleMatrix3D(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);
+ //elements[index(slice,row,column)] = value;
+ //manually inlined:
+ elements[offset + sliceOffsets[sliceZero + slice * sliceStride] + rowOffsets[rowZero + row * rowStride] +
+ columnOffsets[columnZero + column * columnStride]] = 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 SelectedDenseDoubleMatrix2D(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 SelectedDenseDoubleMatrix2D(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 SelectedDenseDoubleMatrix3D(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 SelectedDenseDoubleMatrix2D(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/SelectedDenseDoubleMatrix3D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix1D.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.ObjectMatrix1D;
+import org.apache.mahout.math.matrix.ObjectMatrix2D;
+
+/**
+ * Selection view on dense 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 SelectedDenseObjectMatrix1D extends ObjectMatrix1D {
+
+ /** The elements of this matrix. */
+ protected final Object[] 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 SelectedDenseObjectMatrix1D(Object[] 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 SelectedDenseObjectMatrix1D(int size, Object[] 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<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[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(ObjectMatrix1D other) {
+ if (other instanceof SelectedDenseObjectMatrix1D) {
+ SelectedDenseObjectMatrix1D otherMatrix = (SelectedDenseObjectMatrix1D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof DenseObjectMatrix1D) {
+ DenseObjectMatrix1D otherMatrix = (DenseObjectMatrix1D) 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 DenseObjectMatrix1D(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 DenseObjectMatrix2D(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);
+ //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 ObjectMatrix1D viewSelectionLike(int[] offsets) {
+ return new SelectedDenseObjectMatrix1D(this.elements, offsets);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix2D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix2D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix2D.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.ObjectMatrix1D;
+import org.apache.mahout.math.matrix.ObjectMatrix2D;
+
+/**
+ * Selection view on dense 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 SelectedDenseObjectMatrix2D extends ObjectMatrix2D {
+
+ /** The elements of this matrix. */
+ protected final Object[] 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 SelectedDenseObjectMatrix2D(Object[] 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 SelectedDenseObjectMatrix2D(int rows, int columns, Object[] 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 <= 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[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(ObjectMatrix2D other) {
+ if (other instanceof SelectedDenseObjectMatrix2D) {
+ SelectedDenseObjectMatrix2D otherMatrix = (SelectedDenseObjectMatrix2D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof DenseObjectMatrix2D) {
+ DenseObjectMatrix2D otherMatrix = (DenseObjectMatrix2D) 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 DenseObjectMatrix2D(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 DenseObjectMatrix1D(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);
+ //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>(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 SelectedDenseObjectMatrix1D(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 SelectedDenseObjectMatrix1D(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 SelectedDenseObjectMatrix2D(this.elements, rowOffsets, columnOffsets, this.offset);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix2D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix3D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix3D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedDenseObjectMatrix3D.java Thu Dec 17 23:22:16 2009
@@ -0,0 +1,378 @@
+/*
+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;
+
+/**
+ * Selection view on dense 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 SelectedDenseObjectMatrix3D extends ObjectMatrix3D {
+
+ /** The elements of this matrix. */
+ protected final Object[] elements;
+
+ /** The offsets of the visible cells of this matrix. */
+ private int[] sliceOffsets;
+ private int[] rowOffsets;
+ private int[] columnOffsets;
+
+ /** The offset. */
+ private 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 SelectedDenseObjectMatrix3D(Object[] 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[index(slice,row,column)];
+ //manually inlined:
+ return elements[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 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 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 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) {
+ 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);
+ //elements[index(slice,row,column)] = value;
+ //manually inlined:
+ elements[offset + sliceOffsets[sliceZero + slice * sliceStride] + rowOffsets[rowZero + row * rowStride] +
+ columnOffsets[columnZero + column * columnStride]] = 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 SelectedDenseObjectMatrix2D(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 SelectedDenseObjectMatrix2D(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 SelectedDenseObjectMatrix3D(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 SelectedDenseObjectMatrix2D(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/SelectedDenseObjectMatrix3D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix1D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix1D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix1D.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.AbstractIntDoubleMap;
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/**
+ * Selection view on sparse 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 SelectedSparseDoubleMatrix1D extends DoubleMatrix1D {
+ /*
+ * The elements of the matrix.
+ */
+ protected final AbstractIntDoubleMap 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 SelectedSparseDoubleMatrix1D(int size, AbstractIntDoubleMap 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 SelectedSparseDoubleMatrix1D(AbstractIntDoubleMap 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 double 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(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) {
+ //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 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 = offset + offsets[zero + index * stride];
+ if (value == 0) {
+ 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 DoubleMatrix1D viewSelectionLike(int[] offsets) {
+ return new SelectedSparseDoubleMatrix1D(this.elements, offsets);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix2D.java?rev=891983&view=auto
==============================================================================
--- lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix2D.java (added)
+++ lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix2D.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.AbstractIntDoubleMap;
+import org.apache.mahout.math.matrix.DoubleMatrix1D;
+import org.apache.mahout.math.matrix.DoubleMatrix2D;
+
+/**
+ * Selection view on sparse 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 SelectedSparseDoubleMatrix2D extends DoubleMatrix2D {
+ /*
+ * The elements of the matrix.
+ */
+ protected final AbstractIntDoubleMap 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 SelectedSparseDoubleMatrix2D(int rows, int columns, AbstractIntDoubleMap 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 SelectedSparseDoubleMatrix2D(AbstractIntDoubleMap 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 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.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(DoubleMatrix2D other) {
+ if (other instanceof SelectedSparseDoubleMatrix2D) {
+ SelectedSparseDoubleMatrix2D otherMatrix = (SelectedSparseDoubleMatrix2D) other;
+ return this.elements == otherMatrix.elements;
+ } else if (other instanceof SparseDoubleMatrix2D) {
+ SparseDoubleMatrix2D otherMatrix = (SparseDoubleMatrix2D) 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 SparseDoubleMatrix2D(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);
+ }
+
+ /**
+ * 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 <= 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, double 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 == 0) {
+ 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>(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 SelectedSparseDoubleMatrix1D(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 SelectedSparseDoubleMatrix1D(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 SelectedSparseDoubleMatrix2D(this.elements, rowOffsets, columnOffsets, this.offset);
+ }
+}
Propchange: lucene/mahout/trunk/math/src/main/java/org/apache/mahout/math/matrix/impl/SelectedSparseDoubleMatrix2D.java
------------------------------------------------------------------------------
svn:eol-style = native