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 [7/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/Sorting.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Sorting.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Sorting.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Sorting.java Wed Nov 25 03:41:28 2009
@@ -44,22 +44,22 @@
*/
@Deprecated
public class Sorting extends org.apache.mahout.matrix.PersistentObject {
- /**
- * A prefabricated quicksort.
- */
- public static final Sorting quickSort = new Sorting(); // already has quicksort implemented
-
- /**
- * A prefabricated mergesort.
- */
- public static final Sorting mergeSort = new Sorting() { // override quicksort with mergesort
- protected void runSort(int[] a, int fromIndex, int toIndex, IntComparator c) {
- org.apache.mahout.matrix.Sorting.mergeSort(a,fromIndex,toIndex,c);
- }
- protected void runSort(int fromIndex, int toIndex, IntComparator c, org.apache.mahout.matrix.Swapper swapper) {
- org.apache.mahout.matrix.GenericSorting.mergeSort(fromIndex, toIndex, c, swapper);
- }
- };
+ /**
+ * A prefabricated quicksort.
+ */
+ public static final Sorting quickSort = new Sorting(); // already has quicksort implemented
+
+ /**
+ * A prefabricated mergesort.
+ */
+ public static final Sorting mergeSort = new Sorting() { // override quicksort with mergesort
+ protected void runSort(int[] a, int fromIndex, int toIndex, IntComparator c) {
+ org.apache.mahout.matrix.Sorting.mergeSort(a,fromIndex,toIndex,c);
+ }
+ protected void runSort(int fromIndex, int toIndex, IntComparator c, org.apache.mahout.matrix.Swapper swapper) {
+ org.apache.mahout.matrix.GenericSorting.mergeSort(fromIndex, toIndex, c, swapper);
+ }
+ };
/**
* Makes this class non instantiable, but still let's others inherit from it.
*/
@@ -68,17 +68,17 @@
* Compare two values, one of which is assumed to be Double.NaN
*/
private static final int compareNaN(double a, double b) {
- if (a!=a) {
- if (b!=b) return 0; // NaN equals NaN
- else return 1; // e.g. NaN > 5
- }
- return -1; // e.g. 5 < NaN
+ if (a!=a) {
+ if (b!=b) return 0; // NaN equals NaN
+ else return 1; // e.g. NaN > 5
+ }
+ return -1; // e.g. 5 < NaN
}
protected void runSort(int[] a, int fromIndex, int toIndex, IntComparator c) {
- org.apache.mahout.matrix.Sorting.quickSort(a,fromIndex,toIndex,c);
+ org.apache.mahout.matrix.Sorting.quickSort(a,fromIndex,toIndex,c);
}
protected void runSort(int fromIndex, int toIndex, IntComparator c, org.apache.mahout.matrix.Swapper swapper) {
- org.apache.mahout.matrix.GenericSorting.quickSort(fromIndex, toIndex, c, swapper);
+ org.apache.mahout.matrix.GenericSorting.quickSort(fromIndex, toIndex, c, swapper);
}
/**
Sorts the vector into ascending order, according to the <i>natural ordering</i>.
@@ -88,36 +88,36 @@
<b>Example:</b>
<table border="1" cellspacing="0">
<tr nowrap>
- <td valign="top"><tt> 7, 1, 3, 1<br>
- </tt></td>
- <td valign="top">
- <p><tt> ==> 1, 1, 3, 7<br>
- The vector IS NOT SORTED.<br>
- The new VIEW IS SORTED.</tt></p>
- </td>
+ <td valign="top"><tt> 7, 1, 3, 1<br>
+ </tt></td>
+ <td valign="top">
+ <p><tt> ==> 1, 1, 3, 7<br>
+ The vector IS NOT SORTED.<br>
+ The new VIEW IS SORTED.</tt></p>
+ </td>
</tr>
</table>
@param vector the vector to be sorted.
@return a new sorted vector (matrix) view.
- <b>Note that the original matrix is left unaffected.</b>
+ <b>Note that the original matrix is left unaffected.</b>
*/
public DoubleMatrix1D sort(final DoubleMatrix1D vector) {
- int[] indexes = new int[vector.size()]; // row indexes to reorder instead of matrix itself
- for (int i=indexes.length; --i >= 0; ) indexes[i] = i;
+ int[] indexes = new int[vector.size()]; // row indexes to reorder instead of matrix itself
+ for (int i=indexes.length; --i >= 0; ) indexes[i] = i;
- IntComparator comp = new IntComparator() {
- public int compare(int a, int b) {
- double av = vector.getQuick(a);
- double bv = vector.getQuick(b);
- if (av!=av || bv!=bv) return compareNaN(av,bv); // swap NaNs to the end
- return av<bv ? -1 : (av==bv ? 0 : 1);
- }
- };
+ IntComparator comp = new IntComparator() {
+ public int compare(int a, int b) {
+ double av = vector.getQuick(a);
+ double bv = vector.getQuick(b);
+ if (av!=av || bv!=bv) return compareNaN(av,bv); // swap NaNs to the end
+ return av<bv ? -1 : (av==bv ? 0 : 1);
+ }
+ };
- runSort(indexes,0,indexes.length,comp);
+ runSort(indexes,0,indexes.length,comp);
- return vector.viewSelection(indexes);
+ return vector.viewSelection(indexes);
}
/**
Sorts the vector into ascending order, according to the order induced by the specified comparator.
@@ -140,21 +140,21 @@
@param vector the vector to be sorted.
@param c the comparator to determine the order.
@return a new matrix view sorted as specified.
- <b>Note that the original vector (matrix) is left unaffected.</b>
+ <b>Note that the original vector (matrix) is left unaffected.</b>
*/
public DoubleMatrix1D sort(final DoubleMatrix1D vector, final org.apache.mahout.matrix.function.DoubleComparator c) {
- int[] indexes = new int[vector.size()]; // row indexes to reorder instead of matrix itself
- for (int i=indexes.length; --i >= 0; ) indexes[i] = i;
+ int[] indexes = new int[vector.size()]; // row indexes to reorder instead of matrix itself
+ for (int i=indexes.length; --i >= 0; ) indexes[i] = i;
- IntComparator comp = new IntComparator() {
- public int compare(int a, int b) {
- return c.compare(vector.getQuick(a), vector.getQuick(b));
- }
- };
+ IntComparator comp = new IntComparator() {
+ public int compare(int a, int b) {
+ return c.compare(vector.getQuick(a), vector.getQuick(b));
+ }
+ };
- runSort(indexes,0,indexes.length,comp);
+ runSort(indexes,0,indexes.length,comp);
- return vector.viewSelection(indexes);
+ return vector.viewSelection(indexes);
}
/**
Sorts the matrix rows into ascending order, according to the <i>natural ordering</i> of the matrix values in the virtual column <tt>aggregates</tt>;
@@ -171,28 +171,28 @@
Each aggregate is the sum of a row
<table border="1" cellspacing="0">
<tr nowrap>
- <td valign="top"><tt>4 x 2 matrix: <br>
- 1, 1<br>
- 5, 4<br>
- 3, 0<br>
- 4, 4 <br>
- </tt></td>
- <td align="left" valign="top">
- <tt>aggregates=<br>
- 2<br>
- 9<br>
- 3<br>
- 8<br>
- ==></tt></td>
- <td valign="top">
- <p><tt>4 x 2 matrix:<br>
- 1, 1<br>
- 3, 0<br>
- 4, 4<br>
- 5, 4</tt><br>
- The matrix IS NOT SORTED.<br>
- The new VIEW IS SORTED.</p>
- </td>
+ <td valign="top"><tt>4 x 2 matrix: <br>
+ 1, 1<br>
+ 5, 4<br>
+ 3, 0<br>
+ 4, 4 <br>
+ </tt></td>
+ <td align="left" valign="top">
+ <tt>aggregates=<br>
+ 2<br>
+ 9<br>
+ 3<br>
+ 8<br>
+ ==></tt></td>
+ <td valign="top">
+ <p><tt>4 x 2 matrix:<br>
+ 1, 1<br>
+ 3, 0<br>
+ 4, 4<br>
+ 5, 4</tt><br>
+ The matrix IS NOT SORTED.<br>
+ The new VIEW IS SORTED.</p>
+ </td>
</tr>
</table>
@@ -226,41 +226,41 @@
@param matrix the matrix to be sorted.
@param aggregates the values to sort on. (As a side effect, this array will also get sorted).
@return a new matrix view having rows sorted.
- <b>Note that the original matrix is left unaffected.</b>
+ <b>Note that the original matrix is left unaffected.</b>
@throws IndexOutOfBoundsException if <tt>aggregates.length != matrix.rows()</tt>.
*/
public DoubleMatrix2D sort(DoubleMatrix2D matrix, final double[] aggregates) {
- int rows = matrix.rows();
- if (aggregates.length != rows) throw new IndexOutOfBoundsException("aggregates.length != matrix.rows()");
-
- // set up index reordering
- final int[] indexes = new int[rows];
- for (int i=rows; --i >= 0; ) indexes[i] = i;
-
- // compares two aggregates at a time
- org.apache.mahout.matrix.function.IntComparator comp = new org.apache.mahout.matrix.function.IntComparator() {
- public int compare(int x, int y) {
- double a = aggregates[x];
- double b = aggregates[y];
- if (a!=a || b!=b) return compareNaN(a,b); // swap NaNs to the end
- return a < b ? -1 : (a==b) ? 0 : 1;
- }
- };
- // swaps aggregates and reorders indexes
- org.apache.mahout.matrix.Swapper swapper = new org.apache.mahout.matrix.Swapper() {
- public void swap(int x, int y) {
- int t1; double t2;
- t1 = indexes[x]; indexes[x] = indexes[y]; indexes[y] = t1;
- t2 = aggregates[x]; aggregates[x] = aggregates[y]; aggregates[y] = t2;
- }
- };
-
- // sort indexes and aggregates
- runSort(0,rows,comp,swapper);
-
- // view the matrix according to the reordered row indexes
- // take all columns in the original order
- return matrix.viewSelection(indexes,null);
+ int rows = matrix.rows();
+ if (aggregates.length != rows) throw new IndexOutOfBoundsException("aggregates.length != matrix.rows()");
+
+ // set up index reordering
+ final int[] indexes = new int[rows];
+ for (int i=rows; --i >= 0; ) indexes[i] = i;
+
+ // compares two aggregates at a time
+ org.apache.mahout.matrix.function.IntComparator comp = new org.apache.mahout.matrix.function.IntComparator() {
+ public int compare(int x, int y) {
+ double a = aggregates[x];
+ double b = aggregates[y];
+ if (a!=a || b!=b) return compareNaN(a,b); // swap NaNs to the end
+ return a < b ? -1 : (a==b) ? 0 : 1;
+ }
+ };
+ // swaps aggregates and reorders indexes
+ org.apache.mahout.matrix.Swapper swapper = new org.apache.mahout.matrix.Swapper() {
+ public void swap(int x, int y) {
+ int t1; double t2;
+ t1 = indexes[x]; indexes[x] = indexes[y]; indexes[y] = t1;
+ t2 = aggregates[x]; aggregates[x] = aggregates[y]; aggregates[y] = t2;
+ }
+ };
+
+ // sort indexes and aggregates
+ runSort(0,rows,comp,swapper);
+
+ // view the matrix according to the reordered row indexes
+ // take all columns in the original order
+ return matrix.viewSelection(indexes,null);
}
/**
Sorts the matrix rows into ascending order, according to the <i>natural ordering</i> of the matrix values in the given column.
@@ -270,57 +270,57 @@
<b>Example:</b>
<table border="1" cellspacing="0">
<tr nowrap>
- <td valign="top"><tt>4 x 2 matrix: <br>
- 7, 6<br>
- 5, 4<br>
- 3, 2<br>
- 1, 0 <br>
- </tt></td>
- <td align="left" valign="top">
- <p><tt>column = 0;<br>
- view = quickSort(matrix,column);<br>
- System.out.println(view); </tt><tt><br>
- ==> </tt></p>
- </td>
- <td valign="top">
- <p><tt>4 x 2 matrix:<br>
- 1, 0<br>
- 3, 2<br>
- 5, 4<br>
- 7, 6</tt><br>
- The matrix IS NOT SORTED.<br>
- The new VIEW IS SORTED.</p>
- </td>
+ <td valign="top"><tt>4 x 2 matrix: <br>
+ 7, 6<br>
+ 5, 4<br>
+ 3, 2<br>
+ 1, 0 <br>
+ </tt></td>
+ <td align="left" valign="top">
+ <p><tt>column = 0;<br>
+ view = quickSort(matrix,column);<br>
+ System.out.println(view); </tt><tt><br>
+ ==> </tt></p>
+ </td>
+ <td valign="top">
+ <p><tt>4 x 2 matrix:<br>
+ 1, 0<br>
+ 3, 2<br>
+ 5, 4<br>
+ 7, 6</tt><br>
+ The matrix IS NOT SORTED.<br>
+ The new VIEW IS SORTED.</p>
+ </td>
</tr>
</table>
@param matrix the matrix to be sorted.
@param column the index of the column inducing the order.
@return a new matrix view having rows sorted by the given column.
- <b>Note that the original matrix is left unaffected.</b>
+ <b>Note that the original matrix is left unaffected.</b>
@throws IndexOutOfBoundsException if <tt>column < 0 || column >= matrix.columns()</tt>.
*/
public DoubleMatrix2D sort(DoubleMatrix2D matrix, int column) {
- if (column < 0 || column >= matrix.columns()) throw new IndexOutOfBoundsException("column="+column+", matrix="+Formatter.shape(matrix));
+ if (column < 0 || column >= matrix.columns()) throw new IndexOutOfBoundsException("column="+column+", matrix="+Formatter.shape(matrix));
- int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder instead of matrix itself
- for (int i=rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
+ int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder instead of matrix itself
+ for (int i=rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
- final DoubleMatrix1D col = matrix.viewColumn(column);
- IntComparator comp = new IntComparator() {
- public int compare(int a, int b) {
- double av = col.getQuick(a);
- double bv = col.getQuick(b);
- if (av!=av || bv!=bv) return compareNaN(av,bv); // swap NaNs to the end
- return av<bv ? -1 : (av==bv ? 0 : 1);
- }
- };
-
- runSort(rowIndexes,0,rowIndexes.length,comp);
-
- // view the matrix according to the reordered row indexes
- // take all columns in the original order
- return matrix.viewSelection(rowIndexes,null);
+ final DoubleMatrix1D col = matrix.viewColumn(column);
+ IntComparator comp = new IntComparator() {
+ public int compare(int a, int b) {
+ double av = col.getQuick(a);
+ double bv = col.getQuick(b);
+ if (av!=av || bv!=bv) return compareNaN(av,bv); // swap NaNs to the end
+ return av<bv ? -1 : (av==bv ? 0 : 1);
+ }
+ };
+
+ runSort(rowIndexes,0,rowIndexes.length,comp);
+
+ // view the matrix according to the reordered row indexes
+ // take all columns in the original order
+ return matrix.viewSelection(rowIndexes,null);
}
/**
Sorts the matrix rows according to the order induced by the specified comparator.
@@ -343,27 +343,27 @@
@param matrix the matrix to be sorted.
@param c the comparator to determine the order.
@return a new matrix view having rows sorted as specified.
- <b>Note that the original matrix is left unaffected.</b>
+ <b>Note that the original matrix is left unaffected.</b>
*/
public DoubleMatrix2D sort(final DoubleMatrix2D matrix, final DoubleMatrix1DComparator c) {
- int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder instead of matrix itself
- for (int i=rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
+ int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder instead of matrix itself
+ for (int i=rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
- final DoubleMatrix1D[] views = new DoubleMatrix1D[matrix.rows()]; // precompute views for speed
- for (int i=views.length; --i >= 0; ) views[i] = matrix.viewRow(i);
+ final DoubleMatrix1D[] views = new DoubleMatrix1D[matrix.rows()]; // precompute views for speed
+ for (int i=views.length; --i >= 0; ) views[i] = matrix.viewRow(i);
- IntComparator comp = new IntComparator() {
- public int compare(int a, int b) {
- //return c.compare(matrix.viewRow(a), matrix.viewRow(b));
- return c.compare(views[a], views[b]);
- }
- };
-
- runSort(rowIndexes,0,rowIndexes.length,comp);
-
- // view the matrix according to the reordered row indexes
- // take all columns in the original order
- return matrix.viewSelection(rowIndexes,null);
+ IntComparator comp = new IntComparator() {
+ public int compare(int a, int b) {
+ //return c.compare(matrix.viewRow(a), matrix.viewRow(b));
+ return c.compare(views[a], views[b]);
+ }
+ };
+
+ runSort(rowIndexes,0,rowIndexes.length,comp);
+
+ // view the matrix according to the reordered row indexes
+ // take all columns in the original order
+ return matrix.viewSelection(rowIndexes,null);
}
/**
Sorts the matrix rows into ascending order, according to the <i>natural ordering</i> of the values computed by applying the given aggregation function to each row;
@@ -379,25 +379,25 @@
Each aggregate is the sum of a row
<table border="1" cellspacing="0">
<tr nowrap>
- <td valign="top"><tt>4 x 2 matrix: <br>
- 1, 1<br>
- 5, 4<br>
- 3, 0<br>
- 4, 4 <br>
- </tt></td>
- <td align="left" valign="top">
- <tt>aggregates=<br>
- hep.aida.bin.BinFunctions1D.sum<br>
- ==></tt></td>
- <td valign="top">
- <p><tt>4 x 2 matrix:<br>
- 1, 1<br>
- 3, 0<br>
- 4, 4<br>
- 5, 4</tt><br>
- The matrix IS NOT SORTED.<br>
- The new VIEW IS SORTED.</p>
- </td>
+ <td valign="top"><tt>4 x 2 matrix: <br>
+ 1, 1<br>
+ 5, 4<br>
+ 3, 0<br>
+ 4, 4 <br>
+ </tt></td>
+ <td align="left" valign="top">
+ <tt>aggregates=<br>
+ hep.aida.bin.BinFunctions1D.sum<br>
+ ==></tt></td>
+ <td valign="top">
+ <p><tt>4 x 2 matrix:<br>
+ 1, 1<br>
+ 3, 0<br>
+ 4, 4<br>
+ 5, 4</tt><br>
+ The matrix IS NOT SORTED.<br>
+ The new VIEW IS SORTED.</p>
+ </td>
</tr>
</table>
@@ -431,17 +431,17 @@
@param matrix the matrix to be sorted.
@param aggregate the function to sort on; aggregates values in a row.
@return a new matrix view having rows sorted.
- <b>Note that the original matrix is left unaffected.</b>
+ <b>Note that the original matrix is left unaffected.</b>
public DoubleMatrix2D sort(DoubleMatrix2D matrix, hep.aida.bin.BinFunction1D aggregate) {
- // precompute aggregates over rows, as defined by "aggregate"
+ // precompute aggregates over rows, as defined by "aggregate"
- // a bit clumsy, because Statistic.aggregate(...) is defined on columns, so we need to transpose views
- DoubleMatrix2D tmp = matrix.like(1,matrix.rows());
- hep.aida.bin.BinFunction1D[] func = {aggregate};
- Statistic.aggregate(matrix.viewDice(), func, tmp);
- double[] aggr = tmp.viewRow(0).toArray();
- return sort(matrix,aggr);
+ // a bit clumsy, because Statistic.aggregate(...) is defined on columns, so we need to transpose views
+ DoubleMatrix2D tmp = matrix.like(1,matrix.rows());
+ hep.aida.bin.BinFunction1D[] func = {aggregate};
+ Statistic.aggregate(matrix.viewDice(), func, tmp);
+ double[] aggr = tmp.viewRow(0).toArray();
+ return sort(matrix,aggr);
}
*/
/**
@@ -462,31 +462,31 @@
@param row the index of the row inducing the order.
@param column the index of the column inducing the order.
@return a new matrix view having slices sorted by the values of the slice view <tt>matrix.viewRow(row).viewColumn(column)</tt>.
- <b>Note that the original matrix is left unaffected.</b>
+ <b>Note that the original matrix is left unaffected.</b>
@throws IndexOutOfBoundsException if <tt>row < 0 || row >= matrix.rows() || column < 0 || column >= matrix.columns()</tt>.
*/
public DoubleMatrix3D sort(DoubleMatrix3D matrix, int row, int column) {
- if (row < 0 || row >= matrix.rows()) throw new IndexOutOfBoundsException("row="+row+", matrix="+Formatter.shape(matrix));
- if (column < 0 || column >= matrix.columns()) throw new IndexOutOfBoundsException("column="+column+", matrix="+Formatter.shape(matrix));
+ if (row < 0 || row >= matrix.rows()) throw new IndexOutOfBoundsException("row="+row+", matrix="+Formatter.shape(matrix));
+ if (column < 0 || column >= matrix.columns()) throw new IndexOutOfBoundsException("column="+column+", matrix="+Formatter.shape(matrix));
- int[] sliceIndexes = new int[matrix.slices()]; // indexes to reorder instead of matrix itself
- for (int i=sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
+ int[] sliceIndexes = new int[matrix.slices()]; // indexes to reorder instead of matrix itself
+ for (int i=sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
- final DoubleMatrix1D sliceView = matrix.viewRow(row).viewColumn(column);
- IntComparator comp = new IntComparator() {
- public int compare(int a, int b) {
- double av = sliceView.getQuick(a);
- double bv = sliceView.getQuick(b);
- if (av!=av || bv!=bv) return compareNaN(av,bv); // swap NaNs to the end
- return av<bv ? -1 : (av==bv ? 0 : 1);
- }
- };
-
- runSort(sliceIndexes,0,sliceIndexes.length,comp);
-
- // view the matrix according to the reordered slice indexes
- // take all rows and columns in the original order
- return matrix.viewSelection(sliceIndexes,null,null);
+ final DoubleMatrix1D sliceView = matrix.viewRow(row).viewColumn(column);
+ IntComparator comp = new IntComparator() {
+ public int compare(int a, int b) {
+ double av = sliceView.getQuick(a);
+ double bv = sliceView.getQuick(b);
+ if (av!=av || bv!=bv) return compareNaN(av,bv); // swap NaNs to the end
+ return av<bv ? -1 : (av==bv ? 0 : 1);
+ }
+ };
+
+ runSort(sliceIndexes,0,sliceIndexes.length,comp);
+
+ // view the matrix according to the reordered slice indexes
+ // take all rows and columns in the original order
+ return matrix.viewSelection(sliceIndexes,null,null);
}
/**
Sorts the matrix slices according to the order induced by the specified comparator.
@@ -509,185 +509,185 @@
@param matrix the matrix to be sorted.
@param c the comparator to determine the order.
@return a new matrix view having slices sorted as specified.
- <b>Note that the original matrix is left unaffected.</b>
+ <b>Note that the original matrix is left unaffected.</b>
*/
public DoubleMatrix3D sort(final DoubleMatrix3D matrix, final DoubleMatrix2DComparator c) {
- int[] sliceIndexes = new int[matrix.slices()]; // indexes to reorder instead of matrix itself
- for (int i=sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
+ int[] sliceIndexes = new int[matrix.slices()]; // indexes to reorder instead of matrix itself
+ for (int i=sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
- final DoubleMatrix2D[] views = new DoubleMatrix2D[matrix.slices()]; // precompute views for speed
- for (int i=views.length; --i >= 0; ) views[i] = matrix.viewSlice(i);
+ final DoubleMatrix2D[] views = new DoubleMatrix2D[matrix.slices()]; // precompute views for speed
+ for (int i=views.length; --i >= 0; ) views[i] = matrix.viewSlice(i);
- IntComparator comp = new IntComparator() {
- public int compare(int a, int b) {
- //return c.compare(matrix.viewSlice(a), matrix.viewSlice(b));
- return c.compare(views[a], views[b]);
- }
- };
-
- runSort(sliceIndexes,0,sliceIndexes.length,comp);
-
- // view the matrix according to the reordered slice indexes
- // take all rows and columns in the original order
- return matrix.viewSelection(sliceIndexes,null,null);
+ IntComparator comp = new IntComparator() {
+ public int compare(int a, int b) {
+ //return c.compare(matrix.viewSlice(a), matrix.viewSlice(b));
+ return c.compare(views[a], views[b]);
+ }
+ };
+
+ runSort(sliceIndexes,0,sliceIndexes.length,comp);
+
+ // view the matrix according to the reordered slice indexes
+ // take all rows and columns in the original order
+ return matrix.viewSelection(sliceIndexes,null,null);
}
/**
* Demonstrates advanced sorting.
* Sorts by sum of row.
*/
public static void zdemo1() {
- Sorting sort = quickSort;
- DoubleMatrix2D matrix = DoubleFactory2D.dense.descending(4,3);
- DoubleMatrix1DComparator comp = new DoubleMatrix1DComparator() {
- public int compare(DoubleMatrix1D a, DoubleMatrix1D b) {
- double as = a.zSum(); double bs = b.zSum();
- return as < bs ? -1 : as == bs ? 0 : 1;
- }
- };
- System.out.println("unsorted:"+matrix);
- System.out.println("sorted :"+sort.sort(matrix,comp));
+ Sorting sort = quickSort;
+ DoubleMatrix2D matrix = DoubleFactory2D.dense.descending(4,3);
+ DoubleMatrix1DComparator comp = new DoubleMatrix1DComparator() {
+ public int compare(DoubleMatrix1D a, DoubleMatrix1D b) {
+ double as = a.zSum(); double bs = b.zSum();
+ return as < bs ? -1 : as == bs ? 0 : 1;
+ }
+ };
+ System.out.println("unsorted:"+matrix);
+ System.out.println("sorted :"+sort.sort(matrix,comp));
}
/**
* Demonstrates advanced sorting.
* Sorts by sum of slice.
*/
public static void zdemo2() {
- Sorting sort = quickSort;
- DoubleMatrix3D matrix = DoubleFactory3D.dense.descending(4,3,2);
- DoubleMatrix2DComparator comp = new DoubleMatrix2DComparator() {
- public int compare(DoubleMatrix2D a, DoubleMatrix2D b) {
- double as = a.zSum();
- double bs = b.zSum();
- return as < bs ? -1 : as == bs ? 0 : 1;
- }
- };
- System.out.println("unsorted:"+matrix);
- System.out.println("sorted :"+sort.sort(matrix,comp));
+ Sorting sort = quickSort;
+ DoubleMatrix3D matrix = DoubleFactory3D.dense.descending(4,3,2);
+ DoubleMatrix2DComparator comp = new DoubleMatrix2DComparator() {
+ public int compare(DoubleMatrix2D a, DoubleMatrix2D b) {
+ double as = a.zSum();
+ double bs = b.zSum();
+ return as < bs ? -1 : as == bs ? 0 : 1;
+ }
+ };
+ System.out.println("unsorted:"+matrix);
+ System.out.println("sorted :"+sort.sort(matrix,comp));
}
/**
* Demonstrates advanced sorting.
* Sorts by sinus of cell values.
*/
public static void zdemo3() {
- Sorting sort = quickSort;
- double[] values = {0.5, 1.5, 2.5, 3.5};
- DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
- org.apache.mahout.matrix.function.DoubleComparator comp = new org.apache.mahout.matrix.function.DoubleComparator() {
- public int compare(double a, double b) {
- double as = Math.sin(a); double bs = Math.sin(b);
- return as < bs ? -1 : as == bs ? 0 : 1;
- }
- };
- System.out.println("unsorted:"+matrix);
-
- DoubleMatrix1D sorted = sort.sort(matrix,comp);
- System.out.println("sorted :"+sorted);
-
- // check whether it is really sorted
- sorted.assign(org.apache.mahout.jet.math.Functions.sin);
- /*
- sorted.assign(
- new org.apache.mahout.matrix.function.DoubleFunction() {
- public double apply(double arg) { return Math.sin(arg); }
- }
- );
- */
- System.out.println("sined :"+sorted);
+ Sorting sort = quickSort;
+ double[] values = {0.5, 1.5, 2.5, 3.5};
+ DoubleMatrix1D matrix = new DenseDoubleMatrix1D(values);
+ org.apache.mahout.matrix.function.DoubleComparator comp = new org.apache.mahout.matrix.function.DoubleComparator() {
+ public int compare(double a, double b) {
+ double as = Math.sin(a); double bs = Math.sin(b);
+ return as < bs ? -1 : as == bs ? 0 : 1;
+ }
+ };
+ System.out.println("unsorted:"+matrix);
+
+ DoubleMatrix1D sorted = sort.sort(matrix,comp);
+ System.out.println("sorted :"+sorted);
+
+ // check whether it is really sorted
+ sorted.assign(org.apache.mahout.jet.math.Functions.sin);
+ /*
+ sorted.assign(
+ new org.apache.mahout.matrix.function.DoubleFunction() {
+ public double apply(double arg) { return Math.sin(arg); }
+ }
+ );
+ */
+ System.out.println("sined :"+sorted);
}
/**
* Demonstrates applying functions.
*/
protected static void zdemo4() {
- double[] values1 = {0, 1, 2, 3};
- double[] values2 = {0, 2, 4, 6};
- DoubleMatrix1D matrix1 = new DenseDoubleMatrix1D(values1);
- DoubleMatrix1D matrix2 = new DenseDoubleMatrix1D(values2);
- System.out.println("m1:"+matrix1);
- System.out.println("m2:"+matrix2);
-
- matrix1.assign(matrix2, org.apache.mahout.jet.math.Functions.pow);
-
- /*
- matrix1.assign(matrix2,
- new org.apache.mahout.matrix.function.DoubleDoubleFunction() {
- public double apply(double x, double y) { return Math.pow(x,y); }
- }
- );
- */
-
- System.out.println("applied:"+matrix1);
+ double[] values1 = {0, 1, 2, 3};
+ double[] values2 = {0, 2, 4, 6};
+ DoubleMatrix1D matrix1 = new DenseDoubleMatrix1D(values1);
+ DoubleMatrix1D matrix2 = new DenseDoubleMatrix1D(values2);
+ System.out.println("m1:"+matrix1);
+ System.out.println("m2:"+matrix2);
+
+ matrix1.assign(matrix2, org.apache.mahout.jet.math.Functions.pow);
+
+ /*
+ matrix1.assign(matrix2,
+ new org.apache.mahout.matrix.function.DoubleDoubleFunction() {
+ public double apply(double x, double y) { return Math.pow(x,y); }
+ }
+ );
+ */
+
+ System.out.println("applied:"+matrix1);
}
/**
* Demonstrates sorting with precomputation of aggregates (median and sum of logarithms).
*
public static void zdemo5(int rows, int columns, boolean print) {
- Sorting sort = quickSort;
- // for reliable benchmarks, call this method twice: once with small dummy parameters to "warm up" the jitter, then with your real work-load
-
- System.out.println("\n\n");
- System.out.print("now initializing... ");
- org.apache.mahout.matrix.Timer timer = new org.apache.mahout.matrix.Timer().start();
-
- final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
- DoubleMatrix2D A = org.apache.mahout.matrix.matrix.DoubleFactory2D.dense.make(rows,columns);
- A.assign(new org.apache.mahout.jet.random.engine.DRand()); // initialize randomly
- timer.stop().display();
-
- // also benchmark copying in its several implementation flavours
- DoubleMatrix2D B = A.like();
- timer.reset().start();
- System.out.print("now copying... ");
- B.assign(A);
- timer.stop().display();
-
- timer.reset().start();
- System.out.print("now copying subrange... ");
- B.viewPart(0,0,rows,columns).assign(A.viewPart(0,0,rows,columns));
- timer.stop().display();
- //System.out.println(A);
-
- timer.reset().start();
- System.out.print("now copying selected... ");
- B.viewSelection(null,null).assign(A.viewSelection(null,null));
- timer.stop().display();
-
- System.out.print("now sorting - quick version with precomputation... ");
- timer.reset().start();
- // THE QUICK VERSION (takes some 10 secs)
- A = sort.sort(A,hep.aida.bin.BinFunctions1D.median);
- //A = sort.sort(A,hep.aida.bin.BinFunctions1D.sumLog);
- timer.stop().display();
-
- // check results for correctness
- // WARNING: be sure NOT TO PRINT huge matrices unless you have tons of main memory and time!!
- // so we just show the first 5 rows
- if (print) {
- int r = Math.min(rows,5);
- hep.aida.bin.BinFunction1D[] funs = {hep.aida.bin.BinFunctions1D.median, hep.aida.bin.BinFunctions1D.sumLog, hep.aida.bin.BinFunctions1D.geometricMean};
- String[] rowNames = new String[r];
- String[] columnNames = new String[columns];
- for (int i=columns; --i >= 0; ) columnNames[i] = Integer.toString(i);
- for (int i=r; --i >= 0; ) rowNames[i] = Integer.toString(i);
- System.out.println("first part of sorted result = \n"+new org.apache.mahout.matrix.matrix.doublealgo.Formatter("%G").toTitleString(
- A.viewPart(0,0,r,columns), rowNames, columnNames, null, null, null, funs
- ));
- }
-
-
- System.out.print("now sorting - slow version... ");
- A = B;
- org.apache.mahout.matrix.matrix.doublealgo.DoubleMatrix1DComparator fun = new org.apache.mahout.matrix.matrix.doublealgo.DoubleMatrix1DComparator() {
- public int compare(DoubleMatrix1D x, DoubleMatrix1D y) {
- double a = org.apache.mahout.matrix.matrix.doublealgo.Statistic.bin(x).median();
- double b = org.apache.mahout.matrix.matrix.doublealgo.Statistic.bin(y).median();
- //double a = x.aggregate(F.plus,F.log);
- //double b = y.aggregate(F.plus,F.log);
- return a < b ? -1 : (a==b) ? 0 : 1;
- }
- };
- timer.reset().start();
- A = sort.sort(A,fun);
- timer.stop().display();
+ Sorting sort = quickSort;
+ // for reliable benchmarks, call this method twice: once with small dummy parameters to "warm up" the jitter, then with your real work-load
+
+ System.out.println("\n\n");
+ System.out.print("now initializing... ");
+ org.apache.mahout.matrix.Timer timer = new org.apache.mahout.matrix.Timer().start();
+
+ final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
+ DoubleMatrix2D A = org.apache.mahout.matrix.matrix.DoubleFactory2D.dense.make(rows,columns);
+ A.assign(new org.apache.mahout.jet.random.engine.DRand()); // initialize randomly
+ timer.stop().display();
+
+ // also benchmark copying in its several implementation flavours
+ DoubleMatrix2D B = A.like();
+ timer.reset().start();
+ System.out.print("now copying... ");
+ B.assign(A);
+ timer.stop().display();
+
+ timer.reset().start();
+ System.out.print("now copying subrange... ");
+ B.viewPart(0,0,rows,columns).assign(A.viewPart(0,0,rows,columns));
+ timer.stop().display();
+ //System.out.println(A);
+
+ timer.reset().start();
+ System.out.print("now copying selected... ");
+ B.viewSelection(null,null).assign(A.viewSelection(null,null));
+ timer.stop().display();
+
+ System.out.print("now sorting - quick version with precomputation... ");
+ timer.reset().start();
+ // THE QUICK VERSION (takes some 10 secs)
+ A = sort.sort(A,hep.aida.bin.BinFunctions1D.median);
+ //A = sort.sort(A,hep.aida.bin.BinFunctions1D.sumLog);
+ timer.stop().display();
+
+ // check results for correctness
+ // WARNING: be sure NOT TO PRINT huge matrices unless you have tons of main memory and time!!
+ // so we just show the first 5 rows
+ if (print) {
+ int r = Math.min(rows,5);
+ hep.aida.bin.BinFunction1D[] funs = {hep.aida.bin.BinFunctions1D.median, hep.aida.bin.BinFunctions1D.sumLog, hep.aida.bin.BinFunctions1D.geometricMean};
+ String[] rowNames = new String[r];
+ String[] columnNames = new String[columns];
+ for (int i=columns; --i >= 0; ) columnNames[i] = Integer.toString(i);
+ for (int i=r; --i >= 0; ) rowNames[i] = Integer.toString(i);
+ System.out.println("first part of sorted result = \n"+new org.apache.mahout.matrix.matrix.doublealgo.Formatter("%G").toTitleString(
+ A.viewPart(0,0,r,columns), rowNames, columnNames, null, null, null, funs
+ ));
+ }
+
+
+ System.out.print("now sorting - slow version... ");
+ A = B;
+ org.apache.mahout.matrix.matrix.doublealgo.DoubleMatrix1DComparator fun = new org.apache.mahout.matrix.matrix.doublealgo.DoubleMatrix1DComparator() {
+ public int compare(DoubleMatrix1D x, DoubleMatrix1D y) {
+ double a = org.apache.mahout.matrix.matrix.doublealgo.Statistic.bin(x).median();
+ double b = org.apache.mahout.matrix.matrix.doublealgo.Statistic.bin(y).median();
+ //double a = x.aggregate(F.plus,F.log);
+ //double b = y.aggregate(F.plus,F.log);
+ return a < b ? -1 : (a==b) ? 0 : 1;
+ }
+ };
+ timer.reset().start();
+ A = sort.sort(A,fun);
+ timer.stop().display();
}
*/
/**
@@ -695,73 +695,73 @@
* Sorts by sum of row.
*/
public static void zdemo6() {
- Sorting sort = quickSort;
- double[][] values = {
- { 3,7,0 },
- { 2,1,0 },
- { 2,2,0 },
- { 1,8,0 },
- { 2,5,0 },
- { 7,0,0 },
- { 2,3,0 },
- { 1,0,0 },
- { 4,0,0 },
- { 2,0,0 }
- };
- DoubleMatrix2D A = DoubleFactory2D.dense.make(values);
- DoubleMatrix2D B,C;
- /*
- DoubleMatrix1DComparator comp = new DoubleMatrix1DComparator() {
- public int compare(DoubleMatrix1D a, DoubleMatrix1D b) {
- double as = a.zSum(); double bs = b.zSum();
- return as < bs ? -1 : as == bs ? 0 : 1;
- }
- };
- */
- System.out.println("\n\nunsorted:"+A);
- B = quickSort.sort(A,1);
- C = quickSort.sort(B,0);
- System.out.println("quick sorted :"+C);
-
- B = mergeSort.sort(A,1);
- C = mergeSort.sort(B,0);
- System.out.println("merge sorted :"+C);
-
+ Sorting sort = quickSort;
+ double[][] values = {
+ { 3,7,0 },
+ { 2,1,0 },
+ { 2,2,0 },
+ { 1,8,0 },
+ { 2,5,0 },
+ { 7,0,0 },
+ { 2,3,0 },
+ { 1,0,0 },
+ { 4,0,0 },
+ { 2,0,0 }
+ };
+ DoubleMatrix2D A = DoubleFactory2D.dense.make(values);
+ DoubleMatrix2D B,C;
+ /*
+ DoubleMatrix1DComparator comp = new DoubleMatrix1DComparator() {
+ public int compare(DoubleMatrix1D a, DoubleMatrix1D b) {
+ double as = a.zSum(); double bs = b.zSum();
+ return as < bs ? -1 : as == bs ? 0 : 1;
+ }
+ };
+ */
+ System.out.println("\n\nunsorted:"+A);
+ B = quickSort.sort(A,1);
+ C = quickSort.sort(B,0);
+ System.out.println("quick sorted :"+C);
+
+ B = mergeSort.sort(A,1);
+ C = mergeSort.sort(B,0);
+ System.out.println("merge sorted :"+C);
+
}
/**
* Demonstrates sorting with precomputation of aggregates, comparing mergesort with quicksort.
*/
public static void zdemo7(int rows, int columns, boolean print) {
- // for reliable benchmarks, call this method twice: once with small dummy parameters to "warm up" the jitter, then with your real work-load
-
- System.out.println("\n\n");
- System.out.println("now initializing... ");
-
- final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
- DoubleMatrix2D A = org.apache.mahout.matrix.matrix.DoubleFactory2D.dense.make(rows,columns);
- A.assign(new org.apache.mahout.jet.random.engine.DRand()); // initialize randomly
- DoubleMatrix2D B = A.copy();
-
- double[] v1 = A.viewColumn(0).toArray();
- double[] v2 = A.viewColumn(0).toArray();
- System.out.print("now quick sorting... ");
- org.apache.mahout.matrix.Timer timer = new org.apache.mahout.matrix.Timer().start();
- quickSort.sort(A,0);
- timer.stop().display();
-
- System.out.print("now merge sorting... ");
- timer.reset().start();
- mergeSort.sort(A,0);
- timer.stop().display();
-
- System.out.print("now quick sorting with simple aggregation... ");
- timer.reset().start();
- quickSort.sort(A,v1);
- timer.stop().display();
-
- System.out.print("now merge sorting with simple aggregation... ");
- timer.reset().start();
- mergeSort.sort(A,v2);
- timer.stop().display();
+ // for reliable benchmarks, call this method twice: once with small dummy parameters to "warm up" the jitter, then with your real work-load
+
+ System.out.println("\n\n");
+ System.out.println("now initializing... ");
+
+ final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
+ DoubleMatrix2D A = org.apache.mahout.matrix.matrix.DoubleFactory2D.dense.make(rows,columns);
+ A.assign(new org.apache.mahout.jet.random.engine.DRand()); // initialize randomly
+ DoubleMatrix2D B = A.copy();
+
+ double[] v1 = A.viewColumn(0).toArray();
+ double[] v2 = A.viewColumn(0).toArray();
+ System.out.print("now quick sorting... ");
+ org.apache.mahout.matrix.Timer timer = new org.apache.mahout.matrix.Timer().start();
+ quickSort.sort(A,0);
+ timer.stop().display();
+
+ System.out.print("now merge sorting... ");
+ timer.reset().start();
+ mergeSort.sort(A,0);
+ timer.stop().display();
+
+ System.out.print("now quick sorting with simple aggregation... ");
+ timer.reset().start();
+ quickSort.sort(A,v1);
+ timer.stop().display();
+
+ System.out.print("now merge sorting with simple aggregation... ");
+ timer.reset().start();
+ mergeSort.sort(A,v2);
+ timer.stop().display();
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Statistic.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Statistic.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Statistic.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Statistic.java Wed Nov 25 03:41:28 2009
@@ -25,31 +25,31 @@
Examples:
<table border="1" cellspacing="0" dwcopytype="CopyTableRow">
<tr valign="top" align="center">
- <td><tt>A</tt></td>
- <td><tt>covariance(A)</tt></td>
- <td><tt>correlation(covariance(A))</tt></td>
- <td><tt>distance(A,EUCLID)</tt></td>
+ <td><tt>A</tt></td>
+ <td><tt>covariance(A)</tt></td>
+ <td><tt>correlation(covariance(A))</tt></td>
+ <td><tt>distance(A,EUCLID)</tt></td>
</tr>
<tr valign="top">
- <td><tt> 4 x 3 matrix<br>
- 1 2 3<br>
- 2 4 6<br>
- 3 6 9<br>
- 4 -8 -10 </tt> </td>
- <td><tt> 3 x 3 matrix<br>
- 1.25 -3.5 -4.5<br>
- -3.5 29 39 <br>
- -4.5 39 52.5 </tt></td>
- <td><tt> 3 x 3 matrix<br>
- 1 -0.581318 -0.555492<br>
- -0.581318 1 0.999507<br>
- -0.555492 0.999507 1
- </tt></td>
- <td><tt> 3 x 3 matrix<br>
- 0 12.569805 15.874508<br>
- 12.569805 0 4.242641<br>
- 15.874508 4.242641 0
- </tt> <tt> </tt></td>
+ <td><tt> 4 x 3 matrix<br>
+ 1 2 3<br>
+ 2 4 6<br>
+ 3 6 9<br>
+ 4 -8 -10 </tt> </td>
+ <td><tt> 3 x 3 matrix<br>
+ 1.25 -3.5 -4.5<br>
+ -3.5 29 39 <br>
+ -4.5 39 52.5 </tt></td>
+ <td><tt> 3 x 3 matrix<br>
+ 1 -0.581318 -0.555492<br>
+ -0.581318 1 0.999507<br>
+ -0.555492 0.999507 1
+ </tt></td>
+ <td><tt> 3 x 3 matrix<br>
+ 0 12.569805 15.874508<br>
+ 12.569805 0 4.242641<br>
+ 15.874508 4.242641 0
+ </tt> <tt> </tt></td>
</tr>
</table>
@@ -61,56 +61,56 @@
*/
@Deprecated
public class Statistic extends Object {
- private static final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
- /**
- * Euclidean distance function; <tt>Sqrt(Sum( (x[i]-y[i])^2 ))</tt>.
- */
- public static final VectorVectorFunction EUCLID = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return Math.sqrt(a.aggregate(b, F.plus, F.chain(F.square,F.minus)));
- }
- };
-
- /**
- * Bray-Curtis distance function; <tt>Sum( abs(x[i]-y[i]) ) / Sum( x[i]+y[i] )</tt>.
- */
- public static final VectorVectorFunction BRAY_CURTIS = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.plus, F.chain(F.abs,F.minus)) / a.aggregate(b, F.plus, F.plus);
- }
- };
-
- /**
- * Canberra distance function; <tt>Sum( abs(x[i]-y[i]) / abs(x[i]+y[i]) )</tt>.
- */
- public static final VectorVectorFunction CANBERRA = new VectorVectorFunction() {
- DoubleDoubleFunction fun = new DoubleDoubleFunction() {
- public final double apply(double a, double b) {
- return Math.abs(a-b) / Math.abs(a+b);
- }
- };
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.plus, fun);
- }
- };
-
- /**
- * Maximum distance function; <tt>Max( abs(x[i]-y[i]) )</tt>.
- */
- public static final VectorVectorFunction MAXIMUM = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.max, F.chain(F.abs,F.minus));
- }
- };
-
- /**
- * Manhattan distance function; <tt>Sum( abs(x[i]-y[i]) )</tt>.
- */
- public static final VectorVectorFunction MANHATTAN = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.plus, F.chain(F.abs,F.minus));
- }
- };
+ private static final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
+ /**
+ * Euclidean distance function; <tt>Sqrt(Sum( (x[i]-y[i])^2 ))</tt>.
+ */
+ public static final VectorVectorFunction EUCLID = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return Math.sqrt(a.aggregate(b, F.plus, F.chain(F.square,F.minus)));
+ }
+ };
+
+ /**
+ * Bray-Curtis distance function; <tt>Sum( abs(x[i]-y[i]) ) / Sum( x[i]+y[i] )</tt>.
+ */
+ public static final VectorVectorFunction BRAY_CURTIS = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.plus, F.chain(F.abs,F.minus)) / a.aggregate(b, F.plus, F.plus);
+ }
+ };
+
+ /**
+ * Canberra distance function; <tt>Sum( abs(x[i]-y[i]) / abs(x[i]+y[i]) )</tt>.
+ */
+ public static final VectorVectorFunction CANBERRA = new VectorVectorFunction() {
+ DoubleDoubleFunction fun = new DoubleDoubleFunction() {
+ public final double apply(double a, double b) {
+ return Math.abs(a-b) / Math.abs(a+b);
+ }
+ };
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.plus, fun);
+ }
+ };
+
+ /**
+ * Maximum distance function; <tt>Max( abs(x[i]-y[i]) )</tt>.
+ */
+ public static final VectorVectorFunction MAXIMUM = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.max, F.chain(F.abs,F.minus));
+ }
+ };
+
+ /**
+ * Manhattan distance function; <tt>Sum( abs(x[i]-y[i]) )</tt>.
+ */
+ public static final VectorVectorFunction MANHATTAN = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.plus, F.chain(F.abs,F.minus));
+ }
+ };
@@ -152,18 +152,18 @@
* @see hep.aida.bin.BinFunctions1D
*
public static DoubleMatrix2D aggregate(DoubleMatrix2D matrix, hep.aida.bin.BinFunction1D[] aggr, DoubleMatrix2D result) {
- DynamicBin1D bin = new DynamicBin1D();
- double[] elements = new double[matrix.rows()];
- org.apache.mahout.matrix.list.DoubleArrayList values = new org.apache.mahout.matrix.list.DoubleArrayList(elements);
- for (int column=matrix.columns(); --column >= 0; ) {
- matrix.viewColumn(column).toArray(elements); // copy column into values
- bin.clear();
- bin.addAllOf(values);
- for (int i=aggr.length; --i >= 0; ) {
- result.set(i, column, aggr[i].apply(bin));
- }
- }
- return result;
+ DynamicBin1D bin = new DynamicBin1D();
+ double[] elements = new double[matrix.rows()];
+ org.apache.mahout.matrix.list.DoubleArrayList values = new org.apache.mahout.matrix.list.DoubleArrayList(elements);
+ for (int column=matrix.columns(); --column >= 0; ) {
+ matrix.viewColumn(column).toArray(elements); // copy column into values
+ bin.clear();
+ bin.addAllOf(values);
+ for (int i=aggr.length; --i >= 0; ) {
+ result.set(i, column, aggr[i].apply(bin));
+ }
+ }
+ return result;
}
*/
/**
@@ -220,9 +220,9 @@
@return a bin holding the statistics measures of the vector.
*
public static DynamicBin1D bin(DoubleMatrix1D vector) {
- DynamicBin1D bin = new DynamicBin1D();
- bin.addAllOf(DoubleFactory1D.dense.toList(vector));
- return bin;
+ DynamicBin1D bin = new DynamicBin1D();
+ bin.addAllOf(DoubleFactory1D.dense.toList(vector));
+ return bin;
}
*/
/**
@@ -240,20 +240,20 @@
* @return the modified covariance, now correlation matrix (for convenience only).
*/
public static DoubleMatrix2D correlation(DoubleMatrix2D covariance) {
- for (int i=covariance.columns(); --i >= 0; ) {
- for (int j=i; --j >= 0; ) {
- double stdDev1 = Math.sqrt(covariance.getQuick(i,i));
- double stdDev2 = Math.sqrt(covariance.getQuick(j,j));
- double cov = covariance.getQuick(i,j);
- double corr = cov / (stdDev1*stdDev2);
-
- covariance.setQuick(i,j,corr);
- covariance.setQuick(j,i,corr); // symmetric
- }
- }
- for (int i=covariance.columns(); --i >= 0; ) covariance.setQuick(i,i,1);
+ for (int i=covariance.columns(); --i >= 0; ) {
+ for (int j=i; --j >= 0; ) {
+ double stdDev1 = Math.sqrt(covariance.getQuick(i,i));
+ double stdDev2 = Math.sqrt(covariance.getQuick(j,j));
+ double cov = covariance.getQuick(i,j);
+ double corr = cov / (stdDev1*stdDev2);
+
+ covariance.setQuick(i,j,corr);
+ covariance.setQuick(j,i,corr); // symmetric
+ }
+ }
+ for (int i=covariance.columns(); --i >= 0; ) covariance.setQuick(i,i,1);
- return covariance;
+ return covariance;
}
/**
* Constructs and returns the covariance matrix of the given matrix.
@@ -268,26 +268,26 @@
* @return the covariance matrix (<tt>n x n, n=matrix.columns</tt>).
*/
public static DoubleMatrix2D covariance(DoubleMatrix2D matrix) {
- int rows = matrix.rows();
- int columns = matrix.columns();
- DoubleMatrix2D covariance = new org.apache.mahout.matrix.matrix.impl.DenseDoubleMatrix2D(columns,columns);
-
- double[] sums = new double[columns];
- DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
- for (int i=columns; --i >= 0; ) {
- cols[i] = matrix.viewColumn(i);
- sums[i] = cols[i].zSum();
- }
-
- for (int i=columns; --i >= 0; ) {
- for (int j=i+1; --j >= 0; ) {
- double sumOfProducts = cols[i].zDotProduct(cols[j]);
- double cov = (sumOfProducts - sums[i]*sums[j]/rows) / rows;
- covariance.setQuick(i,j,cov);
- covariance.setQuick(j,i,cov); // symmetric
- }
- }
- return covariance;
+ int rows = matrix.rows();
+ int columns = matrix.columns();
+ DoubleMatrix2D covariance = new org.apache.mahout.matrix.matrix.impl.DenseDoubleMatrix2D(columns,columns);
+
+ double[] sums = new double[columns];
+ DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
+ for (int i=columns; --i >= 0; ) {
+ cols[i] = matrix.viewColumn(i);
+ sums[i] = cols[i].zSum();
+ }
+
+ for (int i=columns; --i >= 0; ) {
+ for (int j=i+1; --j >= 0; ) {
+ double sumOfProducts = cols[i].zDotProduct(cols[j]);
+ double cov = (sumOfProducts - sums[i]*sums[j]/rows) / rows;
+ covariance.setQuick(i,j,cov);
+ covariance.setQuick(j,i,cov); // symmetric
+ }
+ }
+ return covariance;
}
/**
2-d OLAP cube operator; Fills all cells of the given vectors into the given histogram.
@@ -326,33 +326,33 @@
@throws IllegalArgumentException if <tt>x.size() != y.size() || y.size() != weights.size()</tt>.
*
public static hep.aida.IHistogram2D cube(DoubleMatrix1D x, DoubleMatrix1D y, DoubleMatrix1D weights) {
- if (x.size() != y.size() || y.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
-
- double epsilon = 1.0E-9;
- org.apache.mahout.matrix.list.DoubleArrayList distinct = new org.apache.mahout.matrix.list.DoubleArrayList();
- double[] vals = new double[x.size()];
- org.apache.mahout.matrix.list.DoubleArrayList sorted = new org.apache.mahout.matrix.list.DoubleArrayList(vals);
-
- // compute distinct values of x
- x.toArray(vals); // copy x into vals
- sorted.sort();
- org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
- // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
- if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
- distinct.trimToSize();
- hep.aida.IAxis xaxis = new hep.aida.ref.VariableAxis(distinct.elements());
-
- // compute distinct values of y
- y.toArray(vals);
- sorted.sort();
- org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
- // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
- if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
- distinct.trimToSize();
- hep.aida.IAxis yaxis = new hep.aida.ref.VariableAxis(distinct.elements());
+ if (x.size() != y.size() || y.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
+
+ double epsilon = 1.0E-9;
+ org.apache.mahout.matrix.list.DoubleArrayList distinct = new org.apache.mahout.matrix.list.DoubleArrayList();
+ double[] vals = new double[x.size()];
+ org.apache.mahout.matrix.list.DoubleArrayList sorted = new org.apache.mahout.matrix.list.DoubleArrayList(vals);
+
+ // compute distinct values of x
+ x.toArray(vals); // copy x into vals
+ sorted.sort();
+ org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
+ // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
+ if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
+ distinct.trimToSize();
+ hep.aida.IAxis xaxis = new hep.aida.ref.VariableAxis(distinct.elements());
+
+ // compute distinct values of y
+ y.toArray(vals);
+ sorted.sort();
+ org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
+ // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
+ if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
+ distinct.trimToSize();
+ hep.aida.IAxis yaxis = new hep.aida.ref.VariableAxis(distinct.elements());
- hep.aida.IHistogram2D histo = new hep.aida.ref.Histogram2D("Cube",xaxis,yaxis);
- return histogram(histo,x,y,weights);
+ hep.aida.IHistogram2D histo = new hep.aida.ref.Histogram2D("Cube",xaxis,yaxis);
+ return histogram(histo,x,y,weights);
}
*/
/**
@@ -366,42 +366,42 @@
@throws IllegalArgumentException if <tt>x.size() != y.size() || x.size() != z.size() || x.size() != weights.size()</tt>.
*
public static hep.aida.IHistogram3D cube(DoubleMatrix1D x, DoubleMatrix1D y, DoubleMatrix1D z, DoubleMatrix1D weights) {
- if (x.size() != y.size() || x.size() != z.size() || x.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
-
- double epsilon = 1.0E-9;
- org.apache.mahout.matrix.list.DoubleArrayList distinct = new org.apache.mahout.matrix.list.DoubleArrayList();
- double[] vals = new double[x.size()];
- org.apache.mahout.matrix.list.DoubleArrayList sorted = new org.apache.mahout.matrix.list.DoubleArrayList(vals);
-
- // compute distinct values of x
- x.toArray(vals); // copy x into vals
- sorted.sort();
- org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
- // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
- if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
- distinct.trimToSize();
- hep.aida.IAxis xaxis = new hep.aida.ref.VariableAxis(distinct.elements());
-
- // compute distinct values of y
- y.toArray(vals);
- sorted.sort();
- org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
- // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
- if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
- distinct.trimToSize();
- hep.aida.IAxis yaxis = new hep.aida.ref.VariableAxis(distinct.elements());
-
- // compute distinct values of z
- z.toArray(vals);
- sorted.sort();
- org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
- // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
- if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
- distinct.trimToSize();
- hep.aida.IAxis zaxis = new hep.aida.ref.VariableAxis(distinct.elements());
+ if (x.size() != y.size() || x.size() != z.size() || x.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
+
+ double epsilon = 1.0E-9;
+ org.apache.mahout.matrix.list.DoubleArrayList distinct = new org.apache.mahout.matrix.list.DoubleArrayList();
+ double[] vals = new double[x.size()];
+ org.apache.mahout.matrix.list.DoubleArrayList sorted = new org.apache.mahout.matrix.list.DoubleArrayList(vals);
+
+ // compute distinct values of x
+ x.toArray(vals); // copy x into vals
+ sorted.sort();
+ org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
+ // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
+ if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
+ distinct.trimToSize();
+ hep.aida.IAxis xaxis = new hep.aida.ref.VariableAxis(distinct.elements());
+
+ // compute distinct values of y
+ y.toArray(vals);
+ sorted.sort();
+ org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
+ // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
+ if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
+ distinct.trimToSize();
+ hep.aida.IAxis yaxis = new hep.aida.ref.VariableAxis(distinct.elements());
+
+ // compute distinct values of z
+ z.toArray(vals);
+ sorted.sort();
+ org.apache.mahout.jet.stat.Descriptive.frequencies(sorted, distinct, null);
+ // since bins are right-open [from,to) we need an additional dummy bin so that the last distinct value does not fall into the overflow bin
+ if (distinct.size()>0) distinct.add(distinct.get(distinct.size()-1) + epsilon);
+ distinct.trimToSize();
+ hep.aida.IAxis zaxis = new hep.aida.ref.VariableAxis(distinct.elements());
- hep.aida.IHistogram3D histo = new hep.aida.ref.Histogram3D("Cube",xaxis,yaxis,zaxis);
- return histogram(histo,x,y,z,weights);
+ hep.aida.IHistogram3D histo = new hep.aida.ref.Histogram3D("Cube",xaxis,yaxis,zaxis);
+ return histogram(histo,x,y,z,weights);
}
*/
/**
@@ -409,10 +409,10 @@
*/
public static void demo1() {
double[][] values = {
- { 1, 2, 3 },
- { 2, 4, 6 },
- { 3, 6, 9 },
- { 4, -8, -10 }
+ { 1, 2, 3 },
+ { 2, 4, 6 },
+ { 3, 6, 9 },
+ { 4, -8, -10 }
};
DoubleFactory2D factory = DoubleFactory2D.dense;
DoubleMatrix2D A = factory.make(values);
@@ -445,8 +445,8 @@
timer.stop().display();
if (print) {
- System.out.println("printing result...");
- System.out.println(corr);
+ System.out.println("printing result...");
+ System.out.println(corr);
}
System.out.println("done.");
}
@@ -455,11 +455,11 @@
*/
public static void demo3(VectorVectorFunction norm) {
double[][] values = {
- { -0.9611052, -0.25421095 },
- { 0.4308269, -0.69932648 },
- { -1.2071029, 0.62030596 },
- { 1.5345166, 0.02135884},
- {-1.1341542, 0.20388430}
+ { -0.9611052, -0.25421095 },
+ { 0.4308269, -0.69932648 },
+ { -1.2071029, 0.62030596 },
+ { 1.5345166, 0.02135884},
+ {-1.1341542, 0.20388430}
};
System.out.println("\n\ninitializing...");
@@ -481,34 +481,34 @@
* @return the distance matrix (<tt>n x n, n=matrix.columns</tt>).
*/
public static DoubleMatrix2D distance(DoubleMatrix2D matrix, VectorVectorFunction distanceFunction) {
- int columns = matrix.columns();
- DoubleMatrix2D distance = new org.apache.mahout.matrix.matrix.impl.DenseDoubleMatrix2D(columns,columns);
+ int columns = matrix.columns();
+ DoubleMatrix2D distance = new org.apache.mahout.matrix.matrix.impl.DenseDoubleMatrix2D(columns,columns);
- // cache views
- DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
- for (int i=columns; --i >= 0; ) {
- cols[i] = matrix.viewColumn(i);
- }
-
- // work out all permutations
- for (int i=columns; --i >= 0; ) {
- for (int j=i; --j >= 0; ) {
- double d = distanceFunction.apply(cols[i], cols[j]);
- distance.setQuick(i,j,d);
- distance.setQuick(j,i,d); // symmetric
- }
- }
- return distance;
+ // cache views
+ DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
+ for (int i=columns; --i >= 0; ) {
+ cols[i] = matrix.viewColumn(i);
+ }
+
+ // work out all permutations
+ for (int i=columns; --i >= 0; ) {
+ for (int j=i; --j >= 0; ) {
+ double d = distanceFunction.apply(cols[i], cols[j]);
+ distance.setQuick(i,j,d);
+ distance.setQuick(j,i,d); // symmetric
+ }
+ }
+ return distance;
}
/**
* Fills all cells of the given vector into the given histogram.
* @return <tt>histo</tt> (for convenience only).
*
public static hep.aida.IHistogram1D histogram(hep.aida.IHistogram1D histo, DoubleMatrix1D vector) {
- for (int i=vector.size(); --i >= 0; ) {
- histo.fill(vector.getQuick(i));
- }
- return histo;
+ for (int i=vector.size(); --i >= 0; ) {
+ histo.fill(vector.getQuick(i));
+ }
+ return histo;
}
**
* Fills all cells of the given vectors into the given histogram.
@@ -516,11 +516,11 @@
* @throws IllegalArgumentException if <tt>x.size() != y.size()</tt>.
*
public static hep.aida.IHistogram2D histogram(hep.aida.IHistogram2D histo, DoubleMatrix1D x, DoubleMatrix1D y) {
- if (x.size() != y.size()) throw new IllegalArgumentException("vectors must have same size");
- for (int i=x.size(); --i >= 0; ) {
- histo.fill(x.getQuick(i), y.getQuick(i));
- }
- return histo;
+ if (x.size() != y.size()) throw new IllegalArgumentException("vectors must have same size");
+ for (int i=x.size(); --i >= 0; ) {
+ histo.fill(x.getQuick(i), y.getQuick(i));
+ }
+ return histo;
}
**
* Fills all cells of the given vectors into the given histogram.
@@ -528,11 +528,11 @@
* @throws IllegalArgumentException if <tt>x.size() != y.size() || y.size() != weights.size()</tt>.
*
public static hep.aida.IHistogram2D histogram(hep.aida.IHistogram2D histo, DoubleMatrix1D x, DoubleMatrix1D y, DoubleMatrix1D weights) {
- if (x.size() != y.size() || y.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
- for (int i=x.size(); --i >= 0; ) {
- histo.fill(x.getQuick(i), y.getQuick(i), weights.getQuick(i));
- }
- return histo;
+ if (x.size() != y.size() || y.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
+ for (int i=x.size(); --i >= 0; ) {
+ histo.fill(x.getQuick(i), y.getQuick(i), weights.getQuick(i));
+ }
+ return histo;
}
*
* Fills all cells of the given vectors into the given histogram.
@@ -540,20 +540,20 @@
* @throws IllegalArgumentException if <tt>x.size() != y.size() || x.size() != z.size() || x.size() != weights.size()</tt>.
*
public static hep.aida.IHistogram3D histogram(hep.aida.IHistogram3D histo, DoubleMatrix1D x, DoubleMatrix1D y, DoubleMatrix1D z, DoubleMatrix1D weights) {
- if (x.size() != y.size() || x.size() != z.size() || x.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
- for (int i=x.size(); --i >= 0; ) {
- histo.fill(x.getQuick(i), y.getQuick(i), z.getQuick(i), weights.getQuick(i));
- }
- return histo;
+ if (x.size() != y.size() || x.size() != z.size() || x.size() != weights.size()) throw new IllegalArgumentException("vectors must have same size");
+ for (int i=x.size(); --i >= 0; ) {
+ histo.fill(x.getQuick(i), y.getQuick(i), z.getQuick(i), weights.getQuick(i));
+ }
+ return histo;
}
**
* Benchmarks covariance computation.
*/
public static void main(String[] args) {
- int rows = Integer.parseInt(args[0]);
- int columns = Integer.parseInt(args[1]);
- boolean print = args[2].equals("print");
- demo2(rows,columns,print);
+ int rows = Integer.parseInt(args[0]);
+ int columns = Integer.parseInt(args[1]);
+ boolean print = args[2].equals("print");
+ demo2(rows,columns,print);
}
/**
Constructs and returns a sampling view with a size of <tt>round(matrix.size() * fraction)</tt>.
@@ -567,27 +567,27 @@
@see org.apache.mahout.jet.random.sampling.RandomSampler
*/
public static DoubleMatrix1D viewSample(DoubleMatrix1D matrix, double fraction, RandomEngine randomGenerator) {
- // check preconditions and allow for a little tolerance
- double epsilon = 1e-09;
- if (fraction < 0 - epsilon || fraction > 1 + epsilon) throw new IllegalArgumentException();
- if (fraction < 0) fraction = 0;
- if (fraction > 1) fraction = 1;
-
- // random generator seeded with current time
- if (randomGenerator==null) randomGenerator = new org.apache.mahout.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
-
- int ncols = (int) Math.round(matrix.size() * fraction);
- int max = ncols;
- long[] selected = new long[max]; // sampler works on long's, not int's
-
- // sample
- int n = ncols;
- int N = matrix.size();
- org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
- int[] selectedCols = new int[n];
- for (int i=0; i<n; i++) selectedCols[i] = (int) selected[i];
+ // check preconditions and allow for a little tolerance
+ double epsilon = 1e-09;
+ if (fraction < 0 - epsilon || fraction > 1 + epsilon) throw new IllegalArgumentException();
+ if (fraction < 0) fraction = 0;
+ if (fraction > 1) fraction = 1;
+
+ // random generator seeded with current time
+ if (randomGenerator==null) randomGenerator = new org.apache.mahout.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
+
+ int ncols = (int) Math.round(matrix.size() * fraction);
+ int max = ncols;
+ long[] selected = new long[max]; // sampler works on long's, not int's
+
+ // sample
+ int n = ncols;
+ int N = matrix.size();
+ org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
+ int[] selectedCols = new int[n];
+ for (int i=0; i<n; i++) selectedCols[i] = (int) selected[i];
- return matrix.viewSelection(selectedCols);
+ return matrix.viewSelection(selectedCols);
}
/**
Constructs and returns a sampling view with <tt>round(matrix.rows() * rowFraction)</tt> rows and <tt>round(matrix.columns() * columnFraction)</tt> columns.
@@ -596,53 +596,53 @@
Examples:
<table border="1" cellspacing="0">
<tr valign="top" align="center">
- <td>
- <div align="left"><tt>matrix</tt></div>
- </td>
- <td>
- <div align="left"><tt>rowFraction=0.2<br>
- columnFraction=0.2</tt></div>
- </td>
- <td>
- <div align="left"><tt>rowFraction=0.2<br>
- columnFraction=1.0 </tt></div>
- </td>
- <td>
- <div align="left"><tt>rowFraction=1.0<br>
- columnFraction=0.2 </tt></div>
- </td>
+ <td>
+ <div align="left"><tt>matrix</tt></div>
+ </td>
+ <td>
+ <div align="left"><tt>rowFraction=0.2<br>
+ columnFraction=0.2</tt></div>
+ </td>
+ <td>
+ <div align="left"><tt>rowFraction=0.2<br>
+ columnFraction=1.0 </tt></div>
+ </td>
+ <td>
+ <div align="left"><tt>rowFraction=1.0<br>
+ columnFraction=0.2 </tt></div>
+ </td>
</tr>
<tr valign="top">
- <td><tt> 10 x 10 matrix<br>
- 1 2 3 4 5 6 7 8 9 10<br>
- 11 12 13 14 15 16 17 18 19 20<br>
- 21 22 23 24 25 26 27 28 29 30<br>
- 31 32 33 34 35 36 37 38 39 40<br>
- 41 42 43 44 45 46 47 48 49 50<br>
- 51 52 53 54 55 56 57 58 59 60<br>
- 61 62 63 64 65 66 67 68 69 70<br>
- 71 72 73 74 75 76 77 78 79 80<br>
- 81 82 83 84 85 86 87 88 89 90<br>
- 91 92 93 94 95 96 97 98 99 100
- </tt> </td>
- <td><tt> 2 x 2 matrix<br>
- 43 50<br>
- 53 60 </tt></td>
- <td><tt> 2 x 10 matrix<br>
- 41 42 43 44 45 46 47 48 49 50<br>
- 91 92 93 94 95 96 97 98 99 100
- </tt> </td>
- <td><tt> 10 x 2 matrix<br>
- 4 8<br>
- 14 18<br>
- 24 28<br>
- 34 38<br>
- 44 48<br>
- 54 58<br>
- 64 68<br>
- 74 78<br>
- 84 88<br>
- 94 98 </tt> </td>
+ <td><tt> 10 x 10 matrix<br>
+ 1 2 3 4 5 6 7 8 9 10<br>
+ 11 12 13 14 15 16 17 18 19 20<br>
+ 21 22 23 24 25 26 27 28 29 30<br>
+ 31 32 33 34 35 36 37 38 39 40<br>
+ 41 42 43 44 45 46 47 48 49 50<br>
+ 51 52 53 54 55 56 57 58 59 60<br>
+ 61 62 63 64 65 66 67 68 69 70<br>
+ 71 72 73 74 75 76 77 78 79 80<br>
+ 81 82 83 84 85 86 87 88 89 90<br>
+ 91 92 93 94 95 96 97 98 99 100
+ </tt> </td>
+ <td><tt> 2 x 2 matrix<br>
+ 43 50<br>
+ 53 60 </tt></td>
+ <td><tt> 2 x 10 matrix<br>
+ 41 42 43 44 45 46 47 48 49 50<br>
+ 91 92 93 94 95 96 97 98 99 100
+ </tt> </td>
+ <td><tt> 10 x 2 matrix<br>
+ 4 8<br>
+ 14 18<br>
+ 24 28<br>
+ 34 38<br>
+ 44 48<br>
+ 54 58<br>
+ 64 68<br>
+ 74 78<br>
+ 84 88<br>
+ 94 98 </tt> </td>
</tr>
</table>
@param matrix any matrix.
@@ -654,39 +654,39 @@
@see org.apache.mahout.jet.random.sampling.RandomSampler
*/
public static DoubleMatrix2D viewSample(DoubleMatrix2D matrix, double rowFraction, double columnFraction, RandomEngine randomGenerator) {
- // check preconditions and allow for a little tolerance
- double epsilon = 1e-09;
- if (rowFraction < 0 - epsilon || rowFraction > 1 + epsilon) throw new IllegalArgumentException();
- if (rowFraction < 0) rowFraction = 0;
- if (rowFraction > 1) rowFraction = 1;
-
- if (columnFraction < 0 - epsilon || columnFraction > 1 + epsilon) throw new IllegalArgumentException();
- if (columnFraction < 0) columnFraction = 0;
- if (columnFraction > 1) columnFraction = 1;
-
- // random generator seeded with current time
- if (randomGenerator==null) randomGenerator = new org.apache.mahout.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
-
- int nrows = (int) Math.round(matrix.rows() * rowFraction);
- int ncols = (int) Math.round(matrix.columns() * columnFraction);
- int max = Math.max(nrows,ncols);
- long[] selected = new long[max]; // sampler works on long's, not int's
-
- // sample rows
- int n = nrows;
- int N = matrix.rows();
- org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
- int[] selectedRows = new int[n];
- for (int i=0; i<n; i++) selectedRows[i] = (int) selected[i];
-
- // sample columns
- n = ncols;
- N = matrix.columns();
- org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
- int[] selectedCols = new int[n];
- for (int i=0; i<n; i++) selectedCols[i] = (int) selected[i];
+ // check preconditions and allow for a little tolerance
+ double epsilon = 1e-09;
+ if (rowFraction < 0 - epsilon || rowFraction > 1 + epsilon) throw new IllegalArgumentException();
+ if (rowFraction < 0) rowFraction = 0;
+ if (rowFraction > 1) rowFraction = 1;
+
+ if (columnFraction < 0 - epsilon || columnFraction > 1 + epsilon) throw new IllegalArgumentException();
+ if (columnFraction < 0) columnFraction = 0;
+ if (columnFraction > 1) columnFraction = 1;
+
+ // random generator seeded with current time
+ if (randomGenerator==null) randomGenerator = new org.apache.mahout.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
+
+ int nrows = (int) Math.round(matrix.rows() * rowFraction);
+ int ncols = (int) Math.round(matrix.columns() * columnFraction);
+ int max = Math.max(nrows,ncols);
+ long[] selected = new long[max]; // sampler works on long's, not int's
+
+ // sample rows
+ int n = nrows;
+ int N = matrix.rows();
+ org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
+ int[] selectedRows = new int[n];
+ for (int i=0; i<n; i++) selectedRows[i] = (int) selected[i];
+
+ // sample columns
+ n = ncols;
+ N = matrix.columns();
+ org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
+ int[] selectedCols = new int[n];
+ for (int i=0; i<n; i++) selectedCols[i] = (int) selected[i];
- return matrix.viewSelection(selectedRows, selectedCols);
+ return matrix.viewSelection(selectedRows, selectedCols);
}
/**
Constructs and returns a sampling view with <tt>round(matrix.slices() * sliceFraction)</tt> slices and <tt>round(matrix.rows() * rowFraction)</tt> rows and <tt>round(matrix.columns() * columnFraction)</tt> columns.
@@ -702,51 +702,51 @@
@see org.apache.mahout.jet.random.sampling.RandomSampler
*/
public static DoubleMatrix3D viewSample(DoubleMatrix3D matrix, double sliceFraction, double rowFraction, double columnFraction, RandomEngine randomGenerator) {
- // check preconditions and allow for a little tolerance
- double epsilon = 1e-09;
- if (sliceFraction < 0 - epsilon || sliceFraction > 1 + epsilon) throw new IllegalArgumentException();
- if (sliceFraction < 0) sliceFraction = 0;
- if (sliceFraction > 1) sliceFraction = 1;
-
- if (rowFraction < 0 - epsilon || rowFraction > 1 + epsilon) throw new IllegalArgumentException();
- if (rowFraction < 0) rowFraction = 0;
- if (rowFraction > 1) rowFraction = 1;
-
- if (columnFraction < 0 - epsilon || columnFraction > 1 + epsilon) throw new IllegalArgumentException();
- if (columnFraction < 0) columnFraction = 0;
- if (columnFraction > 1) columnFraction = 1;
-
- // random generator seeded with current time
- if (randomGenerator==null) randomGenerator = new org.apache.mahout.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
-
- int nslices = (int) Math.round(matrix.slices() * sliceFraction);
- int nrows = (int) Math.round(matrix.rows() * rowFraction);
- int ncols = (int) Math.round(matrix.columns() * columnFraction);
- int max = Math.max(nslices,Math.max(nrows,ncols));
- long[] selected = new long[max]; // sampler works on long's, not int's
-
- // sample slices
- int n = nslices;
- int N = matrix.slices();
- org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
- int[] selectedSlices = new int[n];
- for (int i=0; i<n; i++) selectedSlices[i] = (int) selected[i];
-
- // sample rows
- n = nrows;
- N = matrix.rows();
- org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
- int[] selectedRows = new int[n];
- for (int i=0; i<n; i++) selectedRows[i] = (int) selected[i];
-
- // sample columns
- n = ncols;
- N = matrix.columns();
- org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
- int[] selectedCols = new int[n];
- for (int i=0; i<n; i++) selectedCols[i] = (int) selected[i];
+ // check preconditions and allow for a little tolerance
+ double epsilon = 1e-09;
+ if (sliceFraction < 0 - epsilon || sliceFraction > 1 + epsilon) throw new IllegalArgumentException();
+ if (sliceFraction < 0) sliceFraction = 0;
+ if (sliceFraction > 1) sliceFraction = 1;
+
+ if (rowFraction < 0 - epsilon || rowFraction > 1 + epsilon) throw new IllegalArgumentException();
+ if (rowFraction < 0) rowFraction = 0;
+ if (rowFraction > 1) rowFraction = 1;
+
+ if (columnFraction < 0 - epsilon || columnFraction > 1 + epsilon) throw new IllegalArgumentException();
+ if (columnFraction < 0) columnFraction = 0;
+ if (columnFraction > 1) columnFraction = 1;
+
+ // random generator seeded with current time
+ if (randomGenerator==null) randomGenerator = new org.apache.mahout.jet.random.engine.MersenneTwister((int) System.currentTimeMillis());
+
+ int nslices = (int) Math.round(matrix.slices() * sliceFraction);
+ int nrows = (int) Math.round(matrix.rows() * rowFraction);
+ int ncols = (int) Math.round(matrix.columns() * columnFraction);
+ int max = Math.max(nslices,Math.max(nrows,ncols));
+ long[] selected = new long[max]; // sampler works on long's, not int's
+
+ // sample slices
+ int n = nslices;
+ int N = matrix.slices();
+ org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
+ int[] selectedSlices = new int[n];
+ for (int i=0; i<n; i++) selectedSlices[i] = (int) selected[i];
+
+ // sample rows
+ n = nrows;
+ N = matrix.rows();
+ org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
+ int[] selectedRows = new int[n];
+ for (int i=0; i<n; i++) selectedRows[i] = (int) selected[i];
+
+ // sample columns
+ n = ncols;
+ N = matrix.columns();
+ org.apache.mahout.jet.random.sampling.RandomSampler.sample(n,N,n,0,selected,0,randomGenerator);
+ int[] selectedCols = new int[n];
+ for (int i=0; i<n; i++) selectedCols[i] = (int) selected[i];
- return matrix.viewSelection(selectedSlices,selectedRows, selectedCols);
+ return matrix.viewSelection(selectedSlices,selectedRows, selectedCols);
}
/**
* Constructs and returns the distance matrix of the given matrix.
@@ -760,46 +760,46 @@
* @return the distance matrix (<tt>n x n, n=matrix.columns</tt>).
*/
private static DoubleMatrix2D xdistanceOld(DoubleMatrix2D matrix, int norm) {
- /*
- int rows = matrix.rows();
- int columns = matrix.columns();
- DoubleMatrix2D distance = new org.apache.mahout.matrix.matrix.impl.DenseDoubleMatrix2D(columns,columns);
-
- // cache views
- DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
- for (int i=columns; --i >= 0; ) {
- cols[i] = matrix.viewColumn(i);
- }
-
- // setup distance function
- org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
- DoubleDoubleFunction function = null;
- //DoubleDoubleFunction function2 = null;
- if (norm==EUCLID) function = F.chain(F.square,F.minus);
- else if (norm==BRAY_CURTIS) function = F.chain(F.abs,F.minus);
- else if (norm==CANBERRA) function = new DoubleDoubleFunction() {
- public final double apply(double a, double b) { return Math.abs(a-b) / Math.abs(a+b);}
- };
- else if (norm==MAXIMUM) function = F.chain(F.abs,F.minus);
- else if (norm==MANHATTAN) function = F.chain(F.abs,F.minus);
- else throw new IllegalArgumentException("Unknown norm");
-
- // work out all permutations
- for (int i=columns; --i >= 0; ) {
- for (int j=i; --j >= 0; ) {
- double d = 0;
- if (norm==EUCLID) d = Math.sqrt(cols[i].aggregate(cols[j], F.plus, function));
- else if (norm==BRAY_CURTIS) d = cols[i].aggregate(cols[j], F.plus, function) / cols[i].aggregate(cols[j], F.plus, F.plus);
- else if (norm==CANBERRA) d = cols[i].aggregate(cols[j], F.plus, function);
- else if (norm==MAXIMUM) d = cols[i].aggregate(cols[j], F.max, function);
- else if (norm==MANHATTAN) d = cols[i].aggregate(cols[j], F.plus, function);
- distance.setQuick(i,j,d);
- distance.setQuick(j,i,d); // symmetric
- }
- }
- return distance;
- */
- return null;
+ /*
+ int rows = matrix.rows();
+ int columns = matrix.columns();
+ DoubleMatrix2D distance = new org.apache.mahout.matrix.matrix.impl.DenseDoubleMatrix2D(columns,columns);
+
+ // cache views
+ DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
+ for (int i=columns; --i >= 0; ) {
+ cols[i] = matrix.viewColumn(i);
+ }
+
+ // setup distance function
+ org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
+ DoubleDoubleFunction function = null;
+ //DoubleDoubleFunction function2 = null;
+ if (norm==EUCLID) function = F.chain(F.square,F.minus);
+ else if (norm==BRAY_CURTIS) function = F.chain(F.abs,F.minus);
+ else if (norm==CANBERRA) function = new DoubleDoubleFunction() {
+ public final double apply(double a, double b) { return Math.abs(a-b) / Math.abs(a+b);}
+ };
+ else if (norm==MAXIMUM) function = F.chain(F.abs,F.minus);
+ else if (norm==MANHATTAN) function = F.chain(F.abs,F.minus);
+ else throw new IllegalArgumentException("Unknown norm");
+
+ // work out all permutations
+ for (int i=columns; --i >= 0; ) {
+ for (int j=i; --j >= 0; ) {
+ double d = 0;
+ if (norm==EUCLID) d = Math.sqrt(cols[i].aggregate(cols[j], F.plus, function));
+ else if (norm==BRAY_CURTIS) d = cols[i].aggregate(cols[j], F.plus, function) / cols[i].aggregate(cols[j], F.plus, F.plus);
+ else if (norm==CANBERRA) d = cols[i].aggregate(cols[j], F.plus, function);
+ else if (norm==MAXIMUM) d = cols[i].aggregate(cols[j], F.max, function);
+ else if (norm==MANHATTAN) d = cols[i].aggregate(cols[j], F.plus, function);
+ distance.setQuick(i,j,d);
+ distance.setQuick(j,i,d); // symmetric
+ }
+ }
+ return distance;
+ */
+ return null;
}
/**
* Constructs and returns the distance matrix of the given matrix.
@@ -813,44 +813,44 @@
* @return the distance matrix (<tt>n x n, n=matrix.columns</tt>).
*/
private static DoubleMatrix2D xdistanceOld2(DoubleMatrix2D matrix, int norm) {
- /*
- // setup distance function
- final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
- VectorVectorFunction function;
- if (norm==EUCLID) function = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return Math.sqrt(a.aggregate(b, F.plus, F.chain(F.square,F.minus)));
- }
- };
- else if (norm==BRAY_CURTIS) function = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.plus, F.chain(F.abs,F.minus)) / a.aggregate(b, F.plus, F.plus);
- }
- };
- else if (norm==CANBERRA) function = new VectorVectorFunction() {
- DoubleDoubleFunction fun = new DoubleDoubleFunction() {
- public final double apply(double a, double b) {
- return Math.abs(a-b) / Math.abs(a+b);
- }
- };
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.plus, fun);
- }
- };
- else if (norm==MAXIMUM) function = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.max, F.chain(F.abs,F.minus));
- }
- };
- else if (norm==MANHATTAN) function = new VectorVectorFunction() {
- public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
- return a.aggregate(b, F.plus, F.chain(F.abs,F.minus));
- }
- };
- else throw new IllegalArgumentException("Unknown norm");
-
- return distance(matrix,function);
- */
- return null;
+ /*
+ // setup distance function
+ final org.apache.mahout.jet.math.Functions F = org.apache.mahout.jet.math.Functions.functions;
+ VectorVectorFunction function;
+ if (norm==EUCLID) function = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return Math.sqrt(a.aggregate(b, F.plus, F.chain(F.square,F.minus)));
+ }
+ };
+ else if (norm==BRAY_CURTIS) function = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.plus, F.chain(F.abs,F.minus)) / a.aggregate(b, F.plus, F.plus);
+ }
+ };
+ else if (norm==CANBERRA) function = new VectorVectorFunction() {
+ DoubleDoubleFunction fun = new DoubleDoubleFunction() {
+ public final double apply(double a, double b) {
+ return Math.abs(a-b) / Math.abs(a+b);
+ }
+ };
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.plus, fun);
+ }
+ };
+ else if (norm==MAXIMUM) function = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.max, F.chain(F.abs,F.minus));
+ }
+ };
+ else if (norm==MANHATTAN) function = new VectorVectorFunction() {
+ public final double apply(DoubleMatrix1D a, DoubleMatrix1D b) {
+ return a.aggregate(b, F.plus, F.chain(F.abs,F.minus));
+ }
+ };
+ else throw new IllegalArgumentException("Unknown norm");
+
+ return distance(matrix,function);
+ */
+ return null;
}
}
Modified: lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Stencil.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Stencil.java?rev=883974&r1=883973&r2=883974&view=diff
==============================================================================
--- lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Stencil.java (original)
+++ lucene/mahout/trunk/matrix/src/main/java/org/apache/mahout/matrix/matrix/doublealgo/Stencil.java Wed Nov 25 03:41:28 2009
@@ -38,28 +38,28 @@
@param A the matrix to operate on.
@param function the function to be applied to each window.
@param maxIterations the maximum number of times the stencil shall be applied to the matrix.
- Should be a multiple of 2 because two iterations are always done in one atomic step.
+ Should be a multiple of 2 because two iterations are always done in one atomic step.
@param hasConverged Convergence condition; will return before maxIterations are done when <tt>hasConverged.apply(A)==true</tt>.
- Set this parameter to <tt>null</tt> to indicate that no convergence checks shall be made.
+ Set this parameter to <tt>null</tt> to indicate that no convergence checks shall be made.
@param convergenceIterations the number of iterations to pass between each convergence check.
- (Since a convergence may be expensive, you may want to do it only every 2,4 or 8 iterations.)
+ (Since a convergence may be expensive, you may want to do it only every 2,4 or 8 iterations.)
@return the number of iterations actually executed.
*/
public static int stencil27(DoubleMatrix3D A, org.apache.mahout.matrix.function.Double27Function function, int maxIterations, DoubleMatrix3DProcedure hasConverged, int convergenceIterations) {
- DoubleMatrix3D B = A.copy();
- if (convergenceIterations <= 1) convergenceIterations=2;
- if (convergenceIterations%2 != 0) convergenceIterations++; // odd -> make it even
+ DoubleMatrix3D B = A.copy();
+ if (convergenceIterations <= 1) convergenceIterations=2;
+ if (convergenceIterations%2 != 0) convergenceIterations++; // odd -> make it even
- int i=0;
- while (i<maxIterations) { // do two steps at a time for efficiency
- A.zAssign27Neighbors(B,function);
- B.zAssign27Neighbors(A,function);
- i=i+2;
- if (i%convergenceIterations == 0 && hasConverged!=null) {
- if (hasConverged.apply(A)) return i;
- }
- }
- return i;
+ int i=0;
+ while (i<maxIterations) { // do two steps at a time for efficiency
+ A.zAssign27Neighbors(B,function);
+ B.zAssign27Neighbors(A,function);
+ i=i+2;
+ if (i%convergenceIterations == 0 && hasConverged!=null) {
+ if (hasConverged.apply(A)) return i;
+ }
+ }
+ return i;
}
/**
9 point stencil operation.
@@ -67,27 +67,27 @@
@param A the matrix to operate on.
@param function the function to be applied to each window.
@param maxIterations the maximum number of times the stencil shall be applied to the matrix.
- Should be a multiple of 2 because two iterations are always done in one atomic step.
+ Should be a multiple of 2 because two iterations are always done in one atomic step.
@param hasConverged Convergence condition; will return before maxIterations are done when <tt>hasConverged.apply(A)==true</tt>.
- Set this parameter to <tt>null</tt> to indicate that no convergence checks shall be made.
+ Set this parameter to <tt>null</tt> to indicate that no convergence checks shall be made.
@param convergenceIterations the number of iterations to pass between each convergence check.
- (Since a convergence may be expensive, you may want to do it only every 2,4 or 8 iterations.)
+ (Since a convergence may be expensive, you may want to do it only every 2,4 or 8 iterations.)
@return the number of iterations actually executed.
*/
public static int stencil9(DoubleMatrix2D A, org.apache.mahout.matrix.function.Double9Function function, int maxIterations, DoubleMatrix2DProcedure hasConverged, int convergenceIterations) {
- DoubleMatrix2D B = A.copy();
- if (convergenceIterations <= 1) convergenceIterations=2;
- if (convergenceIterations%2 != 0) convergenceIterations++; // odd -> make it even
+ DoubleMatrix2D B = A.copy();
+ if (convergenceIterations <= 1) convergenceIterations=2;
+ if (convergenceIterations%2 != 0) convergenceIterations++; // odd -> make it even
- int i=0;
- while (i<maxIterations) { // do two steps at a time for efficiency
- A.zAssign8Neighbors(B,function);
- B.zAssign8Neighbors(A,function);
- i=i+2;
- if (i%convergenceIterations == 0 && hasConverged!=null) {
- if (hasConverged.apply(A)) return i;
- }
- }
- return i;
+ int i=0;
+ while (i<maxIterations) { // do two steps at a time for efficiency
+ A.zAssign8Neighbors(B,function);
+ B.zAssign8Neighbors(A,function);
+ i=i+2;
+ if (i%convergenceIterations == 0 && hasConverged!=null) {
+ if (hasConverged.apply(A)) return i;
+ }
+ }
+ return i;
}
}