Source code: jmat/data/arrayTools/Find.java
1 package jmat.data.arrayTools;
2
3
4 /** Find a value or a verified condition in a 2D-Array of double.
5 */
6 public class Find
7 {
8 //~ Instance fields ////////////////////////////////////////////////////////
9
10 /** String of the test.
11 */
12 private String test;
13
14 /* ------------------------
15 Class variables
16 * ------------------------ */
17
18 /** 2D-Array to test.
19 */
20 private double[][] A;
21
22 /** Indices of elements found.
23 */
24 private int[][] indices;
25
26 /** Value to find or to compare.
27 */
28 private double value;
29
30 //~ Constructors ///////////////////////////////////////////////////////////
31
32 /* ------------------------
33 Constructors
34 * ------------------------ */
35
36 /** Find a value.
37 @param a Double array to test.
38 @param v Value to compare.
39 */
40 public Find(double[][] a, double v)
41 {
42 A = a;
43 value = v;
44 test = "==";
45 indices = find(A, test, value);
46 }
47
48 /** Find elements verifying a test.
49 @param a Double array to test.
50 @param t Test : "==", "<=", ">=", "<", ">", "!=".
51 @param v Value to compare.
52 */
53 public Find(double[][] a, String t, double v)
54 {
55 A = a;
56 value = v;
57 test = t;
58 indices = find(A, test, value);
59 }
60
61 //~ Methods ////////////////////////////////////////////////////////////////
62
63 /** Get the boolean array of the test (true / false).
64 @return 2D-boolean array.
65 */
66 public boolean[][] getBooleanArray()
67 {
68 boolean[][] b = setBooleanArray(indices, A);
69
70 return b;
71 }
72
73 /** Get the double array of the test (1D / 0D).
74 @return 2D-double array of 1 or 0.
75 */
76 public double[][] getDoubleArray()
77 {
78 double[][] d = setDoubleArray(indices, A);
79
80 return d;
81 }
82
83 /* ------------------------
84 Public Methods
85 * ------------------------ */
86
87 /** Get the indices verifying the test.
88 @return 2D-indices.
89 */
90 public int[][] getIndices()
91 {
92 return indices;
93 }
94
95 /* ------------------------
96 Private Methods
97 * ------------------------ */
98
99 /** Set the boolean array.
100 @param ind Indices verifying the test.
101 @param a Array to test
102 @return Boolean array.
103 */
104 private boolean[][] setBooleanArray(int[][] ind, double[][] a)
105 {
106 boolean[][] b = new boolean[a.length][a[0].length];
107
108 for (int i = 0; i < a.length; i++)
109 {
110 for (int j = 0; j < a[0].length; j++)
111 {
112 b[i][j] = false;
113 }
114 }
115
116 for (int i = 0; i < ind.length; i++)
117 {
118 b[ind[i][0]][ind[i][1]] = true;
119 }
120
121 return b;
122 }
123
124 /** Set the double array.
125 @param ind Indices verifying the test.
126 @param a Array to test.
127 @return Double array.
128 */
129 private double[][] setDoubleArray(int[][] ind, double[][] a)
130 {
131 double[][] d = new double[a.length][a[0].length];
132
133 for (int i = 0; i < a.length; i++)
134 {
135 for (int j = 0; j < a[0].length; j++)
136 {
137 d[i][j] = 0;
138 }
139 }
140
141 for (int i = 0; i < ind.length; i++)
142 {
143 d[ind[i][0]][ind[i][1]] = 1;
144 }
145
146 return d;
147 }
148
149 /** Find elements verifying the test.
150 @param a Double array to test.
151 @param t String of the test.
152 @param v Value to test
153 @return Double array.
154 */
155 private int[][] find(double[][] a, String t, double v)
156 {
157 if (t.equals("=="))
158 {
159 return findEqual(a, v);
160 }
161 else if (t.equals("<="))
162 {
163 return findInfEqual(a, v);
164 }
165 else if (t.equals(">="))
166 {
167 return findSupEqual(a, v);
168 }
169 else if (t.equals("<"))
170 {
171 return findInf(a, v);
172 }
173 else if (t.equals(">"))
174 {
175 return findSup(a, v);
176 }
177 else if (t.equals("!="))
178 {
179 return findDiff(a, v);
180 }
181 else
182 {
183 throw new IllegalArgumentException("Test String " + t +
184 " is unknown.");
185 }
186 }
187
188 private int[][] findDiff(double[][] a, double v)
189 {
190 int[][] ind = new int[0][2];
191
192 for (int i = 0; i < a.length; i++)
193 {
194 for (int j = 0; j < a[0].length; j++)
195 {
196 if (a[i][j] != v)
197 {
198 ind = put(ind, i, j);
199 }
200 }
201 }
202
203 return ind;
204 }
205
206 private int[][] findEqual(double[][] a, double v)
207 {
208 int[][] ind = new int[0][2];
209
210 for (int i = 0; i < a.length; i++)
211 {
212 for (int j = 0; j < a[0].length; j++)
213 {
214 if (a[i][j] == v)
215 {
216 ind = put(ind, i, j);
217 }
218 }
219 }
220
221 return ind;
222 }
223
224 private int[][] findInf(double[][] a, double v)
225 {
226 int[][] ind = new int[0][2];
227
228 for (int i = 0; i < a.length; i++)
229 {
230 for (int j = 0; j < a[0].length; j++)
231 {
232 if (a[i][j] < v)
233 {
234 ind = put(ind, i, j);
235 }
236 }
237 }
238
239 return ind;
240 }
241
242 private int[][] findInfEqual(double[][] a, double v)
243 {
244 int[][] ind = new int[0][2];
245
246 for (int i = 0; i < a.length; i++)
247 {
248 for (int j = 0; j < a[0].length; j++)
249 {
250 if (a[i][j] <= v)
251 {
252 ind = put(ind, i, j);
253 }
254 }
255 }
256
257 return ind;
258 }
259
260 private int[][] findSup(double[][] a, double v)
261 {
262 int[][] ind = new int[0][2];
263
264 for (int i = 0; i < a.length; i++)
265 {
266 for (int j = 0; j < a[0].length; j++)
267 {
268 if (a[i][j] > v)
269 {
270 ind = put(ind, i, j);
271 }
272 }
273 }
274
275 return ind;
276 }
277
278 private int[][] findSupEqual(double[][] a, double v)
279 {
280 int[][] ind = new int[0][2];
281
282 for (int i = 0; i < a.length; i++)
283 {
284 for (int j = 0; j < a[0].length; j++)
285 {
286 if (a[i][j] >= v)
287 {
288 ind = put(ind, i, j);
289 }
290 }
291 }
292
293 return ind;
294 }
295
296 private int[][] put(int[][] ind, int i0, int j0)
297 {
298 int[][] new_ind = new int[ind.length + 1][2];
299
300 for (int i = 0; i < ind.length; i++)
301 {
302 for (int j = 0; j < 2; j++)
303 {
304 new_ind[i][j] = ind[i][j];
305 }
306 }
307
308 new_ind[ind.length][0] = i0;
309 new_ind[ind.length][1] = j0;
310
311 return new_ind;
312 }
313 }
314 ///////////////////////////////////////////////////////////////////////////////
315 // END OF FILE.
316 ///////////////////////////////////////////////////////////////////////////////