Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.math

File MatrixTest.java

 

Code metrics

44
227
19
1
577
367
42
0.19
11.95
19
2.21

Classes

Class Line # Actions
MatrixTest 16 227 42
0.8379310483.8%
 

Contributing tests

This file is covered by 13 tests. .

Source view

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.assertNotSame;
6    import static org.testng.Assert.assertNull;
7    import static org.testng.Assert.assertTrue;
8    import static org.testng.Assert.fail;
9   
10    import java.util.Arrays;
11    import java.util.Random;
12   
13    import org.testng.annotations.Test;
14    import org.testng.internal.junit.ArrayAsserts;
15   
 
16    public class MatrixTest
17    {
18    final static double DELTA = 0.000001d;
19   
 
20  0 toggle @Test(groups = "Timing")
21    public void testPreMultiply_timing()
22    {
23  0 int rows = 50; // increase to stress test timing
24  0 int cols = 100;
25  0 double[][] d1 = new double[rows][cols];
26  0 double[][] d2 = new double[cols][rows];
27  0 Matrix m1 = new Matrix(d1);
28  0 Matrix m2 = new Matrix(d2);
29  0 long start = System.currentTimeMillis();
30  0 m1.preMultiply(m2);
31  0 long elapsed = System.currentTimeMillis() - start;
32  0 System.out.println(rows + "x" + cols
33    + " multiplications of double took " + elapsed + "ms");
34    }
35   
 
36  1 toggle @Test(groups = "Functional")
37    public void testPreMultiply()
38    {
39  1 Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 } }); // 1x3
40  1 Matrix m2 = new Matrix(new double[][] { { 5 }, { 6 }, { 7 } }); // 3x1
41   
42    /*
43    * 1x3 times 3x1 is 1x1
44    * 2x5 + 3x6 + 4*7 = 56
45    */
46  1 MatrixI m3 = m2.preMultiply(m1);
47  1 assertEquals(m3.height(), 1);
48  1 assertEquals(m3.width(), 1);
49  1 assertEquals(m3.getValue(0, 0), 56d);
50   
51    /*
52    * 3x1 times 1x3 is 3x3
53    */
54  1 m3 = m1.preMultiply(m2);
55  1 assertEquals(m3.height(), 3);
56  1 assertEquals(m3.width(), 3);
57  1 assertEquals(Arrays.toString(m3.getRow(0)), "[10.0, 15.0, 20.0]");
58  1 assertEquals(Arrays.toString(m3.getRow(1)), "[12.0, 18.0, 24.0]");
59  1 assertEquals(Arrays.toString(m3.getRow(2)), "[14.0, 21.0, 28.0]");
60    }
61   
 
62  1 toggle @Test(
63    groups = "Functional",
64    expectedExceptions = { IllegalArgumentException.class })
65    public void testPreMultiply_tooManyColumns()
66    {
67  1 Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
68   
69    /*
70    * 2x3 times 2x3 invalid operation -
71    * multiplier has more columns than multiplicand has rows
72    */
73  1 m1.preMultiply(m1);
74  0 fail("Expected exception");
75    }
76   
 
77  1 toggle @Test(
78    groups = "Functional",
79    expectedExceptions = { IllegalArgumentException.class })
80    public void testPreMultiply_tooFewColumns()
81    {
82  1 Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3
83   
84    /*
85    * 3x2 times 3x2 invalid operation -
86    * multiplier has more columns than multiplicand has row
87    */
88  1 m1.preMultiply(m1);
89  0 fail("Expected exception");
90    }
91   
92   
 
93  1 toggle private boolean matrixEquals(Matrix m1, Matrix m2) {
94  1 if (m1.width() != m2.width() || m1.height() != m2.height())
95    {
96  0 return false;
97    }
98  6 for (int i = 0; i < m1.height(); i++)
99    {
100  5 if (!Arrays.equals(m1.getRow(i), m2.getRow(i)))
101    {
102  0 return false;
103    }
104    }
105  1 return true;
106    }
107   
 
108  1 toggle @Test(groups = "Functional")
109    public void testPostMultiply()
110    {
111    /*
112    * Square matrices
113    * (2 3) . (10 100)
114    * (4 5) (1000 10000)
115    * =
116    * (3020 30200)
117    * (5040 50400)
118    */
119  1 MatrixI m1 = new Matrix(new double[][] { { 2, 3 }, { 4, 5 } });
120  1 MatrixI m2 = new Matrix(new double[][] { { 10, 100 }, { 1000, 10000 } });
121  1 MatrixI m3 = m1.postMultiply(m2);
122  1 assertEquals(Arrays.toString(m3.getRow(0)), "[3020.0, 30200.0]");
123  1 assertEquals(Arrays.toString(m3.getRow(1)), "[5040.0, 50400.0]");
124   
125    /*
126    * also check m2.preMultiply(m1) - should be same as m1.postMultiply(m2)
127    */
128  1 m3 = m2.preMultiply(m1);
129  1 assertEquals(Arrays.toString(m3.getRow(0)), "[3020.0, 30200.0]");
130  1 assertEquals(Arrays.toString(m3.getRow(1)), "[5040.0, 50400.0]");
131   
132    /*
133    * m1 has more rows than columns
134    * (2).(10 100 1000) = (20 200 2000)
135    * (3) (30 300 3000)
136    */
137  1 m1 = new Matrix(new double[][] { { 2 }, { 3 } });
138  1 m2 = new Matrix(new double[][] { { 10, 100, 1000 } });
139  1 m3 = m1.postMultiply(m2);
140  1 assertEquals(m3.height(), 2);
141  1 assertEquals(m3.width(), 3);
142  1 assertEquals(Arrays.toString(m3.getRow(0)), "[20.0, 200.0, 2000.0]");
143  1 assertEquals(Arrays.toString(m3.getRow(1)), "[30.0, 300.0, 3000.0]");
144  1 m3 = m2.preMultiply(m1);
145  1 assertEquals(m3.height(), 2);
146  1 assertEquals(m3.width(), 3);
147  1 assertEquals(Arrays.toString(m3.getRow(0)), "[20.0, 200.0, 2000.0]");
148  1 assertEquals(Arrays.toString(m3.getRow(1)), "[30.0, 300.0, 3000.0]");
149   
150    /*
151    * m1 has more columns than rows
152    * (2 3 4) . (5 4) = (56 25)
153    * (6 3)
154    * (7 2)
155    * [0, 0] = 2*5 + 3*6 + 4*7 = 56
156    * [0, 1] = 2*4 + 3*3 + 4*2 = 25
157    */
158  1 m1 = new Matrix(new double[][] { { 2, 3, 4 } });
159  1 m2 = new Matrix(new double[][] { { 5, 4 }, { 6, 3 }, { 7, 2 } });
160  1 m3 = m1.postMultiply(m2);
161  1 assertEquals(m3.height(), 1);
162  1 assertEquals(m3.width(), 2);
163  1 assertEquals(m3.getRow(0)[0], 56d);
164  1 assertEquals(m3.getRow(0)[1], 25d);
165   
166    /*
167    * and check premultiply equivalent
168    */
169  1 m3 = m2.preMultiply(m1);
170  1 assertEquals(m3.height(), 1);
171  1 assertEquals(m3.width(), 2);
172  1 assertEquals(m3.getRow(0)[0], 56d);
173  1 assertEquals(m3.getRow(0)[1], 25d);
174    }
175   
 
176  1 toggle @Test(groups = "Functional")
177    public void testCopy()
178    {
179  1 Random r = new Random();
180  1 int rows = 5;
181  1 int cols = 11;
182  1 double[][] in = new double[rows][cols];
183   
184  6 for (int i = 0; i < rows; i++)
185    {
186  60 for (int j = 0; j < cols; j++)
187    {
188  55 in[i][j] = r.nextDouble();
189    }
190    }
191  1 Matrix m1 = new Matrix(in);
192   
193  1 Matrix m2 = (Matrix) m1.copy();
194  1 assertNotSame(m1, m2);
195  1 assertTrue(matrixEquals(m1, m2));
196  1 assertNull(m2.d);
197  1 assertNull(m2.e);
198   
199    /*
200    * now add d and e vectors and recopy
201    */
202  1 m1.d = Arrays.copyOf(in[2], in[2].length);
203  1 m1.e = Arrays.copyOf(in[4], in[4].length);
204  1 m2 = (Matrix) m1.copy();
205  1 assertNotSame(m2.d, m1.d);
206  1 assertNotSame(m2.e, m1.e);
207  1 assertEquals(m2.d, m1.d);
208  1 assertEquals(m2.e, m1.e);
209    }
210   
211    /**
212    * main method extracted from Matrix
213    *
214    * @param args
215    */
 
216  0 toggle public static void main(String[] args) throws Exception
217    {
218  0 int n = Integer.parseInt(args[0]);
219  0 double[][] in = new double[n][n];
220   
221  0 for (int i = 0; i < n; i++)
222    {
223  0 for (int j = 0; j < n; j++)
224    {
225  0 in[i][j] = Math.random();
226    }
227    }
228   
229  0 Matrix origmat = new Matrix(in);
230   
231    // System.out.println(" --- Original matrix ---- ");
232    // / origmat.print(System.out);
233    // System.out.println();
234    // System.out.println(" --- transpose matrix ---- ");
235  0 MatrixI trans = origmat.transpose();
236   
237    // trans.print(System.out);
238    // System.out.println();
239    // System.out.println(" --- OrigT * Orig ---- ");
240  0 MatrixI symm = trans.postMultiply(origmat);
241   
242    // symm.print(System.out);
243    // System.out.println();
244    // Copy the symmetric matrix for later
245    // Matrix origsymm = symm.copy();
246   
247    // This produces the tridiagonal transformation matrix
248    // long tstart = System.currentTimeMillis();
249  0 symm.tred();
250   
251    // long tend = System.currentTimeMillis();
252   
253    // System.out.println("Time take for tred = " + (tend-tstart) + "ms");
254    // System.out.println(" ---Tridiag transform matrix ---");
255    // symm.print(System.out);
256    // System.out.println();
257    // System.out.println(" --- D vector ---");
258    // symm.printD(System.out);
259    // System.out.println();
260    // System.out.println(" --- E vector ---");
261    // symm.printE(System.out);
262    // System.out.println();
263    // Now produce the diagonalization matrix
264    // tstart = System.currentTimeMillis();
265  0 symm.tqli();
266    // tend = System.currentTimeMillis();
267   
268    // System.out.println("Time take for tqli = " + (tend-tstart) + " ms");
269    // System.out.println(" --- New diagonalization matrix ---");
270    // symm.print(System.out);
271    // System.out.println();
272    // System.out.println(" --- D vector ---");
273    // symm.printD(System.out);
274    // System.out.println();
275    // System.out.println(" --- E vector ---");
276    // symm.printE(System.out);
277    // System.out.println();
278    // System.out.println(" --- First eigenvector --- ");
279    // double[] eigenv = symm.getColumn(0);
280    // for (int i=0; i < eigenv.length;i++) {
281    // Format.print(System.out,"%15.4f",eigenv[i]);
282    // }
283    // System.out.println();
284    // double[] neigenv = origsymm.vectorPostMultiply(eigenv);
285    // for (int i=0; i < neigenv.length;i++) {
286    // Format.print(System.out,"%15.4f",neigenv[i]/symm.d[0]);
287    // }
288    // System.out.println();
289    }
290   
 
291  0 toggle @Test(groups = "Timing")
292    public void testSign()
293    {
294  0 assertEquals(Matrix.sign(-1, -2), -1d);
295  0 assertEquals(Matrix.sign(-1, 2), 1d);
296  0 assertEquals(Matrix.sign(-1, 0), 1d);
297  0 assertEquals(Matrix.sign(1, -2), -1d);
298  0 assertEquals(Matrix.sign(1, 2), 1d);
299  0 assertEquals(Matrix.sign(1, 0), 1d);
300    }
301   
302    /**
303    * Helper method to make values for a sparse, pseudo-random symmetric matrix
304    *
305    * @param rows
306    * @param cols
307    * @param occupancy
308    * one in 'occupancy' entries will be non-zero
309    * @return
310    */
 
311  1 toggle public double[][] getSparseValues(int rows, int cols, int occupancy)
312    {
313  1 Random r = new Random(1729);
314   
315    /*
316    * generate whole number values between -12 and +12
317    * (to mimic score matrices used in Jalview)
318    */
319  1 double[][] d = new double[rows][cols];
320  1 int m = 0;
321  11 for (int i = 0; i < rows; i++)
322    {
323  10 if (++m % occupancy == 0)
324    {
325  0 d[i][i] = r.nextInt() % 13; // diagonal
326    }
327  55 for (int j = 0; j < i; j++)
328    {
329  45 if (++m % occupancy == 0)
330    {
331  18 d[i][j] = r.nextInt() % 13;
332  18 d[j][i] = d[i][j];
333    }
334    }
335    }
336  1 return d;
337   
338    }
339   
340    /**
341    * Verify that the results of method tred() are the same if the calculation is
342    * redone
343    */
 
344  1 toggle @Test(groups = "Functional")
345    public void testTred_reproducible()
346    {
347    /*
348    * make a pseudo-random symmetric matrix as required for tred/tqli
349    */
350  1 int rows = 10;
351  1 int cols = rows;
352  1 double[][] d = getSparseValues(rows, cols, 3);
353   
354    /*
355    * make a copy of the values so m1, m2 are not
356    * sharing arrays!
357    */
358  1 double[][] d1 = new double[rows][cols];
359  11 for (int row = 0; row < rows; row++)
360    {
361  110 for (int col = 0; col < cols; col++)
362    {
363  100 d1[row][col] = d[row][col];
364    }
365    }
366  1 Matrix m1 = new Matrix(d);
367  1 Matrix m2 = new Matrix(d1);
368  1 assertMatricesMatch(m1, m2); // sanity check
369  1 m1.tred();
370  1 m2.tred();
371  1 assertMatricesMatch(m1, m2);
372    }
373   
 
374  2 toggle public static void assertMatricesMatch(MatrixI m1, MatrixI m2)
375    {
376  2 if (m1.height() != m2.height())
377    {
378  0 fail("height mismatch");
379    }
380  2 if (m1.width() != m2.width())
381    {
382  0 fail("width mismatch");
383    }
384  22 for (int row = 0; row < m1.height(); row++)
385    {
386  220 for (int col = 0; col < m1.width(); col++)
387    {
388  200 double v2 = m2.getValue(row, col);
389  200 double v1 = m1.getValue(row, col);
390  200 if (Math.abs(v1 - v2) > DELTA)
391    {
392  0 fail(String.format("At [%d, %d] %f != %f", row, col, v1, v2));
393    }
394    }
395    }
396  2 ArrayAsserts.assertArrayEquals("D vector", m1.getD(), m2.getD(),
397    0.00001d);
398  2 ArrayAsserts.assertArrayEquals("E vector", m1.getE(), m2.getE(),
399    0.00001d);
400    }
401   
 
402  1 toggle @Test(groups = "Functional")
403    public void testFindMinMax()
404    {
405    /*
406    * empty matrix case
407    */
408  1 Matrix m = new Matrix(new double[][] { {} });
409  1 assertNull(m.findMinMax());
410   
411    /*
412    * normal case
413    */
414  1 double[][] vals = new double[2][];
415  1 vals[0] = new double[] {7d, 1d, -2.3d};
416  1 vals[1] = new double[] {-12d, 94.3d, -102.34d};
417  1 m = new Matrix(vals);
418  1 double[] minMax = m.findMinMax();
419  1 assertEquals(minMax[0], -102.34d);
420  1 assertEquals(minMax[1], 94.3d);
421    }
422   
 
423  1 toggle @Test(groups = { "Functional", "Timing" })
424    public void testFindMinMax_timing()
425    {
426  1 Random r = new Random();
427  1 int size = 1000; // increase to stress test timing
428  1 double[][] vals = new double[size][size];
429  1 double max = -Double.MAX_VALUE;
430  1 double min = Double.MAX_VALUE;
431  1001 for (int i = 0; i < size; i++)
432    {
433  1000 vals[i] = new double[size];
434  1001000 for (int j = 0; j < size; j++)
435    {
436    // use nextLong rather than nextDouble to include negative values
437  1000000 double d = r.nextLong();
438  1000000 if (d > max)
439    {
440  15 max = d;
441    }
442  1000000 if (d < min)
443    {
444  10 min = d;
445    }
446  1000000 vals[i][j] = d;
447    }
448    }
449  1 Matrix m = new Matrix(vals);
450  1 long now = System.currentTimeMillis();
451  1 double[] minMax = m.findMinMax();
452  1 System.out.println(String.format("findMinMax for %d x %d took %dms",
453    size, size, (System.currentTimeMillis() - now)));
454  1 assertEquals(minMax[0], min);
455  1 assertEquals(minMax[1], max);
456    }
457   
458    /**
459    * Test range reversal with maximum value becoming zero
460    */
 
461  1 toggle @Test(groups = "Functional")
462    public void testReverseRange_maxToZero()
463    {
464  1 Matrix m1 = new Matrix(
465    new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } });
466   
467    /*
468    * subtract all from max: range -3.4 to 15 becomes 18.4 to 0
469    */
470  1 m1.reverseRange(true);
471  1 assertEquals(m1.getValue(0, 0), 13d, DELTA);
472  1 assertEquals(m1.getValue(0, 1), 11.5d, DELTA);
473  1 assertEquals(m1.getValue(0, 2), 11d, DELTA);
474  1 assertEquals(m1.getValue(1, 0), 18.4d, DELTA);
475  1 assertEquals(m1.getValue(1, 1), 11d, DELTA);
476  1 assertEquals(m1.getValue(1, 2), 0d, DELTA);
477   
478    /*
479    * repeat operation - range is now 0 to 18.4
480    */
481  1 m1.reverseRange(true);
482  1 assertEquals(m1.getValue(0, 0), 5.4d, DELTA);
483  1 assertEquals(m1.getValue(0, 1), 6.9d, DELTA);
484  1 assertEquals(m1.getValue(0, 2), 7.4d, DELTA);
485  1 assertEquals(m1.getValue(1, 0), 0d, DELTA);
486  1 assertEquals(m1.getValue(1, 1), 7.4d, DELTA);
487  1 assertEquals(m1.getValue(1, 2), 18.4d, DELTA);
488    }
489   
490    /**
491    * Test range reversal with minimum and maximum values swapped
492    */
 
493  1 toggle @Test(groups = "Functional")
494    public void testReverseRange_swapMinMax()
495    {
496  1 Matrix m1 = new Matrix(
497    new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } });
498   
499    /*
500    * swap all values in min-max range
501    * = subtract from (min + max = 11.6)
502    * range -3.4 to 15 becomes 18.4 to -3.4
503    */
504  1 m1.reverseRange(false);
505  1 assertEquals(m1.getValue(0, 0), 9.6d, DELTA);
506  1 assertEquals(m1.getValue(0, 1), 8.1d, DELTA);
507  1 assertEquals(m1.getValue(0, 2), 7.6d, DELTA);
508  1 assertEquals(m1.getValue(1, 0), 15d, DELTA);
509  1 assertEquals(m1.getValue(1, 1), 7.6d, DELTA);
510  1 assertEquals(m1.getValue(1, 2), -3.4d, DELTA);
511   
512    /*
513    * repeat operation - original values restored
514    */
515  1 m1.reverseRange(false);
516  1 assertEquals(m1.getValue(0, 0), 2d, DELTA);
517  1 assertEquals(m1.getValue(0, 1), 3.5d, DELTA);
518  1 assertEquals(m1.getValue(0, 2), 4d, DELTA);
519  1 assertEquals(m1.getValue(1, 0), -3.4d, DELTA);
520  1 assertEquals(m1.getValue(1, 1), 4d, DELTA);
521  1 assertEquals(m1.getValue(1, 2), 15d, DELTA);
522    }
523   
 
524  1 toggle @Test(groups = "Functional")
525    public void testMultiply()
526    {
527  1 Matrix m = new Matrix(new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } });
528  1 m.multiply(2d);
529  1 assertEquals(m.getValue(0, 0), 4d, DELTA);
530  1 assertEquals(m.getValue(0, 1), 7d, DELTA);
531  1 assertEquals(m.getValue(0, 2), 8d, DELTA);
532  1 assertEquals(m.getValue(1, 0), -6.8d, DELTA);
533  1 assertEquals(m.getValue(1, 1), 8d, DELTA);
534  1 assertEquals(m.getValue(1, 2), 30d, DELTA);
535    }
536   
 
537  1 toggle @Test(groups = "Functional")
538    public void testConstructor()
539    {
540  1 double[][] values = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
541  1 Matrix m = new Matrix(values);
542  1 assertEquals(m.getValue(0, 0), 1d, DELTA);
543   
544    /*
545    * verify the matrix has a copy of the original array
546    */
547  1 assertNotSame(values[0], m.getRow(0));
548  1 values[0][0] = -1d;
549  1 assertEquals(m.getValue(0, 0), 1d, DELTA); // unchanged
550    }
551   
 
552  1 toggle @Test(groups = "Functional")
553    public void testEquals()
554    {
555  1 double[][] values = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
556  1 Matrix m1 = new Matrix(values);
557  1 double[][] values2 = new double[][] { { 1, 2, 3 }, { 4, 5, 6 } };
558  1 Matrix m2 = new Matrix(values2);
559   
560  1 double delta = 0.0001d;
561  1 assertTrue(m1.equals(m1, delta));
562  1 assertTrue(m1.equals(m2, delta));
563  1 assertTrue(m2.equals(m1, delta));
564   
565  1 double[][] values3 = new double[][] { { 1, 2, 3 }, { 4, 5, 7 } };
566  1 m2 = new Matrix(values3);
567  1 assertFalse(m1.equals(m2, delta));
568  1 assertFalse(m2.equals(m1, delta));
569   
570    // must be same shape
571  1 values2 = new double[][] { { 1, 2, 3 } };
572  1 m2 = new Matrix(values2);
573  1 assertFalse(m2.equals(m1, delta));
574   
575  1 assertFalse(m1.equals(null, delta));
576    }
577    }