GetFEM  5.5
gmm_precond_diagonal.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-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 /**@file gmm_precond_diagonal.h
32  @author Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date June 5, 2003.
34  @brief Diagonal matrix preconditoner.
35 */
36 
37 #ifndef GMM_PRECOND_DIAGONAL_H
38 #define GMM_PRECOND_DIAGONAL_H
39 
40 #include "gmm_precond.h"
41 
42 namespace gmm {
43 
44  /** Diagonal preconditioner. */
45  template<typename Matrix> struct diagonal_precond {
46  typedef typename linalg_traits<Matrix>::value_type value_type;
47  typedef typename number_traits<value_type>::magnitude_type magnitude_type;
48 
49  std::vector<magnitude_type> diag;
50 
51  void build_with(const Matrix &M) {
52  diag.resize(mat_nrows(M));
53  for (size_type i = 0; i < mat_nrows(M); ++i) {
54  magnitude_type x = gmm::abs(M(i, i));
55  if (x == magnitude_type(0)) {
56  x = magnitude_type(1);
57  GMM_WARNING2("The matrix has a zero on its diagonal");
58  }
59  diag[i] = magnitude_type(1) / x;
60  }
61  }
62  size_type memsize() const { return sizeof(*this) + diag.size() * sizeof(value_type); }
63  diagonal_precond(const Matrix &M) { build_with(M); }
64  diagonal_precond(void) {}
65  };
66 
67  template <typename Matrix, typename V2> inline
68  void mult_diag_p(const diagonal_precond<Matrix>& P, V2 &v2, abstract_sparse){
69  typename linalg_traits<V2>::iterator it = vect_begin(v2),
70  ite = vect_end(v2);
71  for (; it != ite; ++it) *it *= P.diag[it.index()];
72  }
73 
74  template <typename Matrix, typename V2> inline
75  void mult_diag_p(const diagonal_precond<Matrix>& P,V2 &v2, abstract_skyline)
76  { mult_diag_p(P, v2, abstract_sparse()); }
77 
78  template <typename Matrix, typename V2> inline
79  void mult_diag_p(const diagonal_precond<Matrix>& P, V2 &v2, abstract_dense){
80  for (size_type i = 0; i < P.diag.size(); ++i) v2[i] *= P.diag[i];
81  }
82 
83  template <typename Matrix, typename V1, typename V2> inline
84  void mult(const diagonal_precond<Matrix>& P, const V1 &v1, V2 &v2) {
85  GMM_ASSERT2(P.diag.size() == vect_size(v2),"dimensions mismatch");
86  copy(v1, v2);
87  mult_diag_p(P, v2, typename linalg_traits<V2>::storage_type());
88  }
89 
90  template <typename Matrix, typename V1, typename V2> inline
91  void transposed_mult(const diagonal_precond<Matrix>& P,const V1 &v1,V2 &v2) {
92  mult(P, v1, v2);
93  }
94 
95  // # define DIAG_LEFT_MULT_SQRT
96 
97  template <typename Matrix, typename V1, typename V2> inline
98  void left_mult(const diagonal_precond<Matrix>& P, const V1 &v1, V2 &v2) {
99  GMM_ASSERT2(P.diag.size() == vect_size(v2), "dimensions mismatch");
100  copy(v1, v2);
101 # ifdef DIAG_LEFT_MULT_SQRT
102  for (size_type i= 0; i < P.diag.size(); ++i) v2[i] *= gmm::sqrt(P.diag[i]);
103 # else
104  for (size_type i= 0; i < P.diag.size(); ++i) v2[i] *= P.diag[i];
105 # endif
106  }
107 
108  template <typename Matrix, typename V1, typename V2> inline
109  void transposed_left_mult(const diagonal_precond<Matrix>& P,
110  const V1 &v1, V2 &v2)
111  { left_mult(P, v1, v2); }
112 
113  template <typename Matrix, typename V1, typename V2> inline
114  void right_mult(const diagonal_precond<Matrix>& P, const V1 &v1, V2 &v2) {
115  // typedef typename linalg_traits<Matrix>::value_type T;
116  GMM_ASSERT2(P.diag.size() == vect_size(v2), "dimensions mismatch");
117  copy(v1, v2);
118 # ifdef DIAG_LEFT_MULT_SQRT
119  for (size_type i= 0; i < P.diag.size(); ++i) v2[i] *= gmm::sqrt(P.diag[i]);
120 # endif
121  }
122 
123  template <typename Matrix, typename V1, typename V2> inline
124  void transposed_right_mult(const diagonal_precond<Matrix>& P,
125  const V1 &v1, V2 &v2)
126  { right_mult(P, v1, v2); }
127 
128 }
129 
130 #endif
131 
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
gmm preconditioners.
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
Diagonal preconditioner.