You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.4 KiB

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