CoCg.cxx
1 // Copyright (C) 2003-2009 Marc DuruflĂ©
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 #ifndef SELDON_FILE_ITERATIVE_COCG_CXX
21 
22 namespace Seldon
23 {
24 
26 
44 #ifdef SELDON_WITH_VIRTUAL
45  template<class T, class Vector1>
46  int CoCg(const VirtualMatrix<T>& A, Vector1& x, const Vector1& b,
47  Preconditioner_Base<T>& M,
48  Iteration<typename ClassComplexType<T>::Treal>& iter)
49 #else
50  template <class Titer, class Matrix1, class Vector1, class Preconditioner>
51  int CoCg(const Matrix1& A, Vector1& x, const Vector1& b,
52  Preconditioner& M, Iteration<Titer> & iter)
53 #endif
54  {
55  const int N = A.GetM();
56  if (N <= 0)
57  return 0;
58 
59  typedef typename Vector1::value_type Complexe;
60  Complexe rho, rho_1, alpha, beta, delta;
61  Complexe zero, one;
62  SetComplexZero(zero);
63  SetComplexOne(one);
64  rho = one;
65  rho_1 = zero;
66 
67  Vector1 p(b), q(b), r(b), z(b);
68  p.Fill(zero); q.Fill(zero); r.Fill(zero); z.Fill(zero);
69 
70  // for implementation see Cg
71  // we initialize iter
72  int success_init = iter.Init(b);
73  if (success_init != 0)
74  return iter.ErrorCode();
75 
76  Copy(b, r);
77  if (!iter.IsInitGuess_Null())
78  iter.MltAdd(-one, A, x, one, r);
79  else
80  x.Fill(zero);
81 
82  iter.SetNumberIteration(0);
83 
84  // Loop until the stopping criteria are reached
85  while (! iter.Finished(r))
86  {
87  // preconditioning
88  M.Solve(A, r, z);
89 
90  // instead of (bar(r),z) in CG we compute (r,z)
91  rho = DotProd(r, z);
92 
93  if (rho == zero)
94  {
95  iter.Fail(1, "Cocg breakdown #1");
96  break;
97  }
98 
99  if (iter.First())
100  Copy(z, p);
101  else
102  {
103  beta = rho / rho_1;
104  Mlt(beta, p);
105  Add(one, z, p);
106  }
107 
108  // product matrix vector
109  iter.Mlt(A, p, q);
110 
111  delta = DotProd(p, q);
112  if (delta == zero)
113  {
114  iter.Fail(2, "Cocg breakdown #2");
115  break;
116  }
117  alpha = rho / delta;
118 
119  Add(alpha, p, x);
120  Add(-alpha, q, r);
121 
122  rho_1 = rho;
123  ++iter;
124  }
125 
126  return iter.ErrorCode();
127  }
128 
129 
130 } // end namespace
131 
132 #define SELDON_FILE_ITERATIVE_COCG_CXX
133 #endif
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::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::CoCg
int CoCg(const Matrix1 &A, Vector1 &x, const Vector1 &b, Preconditioner &M, Iteration< Titer > &iter)
Solves a linear system by using Conjugate Orthogonal Conjugate Gradient.
Definition: CoCg.cxx:51
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