From a53dccf77a22c265a119a8b7da653eb03fd252eb Mon Sep 17 00:00:00 2001 From: "tomas.mccandless" Date: Thu, 15 Sep 2016 13:02:55 -0700 Subject: [PATCH] add dimensionality check to MatrixApproximatelyEquals --- src/test/java/org/surus/math/RPCA_Test.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/test/java/org/surus/math/RPCA_Test.java b/src/test/java/org/surus/math/RPCA_Test.java index 299872b..ff180e9 100644 --- a/src/test/java/org/surus/math/RPCA_Test.java +++ b/src/test/java/org/surus/math/RPCA_Test.java @@ -1,6 +1,7 @@ package org.surus.math; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import org.junit.Test; @@ -20,6 +21,11 @@ public boolean MatrixApproximatelyEquals(double[][] X, double[][] Y, double epsi boolean testOutput = true; int printCnt = 0; + if (!(X[0].length == Y[0].length && X.length == Y.length)) { + System.out.println("X and Y matrices had differing dimensions."); + return false; + } + for (int j = 0; j < X[0].length; j++) { for (int i = 0; i < X.length; i++) { if (Math.abs(X[i][j] - Y[i][j]) > epsilon) { @@ -31,6 +37,17 @@ public boolean MatrixApproximatelyEquals(double[][] X, double[][] Y, double epsi } return testOutput; } + + @Test + public void testMatrixApproximatelyEquals() { + final double[] ts1 = new double[] { 1.0, 0.0, 0.0, 1.0 }; + final double[] ts2 = new double[] { 1.0, 0.0, 0.0, 1.0 , 0.0, 0.0}; + + final double[][] X1 = VectorToMatrix(ts1, 2, 2); + final double[][] X2 = VectorToMatrix(ts1, 2, 3); + + assertFalse(MatrixApproximatelyEquals(X1, X2, 0.0)); + } @Test public void testRSVD() {