From: gmungoc Date: Thu, 26 Jan 2017 09:20:46 +0000 (+0000) Subject: JAL-2380 code tidy after review X-Git-Tag: Release_2_10_3b1~360 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=05d2ce87440717d47ba3dc6ef293e25e4543fdd1;p=jalview.git JAL-2380 code tidy after review --- diff --git a/src/jalview/analysis/PCA.java b/src/jalview/analysis/PCA.java index 06a139b..eaea7bf 100755 --- a/src/jalview/analysis/PCA.java +++ b/src/jalview/analysis/PCA.java @@ -144,8 +144,8 @@ public class PCA implements Runnable // System.out.println("Created array"); // printMemory(rt); // System.out.println(" --- Original matrix ---- "); - m = new Matrix(seqmat, count, bs[0].getDBinary().length); - m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length); + m = new Matrix(seqmat); + m2 = new Matrix(seqmat2); } @@ -252,15 +252,18 @@ public class PCA implements Runnable /** * DOCUMENT ME! */ + @Override public void run() { PrintStream ps = new PrintStream(System.out) { + @Override public void print(String x) { details.append(x); } + @Override public void println() { details.append("\n"); diff --git a/src/jalview/math/Matrix.java b/src/jalview/math/Matrix.java index 28b9d67..647fc3a 100755 --- a/src/jalview/math/Matrix.java +++ b/src/jalview/math/Matrix.java @@ -26,22 +26,23 @@ import jalview.util.MessageManager; import java.io.PrintStream; /** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ + * A class to model rectangular matrices of double values and operations on them */ public class Matrix { - /** - * SMJSPUBLIC + /* + * the cell values in row-major order */ public double[][] value; - /** DOCUMENT ME!! */ + /* + * the number of rows + */ public int rows; - /** DOCUMENT ME!! */ + /* + * the number of columns + */ public int cols; /** DOCUMENT ME!! */ @@ -60,10 +61,10 @@ public class Matrix * Creates a new Matrix object. For example * *
-   *   new Matrix(new double[][] {{2, 3}, {4, 5}, 2, 2)
+   *   new Matrix(new double[][] {{2, 3, 4}, {5, 6, 7})
    * constructs
-   *   (2 3)
-   *   (4 5)
+   *   (2 3 4)
+   *   (5 6 7)
    * 
* * Note that ragged arrays (with not all rows, or columns, of the same @@ -72,13 +73,11 @@ public class Matrix * * @param values * the matrix values in row-major order - * @param rows - * @param cols */ - public Matrix(double[][] values, int rows, int cols) + public Matrix(double[][] values) { - this.rows = rows; - this.cols = cols; + this.rows = values.length; + this.cols = this.rows == 0 ? 0 : values[0].length; this.value = values; } @@ -99,7 +98,7 @@ public class Matrix } } - return new Matrix(out, cols, rows); + return new Matrix(out); } /** @@ -155,7 +154,7 @@ public class Matrix } } - return new Matrix(tmp, in.rows, this.cols); + return new Matrix(tmp); } /** @@ -218,13 +217,9 @@ public class Matrix for (int i = 0; i < rows; i++) { System.arraycopy(value[i], 0, newmat[i], 0, value[i].length); - // for (int j = 0; j < cols; j++) - // { - // newmat[i][j] = value[i][j]; - // } } - return new Matrix(newmat, rows, cols); + return new Matrix(newmat); } /** diff --git a/test/jalview/math/MatrixTest.java b/test/jalview/math/MatrixTest.java index 795b2fa..a4acbd0 100644 --- a/test/jalview/math/MatrixTest.java +++ b/test/jalview/math/MatrixTest.java @@ -1,9 +1,11 @@ package jalview.math; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import java.util.Arrays; +import java.util.Random; import org.testng.annotations.Test; @@ -16,8 +18,8 @@ public class MatrixTest int cols = 1000; double[][] d1 = new double[rows][cols]; double[][] d2 = new double[cols][rows]; - Matrix m1 = new Matrix(d1, rows, cols); - Matrix m2 = new Matrix(d2, cols, rows); + Matrix m1 = new Matrix(d1); + Matrix m2 = new Matrix(d2); long start = System.currentTimeMillis(); m1.preMultiply(m2); long elapsed = System.currentTimeMillis() - start; @@ -28,8 +30,8 @@ public class MatrixTest @Test(groups = "Functional") public void testPreMultiply() { - Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 } }, 1, 3); // 1x3 - Matrix m2 = new Matrix(new double[][] { { 5 }, { 6 }, { 7 } }, 3, 1); // 3x1 + Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 } }); // 1x3 + Matrix m2 = new Matrix(new double[][] { { 5 }, { 6 }, { 7 } }); // 3x1 /* * 1x3 times 3x1 is 1x1 @@ -56,8 +58,7 @@ public class MatrixTest expectedExceptions = { IllegalArgumentException.class }) public void testPreMultiply_tooManyColumns() { - Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }, 2, - 3); // 2x3 + Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3 /* * 2x3 times 2x3 invalid operation - @@ -72,8 +73,7 @@ public class MatrixTest expectedExceptions = { IllegalArgumentException.class }) public void testPreMultiply_tooFewColumns() { - Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }, 2, - 3); // 2x3 + Matrix m1 = new Matrix(new double[][] { { 2, 3, 4 }, { 3, 4, 5 } }); // 2x3 /* * 3x2 times 3x2 invalid operation - @@ -99,9 +99,8 @@ public class MatrixTest * (3020 30200) * (5040 50400) */ - Matrix m1 = new Matrix(new double[][] { { 2, 3 }, { 4, 5 } }, 2, 2); - Matrix m2 = new Matrix(new double[][] { { 10, 100 }, { 1000, 10000 } }, - 2, 2); + Matrix m1 = new Matrix(new double[][] { { 2, 3 }, { 4, 5 } }); + Matrix m2 = new Matrix(new double[][] { { 10, 100 }, { 1000, 10000 } }); Matrix m3 = m1.postMultiply(m2); assertEquals(Arrays.toString(m3.value[0]), "[3020.0, 30200.0]"); assertEquals(Arrays.toString(m3.value[1]), "[5040.0, 50400.0]"); @@ -118,8 +117,8 @@ public class MatrixTest * (2).(10 100 1000) = (20 200 2000) * (3) (30 300 3000) */ - m1 = new Matrix(new double[][] { { 2 }, { 3 } }, 2, 1); - m2 = new Matrix(new double[][] { { 10, 100, 1000 } }, 1, 3); + m1 = new Matrix(new double[][] { { 2 }, { 3 } }); + m2 = new Matrix(new double[][] { { 10, 100, 1000 } }); m3 = m1.postMultiply(m2); assertEquals(m3.rows, 2); assertEquals(m3.cols, 3); @@ -139,8 +138,8 @@ public class MatrixTest * [0, 0] = 2*5 + 3*6 + 4*7 = 56 * [0, 1] = 2*4 + 3*3 + 4*2 = 25 */ - m1 = new Matrix(new double[][] { { 2, 3, 4 } }, 1, 3); - m2 = new Matrix(new double[][] { { 5, 4 }, { 6, 3 }, { 7, 2 } }, 3, 2); + m1 = new Matrix(new double[][] { { 2, 3, 4 } }); + m2 = new Matrix(new double[][] { { 5, 4 }, { 6, 3 }, { 7, 2 } }); m3 = m1.postMultiply(m2); assertEquals(m3.rows, 1); assertEquals(m3.cols, 2); @@ -157,6 +156,26 @@ public class MatrixTest assertEquals(m3.value[0][1], 25d); } + @Test(groups = "Functional") + public void testCopy() + { + Random r = new Random(); + int rows = 5; + int cols = 11; + double[][] in = new double[rows][cols]; + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + in[i][j] = r.nextDouble(); + } + } + Matrix m1 = new Matrix(in); + Matrix m2 = m1.copy(); + assertTrue(matrixEquals(m1, m2)); + } + /** * main method extracted from Matrix * @@ -175,7 +194,7 @@ public class MatrixTest } } - Matrix origmat = new Matrix(in, n, n); + Matrix origmat = new Matrix(in); // System.out.println(" --- Original matrix ---- "); // / origmat.print(System.out);