TBCI Numerical high perf. C++ Library 2.8.0
cheby.h
Go to the documentation of this file.
1
4
5// $Id: cheby.h,v 1.9.2.4 2019/05/28 11:13:02 garloff Exp $
6
7#ifndef TBCI_SOLVER_CHEBY_H
8#define TBCI_SOLVER_CHEBY_H
9
10#include "tbci/basics.h"
11#include "tbci/solver/precond.h"
12
14
15INST3(template <typename T, Matrix<T>, Vector<T> > class NN friend \
16int CHEBY (const Matrix<T>&, Vector<T>&, const Vector<T>&,\
17 const Preconditioner_Sig<T, Matrix<T> >&, unsigned int&, double&, T, T);)
18INST3(template <typename T, BdMatrix<T>, Vector<T> > class NN friend \
19int CHEBY (const BdMatrix<T>&, Vector<T>&, const Vector<T>&,\
20 const Preconditioner_Sig<T, BdMatrix<T> >&, unsigned int&, double&, T, T);)
21
22
23/*statt:
24template < class Matrix, class Vector, class Preconditioner, class Real,
25 class Type >
26int
27CHEBY(const Matrix &A, Vector &x, const Vector &b,
28 const Preconditioner &M, int &max_iter, Real &tol,
29 Type eigmin, Type eigmax)*/
30
31#ifndef HAVE_BCXX_TYPENAME_BUG
32# define VALUE_TYPE typename SysVector::value_type
33#else
34# define VALUE_TYPE SysVector::value_type
35#endif
36
37
58
59template < typename T, typename SysMatrix, typename SysVector >
60int CHEBY(const SysMatrix &A, SysVector &x, const SysVector &b,
61 const Preconditioner_Sig<T, SysMatrix> &M, unsigned int &max_iter, double &tol,
62 T eigmin, T eigmax)
63{
64 double resid;
65 unsigned int dim = A.rows();
66 T alpha(0), beta, c, d;
67 SysVector p(dim), q(dim), z(dim);
68
69 double normb = b.fabs();
70 SysVector r(b - A * x);
71
72 if (normb == 0.0)
73 normb = 1;
74
75 if ((resid = r.fabs() / normb) <= tol)
76 {
77 tol = resid;
78 max_iter = 0;
79 return 0;
80 }
81
82 c = (eigmax - eigmin) / 2.0;
83 d = (eigmax + eigmin) / 2.0;
84
85 for (unsigned int i = 1; i <= max_iter; i++)
86 {
87 z = M.solve(r);
88
89 if (i == 1)
90 {
91 p = z;
92 alpha = 2.0 / d;
93 } else
94 {
95 beta = c * alpha / 2.0; // calculate new beta
96 beta = beta * beta;
97 alpha = 1.0 / (d - beta); // calculate new alpha
98 p = z + (VALUE_TYPE)beta * p; // update search direction
99 }
100
101 q = A * p;
102 x += VALUE_TYPE (alpha) * p; // update approximation vector
103 r -= VALUE_TYPE (alpha) * q; // compute residual
104
105 if ((resid = r.fabs() / normb) <= tol)
106 {
107 tol = resid;
108 max_iter = i;
109 return 0; // convergence
110 }
111 }
112
113 tol = resid;
114 return 1; // no convergence
115}
116
118
119#endif /* TBCI_SOLVER_CHEBY_H */
const Vector< T > const Vector< T > const Vector< T > & p
Definition LM_fit.h:98
const Vector< T > const Vector< T > & x
Definition LM_fit.h:97
int i
Definition LM_fit.h:71
#define INST3(x, y, z)
Definition basics.h:240
#define NAMESPACE_END
Definition basics.h:323
#define NAMESPACE_TBCI
Definition basics.h:317
#define T
Definition bdmatlib.cc:20
SysMatrix
Definition bicgstab.h:90
NAMESPACE_TBCI Vector< T > class NN friend int CHEBY(const BdMatrix< T > &, Vector< T > &, const Vector< T > &, const Preconditioner_Sig< T, BdMatrix< T > > &, unsigned int &, double &, T, T)
#define VALUE_TYPE
Definition cheby.h:32
The class BdMatrix is an implementation to store and do operations on sparse Matrices with a band str...
Abstract base class for all Preconditioners.
Definition precond.h:41
virtual TVector< T > solve(const Vector< T > &) const =0
return c
Definition f_matrix.h:760
F_TMatrix< T > b
Definition f_matrix.h:736