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 
48  {
50  }
51 
53  typedef typename C::size_type size_type;
54 
56 
61  {
62  _modified = true;
63  return _weight;
64  }
65 
67 
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 
130  {
131  _modified = true;
132  return _container;
133  }
134 
136  const Container& container() const
137  {
138  return _container;
139  }
140 
142 
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 
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 
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 
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 
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 
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 
358  {
359  using namespace std::placeholders;
360  std::transform(
361  _container.begin(),
362  _container.end(),
363  _container.begin(),
364  std::bind(std::multiplies<T>(),x,_1)
365  );
366  return *this;
367  }
368 
370  size_type nrows () const
371  {
372  return _rows;
373  }
374 
376  size_type ncols () const
377  {
378  return _cols;
379  }
380 
382  template<class X, class R>
383  void umv (const X& x, R& y) const
384  {
385  for (size_type i=0; i<_rows; ++i)
386  {
387  for (size_type j=0; j<_cols; j++)
388  accessBaseContainer(y)[i] += getEntry(i,j) * accessBaseContainer(x)[j];
389  }
390  }
391 
393  template<class X, class R>
394  void usmv (const value_type& alpha, const X& x, R& y) const
395  {
396  for (size_type i=0; i<_rows; ++i)
397  {
398  for (size_type j=0; j<_cols; j++)
399  accessBaseContainer(y)[i] += alpha * getEntry(i,j) * accessBaseContainer(x)[j];
400  }
401  }
402 
405  {
406  return WeightedAccumulationView(*this,weight);
407  }
408 
410 
414  {
415  return _container;
416  }
417 
419 
422  const BaseContainer& base() const
423  {
424  return _container;
425  }
426 
428 
437  {
438  return _container[j*_rows + i];
439  }
440 
442 
451  {
452  return _container[j*_rows + i];
453  }
454 
455  private:
456 
457  std::vector<T> _container;
458  size_type _rows, _cols;
459  };
460 
461  template<class Stream, class T, class W>
462  Stream &operator<<(Stream &stream, const LocalMatrix<T,W> &m) {
463  for(int r = 0; r < m.nrows(); ++r) {
464  if(m.ncols() >= 1)
465  stream << m.getEntry(r, 0);
466  for(int c = 1; c < m.ncols(); ++c)
467  stream << "\t" << m.getEntry(r, c);
468  stream << "\n";
469  }
470  return stream;
471  }
472 
477  } // namespace PDELab
478 } // namespace Dune
479 
480 #endif // DUNE_PDELAB_GRIDOPERATOR_COMMON_LOCALMATRIX_HH
Dune::PDELab::LocalMatrix::base
const BaseContainer & base() const
Returns the underlying storage container (const version).
Definition: localmatrix.hh:422
Dune::PDELab::WeightedMatrixAccumulationView::resetModified
void resetModified()
Resets the modification state of the view to not modified.
Definition: localmatrix.hh:123
Dune::PDELab::LocalMatrix::begin
iterator begin()
Definition: localmatrix.hh:280
Dune::PDELab::WeightedMatrixAccumulationView::weight_type
C::weight_type weight_type
The type of the weight applied when accumulating contributions.
Definition: localmatrix.hh:39
Dune::PDELab::LocalMatrix::BaseContainer
std::vector< T > BaseContainer
The type of the underlying storage container.
Definition: localmatrix.hh:192
Dune::PDELab::LocalMatrix::ncols
size_type ncols() const
Returns the number of columns.
Definition: localmatrix.hh:376
Dune::PDELab::WeightedMatrixAccumulationView::container
const Container & container() const
Returns the container (of type LocalMatrix) that this view is based on (const version).
Definition: localmatrix.hh:136
Dune::PDELab::LocalMatrix::nrows
size_type nrows() const
Returns the number of rows.
Definition: localmatrix.hh:370
Dune::PDELab::LocalMatrix::iterator::col
size_type col() const
Definition: localmatrix.hh:269
Dune::PDELab::LocalMatrix::iterator
Definition: localmatrix.hh:216
Dune::PDELab::WeightedMatrixAccumulationView::WeightedAccumulationView
WeightedMatrixAccumulationView WeightedAccumulationView
Export this type for uniform handling of the containers themselves and their views.
Definition: localmatrix.hh:43
Dune::PDELab::LocalMatrix::WeightedAccumulationView
WeightedMatrixAccumulationView< LocalMatrix > WeightedAccumulationView
An accumulate-only view of this container that automatically applies a weight to all contributions.
Definition: localmatrix.hh:214
Dune
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Dune::PDELab::LocalMatrix::weight_type
W weight_type
The weight type of this container.
Definition: localmatrix.hh:211
Dune::PDELab::WeightedMatrixAccumulationView::accumulate
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
Dune::PDELab::WeightedMatrixAccumulationView::setWeight
void setWeight(weight_type weight)
Resets the weighting coefficient of the view.
Definition: localmatrix.hh:71
Dune::PDELab::LocalMatrix::iterator::increment
void increment()
Definition: localmatrix.hh:242
Dune::PDELab::LocalMatrix::resize
void resize(size_type r, size_type c)
Resize the matrix.
Definition: localmatrix.hh:308
Dune::PDELab::LocalMatrix::size_type
BaseContainer::size_type size_type
The size type of this container.
Definition: localmatrix.hh:198
Dune::PDELab::LocalMatrix::const_reference
BaseContainer::const_reference const_reference
The const reference type of this container.
Definition: localmatrix.hh:204
Dune::PDELab::LocalMatrix::iterator::iterator
iterator(LocalMatrix &m, size_type i, size_type j)
Definition: localmatrix.hh:226
Dune::PDELab::LocalMatrix::value_type
BaseContainer::value_type value_type
The value type of this container.
Definition: localmatrix.hh:195
Dune::PDELab::LocalMatrix::iterator::decrement
void decrement()
Definition: localmatrix.hh:253
Dune::PDELab::WeightedMatrixAccumulationView::size_type
C::size_type size_type
The size_type of the underlying container.
Definition: localmatrix.hh:53
Dune::PDELab::LocalMatrix::iterator::row
size_type row() const
Definition: localmatrix.hh:264
Dune::PDELab::LocalMatrix::LocalMatrix
LocalMatrix()
Default constructor.
Definition: localmatrix.hh:291
Dune::PDELab::WeightedMatrixAccumulationView::ncols
size_type ncols() const
Returns the number of colums of the underlying container.
Definition: localmatrix.hh:107
Dune::PDELab::LocalMatrix::operator()
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: localmatrix.hh:338
Dune::PDELab::LocalMatrix::getEntry
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:450
Dune::PDELab::WeightedMatrixAccumulationView::base
const BaseContainer & base() const
Returns the storage container of the underlying LocalVector (const version).
Definition: localmatrix.hh:155
localvector.hh
Dune::PDELab::LocalMatrix::weightedAccumulationView
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a weighted accumulate-only view of this matrix with the given weight.
Definition: localmatrix.hh:404
Dune::PDELab::WeightedMatrixAccumulationView::value_type
C::value_type value_type
The value type of the entries.
Definition: localmatrix.hh:36
Dune::PDELab::LocalMatrix::iterator::_m
LocalMatrix * _m
Definition: localmatrix.hh:274
Dune::PDELab::LocalMatrix::iterator::_i
size_type _i
Definition: localmatrix.hh:275
Dune::PDELab::WeightedMatrixAccumulationView::base
BaseContainer & base()
Returns the storage container of the underlying LocalMatrix.
Definition: localmatrix.hh:145
Dune::PDELab::LocalMatrix::assign
void assign(size_type r, size_type c, const T &t)
Resize the matrix and assign t to all entries.
Definition: localmatrix.hh:323
Dune::PDELab::WeightedMatrixAccumulationView::nrows
size_type nrows() const
Returns the number of rows of the underlying container.
Definition: localmatrix.hh:101
Dune::PDELab::LocalMatrix
A dense matrix for storing data associated with the degrees of freedom of a pair of LocalFunctionSpac...
Definition: localmatrix.hh:184
Dune::PDELab::LocalMatrix::LocalMatrix
LocalMatrix(size_type r, size_type c)
Construct a LocalMatrix with r rows and c columns.
Definition: localmatrix.hh:294
Dune::PDELab::WeightedMatrixAccumulationView::container
Container & container()
Returns the container (of type LocalMatrix) that this view is based on.
Definition: localmatrix.hh:129
Dune::PDELab::LocalMatrix::iterator::iterator
iterator()
Definition: localmatrix.hh:220
Dune::PDELab::LocalMatrix::base
BaseContainer & base()
Returns the underlying storage container.
Definition: localmatrix.hh:413
Dune::PDELab::WeightedMatrixAccumulationView::Container
C Container
The type of the underlying container.
Definition: localmatrix.hh:27
Dune::PDELab::WeightedMatrixAccumulationView::weight
weight_type weight()
Returns the weight associated with this view.
Definition: localmatrix.hh:60
Dune::PDELab::LocalMatrix::end
iterator end()
Definition: localmatrix.hh:285
Dune::PDELab::operator<<
std::ostream & operator<<(std::ostream &s, const TimeSpec &t)
insert a timespec into an output stream
Definition: clock.cc:39
Dune::PDELab::LocalMatrix::iterator::_j
size_type _j
Definition: localmatrix.hh:276
Dune::PDELab::WeightedMatrixAccumulationView::modified
bool modified() const
Returns whether this view has been written to.
Definition: localmatrix.hh:113
Dune::PDELab::accessBaseContainer
C & accessBaseContainer(C &c)
Definition: localvector.hh:302
Dune::PDELab::WeightedMatrixAccumulationView::BaseContainer
Container::BaseContainer BaseContainer
The type of the storage container underlying the LocalVector.
Definition: localmatrix.hh:33
Dune::PDELab::LocalMatrix::LocalMatrix
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
Dune::PDELab::WeightedMatrixAccumulationView::rawAccumulate
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
Dune::PDELab::WeightedMatrixAccumulationView
An accumulate-only view on a local matrix that automatically takes into account an accumulation weigh...
Definition: localmatrix.hh:21
Dune::PDELab::WeightedMatrixAccumulationView::WeightedMatrixAccumulationView
WeightedMatrixAccumulationView(Container &container, weight_type weight)
Constructor.
Definition: localmatrix.hh:161
Dune::PDELab::WeightedMatrixAccumulationView::weightedAccumulationView
WeightedAccumulationView weightedAccumulationView(weight_type weight)
Returns a WeighedAccumulationView with some weight in addition to this view's weight.
Definition: localmatrix.hh:47
Dune::PDELab::LocalMatrix::umv
void umv(const X &x, R &y) const
y += A x
Definition: localmatrix.hh:383
Dune::PDELab::LocalMatrix::iterator::equals
bool equals(const iterator &other) const
Definition: localmatrix.hh:232
Dune::PDELab::LocalMatrix::operator=
LocalMatrix & operator=(const T &t)
Assign t to all entries of the matrix.
Definition: localmatrix.hh:316
Dune::PDELab::LocalMatrix::usmv
void usmv(const value_type &alpha, const X &x, R &y) const
y += alpha A x
Definition: localmatrix.hh:394
Dune::PDELab::LocalMatrix::operator*=
LocalMatrix & operator*=(const T &x)
Multiplies all entries of the matrix with x.
Definition: localmatrix.hh:357
Dune::PDELab::LocalMatrix::getEntry
value_type & getEntry(size_type i, size_type j)
Direct (unmapped) access to the (i,j)-th entry of the matrix.
Definition: localmatrix.hh:436
Dune::PDELab::LocalMatrix::iterator::dereference
value_type & dereference() const
Definition: localmatrix.hh:237
Dune::PDELab::LocalMatrix::reference
BaseContainer::reference reference
The reference type of this container.
Definition: localmatrix.hh:201