/*This file Eigen.h creates a class template Eigen.  This template will include
data members to store the eigenvalues and eigenvectors of some input square
matrix.  The data members of this class will be the input matrix, a matrix to
stor the eigenvectors, a vector to store the eigenvalues, and a vector which
tells the user how many rotations were needed for the calculation.  This class
is using the jacobi routine to compute the eigenvalues and eigenvectors for the
input matrix.*/

#ifndef Eigen_H
#define Eigen_H

#include <math.h>
#include <stdlib.h>
#include "Matrix.h"
#include "Error.h"

template <class T> class Eigen;

template <class T>
class Eigen{
public:
    Eigen();
    Eigen(Matrix<T>& m);
    Matrix<T>& get_vectors();//gets the eigenvectors
    Vector<T>& get_values();//gets the eigenvalues
    int get_nrot(); //returns the number of jacobi rotations
private:
    Matrix<T> input;
    Matrix<T> eigen_vectors;
    Vector<T> eigen_values;
    int nrot; //stores the number of rotations by jacobi
    void jacobi();//vill find eigen-values/vectors of input matrix.
    void eigsrt(); //will sort the eigen-values/vectors found by jacobi
};

//this is the default constructor and should not be used
template <class T>
Eigen<T>::Eigen()
{
    cout <<"Calling the default constructor for Eigen"<<endl;
}

 //this constructur sets up the elements for jacobi routine
template <class T>
Eigen<T>::Eigen(Matrix<T>& m)
{
    input = m;
    eigen_vectors.setSize(m.numRows(), m.numCols());
    eigen_vectors = 0; //intialize the eige vectors to zero
    eigen_values.setSize(m.numRows());
    eigen_values=0;  //intialize the eigen values to zero
    nrot = 0;   //intialize the number of rotations to zero
    if (input.numRows() == input.numCols())
    {
        jacobi();
        eigsrt();
    }
    else
        throw InvalidSizes("NumRows","NumCols", __FILE__ , __LINE__);
}

//returns a matrix containing the eigen vectors
template <class T>
Matrix<T>& Eigen<T>::get_vectors()
{
    return (eigen_vectors);
}

//returns a vector that contains the eigen values
template <class T>
Vector<T>& Eigen<T>::get_values()
{
    return (eigen_values);
}

//this will return the number of rotations performed by jacobi
template <class T>
int Eigen<T>::get_nrot()
{
	return (nrot);
}

/*This routine will compute the eigen values and the eigen vectors of the input 
matrix.  Thie routine is more limited than I had originally thought.  The 
routine only works for symmetric matrices.  This isn't a big limitation for 
physics applications but it is a flaw. */

template <class T>
void Eigen<T>::jacobi()
{
    int n = input.numRows();  //makes it easier to set up loops
    int j, iq, ip, i;
    T thresh, theta, tau, t, sm, s, h, g, c;
    Vector<T> b(input.numRows()); 
    Vector<T> z(input.numRows());
    for (ip = 0; ip<n; ip++)
        eigen_vectors(ip,ip)=1.0;  
    for (ip = 0; ip<n; ip++)
    {
        eigen_values(ip)=input(ip,ip);
        b(ip) = eigen_values(ip);
    }
    z = 0;
    for (i= 0; i<50; i++)
    {
        sm= 0.0;
        for (ip=0; ip<=n-1; ip++){
            for (iq=ip+1; iq<n; iq++)
                sm+=fabs(input(ip,iq));
        }
        if (sm == 0)
            return;
        if ( i < 4 )
            thresh = 0.2 * sm/(n*n);
        else
            thresh = 0.0;
        for (ip =0; ip<n-1; ip++)
        {
            for (iq = ip+1; iq<n; iq++)
            {
                g = 100.0 * fabs(input(ip,iq));
                if (i > 4 && (T)(fabs(eigen_values(ip))+g) ==
                    (T)fabs(eigen_values(ip))&&(T)(fabs(eigen_values(iq))+g) ==
                    (T)fabs(eigen_values(iq)))
                        input(ip,iq)=0.0;
                else if (fabs(input(ip,iq))>thresh)
                {
                    h = eigen_values(iq) - eigen_values(ip);
                    if ((T)(fabs(h)+g) == (T)fabs(h))
                        t = input(ip,iq)/h;
                    else
                    {
                        theta = 0.5 * h/input(ip,iq);
                        t = 1.0/(fabs(theta) + sqrt(1.0+theta*theta));
                        if (theta < 0.0)
                            t = -t;
                    }
                    c = 1.0/sqrt(1.0+t*t);
                    s = t*c;
                    tau = s/(1.0+c);
                    h = t * input(ip,iq);
                    z(ip)-=h;
                    z(iq)+=h;
                    eigen_values(ip)-=h;
                    eigen_values(iq)+=h;
                    input(ip,iq)=0.0;
                    for (j=0; j<=ip-1; j++)
                    {
                        g = input(j,ip);
                        h = input(j,iq);
                        input(j,ip)=g-s*(h+g*tau);
                        input(j,iq)=h+s*(g-h*tau);   
                    }
                    for (j=ip+1;j<=iq-1;j++)
                    {
                        g = input(ip,j);
                        h = input(j,iq);
                        input(ip,j)=g-s*(h+g*tau);
                        input(j,iq)=h+s*(g-h*tau);  
                    }
                    for (j=iq+1;j<n;j++)
                    {
                        g = input(ip,j);
                        h = input(iq,j);
                        input(ip,j)=g-s*(h+g*tau);
                        input(iq,j)=h+s*(g-h*tau); 
                        
                    }
                    for (j=0; j<n; j++)
                    {
                        g = eigen_vectors(j,ip);
                        h = eigen_vectors(j,iq);
                        eigen_vectors(j,ip)=g-s*(h+g*tau);
                        eigen_vectors(j,iq)=h+s*(g-h*tau);    
                    }
                    ++(nrot);
                }
            }
        }
        for (ip=0; ip<n; ip++)
        {
            b(ip) += z(ip);
            eigen_values(ip) = b(ip);
        }
        z = 0;
    }
    throw Exception("Too many rotations with jacobi.....",__FILE__ ,__LINE__ );
}


//the following fuctions sorts the results from jacobi (from NR)
template <class T>
void Eigen<T>::eigsrt()
{
    T p;
    int i, j, k;
    int n = eigen_vectors.numRows();
    for (i = 0; i < n-1; i++)
    {
        p = eigen_values(k=i);
        for (j=i+1; j<n; j++)
            if (eigen_values(j) >= p)
                p = eigen_values(k=j);
        if (k != 1)
        {
            eigen_values(k) = eigen_values(i);
            eigen_values(i) = p;
            for (j=0; j<n; j++)
            {
                p = eigen_vectors(j,i);
                eigen_vectors(j,i) = eigen_vectors(j,k);
                eigen_vectors(j,k) = p;
            }
        }
   }
}

#endif    



