All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
power_iter.hpp
Go to the documentation of this file.
#ifndef VIENNACL_LINALG_POWER_ITER_HPP_
#define VIENNACL_LINALG_POWER_ITER_HPP_
/* =========================================================================
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 manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <cmath>
#include <vector>
#include "viennacl/linalg/bisect.hpp"
#include "viennacl/linalg/prod.hpp"
#include "viennacl/linalg/norm_2.hpp"
namespace viennacl
{
namespace linalg
{
class power_iter_tag
{
public:
power_iter_tag(double tfac = 1e-8, vcl_size_t max_iters = 50000) : termination_factor_(tfac), max_iterations_(max_iters) {}
void factor(double fct){ termination_factor_ = fct; }
double factor() const { return termination_factor_; }
vcl_size_t max_iterations() const { return max_iterations_; }
void max_iterations(vcl_size_t new_max) { max_iterations_ = new_max; }
private:
double termination_factor_;
vcl_size_t max_iterations_;
};
template<typename MatrixT, typename VectorT >
typename viennacl::result_of::cpu_value_type<typename MatrixT::value_type>::type
eig(MatrixT const& A, power_iter_tag const & tag, VectorT & eigenvec)
{
typedef typename viennacl::result_of::value_type<MatrixT>::type ScalarType;
typedef typename viennacl::result_of::cpu_value_type<ScalarType>::type CPU_ScalarType;
vcl_size_t matrix_size = A.size1();
VectorT r(eigenvec);
std::vector<CPU_ScalarType> s(matrix_size);
for (vcl_size_t i=0; i<s.size(); ++i)
s[i] = CPU_ScalarType(i % 3) * CPU_ScalarType(0.1234) - CPU_ScalarType(0.5); //'random' starting vector
detail::copy_vec_to_vec(s, eigenvec);
double epsilon = tag.factor();
CPU_ScalarType norm = norm_2(eigenvec);
CPU_ScalarType norm_prev = 0;
long numiter = 0;
for (vcl_size_t i=0; i<tag.max_iterations(); ++i)
{
if (std::fabs(norm - norm_prev) / std::fabs(norm) < epsilon)
break;
eigenvec /= norm;
r = viennacl::linalg::prod(A, eigenvec); //using helper vector r for the computation of x <- A * x in order to avoid the repeated creation of temporaries
eigenvec = r;
norm_prev = norm;
norm = norm_2(eigenvec);
numiter++;
}
return norm;
}
template< typename MatrixT >
typename viennacl::result_of::cpu_value_type<typename MatrixT::value_type>::type
eig(MatrixT const& A, power_iter_tag const & tag)
{
typedef typename viennacl::result_of::vector_for_matrix<MatrixT>::type VectorT;
VectorT eigenvec(A.size1());
return eig(A, tag, eigenvec);
}
} // end namespace linalg
} // end namespace viennacl
#endif
viennacl::linalg::norm_2
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
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
norm_2.hpp
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
viennacl::linalg::power_iter_tag::power_iter_tag
power_iter_tag(double tfac=1e-8, vcl_size_t max_iters=50000)
The constructor.
Definition: power_iter.hpp:47
prod.hpp
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
viennacl::linalg::power_iter_tag::factor
void factor(double fct)
Sets the factor for termination.
Definition: power_iter.hpp:50
viennacl::result_of::value_type::type
T::value_type type
Definition: result_of.hpp:213
viennacl::linalg::power_iter_tag::factor
double factor() const
Returns the factor for termination.
Definition: power_iter.hpp:53
viennacl::linalg::power_iter_tag
A tag for the power iteration algorithm.
Definition: power_iter.hpp:38
viennacl::linalg::power_iter_tag::max_iterations
vcl_size_t max_iterations() const
Definition: power_iter.hpp:55
viennacl::linalg::prod
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
viennacl::linalg::detail::copy_vec_to_vec
void copy_vec_to_vec(viennacl::vector< NumericT > const &src, OtherVectorT &dest)
overloaded function for copying vectors
Definition: bisect.hpp:44
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::result_of::cpu_value_type::type
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
ScalarType
float ScalarType
Definition: fft_1d.cpp:42
viennacl::linalg::power_iter_tag::max_iterations
void max_iterations(vcl_size_t new_max)
Definition: power_iter.hpp:56
bisect.hpp
Implementation of the algorithm for finding eigenvalues of a tridiagonal matrix.