All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lanczos.cpp
Go to the documentation of this file.
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the PDF manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
// include necessary system headers
#include <iostream>
//include basic scalar and vector types of ViennaCL
#include "viennacl/scalar.hpp"
#include "viennacl/vector.hpp"
#include "viennacl/matrix.hpp"
#include "viennacl/matrix_proxy.hpp"
#include "viennacl/compressed_matrix.hpp"
#include "viennacl/linalg/lanczos.hpp"
#include "viennacl/io/matrix_market.hpp"
// Some helper functions for this tutorial:
#include <iostream>
#include <string>
#include <iomanip>
int main()
{
// If you GPU does not support double precision, use `float` instead of `double`:
typedef double ScalarType;
std::vector< std::map<unsigned int, ScalarType> > host_A;
if (!viennacl::io::read_matrix_market_file(host_A, "../examples/testdata/mat65k.mtx"))
{
std::cout << "Error reading Matrix file" << std::endl;
return EXIT_FAILURE;
}
viennacl::compressed_matrix<ScalarType> A;
viennacl::copy(host_A, A);
viennacl::linalg::lanczos_tag ltag(0.75, // Select a power of 0.75 as the tolerance for the machine precision.
10, // Compute (approximations to) the 10 largest eigenvalues
viennacl::linalg::lanczos_tag::partial_reorthogonalization, // use partial reorthogonalization
30); // Maximum size of the Krylov space
std::cout << "Running Lanczos algorithm (eigenvalues only). This might take a while..." << std::endl;
std::vector<ScalarType> lanczos_eigenvalues = viennacl::linalg::eig(A, ltag);
std::cout << "Running Lanczos algorithm (with eigenvectors). This might take a while..." << std::endl;
viennacl::matrix<ScalarType> approx_eigenvectors_A(A.size1(), ltag.num_eigenvalues());
lanczos_eigenvalues = viennacl::linalg::eig(A, approx_eigenvectors_A, ltag);
for (std::size_t i = 0; i< lanczos_eigenvalues.size(); i++)
{
std::cout << "Approx. eigenvalue " << std::setprecision(7) << lanczos_eigenvalues[i];
// test approximated eigenvector by computing A*v:
viennacl::vector<ScalarType> approx_eigenvector = viennacl::column(approx_eigenvectors_A, static_cast<unsigned int>(i));
viennacl::vector<ScalarType> Aq = viennacl::linalg::prod(A, approx_eigenvector);
std::cout << " (" << viennacl::linalg::inner_prod(Aq, approx_eigenvector) << " for <Av,v> with approx. eigenvector v)" << std::endl;
}
return EXIT_SUCCESS;
}
matrix_market.hpp
A reader and writer for the matrix market format is implemented here.
viennacl::linalg::eig
std::vector< typename viennacl::result_of::cpu_value_type< typename MatrixT::value_type >::type > eig(MatrixT const &matrix, DenseMatrixT &eigenvectors_A, lanczos_tag const &tag, bool compute_eigenvectors=true)
Implementation of the calculation of eigenvalues using lanczos (with and without reorthogonalization)...
Definition: lanczos.hpp:452
viennacl::compressed_matrix::size1
const vcl_size_t & size1() const
Returns the number of rows.
Definition: compressed_matrix.hpp:927
matrix.hpp
Implementation of the dense matrix class.
main
int main()
Definition: bisect.cpp:91
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::linalg::inner_prod
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:100
viennacl::linalg::lanczos_tag::partial_reorthogonalization
Definition: lanczos.hpp:51
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
compressed_matrix.hpp
Implementation of the compressed_matrix class.
viennacl::vector< ScalarType >
matrix_proxy.hpp
Proxy classes for matrices.
vector.hpp
The vector type with operator-overloads and proxy classes is defined here. Linear algebra operations ...
viennacl::copy
void copy(std::vector< NumericT > &cpu_vec, circulant_matrix< NumericT, AlignmentV > &gpu_mat)
Copies a circulant matrix from the std::vector to the OpenCL device (either GPU or multi-core CPU) ...
Definition: circulant_matrix.hpp:150
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::compressed_matrix
A sparse square matrix in compressed sparse rows format.
Definition: compressed_matrix.hpp:559
viennacl::column
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_column > column(const matrix_base< NumericT, F > &A, unsigned int j)
Definition: matrix.hpp:918
viennacl::io::read_matrix_market_file
long read_matrix_market_file(MatrixT &mat, const char *file, long index_base=1)
Reads a sparse matrix from a file (MatrixMarket format)
Definition: matrix_market.hpp:339
scalar.hpp
Implementation of the ViennaCL scalar class.
viennacl::linalg::lanczos_tag
A tag for the lanczos algorithm.
Definition: lanczos.hpp:45
lanczos.hpp
Generic interface for the Lanczos algorithm.