ViennaCL - The Vienna Computing Library  1.6.2
Free open-source GPU-accelerated linear algebra and solver library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2014, 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 PDF 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 
71  template< typename MatrixT >
73  eig(MatrixT const& matrix, power_iter_tag const & tag)
74  {
75 
77  typedef typename viennacl::result_of::cpu_value_type<ScalarType>::type CPU_ScalarType;
78  typedef typename viennacl::result_of::vector_for_matrix<MatrixT>::type VectorT;
79 
80  CPU_ScalarType eigenvalue;
81  vcl_size_t matrix_size = matrix.size1();
82  VectorT r(matrix_size);
83  VectorT r2(matrix_size);
84  std::vector<CPU_ScalarType> s(matrix_size);
85 
86  for (vcl_size_t i=0; i<s.size(); ++i)
87  s[i] = CPU_ScalarType(i % 3) * CPU_ScalarType(0.1234) - CPU_ScalarType(0.5); //'random' starting vector
88 
90 
91  //std::cout << s << std::endl;
92 
93  double epsilon = tag.factor();
94  CPU_ScalarType norm = norm_2(r);
95  CPU_ScalarType norm_prev = 0;
96  long numiter = 0;
97 
98  for (vcl_size_t i=0; i<tag.max_iterations(); ++i)
99  {
100  if (std::fabs(norm - norm_prev) / std::fabs(norm) < epsilon)
101  break;
102 
103  r /= norm;
104  r2 = viennacl::linalg::prod(matrix, r); //using helper vector r2 for the computation of r <- A * r in order to avoid the repeated creation of temporaries
105  r = r2;
106  norm_prev = norm;
107  norm = norm_2(r);
108  numiter++;
109  }
110 
111  eigenvalue = norm;
112  return eigenvalue;
113  }
114 
115 
116  } // end namespace linalg
117 } // end namespace viennacl
118 #endif
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:86
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
A dense matrix class.
Definition: forwards.h:374
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
VectorT prod(std::vector< std::vector< T, A1 >, A2 > const &matrix, VectorT const &vector)
Definition: prod.hpp:91
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:74
T::ERROR_CANNOT_DEDUCE_CPU_SCALAR_TYPE_FOR_T type
Definition: result_of.hpp:238
std::vector< typename viennacl::result_of::cpu_value_type< typename MatrixT::value_type >::type > eig(MatrixT const &matrix, lanczos_tag const &tag)
Implementation of the calculation of eigenvalues using lanczos.
Definition: lanczos.hpp:430
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.