You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2009/11/25 04:41:31 UTC
svn commit: r883974 [8/20] - in
/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix:
./ bench/ doublealgo/ impl/ linalg/ objectalgo/
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Transform.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Transform.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Transform.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Transform.java Wed Nov 25 03:41:28 2009
@@ -46,24 +46,24 @@
*/
@Deprecated
public class Transform extends org.apache.mahout.matrix.PersistentObject {
- /**
- * Little trick to allow for "aliasing", that is, renaming this class.
- * Normally you would write
- * <pre>
- * Transform.mult(myMatrix,2);
- * Transform.plus(myMatrix,5);
- * </pre>
- * Since this class has only static methods, but no instance methods
- * you can also shorten the name "DoubleTransform" to a name that better suits you, for example "Trans".
- * <pre>
- * Transform T = Transform.transform; // kind of "alias"
- * T.mult(myMatrix,2);
- * T.plus(myMatrix,5);
- * </pre>
- */
- public static final Transform transform = new Transform();
+ /**
+ * Little trick to allow for "aliasing", that is, renaming this class.
+ * Normally you would write
+ * <pre>
+ * Transform.mult(myMatrix,2);
+ * Transform.plus(myMatrix,5);
+ * </pre>
+ * Since this class has only static methods, but no instance methods
+ * you can also shorten the name "DoubleTransform" to a name that better suits you, for example "Trans".
+ * <pre>
+ * Transform T = Transform.transform; // kind of "alias"
+ * T.mult(myMatrix,2);
+ * T.plus(myMatrix,5);
+ * </pre>
+ */
+ public static final Transform transform = new Transform();
- private static final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions; // alias
+ private static final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions; // alias
/**
* Makes this class non instantiable, but still let's others inherit from it.
@@ -75,7 +75,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D abs(DoubleMatrix1D A) {
- return A.assign(F.abs);
+ return A.assign(F.abs);
}
/**
* <tt>A[row,col] = Math.abs(A[row,col])</tt>.
@@ -83,7 +83,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D abs(DoubleMatrix2D A) {
- return A.assign(F.abs);
+ return A.assign(F.abs);
}
/**
* <tt>A = A / s <=> A[i] = A[i] / s</tt>.
@@ -92,7 +92,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D div(DoubleMatrix1D A, double s) {
- return A.assign(F.div(s));
+ return A.assign(F.div(s));
}
/**
* <tt>A = A / B <=> A[i] = A[i] / B[i]</tt>.
@@ -101,7 +101,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D div(DoubleMatrix1D A, DoubleMatrix1D B) {
- return A.assign(B,F.div);
+ return A.assign(B,F.div);
}
/**
* <tt>A = A / s <=> A[row,col] = A[row,col] / s</tt>.
@@ -110,7 +110,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D div(DoubleMatrix2D A, double s) {
- return A.assign(F.div(s));
+ return A.assign(F.div(s));
}
/**
* <tt>A = A / B <=> A[row,col] = A[row,col] / B[row,col]</tt>.
@@ -119,7 +119,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D div(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.div);
+ return A.assign(B,F.div);
}
/**
* <tt>A[row,col] = A[row,col] == s ? 1 : 0</tt>; ignores tolerance.
@@ -128,7 +128,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D equals(DoubleMatrix2D A, double s) {
- return A.assign(F.equals(s));
+ return A.assign(F.equals(s));
}
/**
* <tt>A[row,col] = A[row,col] == B[row,col] ? 1 : 0</tt>; ignores tolerance.
@@ -137,7 +137,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D equals(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.equals);
+ return A.assign(B,F.equals);
}
/**
* <tt>A[row,col] = A[row,col] > s ? 1 : 0</tt>.
@@ -146,7 +146,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D greater(DoubleMatrix2D A, double s) {
- return A.assign(F.greater(s));
+ return A.assign(F.greater(s));
}
/**
* <tt>A[row,col] = A[row,col] > B[row,col] ? 1 : 0</tt>.
@@ -155,7 +155,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D greater(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.greater);
+ return A.assign(B,F.greater);
}
/**
* <tt>A[row,col] = A[row,col] < s ? 1 : 0</tt>.
@@ -164,7 +164,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D less(DoubleMatrix2D A, double s) {
- return A.assign(F.less(s));
+ return A.assign(F.less(s));
}
/**
* <tt>A[row,col] = A[row,col] < B[row,col] ? 1 : 0</tt>.
@@ -173,7 +173,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D less(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.less);
+ return A.assign(B,F.less);
}
/**
* <tt>A = A - s <=> A[i] = A[i] - s</tt>.
@@ -182,7 +182,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D minus(DoubleMatrix1D A, double s) {
- return A.assign(F.minus(s));
+ return A.assign(F.minus(s));
}
/**
* <tt>A = A - B <=> A[i] = A[i] - B[i]</tt>.
@@ -191,7 +191,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D minus(DoubleMatrix1D A, DoubleMatrix1D B) {
- return A.assign(B,F.minus);
+ return A.assign(B,F.minus);
}
/**
* <tt>A = A - s <=> A[row,col] = A[row,col] - s</tt>.
@@ -200,7 +200,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D minus(DoubleMatrix2D A, double s) {
- return A.assign(F.minus(s));
+ return A.assign(F.minus(s));
}
/**
* <tt>A = A - B <=> A[row,col] = A[row,col] - B[row,col]</tt>.
@@ -209,7 +209,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D minus(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.minus);
+ return A.assign(B,F.minus);
}
/**
* <tt>A = A - B*s <=> A[i] = A[i] - B[i]*s</tt>.
@@ -219,7 +219,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D minusMult(DoubleMatrix1D A, DoubleMatrix1D B, double s) {
- return A.assign(B,F.minusMult(s));
+ return A.assign(B,F.minusMult(s));
}
/**
* <tt>A = A - B*s <=> A[row,col] = A[row,col] - B[row,col]*s</tt>.
@@ -229,7 +229,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D minusMult(DoubleMatrix2D A, DoubleMatrix2D B, double s) {
- return A.assign(B,F.minusMult(s));
+ return A.assign(B,F.minusMult(s));
}
/**
* <tt>A = A * s <=> A[i] = A[i] * s</tt>.
@@ -238,7 +238,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D mult(DoubleMatrix1D A, double s) {
- return A.assign(F.mult(s));
+ return A.assign(F.mult(s));
}
/**
* <tt>A = A * B <=> A[i] = A[i] * B[i]</tt>.
@@ -247,7 +247,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D mult(DoubleMatrix1D A, DoubleMatrix1D B) {
- return A.assign(B,F.mult);
+ return A.assign(B,F.mult);
}
/**
* <tt>A = A * s <=> A[row,col] = A[row,col] * s</tt>.
@@ -256,7 +256,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D mult(DoubleMatrix2D A, double s) {
- return A.assign(F.mult(s));
+ return A.assign(F.mult(s));
}
/**
* <tt>A = A * B <=> A[row,col] = A[row,col] * B[row,col]</tt>.
@@ -265,21 +265,21 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D mult(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.mult);
+ return A.assign(B,F.mult);
}
/**
* <tt>A = -A <=> A[i] = -A[i]</tt> for all cells.
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D negate(DoubleMatrix1D A) {
- return A.assign(F.mult(-1));
+ return A.assign(F.mult(-1));
}
/**
* <tt>A = -A <=> A[row,col] = -A[row,col]</tt>.
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D negate(DoubleMatrix2D A) {
- return A.assign(F.mult(-1));
+ return A.assign(F.mult(-1));
}
/**
* <tt>A = A + s <=> A[i] = A[i] + s</tt>.
@@ -288,7 +288,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D plus(DoubleMatrix1D A, double s) {
- return A.assign(F.plus(s));
+ return A.assign(F.plus(s));
}
/**
* <tt>A = A + B <=> A[i] = A[i] + B[i]</tt>.
@@ -297,7 +297,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D plus(DoubleMatrix1D A, DoubleMatrix1D B) {
- return A.assign(B,F.plus);
+ return A.assign(B,F.plus);
}
/**
* <tt>A = A + s <=> A[row,col] = A[row,col] + s</tt>.
@@ -306,7 +306,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D plus(DoubleMatrix2D A, double s) {
- return A.assign(F.plus(s));
+ return A.assign(F.plus(s));
}
/**
* <tt>A = A + B <=> A[row,col] = A[row,col] + B[row,col]</tt>.
@@ -315,7 +315,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D plus(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.plus);
+ return A.assign(B,F.plus);
}
/**
* <tt>A = A + B*s<=> A[i] = A[i] + B[i]*s</tt>.
@@ -325,7 +325,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D plusMult(DoubleMatrix1D A, DoubleMatrix1D B, double s) {
- return A.assign(B,F.plusMult(s));
+ return A.assign(B,F.plusMult(s));
}
/**
* <tt>A = A + B*s <=> A[row,col] = A[row,col] + B[row,col]*s</tt>.
@@ -335,7 +335,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D plusMult(DoubleMatrix2D A, DoubleMatrix2D B, double s) {
- return A.assign(B,F.plusMult(s));
+ return A.assign(B,F.plusMult(s));
}
/**
* <tt>A = A<sup>s</sup> <=> A[i] = Math.pow(A[i], s)</tt>.
@@ -344,7 +344,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D pow(DoubleMatrix1D A, double s) {
- return A.assign(F.pow(s));
+ return A.assign(F.pow(s));
}
/**
* <tt>A = A<sup>B</sup> <=> A[i] = Math.pow(A[i], B[i])</tt>.
@@ -353,7 +353,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix1D pow(DoubleMatrix1D A, DoubleMatrix1D B) {
- return A.assign(B,F.pow);
+ return A.assign(B,F.pow);
}
/**
* <tt>A = A<sup>s</sup> <=> A[row,col] = Math.pow(A[row,col], s)</tt>.
@@ -362,7 +362,7 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D pow(DoubleMatrix2D A, double s) {
- return A.assign(F.pow(s));
+ return A.assign(F.pow(s));
}
/**
* <tt>A = A<sup>B</sup> <=> A[row,col] = Math.pow(A[row,col], B[row,col])</tt>.
@@ -371,6 +371,6 @@
* @return <tt>A</tt> (for convenience only).
*/
public static DoubleMatrix2D pow(DoubleMatrix2D A, DoubleMatrix2D B) {
- return A.assign(B,F.pow);
+ return A.assign(B,F.pow);
}
}
Modified: 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=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractFormatter.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractFormatter.java Wed Nov 25 03:41:28 2009
@@ -27,90 +27,90 @@
*/
@Deprecated
public abstract class AbstractFormatter extends org.apache.mahout.matrix.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();
- }
+ /**
+ * 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.
*/
@@ -119,101 +119,101 @@
* 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);
- }
+ 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);
+ //{-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 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();
- }
+ 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();
+ 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.
@@ -222,11 +222,11 @@
/*
// 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}
+ {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};
@@ -240,26 +240,26 @@
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.matrix.matrixpattern.Converting.toHTML(strings[i]);
- //htmlSourceCodes[i] = org.apache.mahout.matrix.matrixpattern.Converting.toHTML(sourceCodes[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.matrix.matrixpattern.Converting.toHTML(strings[i]);
+ //htmlSourceCodes[i] = org.apache.mahout.matrix.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]);
+ //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]);
+ System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
+ System.out.println("\nsourceCode("+formats[i]+"):\n"+sourceCodes[i]);
}
*/
}
@@ -270,9 +270,9 @@
/*
// 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
+ //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"};
@@ -286,17 +286,17 @@
//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)));
- }
+ 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]);
+ System.out.println("\nstring("+formats[i]+"):\n"+strings[i]);
}
*/
}
@@ -317,152 +317,152 @@
* 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;
+ 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();
+ 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();
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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;
+ 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]+"-");
- }
+ // 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";
+ //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";
+ 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";
+ 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;
+ 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);
- }
+ 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();
+ 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();
+ String[][] strings = this.format(matrix);
+ align(strings);
+ StringBuffer total = new StringBuffer(toString(strings));
+ if (printShape) total.insert(0, shape(matrix) + "\n");
+ return total.toString();
}
}
Modified: 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=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix.java Wed Nov 25 03:41:28 2009
@@ -22,8 +22,8 @@
*/
@Deprecated
public abstract class AbstractMatrix extends org.apache.mahout.matrix.PersistentObject {
- protected boolean isNoView = true;
- //public static boolean debug = true;
+ protected boolean isNoView = true;
+ //public static boolean debug = true;
/**
* Makes this class non instantiable, but still let's others inherit from it.
*/
@@ -41,7 +41,7 @@
* Returns whether the receiver is a view or not.
*/
protected boolean isView() {
- return ! this.isNoView;
+ return ! this.isNoView;
}
/**
* Returns the number of cells.
Modified: 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=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix1D.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix1D.java Wed Nov 25 03:41:28 2009
@@ -22,29 +22,29 @@
*/
@Deprecated
public abstract class AbstractMatrix1D extends AbstractMatrix {
- /** the number of cells this matrix (view) has */
- protected int size;
+ /** 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;
+
+ /** 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
+ // this.isNoView implies: offset==0, stride==1
/**
* Makes this class non instantiable, but still let's others inherit from it.
*/
@@ -57,7 +57,7 @@
* @return the position.
*/
protected int _offset(int absRank) {
- return absRank;
+ return absRank;
}
/**
* Returns the absolute rank of the given relative rank.
@@ -66,48 +66,48 @@
* @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
+ 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);
+ 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);
- }
+ 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>.
+ * @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);
+ 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);
+ 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());
+ 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.
@@ -116,7 +116,7 @@
* @param rank the rank of the element.
*/
protected int index(int rank) {
- return _offset(_rank(rank));
+ return _offset(_rank(rank));
}
/**
* Sets up a matrix with a given number of cells.
@@ -124,7 +124,7 @@
* @throws IllegalArgumentException if <tt>size<0</tt>.
*/
protected void setUp(int size) {
- setUp(size,0,1);
+ setUp(size,0,1);
}
/**
* Sets up a matrix with the given parameters.
@@ -134,18 +134,18 @@
* @throws IllegalArgumentException if <tt>size<0</tt>.
*/
protected void setUp(int size, int zero, int stride) {
- if (size<0) throw new IllegalArgumentException("negative size");
+ if (size<0) throw new IllegalArgumentException("negative size");
- this.size = size;
- this.zero = zero;
- this.stride = stride;
- this.isNoView = true;
+ this.size = size;
+ this.zero = zero;
+ this.stride = stride;
+ this.isNoView = true;
}
/**
* Returns the number of cells.
*/
public int size() {
- return size;
+ return size;
}
/**
* Returns the stride of the given dimension (axis, rank).
@@ -155,47 +155,47 @@
* @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;
+ 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);
+ 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;
+ 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>.
+@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;
+ 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>.
+@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;
+ 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;
}
}
Modified: 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=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix2D.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix2D.java Wed Nov 25 03:41:28 2009
@@ -22,30 +22,30 @@
*/
@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 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;
+
+ /** 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 (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;
+ /**
+ * 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.
@@ -59,7 +59,7 @@
* @return the position.
*/
protected int _columnOffset(int absRank) {
- return absRank;
+ return absRank;
}
/**
* Returns the absolute rank of the given relative rank.
@@ -68,9 +68,9 @@
* @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
+ 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.
@@ -80,7 +80,7 @@
* @return the position.
*/
protected int _rowOffset(int absRank) {
- return absRank;
+ return absRank;
}
/**
* Returns the absolute rank of the given relative rank.
@@ -89,70 +89,70 @@
* @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
+ 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>
+ * @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);
+ 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);
+ 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);
- }
+ 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);
+ 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);
- }
+ 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());
+ 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());
+ 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;
+ return columns;
}
/**
* Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array.
@@ -161,22 +161,22 @@
* @param column the index of the column-coordinate.
*/
protected int index(int row, int column) {
- return _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
+ return _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
}
/**
* Returns the number of rows.
*/
public int rows() {
- return 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>.
+ * @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);
+ setUp(rows,columns,0,0,columns,1);
}
/**
* Sets up a matrix with a given number of rows and columns and the given strides.
@@ -186,95 +186,95 @@
* @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.
+ * @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;
+ 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");
+ 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;
+ return rows*columns;
}
/**
* Returns a string representation of the receiver's shape.
*/
public String toStringShort() {
- return AbstractFormatter.shape(this);
+ 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;
+ 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;
+ int tmp;
+ // swap;
+ tmp = rows; rows = columns; columns = tmp;
+ tmp = rowStride; rowStride = columnStride; columnStride = tmp;
+ tmp = rowZero; rowZero = columnZero; columnZero = tmp;
- // flips stay unaffected
+ // flips stay unaffected
- this.isNoView = false;
- return this;
+ 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>
+@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;
+ 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;
+ 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>.
+@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;
+ 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;
}
}
Modified: 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=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix3D.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/AbstractMatrix3D.java Wed Nov 25 03:41:28 2009
@@ -22,28 +22,28 @@
*/
@Deprecated
public abstract class AbstractMatrix3D extends AbstractMatrix {
- /** the number of slices this matrix (view) has */
- protected int slices;
+ /** 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 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 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 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
+ /** 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.
*/
@@ -56,7 +56,7 @@
* @return the position.
*/
protected int _columnOffset(int absRank) {
- return absRank;
+ return absRank;
}
/**
* Returns the absolute rank of the given relative rank.
@@ -65,7 +65,7 @@
* @return the absolute rank of the element.
*/
protected int _columnRank(int rank) {
- return columnZero + rank*columnStride;
+ return columnZero + rank*columnStride;
}
/**
* Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
@@ -75,7 +75,7 @@
* @return the position.
*/
protected int _rowOffset(int absRank) {
- return absRank;
+ return absRank;
}
/**
* Returns the absolute rank of the given relative rank.
@@ -84,7 +84,7 @@
* @return the absolute rank of the element.
*/
protected int _rowRank(int rank) {
- return rowZero + rank*rowStride;
+ return rowZero + rank*rowStride;
}
/**
* Returns the position of the given absolute rank within the (virtual or non-virtual) internal 1-dimensional array.
@@ -94,7 +94,7 @@
* @return the position.
*/
protected int _sliceOffset(int absRank) {
- return absRank;
+ return absRank;
}
/**
* Returns the absolute rank of the given relative rank.
@@ -103,85 +103,85 @@
* @return the absolute rank of the element.
*/
protected int _sliceRank(int rank) {
- return sliceZero + rank*sliceStride;
+ 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>
+ * @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);
+ 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);
+ 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);
- }
+ 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);
+ 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);
- }
+ 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());
+ 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());
+ 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);
+ 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);
- }
+ 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;
+ return columns;
}
/**
* Returns the position of the given coordinate within the (virtual or non-virtual) internal 1-dimensional array.
@@ -191,24 +191,24 @@
* @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));
+ return _sliceOffset(_sliceRank(slice)) + _rowOffset(_rowRank(row)) + _columnOffset(_columnRank(column));
}
/**
* Returns the number of rows.
*/
public int rows() {
- return 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>.
+ * @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);
+ 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.
@@ -221,150 +221,150 @@
* @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>.
+ * @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;
+ 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;
+ 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;
+ 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;
+ return slices*rows*columns;
}
/**
* Returns the number of slices.
*/
public int slices() {
- return slices;
+ return slices;
}
/**
* Returns a string representation of the receiver's shape.
*/
public String toStringShort() {
- return AbstractFormatter.shape(this);
+ 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;
+ 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);
- }
+ 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;
+ // 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.sliceStride = strides[axis0];
+ this.rowStride = strides[axis1];
+ this.columnStride = strides[axis2];
- this.isNoView = false;
- return this;
+ 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;
+ 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;
+ 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;
+ 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>.
+@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;
+ 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;
}
}
Modified: 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=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/Benchmark.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/impl/Benchmark.java Wed Nov 25 03:41:28 2009
@@ -21,178 +21,178 @@
* Makes this class non instantiable, but still let's others inherit from it.
*/
protected Benchmark() {
- throw new RuntimeException("Non instantiable");
+ 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
+ // 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.matrix.Timer timer1 = new org.apache.mahout.matrix.Timer();
- org.apache.mahout.matrix.Timer timer2 = new org.apache.mahout.matrix.Timer();
- org.apache.mahout.matrix.Timer timer3 = new org.apache.mahout.matrix.Timer();
- org.apache.mahout.matrix.Timer timer4 = new org.apache.mahout.matrix.Timer();
- org.apache.mahout.matrix.Timer timer5 = new org.apache.mahout.matrix.Timer();
- org.apache.mahout.matrix.Timer timer6 = new org.apache.mahout.matrix.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.matrix.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.matrix.matrix.Blas.matrixMultiply(A,B,C);
- }
- timer5.stop();
- timer5.display();
- }
- */
-
+ org.apache.mahout.matrix.Timer timer1 = new org.apache.mahout.matrix.Timer();
+ org.apache.mahout.matrix.Timer timer2 = new org.apache.mahout.matrix.Timer();
+ org.apache.mahout.matrix.Timer timer3 = new org.apache.mahout.matrix.Timer();
+ org.apache.mahout.matrix.Timer timer4 = new org.apache.mahout.matrix.Timer();
+ org.apache.mahout.matrix.Timer timer5 = new org.apache.mahout.matrix.Timer();
+ org.apache.mahout.matrix.Timer timer6 = new org.apache.mahout.matrix.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.matrix.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.matrix.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();
- }
+ 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);
+ if (print) System.out.println(matrix);
- System.out.println("bye bye.");
+ 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;
+ 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);
+ 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);
}
}