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.

127 lines
5.4 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2002-2015 Yves Renard
  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. //===========================================================================
  27. //
  28. // Copyright (c) 1998-2001, University of Notre Dame. All rights reserved.
  29. // Redistribution and use in source and binary forms, with or without
  30. // modification, are permitted provided that the following conditions are met:
  31. //
  32. // * Redistributions of source code must retain the above copyright
  33. // notice, this list of conditions and the following disclaimer.
  34. // * Redistributions in binary form must reproduce the above copyright
  35. // notice, this list of conditions and the following disclaimer in the
  36. // documentation and/or other materials provided with the distribution.
  37. // * Neither the name of the University of Notre Dame nor the
  38. // names of its contributors may be used to endorse or promote products
  39. // derived from this software without specific prior written permission.
  40. //
  41. // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
  42. // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  43. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  44. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
  45. // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  46. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  47. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  48. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  49. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  50. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  51. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. //
  53. //===========================================================================
  54. /**@file gmm_modified_gram_schmidt.h
  55. @author Andrew Lumsdaine <lums@osl.iu.edu>, Lie-Quan Lee <llee@osl.iu.edu>
  56. @date October 13, 2002.
  57. @brief Modified Gram-Schmidt orthogonalization
  58. */
  59. #ifndef GMM_MODIFIED_GRAM_SCHMIDT_H
  60. #define GMM_MODIFIED_GRAM_SCHMIDT_H
  61. #include "gmm_kernel.h"
  62. namespace gmm {
  63. template <typename T>
  64. class modified_gram_schmidt {
  65. protected:
  66. typedef dense_matrix<T> MAT;
  67. MAT M;
  68. public:
  69. modified_gram_schmidt(int restart, size_t s) : M(s, restart+1) {}
  70. typename linalg_traits<MAT>::const_sub_col_type
  71. operator[](size_t i) const { return mat_const_col(M, i); }
  72. typename linalg_traits<MAT>::sub_col_type
  73. operator[](size_t i) { return mat_col(M, i); }
  74. inline size_type nrows(void) const { return M.nrows(); }
  75. inline size_type ncols(void) const { return M.ncols(); }
  76. MAT &mat(void) { return M; }
  77. const MAT &mat(void) const { return M; }
  78. };
  79. template <typename T, typename VecHi> inline
  80. void orthogonalize(modified_gram_schmidt<T>& V, const VecHi& Hi_, size_t i) {
  81. VecHi& Hi = const_cast<VecHi&>(Hi_);
  82. for (size_t k = 0; k <= i; k++) {
  83. Hi[k] = gmm::vect_hp(V[i+1], V[k]);
  84. gmm::add(gmm::scaled(V[k], -Hi[k]), V[i+1]);
  85. }
  86. }
  87. template <typename T, typename VecHi>
  88. void orthogonalize_with_refinment(modified_gram_schmidt<T>& V,
  89. const VecHi& Hi_, size_t i) {
  90. VecHi& Hi = const_cast<VecHi&>(Hi_);
  91. orthogonalize(V, Hi_, i);
  92. sub_interval SUBI(0, V.nrows()), SUBJ(0, i+1);
  93. std::vector<T> corr(i+1);
  94. gmm::mult(conjugated(sub_matrix(V.mat(), SUBI, SUBJ)),
  95. V[i+1], corr);
  96. gmm::mult(sub_matrix(V.mat(), SUBI, SUBJ),
  97. scaled(corr, T(-1)), V[i+1],V[i+1]);
  98. gmm::add(corr, sub_vector(Hi, SUBJ));
  99. }
  100. template <typename T, typename VecS, typename VecX>
  101. void combine(modified_gram_schmidt<T>& V, const VecS& s, VecX& x, size_t i)
  102. { for (size_t j = 0; j < i; ++j) gmm::add(gmm::scaled(V[j], s[j]), x); }
  103. }
  104. #endif