File: function4.html

package info (click to toggle)
libcolt-free-java 1.2.0%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,836 kB
  • sloc: java: 30,337; xml: 893; makefile: 26; sh: 3
file content (63 lines) | stat: -rw-r--r-- 3,391 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<HTML><title>Function Objects</title>
<BODY>
<h1>Example 4: Sorting by user specified order </h1>
<p>Assume, we would like to sort the rows of a 2d matrix by the the last column (representing "age"). This can be done with
<pre>
// sort by last column
sorted = matrix.viewSorted(matrix.columns()-1);
</pre>
<p>Or assume, we would like to sort the columns of a 2d matrix by the the last row. 
Unfortunately, there is no convenience method to directly sort by row. So we need to view columns as rows and rows as columns, then sort, then adjust our view again.
<pre>
// sort by last row
int lastRow = matrix.rows()-1;
sorted = matrix.viewDice().viewSorted(lastRow).viewDice();
</pre>
<p>Next, we would like to sort the rows of a 2d matrix by the aggregate sum 
  of values in a row. A <i>comparator</i> object is used to do the job: 
<pre>// sort by sum of values in a row
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;
	}
};
sorted = cern.colt.matrix.doublealgo.Sorting.quickSort(matrix,comp);
</pre>
<p>Further, we would like to sort the rows of a 2d matrix by the aggregate sum of 
  logarithms in a row (which is a way to achieve sorting by <i>geometric mean</i> 
  when viewing a row as a series of samples). A slightly more complex comparator 
  object is needed: 
<pre>// sort by sum of logarithms in a row
DoubleMatrix1DComparator comp = new DoubleMatrix1DComparator() {
	public int compare(DoubleMatrix1D a, DoubleMatrix1D b) {
		double as = a.aggregate(cern.jet.math.Functions.plus,cern.jet.math.Functions.log); <br>		double bs = b.aggregate(cern.jet.math.Functions.plus,cern.jet.math.Functions.log);
		return as < bs ? -1 : as == bs ? 0 : 1;
	}
};
sorted = cern.colt.matrix.doublealgo.Sorting.quickSort(matrix,comp);
</pre>
This is certainly not most efficient since row sums are recomputed many times 
(<tt>2*rows*log(rows)</tt> times, on average), but will suffice as an example. 
An efficient app will precompute the sums and use <tt>cern.colt.GenericSorting</tt> 
to sort the matrix. In general, if comparisons are expensive, precomputation boots 
performance by a factor <tt>2*log(rows)</tt>. 
<p> <i><img src="../../doc-files/new.gif" width="32" height="22" align="bottom"></i>Recently, 
  two methods that do exactly that were added to <a href="../doublealgo/Sorting.html">cern.colt.matrix.doublealgo.Sorting</a>. 
  One of them works by filling a row into a so-called "bin", which is a multi-set 
  with statistics operations defined upon. Aggregate measures over the row are 
  then computed via a <a href="../../../jet/histo/BinFunction1D.html">BinFunction1D</a>. 
  Some prefabricated functions are contained in <a href="../../../jet/histo/BinFunctions1D.html">BinFunctions1D</a> 
  Here is how to solve the problem efficiently: 
<pre>
// sort by sum of logarithms in a row
sorted = cern.colt.matrix.doublealgo.Sorting.quickSort(matrix,hep.aida.bin.BinFunctions1D.sumLog);

// sort by median in a row
sorted = cern.colt.matrix.doublealgo.Sorting.quickSort(matrix,hep.aida.bin.BinFunctions1D.median);

// sort by maximum in a row
sorted = cern.colt.matrix.doublealgo.Sorting.quickSort(matrix,hep.aida.bin.BinFunctions1D.max);
</pre>
</BODY>
</HTML>