NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
Functions
netlib Namespace Reference

Description

Iterative template routines.

Original version of template routines from netlib as of 2016-11-08 (file dates: 1998-07-21), slightly modified for numcxx (see corresponding comments).

Functions

template<class Matrix , class Vector , class Preconditioner , class Real >
int BiCGSTAB (const Matrix &A, Vector &x, const Vector &b, const Preconditioner &M, int &max_iter, Real &tol)
 Iterative template routine – BiCGSTAB. More...
 
template<class Matrix , class Vector , class Preconditioner , class Real >
int CG (const Matrix &A, Vector &x, const Vector &b, const Preconditioner &M, int &max_iter, Real &tol)
 Iterative template routine – CG. More...
 

Function Documentation

template<class Matrix , class Vector , class Preconditioner , class Real >
int netlib::BiCGSTAB ( const Matrix &  A,
Vector &  x,
const Vector &  b,
const Preconditioner &  M,
int &  max_iter,
Real &  tol 
)

Iterative template routine – BiCGSTAB.

Original version from netlib as of 2016-11-08 (file date: 1998-07-21), slightly modified for numcxx (see corresponding comment).

BiCGSTAB solves the unsymmetric linear system Ax = b using the Preconditioned BiConjugate Gradient Stabilized method

BiCGSTAB follows the algorithm described on p. 27 of the (SIAM Templates book)[http://www.netlib.org/templates/templates.pdf].

The return value indicates convergence within max_iter (input) iterations (0), or no convergence within max_iter iterations (1).

Upon successful return, output arguments have the following values:

Parameters
x– approximate solution to Ax = b
max_iter– the number of iterations performed before the tolerance was reached
tol– the residual after the final iteration

Definition at line 29 of file bicgstab.hxx.

31  {
32  Real resid;
33  Vector rho_1(1), rho_2(1), alpha(1), beta(1), omega(1);
34  Vector p, phat, s, shat, t, v;
35 
36  Real normb = norm(b);
37  Vector r = b - A * x;
38  Vector rtilde = r;
39 
40  if (normb == 0.0)
41  normb = 1;
42 
43  if ((resid = norm(r) / normb) <= tol) {
44  tol = resid;
45  max_iter = 0;
46  return 0;
47  }
48 
49  for (int i = 1; i <= max_iter; i++) {
50  rho_1(0) = dot(rtilde, r);
51  if (rho_1(0) == 0) {
52  tol = norm(r) / normb;
53  return 2;
54  }
55  if (i == 1)
56  p = r;
57  else {
58  beta(0) = (rho_1(0)/rho_2(0)) * (alpha(0)/omega(0));
59  p = r + beta(0) * (p - omega(0) * v);
60  }
61  // numcxx: was
62  // phat = M.solve(p);
63  M.solve(phat,p);
64  v = A * phat;
65  alpha(0) = rho_1(0) / dot(rtilde, v);
66  s = r - alpha(0) * v;
67  if ((resid = norm(s)/normb) < tol) {
68  x += alpha(0) * phat;
69  tol = resid;
70  return 0;
71  }
72  // numcxx: was
73  //shat = M.solve(s);
74  M.solve(shat,s);
75  t = A * shat;
76  omega = dot(t,s) / dot(t,t);
77  x += alpha(0) * phat + omega(0) * shat;
78  r = s - omega(0) * t;
79 
80  rho_2(0) = rho_1(0);
81  if ((resid = norm(r) / normb) < tol) {
82  tol = resid;
83  max_iter = i;
84  return 0;
85  }
86  if (omega(0) == 0) {
87  tol = norm(r) / normb;
88  return 3;
89  }
90  }
91 
92  tol = resid;
93  return 1;
94  }
A::value_type dot(const A &a, const B &b)
Dot product of array or expression.
Definition: util.ixx:91
A::value_type norm(const A &a)
Euklidean norm of array or expression.
Definition: util.hxx:54

+ Here is the call graph for this function:

template<class Matrix , class Vector , class Preconditioner , class Real >
int netlib::CG ( const Matrix &  A,
Vector &  x,
const Vector &  b,
const Preconditioner &  M,
int &  max_iter,
Real &  tol 
)

Iterative template routine – CG.

Original version from netlib as of 2016-11-08 (file date: 1998-07-21), slightly modified for numcxx (see corresponding comment).

CG solves the symmetric positive definite linear system Ax=b using the Conjugate Gradient method.

CG follows the algorithm described on p. 15 in the (SIAM Templates book)[http://www.netlib.org/templates/templates.pdf].

The return value indicates convergence within max_iter (input) iterations (0), or no convergence within max_iter iterations (1).

Upon successful return, output arguments have the following values:

Parameters
x– approximate solution to Ax = b
max_iter– the number of iterations performed before the tolerance was reached
tol– the residual after the final iteration

Definition at line 28 of file cg.hxx.

30  {
31  Real resid;
32  Vector p, z, q;
33  Vector alpha(1), beta(1), rho(1), rho_1(1);
34 
35  Real normb = norm(b);
36  Vector r = b - A*x;
37 
38  if (normb == 0.0)
39  normb = 1;
40 
41  if ((resid = norm(r) / normb) <= tol) {
42  tol = resid;
43  max_iter = 0;
44  return 0;
45  }
46 
47  for (int i = 1; i <= max_iter; i++) {
48  // numcxx: was
49  // z=M.solve(r)
50  M.solve(z, r);
51  rho(0) = dot(r, z);
52 
53  if (i == 1)
54  p = z;
55  else {
56  beta(0) = rho(0) / rho_1(0);
57  p = z + beta(0) * p;
58  }
59 
60  q = A*p;
61  alpha(0) = rho(0) / dot(p, q);
62 
63  x += alpha(0) * p;
64  r -= alpha(0) * q;
65 
66  if ((resid = norm(r) / normb) <= tol) {
67  tol = resid;
68  max_iter = i;
69  return 0;
70  }
71 
72  rho_1(0) = rho(0);
73  }
74 
75  tol = resid;
76  return 1;
77  }
A::value_type dot(const A &a, const B &b)
Dot product of array or expression.
Definition: util.ixx:91
A::value_type norm(const A &a)
Euklidean norm of array or expression.
Definition: util.hxx:54

+ Here is the call graph for this function: