KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > tests > MatrixDecompositionTest


1 package JSci.tests;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.util.*;
5 import junit.framework.*;
6 import JSci.maths.*;
7 import JSci.maths.matrices.*;
8 import JSci.util.MatrixToolkit;
9
10 /**
11 * Testcase for matrix decomposition methods.
12 * @author Mark Hale
13 */

14 public class MatrixDecompositionTest extends TestCase {
15         private final int N=5;
16         private Constructor JavaDoc dConstructor, zConstructor;
17         private double dArray[][];
18         private Complex zArray[][];
19
20         public static void main(String JavaDoc arg[]) {
21                 junit.textui.TestRunner.run(suite());
22         }
23         public static Test suite() {
24                 Class JavaDoc[] dClasses = new Class JavaDoc[] {
25                         DoubleSquareMatrix.class, DoubleTridiagonalMatrix.class,
26                         DoubleDiagonalMatrix.class, DoubleSparseSquareMatrix.class
27                 };
28                 Class JavaDoc[] zClasses = new Class JavaDoc[] {
29                         ComplexSquareMatrix.class, ComplexTridiagonalMatrix.class,
30                         ComplexDiagonalMatrix.class, ComplexSquareMatrix.class // dummy
31
};
32                 TestSuite suite = new TestSuite(MatrixDecompositionTest.class.toString());
33                 for(int i=0; i<dClasses.length; i++) {
34                         Map properties = new HashMap();
35                         properties.put("test.matrix.double.class", dClasses[i]);
36                         properties.put("test.matrix.complex.class", zClasses[i]);
37                         suite.addTest(new junit.extensions.RepeatedTest(new TestProperties(new TestSuite(MatrixDecompositionTest.class, dClasses[i]+", "+zClasses[i]), properties), 5));
38                 }
39                 return suite;
40         }
41         public MatrixDecompositionTest(String JavaDoc name) {
42                 super(name);
43         }
44         protected void setUp() throws Exception JavaDoc {
45                 JSci.GlobalSettings.ZERO_TOL=1.0e-6;
46                 dArray=new double[N][N];
47                 Class JavaDoc matrixClass = (Class JavaDoc) TestProperties.getProperties().get("test.matrix.double.class");
48                 dConstructor = matrixClass.getConstructor(new Class JavaDoc[] {double[][].class});
49                 if(DiagonalMatrix.class.isAssignableFrom(matrixClass))
50                         setUpDoubleDiagonal();
51                 else if(TridiagonalMatrix.class.isAssignableFrom(matrixClass))
52                         setUpDoubleTridiagonal();
53                 else
54                         setUpDoubleRectangular();
55                 zArray=new Complex[N][N];
56                 matrixClass = (Class JavaDoc) TestProperties.getProperties().get("test.matrix.complex.class");
57                 zConstructor = matrixClass.getConstructor(new Class JavaDoc[] {Complex[][].class});
58                 if(DiagonalMatrix.class.isAssignableFrom(matrixClass))
59                         setUpComplexDiagonal();
60                 else if(TridiagonalMatrix.class.isAssignableFrom(matrixClass))
61                         setUpComplexTridiagonal();
62                 else
63                         setUpComplexRectangular();
64                 super.setUp();
65         }
66         private void setUpDoubleRectangular() {
67                 for(int i=0;i<N;i++) {
68                         for(int j=0;j<N;j++) {
69                                 dArray[i][j]=ExtraMath.random(-1.0, 1.0);
70                         }
71                 }
72         }
73         private void setUpDoubleTridiagonal() {
74                 for(int i=0;i<N;i++) {
75                         for(int j=0;j<N;j++) {
76                                 if(j>=i-1 && j<=i+1) {
77                                         dArray[i][j]=ExtraMath.random(-1.0, 1.0);
78                                 }
79                         }
80                 }
81         }
82         private void setUpDoubleDiagonal() {
83                 for(int i=0;i<N;i++) {
84                         dArray[i][i]=ExtraMath.random(-1.0, 1.0);
85                 }
86         }
87         private void setUpComplexRectangular() {
88                 for(int i=0;i<N;i++) {
89                         for(int j=0;j<N;j++) {
90                                 zArray[i][j]=new Complex(dArray[i][j], ExtraMath.random(-1.0, 1.0));
91                         }
92                 }
93         }
94         private void setUpComplexTridiagonal() {
95                 for(int i=0;i<N;i++) {
96                         for(int j=0;j<N;j++) {
97                                 if(j>=i-1 && j<=i+1) {
98                                         zArray[i][j]=new Complex(dArray[i][j], ExtraMath.random(-1.0, 1.0));
99                                 }
100                         }
101                 }
102         }
103         private void setUpComplexDiagonal() {
104                 for(int i=0;i<N;i++) {
105                         zArray[i][i]=new Complex(dArray[i][i], ExtraMath.random(-1.0, 1.0));
106                 }
107         }
108
109         protected AbstractDoubleSquareMatrix createDoubleInstance() {
110                 try {
111                         return (AbstractDoubleSquareMatrix) dConstructor.newInstance(new Object JavaDoc[] {dArray});
112                 } catch(Exception JavaDoc e) {
113                         throw new RuntimeException JavaDoc(e);
114                 }
115         }
116         protected AbstractComplexSquareMatrix createComplexInstance() {
117                 try {
118                         return (AbstractComplexSquareMatrix) zConstructor.newInstance(new Object JavaDoc[] {zArray});
119                 } catch(Exception JavaDoc e) {
120                         throw new RuntimeException JavaDoc(e);
121                 }
122         }
123         
124         
125         
126         public void testLU_pivot() {
127                 AbstractDoubleSquareMatrix mat= createDoubleInstance();
128                 int p[]=new int[N+1];
129                 AbstractDoubleSquareMatrix luArray[]=mat.luDecompose(p);
130                 AbstractDoubleSquareMatrix lu=luArray[0].multiply(luArray[1]);
131                 double pmatArray[][]=new double[N][N];
132                 for(int j,i=0;i<N;i++) {
133                         for(j=0;j<N;j++)
134                                 pmatArray[i][j]=mat.getElement(p[i],j);
135                 }
136                 DoubleSquareMatrix pmat=new DoubleSquareMatrix(pmatArray);
137                 assertEquals(pmat, lu);
138         }
139         public void testLU() {
140                 AbstractDoubleSquareMatrix mat= createDoubleInstance();
141                 AbstractDoubleSquareMatrix luArray[]=mat.luDecompose();
142                 AbstractDoubleSquareMatrix lu=luArray[0].multiply(luArray[1]);
143                 assertEquals(lu, mat); // square.equals(subclass)
144
}
145         public void testCholesky() {
146                 AbstractDoubleSquareMatrix mat= createDoubleInstance();
147                 mat=(AbstractDoubleSquareMatrix)mat.multiply(mat.transpose()); // make symmetric and positive
148
AbstractDoubleSquareMatrix lu[]=mat.choleskyDecompose();
149                 assertEquals(mat, lu[0].multiply(lu[1]));
150         }
151         public void testQR() {
152                 AbstractDoubleSquareMatrix mat= createDoubleInstance();
153                 AbstractDoubleSquareMatrix qrArray[]=mat.qrDecompose();
154                 AbstractDoubleSquareMatrix qr=qrArray[0].multiply(qrArray[1]);
155                 assertEquals(qr, mat); // square.equals(subclass)
156
}
157         public void testSVD() {
158                 AbstractDoubleSquareMatrix mat= createDoubleInstance();
159                 AbstractDoubleSquareMatrix svdArray[]=mat.singularValueDecompose();
160                 AbstractDoubleSquareMatrix svd=(AbstractDoubleSquareMatrix)svdArray[0].multiply(svdArray[1]).multiply(svdArray[2].transpose());
161                 assertEquals(svd, mat); // square.equals(subclass)
162
}
163         public void testInverse() {
164                 AbstractDoubleSquareMatrix mat= createDoubleInstance();
165                 AbstractDoubleSquareMatrix inv=mat.inverse();
166                 assertEquals(mat.multiply(inv), DoubleDiagonalMatrix.identity(N)); // square.equals(subclass)
167
}
168         public void testComplexLU_pivot() {
169                 AbstractComplexSquareMatrix mat=createComplexInstance();
170                 int p[]=new int[N+1];
171                 AbstractComplexSquareMatrix luArray[]=mat.luDecompose(p);
172                 AbstractComplexSquareMatrix lu=luArray[0].multiply(luArray[1]);
173                 Complex pmatArray[][]=new Complex[N][N];
174                 for(int j,i=0;i<N;i++) {
175                         for(j=0;j<N;j++)
176                                 pmatArray[i][j]=mat.getElement(p[i],j);
177                 }
178                 ComplexSquareMatrix pmat=new ComplexSquareMatrix(pmatArray);
179                 assertEquals(pmat, lu);
180         }
181         public void testComplexLU() {
182                 AbstractComplexSquareMatrix mat=createComplexInstance();
183                 AbstractComplexSquareMatrix luArray[]=mat.luDecompose();
184                 AbstractComplexSquareMatrix lu=luArray[0].multiply(luArray[1]);
185                 assertEquals(lu, mat); // square.equals(subclass)
186
}
187         public void testComplexInverse() {
188                 AbstractComplexSquareMatrix mat=createComplexInstance();
189                 AbstractComplexSquareMatrix inv=mat.inverse();
190                 assertEquals(mat.multiply(inv), ComplexDiagonalMatrix.identity(N)); // square.equals(subclass)
191
}
192 }
193
Popular Tags