GetFEM  5.5
gmm_precond_mr_approx_inverse.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 // This file is a modified version of approximate_inverse.h from ITL.
33 // See http://osl.iu.edu/research/itl/
34 // Following the corresponding Copyright notice.
35 //===========================================================================
36 //
37 // Copyright (c) 1998-2020, University of Notre Dame. All rights reserved.
38 // Redistribution and use in source and binary forms, with or without
39 // modification, are permitted provided that the following conditions are met:
40 //
41 // * Redistributions of source code must retain the above copyright
42 // notice, this list of conditions and the following disclaimer.
43 // * Redistributions in binary form must reproduce the above copyright
44 // notice, this list of conditions and the following disclaimer in the
45 // documentation and/or other materials provided with the distribution.
46 // * Neither the name of the University of Notre Dame nor the
47 // names of its contributors may be used to endorse or promote products
48 // derived from this software without specific prior written permission.
49 //
50 // THIS SOFTWARE IS PROVIDED BY THE TRUSTEES OF INDIANA UNIVERSITY AND
51 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
52 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
53 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES
54 // OF INDIANA UNIVERSITY AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
55 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 //
62 //===========================================================================
63 
64 /**@file gmm_precond_mr_approx_inverse.h
65  @author Andrew Lumsdaine <lums@osl.iu.edu>
66  @author Lie-Quan Lee <llee@osl.iu.edu>
67  @author Yves Renard <Yves.Renard@insa-lyon.fr>
68  @date June 5, 2003.
69  @brief Approximate inverse via MR iteration.
70 */
71 
72 #ifndef GMM_PRECOND_MR_APPROX_INVERSE_H
73 #define GMM_PRECOND_MR_APPROX_INVERSE_H
74 
75 
76 #include "gmm_precond.h"
77 
78 namespace gmm {
79 
80  /** Approximate inverse via MR iteration (see P301 of Saad book).
81  */
82  template <typename Matrix>
84 
85  typedef typename linalg_traits<Matrix>::value_type value_type;
86  typedef typename number_traits<value_type>::magnitude_type magnitude_type;
87  typedef typename principal_orientation_type<typename
88  linalg_traits<Matrix>::sub_orientation>::potype sub_orientation;
90  typedef col_matrix<VVector> MMatrix;
91 
92  MMatrix M;
93  size_type nb_it;
94  magnitude_type threshold;
95 
96  void build_with(const Matrix& A);
97  mr_approx_inverse_precond(const Matrix& A, size_type nb_it_,
98  magnitude_type threshold_)
99  : M(mat_nrows(A), mat_ncols(A))
100  { threshold = threshold_; nb_it = nb_it_; build_with(A); }
102  { threshold = magnitude_type(1E-7); nb_it = 5; }
103  mr_approx_inverse_precond(size_type nb_it_, magnitude_type threshold_)
104  { threshold = threshold_; nb_it = nb_it_; }
105  const MMatrix &approx_inverse(void) const { return M; }
106  };
107 
108  template <typename Matrix, typename V1, typename V2> inline
109  void mult(const mr_approx_inverse_precond<Matrix>& P, const V1 &v1, V2 &v2)
110  { mult(P.M, v1, v2); }
111 
112  template <typename Matrix, typename V1, typename V2> inline
113  void transposed_mult(const mr_approx_inverse_precond<Matrix>& P,
114  const V1 &v1,V2 &v2)
115  { mult(gmm::conjugated(P.M), v1, v2); }
116 
117  template <typename Matrix>
118  void mr_approx_inverse_precond<Matrix>::build_with(const Matrix& A) {
119  gmm::resize(M, mat_nrows(A), mat_ncols(A));
120  typedef value_type T;
121  typedef magnitude_type R;
122  VVector m(mat_ncols(A)),r(mat_ncols(A)),ei(mat_ncols(A)),Ar(mat_ncols(A));
124  if (alpha == T(0)) alpha = T(1);
125 
126  for (size_type i = 0; i < mat_nrows(A); ++i) {
127  gmm::clear(m); gmm::clear(ei);
128  m[i] = alpha;
129  ei[i] = T(1);
130 
131  for (size_type j = 0; j < nb_it; ++j) {
132  gmm::mult(A, gmm::scaled(m, T(-1)), r);
133  gmm::add(ei, r);
134  gmm::mult(A, r, Ar);
135  T nAr = vect_sp(Ar,Ar);
136  if (gmm::abs(nAr) > R(0)) {
137  gmm::add(gmm::scaled(r, gmm::safe_divide(vect_sp(r, Ar), vect_sp(Ar, Ar))), m);
138  gmm::clean(m, threshold * gmm::vect_norm2(m));
139  } else gmm::clear(m);
140  }
141  if (gmm::vect_norm2(m) == R(0)) m[i] = alpha;
142  gmm::copy(m, M.col(i));
143  }
144  }
145 }
146 
147 #endif
148 
sparse vector built upon std::map.
Definition: gmm_vector.h:752
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
linalg_traits< M >::value_type mat_trace(const M &m)
Trace of a matrix.
Definition: gmm_blas.h:527
void clear(L &l)
clear (fill with zeros) a vector or matrix.
Definition: gmm_blas.h:58
void resize(V &v, size_type n)
*‍/
Definition: gmm_blas.h:209
void clean(L &l, double threshold)
Clean a vector or matrix (replace near-zero entries with zeroes).
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
strongest_value_type< V1, V2 >::value_type vect_sp(const V1 &v1, const V2 &v2)
*‍/
Definition: gmm_blas.h:263
void add(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:1275
number_traits< typename linalg_traits< M >::value_type >::magnitude_type mat_euclidean_norm_sqr(const M &m)
*‍/
Definition: gmm_blas.h:625
gmm preconditioners.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
size_type alpha(short_type n, short_type d)
Return the value of which is the number of monomials of a polynomial of variables and degree .
Definition: bgeot_poly.cc:46
Approximate inverse via MR iteration (see P301 of Saad book).