Cg.cxx
1 // Copyright (C) 2003-2009 Marc DuruflĂ©
2 // Copyright (C) 2001-2009 Vivien Mallet
3 //
4 // This file is part of the linear-algebra library Seldon,
5 // http://seldon.sourceforge.net/.
6 //
7 // Seldon is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU Lesser General Public License as published by the Free
9 // Software Foundation; either version 2.1 of the License, or (at your option)
10 // any later version.
11 //
12 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 // more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with Seldon. If not, see http://www.gnu.org/licenses/.
19 
20 
21 #ifndef SELDON_FILE_ITERATIVE_CG_CXX
22 
23 namespace Seldon
24 {
25 
27 
45 #ifdef SELDON_WITH_VIRTUAL
46  template<class T, class Vector1>
47  int Cg(const VirtualMatrix<T>& A, Vector1& x, const Vector1& b,
48  Preconditioner_Base<T>& M,
49  Iteration<typename ClassComplexType<T>::Treal>& iter)
50 #else
51  template <class Titer, class Matrix1, class Vector1, class Preconditioner>
52  int Cg(const Matrix1& A, Vector1& x, const Vector1& b,
53  Preconditioner& M, Iteration<Titer> & iter)
54 #endif
55  {
56  const int N = A.GetM();
57  if (N <= 0)
58  return 0;
59 
60  typedef typename Vector1::value_type Complexe;
61  Complexe rho, rho_1, alpha, beta, delta;
62  Vector1 p(b), q(b), r(b), z(b);
63  Complexe zero, one;
64  SetComplexZero(zero);
65  SetComplexOne(one);
66  rho = one; rho_1 = one;
67 
68  // we initialize iter
69  int success_init = iter.Init(b);
70  if (success_init != 0)
71  return iter.ErrorCode();
72 
73  // we compute the initial residual r = b - Ax
74  Copy(b, r);
75  if (!iter.IsInitGuess_Null())
76  iter.MltAdd(-one, A, x, one, r);
77  else
78  x.Fill(zero);
79 
80  iter.SetNumberIteration(0);
81  // Loop until the stopping criteria are satisfied
82  while (! iter.Finished(r))
83  {
84 
85  // Preconditioning z = M^{-1} r
86  M.Solve(A, r, z);
87 
88  // rho = (conj(r),z)
89  rho = DotProdConj(r, z);
90 
91  if (rho == zero)
92  {
93  iter.Fail(1, "Cg breakdown #1");
94  break;
95  }
96 
97  if (iter.First())
98  Copy(z, p);
99  else
100  {
101  // p = beta*p + z where beta = rho_i/rho_{i-1}
102  beta = rho / rho_1;
103  Mlt(beta, p);
104  Add(one, z, p);
105  }
106 
107  // matrix vector product q = A*p
108  iter.Mlt(A, p, q);
109  delta = DotProdConj(p, q);
110  if (delta == zero)
111  {
112  iter.Fail(2, "Cg breakdown #2");
113  break;
114  }
115  alpha = rho / delta;
116 
117  // x = x + alpha*p and r = r - alpha*q where alpha = rho/(bar(p),q)
118  Add(alpha, p, x);
119  Add(-alpha, q, r);
120 
121  rho_1 = rho;
122 
123  ++iter;
124  }
125 
126  return iter.ErrorCode();
127  }
128 
129 
130 } // end namespace
131 
132 #define SELDON_FILE_ITERATIVE_CG_CXX
133 #endif
134 
Seldon::Iteration::First
bool First() const
Returns true if it is the first iteration.
Definition: Iterative.cxx:236
Seldon::Iteration::Init
int Init(const Vector1 &r)
Initialization with the right hand side.
Definition: Iterative.cxx:218
Seldon::Iteration::Mlt
void Mlt(const Matrix1 &A, const Vector1 &x, Vector1 &y)
Computes y = A x.
Definition: IterativeInline.cxx:123
Seldon::Cg
int Cg(const Matrix1 &A, Vector1 &x, const Vector1 &b, Preconditioner &M, Iteration< Titer > &iter)
Solves a linear system by using Conjugate Gradient (CG)
Definition: Cg.cxx:52
Seldon::Iteration::ErrorCode
int ErrorCode()
Returns the error code (if an error occured)
Definition: Iterative.cxx:351
Seldon::Iteration::Fail
void Fail(int i, const string &s)
Informs of a failure in the iterative solver.
Definition: Iterative.cxx:330
Seldon::Iteration::MltAdd
void MltAdd(const T1 &alpha, const Matrix1 &A, const Vector1 &x, const T1 &beta, Vector1 &y)
Computes y = beta y + alpha A x.
Definition: IterativeInline.cxx:109
Seldon::Iteration::SetNumberIteration
void SetNumberIteration(int nb)
Changes the number of iterations.
Definition: Iterative.cxx:177
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::Iteration
Class containing parameters for an iterative resolution.
Definition: Iterative.hxx:59
Seldon::Iteration::IsInitGuess_Null
bool IsInitGuess_Null() const
Returns true if the initial guess is null.
Definition: Iterative.cxx:247
Seldon::Iteration::Finished
bool Finished(const Vector1 &r) const
Returns true if the iterative solver has reached its end.
Definition: Iterative.cxx:264