1 18 19 package org.apache.commons.math.linear; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 import junit.textui.TestRunner; 25 26 31 public class CholeskySolverTest 32 extends TestCase { 33 34 private double[][] m1 = {{1}}; 35 private double m1Det = 1.0d; 36 37 private double[][] m2 = {{1, 0} , 38 {0, 2}}; 39 private double m2Det = 2.0d; 40 41 private double[][] m3 = {{1, 0, 0}, 42 {0, 2, 0}, 43 {0, 0, 3}}; 44 private double m3Det = 6.0d; 45 46 private double[][] m4 = {{1, 0, 0}, 47 {2, 3, 0}, 48 {4, 5, 6}}; 49 private double m4Det = 18.0d; 50 51 private double[][] m5 = {{ 1, 0, 0, 0, 0}, 52 {-2, 3, 0, 0, 0}, 53 { 4, -5, 6, 0, 0}, 54 { 7, 8, -9, 10, 0}, 55 {11, 12, 13, 14, 15}}; 56 private double m5Det = 2700.0d; 57 58 59 private double[][] m6 = {{1, 0, 0}, 60 {2, 0, 0}, 61 {4, 5, 6}}; 62 63 private double[][] m7 = {{1, 2, 3}, 64 {4, 5, 6}}; 65 66 69 public CholeskySolverTest(String nameOfTest) { 70 super(nameOfTest); 71 } 73 public void setUp() 74 throws java.lang.Exception { 75 super.setUp(); 76 } 78 79 public void tearDown() 80 throws java.lang.Exception { 81 super.tearDown(); 82 } 84 public static Test suite() { 85 TestSuite suite = new TestSuite(CholeskySolverTest.class); 86 suite.setName("CholeskySolver Tests"); 87 return suite; 88 } 90 91 94 public void testNumericalZero() { 95 CholeskySolver solver = new CholeskySolver(); 96 double numericalZero = 77.77d; 97 solver.setNumericalZero(numericalZero); 98 assertEquals(solver.getNumericalZero(), numericalZero, 0.0d); 99 100 try { 101 solver.decompose( 102 new RealMatrixImpl(new double[][]{{numericalZero/2, 0}, 103 {0, numericalZero/2}})); 104 fail("testing numericalZero"); 105 } catch (IllegalArgumentException e) {} 106 107 } 109 110 113 public void testDecompose() { 114 115 testDecompose(m1, "Decomposing matrix m1"); 117 testDecompose(m2, "Decomposing matrix m2"); 118 testDecompose(m3, "Decomposing matrix m3"); 119 testDecompose(m4, "Decomposing matrix m4"); 120 testDecompose(m5, "Decomposing matrix m5"); 121 122 try { 125 testDecompose(m6, "Decomposing matrix m6"); 126 fail("Decomposing matrix m6"); 127 } catch (IllegalArgumentException e) {} 128 129 try { 130 CholeskySolver solver = new CholeskySolver(); 131 solver.decompose(new RealMatrixImpl(m7)); 132 fail("Decomposing matrix m7"); 133 } catch (IllegalArgumentException e) {} 134 135 } 137 138 141 public void testSolve() { 142 143 try { 145 CholeskySolver solver = new CholeskySolver(); 146 solver.solve(new double[] {1,2,3}); 147 fail("solving a liniar equitation with a missing matrix should fail"); 148 } catch (IllegalStateException e) {} 149 150 testSolve(m1, "Solving matrix m1"); 152 testSolve(m2, "Solving matrix m2"); 153 testSolve(m3, "Solving matrix m3"); 154 testSolve(m4, "Solving matrix m4"); 155 testSolve(m5, "Solving matrix m5"); 156 157 try { 160 testSolve(m6, "Solving matrix m6"); 161 fail("Solving matrix m6"); 162 } catch (IllegalArgumentException e) {} 163 164 try { 165 CholeskySolver solver = new CholeskySolver(); 166 solver.solve(new RealMatrixImpl(m3), new double[] {1, 2, 3, 4}); 167 fail("Solving matrix m3[3x3], v[4]"); 168 } catch (IllegalArgumentException e) {} 169 170 } 172 173 176 public void testGetDeterminant() { 177 178 try { 180 CholeskySolver solver = new CholeskySolver(); 181 solver.getDeterminant(); 182 fail("Calculating determinant of missing matrix should fail"); 183 } catch (IllegalStateException e) {} 184 185 testGetDeterminant(m1, m1Det, "Calculating determinant of m1"); 187 testGetDeterminant(m2, m2Det, "Calculating determinant of m2"); 188 testGetDeterminant(m3, m3Det, "Calculating determinant of m3"); 189 testGetDeterminant(m4, m4Det, "Calculating determinant of m4"); 190 testGetDeterminant(m5, m5Det, "Calculating determinant of m5"); 191 } 193 194 202 private void testDecompose(double[][] lowerTriangularMatrix, String message) 203 throws IllegalArgumentException { 204 205 RealMatrix triangularMatrix = new RealMatrixImpl(lowerTriangularMatrix); 206 RealMatrix pdMatrix = 207 triangularMatrix.multiply(triangularMatrix.transpose()); 208 209 CholeskySolver solver = new CholeskySolver(); 210 solver.decompose(pdMatrix); 211 212 assertTrue(message, 213 areEqual(triangularMatrix, solver.getDecomposition(), 1.0E-10)); 214 215 } 217 218 221 private void testSolve(double[][] lowerTriangularMatrix, String message) { 222 223 RealMatrix triangularMatrix = 224 new RealMatrixImpl(lowerTriangularMatrix); 225 RealMatrixImpl pdMatrix = 226 (RealMatrixImpl) triangularMatrix.multiply(triangularMatrix.transpose()); 227 CholeskySolver solver = 228 new CholeskySolver(); 229 230 double[] c = new double[lowerTriangularMatrix.length]; 231 for (int i=0; i<c.length; i++) 232 for (int j=0; j<lowerTriangularMatrix[0].length; j++) 233 c[i] += lowerTriangularMatrix[i][j]; 234 235 solver.decompose(pdMatrix); 236 RealMatrix x = new RealMatrixImpl(solver.solve(c)); 237 238 assertTrue(message, 239 areEqual(pdMatrix.multiply(x), new RealMatrixImpl(c), 1.0E-10)); 240 } 242 243 246 private void testGetDeterminant(double[][] lowerTriangularMatrix, 247 double determinant, 248 String message) 249 throws IllegalArgumentException { 250 251 RealMatrix triangularMatrix = new RealMatrixImpl(lowerTriangularMatrix); 252 RealMatrix pdMatrix = 253 triangularMatrix.multiply(triangularMatrix.transpose()); 254 double pdDeterminant = determinant * determinant; 255 256 CholeskySolver solver = new CholeskySolver(); 257 solver.decompose(pdMatrix); 258 assertEquals(message, solver.getDeterminant(), pdDeterminant, 1.0E-10); 259 } 261 262 265 private static boolean areEqual(RealMatrix m1, RealMatrix m2, double delta) { 266 267 double[][] mv1 = m1.getData(); 268 double[][] mv2 = m2.getData(); 269 270 if (mv1.length != mv1.length || 271 mv1[0].length != mv2[0].length) 272 return false; 273 274 for (int i=0; i<mv1.length; i++) 275 for (int j=0; j<mv1[0].length; j++) 276 if (Math.abs(mv1[i][j] -mv2[i][j]) > delta) 277 return false; 278 279 return true; 280 } 282 283 286 public static void main(String [] args) { 287 System.out.println("Start"); 288 TestRunner runner = new TestRunner(); 289 runner.doRun(CholeskySolverTest.suite()); 290 System.out.println("End"); 291 } 293 } | Popular Tags |