KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > tests > ComplexMatrixEigenTest


1 package JSci.tests;
2
3 import JSci.maths.*;
4 import JSci.maths.matrices.*;
5 import JSci.maths.vectors.AbstractComplexVector;
6 import JSci.maths.vectors.ComplexVector;
7 import JSci.util.MatrixToolkit;
8
9 /**
10 * Testcase for complex eigenvalue/vector methods.
11 * @author Mark Hale
12 */

13 public class ComplexMatrixEigenTest extends junit.framework.TestCase {
14         private final int N=5;
15         private AbstractComplexSquareMatrix sqmat;
16         private double eval[];
17         private AbstractComplexVector evec[];
18
19         public static void main(String JavaDoc arg[]) {
20                 junit.textui.TestRunner.run(ComplexMatrixEigenTest.class);
21         }
22         public ComplexMatrixEigenTest(String JavaDoc name) {
23                 super(name);
24         }
25         protected void setUp() {
26                 JSci.GlobalSettings.ZERO_TOL=1.0e-6;
27                 sqmat=MatrixToolkit.randomComplexSquareMatrix(N);
28                 sqmat=(AbstractComplexSquareMatrix) sqmat.add(sqmat.hermitianAdjoint()); // make hermitian
29
eval=new double[N];
30                 evec=new ComplexVector[N];
31                 try {
32                         eval=LinearMath.eigenSolveHermitian(sqmat,evec);
33                 } catch(MaximumIterationsExceededException e) {fail(e.toString());}
34         }
35         public void testEigenvectors() {
36                 for(int i=0;i<N;i++)
37                         assertEquals(sqmat.multiply(evec[i]), evec[i].scalarMultiply(eval[i]));
38         }
39         public void testTrace() {
40                 double tr=0.0;
41                 for(int i=0;i<N;i++)
42                         tr+=eval[i];
43                 assertEquals(sqmat.trace(), new Complex(tr,0.0));
44         }
45         public void testDet() {
46                 double det=1.0;
47                 for(int i=0;i<N;i++)
48                         det*=eval[i];
49                 assertEquals(sqmat.det(), new Complex(det,0.0));
50         }
51 }
52
53
Popular Tags