This tutorial explains how one can use the tql-algorithm to compute the eigenvalues of tridiagonal matrices.
We start with including the necessary headers:
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#include <iomanip>
#define VIENNACL_WITH_UBLAS
namespace ublas = boost::numeric::ublas;
We generate a symmetric tridiagonal matrix with known eigenvalues, call the tql-algorithm, and then print the results.
{
std::size_t sz = 10;
std::cout << "Compute eigenvalues and eigenvectors of matrix of size " << sz << "-by-" << sz << std::endl << std::endl;
std::vector<ScalarType> d(sz), e(sz);
d[0] = 1; e[0] = 0;
d[1] = 2; e[1] = 4;
d[2] =-4; e[2] = 5;
d[3] = 6; e[3] = 1;
d[4] = 3; e[4] = 2;
d[5] = 4; e[5] =-3;
d[6] = 7; e[6] = 5;
d[7] = 9; e[7] = 1;
d[8] = 3; e[8] = 5;
d[9] = 8; e[9] = 2;
Initialize the matrix Q as the identity matrix. It will hold the eigenvectors.
Compute the eigenvalues and eigenvectors
Print the results:
std::cout << "Eigenvalues: " << std::endl;
for (unsigned int i = 0; i < d.size(); i++)
std::cout << std::setprecision(6) << std::fixed << d[i] << " ";
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Eigenvectors corresponding to the eigenvalues above are the columns: " << std::endl << std::endl;
std::cout << Q << std::endl;
That's it. Print success message and exit.
std::cout << std::endl <<"--------TUTORIAL COMPLETED----------" << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#include <iomanip>
#define VIENNACL_WITH_UBLAS
namespace ublas = boost::numeric::ublas;
{
std::size_t sz = 10;
std::cout << "Compute eigenvalues and eigenvectors of matrix of size " << sz << "-by-" << sz << std::endl << std::endl;
std::vector<ScalarType> d(sz), e(sz);
d[0] = 1; e[0] = 0;
d[1] = 2; e[1] = 4;
d[2] =-4; e[2] = 5;
d[3] = 6; e[3] = 1;
d[4] = 3; e[4] = 2;
d[5] = 4; e[5] =-3;
d[6] = 7; e[6] = 5;
d[7] = 9; e[7] = 1;
d[8] = 3; e[8] = 5;
d[9] = 8; e[9] = 2;
std::cout << "Eigenvalues: " << std::endl;
for (unsigned int i = 0; i < d.size(); i++)
std::cout << std::setprecision(6) << std::fixed << d[i] << " ";
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Eigenvectors corresponding to the eigenvalues above are the columns: " << std::endl << std::endl;
std::cout << Q << std::endl;
std::cout << std::endl <<"--------TUTORIAL COMPLETED----------" << std::endl;
return EXIT_SUCCESS;
}