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 [6/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/Formatter.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Formatter.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Formatter.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Formatter.java Wed Nov 25 03:41:28 2009
@@ -32,128 +32,128 @@
Examples demonstrate usage on 2-d matrices. 1-d and 3-d matrices formatting works very similar.
<table border="1" cellspacing="0">
<tr align="center">
- <td>Original matrix</td>
+ <td>Original matrix</td>
</tr>
<tr>
- <td>
-
- <p><tt>double[][] values = {<br>
- {3, 0, -3.4, 0},<br>
- {5.1 ,0, +3.0123456789, 0}, <br>
- {16.37, 0.0, 2.5, 0}, <br>
- {-16.3, 0, -3.012345678E-4, -1},<br>
- {1236.3456789, 0, 7, -1.2}<br>
- };<br>
- matrix = new DenseDoubleMatrix2D(values);</tt></p>
- </td>
+ <td>
+
+ <p><tt>double[][] values = {<br>
+ {3, 0, -3.4, 0},<br>
+ {5.1 ,0, +3.0123456789, 0}, <br>
+ {16.37, 0.0, 2.5, 0}, <br>
+ {-16.3, 0, -3.012345678E-4, -1},<br>
+ {1236.3456789, 0, 7, -1.2}<br>
+ };<br>
+ matrix = new DenseDoubleMatrix2D(values);</tt></p>
+ </td>
</tr>
</table>
<p> </p>
<table border="1" cellspacing="0">
<tr align="center">
- <td><tt>format</tt></td>
- <td valign="top"><tt>Formatter.toString(matrix);</tt></td>
- <td valign="top"><tt>Formatter.toSourceCode(matrix);</tt></td>
- </tr>
- <tr>
- <td><tt>%G </tt><br>
- (default)</td>
- <td align="left" valign="top"><tt>5 x 4 matrix<br>
- 3 0 -3.4 0 <br>
- 5.1 0 3.012346 0 <br>
- 16.37 0 2.5 0 <br>
- -16.3 0 -0.000301 -1 <br>
- 1236.345679 0 7 -1.2
- </tt></td>
- <td align="left" valign="top"><tt>{<br>
- { 3 , 0, -3.4 , 0 },<br>
- { 5.1 , 0, 3.012346, 0 },<br>
- { 16.37 , 0, 2.5 , 0 },<br>
- { -16.3 , 0, -0.000301, -1 },<br>
- {1236.345679, 0, 7 , -1.2}<br>
- }; </tt></td>
- </tr>
- <tr>
- <td><tt>%1.10G</tt></td>
- <td align="left" valign="top"><tt>5 x 4 matrix<br>
- 3 0 -3.4 0 <br>
- 5.1 0 3.0123456789 0 <br>
- 16.37 0 2.5 0 <br>
- -16.3 0 -0.0003012346 -1 <br>
- 1236.3456789 0 7 -1.2
- </tt></td>
- <td align="left" valign="top"><tt>{<br>
- { 3 , 0, -3.4 , 0 },<br>
- { 5.1 , 0, 3.0123456789, 0 },<br>
- { 16.37 , 0, 2.5 , 0 },<br>
- { -16.3 , 0, -0.0003012346, -1 },<br>
- {1236.3456789, 0, 7 , -1.2}<br>
- }; </tt></td>
- </tr>
- <tr>
- <td><tt>%f</tt></td>
- <td align="left" valign="top"> <tt> 5 x 4 matrix<br>
- 3.000000 0.000000 -3.400000 0.000000<br>
- 5.100000 0.000000 3.012346 0.000000<br>
- 16.370000 0.000000 2.500000 0.000000<br>
- -16.300000 0.000000 -0.000301 -1.000000<br>
- 1236.345679 0.000000 7.000000 -1.200000 </tt> </td>
- <td align="left" valign="top"><tt> {<br>
- { 3.000000, 0.000000, -3.400000, 0.000000},<br>
- { 5.100000, 0.000000, 3.012346, 0.000000},<br>
- { 16.370000, 0.000000, 2.500000, 0.000000},<br>
- { -16.300000, 0.000000, -0.000301, -1.000000},<br>
- {1236.345679, 0.000000, 7.000000, -1.200000}<br>
- }; </tt> </td>
- </tr>
- <tr>
- <td><tt>%1.2f</tt></td>
- <td align="left" valign="top"><tt>5 x 4 matrix<br>
- 3.00 0.00 -3.40 0.00<br>
- 5.10 0.00 3.01 0.00<br>
- 16.37 0.00 2.50 0.00<br>
- -16.30 0.00 -0.00 -1.00<br>
- 1236.35 0.00 7.00 -1.20 </tt></td>
- <td align="left" valign="top"><tt>{<br>
- { 3.00, 0.00, -3.40, 0.00},<br>
- { 5.10, 0.00, 3.01, 0.00},<br>
- { 16.37, 0.00, 2.50, 0.00},<br>
- { -16.30, 0.00, -0.00, -1.00},<br>
- {1236.35, 0.00, 7.00, -1.20}<br>
- }; </tt></td>
- </tr>
- <tr>
- <td><tt>%0.2e</tt></td>
- <td align="left" valign="top"><tt>5 x 4 matrix<br>
- 3.00e+000 0.00e+000 -3.40e+000 0.00e+000<br>
- 5.10e+000 0.00e+000 3.01e+000 0.00e+000<br>
- 1.64e+001 0.00e+000 2.50e+000 0.00e+000<br>
- -1.63e+001 0.00e+000 -3.01e-004 -1.00e+000<br>
- 1.24e+003 0.00e+000 7.00e+000 -1.20e+000 </tt></td>
- <td align="left" valign="top"><tt>{<br>
- { 3.00e+000, 0.00e+000, -3.40e+000, 0.00e+000},<br>
- { 5.10e+000, 0.00e+000, 3.01e+000, 0.00e+000},<br>
- { 1.64e+001, 0.00e+000, 2.50e+000, 0.00e+000},<br>
- {-1.63e+001, 0.00e+000, -3.01e-004, -1.00e+000},<br>
- { 1.24e+003, 0.00e+000, 7.00e+000, -1.20e+000}<br>
- }; </tt></td>
- </tr>
- <tr>
- <td><tt>null</tt></td>
- <td align="left" valign="top"><tt>5 x 4 matrix <br>
- 3.0 0.0 -3.4 0.0<br>
- 5.1 0.0 3.0123456789 0.0<br>
- 16.37 0.0 2.5 0.0<br>
- -16.3 0.0 -3.012345678E-4 -1.0<br>
- 1236.3456789 0.0 7.0 -1.2
- </tt> <tt> </tt></td>
- <td align="left" valign="top"><tt> {<br>
- { 3.0 , 0.0, -3.4 , 0.0},<br>
- { 5.1 , 0.0, 3.0123456789 , 0.0},<br>
- { 16.37 , 0.0, 2.5 , 0.0},<br>
- { -16.3 , 0.0, -3.012345678E-4, -1.0},<br>
- {1236.3456789, 0.0, 7.0 , -1.2}<br>
- }; </tt> </td>
+ <td><tt>format</tt></td>
+ <td valign="top"><tt>Formatter.toString(matrix);</tt></td>
+ <td valign="top"><tt>Formatter.toSourceCode(matrix);</tt></td>
+ </tr>
+ <tr>
+ <td><tt>%G </tt><br>
+ (default)</td>
+ <td align="left" valign="top"><tt>5 x 4 matrix<br>
+ 3 0 -3.4 0 <br>
+ 5.1 0 3.012346 0 <br>
+ 16.37 0 2.5 0 <br>
+ -16.3 0 -0.000301 -1 <br>
+ 1236.345679 0 7 -1.2
+ </tt></td>
+ <td align="left" valign="top"><tt>{<br>
+ { 3 , 0, -3.4 , 0 },<br>
+ { 5.1 , 0, 3.012346, 0 },<br>
+ { 16.37 , 0, 2.5 , 0 },<br>
+ { -16.3 , 0, -0.000301, -1 },<br>
+ {1236.345679, 0, 7 , -1.2}<br>
+ }; </tt></td>
+ </tr>
+ <tr>
+ <td><tt>%1.10G</tt></td>
+ <td align="left" valign="top"><tt>5 x 4 matrix<br>
+ 3 0 -3.4 0 <br>
+ 5.1 0 3.0123456789 0 <br>
+ 16.37 0 2.5 0 <br>
+ -16.3 0 -0.0003012346 -1 <br>
+ 1236.3456789 0 7 -1.2
+ </tt></td>
+ <td align="left" valign="top"><tt>{<br>
+ { 3 , 0, -3.4 , 0 },<br>
+ { 5.1 , 0, 3.0123456789, 0 },<br>
+ { 16.37 , 0, 2.5 , 0 },<br>
+ { -16.3 , 0, -0.0003012346, -1 },<br>
+ {1236.3456789, 0, 7 , -1.2}<br>
+ }; </tt></td>
+ </tr>
+ <tr>
+ <td><tt>%f</tt></td>
+ <td align="left" valign="top"> <tt> 5 x 4 matrix<br>
+ 3.000000 0.000000 -3.400000 0.000000<br>
+ 5.100000 0.000000 3.012346 0.000000<br>
+ 16.370000 0.000000 2.500000 0.000000<br>
+ -16.300000 0.000000 -0.000301 -1.000000<br>
+ 1236.345679 0.000000 7.000000 -1.200000 </tt> </td>
+ <td align="left" valign="top"><tt> {<br>
+ { 3.000000, 0.000000, -3.400000, 0.000000},<br>
+ { 5.100000, 0.000000, 3.012346, 0.000000},<br>
+ { 16.370000, 0.000000, 2.500000, 0.000000},<br>
+ { -16.300000, 0.000000, -0.000301, -1.000000},<br>
+ {1236.345679, 0.000000, 7.000000, -1.200000}<br>
+ }; </tt> </td>
+ </tr>
+ <tr>
+ <td><tt>%1.2f</tt></td>
+ <td align="left" valign="top"><tt>5 x 4 matrix<br>
+ 3.00 0.00 -3.40 0.00<br>
+ 5.10 0.00 3.01 0.00<br>
+ 16.37 0.00 2.50 0.00<br>
+ -16.30 0.00 -0.00 -1.00<br>
+ 1236.35 0.00 7.00 -1.20 </tt></td>
+ <td align="left" valign="top"><tt>{<br>
+ { 3.00, 0.00, -3.40, 0.00},<br>
+ { 5.10, 0.00, 3.01, 0.00},<br>
+ { 16.37, 0.00, 2.50, 0.00},<br>
+ { -16.30, 0.00, -0.00, -1.00},<br>
+ {1236.35, 0.00, 7.00, -1.20}<br>
+ }; </tt></td>
+ </tr>
+ <tr>
+ <td><tt>%0.2e</tt></td>
+ <td align="left" valign="top"><tt>5 x 4 matrix<br>
+ 3.00e+000 0.00e+000 -3.40e+000 0.00e+000<br>
+ 5.10e+000 0.00e+000 3.01e+000 0.00e+000<br>
+ 1.64e+001 0.00e+000 2.50e+000 0.00e+000<br>
+ -1.63e+001 0.00e+000 -3.01e-004 -1.00e+000<br>
+ 1.24e+003 0.00e+000 7.00e+000 -1.20e+000 </tt></td>
+ <td align="left" valign="top"><tt>{<br>
+ { 3.00e+000, 0.00e+000, -3.40e+000, 0.00e+000},<br>
+ { 5.10e+000, 0.00e+000, 3.01e+000, 0.00e+000},<br>
+ { 1.64e+001, 0.00e+000, 2.50e+000, 0.00e+000},<br>
+ {-1.63e+001, 0.00e+000, -3.01e-004, -1.00e+000},<br>
+ { 1.24e+003, 0.00e+000, 7.00e+000, -1.20e+000}<br>
+ }; </tt></td>
+ </tr>
+ <tr>
+ <td><tt>null</tt></td>
+ <td align="left" valign="top"><tt>5 x 4 matrix <br>
+ 3.0 0.0 -3.4 0.0<br>
+ 5.1 0.0 3.0123456789 0.0<br>
+ 16.37 0.0 2.5 0.0<br>
+ -16.3 0.0 -3.012345678E-4 -1.0<br>
+ 1236.3456789 0.0 7.0 -1.2
+ </tt> <tt> </tt></td>
+ <td align="left" valign="top"><tt> {<br>
+ { 3.0 , 0.0, -3.4 , 0.0},<br>
+ { 5.1 , 0.0, 3.0123456789 , 0.0},<br>
+ { 16.37 , 0.0, 2.5 , 0.0},<br>
+ { -16.3 , 0.0, -3.012345678E-4, -1.0},<br>
+ {1236.3456789, 0.0, 7.0 , -1.2}<br>
+ }; </tt> </td>
</tr>
</table>
@@ -161,29 +161,29 @@
title and some statistical aggregations.</p>
<table border="1" cellspacing="0">
<tr>
- <td nowrap>
- <p><tt> double[][] values = {<br>
- {5 ,10, 20, 40 },<br>
- { 7, 8 , 6 , 7 },<br>
- {12 ,10, 20, 19 },<br>
- { 3, 1 , 5 , 6 }<br>
- }; <br>
- </tt><tt>String title = "CPU performance over time [nops/sec]";<br>
- String columnAxisName = "Year";<br>
- String rowAxisName = "CPU"; <br>
- String[] columnNames = {"1996", "1997", "1998", "1999"};<br>
- String[] rowNames = { "PowerBar", "Benzol", "Mercedes", "Sparcling"};<br>
- hep.aida.bin.BinFunctions1D F = hep.aida.bin.BinFunctions1D.functions; // alias<br>
- hep.aida.bin.BinFunction1D[] aggr = {F.mean, F.rms, F.quantile(0.25), F.median, F.quantile(0.75), F.stdDev, F.min, F.max};<br>
- String format = "%1.2G";<br>
- DoubleMatrix2D matrix = new DenseDoubleMatrix2D(values); <br>
- new Formatter(format).toTitleString(<br>
- matrix,rowNames,columnNames,rowAxisName,columnAxisName,title,aggr); </tt>
- </p>
- </td>
+ <td nowrap>
+ <p><tt> double[][] values = {<br>
+ {5 ,10, 20, 40 },<br>
+ { 7, 8 , 6 , 7 },<br>
+ {12 ,10, 20, 19 },<br>
+ { 3, 1 , 5 , 6 }<br>
+ }; <br>
+ </tt><tt>String title = "CPU performance over time [nops/sec]";<br>
+ String columnAxisName = "Year";<br>
+ String rowAxisName = "CPU"; <br>
+ String[] columnNames = {"1996", "1997", "1998", "1999"};<br>
+ String[] rowNames = { "PowerBar", "Benzol", "Mercedes", "Sparcling"};<br>
+ hep.aida.bin.BinFunctions1D F = hep.aida.bin.BinFunctions1D.functions; // alias<br>
+ hep.aida.bin.BinFunction1D[] aggr = {F.mean, F.rms, F.quantile(0.25), F.median, F.quantile(0.75), F.stdDev, F.min, F.max};<br>
+ String format = "%1.2G";<br>
+ DoubleMatrix2D matrix = new DenseDoubleMatrix2D(values); <br>
+ new Formatter(format).toTitleString(<br>
+ matrix,rowNames,columnNames,rowAxisName,columnAxisName,title,aggr); </tt>
+ </p>
+ </td>
</tr>
<tr>
- <td><tt>
+ <td><tt>
CPU performance over time [nops/sec]<br>
| Year<br>
| 1996 1997 1998 1999 | Mean RMS 25% Q. Median 75% Q. StdDev Min Max<br>
@@ -205,30 +205,30 @@
</td>
</tr>
<tr>
- <td nowrap><tt> same as above, but now without aggregations<br>
- aggr=null; </tt> </td>
+ <td nowrap><tt> same as above, but now without aggregations<br>
+ aggr=null; </tt> </td>
</tr>
<tr>
- <td><tt> CPU performance over time [nops/sec]<br>
- | Year<br>
- | 1996 1997 1998 1999<br>
- ---------------------------------<br>
- C PowerBar | 5 10 20 40 <br>
- P Benzol | 7 8 6 7 <br>
- U Mercedes | 12 10 20 19 <br>
- Sparcling | 3 1 5 6
- </tt> </td>
- </tr>
- <tr>
- <td nowrap>
- <p><tt> same as above, but now without rows labeled<br>
- aggr=null;<br>
- rowNames=null;<br>
- rowAxisName=null; </tt> </p>
- </td>
+ <td><tt> CPU performance over time [nops/sec]<br>
+ | Year<br>
+ | 1996 1997 1998 1999<br>
+ ---------------------------------<br>
+ C PowerBar | 5 10 20 40 <br>
+ P Benzol | 7 8 6 7 <br>
+ U Mercedes | 12 10 20 19 <br>
+ Sparcling | 3 1 5 6
+ </tt> </td>
+ </tr>
+ <tr>
+ <td nowrap>
+ <p><tt> same as above, but now without rows labeled<br>
+ aggr=null;<br>
+ rowNames=null;<br>
+ rowAxisName=null; </tt> </p>
+ </td>
</tr>
<tr>
- <td><tt>
+ <td><tt>
CPU performance over time [nops/sec]<br>
Year<br>
1996 1997 1998 1999<br>
@@ -275,15 +275,15 @@
* Constructs and returns a matrix formatter with format <tt>"%G"</tt>.
*/
public Formatter() {
- this("%G");
+ this("%G");
}
/**
* Constructs and returns a matrix formatter.
* @param format the given format used to convert a single cell value.
*/
public Formatter(String format) {
- setFormat(format);
- setAlignment(DECIMAL);
+ setFormat(format);
+ setAlignment(DECIMAL);
}
/**
* Demonstrates how to use this class.
@@ -291,11 +291,11 @@
public static void demo1() {
// parameters
double[][] 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};
@@ -309,26 +309,26 @@
String[] htmlSourceCodes = new String[size];
for (int i=0; i<size; i++) {
- String format = formats[i];
- strings[i] = new Formatter(format).toString(matrix);
- sourceCodes[i] = new Formatter(format).toSourceCode(matrix);
-
- // 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] = new Formatter(format).toString(matrix);
+ sourceCodes[i] = new Formatter(format).toSourceCode(matrix);
+
+ // 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"+new Formatter().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]);
}
}
@@ -338,9 +338,9 @@
public static void demo2() {
// parameters
double[] values = {
- //5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MIN_VALUE, Double.MAX_VALUE
- 5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.MIN_VALUE, Double.MAX_VALUE , Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
- //Double.MIN_VALUE, Double.MAX_VALUE //, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
+ //5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.MIN_VALUE, Double.MAX_VALUE
+ 5, 0.0, -0.0, -Double.NaN, Double.NaN, 0.0/0.0, Double.MIN_VALUE, Double.MAX_VALUE , Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
+ //Double.MIN_VALUE, Double.MAX_VALUE //, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY
};
//String[] formats = {"%G", "%1.10G", "%f", "%1.2f", "%0.2e"};
String[] formats = {"%G", "%1.19G"};
@@ -354,17 +354,17 @@
//String[] javaStrings = new String[size];
for (int i=0; i<size; i++) {
- String format = formats[i];
- strings[i] = new Formatter(format).toString(matrix);
- for (int j=0; j<matrix.size(); j++) {
- System.out.println(String.valueOf(matrix.get(j)));
- }
+ String format = formats[i];
+ strings[i] = new Formatter(format).toString(matrix);
+ for (int j=0; j<matrix.size(); j++) {
+ System.out.println(String.valueOf(matrix.get(j)));
+ }
}
System.out.println("original:\n"+new Formatter().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]);
}
}
@@ -372,43 +372,43 @@
* Demonstrates how to use this class.
*/
public static void demo3(int size, double value) {
- org.apache.mahout.matrix.Timer timer = new org.apache.mahout.matrix.Timer();
- String s;
- StringBuffer buf;
- DoubleMatrix2D matrix = org.apache.mahout.matrix.matrix.DoubleFactory2D.dense.make(size,size, value);
-
- timer.reset().start();
- buf = new StringBuffer();
- for (int i=size; --i >= 0; ) {
- for (int j=size; --j >= 0; ) {
- buf.append(matrix.getQuick(i,j));
- }
- }
- buf = null;
- timer.stop().display();
-
- timer.reset().start();
- org.apache.mahout.matrix.matrix.impl.Former format = new org.apache.mahout.matrix.matrix.impl.FormerFactory().create("%G");
- buf = new StringBuffer();
- for (int i=size; --i >= 0; ) {
- for (int j=size; --j >= 0; ) {
- buf.append(format.form(matrix.getQuick(i,j)));
- }
- }
- buf = null;
- timer.stop().display();
-
- timer.reset().start();
- s = new Formatter(null).toString(matrix);
- //System.out.println(s);
- s = null;
- timer.stop().display();
-
- timer.reset().start();
- s = new Formatter("%G").toString(matrix);
- //System.out.println(s);
- s = null;
- timer.stop().display();
+ org.apache.mahout.matrix.Timer timer = new org.apache.mahout.matrix.Timer();
+ String s;
+ StringBuffer buf;
+ DoubleMatrix2D matrix = org.apache.mahout.matrix.matrix.DoubleFactory2D.dense.make(size,size, value);
+
+ timer.reset().start();
+ buf = new StringBuffer();
+ for (int i=size; --i >= 0; ) {
+ for (int j=size; --j >= 0; ) {
+ buf.append(matrix.getQuick(i,j));
+ }
+ }
+ buf = null;
+ timer.stop().display();
+
+ timer.reset().start();
+ org.apache.mahout.matrix.matrix.impl.Former format = new org.apache.mahout.matrix.matrix.impl.FormerFactory().create("%G");
+ buf = new StringBuffer();
+ for (int i=size; --i >= 0; ) {
+ for (int j=size; --j >= 0; ) {
+ buf.append(format.form(matrix.getQuick(i,j)));
+ }
+ }
+ buf = null;
+ timer.stop().display();
+
+ timer.reset().start();
+ s = new Formatter(null).toString(matrix);
+ //System.out.println(s);
+ s = null;
+ timer.stop().display();
+
+ timer.reset().start();
+ s = new Formatter("%G").toString(matrix);
+ //System.out.println(s);
+ s = null;
+ timer.stop().display();
}
/**
* Demonstrates how to use this class.
@@ -416,16 +416,16 @@
public static void demo4() {
// parameters
double[][] 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}
};
/*
double[][] values = {
- {3, 1, },
- {5.1 ,16.37, }
+ {3, 1, },
+ {5.1 ,16.37, }
};
*/
//String[] columnNames = { "he", "", "he", "four" };
@@ -444,16 +444,16 @@
public static void demo5() {
// parameters
double[][] 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}
};
/*
double[][] values = {
- {3, 1, },
- {5.1 ,16.37, }
+ {3, 1, },
+ {5.1 ,16.37, }
};
*/
//String[] columnNames = { "he", "", "he", "four" };
@@ -472,16 +472,16 @@
public static void demo6() {
// parameters
double[][] 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}
};
/*
double[][] values = {
- {3, 1, },
- {5.1 ,16.37, }
+ {3, 1, },
+ {5.1 ,16.37, }
};
*/
//String[] columnNames = { "he", "", "he", "four" };
@@ -504,18 +504,18 @@
// parameters
/*
double[][] 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}
};
*/
double[][] values = {
- {5 ,10, 20, 40 },
- { 7, 8 , 6 , 7 },
- {12 ,10, 20, 19 },
- { 3, 1 , 5 , 6 }
+ {5 ,10, 20, 40 },
+ { 7, 8 , 6 , 7 },
+ {12 ,10, 20, 19 },
+ { 3, 1 , 5 , 6 }
};
String[] columnNames = {"1996", "1997", "1998", "1999"};
String[] rowNames = { "PowerBar", "Benzol", "Mercedes", "Sparcling"};
@@ -541,125 +541,125 @@
* Converts a given cell to a String; no alignment considered.
*/
protected String form(DoubleMatrix1D matrix, int index, Former formatter) {
- return formatter.form(matrix.get(index));
+ return formatter.form(matrix.get(index));
}
/**
* Converts a given cell to a String; no alignment considered.
*/
protected String form(AbstractMatrix1D matrix, int index, Former formatter) {
- return this.form((DoubleMatrix1D) matrix, index, formatter);
+ return this.form((DoubleMatrix1D) matrix, index, formatter);
}
/**
* Returns a string representations of all cells; no alignment considered.
*/
public String[][] format(DoubleMatrix2D matrix) {
- String[][] strings = new String[matrix.rows()][matrix.columns()];
- for (int row=matrix.rows(); --row >= 0; ) strings[row] = formatRow(matrix.viewRow(row));
- return strings;
+ String[][] strings = new String[matrix.rows()][matrix.columns()];
+ for (int row=matrix.rows(); --row >= 0; ) strings[row] = formatRow(matrix.viewRow(row));
+ return strings;
}
/**
* Returns a string representations of all cells; no alignment considered.
*/
protected String[][] format(AbstractMatrix2D matrix) {
- return this.format((DoubleMatrix2D) matrix);
+ return this.format((DoubleMatrix2D) matrix);
}
/**
* Returns the index of the decimal point.
*/
protected int indexOfDecimalPoint(String s) {
- int i = s.lastIndexOf('.');
- if (i<0) i = s.lastIndexOf('e');
- if (i<0) i = s.lastIndexOf('E');
- if (i<0) i = s.length();
- return i;
+ int i = s.lastIndexOf('.');
+ if (i<0) i = s.lastIndexOf('e');
+ if (i<0) i = s.lastIndexOf('E');
+ if (i<0) i = s.length();
+ return i;
}
/**
* Returns the number of characters before the decimal point.
*/
protected int lead(String s) {
- if (alignment.equals(DECIMAL)) return indexOfDecimalPoint(s);
- return super.lead(s);
+ if (alignment.equals(DECIMAL)) return indexOfDecimalPoint(s);
+ return super.lead(s);
}
/**
* Returns a string <tt>s</tt> such that <tt>Object[] m = s</tt> is a legal Java statement.
* @param matrix the matrix to format.
*/
public String toSourceCode(DoubleMatrix1D matrix) {
- Formatter copy = (Formatter) this.clone();
- copy.setPrintShape(false);
- copy.setColumnSeparator(", ");
- String lead = "{";
- String trail = "};";
- return lead + copy.toString(matrix) + trail;
+ Formatter copy = (Formatter) this.clone();
+ copy.setPrintShape(false);
+ copy.setColumnSeparator(", ");
+ String lead = "{";
+ String trail = "};";
+ return lead + copy.toString(matrix) + trail;
}
/**
* Returns a string <tt>s</tt> such that <tt>Object[] m = s</tt> is a legal Java statement.
* @param matrix the matrix to format.
*/
public String toSourceCode(DoubleMatrix2D matrix) {
- Formatter copy = (Formatter) this.clone();
- String b3 = blanks(3);
- copy.setPrintShape(false);
- copy.setColumnSeparator(", ");
- copy.setRowSeparator("},\n"+b3+"{");
- String lead = "{\n"+b3+"{";
- String trail = "}\n};";
- return lead + copy.toString(matrix) + trail;
+ Formatter copy = (Formatter) this.clone();
+ String b3 = blanks(3);
+ copy.setPrintShape(false);
+ copy.setColumnSeparator(", ");
+ copy.setRowSeparator("},\n"+b3+"{");
+ String lead = "{\n"+b3+"{";
+ String trail = "}\n};";
+ return lead + copy.toString(matrix) + trail;
}
/**
* Returns a string <tt>s</tt> such that <tt>Object[] m = s</tt> is a legal Java statement.
* @param matrix the matrix to format.
*/
public String toSourceCode(DoubleMatrix3D matrix) {
- Formatter copy = (Formatter) this.clone();
- String b3 = blanks(3);
- String b6 = blanks(6);
- copy.setPrintShape(false);
- copy.setColumnSeparator(", ");
- copy.setRowSeparator("},\n"+b6+"{");
- copy.setSliceSeparator("}\n"+b3+"},\n"+b3+"{\n"+b6+"{");
- String lead = "{\n"+b3+"{\n"+b6+"{";
- String trail = "}\n"+b3+"}\n}";
- return lead + copy.toString(matrix) + trail;
+ Formatter copy = (Formatter) this.clone();
+ String b3 = blanks(3);
+ String b6 = blanks(6);
+ copy.setPrintShape(false);
+ copy.setColumnSeparator(", ");
+ copy.setRowSeparator("},\n"+b6+"{");
+ copy.setSliceSeparator("}\n"+b3+"},\n"+b3+"{\n"+b6+"{");
+ String lead = "{\n"+b3+"{\n"+b6+"{";
+ String trail = "}\n"+b3+"}\n}";
+ return lead + copy.toString(matrix) + trail;
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
public String toString(DoubleMatrix1D matrix) {
- DoubleMatrix2D easy = matrix.like2D(1,matrix.size());
- easy.viewRow(0).assign(matrix);
- return toString(easy);
+ DoubleMatrix2D easy = matrix.like2D(1,matrix.size());
+ easy.viewRow(0).assign(matrix);
+ return toString(easy);
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
public String toString(DoubleMatrix2D matrix) {
- return super.toString(matrix);
+ return super.toString(matrix);
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
public String toString(DoubleMatrix3D matrix) {
- StringBuffer buf = new StringBuffer();
- boolean oldPrintShape = this.printShape;
- this.printShape = false;
- for (int slice=0; slice < matrix.slices(); slice++) {
- if (slice!=0) buf.append(sliceSeparator);
- buf.append(toString(matrix.viewSlice(slice)));
- }
- this.printShape = oldPrintShape;
- if (printShape) buf.insert(0,shape(matrix) + "\n");
- return buf.toString();
+ StringBuffer buf = new StringBuffer();
+ boolean oldPrintShape = this.printShape;
+ this.printShape = false;
+ for (int slice=0; slice < matrix.slices(); slice++) {
+ if (slice!=0) buf.append(sliceSeparator);
+ buf.append(toString(matrix.viewSlice(slice)));
+ }
+ this.printShape = oldPrintShape;
+ if (printShape) buf.insert(0,shape(matrix) + "\n");
+ return buf.toString();
}
/**
* Returns a string representation of the given matrix.
* @param matrix the matrix to convert.
*/
protected String toString(AbstractMatrix2D matrix) {
- return this.toString((DoubleMatrix2D) matrix);
+ return this.toString((DoubleMatrix2D) matrix);
}
/**
Returns a string representation of the given matrix with axis as well as rows and columns labeled.
@@ -674,13 +674,13 @@
@return the matrix converted to a string.
*/
protected String toTitleString(DoubleMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title) {
- if (matrix.size()==0) return "Empty matrix";
- String[][] s = format(matrix);
- //String oldAlignment = this.alignment;
- //this.alignment = DECIMAL;
- align(s);
- //this.alignment = oldAlignment;
- return new org.apache.mahout.matrix.matrix.objectalgo.Formatter().toTitleString(org.apache.mahout.matrix.matrix.ObjectFactory2D.dense.make(s), rowNames,columnNames,rowAxisName,columnAxisName,title);
+ if (matrix.size()==0) return "Empty matrix";
+ String[][] s = format(matrix);
+ //String oldAlignment = this.alignment;
+ //this.alignment = DECIMAL;
+ align(s);
+ //this.alignment = oldAlignment;
+ return new org.apache.mahout.matrix.matrix.objectalgo.Formatter().toTitleString(org.apache.mahout.matrix.matrix.ObjectFactory2D.dense.make(s), rowNames,columnNames,rowAxisName,columnAxisName,title);
}
/**
Same as <tt>toTitleString</tt> except that additionally statistical aggregates (mean, median, sum, etc.) of rows and columns are printed.
@@ -698,65 +698,65 @@
@see hep.aida.bin.BinFunctions1D
public String toTitleString(DoubleMatrix2D matrix, String[] rowNames, String[] columnNames, String rowAxisName, String columnAxisName, String title, hep.aida.bin.BinFunction1D[] aggr) {
- if (matrix.size()==0) return "Empty matrix";
- if (aggr==null || aggr.length==0) return toTitleString(matrix,rowNames,columnNames,rowAxisName,columnAxisName,title);
-
- DoubleMatrix2D rowStats = matrix.like(matrix.rows(), aggr.length); // hold row aggregations
- DoubleMatrix2D colStats = matrix.like(aggr.length, matrix.columns()); // hold column aggregations
-
- org.apache.mahout.matrix.matrix.doublealgo.Statistic.aggregate(matrix, aggr, colStats); // aggregate an entire column at a time
- org.apache.mahout.matrix.matrix.doublealgo.Statistic.aggregate(matrix.viewDice(), aggr, rowStats.viewDice()); // aggregate an entire row at a time
-
- // turn into strings
- // tmp holds "matrix" plus "colStats" below (needed so that numbers in a columns can be decimal point aligned)
- DoubleMatrix2D tmp = matrix.like(matrix.rows()+aggr.length, matrix.columns());
- tmp.viewPart(0,0,matrix.rows(),matrix.columns()).assign(matrix);
- tmp.viewPart(matrix.rows(),0,aggr.length,matrix.columns()).assign(colStats);
- colStats = null;
-
- String[][] s1 = format(tmp); align(s1); tmp = null;
- String[][] s2 = format(rowStats); align(s2); rowStats = null;
-
- // copy strings into a large matrix holding the source matrix and all aggregations
- org.apache.mahout.matrix.matrix.ObjectMatrix2D allStats = org.apache.mahout.matrix.matrix.ObjectFactory2D.dense.make(matrix.rows()+aggr.length, matrix.columns()+aggr.length+1);
- allStats.viewPart(0,0,matrix.rows()+aggr.length,matrix.columns()).assign(s1);
- allStats.viewColumn(matrix.columns()).assign("|");
- allStats.viewPart(0,matrix.columns()+1,matrix.rows(),aggr.length).assign(s2);
- s1 = null; s2 = null;
-
- // append a vertical "|" separator plus names of aggregation functions to line holding columnNames
- if (columnNames!=null) {
- org.apache.mahout.matrix.list.ObjectArrayList list = new org.apache.mahout.matrix.list.ObjectArrayList(columnNames);
- list.add("|");
- for (int i=0; i<aggr.length; i++) list.add(aggr[i].name()); // add names of aggregation functions
- columnNames = new String[list.size()];
- list.toArray(columnNames);
- }
-
- // append names of aggregation functions to line holding rowNames
- if (rowNames!=null) {
- org.apache.mahout.matrix.list.ObjectArrayList list = new org.apache.mahout.matrix.list.ObjectArrayList(rowNames);
- for (int i=0; i<aggr.length; i++) list.add(aggr[i].name()); // add names of aggregation functions
- rowNames = new String[list.size()];
- list.toArray(rowNames);
- }
-
- // turn large matrix into string
- String s = new org.apache.mahout.matrix.matrix.objectalgo.Formatter().toTitleString(allStats, rowNames,columnNames,rowAxisName,columnAxisName,title);
-
- // insert a horizontal "----------------------" separation line above the column stats
- // determine insertion position and line width
- int last = s.length()+1;
- int secondLast = last;
- int v = Math.max(0, rowAxisName==null ? 0 : rowAxisName.length()-matrix.rows()-aggr.length);
- for (int k=0; k<aggr.length+1+v; k++) { // scan "aggr.length+1+v" lines backwards
- secondLast = last;
- last = s.lastIndexOf(rowSeparator, last-1);
- }
- StringBuffer buf = new StringBuffer(s);
- buf.insert(secondLast,rowSeparator+repeat('-',secondLast-last-1));
-
- return buf.toString();
+ if (matrix.size()==0) return "Empty matrix";
+ if (aggr==null || aggr.length==0) return toTitleString(matrix,rowNames,columnNames,rowAxisName,columnAxisName,title);
+
+ DoubleMatrix2D rowStats = matrix.like(matrix.rows(), aggr.length); // hold row aggregations
+ DoubleMatrix2D colStats = matrix.like(aggr.length, matrix.columns()); // hold column aggregations
+
+ org.apache.mahout.matrix.matrix.doublealgo.Statistic.aggregate(matrix, aggr, colStats); // aggregate an entire column at a time
+ org.apache.mahout.matrix.matrix.doublealgo.Statistic.aggregate(matrix.viewDice(), aggr, rowStats.viewDice()); // aggregate an entire row at a time
+
+ // turn into strings
+ // tmp holds "matrix" plus "colStats" below (needed so that numbers in a columns can be decimal point aligned)
+ DoubleMatrix2D tmp = matrix.like(matrix.rows()+aggr.length, matrix.columns());
+ tmp.viewPart(0,0,matrix.rows(),matrix.columns()).assign(matrix);
+ tmp.viewPart(matrix.rows(),0,aggr.length,matrix.columns()).assign(colStats);
+ colStats = null;
+
+ String[][] s1 = format(tmp); align(s1); tmp = null;
+ String[][] s2 = format(rowStats); align(s2); rowStats = null;
+
+ // copy strings into a large matrix holding the source matrix and all aggregations
+ org.apache.mahout.matrix.matrix.ObjectMatrix2D allStats = org.apache.mahout.matrix.matrix.ObjectFactory2D.dense.make(matrix.rows()+aggr.length, matrix.columns()+aggr.length+1);
+ allStats.viewPart(0,0,matrix.rows()+aggr.length,matrix.columns()).assign(s1);
+ allStats.viewColumn(matrix.columns()).assign("|");
+ allStats.viewPart(0,matrix.columns()+1,matrix.rows(),aggr.length).assign(s2);
+ s1 = null; s2 = null;
+
+ // append a vertical "|" separator plus names of aggregation functions to line holding columnNames
+ if (columnNames!=null) {
+ org.apache.mahout.matrix.list.ObjectArrayList list = new org.apache.mahout.matrix.list.ObjectArrayList(columnNames);
+ list.add("|");
+ for (int i=0; i<aggr.length; i++) list.add(aggr[i].name()); // add names of aggregation functions
+ columnNames = new String[list.size()];
+ list.toArray(columnNames);
+ }
+
+ // append names of aggregation functions to line holding rowNames
+ if (rowNames!=null) {
+ org.apache.mahout.matrix.list.ObjectArrayList list = new org.apache.mahout.matrix.list.ObjectArrayList(rowNames);
+ for (int i=0; i<aggr.length; i++) list.add(aggr[i].name()); // add names of aggregation functions
+ rowNames = new String[list.size()];
+ list.toArray(rowNames);
+ }
+
+ // turn large matrix into string
+ String s = new org.apache.mahout.matrix.matrix.objectalgo.Formatter().toTitleString(allStats, rowNames,columnNames,rowAxisName,columnAxisName,title);
+
+ // insert a horizontal "----------------------" separation line above the column stats
+ // determine insertion position and line width
+ int last = s.length()+1;
+ int secondLast = last;
+ int v = Math.max(0, rowAxisName==null ? 0 : rowAxisName.length()-matrix.rows()-aggr.length);
+ for (int k=0; k<aggr.length+1+v; k++) { // scan "aggr.length+1+v" lines backwards
+ secondLast = last;
+ last = s.lastIndexOf(rowSeparator, last-1);
+ }
+ StringBuffer buf = new StringBuffer(s);
+ buf.insert(secondLast,rowSeparator+repeat('-',secondLast-last-1));
+
+ return buf.toString();
}
*/
/**
@@ -777,13 +777,13 @@
@see hep.aida.bin.BinFunctions1D
public String toTitleString(DoubleMatrix3D matrix, String[] sliceNames, String[] rowNames, String[] columnNames, String sliceAxisName, String rowAxisName, String columnAxisName, String title, hep.aida.bin.BinFunction1D[] aggr) {
- if (matrix.size()==0) return "Empty matrix";
- StringBuffer buf = new StringBuffer();
- for (int i=0; i<matrix.slices(); i++) {
- if (i!=0) buf.append(sliceSeparator);
- buf.append(toTitleString(matrix.viewSlice(i),rowNames,columnNames,rowAxisName,columnAxisName,title+"\n"+sliceAxisName+"="+sliceNames[i],aggr));
- }
- return buf.toString();
+ if (matrix.size()==0) return "Empty matrix";
+ StringBuffer buf = new StringBuffer();
+ for (int i=0; i<matrix.slices(); i++) {
+ if (i!=0) buf.append(sliceSeparator);
+ buf.append(toTitleString(matrix.viewSlice(i),rowNames,columnNames,rowAxisName,columnAxisName,title+"\n"+sliceAxisName+"="+sliceNames[i],aggr));
+ }
+ return buf.toString();
}
*/
/**
@@ -801,12 +801,12 @@
@return the matrix converted to a string.
*/
private String xtoTitleString(DoubleMatrix3D matrix, String[] sliceNames, String[] rowNames, String[] columnNames, String sliceAxisName, String rowAxisName, String columnAxisName, String title) {
- if (matrix.size()==0) return "Empty matrix";
- StringBuffer buf = new StringBuffer();
- for (int i=0; i<matrix.slices(); i++) {
- if (i!=0) buf.append(sliceSeparator);
- buf.append(toTitleString(matrix.viewSlice(i),rowNames,columnNames,rowAxisName,columnAxisName,title+"\n"+sliceAxisName+"="+sliceNames[i]));
- }
- return buf.toString();
+ if (matrix.size()==0) return "Empty matrix";
+ StringBuffer buf = new StringBuffer();
+ for (int i=0; i<matrix.slices(); i++) {
+ if (i!=0) buf.append(sliceSeparator);
+ buf.append(toTitleString(matrix.viewSlice(i),rowNames,columnNames,rowAxisName,columnAxisName,title+"\n"+sliceAxisName+"="+sliceNames[i]));
+ }
+ return buf.toString();
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Partitioning.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Partitioning.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Partitioning.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Partitioning.java Wed Nov 25 03:41:28 2009
@@ -23,8 +23,6 @@
*
* @see org.apache.mahout.matrix.Partitioning "Partitioning arrays (provides more documentation)"
*
- * @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.
@@ -53,42 +51,42 @@
<b>Example:</b>
<table border="1" cellspacing="0">
<tr nowrap>
- <td valign="top"><tt>8 x 3 matrix:<br>
- 23, 22, 21<br>
- 20, 19, 18<br>
- 17, 16, 15<br>
- 14, 13, 12<br>
- 11, 10, 9<br>
- 8, 7, 6<br>
- 5, 4, 3<br>
- 2, 1, 0 </tt></td>
- <td align="left" valign="top">
- <p><tt>column = 0;<br>
- rowIndexes = {0,1,2,..,matrix.rows()-1};
- rowFrom = 0;<br>
- rowTo = matrix.rows()-1;<br>
- splitters = {5,10,12}<br>
- c = 0; <br>
- d = splitters.length-1;<br>
- partition(matrix,rowIndexes,rowFrom,rowTo,column,splitters,c,d,splitIndexes);<br>
- ==><br>
- splitIndexes == {0, 2, 3}<br>
- rowIndexes == {7, 6, 5, 4, 0, 1, 2, 3}</tt></p>
- </td>
- <td valign="top">
- The matrix IS NOT REORDERED.<br>
- Here is how it would look<br>
- like, if it would be reordered<br>
- accoring to <tt>rowIndexes</tt>.<br>
- <tt>8 x 3 matrix:<br>
- 2, 1, 0<br>
- 5, 4, 3<br>
- 8, 7, 6<br>
- 11, 10, 9<br>
- 23, 22, 21<br>
- 20, 19, 18<br>
- 17, 16, 15<br>
- 14, 13, 12 </tt></td>
+ <td valign="top"><tt>8 x 3 matrix:<br>
+ 23, 22, 21<br>
+ 20, 19, 18<br>
+ 17, 16, 15<br>
+ 14, 13, 12<br>
+ 11, 10, 9<br>
+ 8, 7, 6<br>
+ 5, 4, 3<br>
+ 2, 1, 0 </tt></td>
+ <td align="left" valign="top">
+ <p><tt>column = 0;<br>
+ rowIndexes = {0,1,2,..,matrix.rows()-1};
+ rowFrom = 0;<br>
+ rowTo = matrix.rows()-1;<br>
+ splitters = {5,10,12}<br>
+ c = 0; <br>
+ d = splitters.length-1;<br>
+ partition(matrix,rowIndexes,rowFrom,rowTo,column,splitters,c,d,splitIndexes);<br>
+ ==><br>
+ splitIndexes == {0, 2, 3}<br>
+ rowIndexes == {7, 6, 5, 4, 0, 1, 2, 3}</tt></p>
+ </td>
+ <td valign="top">
+ The matrix IS NOT REORDERED.<br>
+ Here is how it would look<br>
+ like, if it would be reordered<br>
+ accoring to <tt>rowIndexes</tt>.<br>
+ <tt>8 x 3 matrix:<br>
+ 2, 1, 0<br>
+ 5, 4, 3<br>
+ 8, 7, 6<br>
+ 11, 10, 9<br>
+ 23, 22, 21<br>
+ 20, 19, 18<br>
+ 17, 16, 15<br>
+ 14, 13, 12 </tt></td>
</tr>
</table>
@param matrix the matrix to be partitioned.
@@ -97,61 +95,61 @@
@param rowTo the index of the last row (inclusive).
@param column the index of the column to partition on.
@param splitters the values at which the rows shall be split into intervals.
- Must be sorted ascending and must not contain multiple identical values.
- These preconditions are not checked; be sure that they are met.
+ Must be sorted ascending and must not contain multiple identical values.
+ These preconditions are not checked; be sure that they are met.
@param splitFrom the index of the first splitter element to be considered.
@param splitTo the index of the last splitter element to be considered.
- The method considers the splitter elements <tt>splitters[splitFrom] .. splitters[splitTo]</tt>.
+ The method considers the splitter elements <tt>splitters[splitFrom] .. splitters[splitTo]</tt>.
@param splitIndexes a list into which this method fills the indexes of rows delimiting intervals.
Upon return <tt>splitIndexes[splitFrom..splitTo]</tt> will be set accordingly.
Therefore, must satisfy <tt>splitIndexes.length >= splitters.length</tt>.
*/
public static void partition(DoubleMatrix2D matrix, int[] rowIndexes, int rowFrom, int rowTo, int column, final double[] splitters, int splitFrom, int splitTo, int[] splitIndexes) {
- if (rowFrom < 0 || rowTo >= matrix.rows() || rowTo >= rowIndexes.length) throw new IllegalArgumentException();
- if (column < 0 || column >= matrix.columns()) throw new IllegalArgumentException();
- if (splitFrom < 0 || splitTo >= splitters.length) throw new IllegalArgumentException();
- if (splitIndexes.length < splitters.length) throw new IllegalArgumentException();
-
- // this one knows how to swap two row indexes (a,b)
- final int[] g = rowIndexes;
- Swapper swapper = new Swapper() {
- public void swap(int b, int c) {
- int tmp = g[b]; g[b] = g[c]; g[c] = tmp;
- }
- };
-
- // compare splitter[a] with columnView[rowIndexes[b]]
- final DoubleMatrix1D columnView = matrix.viewColumn(column);
- IntComparator comp = new IntComparator() {
- public int compare(int a, int b) {
- double av = splitters[a];
- double bv = columnView.getQuick(g[b]);
- return av<bv ? -1 : (av==bv ? 0 : 1);
- }
- };
-
- // compare columnView[rowIndexes[a]] with columnView[rowIndexes[b]]
- IntComparator comp2 = new IntComparator() {
- public int compare(int a, int b) {
- double av = columnView.getQuick(g[a]);
- double bv = columnView.getQuick(g[b]);
- return av<bv ? -1 : (av==bv ? 0 : 1);
- }
- };
-
- // compare splitter[a] with splitter[b]
- IntComparator comp3 = new IntComparator() {
- public int compare(int a, int b) {
- double av = splitters[a];
- double bv = splitters[b];
- return av<bv ? -1 : (av==bv ? 0 : 1);
- }
- };
+ if (rowFrom < 0 || rowTo >= matrix.rows() || rowTo >= rowIndexes.length) throw new IllegalArgumentException();
+ if (column < 0 || column >= matrix.columns()) throw new IllegalArgumentException();
+ if (splitFrom < 0 || splitTo >= splitters.length) throw new IllegalArgumentException();
+ if (splitIndexes.length < splitters.length) throw new IllegalArgumentException();
+
+ // this one knows how to swap two row indexes (a,b)
+ final int[] g = rowIndexes;
+ Swapper swapper = new Swapper() {
+ public void swap(int b, int c) {
+ int tmp = g[b]; g[b] = g[c]; g[c] = tmp;
+ }
+ };
+
+ // compare splitter[a] with columnView[rowIndexes[b]]
+ final DoubleMatrix1D columnView = matrix.viewColumn(column);
+ IntComparator comp = new IntComparator() {
+ public int compare(int a, int b) {
+ double av = splitters[a];
+ double bv = columnView.getQuick(g[b]);
+ return av<bv ? -1 : (av==bv ? 0 : 1);
+ }
+ };
+
+ // compare columnView[rowIndexes[a]] with columnView[rowIndexes[b]]
+ IntComparator comp2 = new IntComparator() {
+ public int compare(int a, int b) {
+ double av = columnView.getQuick(g[a]);
+ double bv = columnView.getQuick(g[b]);
+ return av<bv ? -1 : (av==bv ? 0 : 1);
+ }
+ };
+
+ // compare splitter[a] with splitter[b]
+ IntComparator comp3 = new IntComparator() {
+ public int compare(int a, int b) {
+ double av = splitters[a];
+ double bv = splitters[b];
+ return av<bv ? -1 : (av==bv ? 0 : 1);
+ }
+ };
- // generic partitioning does the main work of reordering row indexes
- org.apache.mahout.matrix.Partitioning.genericPartition(rowFrom,rowTo,splitFrom,splitTo,splitIndexes,comp,comp2,comp3,swapper);
+ // generic partitioning does the main work of reordering row indexes
+ org.apache.mahout.matrix.Partitioning.genericPartition(rowFrom,rowTo,splitFrom,splitTo,splitIndexes,comp,comp2,comp3,swapper);
}
/**
Same as {@link org.apache.mahout.matrix.Partitioning#partition(int[],int,int,int[],int,int,int[])}
@@ -171,41 +169,41 @@
<b>Example:</b>
<table border="1" cellspacing="0">
<tr nowrap>
- <td valign="top"><tt>8 x 3 matrix:<br>
- 23, 22, 21<br>
- 20, 19, 18<br>
- 17, 16, 15<br>
- 14, 13, 12<br>
- 11, 10, 9<br>
- 8, 7, 6<br>
- 5, 4, 3<br>
- 2, 1, 0 </tt></td>
- <td align="left" valign="top">
- <tt>column = 0;<br>
- splitters = {5,10,12}<br>
- partition(matrix,column,splitters,splitIndexes);<br>
- ==><br>
- splitIndexes == {0, 2, 3}</tt></p>
- </td>
- <td valign="top">
- The matrix IS NOT REORDERED.<br>
- The new VIEW IS REORDERED:<br>
- <tt>8 x 3 matrix:<br>
- 2, 1, 0<br>
- 5, 4, 3<br>
- 8, 7, 6<br>
- 11, 10, 9<br>
- 23, 22, 21<br>
- 20, 19, 18<br>
- 17, 16, 15<br>
- 14, 13, 12 </tt></td>
+ <td valign="top"><tt>8 x 3 matrix:<br>
+ 23, 22, 21<br>
+ 20, 19, 18<br>
+ 17, 16, 15<br>
+ 14, 13, 12<br>
+ 11, 10, 9<br>
+ 8, 7, 6<br>
+ 5, 4, 3<br>
+ 2, 1, 0 </tt></td>
+ <td align="left" valign="top">
+ <tt>column = 0;<br>
+ splitters = {5,10,12}<br>
+ partition(matrix,column,splitters,splitIndexes);<br>
+ ==><br>
+ splitIndexes == {0, 2, 3}</tt></p>
+ </td>
+ <td valign="top">
+ The matrix IS NOT REORDERED.<br>
+ The new VIEW IS REORDERED:<br>
+ <tt>8 x 3 matrix:<br>
+ 2, 1, 0<br>
+ 5, 4, 3<br>
+ 8, 7, 6<br>
+ 11, 10, 9<br>
+ 23, 22, 21<br>
+ 20, 19, 18<br>
+ 17, 16, 15<br>
+ 14, 13, 12 </tt></td>
</tr>
</table>
@param matrix the matrix to be partitioned.
@param column the index of the column to partition on.
@param splitters the values at which the rows shall be split into intervals.
- Must be sorted ascending and must not contain multiple identical values.
- These preconditions are not checked; be sure that they are met.
+ Must be sorted ascending and must not contain multiple identical values.
+ These preconditions are not checked; be sure that they are met.
@param splitIndexes a list into which this method fills the indexes of rows delimiting intervals.
Therefore, must satisfy <tt>splitIndexes.length >= splitters.length</tt>.
@@ -213,21 +211,21 @@
@return a new matrix view having rows partitioned by the given column and splitters.
*/
public static DoubleMatrix2D partition(DoubleMatrix2D matrix, int column, final double[] splitters, int[] splitIndexes) {
- int rowFrom = 0;
- int rowTo = matrix.rows()-1;
- int splitFrom = 0;
- int splitTo = splitters.length-1;
- int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder instead of matrix itself
- for (int i=rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
-
- partition(matrix,rowIndexes,rowFrom,rowTo,column,splitters,splitFrom,splitTo,splitIndexes);
-
- // take all columns in the original order
- int[] columnIndexes = new int[matrix.columns()];
- for (int i=columnIndexes.length; --i >= 0; ) columnIndexes[i] = i;
+ int rowFrom = 0;
+ int rowTo = matrix.rows()-1;
+ int splitFrom = 0;
+ int splitTo = splitters.length-1;
+ int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder instead of matrix itself
+ for (int i=rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
+
+ partition(matrix,rowIndexes,rowFrom,rowTo,column,splitters,splitFrom,splitTo,splitIndexes);
+
+ // take all columns in the original order
+ int[] columnIndexes = new int[matrix.columns()];
+ for (int i=columnIndexes.length; --i >= 0; ) columnIndexes[i] = i;
- // view the matrix according to the reordered row indexes
- return matrix.viewSelection(rowIndexes,columnIndexes);
+ // view the matrix according to the reordered row indexes
+ return matrix.viewSelection(rowIndexes,columnIndexes);
}
/**
Same as {@link #partition(int[],int,int,int[],int,int,int[])}
@@ -251,108 +249,108 @@
<b>Example:</b>
<table border="1" cellspacing="0">
<tr nowrap>
- <td valign="top"><tt>8 x 3 matrix:<br>
- 23, 22, 21<br>
- 20, 19, 18<br>
- 17, 16, 15<br>
- 14, 13, 12<br>
- 11, 10, 9<br>
- 8, 7, 6<br>
- 5, 4, 3<br>
- 2, 1, 0 </tt></td>
- <td align="left">
- <p><tt>column = matrix.viewColumn(0);<br>
- a = 0;<br>
- b = column.size()-1;</tt><tt><br>
- splitters={5,10,12}<br>
- c=0; <br>
- d=splitters.length-1;</tt><tt><br>
- partition(matrix,column,a,b,splitters,c,d,splitIndexes);<br>
- ==><br>
- splitIndexes == {0, 2, 3}</tt></p>
- </td>
- <td valign="top"><tt>8 x 3 matrix:<br>
- 2, 1, 0<br>
- 5, 4, 3<br>
- 8, 7, 6<br>
- 11, 10, 9<br>
- 23, 22, 21<br>
- 20, 19, 18<br>
- 17, 16, 15<br>
- 14, 13, 12 </tt></td>
+ <td valign="top"><tt>8 x 3 matrix:<br>
+ 23, 22, 21<br>
+ 20, 19, 18<br>
+ 17, 16, 15<br>
+ 14, 13, 12<br>
+ 11, 10, 9<br>
+ 8, 7, 6<br>
+ 5, 4, 3<br>
+ 2, 1, 0 </tt></td>
+ <td align="left">
+ <p><tt>column = matrix.viewColumn(0);<br>
+ a = 0;<br>
+ b = column.size()-1;</tt><tt><br>
+ splitters={5,10,12}<br>
+ c=0; <br>
+ d=splitters.length-1;</tt><tt><br>
+ partition(matrix,column,a,b,splitters,c,d,splitIndexes);<br>
+ ==><br>
+ splitIndexes == {0, 2, 3}</tt></p>
+ </td>
+ <td valign="top"><tt>8 x 3 matrix:<br>
+ 2, 1, 0<br>
+ 5, 4, 3<br>
+ 8, 7, 6<br>
+ 11, 10, 9<br>
+ 23, 22, 21<br>
+ 20, 19, 18<br>
+ 17, 16, 15<br>
+ 14, 13, 12 </tt></td>
</tr>
</table>
*/
private static void xPartitionOld(DoubleMatrix2D matrix, DoubleMatrix1D column, int from, int to, double[] splitters, int splitFrom, int splitTo, int[] splitIndexes) {
- /*
- double splitter; // int, double --> template type dependent
-
- if (splitFrom>splitTo) return; // nothing to do
- if (from>to) { // all bins are empty
- from--;
- for (int i = splitFrom; i<=splitTo; ) splitIndexes[i++] = from;
- return;
- }
-
- // Choose a partition (pivot) index, m
- // Ideally, the pivot should be the median, because a median splits a list into two equal sized sublists.
- // However, computing the median is expensive, so we use an approximation.
- int medianIndex;
- if (splitFrom==splitTo) { // we don't really have a choice
- medianIndex = splitFrom;
- }
- else { // we do have a choice
- int m = (from+to) / 2; // Small arrays, middle element
- int len = to-from+1;
- if (len > SMALL) {
- int l = from;
- int n = to;
- if (len > MEDIUM) { // Big arrays, pseudomedian of 9
- int s = len/8;
- l = med3(column, l, l+s, l+2*s);
- m = med3(column, m-s, m, m+s);
- n = med3(column, n-2*s, n-s, n);
- }
- m = med3(column, l, m, n); // Mid-size, pseudomedian of 3
- }
-
- // Find the splitter closest to the pivot, i.e. the splitter that best splits the list into two equal sized sublists.
- medianIndex = org.apache.mahout.matrix.Sorting.binarySearchFromTo(splitters,column.getQuick(m),splitFrom,splitTo);
- if (medianIndex < 0) medianIndex = -medianIndex - 1; // not found
- if (medianIndex > splitTo) medianIndex = splitTo; // not found, one past the end
-
- }
- splitter = splitters[medianIndex];
-
- // Partition the list according to the splitter, i.e.
- // Establish invariant: list[i] < splitter <= list[j] for i=from..medianIndex and j=medianIndex+1 .. to
- int splitIndex = xPartitionOld(matrix,column,from,to,splitter);
- splitIndexes[medianIndex] = splitIndex;
-
- // Optimization: Handle special cases to cut down recursions.
- if (splitIndex < from) { // no element falls into this bin
- // all bins with splitters[i] <= splitter are empty
- int i = medianIndex-1;
- while (i>=splitFrom && (!(splitter < splitters[i]))) splitIndexes[i--] = splitIndex;
- splitFrom = medianIndex+1;
- }
- else if (splitIndex >= to) { // all elements fall into this bin
- // all bins with splitters[i] >= splitter are empty
- int i = medianIndex+1;
- while (i<=splitTo && (!(splitter > splitters[i]))) splitIndexes[i++] = splitIndex;
- splitTo = medianIndex-1;
- }
-
- // recursively partition left half
- if (splitFrom <= medianIndex-1) {
- xPartitionOld(matrix, column, from, splitIndex, splitters, splitFrom, medianIndex-1, splitIndexes);
- }
-
- // recursively partition right half
- if (medianIndex+1 <= splitTo) {
- xPartitionOld(matrix, column, splitIndex+1, to, splitters, medianIndex+1, splitTo, splitIndexes);
- }
- */
+ /*
+ double splitter; // int, double --> template type dependent
+
+ if (splitFrom>splitTo) return; // nothing to do
+ if (from>to) { // all bins are empty
+ from--;
+ for (int i = splitFrom; i<=splitTo; ) splitIndexes[i++] = from;
+ return;
+ }
+
+ // Choose a partition (pivot) index, m
+ // Ideally, the pivot should be the median, because a median splits a list into two equal sized sublists.
+ // However, computing the median is expensive, so we use an approximation.
+ int medianIndex;
+ if (splitFrom==splitTo) { // we don't really have a choice
+ medianIndex = splitFrom;
+ }
+ else { // we do have a choice
+ int m = (from+to) / 2; // Small arrays, middle element
+ int len = to-from+1;
+ if (len > SMALL) {
+ int l = from;
+ int n = to;
+ if (len > MEDIUM) { // Big arrays, pseudomedian of 9
+ int s = len/8;
+ l = med3(column, l, l+s, l+2*s);
+ m = med3(column, m-s, m, m+s);
+ n = med3(column, n-2*s, n-s, n);
+ }
+ m = med3(column, l, m, n); // Mid-size, pseudomedian of 3
+ }
+
+ // Find the splitter closest to the pivot, i.e. the splitter that best splits the list into two equal sized sublists.
+ medianIndex = org.apache.mahout.matrix.Sorting.binarySearchFromTo(splitters,column.getQuick(m),splitFrom,splitTo);
+ if (medianIndex < 0) medianIndex = -medianIndex - 1; // not found
+ if (medianIndex > splitTo) medianIndex = splitTo; // not found, one past the end
+
+ }
+ splitter = splitters[medianIndex];
+
+ // Partition the list according to the splitter, i.e.
+ // Establish invariant: list[i] < splitter <= list[j] for i=from..medianIndex and j=medianIndex+1 .. to
+ int splitIndex = xPartitionOld(matrix,column,from,to,splitter);
+ splitIndexes[medianIndex] = splitIndex;
+
+ // Optimization: Handle special cases to cut down recursions.
+ if (splitIndex < from) { // no element falls into this bin
+ // all bins with splitters[i] <= splitter are empty
+ int i = medianIndex-1;
+ while (i>=splitFrom && (!(splitter < splitters[i]))) splitIndexes[i--] = splitIndex;
+ splitFrom = medianIndex+1;
+ }
+ else if (splitIndex >= to) { // all elements fall into this bin
+ // all bins with splitters[i] >= splitter are empty
+ int i = medianIndex+1;
+ while (i<=splitTo && (!(splitter > splitters[i]))) splitIndexes[i++] = splitIndex;
+ splitTo = medianIndex-1;
+ }
+
+ // recursively partition left half
+ if (splitFrom <= medianIndex-1) {
+ xPartitionOld(matrix, column, from, splitIndex, splitters, splitFrom, medianIndex-1, splitIndexes);
+ }
+
+ // recursively partition right half
+ if (medianIndex+1 <= splitTo) {
+ xPartitionOld(matrix, column, splitIndex+1, to, splitters, medianIndex+1, splitTo, splitIndexes);
+ }
+ */
}
/**
* Same as {@link #partition(int[],int,int,int)}
@@ -374,18 +372,18 @@
* Note that arguments are not checked for validity.
*/
private static int xPartitionOld(DoubleMatrix2D matrix, DoubleMatrix1D column, int from, int to, double splitter) {
- /*
- double element; // int, double --> template type dependent
- for (int i=from-1; ++i<=to; ) {
- element = column.getQuick(i);
- if (element < splitter) {
- // swap x[i] with x[from]
- matrix.swapRows(i,from);
- from++;
- }
- }
- return from-1;
- */
- return 0;
+ /*
+ double element; // int, double --> template type dependent
+ for (int i=from-1; ++i<=to; ) {
+ element = column.getQuick(i);
+ if (element < splitter) {
+ // swap x[i] with x[from]
+ matrix.swapRows(i,from);
+ from++;
+ }
+ }
+ return from-1;
+ */
+ return 0;
}
}