File: function3.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 (76 lines) | stat: -rw-r--r-- 2,407 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
64
65
66
67
68
69
70
71
72
73
74
75
76
<HTML><title>Function Objects</title>
<BODY>
<h2><a name="Selection"></a>Example 3: Selection views based on conditions </h2>
<p>Using condition functions (predicates), we can filter away uninteresting data 
  and keep only interesting data. In physics codes, this process is often called 
  <i>cutting on a predicate</i>. 
<hr>
<h2>Conditions on 1-d matrices (vectors) </h2>
<pre>
// the naming shortcut (alias) saves some keystrokes:
cern.jet.math.Functions F = cern.jet.math.Functions.functions;

double[] v1 = {0, 1, 2, 3};
DoubleMatrix1D matrix = new DenseDoubleMatrix1D(v1);
// 0 1 2 3 

// view all cells for which holds: lower <= value <= upper
final double lower = 0.2;
final double upper = 2.5
matrix.viewSelection(F.isBetween(lower, upper)); 
// --> 1 2

// equivalent, but less concise:
matrix.viewSelection( 
	new DoubleProcedure() {
		public final boolean apply(double a) { return lower &lt;= a &amp;&amp; a &lt;= upper; }
	}
);
// --> 1 2
</pre>
<pre></pre>
<pre>// view all cells with even value
matrix.viewSelection( 
	new DoubleProcedure() {
		public final boolean apply(double a) { return a % 2 == 0; }
	}
);
// --> 0 2

// sum of all cells for which holds: lower <= value <= upper
double sum = matrix.viewSelection(F.isBetween(lower, upper)).zSum(); 
// --> 3
<br>// equivalent: 
double sum = matrix.viewSelection(F.isBetween(lower, upper)).aggregate(F.plus,F.identity); 
</pre>
<hr>
<h2>Conditions on 2-d matrices </h2>
<pre>
// view all rows which have a value < threshold in the first column (representing "age")
final double threshold = 16;
matrix.viewSelection( 
	new DoubleMatrix1DProcedure() {
		public final boolean apply(DoubleMatrix1D m) { return m.get(0) < threshold; }
	}
);

// view all rows with RMS < threshold.<br>// the RMS (Root-Mean-Square) is a measure of the average "size" of the elements of a data sequence.
final double threshold = 0.5;
matrix.viewSelection( 
	new DoubleMatrix1DProcedure() {
		public final boolean apply(DoubleMatrix1D m) { return Math.sqrt(m.aggregate(F.plus,F.square) / m.size()) < threshold; }
	}
);
</pre>
<hr>
<h2>Conditions on 3-d matrices</h2>
<pre>
// view all slices which have an aggregate sum > 1000
matrix.viewSelection( 
	new DoubleMatrix2DProcedure() {
		public final boolean apply(DoubleMatrix2D m) { return m.zSum() > 1000; }
	}
);
</pre>
</BODY>
</HTML>