Loading...
Searching...
No Matches
power_iteration.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <limits>
6
8
9/// @brief Power iteration to estimate the largest eigenvalue of a
10/// row-normalized operator D^{-1}A, where D^{-1} is the inverted diagonal of A.
11///
12/// @tparam OperatorT Operator type (must satisfy OperatorLike).
13template < terra::linalg::OperatorLike OperatorT >
14//template<typename OperatorT>
16 OperatorT& op,
17 SrcOf< OperatorT >& tmpIt,
18 SrcOf< OperatorT >& tmpAux,
19 //VectorQ1Scalar< double>& tmpIt,
20 //VectorQ1Scalar< double>& tmpAux,
21 const int iterations )
22{
23 // TODO typecheck on src dst
24
25 // randomize start
26 randomize( tmpIt );
27
28 // normalize
29 auto norm = linalg::norm_2( tmpIt );
30 if ( norm < std::numeric_limits< double >::min() )
31 {
32 return 1.0; // safe fallback
33 }
34 lincomb( tmpIt, { 1.0 / norm }, { tmpIt }, 0.0 );
35
36 // apply operator
37 apply( op, tmpIt, tmpAux );
38
39 auto radius = 0.0;
40 for ( int iteration = 0; iteration < iterations; ++iteration )
41 {
42 // normalize
43 norm = linalg::norm_2( tmpAux );
44 if ( norm < std::numeric_limits< double >::min() )
45 {
46 return radius > 0.0 ? radius : 1.0;
47 }
48 lincomb( tmpIt, { 1.0 / norm }, { tmpAux }, 0.0 );
49
50 // apply operator
51 apply( op, tmpIt, tmpAux );
52
53 // compute radius
54 radius = dot( tmpIt, tmpAux );
55 }
56 return radius;
57}
58
59} // namespace terra::linalg::solvers
Definition block_preconditioner_2x2.hpp:7
double power_iteration(OperatorT &op, SrcOf< OperatorT > &tmpIt, SrcOf< OperatorT > &tmpAux, const int iterations)
Power iteration to estimate the largest eigenvalue of a row-normalized operator D^{-1}A,...
Definition power_iteration.hpp:15
void lincomb(Vector &y, const std::vector< ScalarOf< Vector > > &c, const std::vector< Vector > &x, const ScalarOf< Vector > &c0)
Compute a linear combination of vectors. Implements: .
Definition vector.hpp:72
ScalarOf< Vector > norm_2(const Vector &y)
Compute the 2-norm (Euclidean norm) of a vector. Implements: .
Definition vector.hpp:166
void randomize(Vector &y)
Randomize the entries of a vector. Sets each entry of to a random value.
Definition vector.hpp:146
Operator::SrcVectorType SrcOf
Alias for the source vector type of an operator.
Definition operator.hpp:145
ScalarOf< Vector > dot(const Vector &y, const Vector &x)
Compute the dot product of two vectors. Implements: .
Definition vector.hpp:118
void apply(LinearForm &L, typename LinearForm::DstVectorType &dst)
Apply a linear form and write to a destination vector.
Definition linear_form.hpp:37