KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > linear > CholeskySolverTest


1 /*
2  *
3  * Copyright (c) 2003-2004 The Apache Software Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

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 /**
27  * Test cases for the {@link CholeskySolver} class.
28  * <p>
29  * @author Stefan Koeberle, 11/2003
30  */

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     /**
67      * Creates a new instance of CholeskySolverTest
68      */

69     public CholeskySolverTest(String JavaDoc nameOfTest) {
70         super(nameOfTest);
71     }//constructor CholeskySolverTest
72

73     public void setUp()
74     throws java.lang.Exception JavaDoc {
75        super.setUp();
76     }//setUp
77

78    
79     public void tearDown()
80     throws java.lang.Exception JavaDoc {
81         super.tearDown();
82     }//tearDown
83

84     public static Test suite() {
85         TestSuite suite = new TestSuite(CholeskySolverTest.class);
86         suite.setName("CholeskySolver Tests");
87         return suite;
88     }//suite
89

90     
91     /**
92      * tests CholeskySolver.setNumericalZero()
93      */

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 JavaDoc e) {}
106         
107     }//testNumericalZero
108

109     
110     /**
111      * tests CholeskySolver.decompose(...)
112      */

113     public void testDecompose() {
114         
115         //The following decompositions should succeed.
116
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         //The following decompositions will fail. An IllegalArgumentException
123
//should be thrown.
124
try {
125             testDecompose(m6, "Decomposing matrix m6");
126             fail("Decomposing matrix m6");
127         } catch (IllegalArgumentException JavaDoc e) {}
128         
129          try {
130              CholeskySolver solver = new CholeskySolver();
131              solver.decompose(new RealMatrixImpl(m7));
132              fail("Decomposing matrix m7");
133         } catch (IllegalArgumentException JavaDoc e) {}
134         
135     }//testDecomposition
136

137     
138     /**
139      * tests CholeskySolver.solve(...)
140      */

141     public void testSolve() {
142
143         //If there's no matrix, there's no linear euqitation to solve ...
144
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 JavaDoc e) {}
149
150         //The following operations should succeed.
151
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         //The following operations will fail. An IllegalArgumentException
158
//should be thrown.
159
try {
160           testSolve(m6, "Solving matrix m6");
161           fail("Solving matrix m6");
162         } catch (IllegalArgumentException JavaDoc 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 JavaDoc e) {}
169         
170     }//testDecomposition
171

172     
173     /**
174      * tests CholeskySolver.getDeterminant(...)
175      */

176     public void testGetDeterminant() {
177         
178         //Since no matrix was decomposed, there's no determinant.
179
try {
180              CholeskySolver solver = new CholeskySolver();
181              solver.getDeterminant();
182              fail("Calculating determinant of missing matrix should fail");
183         } catch (IllegalStateException JavaDoc e) {}
184        
185         //These test will suceed.
186
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     }//test
192

193     
194     /**
195      * Generates the matrix
196      * <code>m = lowerTriangularMatrix * lowerTriangularMatrix^T</code>.
197      * If alle diagonalelements of <code>lowerTriangularMatrix</code> are
198      * positiv, <code>m</code> will be positiv definit.
199      * Decomposing <code>m</code> should result in
200      * <code>lowerTriangularMatrix</code> again. So there's a simple test ...
201      */

202     private void testDecompose(double[][] lowerTriangularMatrix, String JavaDoc message)
203     throws IllegalArgumentException JavaDoc {
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     }//testDecompose
216

217     
218     /**
219      * Similar to <code> private testDecompose(...)</code>.
220      */

221     private void testSolve(double[][] lowerTriangularMatrix, String JavaDoc 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     }//testSolve
241

242     
243     /**
244      * Similar to <code> private testDecompose(...)</code>.
245      */

246     private void testGetDeterminant(double[][] lowerTriangularMatrix,
247                                     double determinant,
248                                     String JavaDoc message)
249     throws IllegalArgumentException JavaDoc {
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     }//testGetDeterminant
260

261     
262     /**
263      * Are <code>m1</code> and <code>m2</code> equal?
264      */

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     }//isEqual
281

282      
283     /**
284      * Executes all tests of this class
285      */

286     public static void main(String JavaDoc[] args) {
287         System.out.println("Start");
288         TestRunner runner = new TestRunner();
289         runner.doRun(CholeskySolverTest.suite());
290         System.out.println("End");
291     }//main
292

293 }//class CholeskySolverTest
294
Popular Tags