use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Sorting method zdemo7.
/**
* 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 cern.jet.math.Functions F = cern.jet.math.Functions.functions;
DoubleMatrix2D A = cern.colt.matrix.DoubleFactory2D.dense.make(rows, columns);
// initialize randomly
A.assign(new cern.jet.random.engine.DRand());
DoubleMatrix2D B = A.copy();
double[] v1 = A.viewColumn(0).toArray();
double[] v2 = A.viewColumn(0).toArray();
System.out.print("now quick sorting... ");
cern.colt.Timer timer = new cern.colt.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();
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Sorting method sort.
/**
*Sorts the matrix slices according to the order induced by the specified comparator.
*The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
*The algorithm compares two slices (2-d matrices) at a time, determinining whether one is smaller, equal or larger than the other.
*To sort ranges use sub-ranging views. To sort by other dimensions, use dice views. To sort descending, use flip views ...
*<p>
*<b>Example:</b>
*<pre>
*// sort by sum of values in a slice
*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;
* }
*};
*sorted = quickSort(matrix,comp);
*</pre>
*
*@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>
*/
public DoubleMatrix3D sort(final DoubleMatrix3D matrix, final DoubleMatrix2DComparator c) {
// indexes to reorder instead of matrix itself
int[] sliceIndexes = new int[matrix.slices()];
for (int i = sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
// precompute views for speed
final DoubleMatrix2D[] views = new DoubleMatrix2D[matrix.slices()];
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);
// take all rows and columns in the original order
return matrix.viewSelection(sliceIndexes, null, null);
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Sorting method sort.
/**
*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;
*Particularly efficient when comparing expensive aggregates, because aggregates need not be recomputed time and again, as is the case for comparator based sorts.
*Essentially, this algorithm makes expensive comparisons cheap.
*Normally <tt>aggregates</tt> defines a summary measure of a row.
*Speedup over comparator based sorting = <tt>2*log(rows)</tt>, on average.
*<p>
*The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
*To sort ranges use sub-ranging views. To sort columns by rows, use dice views. To sort descending, use flip views ...
*<p>
*<b>Example:</b>
*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>
* </tr>
*</table>
*
*<table>
*<td class="PRE">
*<pre>
*// sort 10000 x 1000 matrix by median or by sum of logarithms in a row (i.e. by geometric mean)
*DoubleMatrix2D matrix = new DenseDoubleMatrix2D(10000,1000);
*matrix.assign(new cern.jet.random.engine.MersenneTwister()); // initialized randomly
*cern.jet.math.Functions F = cern.jet.math.Functions.functions; // alias for convenience
*
*// THE QUICK VERSION (takes some 10 secs)
*DoubleMatrix2D sorted = quickSort(matrix,hep.aida.bin.BinFunctions1D.median);
*//DoubleMatrix2D sorted = quickSort(matrix,hep.aida.bin.BinFunctions1D.sumOfLogarithms);
*
*// THE SLOW VERSION (takes some 300 secs)
*DoubleMatrix1DComparator comparator = new DoubleMatrix1DComparator() {
* public int compare(DoubleMatrix1D x, DoubleMatrix1D y) {
* double a = cern.colt.matrix.doublealgo.Statistic.bin(x).median();
* double b = cern.colt.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;
* }
*};
*DoubleMatrix2D sorted = quickSort(matrix,comparator);
*</pre>
*</td>
*</table>
*
*@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>
*/
public DoubleMatrix2D sort(DoubleMatrix2D matrix, hep.aida.bin.BinFunction1D 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);
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Statistic method demo3.
/**
* Demonstrates usage of this class.
*/
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 } };
System.out.println("\n\ninitializing...");
DoubleFactory2D factory = DoubleFactory2D.dense;
DoubleMatrix2D A = factory.make(values).viewDice();
System.out.println("\nA=" + A.viewDice());
System.out.println("\ndist=" + distance(A, norm).viewDice());
}
use of cern.colt.matrix.DoubleMatrix2D in project tdq-studio-se by Talend.
the class Stencil method stencil9.
/**
*9 point stencil operation.
*Applies a function to a moving <tt>3 x 3</tt> window.
*@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.
*@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.
*@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.)
*@return the number of iterations actually executed.
*/
public static int stencil9(DoubleMatrix2D A, cern.colt.function.Double9Function function, int maxIterations, DoubleMatrix2DProcedure hasConverged, int convergenceIterations) {
DoubleMatrix2D B = A.copy();
if (convergenceIterations <= 1)
convergenceIterations = 2;
// odd -> make it even
if (convergenceIterations % 2 != 0)
convergenceIterations++;
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;
}
Aggregations