This tutorial demonstrates the calculation of the eigenvalue with largest modulus using the power iteration method.
We start with including the necessary headers:
#ifndef NDEBUG
#define BOOST_UBLAS_NDEBUG
#endif
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#define VIENNACL_WITH_UBLAS
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/matrix_expression.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
To run the power iteration, we set up a sparse matrix using Boost.uBLAS, transfer it over to a ViennaCL matrix, and then run the algorithm.
Create the sparse uBLAS matrix and read the matrix from the matrix-market file:
boost::numeric::ublas::compressed_matrix<ScalarType> ublas_A;
{
std::cout << "Error reading Matrix file" << std::endl;
return EXIT_FAILURE;
}
Transfer the data from the uBLAS matrix over to the ViennaCL sparse matrix:
Run the power iteration up until the largest eigenvalue changes by less than the specified tolerance. Print the results of running with uBLAS as well as ViennaCL and exit.
std::cout << "Starting computation of eigenvalue with largest modulus (might take about a minute)..." << std::endl;
std::cout <<
"Result of power iteration with ublas matrix (single-threaded): " <<
viennacl::linalg::eig(ublas_A, ptag) << std::endl;
std::cout <<
"Result of power iteration with ViennaCL (OpenCL accelerated): " <<
viennacl::linalg::eig(vcl_A, ptag) << std::endl;
You can also obtain the associated approximated eigenvector by passing it as a third argument to eig() Tighten the tolerance passed to ptag above in order to obtain more accurate results.
std::cout << "First three entries in eigenvector: " << eigenvector[0] << " " << eigenvector[1] << " " << eigenvector[2] << std::endl;
std::cout << "First three entries in A*eigenvector: " << Ax[0] << " " << Ax[1] << " " << Ax[2] << std::endl;
return EXIT_SUCCESS;
}
Full Example Code
#ifndef NDEBUG
#define BOOST_UBLAS_NDEBUG
#endif
#include <iostream>
#include <fstream>
#include <limits>
#include <string>
#define VIENNACL_WITH_UBLAS
#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/matrix_expression.hpp>
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/operation.hpp>
#include <boost/numeric/ublas/vector_expression.hpp>
{
boost::numeric::ublas::compressed_matrix<ScalarType> ublas_A;
{
std::cout << "Error reading Matrix file" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Starting computation of eigenvalue with largest modulus (might take about a minute)..." << std::endl;
std::cout <<
"Result of power iteration with ublas matrix (single-threaded): " <<
viennacl::linalg::eig(ublas_A, ptag) << std::endl;
std::cout <<
"Result of power iteration with ViennaCL (OpenCL accelerated): " <<
viennacl::linalg::eig(vcl_A, ptag) << std::endl;
std::cout << "First three entries in eigenvector: " << eigenvector[0] << " " << eigenvector[1] << " " << eigenvector[2] << std::endl;
std::cout << "First three entries in A*eigenvector: " << Ax[0] << " " << Ax[1] << " " << Ax[2] << std::endl;
return EXIT_SUCCESS;
}