GetFEM++  5.3
gmm_least_squares_cg.h
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2017 Yves Renard, Benjamin Schleimer
5 
6  This file is a part of GetFEM++
7 
8  GetFEM++ is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program; if not, write to the Free Software Foundation,
19  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, you may use this file as it is a part of a free
22  software library without restriction. Specifically, if other files
23  instantiate templates or use macros or inline functions from this file,
24  or you compile this file and link it with other files to produce an
25  executable, this file does not by itself cause the resulting executable
26  to be covered by the GNU Lesser General Public License. This exception
27  does not however invalidate any other reasons why the executable file
28  might be covered by the GNU Lesser General Public License.
29 
30 ===========================================================================*/
31 
32 /**@file gmm_leastsquares_cg.h
33  @author Benjamin Schleimer <bensch128 (at) yahoo (dot) com>
34  @date January 23, 2007.
35  @brief Conjugate gradient least squares algorithm.
36  Algorithm taken from http://www.stat.washington.edu/wxs/Stat538-w05/Notes/conjugate-gradients.pdf page 6
37 */
38 #ifndef GMM_LEAST_SQUARES_CG_H__
39 #define GMM_LEAST_SQUARES_CG_H__
40 
41 #include "gmm_kernel.h"
42 #include "gmm_iter.h"
43 #include "gmm_conjugated.h"
44 
45 namespace gmm {
46 
47  template <typename Matrix, typename Vector1, typename Vector2>
48  void least_squares_cg(const Matrix& C, Vector1& x, const Vector2& y,
49  iteration &iter) {
50 
51  typedef typename temporary_dense_vector<Vector1>::vector_type temp_vector;
52  typedef typename linalg_traits<Vector1>::value_type T;
53 
54  T rho, rho_1(0), a;
55  temp_vector p(vect_size(x)), q(vect_size(y)), g(vect_size(x));
56  temp_vector r(vect_size(y));
57  iter.set_rhsnorm(gmm::sqrt(gmm::abs(vect_hp(y, y))));
58 
59  if (iter.get_rhsnorm() == 0.0)
60  clear(x);
61  else {
62  mult(C, scaled(x, T(-1)), y, r);
63  mult(conjugated(C), r, g);
64  rho = vect_hp(g, g);
65  copy(g, p);
66 
67  while (!iter.finished_vect(g)) {
68 
69  if (!iter.first()) {
70  rho = vect_hp(g, g);
71  add(g, scaled(p, rho / rho_1), p);
72  }
73 
74  mult(C, p, q);
75 
76  a = rho / vect_hp(q, q);
77  add(scaled(p, a), x);
78  add(scaled(q, -a), r);
79  // NOTE: how do we minimize the impact to the transpose?
80  mult(conjugated(C), r, g);
81  rho_1 = rho;
82 
83  ++iter;
84  }
85  }
86  }
87 
88  template <typename Matrix, typename Precond,
89  typename Vector1, typename Vector2> inline
90  void least_squares_cg(const Matrix& C, const Vector1& x, const Vector2& y,
91  iteration &iter)
92  { least_squares_cg(C, linalg_const_cast(x), y, iter); }
93 }
94 
95 
96 #endif // GMM_SOLVER_CG_H__
handle conjugation of complex matrices/vectors.
conjugated_return< L >::return_type conjugated(const L &v)
return a conjugated view of the input matrix or vector.
void copy(const L1 &l1, L2 &l2)
*/
Definition: gmm_blas.h:977
Include the base gmm files.
Iteration object.
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:59
strongest_value_type< V1, V2 >::value_type vect_hp(const V1 &v1, const V2 &v2)
*/
Definition: gmm_blas.h:511
void add(const L1 &l1, L2 &l2)
*/
Definition: gmm_blas.h:1268