#ifndef EigenValuesH
#define EigenValuesH

#include "BigMatrix.h"


// this class stores the elements of a symmetric tridiagonal matrix as
// two vectors, one for the diagonal and one for the off-diagonal elements

class SymmetricTridiagonalMatrix
{
public:
	BigVector diagonal;       // matrix elements at diagonal
	BigVector offdiagonal;    // matrix elements at first off-diagonal

	SymmetricTridiagonalMatrix(BigInt n=0)
		: diagonal(n), offdiagonal(n) { }
	SymmetricTridiagonalMatrix(const SymmetricTridiagonalMatrix & tdm)
		{ diagonal = tdm.diagonal; offdiagonal = tdm.offdiagonal; }
	~SymmetricTridiagonalMatrix() { }

	const SymmetricTridiagonalMatrix & operator=
		(const SymmetricTridiagonalMatrix & tdm)
		{ diagonal = tdm.diagonal; offdiagonal = tdm.offdiagonal; return *this; }
	BigInt size() const                     // return size of matrix
		{ return diagonal.size(); }
};


// this class stores the elements of a vector in form of a list
// each entry in the list contains index and value of a vector element

class SparseVector
{
public:
	class element
	{
	public:
		BigInt index;
		BigFloat value;
	};

	BigInt length;
	BigInt count;
	element* data;

	SparseVector(BigInt);                 // create vector with certain maximum size
	~SparseVector()  { delete[] data; }

	void add(const BigInt &, const BigFloat &);   // add new entry
	void reset()  { count=0; }            // delete all entries
	void compress();                      // adds up entries with equal indices
	                                      // and sorts entries by index
};

class SparseVectorOverflow       // array not large enough to store all elements
	: public BigMatrixError {};  // this can only happen, if indices outside
                                 // the allowed range were stored in 'add'



// 'EigenValues' computes the eigenvalues of a symmetric sparse matrix
// using the functions 'Householder_SymToTridiag' and 'QL_TridiagToDiag'
// defined below.

BigVector EigenValues(const SymmetricBigMatrix &);


// 'Householder_SymToTridiag' reduces a symmetric matrix to tridiagonal form.
// It uses the Householder reduction described in:
// "Numerical Recipes in C", Second edition, Section 11.2.

SymmetricTridiagonalMatrix Householder_SymToTridiag(const SymmetricBigMatrix &);


// 'QL_TridiagToDiag' computes the eigenvalues of a symmetric tridiagonal matrix.
// It uses the QL algorithm with implicit shifts as described in:
// "Numerical Recipes in C", Second edition, Section 11.3.
// The eigenvalues are stored in a vector in order from smallest to largest.

BigVector QL_TridiagToDiag(const SymmetricTridiagonalMatrix & tdm);

class QL_NotConverging                        // the QL algorithm is not converging
	: public BigMatrixError {};

BigFloat BM_pythag(BigFloat, BigFloat);       // calculates sqrt(a^2 + b^2) without
                                              // destructive underflow and overflow
inline
BigFloat BM_sqr(BigFloat x) { return x*x; }   // the square function



#endif
