ViennaCL - The Vienna Computing Library  1.7.0
Free open-source GPU-accelerated linear algebra and solver library.
power_iter.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_POWER_ITER_HPP_
2 #define VIENNACL_LINALG_POWER_ITER_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2015, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8  Portions of this software are copyright by UChicago Argonne, LLC.
9 
10  -----------------
11  ViennaCL - The Vienna Computing Library
12  -----------------
13 
14  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15 
16  (A list of authors and contributors can be found in the manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
27 #include <cmath>
28 #include <vector>
30 #include "viennacl/linalg/prod.hpp"
32 
33 namespace viennacl
34 {
35  namespace linalg
36  {
39  {
40  public:
41 
47  power_iter_tag(double tfac = 1e-8, vcl_size_t max_iters = 50000) : termination_factor_(tfac), max_iterations_(max_iters) {}
48 
50  void factor(double fct){ termination_factor_ = fct; }
51 
53  double factor() const { return termination_factor_; }
54 
55  vcl_size_t max_iterations() const { return max_iterations_; }
56  void max_iterations(vcl_size_t new_max) { max_iterations_ = new_max; }
57 
58  private:
59  double termination_factor_;
60  vcl_size_t max_iterations_;
61 
62  };
63 
72  template<typename MatrixT, typename VectorT >
74  eig(MatrixT const& A, power_iter_tag const & tag, VectorT & eigenvec)
75  {
76 
78  typedef typename viennacl::result_of::cpu_value_type<ScalarType>::type CPU_ScalarType;
79 
80  vcl_size_t matrix_size = A.size1();
81  VectorT r(eigenvec);
82  std::vector<CPU_ScalarType> s(matrix_size);
83 
84  for (vcl_size_t i=0; i<s.size(); ++i)
85  s[i] = CPU_ScalarType(i % 3) * CPU_ScalarType(0.1234) - CPU_ScalarType(0.5); //'random' starting vector
86 
87  detail::copy_vec_to_vec(s, eigenvec);
88 
89  double epsilon = tag.factor();
90  CPU_ScalarType norm = norm_2(eigenvec);
91  CPU_ScalarType norm_prev = 0;
92  long numiter = 0;
93 
94  for (vcl_size_t i=0; i<tag.max_iterations(); ++i)
95  {
96  if (std::fabs(norm - norm_prev) / std::fabs(norm) < epsilon)
97  break;
98 
99  eigenvec /= norm;
100  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
101  eigenvec = r;
102  norm_prev = norm;
103  norm = norm_2(eigenvec);
104  numiter++;
105  }
106 
107  return norm;
108  }
109 
117  template< typename MatrixT >
119  eig(MatrixT const& A, power_iter_tag const & tag)
120  {
121  typedef typename viennacl::result_of::vector_for_matrix<MatrixT>::type VectorT;
122 
123  VectorT eigenvec(A.size1());
124  return eig(A, tag, eigenvec);
125  }
126 
127  } // end namespace linalg
128 } // end namespace viennacl
129 #endif
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:96
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
Generic interface for the l^2-norm. See viennacl/linalg/vector_operations.hpp for implementations...
power_iter_tag(double tfac=1e-8, vcl_size_t max_iters=50000)
The constructor.
Definition: power_iter.hpp:47
Generic interface for matrix-vector and matrix-matrix products. See viennacl/linalg/vector_operations...
void factor(double fct)
Sets the factor for termination.
Definition: power_iter.hpp:50
double factor() const
Returns the factor for termination.
Definition: power_iter.hpp:53
A tag for the power iteration algorithm.
Definition: power_iter.hpp:38
vcl_size_t max_iterations() const
Definition: power_iter.hpp:55
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:34
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:102
void copy_vec_to_vec(viennacl::vector< NumericT > const &src, OtherVectorT &dest)
overloaded function for copying vectors
Definition: bisect.hpp:44
std::size_t vcl_size_t
Definition: forwards.h:75
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:271
float ScalarType
Definition: fft_1d.cpp:42
void max_iterations(vcl_size_t new_max)
Definition: power_iter.hpp:56
Implementation of the algorithm for finding eigenvalues of a tridiagonal matrix.