# EIGEN VALUES OF A GIVEN MATRIX

write a c program to find eigen values of a given matrix?

// Test …/include/LinAlg/UpperHessenbergEigen.h and
// …/include/LinAlg/TridiagEigen.h
#include <LinAlg/UpperHessenbergEigen.h>
#include <LinAlg/TridiagEigen.h>
#include <Eigen/Eigenvalues>
#include

using namespace Spectra;

#define CATCH_CONFIG_MAIN
#include “catch.hpp”

using Eigen::MatrixXd;
using Eigen::VectorXd;
using Eigen::MatrixXcd;
using Eigen::VectorXcd;

TEST_CASE(“Eigen decomposition of upper Hessenberg matrix”, “[Eigen]”)
{
std::srand(123);
int n = 100;
MatrixXd m = MatrixXd::Random(n, n);
m.array() -= 0.5;
MatrixXd H = m.triangularViewEigen::Upper();
H.diagonal(-1) = m.diagonal(-1);

``````UpperHessenbergEigen<double> decomp(H);
VectorXcd evals = decomp.eigenvalues();
MatrixXcd evecs = decomp.eigenvectors();

MatrixXcd err = H * evecs - evecs * evals.asDiagonal();

INFO( "||HU - UD||_inf = " << err.cwiseAbs().maxCoeff() );
REQUIRE( err.cwiseAbs().maxCoeff() == Approx(0.0) );

clock_t t1, t2;
t1 = clock();
for(int i = 0; i < 100; i++)
{
UpperHessenbergEigen<double> decomp(H);
VectorXcd evals = decomp.eigenvalues();
MatrixXcd evecs = decomp.eigenvectors();
}
t2 = clock();
std::cout << "elapsed time for UpperHessenbergEigen: "
<< double(t2 - t1) / CLOCKS_PER_SEC << " secs\n";

t1 = clock();
for(int i = 0; i < 100; i++)
{
Eigen::EigenSolver<MatrixXd> decomp(H);
VectorXcd evals = decomp.eigenvalues();
MatrixXcd evecs = decomp.eigenvectors();
}
t2 = clock();
std::cout << "elapsed time for Eigen::EigenSolver: "
<< double(t2 - t1) / CLOCKS_PER_SEC << " secs\n";
``````

}

TEST_CASE(“Eigen decomposition of symmetric tridiagonal matrix”, “[Eigen]”)
{
std::srand(123);
int n = 100;
MatrixXd m = MatrixXd::Random(n, n);
m.array() -= 0.5;
MatrixXd H = MatrixXd::Zero(n, n);
H.diagonal() = m.diagonal();
H.diagonal(-1) = m.diagonal(-1);
H.diagonal(1) = m.diagonal(-1);

``````TridiagEigen<double> decomp(H);
VectorXd evals = decomp.eigenvalues();
MatrixXd evecs = decomp.eigenvectors();

MatrixXd err = H * evecs - evecs * evals.asDiagonal();

INFO( "||HU - UD||_inf = " << err.cwiseAbs().maxCoeff() );
REQUIRE( err.cwiseAbs().maxCoeff() == Approx(0.0) );

clock_t t1, t2;
t1 = clock();
for(int i = 0; i < 100; i++)
{
TridiagEigen<double> decomp(H);
VectorXd evals = decomp.eigenvalues();
MatrixXd evecs = decomp.eigenvectors();
}
t2 = clock();
std::cout << "elapsed time for TridiagEigen: "
<< double(t2 - t1) / CLOCKS_PER_SEC << " secs\n";

t1 = clock();
for(int i = 0; i < 100; i++)
{
VectorXd evals = decomp.eigenvalues();
MatrixXd evecs = decomp.eigenvectors();
}
t2 = clock();
std::cout << "elapsed time for Eigen::SelfAdjointEigenSolver: "
<< double(t2 - t1) / CLOCKS_PER_SEC << " secs\n";
``````

}

.h#include
.h#include
.h#include
using namespace std;
int main()
{
cout.precision(2);
cout.setf(ios::fixed);
int n,i,j;
cout<<"\nEnter the order of the matrix:\n"; //Get the order of the square matrix
cin>>n;
double a[n][n],b[n],c[n],k,eps,y; //declare arrays and other variables to be used
cout<<"\nEnter the elements of matrix row-wise:\n";
for (i=0;i<n;i++) //Get the elements of the matrix
for (j=0;j<n;j++)
cin>>a[i][j];
cout<<"\nEnter the initial values of the eigen-vector:\n";
for (i=0;i<n;i++) //Get the iinitial guess for the eigen vector
cin>>b[i];
cout<<"\nEnter the accuracy desired:\n";
cin>>eps;
k=b[0]; //Assign some initial value to the eigen value, ‘k’
do
{
y=k;
for (i=0;i<n;i++) //Calculate axb where a is the matrix and b its eigen vector
{
c[i]=0;
for (j=0;j<n;j++)
c[i]=c[i]+a[i][j]*b[j]; //After all the iterations axb=c
}
k=abs(c[0]);
for (i=1;i<n;i++)
k=abs(c[i])>k?abs(c[i]):k; //Find the largest element of c and assign it to k where k is the eigen value
for (i=0;i<n;i++)
b[i]=c[i]/k; //Calculate the new Eigen Vector
}while (abs(k-y)>eps); //Check if the error in eigen value is within the tolerance limit
cout<<“The eigen-value is: “<<k<<endl;
cout<<”\nAnd the Eigen-vector is [”;
for (i=0;i<n;i++)
cout<<b[i]<<setw(9);
cout<<"]\n";
return 0;
}