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/11/23 16:14:38 UTC
svn commit: r883365 [35/47] - in /lucene/mahout/trunk: ./ examples/ matrix/
matrix/src/ matrix/src/main/ matrix/src/main/java/
matrix/src/main/java/org/ matrix/src/main/java/org/apache/
matrix/src/main/java/org/apache/mahout/ matrix/src/main/java/org/a...
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/package.html
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/package.html?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/package.html (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/package.html Mon Nov 23 15:14:26 2009
@@ -0,0 +1,5 @@
+<HTML>
+<BODY>
+Double matrix <i>algorithms</i> such as print formatting, sorting, partitioning and statistics.
+</BODY>
+</HTML>
\ No newline at end of file
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/package.html
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractFormatter.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractFormatter.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractFormatter.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractFormatter.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,468 @@
+/*
+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.colt.matrix.impl;
+
+/**
+Abstract base class for flexible, well human readable matrix print formatting.
+Value type independent.
+A single cell is formatted via a format string.
+Columns can be aligned left, centered, right and by decimal point.
+<p>A column can be broader than specified by the parameter <tt>minColumnWidth</tt>
+ (because a cell may not fit into that width) but a column is never smaller than
+ <tt>minColumnWidth</tt>. Normally one does not need to specify <tt>minColumnWidth</tt>.
+Cells in a row are separated by a separator string, similar separators can be set for rows and slices.
+For more info, see the concrete subclasses.
+
+@author wolfgang.hoschek@cern.ch
+@version 1.0, 09/24/99
+*/
+/**
+ * @deprecated until unit tests are in place. Until this time, this class/interface is unsupported.
+ */
+@Deprecated
+public abstract class AbstractFormatter extends org.apache.mahout.colt.PersistentObject {
+ /**
+ * The alignment string aligning the cells of a column to the left.
+ */
+ public static final String LEFT = "left";
+
+ /**
+ * The alignment string aligning the cells of a column to its center.
+ */
+ public static final String CENTER = "center";
+
+ /**
+ * The alignment string aligning the cells of a column to the right.
+ */
+ public static final String RIGHT = "right";
+
+ /**
+ * The alignment string aligning the cells of a column to the decimal point.
+ */
+ public static final String DECIMAL = "decimal";
+
+ /**
+ * The default minimum number of characters a column may have; currently <tt>1</tt>.
+ */
+ public static final int DEFAULT_MIN_COLUMN_WIDTH = 1;
+
+ /**
+ * The default string separating any two columns from another; currently <tt>" "</tt>.
+ */
+ public static final String DEFAULT_COLUMN_SEPARATOR = " ";
+
+ /**
+ * The default string separating any two rows from another; currently <tt>"\n"</tt>.
+ */
+ public static final String DEFAULT_ROW_SEPARATOR = "\n";
+
+ /**
+ * The default string separating any two slices from another; currently <tt>"\n\n"</tt>.
+ */
+ public static final String DEFAULT_SLICE_SEPARATOR = "\n\n";
+
+
+ /**
+ * The default format string for formatting a single cell value; currently <tt>"%G"</tt>.
+ */
+ protected String alignment = LEFT;
+
+ /**
+ * The default format string for formatting a single cell value; currently <tt>"%G"</tt>.
+ */
+ protected String format = "%G";
+
+ /**
+ * The default minimum number of characters a column may have; currently <tt>1</tt>.
+ */
+ protected int minColumnWidth = DEFAULT_MIN_COLUMN_WIDTH;
+
+ /**
+ * The default string separating any two columns from another; currently <tt>" "</tt>.
+ */
+ protected String columnSeparator= DEFAULT_COLUMN_SEPARATOR;
+
+ /**
+ * The default string separating any two rows from another; currently <tt>"\n"</tt>.
+ */
+ protected String rowSeparator = DEFAULT_ROW_SEPARATOR;
+
+ /**
+ * The default string separating any two slices from another; currently <tt>"\n\n"</tt>.
+ */
+ protected String sliceSeparator = DEFAULT_SLICE_SEPARATOR;
+
+ /**
+ * Tells whether String representations are to be preceded with summary of the shape; currently <tt>true</tt>.
+ */
+ protected boolean printShape = true;
+
+
+ private static String[] blanksCache; // for efficient String manipulations
+
+ protected static final FormerFactory factory = new FormerFactory();
+
+ static {
+ setupBlanksCache();
+ }
+/**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+protected AbstractFormatter() {}
+/**
+ * Modifies the strings in a column of the string matrix to be aligned (left,centered,right,decimal).
+ */
+protected void align(String[][] strings) {
+ int rows = strings.length;
+ int columns = 0;
+ if (rows>0) columns = strings[0].length;
+
+ int[] maxColWidth = new int[columns];
+ int[] maxColLead = null;
+ boolean isDecimal = alignment.equals(DECIMAL);
+ if (isDecimal) maxColLead = new int[columns];
+ //int[] maxColTrail = new int[columns];
+
+ // for each column, determine alignment parameters
+ for (int column=0; column<columns; column++) {
+ int maxWidth = minColumnWidth;
+ int maxLead = Integer.MIN_VALUE;
+ //int maxTrail = Integer.MIN_VALUE;
+ for (int row=0; row<rows; row++) {
+ String s = strings[row][column];
+ maxWidth = Math.max(maxWidth, s.length());
+ if (isDecimal) maxLead = Math.max(maxLead, lead(s));
+ //maxTrail = Math.max(maxTrail, trail(s));
+ }
+ maxColWidth[column] = maxWidth;
+ if (isDecimal) maxColLead[column] = maxLead;
+ //maxColTrail[column] = maxTrail;
+ }
+
+ // format each row according to alignment parameters
+ //StringBuffer total = new StringBuffer();
+ for (int row=0; row<rows; row++) {
+ alignRow(strings[row], maxColWidth, maxColLead);
+ }
+
+}
+/**
+ * Converts a row into a string.
+ */
+protected int alignmentCode(String alignment) {
+ //{-1,0,1,2} = {left,centered,right,decimal point}
+ if (alignment.equals(LEFT)) return -1;
+ else if (alignment.equals(CENTER)) return 0;
+ else if (alignment.equals(RIGHT)) return 1;
+ else if (alignment.equals(DECIMAL)) return 2;
+ else throw new IllegalArgumentException("unknown alignment: "+alignment);
+}
+/**
+ * Modifies the strings the string matrix to be aligned (left,centered,right,decimal).
+ */
+protected void alignRow(String[] row, int[] maxColWidth, int[] maxColLead) {
+ int align = alignmentCode(alignment); //{-1,0,1,2} = {left,centered,right,decimal point}
+ StringBuffer s = new StringBuffer();
+
+ int columns = row.length;
+ for (int column=0; column<columns; column++) {
+ s.setLength(0);
+ String c = row[column];
+ //if (alignment==1) {
+ if (alignment.equals(RIGHT)) {
+ s.append(blanks(maxColWidth[column] - s.length()));
+ s.append(c);
+ }
+ //else if (alignment==2) {
+ else if (alignment.equals(DECIMAL)) {
+ s.append(blanks(maxColLead[column] - lead(c)));
+ s.append(c);
+ s.append(blanks(maxColWidth[column] - s.length()));
+ }
+ //else if (align==0) {
+ else if (alignment.equals(CENTER)) {
+ s.append(blanks((maxColWidth[column] - c.length()) / 2));
+ s.append(c);
+ s.append(blanks(maxColWidth[column] - s.length()));
+
+ }
+ //else if (align<0) {
+ else if (alignment.equals(LEFT)) {
+ s.append(c);
+ s.append(blanks(maxColWidth[column] - s.length()));
+ }
+ else throw new InternalError();
+
+ row[column] = s.toString();
+ }
+}
+/**
+ * Returns a String with <tt>length</tt> blanks.
+ */
+protected String blanks(int length) {
+ if (length < 0) length = 0;
+ if (length < blanksCache.length) return blanksCache[length];
+
+ StringBuffer buf = new StringBuffer(length);
+ for (int k = 0; k < length; k++) {
+ buf.append(' ');
+ }
+ return buf.toString();
+}
+/**
+ * Demonstrates how to use this class.
+ */
+public static void demo1() {
+/*
+// parameters
+Object[][] values = {
+ {3, 0, -3.4, 0},
+ {5.1 ,0, +3.0123456789, 0},
+ {16.37, 0.0, 2.5, 0},
+ {-16.3, 0, -3.012345678E-4, -1},
+ {1236.3456789, 0, 7, -1.2}
+};
+String[] formats = {"%G", "%1.10G", "%f", "%1.2f", "%0.2e", null};
+
+
+// now the processing
+int size = formats.length;
+ObjectMatrix2D matrix = org.apache.mahout.colt.matrix.ObjectFactory2D.dense.make(values);
+String[] strings = new String[size];
+String[] sourceCodes = new String[size];
+String[] htmlStrings = new String[size];
+String[] htmlSourceCodes = new String[size];
+
+for (int i=0; i<size; i++) {
+ String format = formats[i];
+ strings[i] = toString(matrix,format);
+ sourceCodes[i] = toSourceCode(matrix,format);
+
+ // may not compile because of packages not included in the distribution
+ //htmlStrings[i] = org.apache.mahout.colt.matrixpattern.Converting.toHTML(strings[i]);
+ //htmlSourceCodes[i] = org.apache.mahout.colt.matrixpattern.Converting.toHTML(sourceCodes[i]);
+}
+
+System.out.println("original:\n"+toString(matrix));
+
+// may not compile because of packages not included in the distribution
+for (int i=0; i<size; i++) {
+ //System.out.println("\nhtmlString("+formats[i]+"):\n"+htmlStrings[i]);
+ //System.out.println("\nhtmlSourceCode("+formats[i]+"):\n"+htmlSourceCodes[i]);
+}
+
+for (int i=0; i<size; i++) {
+ System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
+ System.out.println("\nsourceCode("+formats[i]+"):\n"+sourceCodes[i]);
+}
+*/
+}
+/**
+ * Demonstrates how to use this class.
+ */
+public static void demo2() {
+/*
+// parameters
+Object[] values = {
+ //5, 0.0, -0.0, -Object.NaN, Object.NaN, 0.0/0.0, Object.NEGATIVE_INFINITY, Object.POSITIVE_INFINITY, Object.MIN_VALUE, Object.MAX_VALUE
+ 5, 0.0, -0.0, -Object.NaN, Object.NaN, 0.0/0.0, Object.MIN_VALUE, Object.MAX_VALUE , Object.NEGATIVE_INFINITY, Object.POSITIVE_INFINITY
+ //Object.MIN_VALUE, Object.MAX_VALUE //, Object.NEGATIVE_INFINITY, Object.POSITIVE_INFINITY
+};
+//String[] formats = {"%G", "%1.10G", "%f", "%1.2f", "%0.2e"};
+String[] formats = {"%G", "%1.19G"};
+
+
+// now the processing
+int size = formats.length;
+ObjectMatrix1D matrix = new DenseObjectMatrix1D(values);
+
+String[] strings = new String[size];
+//String[] javaStrings = new String[size];
+
+for (int i=0; i<size; i++) {
+ String format = formats[i];
+ strings[i] = toString(matrix,format);
+ for (int j=0; j<matrix.size(); j++) {
+ System.out.println(String.valueOf(matrix.get(j)));
+ }
+}
+
+System.out.println("original:\n"+toString(matrix));
+
+for (int i=0; i<size; i++) {
+ System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
+}
+*/
+}
+/**
+ * Demonstrates how to use this class.
+ */
+public static void demo3(int size, Object value) {
+}
+/**
+ * Converts a given cell to a String; no alignment considered.
+ */
+protected abstract String form(AbstractMatrix1D matrix, int index, Former formatter);
+/**
+ * Returns a string representations of all cells; no alignment considered.
+ */
+protected abstract String[][] format(AbstractMatrix2D matrix);
+/**
+ * Returns a string representations of all cells; no alignment considered.
+ */
+protected String[] formatRow(AbstractMatrix1D vector) {
+ Former formatter = null;
+ formatter = factory.create(format);
+ int s = vector.size();
+ String[] strings = new String[s];
+ for (int i=0; i<s; i++) {
+ strings[i] = form(vector,i,formatter);
+ }
+ return strings;
+}
+/**
+ * Returns the number of characters or the number of characters before the decimal point.
+ */
+protected int lead(String s) {
+ return s.length();
+}
+/**
+ * Returns a String with the given character repeated <tt>length</tt> times.
+ */
+protected String repeat(char character, int length) {
+ if (character==' ') return blanks(length);
+ if (length < 0) length = 0;
+ StringBuffer buf = new StringBuffer(length);
+ for (int k = 0; k < length; k++) {
+ buf.append(character);
+ }
+ return buf.toString();
+}
+/**
+ * Sets the column alignment (left,center,right,decimal).
+ * @param alignment the new alignment to be used; must be one of <tt>{LEFT,CENTER,RIGHT,DECIMAL}</tt>.
+ */
+public void setAlignment(String alignment) {
+ this.alignment = alignment;
+}
+/**
+ * Sets the string separating any two columns from another.
+ * @param columnSeparator the new columnSeparator to be used.
+ */
+public void setColumnSeparator(String columnSeparator) {
+ this.columnSeparator = columnSeparator;
+}
+/**
+ * Sets the way a <i>single</i> cell value is to be formatted.
+ * @param format the new format to be used.
+ */
+public void setFormat(String format) {
+ this.format = format;
+}
+/**
+ * Sets the minimum number of characters a column may have.
+ * @param minColumnWidth the new minColumnWidth to be used.
+ */
+public void setMinColumnWidth(int minColumnWidth) {
+ if (minColumnWidth<0) throw new IllegalArgumentException();
+ this.minColumnWidth = minColumnWidth;
+}
+/**
+ * Specifies whether a string representation of a matrix is to be preceded with a summary of its shape.
+ * @param printShape <tt>true</tt> shape summary is printed, otherwise not printed.
+ */
+public void setPrintShape(boolean printShape) {
+ this.printShape = printShape;
+}
+/**
+ * Sets the string separating any two rows from another.
+ * @param rowSeparator the new rowSeparator to be used.
+ */
+public void setRowSeparator(String rowSeparator) {
+ this.rowSeparator = rowSeparator;
+}
+/**
+ * Sets the string separating any two slices from another.
+ * @param sliceSeparator the new sliceSeparator to be used.
+ */
+public void setSliceSeparator(String sliceSeparator) {
+ this.sliceSeparator = sliceSeparator;
+}
+/**
+ * Cache for faster string processing.
+ */
+protected static void setupBlanksCache() {
+ // Pre-fabricate 40 static strings with 0,1,2,..,39 blanks, for usage within method blanks(length).
+ // Now, we don't need to construct and fill them on demand, and garbage collect them again.
+ // All 40 strings share the identical char[] array, only with different offset and length --> somewhat smaller static memory footprint
+ int size = 40;
+ blanksCache = new String[size];
+ StringBuffer buf = new StringBuffer(size);
+ for (int i=size; --i >= 0; ) buf.append(' ');
+ String str = buf.toString();
+ for (int i=size; --i >= 0; ) {
+ blanksCache[i] = str.substring(0,i);
+ //System.out.println(i+"-"+blanksCache[i]+"-");
+ }
+}
+/**
+ * Returns a short string representation describing the shape of the matrix.
+ */
+public static String shape(AbstractMatrix1D matrix) {
+ //return "Matrix1D of size="+matrix.size();
+ //return matrix.size()+" element matrix";
+ //return "matrix("+matrix.size()+")";
+ return matrix.size()+" matrix";
+}
+/**
+ * Returns a short string representation describing the shape of the matrix.
+ */
+public static String shape(AbstractMatrix2D matrix) {
+ return matrix.rows()+" x "+matrix.columns()+" matrix";
+}
+/**
+ * Returns a short string representation describing the shape of the matrix.
+ */
+public static String shape(AbstractMatrix3D matrix) {
+ return matrix.slices()+" x "+matrix.rows()+" x "+matrix.columns()+" matrix";
+}
+/**
+ * Returns a single string representation of the given string matrix.
+ * @param strings the matrix to be converted to a single string.
+ */
+protected String toString(String[][] strings) {
+ int rows = strings.length;
+ int columns = strings.length<=0 ? 0: strings[0].length;
+
+ StringBuffer total = new StringBuffer();
+ StringBuffer s = new StringBuffer();
+ for (int row=0; row<rows; row++) {
+ s.setLength(0);
+ for (int column=0; column<columns; column++) {
+ s.append(strings[row][column]);
+ if (column<columns-1) s.append(columnSeparator);
+ }
+ total.append(s);
+ if (row<rows-1) total.append(rowSeparator);
+ }
+
+ return total.toString();
+}
+/**
+ * Returns a string representation of the given matrix.
+ * @param matrix the matrix to convert.
+ */
+protected String toString(AbstractMatrix2D matrix) {
+ String[][] strings = this.format(matrix);
+ align(strings);
+ StringBuffer total = new StringBuffer(toString(strings));
+ if (printShape) total.insert(0, shape(matrix) + "\n");
+ return total.toString();
+}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractFormatter.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,57 @@
+/*
+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.colt.matrix.impl;
+
+/**
+Abstract base class for arbitrary-dimensional matrices holding objects or primitive data types such as <code>int</code>, <code>float</code>, etc.
+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>Note that this implementation is not synchronized.</b>
+
+@author wolfgang.hoschek@cern.ch
+@version 1.0, 09/24/99
+*/
+/**
+ * @deprecated until unit tests are in place. Until this time, this class/interface is unsupported.
+ */
+@Deprecated
+public abstract class AbstractMatrix extends org.apache.mahout.colt.PersistentObject {
+ protected boolean isNoView = true;
+ //public static boolean debug = true;
+/**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+protected AbstractMatrix() {}
+/**
+ * Ensures that the receiver can hold at least the specified number of non-zero (non-null) cells without needing to allocate new internal memory.
+ * If necessary, allocates new internal memory and increases the capacity of the receiver.
+ * <p>
+ * This default implementation does nothing. Override this method if necessary.
+ *
+ * @param minNonZeros the desired minimum number of non-zero (non-null) cells.
+ */
+public void ensureCapacity(int minNonZeros) {}
+/**
+ * Returns whether the receiver is a view or not.
+ */
+protected boolean isView() {
+ return ! this.isNoView;
+}
+/**
+ * Returns the number of cells.
+ */
+public abstract int size();
+/**
+ * Releases any superfluous internal memory. An application can use this operation to minimize the
+ * storage of the receiver.
+ * <p>
+ * This default implementation does nothing. Override this method if necessary.
+ */
+public void trimToSize() {}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix1D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix1D.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix1D.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix1D.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,201 @@
+/*
+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.colt.matrix.impl;
+
+/**
+Abstract base class for 1-d matrices (aka <i>vectors</i>) holding objects or primitive data types such as <code>int</code>, <code>double</code>, etc.
+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>Note that this implementation is not synchronized.</b>
+
+@author wolfgang.hoschek@cern.ch
+@version 1.0, 09/24/99
+*/
+/**
+ * @deprecated until unit tests are in place. Until this time, this class/interface is unsupported.
+ */
+@Deprecated
+public abstract class AbstractMatrix1D extends AbstractMatrix {
+ /** the number of cells this matrix (view) has */
+ protected int size;
+
+
+ /** the index of the first element */
+ protected int zero;
+
+ /** the number of indexes between any two elements, i.e. <tt>index(i+1) - index(i)</tt>. */
+ protected int stride;
+
+ /**
+ * Indicates non-flipped state (flip==1) or flipped state (flip==-1).
+ * see _setFlip() for further info.
+ */
+ //protected int flip;
+
+ /**
+ * Indicates non-flipped state or flipped state.
+ * see _setFlip() for further info.
+ */
+ //protected int flipMask;
+
+ // this.isNoView implies: offset==0, stride==1
+/**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+protected AbstractMatrix1D() {}
+/**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param rank the absolute rank of the element.
+ * @return the position.
+ */
+protected int _offset(int absRank) {
+ return absRank;
+}
+/**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+protected int _rank(int rank) {
+ return zero + rank*stride;
+ //return zero + ((rank+flipMask)^flipMask);
+ //return zero + rank*flip; // slower
+}
+/**
+ * Sanity check for operations requiring an index to be within bounds.
+ * @throws IndexOutOfBoundsException if <tt>index < 0 || index >= size()</tt>.
+ */
+protected void checkIndex(int index) {
+ if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Attempted to access "+toStringShort()+" at index="+index);
+}
+/**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < size())</tt> for any i=0..indexes.length()-1.
+ */
+protected void checkIndexes(int[] indexes) {
+ for (int i=indexes.length; --i >= 0; ) {
+ int index = indexes[i];
+ if (index < 0 || index >= size) checkIndex(index);
+ }
+}
+/**
+ * Checks whether the receiver contains the given range and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>index<0 || index+width>size()</tt>.
+ */
+protected void checkRange(int index, int width) {
+ if (index<0 || index+width>size)
+ throw new IndexOutOfBoundsException("index: "+index+", width: "+width+", size="+size);
+}
+/**
+ * Sanity check for operations requiring two matrices with the same size.
+ * @throws IllegalArgumentException if <tt>size() != B.size()</tt>.
+ */
+protected void checkSize(double[] B) {
+ if (size != B.length) throw new IllegalArgumentException("Incompatible sizes: "+toStringShort()+" and "+B.length);
+}
+/**
+ * Sanity check for operations requiring two matrices with the same size.
+ * @throws IllegalArgumentException if <tt>size() != B.size()</tt>.
+ */
+public void checkSize(AbstractMatrix1D B) {
+ if (size != B.size) throw new IllegalArgumentException("Incompatible sizes: "+toStringShort()+" and "+B.toStringShort());
+}
+/**
+ * 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.
+ */
+protected int index(int rank) {
+ return _offset(_rank(rank));
+}
+/**
+ * Sets up a matrix with a given number of cells.
+ * @param size the number of cells the matrix shall have.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+protected void setUp(int size) {
+ setUp(size,0,1);
+}
+/**
+ * Sets up a matrix with the given parameters.
+ * @param size the number of elements 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>.
+ * @throws IllegalArgumentException if <tt>size<0</tt>.
+ */
+protected void setUp(int size, int zero, int stride) {
+ if (size<0) throw new IllegalArgumentException("negative size");
+
+ this.size = size;
+ this.zero = zero;
+ this.stride = stride;
+ this.isNoView = true;
+}
+/**
+ * Returns the number of cells.
+ */
+public int size() {
+ return size;
+}
+/**
+ * Returns the stride of the given dimension (axis, rank).
+ *
+ * @dimension the index of the dimension.
+ * @return the stride in the given dimension.
+ * @throws IllegalArgumentException if <tt>dimension != 0</tt>.
+ */
+protected int stride(int dimension) {
+ if (dimension != 0) throw new IllegalArgumentException("invalid dimension: "+dimension+"used to access"+toStringShort());
+ return this.stride;
+}
+/**
+ * Returns a string representation of the receiver's shape.
+ */
+public String toStringShort() {
+ return AbstractFormatter.shape(this);
+}
+/**
+Self modifying version of viewFlip().
+What used to be index <tt>0</tt> is now index <tt>size()-1</tt>, ..., what used to be index <tt>size()-1</tt> is now index <tt>0</tt>.
+*/
+protected AbstractMatrix1D vFlip() {
+ if (size>0) {
+ this.zero += (this.size-1)*this.stride;
+ this.stride = - this.stride;
+ this.isNoView = false;
+ }
+ return this;
+}
+/**
+Self modifying version of viewPart().
+@throws IndexOutOfBoundsException if <tt>index<0 || index+width>size()</tt>.
+*/
+protected AbstractMatrix1D vPart(int index, int width) {
+ checkRange(index,width);
+ this.zero += this.stride * index;
+ this.size = width;
+ this.isNoView = false;
+ return this;
+}
+/**
+Self modifying version of viewStrides().
+@throws IndexOutOfBoundsException if <tt>stride <= 0</tt>.
+*/
+protected AbstractMatrix1D vStrides(int stride) {
+ if (stride<=0) throw new IndexOutOfBoundsException("illegal stride: "+stride);
+ this.stride *= stride;
+ if (this.size!=0) this.size = (this.size-1)/stride +1;
+ this.isNoView = false;
+ return this;
+}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix1D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix2D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix2D.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix2D.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix2D.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,280 @@
+/*
+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.colt.matrix.impl;
+
+/**
+Abstract base class for 2-d matrices holding objects or primitive data types such as <code>int</code>, <code>double</code>, etc.
+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>Note that this implementation is not synchronized.</b>
+
+@author wolfgang.hoschek@cern.ch
+@version 1.0, 09/24/99
+*/
+/**
+ * @deprecated until unit tests are in place. Until this time, this class/interface is unsupported.
+ */
+@Deprecated
+public abstract class AbstractMatrix2D extends AbstractMatrix {
+ /** the number of colums and rows this matrix (view) has */
+ protected int columns, rows;
+
+ /** the number of elements between two rows, i.e. <tt>index(i+1,j,k) - index(i,j,k)</tt>. */
+ protected int rowStride;
+
+ /** the number of elements between two columns, i.e. <tt>index(i,j+1,k) - index(i,j,k)</tt>. */
+ protected int columnStride;
+
+
+ /** the index of the first element */
+ protected int rowZero, columnZero;
+
+ /**
+ * Indicates non-flipped state (flip==1) or flipped state (flip==-1).
+ * see _setFlip() for further info.
+ */
+ //protected int rowFlip, columnFlip;
+
+ /**
+ * Indicates non-flipped state or flipped state.
+ * see _setFlip() for further info.
+ */
+ //protected int rowFlipMask, columnFlipMask;
+
+/**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+protected AbstractMatrix2D() {}
+/**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param rank the absolute rank of the element.
+ * @return the position.
+ */
+protected int _columnOffset(int absRank) {
+ return absRank;
+}
+/**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+protected int _columnRank(int rank) {
+ return columnZero + rank*columnStride;
+ //return columnZero + ((rank+columnFlipMask)^columnFlipMask);
+ //return columnZero + rank*columnFlip; // slower
+}
+/**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param rank the absolute rank of the element.
+ * @return the position.
+ */
+protected int _rowOffset(int absRank) {
+ return absRank;
+}
+/**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+protected int _rowRank(int rank) {
+ return rowZero + rank*rowStride;
+ //return rowZero + ((rank+rowFlipMask)^rowFlipMask);
+ //return rowZero + rank*rowFlip; // slower
+}
+/**
+ * Checks whether the receiver contains the given box and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>column<0 || width<0 || column+width>columns() || row<0 || height<0 || row+height>rows()</tt>
+ */
+protected void checkBox(int row, int column, int height, int width) {
+ if (column<0 || width<0 || column+width>columns || row<0 || height<0 || row+height>rows) throw new IndexOutOfBoundsException(toStringShort()+", column:"+column+", row:"+row+" ,width:"+width+", height:"+height);
+}
+/**
+ * Sanity check for operations requiring a column index to be within bounds.
+ * @throws IndexOutOfBoundsException if <tt>column < 0 || column >= columns()</tt>.
+ */
+protected void checkColumn(int column) {
+ if (column < 0 || column >= columns) throw new IndexOutOfBoundsException("Attempted to access "+toStringShort()+" at column="+column);
+}
+/**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < columns())</tt> for any i=0..indexes.length()-1.
+ */
+protected void checkColumnIndexes(int[] indexes) {
+ for (int i=indexes.length; --i >= 0; ) {
+ int index = indexes[i];
+ if (index < 0 || index >= columns) checkColumn(index);
+ }
+}
+/**
+ * Sanity check for operations requiring a row index to be within bounds.
+ * @throws IndexOutOfBoundsException if <tt>row < 0 || row >= rows()</tt>.
+ */
+protected void checkRow(int row) {
+ if (row < 0 || row >= rows) throw new IndexOutOfBoundsException("Attempted to access "+toStringShort()+" at row="+row);
+}
+/**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < rows())</tt> for any i=0..indexes.length()-1.
+ */
+protected void checkRowIndexes(int[] indexes) {
+ for (int i=indexes.length; --i >= 0; ) {
+ int index = indexes[i];
+ if (index < 0 || index >= rows) checkRow(index);
+ }
+}
+/**
+ * Sanity check for operations requiring two matrices with the same number of columns and rows.
+ * @throws IllegalArgumentException if <tt>columns() != B.columns() || rows() != B.rows()</tt>.
+ */
+public void checkShape(AbstractMatrix2D B) {
+ if (columns != B.columns || rows != B.rows) throw new IllegalArgumentException("Incompatible dimensions: "+toStringShort()+" and "+B.toStringShort());
+}
+/**
+ * Sanity check for operations requiring matrices with the same number of columns and rows.
+ * @throws IllegalArgumentException if <tt>columns() != B.columns() || rows() != B.rows() || columns() != C.columns() || rows() != C.rows()</tt>.
+ */
+public void checkShape(AbstractMatrix2D B, AbstractMatrix2D C) {
+ if (columns != B.columns || rows != B.rows || columns != C.columns || rows != C.rows) throw new IllegalArgumentException("Incompatible dimensions: "+toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
+}
+/**
+ * Returns the number of columns.
+ */
+public int columns() {
+ return columns;
+}
+/**
+ * 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.
+ */
+protected int index(int row, int column) {
+ return _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
+}
+/**
+ * Returns the number of rows.
+ */
+public int rows() {
+ return rows;
+}
+/**
+ * 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>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt>.
+ */
+protected void setUp(int rows, int columns) {
+ setUp(rows,columns,0,0,columns,1);
+}
+/**
+ * Sets up a matrix with a given number of rows and columns and the given strides.
+ * @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>.
+ * @throws IllegalArgumentException if <tt>rows<0 || columns<0 || (double)columns*rows > Integer.MAX_VALUE</tt> or flip's are illegal.
+ */
+protected void setUp(int rows, int columns, int rowZero, int columnZero, int rowStride, int columnStride) {
+ if (rows<0 || columns<0) throw new IllegalArgumentException("negative size");
+ this.rows = rows;
+ this.columns = columns;
+
+ this.rowZero = rowZero;
+ this.columnZero = columnZero;
+
+ this.rowStride = rowStride;
+ this.columnStride = columnStride;
+
+ this.isNoView = true;
+ if ((double)columns*rows > Integer.MAX_VALUE) throw new IllegalArgumentException("matrix too large");
+}
+/**
+ * Returns the number of cells which is <tt>rows()*columns()</tt>.
+ */
+public int size() {
+ return rows*columns;
+}
+/**
+ * Returns a string representation of the receiver's shape.
+ */
+public String toStringShort() {
+ return AbstractFormatter.shape(this);
+}
+/**
+Self modifying version of viewColumnFlip().
+*/
+protected AbstractMatrix2D vColumnFlip() {
+ if (columns>0) {
+ columnZero += (columns-1)*columnStride;
+ columnStride = -columnStride;
+ this.isNoView = false;
+ }
+ return this;
+}
+/**
+Self modifying version of viewDice().
+*/
+protected AbstractMatrix2D vDice() {
+ int tmp;
+ // swap;
+ tmp = rows; rows = columns; columns = tmp;
+ tmp = rowStride; rowStride = columnStride; columnStride = tmp;
+ tmp = rowZero; rowZero = columnZero; columnZero = tmp;
+
+ // flips stay unaffected
+
+ this.isNoView = false;
+ return this;
+}
+/**
+Self modifying version of viewPart().
+@throws IndexOutOfBoundsException if <tt>column<0 || width<0 || column+width>columns() || row<0 || height<0 || row+height>rows()</tt>
+*/
+protected AbstractMatrix2D vPart(int row, int column, int height, int width) {
+ checkBox(row,column,height,width);
+ this.rowZero += this.rowStride * row;
+ this.columnZero += this.columnStride * column;
+ this.rows = height;
+ this.columns = width;
+ this.isNoView = false;
+ return this;
+}
+/**
+Self modifying version of viewRowFlip().
+*/
+protected AbstractMatrix2D vRowFlip() {
+ if (rows>0) {
+ rowZero += (rows-1)*rowStride;
+ rowStride = -rowStride;
+ this.isNoView = false;
+ }
+ return this;
+}
+/**
+Self modifying version of viewStrides().
+@throws IndexOutOfBoundsException if <tt>rowStride<=0 || columnStride<=0</tt>.
+*/
+protected AbstractMatrix2D vStrides(int rowStride, int columnStride) {
+ if (rowStride<=0 || columnStride<=0) throw new IndexOutOfBoundsException("illegal strides: "+rowStride+", "+columnStride);
+ this.rowStride *= rowStride;
+ this.columnStride *= columnStride;
+ if (this.rows!=0) this.rows = (this.rows-1)/rowStride +1;
+ if (this.columns!=0) this.columns = (this.columns-1)/columnStride +1;
+ this.isNoView = false;
+ return this;
+}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix2D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix3D.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix3D.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix3D.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix3D.java Mon Nov 23 15:14:26 2009
@@ -0,0 +1,370 @@
+/*
+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.colt.matrix.impl;
+
+/**
+Abstract base class for 3-d matrices holding objects or primitive data types such as <code>int</code>, <code>double</code>, etc.
+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>Note that this implementation is not synchronized.</b>
+
+@author wolfgang.hoschek@cern.ch
+@version 1.0, 09/24/99
+*/
+/**
+ * @deprecated until unit tests are in place. Until this time, this class/interface is unsupported.
+ */
+@Deprecated
+public abstract class AbstractMatrix3D extends AbstractMatrix {
+ /** the number of slices this matrix (view) has */
+ protected int slices;
+
+ /** the number of rows this matrix (view) has */
+ protected int rows;
+
+ /** the number of columns this matrix (view) has */
+ protected int columns;
+
+
+ /** the number of elements between two slices, i.e. <tt>index(k+1,i,j) - index(k,i,j)</tt>. */
+ protected int sliceStride;
+
+ /** the number of elements between two rows, i.e. <tt>index(k,i+1,j) - index(k,i,j)</tt>. */
+ protected int rowStride;
+
+ /** the number of elements between two columns, i.e. <tt>index(k,i,j+1) - index(k,i,j)</tt>. */
+ protected int columnStride;
+
+ /** the index of the first element */
+ protected int sliceZero, rowZero, columnZero;
+ // this.isNoView implies: offset==0, sliceStride==rows*slices, rowStride==columns, columnStride==1
+/**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+protected AbstractMatrix3D() {}
+/**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param rank the absolute rank of the element.
+ * @return the position.
+ */
+protected int _columnOffset(int absRank) {
+ return absRank;
+}
+/**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+protected int _columnRank(int rank) {
+ return columnZero + rank*columnStride;
+}
+/**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param rank the absolute rank of the element.
+ * @return the position.
+ */
+protected int _rowOffset(int absRank) {
+ return absRank;
+}
+/**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+protected int _rowRank(int rank) {
+ return rowZero + rank*rowStride;
+}
+/**
+ * Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
+ * Default implementation. Override, if necessary.
+ *
+ * @param rank the absolute rank of the element.
+ * @return the position.
+ */
+protected int _sliceOffset(int absRank) {
+ return absRank;
+}
+/**
+ * Returns the absolute rank of the given relative rank.
+ *
+ * @param rank the relative rank of the element.
+ * @return the absolute rank of the element.
+ */
+protected int _sliceRank(int rank) {
+ return sliceZero + rank*sliceStride;
+}
+/**
+ * Checks whether the receiver contains the given box and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>row<0 || height<0 || row+height>rows || slice<0 || depth<0 || slice+depth>slices || column<0 || width<0 || column+width>columns</tt>
+ */
+protected void checkBox(int slice, int row, int column, int depth, int height, int width) {
+ if (slice<0 || depth<0 || slice+depth>slices || row<0 || height<0 || row+height>rows || column<0 || width<0 || column+width>columns) throw new IndexOutOfBoundsException(toStringShort()+", slice:"+slice+", row:"+row+" ,column:"+column+", depth:"+depth+" ,height:"+height+", width:"+width);
+}
+/**
+ * Sanity check for operations requiring a column index to be within bounds.
+ * @throws IndexOutOfBoundsException if <tt>column < 0 || column >= columns()</tt>.
+ */
+protected void checkColumn(int column) {
+ if (column < 0 || column >= columns) throw new IndexOutOfBoundsException("Attempted to access "+toStringShort()+" at column="+column);
+}
+/**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < columns())</tt> for any i=0..indexes.length()-1.
+ */
+protected void checkColumnIndexes(int[] indexes) {
+ for (int i=indexes.length; --i >= 0; ) {
+ int index = indexes[i];
+ if (index < 0 || index >= columns) checkColumn(index);
+ }
+}
+/**
+ * Sanity check for operations requiring a row index to be within bounds.
+ * @throws IndexOutOfBoundsException if <tt>row < 0 || row >= rows()</tt>.
+ */
+protected void checkRow(int row) {
+ if (row < 0 || row >= rows) throw new IndexOutOfBoundsException("Attempted to access "+toStringShort()+" at row="+row);
+}
+/**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < rows())</tt> for any i=0..indexes.length()-1.
+ */
+protected void checkRowIndexes(int[] indexes) {
+ for (int i=indexes.length; --i >= 0; ) {
+ int index = indexes[i];
+ if (index < 0 || index >= rows) checkRow(index);
+ }
+}
+/**
+ * Sanity check for operations requiring two matrices with the same number of slices, rows and columns.
+ * @throws IllegalArgumentException if <tt>slices() != B.slices() || rows() != B.rows() || columns() != B.columns()</tt>.
+ */
+public void checkShape(AbstractMatrix3D B) {
+ if (slices != B.slices || rows != B.rows || columns != B.columns) throw new IllegalArgumentException("Incompatible dimensions: "+toStringShort()+" and "+B.toStringShort());
+}
+/**
+ * Sanity check for operations requiring matrices with the same number of slices, rows and columns.
+ * @throws IllegalArgumentException if <tt>slices() != B.slices() || rows() != B.rows() || columns() != B.columns() || slices() != C.slices() || rows() != C.rows() || columns() != C.columns()</tt>.
+ */
+public void checkShape(AbstractMatrix3D B, AbstractMatrix3D C) {
+ if (slices != B.slices || rows != B.rows || columns != B.columns || slices != C.slices || rows != C.rows || columns != C.columns) throw new IllegalArgumentException("Incompatible dimensions: "+toStringShort()+", "+B.toStringShort()+", "+C.toStringShort());
+}
+/**
+ * Sanity check for operations requiring a slice index to be within bounds.
+ * @throws IndexOutOfBoundsException if <tt>slice < 0 || slice >= slices()</tt>.
+ */
+protected void checkSlice(int slice) {
+ if (slice < 0 || slice >= slices) throw new IndexOutOfBoundsException("Attempted to access "+toStringShort()+" at slice="+slice);
+}
+/**
+ * Checks whether indexes are legal and throws an exception, if necessary.
+ * @throws IndexOutOfBoundsException if <tt>! (0 <= indexes[i] < slices())</tt> for any i=0..indexes.length()-1.
+ */
+protected void checkSliceIndexes(int[] indexes) {
+ for (int i=indexes.length; --i >= 0; ) {
+ int index = indexes[i];
+ if (index < 0 || index >= slices) checkSlice(index);
+ }
+}
+/**
+ * Returns the number of columns.
+ */
+public int columns() {
+ return columns;
+}
+/**
+ * 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.
+ */
+protected int index(int slice, int row, int column) {
+ return _sliceOffset(_sliceRank(slice)) + _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
+}
+/**
+ * Returns the number of rows.
+ */
+public int rows() {
+ return rows;
+}
+/**
+ * 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>.
+ * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+ */
+protected void setUp(int slices, int rows, int columns) {
+ setUp(slices,rows,columns,0,0,0,rows*columns,columns,1);
+}
+/**
+ * Sets up a matrix with a given number of slices and rows and the given strides.
+ * @param slices the number of slices the matrix shall have.
+ * @param rows the number of rows the matrix shall have.
+ * @param columns the number of columns the matrix shall have.
+ * @param sliceZero the position of the first element.
+ * @param rowZero the position of the first element.
+ * @param columnZero the position of the first element.
+ * @param sliceStride the number of elements between two slices, i.e. <tt>index(k+1,i,j)-index(k,i,j)</tt>.
+ * @param rowStride the number of elements between two rows, i.e. <tt>index(k,i+1,j)-index(k,i,j)</tt>.
+ * @param columnnStride the number of elements between two columns, i.e. <tt>index(k,i,j+1)-index(k,i,j)</tt>.
+ * @throws IllegalArgumentException if <tt>(double)slices*rows*columnss > Integer.MAX_VALUE</tt>.
+ * @throws IllegalArgumentException if <tt>slices<0 || rows<0 || columns<0</tt>.
+ */
+protected void setUp(int slices, int rows, int columns, int sliceZero, int rowZero, int columnZero, int sliceStride, int rowStride, int columnStride) {
+ if (slices<0 || rows<0 || columns<0) throw new IllegalArgumentException("negative size");
+ if ((double)slices*rows*columns > Integer.MAX_VALUE) throw new IllegalArgumentException("matrix too large");
+
+ this.slices = slices;
+ this.rows = rows;
+ this.columns = columns;
+
+ this.sliceZero = sliceZero;
+ this.rowZero = rowZero;
+ this.columnZero = columnZero;
+
+ this.sliceStride = sliceStride;
+ this.rowStride = rowStride;
+ this.columnStride = columnStride;
+
+ this.isNoView = true;
+}
+protected int[] shape() {
+ int[] shape = new int[3];
+ shape[0] = slices;
+ shape[1] = rows;
+ shape[2] = columns;
+ return shape;
+}
+/**
+ * Returns the number of cells which is <tt>slices()*rows()*columns()</tt>.
+ */
+public int size() {
+ return slices*rows*columns;
+}
+/**
+ * Returns the number of slices.
+ */
+public int slices() {
+ return slices;
+}
+/**
+ * Returns a string representation of the receiver's shape.
+ */
+public String toStringShort() {
+ return AbstractFormatter.shape(this);
+}
+/**
+Self modifying version of viewColumnFlip().
+*/
+protected AbstractMatrix3D vColumnFlip() {
+ if (columns>0) {
+ columnZero += (columns-1)*columnStride;
+ columnStride = -columnStride;
+ this.isNoView = false;
+ }
+ return this;
+}
+/**
+Self modifying version of viewDice().
+@throws IllegalArgumentException if some of the parameters are equal or not in range 0..2.
+*/
+protected AbstractMatrix3D vDice(int axis0, int axis1, int axis2) {
+ int d = 3;
+ if (axis0 < 0 || axis0 >= d || axis1 < 0 || axis1 >= d || axis2 < 0 || axis2 >= d ||
+ axis0 == axis1 || axis0 == axis2 || axis1 == axis2) {
+ throw new IllegalArgumentException("Illegal Axes: "+axis0+", "+axis1+", "+axis2);
+ }
+
+ // swap shape
+ int[] shape = shape();
+
+ this.slices = shape[axis0];
+ this.rows = shape[axis1];
+ this.columns = shape[axis2];
+
+ // swap strides
+ int[] strides = new int[3];
+ strides[0] = this.sliceStride;
+ strides[1] = this.rowStride;
+ strides[2] = this.columnStride;
+
+ this.sliceStride = strides[axis0];
+ this.rowStride = strides[axis1];
+ this.columnStride = strides[axis2];
+
+ this.isNoView = false;
+ return this;
+}
+/**
+Self modifying version of viewPart().
+@throws IndexOutOfBoundsException if <tt>slice<0 || depth<0 || slice+depth>slices() || row<0 || height<0 || row+height>rows() || column<0 || width<0 || column+width>columns()</tt>
+*/
+protected AbstractMatrix3D vPart(int slice, int row, int column, int depth, int height, int width) {
+ checkBox(slice,row,column,depth,height,width);
+
+ this.sliceZero += this.sliceStride * slice;
+ this.rowZero += this.rowStride * row;
+ this.columnZero += this.columnStride * column;
+
+ this.slices = depth;
+ this.rows = height;
+ this.columns = width;
+
+ this.isNoView = false;
+ return this;
+}
+/**
+Self modifying version of viewRowFlip().
+*/
+protected AbstractMatrix3D vRowFlip() {
+ if (rows>0) {
+ rowZero += (rows-1)*rowStride;
+ rowStride = -rowStride;
+ this.isNoView = false;
+ }
+ return this;
+}
+/**
+Self modifying version of viewSliceFlip().
+*/
+protected AbstractMatrix3D vSliceFlip() {
+ if (slices>0) {
+ sliceZero += (slices-1)*sliceStride;
+ sliceStride = -sliceStride;
+ this.isNoView = false;
+ }
+ return this;
+}
+/**
+Self modifying version of viewStrides().
+@throws IndexOutOfBoundsException if <tt>sliceStride<=0 || rowStride<=0 || columnStride<=0</tt>.
+*/
+protected AbstractMatrix3D vStrides(int sliceStride, int rowStride, int columnStride) {
+ if (sliceStride<=0 || rowStride<=0 || columnStride<=0) throw new IndexOutOfBoundsException("illegal strides: "+sliceStride+", "+rowStride+", "+columnStride);
+
+ this.sliceStride *= sliceStride;
+ this.rowStride *= rowStride;
+ this.columnStride *= columnStride;
+
+ if (this.slices!=0) this.slices = (this.slices-1)/sliceStride +1;
+ if (this.rows!=0) this.rows = (this.rows-1)/rowStride +1;
+ if (this.columns!=0) this.columns = (this.columns-1)/columnStride +1;
+
+ this.isNoView = false;
+ return this;
+}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix3D.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/Benchmark.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/Benchmark.java?rev=883365&view=auto
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/Benchmark.java (added)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/Benchmark.java Mon Nov 23 15:14:26 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.colt.matrix.impl;
+
+import org.apache.mahout.colt.matrix.DoubleFactory2D;
+import org.apache.mahout.colt.matrix.DoubleMatrix2D;
+/**
+Benchmarks the performance of matrix algorithms.
+
+@author wolfgang.hoschek@cern.ch
+@version 1.0, 09/24/99
+*/
+class Benchmark {
+/**
+ * Makes this class non instantiable, but still let's others inherit from it.
+ */
+protected Benchmark() {
+ throw new RuntimeException("Non instantiable");
+}
+/**
+ * Runs a bench on matrices holding double elements.
+ */
+public static void benchmark(int runs, int size, String kind, boolean print, int initialCapacity, double minLoadFactor, double maxLoadFactor, double percentNonZero) {
+ // certain loops need to be constructed so that the jitter can't optimize them away and we get fantastic numbers.
+ // this involves primarly read-loops
+
+ org.apache.mahout.colt.Timer timer1 = new org.apache.mahout.colt.Timer();
+ org.apache.mahout.colt.Timer timer2 = new org.apache.mahout.colt.Timer();
+ org.apache.mahout.colt.Timer timer3 = new org.apache.mahout.colt.Timer();
+ org.apache.mahout.colt.Timer timer4 = new org.apache.mahout.colt.Timer();
+ org.apache.mahout.colt.Timer timer5 = new org.apache.mahout.colt.Timer();
+ org.apache.mahout.colt.Timer timer6 = new org.apache.mahout.colt.Timer();
+
+ DoubleMatrix2D matrix = null;
+ if (kind.equals("sparse")) matrix = new SparseDoubleMatrix2D(size,size,initialCapacity,minLoadFactor,maxLoadFactor);
+ else if (kind.equals("dense")) matrix = org.apache.mahout.colt.matrix.DoubleFactory2D.dense.make(size,size);
+ //else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(size,size);
+ else throw new RuntimeException("unknown kind");
+
+ System.out.println("\nNow initializing...");
+ //Matrix AJ = new Matrix(columnwise,3);
+ //Basic.random(matrix, new org.apache.mahout.jet.random.Uniform(new org.apache.mahout.jet.random.engine.MersenneTwister()));
+ double value = 2;
+ DoubleMatrix2D tmp = DoubleFactory2D.dense.sample(matrix.rows(), matrix.columns(), value, percentNonZero);
+ matrix.assign(tmp);
+ tmp = null;
+ /*
+ long NN = matrix.size();
+ int nn = (int) (NN*percentNonZero);
+ long[] nonZeroIndexes = new long[nn];
+ org.apache.mahout.jet.random.sampling.RandomSampler sampler = new org.apache.mahout.jet.random.sampling.RandomSampler(nn,NN,0,new org.apache.mahout.jet.random.engine.MersenneTwister());
+ sampler.nextBlock(nn,nonZeroIndexes,0);
+ for (int i=nn; --i >=0; ) {
+ int row = (int) (nonZeroIndexes[i]/size);
+ int column = (int) (nonZeroIndexes[i]%size);
+ matrix.set(row,column, value);
+ }
+ */
+
+ /*
+ timer1.start();
+ for (int i=0; i<runs; i++) {
+ LUDecomposition LU = new LUDecomposition(matrix);
+ }
+ timer1.stop();
+ timer1.display();
+
+ {
+ Jama.Matrix jmatrix = new Jama.Matrix(matrix.toArray());
+ timer2.start();
+ for (int i=0; i<runs; i++) {
+ Jama.LUDecomposition LU = new Jama.LUDecomposition(jmatrix);
+ }
+ timer2.stop();
+ timer2.display();
+ }
+ */
+ System.out.println("\ntesting...");
+ if (print) System.out.println(matrix);
+ DoubleMatrix2D dense = DoubleFactory2D.dense.make(size,size);
+ dense.assign(matrix);
+ if (! dense.equals(matrix)) throw new InternalError();
+ DoubleMatrix2D ADense = dense.copy();
+ DoubleMatrix2D BDense = dense.copy();
+ DoubleMatrix2D CDense = dense.copy();
+ ADense.zMult(BDense,CDense);
+ System.out.println("\nNext testing...");
+ /*
+ {
+ timer6.start();
+ double a = cubicLoop(runs,size);
+ timer6.stop();
+ timer6.display();
+ System.out.println(a);
+ }
+ */
+
+
+ {
+ DoubleMatrix2D A = matrix.copy();
+ DoubleMatrix2D B = matrix.copy();
+ //DoubleMatrix2D C = Basic.product(A,B);
+ DoubleMatrix2D C = matrix.copy();
+ A.zMult(B,C);
+ if (! (C.equals(CDense))) throw new InternalError();
+ C.assign(matrix);
+ System.out.println("\nNow benchmarking...");
+
+ timer3.start();
+ for (int i=0; i<runs; i++) {
+ A.zMult(B,C);
+ }
+ timer3.stop();
+ timer3.display();
+ int m = A.rows();
+ int n = A.columns();
+ int p = B.rows();
+ int reps = runs;
+ double mflops = 1.0e-3*(2.0*m*n*p*reps)/timer3.millis();
+ System.out.println("mflops: "+mflops);
+ }
+
+ /*
+ {
+ DoubleMatrix2D A = matrix.like().assign(value);
+ DoubleMatrix2D B = matrix.like().assign(value);
+ DoubleMatrix2D C = Basic.product(A,B);
+ timer5.start();
+ for (int i=0; i<runs; i++) {
+ org.apache.mahout.colt.matrix.Blas.matrixMultiply(A,B,C);
+ }
+ timer5.stop();
+ timer5.display();
+ }
+ */
+
+
+/*
+{
+ Jama.Matrix A = new Jama.Matrix(size,size);
+ Jama.Matrix B = new Jama.Matrix(size,size);
+ Jama.Matrix C;
+ timer4.start();
+ for (int i=0; i<runs; i++) {
+ C = A.times(B);
+ }
+ timer4.stop();
+ timer4.display();
+ }
+*/
+
+ if (print) System.out.println(matrix);
+
+ System.out.println("bye bye.");
+}
+/**
+ *
+ */
+protected static double cubicLoop(int runs, int size) {
+ double a = 1.123;
+ double b = 1.000000000012345;
+ for (int r=0; r<runs; r++) {
+ for (int i=size; --i >= 0; ) {
+ for (int j=size; --j >= 0; ) {
+ for (int k=size; --k >= 0; ) {
+ a *= b;
+ }
+ }
+ }
+ }
+ return a;
+}
+/**
+ * Benchmarks various matrix methods.
+ */
+public static void main(String args[]) {
+ int runs = Integer.parseInt(args[0]);
+ int rows = Integer.parseInt(args[1]);
+ int columns = Integer.parseInt(args[2]);
+ //int size = Integer.parseInt(args[3]);
+ //boolean isSparse = args[4].equals("sparse");
+ String kind = args[3];
+ int initialCapacity = Integer.parseInt(args[4]);
+ double minLoadFactor = new Double(args[5]).doubleValue();
+ double maxLoadFactor = new Double(args[6]).doubleValue();
+ boolean print = args[7].equals("print");
+ double initialValue = new Double(args[8]).doubleValue();
+ int size = rows;
+
+ benchmark(runs,size,kind,print,initialCapacity,minLoadFactor,maxLoadFactor,initialValue);
+}
+}
Propchange: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/Benchmark.java
------------------------------------------------------------------------------
svn:eol-style = native