GetFEM  5.5
gmm_modified_gram_schmidt.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2002-2026 Yves Renard
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, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 //===========================================================================
32 //
33 // Copyright (c) 1998-2020, University of Notre Dame. All rights reserved.
34 // Redistribution and use in source and binary forms, with or without
35 // modification, are permitted provided that the following conditions are met:
36 //
37 // * Redistributions of source code must retain the above copyright
38 // notice, this list of conditions and the following disclaimer.
39 // * Redistributions in binary form must reproduce the above copyright
40 // notice, this list of conditions and the following disclaimer in the
41 // documentation and/or other materials provided with the distribution.
42 // * Neither the name of the University of Notre Dame nor the
43 // names of its contributors may be used to endorse or promote products
44 // derived from this software without specific prior written permission.
45 //
46 // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
47 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
48 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
49 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
50 // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 //
58 //===========================================================================
59 
60 /**@file gmm_modified_gram_schmidt.h
61  @author Andrew Lumsdaine <lums@osl.iu.edu>, Lie-Quan Lee <llee@osl.iu.edu>
62  @date October 13, 2002.
63  @brief Modified Gram-Schmidt orthogonalization
64 */
65 
66 #ifndef GMM_MODIFIED_GRAM_SCHMIDT_H
67 #define GMM_MODIFIED_GRAM_SCHMIDT_H
68 
69 #include "gmm_kernel.h"
70 
71 namespace gmm {
72 
73  template <typename T>
74  class modified_gram_schmidt {
75  protected:
76  typedef dense_matrix<T> MAT;
77  MAT M;
78 
79  public:
80 
81  modified_gram_schmidt(int restart, size_t s) : M(s, restart+1) {}
82 
83  typename linalg_traits<MAT>::const_sub_col_type
84  operator[](size_t i) const { return mat_const_col(M, i); }
85 
86  typename linalg_traits<MAT>::sub_col_type
87  operator[](size_t i) { return mat_col(M, i); }
88 
89  inline size_type nrows(void) const { return M.nrows(); }
90  inline size_type ncols(void) const { return M.ncols(); }
91  MAT &mat(void) { return M; }
92  const MAT &mat(void) const { return M; }
93 
94  };
95 
96  template <typename T, typename VecHi> inline
97  void orthogonalize(modified_gram_schmidt<T>& V, const VecHi& Hi_, size_t i) {
98  VecHi& Hi = const_cast<VecHi&>(Hi_);
99 
100  for (size_t k = 0; k <= i; k++) {
101  Hi[k] = gmm::vect_hp(V[i+1], V[k]);
102  gmm::add(gmm::scaled(V[k], -Hi[k]), V[i+1]);
103  }
104  }
105 
106  template <typename T, typename VecHi>
107  void orthogonalize_with_refinment(modified_gram_schmidt<T>& V,
108  const VecHi& Hi_, size_t i) {
109  VecHi& Hi = const_cast<VecHi&>(Hi_);
110  orthogonalize(V, Hi_, i);
111 
112  sub_interval SUBI(0, V.nrows()), SUBJ(0, i+1);
113  std::vector<T> corr(i+1);
114  gmm::mult(conjugated(sub_matrix(V.mat(), SUBI, SUBJ)),
115  V[i+1], corr);
116  gmm::mult(sub_matrix(V.mat(), SUBI, SUBJ),
117  scaled(corr, T(-1)), V[i+1],V[i+1]);
118  gmm::add(corr, sub_vector(Hi, SUBJ));
119  }
120 
121  template <typename T, typename VecS, typename VecX>
122  void combine(modified_gram_schmidt<T>& V, const VecS& s, VecX& x, size_t i)
123  { for (size_t j = 0; j < i; ++j) gmm::add(gmm::scaled(V[j], s[j]), x); }
124 }
125 
126 #endif
strongest_value_type< V1, V2 >::value_type vect_hp(const V1 &v1, const V2 &v2)
*‍/
Definition: gmm_blas.h:510
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
conjugated_return< L >::return_type conjugated(const L &v)
return a conjugated view of the input matrix or vector.
Include the base gmm files.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48