dune-pdelab  2.5-dev
localmatrix.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_LOCALMATRIX_HH
5 #define DUNE_PDELAB_GRIDOPERATOR_COMMON_LOCALMATRIX_HH
6 
7 #include <dune/common/iteratorfacades.hh>
8 
10 
11 namespace Dune {
12  namespace PDELab {
13 
19  template<typename C>
22  {
23 
24  public:
25 
27  typedef C Container;
28 
30 
33  typedef typename Container::BaseContainer BaseContainer;
34 
36  typedef typename C::value_type value_type;
37 
39  typedef typename C::weight_type weight_type;
40 
44 
47  WeightedAccumulationView weightedAccumulationView(weight_type weight)
48  {
49  return WeightedAccumulationView(container(),weight*this->weight());
50  }
51 
53  typedef typename C::size_type size_type;
54 
56 
60  weight_type weight()
61  {
62  _modified = true;
63  return _weight;
64  }
65 
67 
71  void setWeight(weight_type weight)
72  {
73  _weight = weight;
74  }
75 
77  template<typename LFSU, typename LFSV>
78  void accumulate(const LFSV& lfsv, size_type i,
79  const LFSU& lfsu, size_type j,
80  value_type v)
81  {
82  _modified = true;
83  _container(lfsv,i,lfsu,j) += _weight * v;
84  }
85 
87 
91  template<typename LFSU, typename LFSV>
92  void rawAccumulate(const LFSV& lfsv, size_type i,
93  const LFSU& lfsu, size_type j,
94  value_type v)
95  {
96  _modified = true;
97  _container(lfsv,i,lfsu,j) += v;
98  }
99 
101  size_type nrows() const
102  {
103  return _container.nrows();
104  }
105 
107  size_type ncols() const
108  {
109  return _container.ncols();
110  }
111 
113  bool modified() const
114  {
115  return _modified;
116  }
117 
119 
124  {
125  _modified = false;
126  }
127 
129  Container& container()
130  {
131  _modified = true;
132  return _container;
133  }
134 
136  const Container& container() const
137  {
138  return _container;
139  }
140 
142 
145  BaseContainer& base()
146  {
147  _modified = true;
148  return _container.base();
149  }
150 
152 
155  const BaseContainer& base() const
156  {
157  return _container.base();
158  }
159 
162  : _container(container)
163  , _weight(weight)
164  , _modified(false)
165  {}
166 
167  private:
168  Container& _container;
169  weight_type _weight;
170  bool _modified;
171  };
172 
173 
175  template<typename T, typename W = T>
185  {
186  public:
187 
189 
192  typedef std::vector<T> BaseContainer;
193 
195  typedef typename BaseContainer::value_type value_type;
196 
198  typedef typename BaseContainer::size_type size_type;
199 
201  typedef typename BaseContainer::reference reference;
202 
204  typedef typename BaseContainer::const_reference const_reference;
205 
207 
211  typedef W weight_type;
212 
215 
216  struct iterator
217  : public Dune::BidirectionalIteratorFacade<iterator,value_type>
218  {
219 
221  : _m(nullptr)
222  , _i(0)
223  , _j(0)
224  {}
225 
226  iterator(LocalMatrix& m, size_type i, size_type j)
227  : _m(&m)
228  , _i(i)
229  , _j(j)
230  {}
231 
232  bool equals(const iterator& other) const
233  {
234  return _m == other._m && _i == other._i && _j == other._j;
235  }
236 
237  value_type& dereference() const
238  {
239  return _m->getEntry(_i,_j);
240  }
241 
242  void increment()
243  {
244  if (_j < _m->ncols() - 1)
245  ++_j;
246  else
247  {
248  ++_i;
249  _j = 0;
250  }
251  }
252 
253  void decrement()
254  {
255  if (_j > 0)
256  --_j;
257  else
258  {
259  --_i;
260  _j = _m->ncols() - 1;
261  }
262  }
263 
264  size_type row() const
265  {
266  return _i;
267  }
268 
269  size_type col() const
270  {
271  return _j;
272  }
273 
275  size_type _i;
276  size_type _j;
277 
278  };
279 
280  iterator begin()
281  {
282  return iterator(*this,0,0);
283  }
284 
285  iterator end()
286  {
287  return iterator(*this,nrows(),0);
288  }
289 
292 
294  LocalMatrix (size_type r, size_type c)
295  : _container(r*c)
296  , _rows(r)
297  , _cols(c)
298  {}
299 
301  LocalMatrix (size_type r, size_type c, const T& t)
302  : _container(r*c,t)
303  , _rows(r)
304  , _cols(c)
305  {}
306 
308  void resize (size_type r, size_type c)
309  {
310  _container.resize(r*c);
311  _rows = r;
312  _cols = c;
313  }
314 
316  LocalMatrix& operator=(const T& t)
317  {
318  std::fill(_container.begin(),_container.end(),t);
319  return *this;
320  }
321 
323  void assign (size_type r, size_type c, const T& t)
324  {
325  _container.assign(r*c,t);
326  _rows = r;
327  _cols = c;
328  }
329 
331 
337  template<typename LFSU, typename LFSV>
338  T& operator() (const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j)
339  {
340  return getEntry(lfsv.localIndex(i),lfsu.localIndex(j));
341  }
342 
344 
350  template<typename LFSU, typename LFSV>
351  const T& operator() (const LFSV& lfsv, size_type i, const LFSU& lfsu, size_type j) const
352  {
353  return getEntry(lfsv.localIndex(i),lfsu.localIndex(j));
354  }
355 
357  LocalMatrix& operator *= (const T& x)
358  {
359  std::transform(_container.begin(),_container.end(),_container.begin(),std::bind1st(std::multiplies<T>(),x));
360  return *this;
361  }
362 
364  size_type nrows () const
365  {
366  return _rows;
367  }
368 
370  size_type ncols () const
371  {
372  return _cols;
373  }
374 
376  template<class X, class R>
377  void umv (const X& x, R& y) const
378  {
379  for (size_type i=0; i<_rows; ++i)
380  {
381  for (size_type j=0; j<_cols; j++)
382  accessBaseContainer(y)[i] += getEntry(i,j) * accessBaseContainer(x)[j];
383  }
384  }
385 
387  template<class X, class R>
388  void usmv (const value_type& alpha, const X& x, R& y) const
389  {
390  for (size_type i=0; i<_rows; ++i)
391  {
392  for (size_type j=0; j<_cols; j++)
393  accessBaseContainer(y)[i] += alpha * getEntry(i,j) * accessBaseContainer(x)[j];
394  }
395  }
396 
398  WeightedAccumulationView weightedAccumulationView(weight_type weight)
399  {
400  return WeightedAccumulationView(*this,weight);
401  }
402 
404 
407  BaseContainer& base()
408  {
409  return _container;
410  }
411 
413 
416  const BaseContainer& base() const
417  {
418  return _container;
419  }
420 
422 
430  value_type& getEntry(size_type i, size_type j)
431  {
432  return _container[j*_rows + i];
433  }
434 
436 
444  const value_type& getEntry(size_type i, size_type j) const
445  {
446  return _container[j*_rows + i];
447  }
448 
449  private:
450 
451  std::vector<T> _container;
452  size_type _rows, _cols;
453  };
454 
455  template<class Stream, class T, class W>
456  Stream &operator<<(Stream &stream, const LocalMatrix<T,W> &m) {
457  for(int r = 0; r < m.nrows(); ++r) {
458  if(m.ncols() >= 1)
459  stream << m.getEntry(r, 0);
460  for(int c = 1; c < m.ncols(); ++c)
461  stream << "\t" << m.getEntry(r, c);
462  stream << "\n";
463  }
464  return stream;
465  }
466 
471  } // namespace PDELab
472 } // namespace Dune
473 
474 #endif // DUNE_PDELAB_GRIDOPERATOR_COMMON_LOCALMATRIX_HH
WeightedMatrixAccumulationView(Container &container, weight_type weight)
Constructor.
Definition: localmatrix.hh:161
WeightedMatrixAccumulationView< LocalMatrix > WeightedAccumulationView
An accumulate-only view of this container that automatically applies a weight to all contributions...
Definition: localmatrix.hh:214
LocalMatrix & operator=(const T &t)
Assign t to all entries of the matrix.
Definition: localmatrix.hh:316
LocalMatrix()
Default constructor.
Definition: localmatrix.hh:291
weight_type weight()
Returns the weight associated with this view.
Definition: localmatrix.hh:60
BaseContainer::const_reference const_reference
The const reference type of this container.
Definition: localmatrix.hh:204
size_type row() const
Definition: localmatrix.hh:264
WeightedMatrixAccumulationView WeightedAccumulationView
Export this type for uniform handling of the containers themselves and their views.
Definition: localmatrix.hh:43
size_type ncols() const
Returns the number of colums of the underlying container.
Definition: localmatrix.hh:107
C & accessBaseContainer(C &c)
Definition: localvector.hh:296
const Container & container() const
Returns the container (of type LocalMatrix) that this view is based on (const version).
Definition: localmatrix.hh:136
iterator end()
Definition: localmatrix.hh:285
const BaseContainer & base() const
Returns the underlying storage container (const version).
Definition: localmatrix.hh:416
iterator(LocalMatrix &m, size_type i, size_type j)
Definition: localmatrix.hh:226
void rawAccumulate(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j, value_type v)
Adds v to the (i,j)-th entry of the underlying container without applying the current weight...
Definition: localmatrix.hh:92
void umv(const X &x, R &y) const
y += A x
Definition: localmatrix.hh:377
size_type nrows() const
Returns the number of rows of the underlying container.
Definition: localmatrix.hh:101
C::value_type value_type
The value type of the entries.
Definition: localmatrix.hh:36
value_type & dereference() const
Definition: localmatrix.hh:237
BaseContainer & base()
Returns the underlying storage container.
Definition: localmatrix.hh:407
BaseContainer & base()
Returns the storage container of the underlying LocalMatrix.
Definition: localmatrix.hh:145
An accumulate-only view on a local matrix that automatically takes into account an accumulation weigh...
Definition: localmatrix.hh:21
LocalMatrix(size_type r, size_type c)
Construct a LocalMatrix with r rows and c columns.
Definition: localmatrix.hh:294
size_type _j
Definition: localmatrix.hh:276
BaseContainer::size_type size_type
The size type of this container.
Definition: localmatrix.hh:198
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: localmatrix.hh:444
const BaseContainer & base() const
Returns the storage container of the underlying LocalVector (const version).
Definition: localmatrix.hh:155
For backward compatibility – Do not use this!
Definition: adaptivity.hh:27
W weight_type
The weight type of this container.
Definition: localmatrix.hh:211
void accumulate(const LFSV &lfsv, size_type i, const LFSU &lfsu, size_type j, value_type v)
Applies the current weight to v and adds the result to the matrix entry associated with the i-th entr...
Definition: localmatrix.hh:78
void resetModified()
Resets the modification state of the view to not modified.
Definition: localmatrix.hh:123
Container & container()
Returns the container (of type LocalMatrix) that this view is based on.
Definition: localmatrix.hh:129
void assign(size_type r, size_type c, const T &t)
Resize the matrix and assign t to all entries.
Definition: localmatrix.hh:323
bool modified() const
Returns whether this view has been written to.
Definition: localmatrix.hh:113
void usmv(const value_type &alpha, const X &x, R &y) const
y += alpha A x
Definition: localmatrix.hh:388
Definition: localmatrix.hh:216
iterator()
Definition: localmatrix.hh:220
Container::BaseContainer BaseContainer
The type of the storage container underlying the LocalVector.
Definition: localmatrix.hh:33
bool equals(const iterator &other) const
Definition: localmatrix.hh:232
size_type nrows() const
Returns the number of rows.
Definition: localmatrix.hh:364
C::weight_type weight_type
The type of the weight applied when accumulating contributions.
Definition: localmatrix.hh:39
value_type & getEntry(size_type i, size_type j)
Direct (unmapped) access to the (i,j)-th entry of the matrix.
Definition: localmatrix.hh:430
size_type ncols() const
Returns the number of columns.
Definition: localmatrix.hh:370
A dense matrix for storing data associated with the degrees of freedom of a pair of LocalFunctionSpac...
Definition: localmatrix.hh:184
void setWeight(weight_type weight)
Resets the weighting coefficient of the view.
Definition: localmatrix.hh:71
void increment()
Definition: localmatrix.hh:242
std::vector< T > BaseContainer
The type of the underlying storage container.
Definition: localmatrix.hh:192
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a WeighedAccumulationView with some weight in addition to this view&#39;s weight.
Definition: localmatrix.hh:47
iterator begin()
Definition: localmatrix.hh:280
void decrement()
Definition: localmatrix.hh:253
C::size_type size_type
The size_type of the underlying container.
Definition: localmatrix.hh:53
LocalMatrix * _m
Definition: localmatrix.hh:274
size_type _i
Definition: localmatrix.hh:275
BaseContainer::value_type value_type
The value type of this container.
Definition: localmatrix.hh:195
LocalMatrix(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: localmatrix.hh:301
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a weighted accumulate-only view of this matrix with the given weight.
Definition: localmatrix.hh:398
void resize(size_type r, size_type c)
Resize the matrix.
Definition: localmatrix.hh:308
BaseContainer::reference reference
The reference type of this container.
Definition: localmatrix.hh:201
C Container
The type of the underlying container.
Definition: localmatrix.hh:27
size_type col() const
Definition: localmatrix.hh:269