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