dune-pdelab  2.0.0
simple/matrix.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil -*-
2 #ifndef DUNE_PDELAB_BACKEND_SIMPLE_MATRIX_HH
3 #define DUNE_PDELAB_BACKEND_SIMPLE_MATRIX_HH
4 
5 #include <vector>
6 #include <algorithm>
7 #include <functional>
8 #include <numeric>
9 
10 #include <dune/common/typetraits.hh>
11 #include <dune/common/shared_ptr.hh>
16 
17 namespace Dune {
18  namespace PDELab {
19  namespace simple {
20 
21  template<typename GFSV, typename GFSU, typename C>
23  {
24 
25  public:
26 
27  typedef C Container;
28  typedef typename Container::value_type ElementType;
29  typedef ElementType E;
30 
32  typedef typename Container::size_type size_type;
33 
34  typedef GFSU TrialGridFunctionSpace;
35  typedef GFSV TestGridFunctionSpace;
36 
37  typedef typename GFSV::Ordering::Traits::ContainerIndex RowIndex;
38  typedef typename GFSU::Ordering::Traits::ContainerIndex ColIndex;
39 
40  template<typename RowCache, typename ColCache>
42 
43  template<typename RowCache, typename ColCache>
45 
46 
47  template<typename GO>
48  MatrixContainer(const GO& go)
49  : _rows(go.testGridFunctionSpace().size())
50  , _cols(go.trialGridFunctionSpace().size())
51  , _container(make_shared<Container>(_rows*_cols,E(0)))
52  {}
53 
54  template<typename GO>
55  MatrixContainer(const GO& go, const E& e)
56  : _rows(go.testGridFunctionSpace().size())
57  , _cols(go.trialGridFunctionSpace().size())
58  , _container(make_shared<Container>(_rows*_cols,e))
59  {}
60 
63  : _rows(0)
64  , _cols(0)
65  {}
66 
69  : _rows(0)
70  , _cols(0)
71  , _container(make_shared<Container>())
72  {}
73 
75  : _rows(rhs._rows)
76  , _cols(rhs._cols)
77  , _container(make_shared<Container>(*(rhs._container)))
78  {}
79 
81  {
82  if (this == &rhs)
83  return *this;
84  if (_rows == 0 && _cols == 0)
85  {
86  _rows = rhs._rows;
87  _cols = rhs._cols;
88  }
89  if (attached())
90  {
91  (*_container) = (*(rhs._container));
92  }
93  else
94  {
95  _container = make_shared<Container>(*(rhs._container));
96  }
97  return *this;
98  }
99 
100  void detach()
101  {
102  _container.reset();
103  }
104 
105  void attach(shared_ptr<Container> container)
106  {
107  _container = container;
108  }
109 
110  bool attached() const
111  {
112  return bool(_container);
113  }
114 
115  const shared_ptr<Container>& storage() const
116  {
117  return _container;
118  }
119 
120  size_type N() const
121  {
122  return _rows;
123  }
124 
125  size_type M() const
126  {
127  return _cols;
128  }
129 
131  {
132  std::fill(_container->begin(),_container->end(),e);
133  return *this;
134  }
135 
137  {
138  using namespace std::placeholders;
139  std::transform(_container->begin(),_container->end(),_container->begin(),std::bind(std::multiplies<E>(),e,_1));
140  return *this;
141  }
142 
143  template<typename V>
144  void mv(const V& x, V& y) const
145  {
146  auto rowit = _container->begin();
147  for (auto& v : y)
148  {
149  v = std::inner_product(rowit,rowit + _cols,x.begin(),E(0));
150  rowit += _cols;
151  }
152  }
153 
154  template<typename V>
155  void usmv(const E alpha, const V& x, V& y) const
156  {
157  auto rowit = _container->begin();
158  for (auto& v : y)
159  {
160  v += alpha * std::inner_product(rowit,rowit + _cols,x.begin(),E(0));
161  rowit += _cols;
162  }
163  }
164 
165  E& operator()(const RowIndex& ri, const ColIndex& ci)
166  {
167  return (*_container)[ri[0]*_cols + ci[0]];
168  }
169 
170  const E& operator()(const RowIndex& ri, const ColIndex& ci) const
171  {
172  return (*_container)[ri[0]*_cols + ci[0]];
173  }
174 
175  const Container& base() const
176  {
177  return *_container;
178  }
179 
181  {
182  return *_container;
183  }
184 
185  void flush()
186  {}
187 
188  void finalize()
189  {}
190 
191  void clear_row(const RowIndex& ri, const E& diagonal_entry)
192  {
193  std::fill(_container->begin() + ri[0]*_cols,_container->begin() + (ri[0]+1)*_cols,E(0));
194  (*this)(ri,ri) = diagonal_entry;
195  }
196 
197  private:
198 
199  std::size_t _rows;
200  std::size_t _cols;
201  shared_ptr<Container> _container;
202 
203  };
204 
205  } // namespace simple
206 
207  } // namespace PDELab
208 } // namespace Dune
209 
210 #endif // DUNE_PDELAB_BACKEND_SIMPLE_MATRIX_HH
Definition: uncachedmatrixview.hh:14
ElementType E
Definition: simple/matrix.hh:29
Definition: uncachedmatrixview.hh:159
const shared_ptr< Container > & storage() const
Definition: simple/matrix.hh:115
E & operator()(const RowIndex &ri, const ColIndex &ci)
Definition: simple/matrix.hh:165
Container & base()
Definition: simple/matrix.hh:180
size_type M() const
Definition: simple/matrix.hh:125
Tag for requesting a vector or matrix container with a pre-attached underlying object.
Definition: backend/tags.hh:27
size_type N() const
Definition: simple/matrix.hh:120
MatrixContainer & operator=(const E &e)
Definition: simple/matrix.hh:130
MatrixContainer(tags::attached_container)
Creates an ISTLMatrixContainer with an empty underlying ISTL matrix.
Definition: simple/matrix.hh:68
GFSU TrialGridFunctionSpace
Definition: simple/matrix.hh:34
GFSU::Ordering::Traits::ContainerIndex ColIndex
Definition: simple/matrix.hh:38
void flush()
Definition: simple/matrix.hh:185
void mv(const V &x, V &y) const
Definition: simple/matrix.hh:144
Definition: simple/matrix.hh:22
void finalize()
Definition: simple/matrix.hh:188
Tag for requesting a vector or matrix container without a pre-attached underlying object...
Definition: backend/tags.hh:23
void usmv(const E alpha, const V &x, V &y) const
Definition: simple/matrix.hh:155
ElementType field_type
Definition: simple/matrix.hh:31
Container::value_type ElementType
Definition: simple/matrix.hh:28
const E & operator()(const RowIndex &ri, const ColIndex &ci) const
Definition: simple/matrix.hh:170
const Container & base() const
Definition: simple/matrix.hh:175
GFSV::Ordering::Traits::ContainerIndex RowIndex
Definition: simple/matrix.hh:37
MatrixContainer(const GO &go, const E &e)
Definition: simple/matrix.hh:55
MatrixContainer & operator*=(const E &e)
Definition: simple/matrix.hh:136
C Container
Definition: simple/matrix.hh:27
MatrixContainer & operator=(const MatrixContainer &rhs)
Definition: simple/matrix.hh:80
MatrixContainer(const GO &go)
Definition: simple/matrix.hh:48
GFSV TestGridFunctionSpace
Definition: simple/matrix.hh:35
MatrixContainer(const MatrixContainer &rhs)
Definition: simple/matrix.hh:74
Container::size_type size_type
Definition: simple/matrix.hh:32
void clear_row(const RowIndex &ri, const E &diagonal_entry)
Definition: simple/matrix.hh:191
const E & e
Definition: interpolate.hh:172
bool attached() const
Definition: simple/matrix.hh:110
void attach(shared_ptr< Container > container)
Definition: simple/matrix.hh:105
MatrixContainer(tags::unattached_container=tags::unattached_container())
Creates an ISTLMatrixContainer without allocating an underlying ISTL matrix.
Definition: simple/matrix.hh:62
void detach()
Definition: simple/matrix.hh:100
Various tags for influencing backend behavior.