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