ug4
matrix_diagonal.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef MATRIX_DIAGONAL_H_
34 #define MATRIX_DIAGONAL_H_
35 
37 
41 namespace ug{
42 template <typename M, typename X, typename Y = X>
43 class MatrixDiagonal : public virtual ILinearOperator<X,Y>,
44  public M
45 {
46  public:
47  // Domain space
49 
50  // Range space
52 
53  // Matrix type
54  typedef M matrix_type;
56 
58 
59  public:
61  m_mo = mo;
62  }
63 
64  // Init Operator J(u)
65  virtual void init(const X& u)
66  {
67  m_mo->init(u);
68  }
69 
70  // Init Operator L
71  virtual void init() { m_mo->init(); }
72 
73  // Apply Operator f = L*u (e.g. d = J(u)*c in iterative scheme)
74  virtual void apply(Y& f, const X& u)
75  {
76  matrix_type &A = m_mo->get_matrix();
77  for(size_t i=0; i<A.num_rows(); i++)
78  f[i] = A(i,i)*u[i];
79  }
80 
81  // Apply Operator, i.e. f = f - L*u;
82  virtual void apply_sub(Y& f, const X& u)
83  {
84  matrix_type &A = m_mo->get_matrix();
85  for(size_t i=0; i<A.num_rows(); i++)
86  f[i] -= A(i,i)*u[i];
87  }
88 };
89 
90 
94 template <typename M, typename X, typename Y = X>
95 class MatrixDiagonalInverse : public virtual ILinearOperator<X,Y>,
96  public M
97 {
98  public:
99  // Domain space
101 
102  // Range space
104 
105  // Matrix type
106  typedef M matrix_type;
108 
110 
111  public:
113  m_mo = mo;
114  }
115 
116  // Init Operator J(u)
117  virtual void init(const X& u)
118  {
119  m_mo->init(u);
120  }
121 
122  // Init Operator L
123  virtual void init() { m_mo->init(); }
124 
125  // Apply Operator f = L*u (e.g. d = J(u)*c in iterative scheme)
126  virtual void apply(Y& f, const X& u)
127  {
128  matrix_type &A = m_mo->get_matrix();
129  for(size_t i=0; i<A.num_rows(); i++)
130  InverseMatMult(f[i], 1.0, A(i,i), u[i]);
131  }
132 
133  // Apply Operator, i.e. f = f - L*u;
134  virtual void apply_sub(Y& f, const X& u)
135  {
136  matrix_type &A = m_mo->get_matrix();
137  typename X::value_type t;
138  for(size_t i=0; i<A.num_rows(); i++)
139  {
140  InverseMatMult(t, 1.0, A(i,i), u[i]);
141  f[i] -= t;
142  }
143 
144  }
145 };
146 
147 }
148 #endif /* MATRIX_DIAGONAL_H_ */
Definition: smart_pointer.h:108
describes a linear mapping X->Y
Definition: linear_operator.h:80
Definition: matrix_diagonal.h:45
Y codomain_function_type
Definition: matrix_diagonal.h:51
virtual void init(const X &u)
init operator depending on a function u
Definition: matrix_diagonal.h:65
SmartPtr< mo_type > m_mo
Definition: matrix_diagonal.h:57
MatrixOperator< M, X, Y > mo_type
Definition: matrix_diagonal.h:55
X domain_function_type
Definition: matrix_diagonal.h:48
M matrix_type
Definition: matrix_diagonal.h:54
virtual void apply(Y &f, const X &u)
Definition: matrix_diagonal.h:74
MatrixDiagonal(SmartPtr< mo_type > mo)
Definition: matrix_diagonal.h:60
virtual void init()
init operator
Definition: matrix_diagonal.h:71
virtual void apply_sub(Y &f, const X &u)
Definition: matrix_diagonal.h:82
Definition: matrix_diagonal.h:97
Y codomain_function_type
Definition: matrix_diagonal.h:103
virtual void apply(Y &f, const X &u)
Definition: matrix_diagonal.h:126
MatrixDiagonalInverse(SmartPtr< mo_type > mo)
Definition: matrix_diagonal.h:112
virtual void apply_sub(Y &f, const X &u)
Definition: matrix_diagonal.h:134
virtual void init(const X &u)
init operator depending on a function u
Definition: matrix_diagonal.h:117
SmartPtr< mo_type > m_mo
Definition: matrix_diagonal.h:109
virtual void init()
init operator
Definition: matrix_diagonal.h:123
M matrix_type
Definition: matrix_diagonal.h:106
MatrixOperator< M, X, Y > mo_type
Definition: matrix_diagonal.h:107
X domain_function_type
Definition: matrix_diagonal.h:100
Definition: matrix_operator.h:49
the ug namespace
bool InverseMatMult(number &dest, const double &beta, const TMat &mat, const TVec &vec)
you can implement this function with GetInverse and MatMult
T value_type
Definition: sparsematrix_interface.h:2