G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsLanczosMatrix.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 namespace gismo
17 {
18 
26 template<class T>
28 {
29 public:
30 
38  gsLanczosMatrix(const std::vector<T> & gamma, const std::vector<T> & delta)
39  : m_gamma(gamma), m_delta(delta), m_n(m_delta.size())
40  { GISMO_ASSERT( m_delta.size() == m_gamma.size() + 1, "Size mismatch." ); }
41 
50  T maxEigenvalue(index_t maxIter = 20, T tol = 1.e-6)
51  {
52  if (m_n==1)
53  return m_delta[0];
54 
55  // Use Gerschgorin theorem to compute upper bound for eigenvalues
56  T x0 = 0;
57  for (size_t i=0;i<m_n;++i)
58  {
59  const T tmp = math::abs(m_delta[i])
60  + ( i<m_n-1 ? math::abs(m_gamma[i]) : (T)(0) )
61  + ( i>0 ? math::abs(m_gamma[i-1]) : (T)(0) );
62  if (tmp>x0) x0 = tmp;
63  }
64 
65  return newtonIteration(x0, maxIter, tol);
66  }
67 
76  T minEigenvalue(index_t maxIter = 20, T tol = 1.e-6)
77  {
78  if (m_n==1)
79  return m_delta[0];
80 
81  T x0 = 0;
82  return newtonIteration(x0, maxIter, tol);
83  }
84 
89  {
90  gsSparseMatrix<T> L(m_n,m_n);
91  gsSparseEntries<T> list;
92  list.reserve(3*m_n);
93 
94  list.add(0,0,m_delta[0]);
95  for (size_t i = 1; i<m_n;i++)
96  {
97  list.add(i,i-1,m_gamma[i-1]);
98  list.add(i-1,i,m_gamma[i-1]);
99  list.add(i,i,m_delta[i]);
100  }
101  L.setFrom(list);
102  return L;
103  }
104 
105 private:
106 
113  std::pair<T,T> eval( T lambda )
114  {
115  std::vector<T> value(m_n+1);
116  std::vector<T> deriv(m_n+1);
117 
118  value[0] = (T)(1);
119  value[1] = m_delta[0]-lambda;
120  deriv[0] = (T)(0);
121  deriv[1] = (T)(-1);
122  for (size_t k=2; k<m_n+1; ++k)
123  {
124  value[k] = (m_delta[k-1]-lambda) * value[k-1] - m_gamma[k-2]*m_gamma[k-2]*value[k-2];
125  deriv[k] = (m_delta[k-1]-lambda) * deriv[k-1] - value[k-1] - m_gamma[k-2]*m_gamma[k-2]*deriv[k-2];
126  }
127  return std::pair<T,T>(value[m_n],deriv[m_n]);
128  }
129 
139  T newtonIteration(T x0, index_t maxIter, T tol)
140  {
141  index_t iter = 0;
142  T res = 1;
143  T x_old = x0;
144  T x_new = x0;
145  while (iter < maxIter && res > tol)
146  {
147  const std::pair<T,T> ev = eval(x_old);
148  const T& value = ev.first;
149  const T& deriv = ev.second;
150 
151  x_new = x_old - value/deriv;
152  res = math::abs(x_old - x_new);
153 
154  x_old = x_new;
155  iter++;
156  }
157  return x_new;
158  }
159 
160 private:
161  const std::vector<T>& m_gamma;
162  const std::vector<T>& m_delta;
163  size_t m_n;
164 };
165 
166 } // namespace gismo
Class that provides a container for triplets (i,j,value) to be filled in a sparse matrix...
Definition: gsSparseMatrix.h:33
std::pair< T, T > eval(T lambda)
Evalutates characteristic polynomial.
Definition: gsLanczosMatrix.h:113
T newtonIteration(T x0, index_t maxIter, T tol)
Newton iteration for searching the zeros of the characteristic polynomial.
Definition: gsLanczosMatrix.h:139
gsLanczosMatrix(const std::vector< T > &gamma, const std::vector< T > &delta)
Constructor for the Lanczos matrix The Lanczos matrix is a symmetric tridiagonal matrix with diagonal...
Definition: gsLanczosMatrix.h:38
Class for representing a Lanczos matrix and calculating its eigenvalues.
Definition: gsLanczosMatrix.h:27
#define index_t
Definition: gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
T minEigenvalue(index_t maxIter=20, T tol=1.e-6)
Calculates the smallest eigenvalue.
Definition: gsLanczosMatrix.h:76
gsSparseMatrix< T > matrix()
This function returns the Lanczos matrix as gsSparseMatrix.
Definition: gsLanczosMatrix.h:88
EIGEN_STRONG_INLINE abs_expr< E > abs(const E &u)
Absolute value.
Definition: gsExpressions.h:4488
T maxEigenvalue(index_t maxIter=20, T tol=1.e-6)
Calculates the largest eigenvalue.
Definition: gsLanczosMatrix.h:50