package math;

import math.matrix.EigenvalueDecomposition;
import math.matrix.Matrix;
import exception.IllegalDimensionException;

/**
 * Test client for computing eigenvalues and eigenvectors of a real
 * symmetric matrix A = V D V^T.
 */
public class Eigenvalues {
   public static void main(String[] args) throws IllegalDimensionException { 
      int N = 5;

      // create a symmetric positive definite matrix
      Matrix A = Matrix.random(N, N);
      A = A.transpose().times(A);

      // compute the spectral decomposition
      EigenvalueDecomposition e = A.eig();
      Matrix V = e.getV();
      Matrix D = e.getD();

      System.out.print("A =");
      A.print(9, 6);
      System.out.print("D =");
      D.print(9, 6);
      System.out.print("V =");
      V.print(9, 6);

      // check that V is orthogonal
      System.out.print("||V * V^T - I|| = ");
      System.out.println(V.times(V.transpose()).minus(Matrix.identity(N, N)).normInf());

      // check that A V = D V
      System.out.print("||AV - DV|| = ");
      System.out.println(A.times(V).minus(V.times(D)).normInf());
   }

}
