dune-pdelab  2.5-dev
diagonallocalmatrix.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 
4 #ifndef DUNE_PDELAB_GRIDOPERATOR_COMMON_DIAGONALLOCALMATRIX_HH
5 #define DUNE_PDELAB_GRIDOPERATOR_COMMON_DIAGONALLOCALMATRIX_HH
6 
8 
9 namespace Dune {
10  namespace PDELab {
11 
18  template<typename T, typename W = T>
29  {
30  public:
31 
33 
36  typedef std::vector<T> BaseContainer;
37 
39  typedef typename BaseContainer::value_type value_type;
40 
42  typedef typename BaseContainer::size_type size_type;
43 
45  typedef typename BaseContainer::reference reference;
46 
48  typedef typename BaseContainer::const_reference const_reference;
49 
51 
55  typedef W weight_type;
56 
59 
60  struct iterator
61  : public Dune::BidirectionalIteratorFacade<iterator,value_type>
62  {
63 
65  : _m(nullptr)
66  , _i(0)
67  {}
68 
69  iterator(DiagonalLocalMatrix& m, size_type i)
70  : _m(&m)
71  , _i(i)
72  {}
73 
74  bool equals(const iterator& other) const
75  {
76  return _m == other._m && _i == other._i;
77  }
78 
79  value_type& dereference() const
80  {
81  return _m->getEntry(_i,_i);
82  }
83 
84  void increment()
85  {
86  ++_i;
87  }
88 
89  void decrement()
90  {
91  --_i;
92  }
93 
94  size_type row() const
95  {
96  return _i;
97  }
98 
99  size_type col() const
100  {
101  return _i;
102  }
103 
105  size_type _i;
106 
107  };
108 
110  {
111  return iterator(*this,0);
112  }
113 
115  {
116  return iterator(*this,nrows());
117  }
118 
121 
123  DiagonalLocalMatrix (size_type r, size_type c)
124  : _container(r)
125  , _rows(r)
126  , _cols(c)
127  {
128  assert(r == c);
129  }
130 
132  DiagonalLocalMatrix (size_type r, size_type c, const T& t)
133  : _container(r,t)
134  , _rows(r)
135  , _cols(c)
136  {
137  assert(r == c);
138  }
139 
141  void resize (size_type r, size_type c)
142  {
143  assert(r == c);
144  _container.resize(r);
145  _rows = r;
146  _cols = c;
147  }
148 
151  {
152  std::fill(_container.begin(),_container.end(),t);
153  return *this;
154  }
155 
157  void assign (size_type r, size_type c, const T& t)
158  {
159  assert(r == c);
160  _container.assign(r,t);
161  _rows = r;
162  _cols = c;
163  }
164 
166 
172  template<typename LFSU, typename LFSV>
173  T& operator() (const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j)
174  {
175  return getEntry(lfsv.localIndex(i),lfsu.localIndex(j));
176  }
177 
179 
185  template<typename LFSU, typename LFSV>
186  const T& operator() (const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j) const
187  {
188  return getEntry(lfsv.localIndex(i),lfsu.localIndex(j));
189  }
190 
193  {
194  std::transform(_container.begin(),_container.end(),_container.begin(),std::bind1st(std::multiplies<T>(),x));
195  return *this;
196  }
197 
199  size_type nrows () const
200  {
201  return _rows;
202  }
203 
205  size_type ncols () const
206  {
207  return _cols;
208  }
209 
211  template<class X, class R>
212  void umv (const X& x, R& y) const
213  {
214  for (size_type i=0; i<_rows; ++i)
215  {
216  for (size_type j=0; j<_cols; j++)
217  accessBaseContainer(y)[i] += getEntry(i,j) * accessBaseContainer(x)[j];
218  }
219  }
220 
222  template<class X, class R>
223  void usmv (const value_type& alpha, const X& x, R& y) const
224  {
225  for (size_type i=0; i<_rows; ++i)
226  {
227  for (size_type j=0; j<_cols; j++)
228  accessBaseContainer(y)[i] += alpha * getEntry(i,j) * accessBaseContainer(x)[j];
229  }
230  }
231 
233  WeightedAccumulationView weightedAccumulationView(weight_type weight)
234  {
235  return WeightedAccumulationView(*this,weight);
236  }
237 
239 
242  BaseContainer& base()
243  {
244  return _container;
245  }
246 
248 
251  const BaseContainer& base() const
252  {
253  return _container;
254  }
255 
257 
265  value_type& getEntry(size_type i, size_type j)
266  {
267  assert(i == j);
268  return _container[i];
269  }
270 
272 
280  const value_type& getEntry(size_type i, size_type j) const
281  {
282  assert(i == j);
283  return _container[i];
284  }
285 
286  private:
287 
288  std::vector<T> _container;
289  size_type _rows, _cols;
290  };
291 
296  } // namespace PDELab
297 } // namespace Dune
298 
299 #endif // DUNE_PDELAB_GRIDOPERATOR_COMMON_DIAGONALLOCALMATRIX_HH
T & operator()(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j)
Access the value associated with the i-th DOF of lfsv and the j-th DOF of lfsu.
Definition: diagonallocalmatrix.hh:173
const value_type & getEntry(size_type i, size_type j) const
Direct (unmapped) access to the (i,j)-th entry of the matrix (const version).
Definition: diagonallocalmatrix.hh:280
size_type col() const
Definition: diagonallocalmatrix.hh:99
BaseContainer::value_type value_type
The value type of this container.
Definition: diagonallocalmatrix.hh:39
DiagonalLocalMatrix & operator*=(const T &x)
Multiplies all entries of the matrix with x.
Definition: diagonallocalmatrix.hh:192
value_type & dereference() const
Definition: diagonallocalmatrix.hh:79
C & accessBaseContainer(C &c)
Definition: localvector.hh:296
void umv(const X &x, R &y) const
y += A x
Definition: diagonallocalmatrix.hh:212
iterator end()
Definition: diagonallocalmatrix.hh:114
W weight_type
The weight type of this container.
Definition: diagonallocalmatrix.hh:55
void usmv(const value_type &alpha, const X &x, R &y) const
y += alpha A x
Definition: diagonallocalmatrix.hh:223
BaseContainer & base()
Returns the underlying storage container.
Definition: diagonallocalmatrix.hh:242
WeightedMatrixAccumulationView< DiagonalLocalMatrix > WeightedAccumulationView
An accumulate-only view of this container that automatically applies a weight to all contributions...
Definition: diagonallocalmatrix.hh:58
void assign(size_type r, size_type c, const T &t)
Resize the matrix and assign t to all entries.
Definition: diagonallocalmatrix.hh:157
A dense matrix for storing data associated with the degrees of freedom of a pair of LocalFunctionSpac...
Definition: diagonallocalmatrix.hh:28
void increment()
Definition: diagonallocalmatrix.hh:84
DiagonalLocalMatrix()
Default constructor.
Definition: diagonallocalmatrix.hh:120
An accumulate-only view on a local matrix that automatically takes into account an accumulation weigh...
Definition: localmatrix.hh:21
DiagonalLocalMatrix(size_type r, size_type c, const T &t)
Construct a LocalMatrix with r rows and c columns and initialize its entries with t...
Definition: diagonallocalmatrix.hh:132
size_type row() const
Definition: diagonallocalmatrix.hh:94
DiagonalLocalMatrix * _m
Definition: diagonallocalmatrix.hh:104
size_type _i
Definition: diagonallocalmatrix.hh:105
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
const BaseContainer & base() const
Returns the underlying storage container (const version).
Definition: diagonallocalmatrix.hh:251
std::vector< T > BaseContainer
The type of the underlying storage container.
Definition: diagonallocalmatrix.hh:36
void decrement()
Definition: diagonallocalmatrix.hh:89
size_type nrows() const
Returns the number of rows.
Definition: diagonallocalmatrix.hh:199
Definition: diagonallocalmatrix.hh:60
value_type & getEntry(size_type i, size_type j)
Direct (unmapped) access to the (i,j)-th entry of the matrix.
Definition: diagonallocalmatrix.hh:265
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a weighted accumulate-only view of this matrix with the given weight.
Definition: diagonallocalmatrix.hh:233
iterator()
Definition: diagonallocalmatrix.hh:64
DiagonalLocalMatrix & operator=(const T &t)
Assign t to all entries of the matrix.
Definition: diagonallocalmatrix.hh:150
DiagonalLocalMatrix(size_type r, size_type c)
Construct a LocalMatrix with r rows and c columns.
Definition: diagonallocalmatrix.hh:123
iterator begin()
Definition: diagonallocalmatrix.hh:109
size_type ncols() const
Returns the number of columns.
Definition: diagonallocalmatrix.hh:205
iterator(DiagonalLocalMatrix &m, size_type i)
Definition: diagonallocalmatrix.hh:69
BaseContainer::reference reference
The reference type of this container.
Definition: diagonallocalmatrix.hh:45
void resize(size_type r, size_type c)
Resize the matrix.
Definition: diagonallocalmatrix.hh:141
bool equals(const iterator &other) const
Definition: diagonallocalmatrix.hh:74
BaseContainer::const_reference const_reference
The const reference type of this container.
Definition: diagonallocalmatrix.hh:48
BaseContainer::size_type size_type
The size type of this container.
Definition: diagonallocalmatrix.hh:42