package JSci.tests;

import JSci.maths.*;
import JSci.util.MatrixToolkit;

/**
* Testcase for double eigenvalue/vector methods.
* @author Mark Hale
*/
public class DoubleMatrixEigenTest extends junit.framework.TestCase {
        private final int N=5;
        private DoubleSquareMatrix sqmat;
        private double eval[];
        private DoubleVector evec[];

        public static void main(String arg[]) {
                junit.textui.TestRunner.run(DoubleMatrixEigenTest.class);
        }
        public DoubleMatrixEigenTest(String name) {
                super(name);
        }
        protected void setUp() {
                JSci.GlobalSettings.ZERO_TOL=1.0e-6;
                sqmat=MatrixToolkit.randomSquareMatrix(N);
                sqmat=(DoubleSquareMatrix) sqmat.add(sqmat.transpose());    // make symmetric
                eval=new double[N];
                evec=new DoubleVector[N];
                try {
                        eval=LinearMath.eigenSolveSymmetric(sqmat,evec);
                } catch(MaximumIterationsExceededException e) {fail(e.toString());}
        }
        public void testEigenvectors() {
                for(int i=0;i<N;i++)
                        assertEquals(sqmat.multiply(evec[i]), evec[i].scalarMultiply(eval[i]));
        }
        public void testTrace() {
                double tr=0.0;
                for(int i=0;i<N;i++)
                        tr+=eval[i];
                assertEquals(sqmat.trace(), tr, JSci.GlobalSettings.ZERO_TOL);
        }
        public void testDet() {
                double det=1.0;
                for(int i=0;i<N;i++)
                        det*=eval[i];
                assertEquals(sqmat.det(), det, JSci.GlobalSettings.ZERO_TOL);
        }
}

