1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.math; |
22 |
|
|
23 |
|
import static org.testng.Assert.assertEquals; |
24 |
|
import static org.testng.Assert.assertFalse; |
25 |
|
import static org.testng.Assert.assertTrue; |
26 |
|
import static org.testng.Assert.fail; |
27 |
|
|
28 |
|
import java.util.Random; |
29 |
|
|
30 |
|
import org.testng.annotations.Test; |
31 |
|
import org.testng.internal.junit.ArrayAsserts; |
32 |
|
|
|
|
| 86.9% |
Uncovered Elements: 27 (206) |
Complexity: 32 |
Complexity Density: 0.2 |
|
33 |
|
public class SparseMatrixTest |
34 |
|
{ |
35 |
|
final static double DELTA = 0.0001d; |
36 |
|
|
37 |
|
Random r = new Random(1729); |
38 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
39 |
1 |
@Test(groups = "Functional")... |
40 |
|
public void testConstructor() |
41 |
|
{ |
42 |
1 |
MatrixI m1 = new SparseMatrix( |
43 |
|
new double[][] |
44 |
|
{ { 2, 0, 4 }, { 0, 6, 0 } }); |
45 |
1 |
assertEquals(m1.getValue(0, 0), 2d); |
46 |
1 |
assertEquals(m1.getValue(0, 1), 0d); |
47 |
1 |
assertEquals(m1.getValue(0, 2), 4d); |
48 |
1 |
assertEquals(m1.getValue(1, 0), 0d); |
49 |
1 |
assertEquals(m1.getValue(1, 1), 6d); |
50 |
1 |
assertEquals(m1.getValue(1, 2), 0d); |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
53 |
1 |
@Test(groups = "Functional")... |
54 |
|
public void testTranspose() |
55 |
|
{ |
56 |
1 |
MatrixI m1 = new SparseMatrix( |
57 |
|
new double[][] |
58 |
|
{ { 2, 0, 4 }, { 5, 6, 0 } }); |
59 |
1 |
MatrixI m2 = m1.transpose(); |
60 |
1 |
assertTrue(m2 instanceof SparseMatrix); |
61 |
1 |
assertEquals(m2.height(), 3); |
62 |
1 |
assertEquals(m2.width(), 2); |
63 |
1 |
assertEquals(m2.getValue(0, 0), 2d); |
64 |
1 |
assertEquals(m2.getValue(0, 1), 5d); |
65 |
1 |
assertEquals(m2.getValue(1, 0), 0d); |
66 |
1 |
assertEquals(m2.getValue(1, 1), 6d); |
67 |
1 |
assertEquals(m2.getValue(2, 0), 4d); |
68 |
1 |
assertEquals(m2.getValue(2, 1), 0d); |
69 |
|
} |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
71 |
1 |
@Test(groups = "Functional")... |
72 |
|
public void testPreMultiply() |
73 |
|
{ |
74 |
1 |
MatrixI m1 = new SparseMatrix(new double[][] { { 2, 3, 4 } }); |
75 |
1 |
MatrixI m2 = new SparseMatrix(new double[][] { { 5 }, { 6 }, { 7 } }); |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
1 |
MatrixI m3 = m2.preMultiply(m1); |
82 |
1 |
assertFalse(m3 instanceof SparseMatrix); |
83 |
1 |
assertEquals(m3.height(), 1); |
84 |
1 |
assertEquals(m3.width(), 1); |
85 |
1 |
assertEquals(m3.getValue(0, 0), 56d); |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
1 |
m3 = m1.preMultiply(m2); |
91 |
1 |
assertEquals(m3.height(), 3); |
92 |
1 |
assertEquals(m3.width(), 3); |
93 |
1 |
assertEquals(m3.getValue(0, 0), 10d); |
94 |
1 |
assertEquals(m3.getValue(0, 1), 15d); |
95 |
1 |
assertEquals(m3.getValue(0, 2), 20d); |
96 |
1 |
assertEquals(m3.getValue(1, 0), 12d); |
97 |
1 |
assertEquals(m3.getValue(1, 1), 18d); |
98 |
1 |
assertEquals(m3.getValue(1, 2), 24d); |
99 |
1 |
assertEquals(m3.getValue(2, 0), 14d); |
100 |
1 |
assertEquals(m3.getValue(2, 1), 21d); |
101 |
1 |
assertEquals(m3.getValue(2, 2), 28d); |
102 |
|
} |
103 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
104 |
1 |
@Test(... |
105 |
|
groups = "Functional", |
106 |
|
expectedExceptions = |
107 |
|
{ IllegalArgumentException.class }) |
108 |
|
public void testPreMultiply_tooManyColumns() |
109 |
|
{ |
110 |
1 |
Matrix m1 = new SparseMatrix( |
111 |
|
new double[][] |
112 |
|
{ { 2, 3, 4 }, { 3, 4, 5 } }); |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
1 |
m1.preMultiply(m1); |
119 |
0 |
fail("Expected exception"); |
120 |
|
} |
121 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
122 |
1 |
@Test(... |
123 |
|
groups = "Functional", |
124 |
|
expectedExceptions = |
125 |
|
{ IllegalArgumentException.class }) |
126 |
|
public void testPreMultiply_tooFewColumns() |
127 |
|
{ |
128 |
1 |
Matrix m1 = new SparseMatrix( |
129 |
|
new double[][] |
130 |
|
{ { 2, 3, 4 }, { 3, 4, 5 } }); |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
1 |
m1.preMultiply(m1); |
137 |
0 |
fail("Expected exception"); |
138 |
|
} |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
140 |
1 |
@Test(groups = "Functional")... |
141 |
|
public void testPostMultiply() |
142 |
|
{ |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
1 |
MatrixI m1 = new SparseMatrix(new double[][] { { 2, 3 }, { 4, 5 } }); |
152 |
1 |
MatrixI m2 = new SparseMatrix( |
153 |
|
new double[][] |
154 |
|
{ { 10, 100 }, { 1000, 10000 } }); |
155 |
1 |
MatrixI m3 = m1.postMultiply(m2); |
156 |
1 |
assertEquals(m3.getValue(0, 0), 3020d); |
157 |
1 |
assertEquals(m3.getValue(0, 1), 30200d); |
158 |
1 |
assertEquals(m3.getValue(1, 0), 5040d); |
159 |
1 |
assertEquals(m3.getValue(1, 1), 50400d); |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
1 |
MatrixI m4 = m2.preMultiply(m1); |
165 |
1 |
assertMatricesMatch(m3, m4, 0.00001d); |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
1 |
m1 = new SparseMatrix(new double[][] { { 2 }, { 3 } }); |
173 |
1 |
m2 = new SparseMatrix(new double[][] { { 10, 100, 1000 } }); |
174 |
1 |
m3 = m1.postMultiply(m2); |
175 |
1 |
assertEquals(m3.height(), 2); |
176 |
1 |
assertEquals(m3.width(), 3); |
177 |
1 |
assertEquals(m3.getValue(0, 0), 20d); |
178 |
1 |
assertEquals(m3.getValue(0, 1), 200d); |
179 |
1 |
assertEquals(m3.getValue(0, 2), 2000d); |
180 |
1 |
assertEquals(m3.getValue(1, 0), 30d); |
181 |
1 |
assertEquals(m3.getValue(1, 1), 300d); |
182 |
1 |
assertEquals(m3.getValue(1, 2), 3000d); |
183 |
|
|
184 |
1 |
m4 = m2.preMultiply(m1); |
185 |
1 |
assertMatricesMatch(m3, m4, 0.00001d); |
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
1 |
m1 = new SparseMatrix(new double[][] { { 2, 3, 4 } }); |
196 |
1 |
m2 = new SparseMatrix(new double[][] { { 5, 4 }, { 6, 3 }, { 7, 2 } }); |
197 |
1 |
m3 = m1.postMultiply(m2); |
198 |
1 |
assertEquals(m3.height(), 1); |
199 |
1 |
assertEquals(m3.width(), 2); |
200 |
1 |
assertEquals(m3.getValue(0, 0), 56d); |
201 |
1 |
assertEquals(m3.getValue(0, 1), 25d); |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
1 |
m4 = m2.preMultiply(m1); |
207 |
1 |
assertMatricesMatch(m3, m4, 0.00001d); |
208 |
|
} |
209 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
210 |
0 |
@Test(groups = "Timing")... |
211 |
|
public void testSign() |
212 |
|
{ |
213 |
0 |
assertEquals(Matrix.sign(-1, -2), -1d); |
214 |
0 |
assertEquals(Matrix.sign(-1, 2), 1d); |
215 |
0 |
assertEquals(Matrix.sign(-1, 0), 1d); |
216 |
0 |
assertEquals(Matrix.sign(1, -2), -1d); |
217 |
0 |
assertEquals(Matrix.sign(1, 2), 1d); |
218 |
0 |
assertEquals(Matrix.sign(1, 0), 1d); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
1PASS
|
|
226 |
1 |
@Test(groups = "Functional")... |
227 |
|
public void testTred_matchesMatrix() |
228 |
|
{ |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
1 |
int rows = 10; |
233 |
1 |
int cols = rows; |
234 |
1 |
double[][] d = getSparseValues(rows, cols, 3); |
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
1 |
double[][] d1 = new double[rows][cols]; |
241 |
11 |
for (int row = 0; row < rows; row++) |
242 |
|
{ |
243 |
110 |
for (int col = 0; col < cols; col++) |
244 |
|
{ |
245 |
100 |
d1[row][col] = d[row][col]; |
246 |
|
} |
247 |
|
} |
248 |
1 |
Matrix m1 = new Matrix(d); |
249 |
1 |
Matrix m2 = new SparseMatrix(d1); |
250 |
1 |
assertMatricesMatch(m1, m2, 0.00001d); |
251 |
1 |
m1.tred(); |
252 |
1 |
m2.tred(); |
253 |
1 |
assertMatricesMatch(m1, m2, 0.00001d); |
254 |
|
} |
255 |
|
|
|
|
| 72.7% |
Uncovered Elements: 6 (22) |
Complexity: 6 |
Complexity Density: 0.5 |
|
256 |
9 |
private void assertMatricesMatch(MatrixI m1, MatrixI m2, double delta)... |
257 |
|
{ |
258 |
9 |
if (m1.height() != m2.height()) |
259 |
|
{ |
260 |
0 |
fail("height mismatch"); |
261 |
|
} |
262 |
9 |
if (m1.width() != m2.width()) |
263 |
|
{ |
264 |
0 |
fail("width mismatch"); |
265 |
|
} |
266 |
66 |
for (int row = 0; row < m1.height(); row++) |
267 |
|
{ |
268 |
541 |
for (int col = 0; col < m1.width(); col++) |
269 |
|
{ |
270 |
484 |
double v2 = m2.getValue(row, col); |
271 |
484 |
double v1 = m1.getValue(row, col); |
272 |
484 |
if (Math.abs(v1 - v2) > DELTA) |
273 |
|
{ |
274 |
0 |
fail(String.format("At [%d, %d] %f != %f", row, col, v1, v2)); |
275 |
|
} |
276 |
|
} |
277 |
|
} |
278 |
9 |
ArrayAsserts.assertArrayEquals(m1.getD(), m2.getD(), delta); |
279 |
9 |
ArrayAsserts.assertArrayEquals(m1.getE(), m2.getE(), 0.00001d); |
280 |
|
} |
281 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
1PASS
|
|
282 |
0 |
@Test... |
283 |
|
public void testGetValue() |
284 |
|
{ |
285 |
0 |
double[][] d = new double[][] { { 0, 0, 1, 0, 0 }, { 2, 3, 0, 0, 0 }, |
286 |
|
{ 4, 0, 0, 0, 5 } }; |
287 |
0 |
MatrixI m = new SparseMatrix(d); |
288 |
0 |
for (int row = 0; row < 3; row++) |
289 |
|
{ |
290 |
0 |
for (int col = 0; col < 5; col++) |
291 |
|
{ |
292 |
0 |
assertEquals(m.getValue(row, col), d[row][col], |
293 |
|
String.format("At [%d, %d]", row, col)); |
294 |
|
} |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
@throws |
304 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
1PASS
|
|
305 |
1 |
@Test(groups = "Functional")... |
306 |
|
public void testTqli_matchesMatrix() throws Exception |
307 |
|
{ |
308 |
|
|
309 |
|
|
310 |
|
|
311 |
1 |
int rows = 6; |
312 |
1 |
int cols = rows; |
313 |
1 |
double[][] d = getSparseValues(rows, cols, 3); |
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
1 |
double[][] d1 = new double[rows][cols]; |
320 |
7 |
for (int row = 0; row < rows; row++) |
321 |
|
{ |
322 |
42 |
for (int col = 0; col < cols; col++) |
323 |
|
{ |
324 |
36 |
d1[row][col] = d[row][col]; |
325 |
|
} |
326 |
|
} |
327 |
1 |
Matrix m1 = new Matrix(d); |
328 |
1 |
Matrix m2 = new SparseMatrix(d1); |
329 |
|
|
330 |
|
|
331 |
1 |
m1.tred(); |
332 |
1 |
m2.tred(); |
333 |
1 |
assertMatricesMatch(m1, m2, 0.00001d); |
334 |
|
|
335 |
1 |
m1.tqli(); |
336 |
1 |
m2.tqli(); |
337 |
1 |
assertMatricesMatch(m1, m2, 0.00001d); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
@param |
344 |
|
@param |
345 |
|
@param |
346 |
|
|
347 |
|
@return |
348 |
|
|
|
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 5 |
Complexity Density: 0.5 |
|
349 |
3 |
public double[][] getSparseValues(int rows, int cols, int occupancy)... |
350 |
|
{ |
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
3 |
double[][] d = new double[rows][cols]; |
356 |
3 |
int m = 0; |
357 |
29 |
for (int i = 0; i < rows; i++) |
358 |
|
{ |
359 |
26 |
if (++m % occupancy == 0) |
360 |
|
{ |
361 |
0 |
d[i][i] = r.nextInt() % 13; |
362 |
|
} |
363 |
131 |
for (int j = 0; j < i; j++) |
364 |
|
{ |
365 |
105 |
if (++m % occupancy == 0) |
366 |
|
{ |
367 |
43 |
d[i][j] = r.nextInt() % 13; |
368 |
43 |
d[j][i] = d[i][j]; |
369 |
|
} |
370 |
|
} |
371 |
|
} |
372 |
3 |
return d; |
373 |
|
|
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
380 |
1 |
@Test(groups = "Functional")... |
381 |
|
public void testPreMultiply_sparseProduct() |
382 |
|
{ |
383 |
1 |
MatrixI m1 = new SparseMatrix( |
384 |
|
new double[][] |
385 |
|
{ { 1 }, { 0 }, { 0 }, { 0 }, { 0 } }); |
386 |
1 |
MatrixI m2 = new SparseMatrix(new double[][] { { 1, 1, 1, 1 } }); |
387 |
|
|
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
|
392 |
1 |
MatrixI m3 = m2.preMultiply(m1); |
393 |
1 |
assertFalse(m3 instanceof SparseMatrix); |
394 |
|
|
395 |
|
|
396 |
|
|
397 |
|
|
398 |
|
|
399 |
1 |
m2 = new SparseMatrix(new double[][] { { 1, 1, 1, 0 } }); |
400 |
1 |
m3 = m2.preMultiply(m1); |
401 |
1 |
assertTrue(m3 instanceof SparseMatrix); |
402 |
|
} |
403 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
404 |
1 |
@Test(groups = "Functional")... |
405 |
|
public void testFillRatio() |
406 |
|
{ |
407 |
1 |
SparseMatrix m1 = new SparseMatrix( |
408 |
|
new double[][] |
409 |
|
{ { 2, 0, 4, 1, 0 }, { 0, 6, 0, 0, 0 } }); |
410 |
1 |
assertEquals(m1.getFillRatio(), 0.4f); |
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
1PASS
|
|
417 |
1 |
@Test(groups = "Functional")... |
418 |
|
public void testTred_reproducible() |
419 |
|
{ |
420 |
|
|
421 |
|
|
422 |
|
|
423 |
1 |
int rows = 10; |
424 |
1 |
int cols = rows; |
425 |
1 |
double[][] d = getSparseValues(rows, cols, 3); |
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
1 |
double[][] d1 = new double[rows][cols]; |
432 |
11 |
for (int row = 0; row < rows; row++) |
433 |
|
{ |
434 |
110 |
for (int col = 0; col < cols; col++) |
435 |
|
{ |
436 |
100 |
d1[row][col] = d[row][col]; |
437 |
|
} |
438 |
|
} |
439 |
1 |
Matrix m1 = new SparseMatrix(d); |
440 |
1 |
Matrix m2 = new SparseMatrix(d1); |
441 |
1 |
assertMatricesMatch(m1, m2, 1.0e16); |
442 |
1 |
m1.tred(); |
443 |
1 |
m2.tred(); |
444 |
1 |
assertMatricesMatch(m1, m2, 0.00001d); |
445 |
|
} |
446 |
|
} |