KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > tests > MatrixTest


1 package JSci.tests;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import junit.framework.*;
7 import JSci.GlobalSettings;
8 import JSci.maths.*;
9 import JSci.maths.matrices.*;
10 import JSci.maths.vectors.*;
11 import JSci.maths.polynomials.*;
12
13 /**
14 * Testcase for matrices.
15 * @author Mark Hale
16 */

17 public class MatrixTest extends junitx.extensions.EqualsHashCodeTestCase {
18         private final int N = 10;
19         private final int M = 10;
20         private Constructor JavaDoc constructor;
21         private double array[][];
22         private double array2[][];
23
24         public static void main(String JavaDoc arg[]) {
25                 junit.textui.TestRunner.run(suite());
26         }
27         public static Test suite() {
28                 Class JavaDoc[] classes = new Class JavaDoc[] {
29                         DoubleMatrix.class, DoubleTridiagonalMatrix.class,
30                         DoubleDiagonalMatrix.class, DoubleSparseMatrix.class
31                 };
32                 TestSuite suite = new TestSuite(MatrixTest.class.toString());
33                 for(int i=0; i<classes.length; i++) {
34                         Map JavaDoc properties = new HashMap JavaDoc();
35                         properties.put("test.matrix.class", classes[i]);
36                         suite.addTest(new junit.extensions.RepeatedTest(new TestProperties(new TestSuite(MatrixTest.class, classes[i].toString()), properties), 5));
37                 }
38                 return suite;
39         }
40         public MatrixTest(String JavaDoc name) {
41                 super(name);
42         }
43         protected void setUp() throws Exception JavaDoc {
44                 GlobalSettings.ZERO_TOL=1.0e-6;
45                 array=new double[N][M];
46                 array2=new double[N][M];
47                 Class JavaDoc matrixClass = (Class JavaDoc) TestProperties.getProperties().get("test.matrix.class");
48                 constructor = matrixClass.getConstructor(new Class JavaDoc[] {double[][].class});
49                 if(DiagonalMatrix.class.isAssignableFrom(matrixClass))
50                         setUpDiagonal();
51                 else if(TridiagonalMatrix.class.isAssignableFrom(matrixClass))
52                         setUpTridiagonal();
53                 else
54                         setUpRectangular();
55                 super.setUp();
56         }
57         private void setUpRectangular() {
58                 for(int i=0;i<N;i++) {
59                         for(int j=0;j<M;j++) {
60                                 array[i][j]=ExtraMath.random(-1.0, 1.0);
61                                 array2[i][j]=ExtraMath.random(-1.0, 1.0);
62                         }
63                 }
64         }
65         private void setUpTridiagonal() {
66                 for(int i=0;i<N;i++) {
67                         for(int j=0;j<M;j++) {
68                                 if(j>=i-1 && j<=i+1) {
69                                         array[i][j]=ExtraMath.random(-1.0, 1.0);
70                                         array2[i][j]=ExtraMath.random(-1.0, 1.0);
71                                 }
72                         }
73                 }
74         }
75         private void setUpDiagonal() {
76                 for(int i=0;i<N;i++) {
77                         array[i][i]=ExtraMath.random(-1.0, 1.0);
78                         array2[i][i]=ExtraMath.random(-1.0, 1.0);
79                 }
80         }
81         protected Object JavaDoc createInstance() {
82                 try {
83                         return constructor.newInstance(new Object JavaDoc[] {array});
84                 } catch(Exception JavaDoc e) {
85                         throw new RuntimeException JavaDoc(e);
86                 }
87         }
88         protected Object JavaDoc createNotEqualInstance() {
89                 try {
90                         return constructor.newInstance(new Object JavaDoc[] {array2});
91                 } catch(Exception JavaDoc e) {
92                         throw new RuntimeException JavaDoc(e);
93                 }
94         }
95         public void testConstructor() {
96                 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
97                 assertEquals(N, mat.rows());
98                 assertEquals(M, mat.columns());
99                 for(int i=0;i<mat.rows();i++) {
100                         for(int j=0;j<mat.columns();j++)
101                                 assertEquals(array[i][j], mat.getElement(i,j), GlobalSettings.ZERO_TOL);
102                 }
103         }
104         public void testSetGet() {
105                 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
106                 for(int i=0;i<mat.rows();i++) {
107                         for(int j=0;j<mat.columns();j++) {
108                                 try {
109                                         mat.setElement(i,j,-1.0);
110                                         assertEquals(-1.0, mat.getElement(i,j), GlobalSettings.ZERO_TOL);
111                                         mat.setElement(i,j,array[i][j]);
112                                 } catch(MatrixDimensionException e) {
113                     // ignore
114
}
115                         }
116                 }
117         }
118         public void testZeroTolerance() {
119                 AbstractDoubleMatrix delta = (AbstractDoubleMatrix) createInstance();
120                 delta = delta.scalarMultiply(Math.random()*GlobalSettings.ZERO_TOL/delta.frobeniusNorm());
121                 AbstractDoubleMatrix m = (AbstractDoubleMatrix) createNotEqualInstance();
122                 assertTrue("m == m + delta", m.equals(m.add(delta)));
123         }
124         public void testAdd() {
125                 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
126                 AbstractDoubleMatrix ans=mat.add(mat);
127                 for(int i=0;i<ans.rows();i++) {
128                         for(int j=0;j<ans.columns();j++) {
129                 String JavaDoc msg = mat.getClass().toString()+": ("+i+","+j+")";
130                                 assertEquals(msg, array[i][j]+array[i][j], ans.getElement(i,j), GlobalSettings.ZERO_TOL);
131             }
132                 }
133         }
134         public void testSubtract() {
135                 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
136                 AbstractDoubleMatrix ans=mat.subtract(mat);
137                 for(int i=0;i<ans.rows();i++) {
138                         for(int j=0;j<ans.columns();j++) {
139                 String JavaDoc msg = mat.getClass().toString()+": ("+i+","+j+")";
140                                 assertEquals(msg, array[i][j]-array[i][j], ans.getElement(i,j), GlobalSettings.ZERO_TOL);
141             }
142                 }
143         }
144         public void testMultiply() {
145                 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
146                 AbstractDoubleMatrix ans=(AbstractDoubleMatrix)mat.multiply(mat.transpose());
147                 for(int i=0;i<ans.rows();i++) {
148                         for(int j=0;j<ans.columns();j++) {
149                                 double sum=0.0;
150                                 for(int k=0;k<mat.columns();k++)
151                                         sum+=array[i][k]*array[j][k];
152                 String JavaDoc msg = mat.getClass().toString()+": ("+i+","+j+")";
153                                 assertEquals(msg, sum, ans.getElement(i,j), GlobalSettings.ZERO_TOL);
154                         }
155                 }
156         }
157     public void testMapElements() {
158                 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
159         RealPolynomial map = new RealPolynomial(new double[] {2.0, 1.0});
160         AbstractDoubleMatrix ans = mat.mapElements(map);
161                 for(int i=0;i<ans.rows();i++) {
162                         for(int j=0;j<ans.columns();j++) {
163                 String JavaDoc msg = mat.getClass().toString()+": ("+i+","+j+")";
164                                 assertEquals(msg, map.map(array[i][j]), ans.getElement(i,j), GlobalSettings.ZERO_TOL);
165             }
166                 }
167     }
168         public void testGMRes() {
169                 AbstractDoubleMatrix mat = (AbstractDoubleMatrix) createInstance();
170                 AbstractDoubleVector y = JSci.util.VectorToolkit.randomVector(mat.rows());
171                 try {
172                         AbstractDoubleVector x = LinearMath.solveGMRes(mat, y, 100, GlobalSettings.ZERO_TOL);
173                         assertEquals(y, mat.multiply(x));
174                 } catch(MaximumIterationsExceededException e) {
175                         throw new RuntimeException JavaDoc(e);
176                 }
177         }
178 }
179
180
Popular Tags