Cgne.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_CGNE_CXX
21 
22 namespace Seldon
23 {
24 
26 
43 #ifdef SELDON_WITH_VIRTUAL
44  template<class T, class Vector1>
45  int Cgne(const VirtualMatrix<T>& A, Vector1& x, const Vector1& b,
46  Preconditioner_Base<T>& M,
47  Iteration<typename ClassComplexType<T>::Treal>& iter)
48 #else
49  template <class Titer, class Matrix1, class Vector1, class Preconditioner>
50  int Cgne(const Matrix1& A, Vector1& x, const Vector1& b,
51  Preconditioner& M, Iteration<Titer> & iter)
52 #endif
53  {
54  const int N = A.GetM();
55  if (N <= 0)
56  return 0;
57 
58  typedef typename Vector1::value_type Complexe;
59  Complexe rho, rho_1, alpha, beta, delta;
60  Vector1 p(b), q(b), r(b), z(b);
61  Complexe zero, one;
62  SetComplexZero(zero);
63  SetComplexOne(one);
64  rho = one; rho_1 = zero;
65 
66  // x should be equal to 0
67  // see Cg to understand implementation
68  // we solve A^t A x = A^t b
69  // left-preconditioner is equal to M M^t
70 
71  // q = A^t (b - A x)
72  if (!iter.IsInitGuess_Null())
73  iter.MltAdd(-one, A, x, one, r);
74  else
75  x.Fill(zero);
76 
77  iter.Mlt(SeldonTrans, A, r, q);
78  Copy(q, r);
79 
80  // we initialize iter
81  int success_init = iter.Init(q);
82  if (success_init != 0)
83  return iter.ErrorCode();
84 
85  iter.SetNumberIteration(0);
86  // Loop until the stopping criteria are satisfied
87  while (! iter.Finished(r))
88  {
89  // Preconditioning
90  M.TransSolve(A, r, q);
91  M.Solve(A, q, z);
92 
93  rho = DotProd(r, z);
94  if (rho == zero)
95  {
96  iter.Fail(1, "Cgne breakdown #1");
97  break;
98  }
99 
100  if (iter.First())
101  Copy(z, p);
102  else
103  {
104  beta = rho / rho_1;
105  Mlt(beta, p);
106  Add(one, z, p);
107  }
108 
109  // instead of q = A*p, we compute q = A^t A *p
110  iter.Mlt(A, p, q);
111  iter.Mlt(SeldonTrans, A, q, z);
112 
113  delta = DotProd(p, z);
114  if (delta == zero)
115  {
116  iter.Fail(2, "Cgne breakdown #2");
117  break;
118  }
119  alpha = rho / delta;
120 
121  Add(alpha, p, x);
122  Add(-alpha, z, r);
123 
124  rho_1 = rho;
125 
126  // two iterations, because of two multiplications with A
127  ++iter;
128  ++iter;
129  }
130 
131  return iter.ErrorCode();
132  }
133 
134 
135 } // end namespace
136 
137 #define SELDON_FILE_ITERATIVE_CGNE_CXX
138 #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::Cgne
int Cgne(const Matrix1 &A, Vector1 &x, const Vector1 &b, Preconditioner &M, Iteration< Titer > &iter)
Solves a linear system using Conjugate Gradient Normal Equation (CGNE)
Definition: Cgne.cxx:50
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