NeoPZ
cgbet.h
Go to the documentation of this file.
1 
24 template < class Matrix, class Vector, class Preconditioner, class Real >
25 int
26 CG(const Matrix &A, Vector &x, const Vector &b,
27  const Preconditioner &M, int64_t &max_iter, Real &tol)
28 {
29  Real resid;
30  Vector p, z, q;
31  Vector alpha(1), beta(1), rho(1), rho_1(1);
32 
33  Real normb = norm(b);
34  Vector r = b - A*x;
35 
36  if (normb == 0.0)
37  normb = 1;
38 
39  if ((resid = norm(r) / normb) <= tol) {
40  tol = resid;
41  max_iter = 0;
42  return 0;
43  }
44 
45  for (int64_t i = 1; i <= max_iter; i++) {
46  z = M.solve(r);
47  rho(0) = dot(r, z);
48 
49  if (i == 1)
50  p = z;
51  else {
52  beta(0) = rho(0) / rho_1(0);
53  p = z + beta(0) * p;
54  }
55 
56  q = A*p;
57  alpha(0) = rho(0) / dot(p, q);
58 
59  x += alpha(0) * p;
60  r -= alpha(0) * q;
61 
62  if ((resid = norm(r) / normb) <= tol) {
63  tol = resid;
64  max_iter = i;
65  return 0;
66  }
67 
68  rho_1(0) = rho(0);
69  }
70 
71  tol = resid;
72  return 1;
73 }
74 
static const double tol
Definition: pzgeoprism.cpp:23
int CG(const Matrix &A, Vector &x, const Vector &b, const Preconditioner &M, int64_t &max_iter, Real &tol)
CG solves the symmetric positive definite linear system using the Conjugate Gradient method...
Definition: cgbet.h:26
Definition: vectors.h:81