dune-fem  2.8-git
fmatrixconverter.hh
Go to the documentation of this file.
1 #ifndef DUNE_FEM_FIELDMATRIXCONVERTER_HH
2 #define DUNE_FEM_FIELDMATRIXCONVERTER_HH
3 
4 #include <cassert>
5 
6 #include <dune/common/fmatrix.hh>
7 
8 namespace Dune
9 {
10 
11  namespace Fem
12  {
13 
14  // Internal Forward Declarations
15  // -----------------------------
16 
17  template< class VectorType, class ConvertToType >
19 
20  template< class K, int m >
22 
23  template< class K, int n, int m >
24  class FlatFieldMatrix;
25  }
26 
27 
28  template< class K, int n, int m >
29  struct DenseMatVecTraits< Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
30  {
32  typedef DenseMatVecTraits< FieldMatrix< K, n, m > > Traits;
33  typedef typename Traits::container_type container_type;
34  typedef typename Traits::value_type value_type;
35  typedef typename Traits::size_type size_type;
36  typedef typename Traits::row_type row_type;
37 
40  };
41 
42  template< class K, int m >
43  struct DenseMatVecTraits< Fem::FieldMatrixConverterRow< K, m > >
44  {
46  typedef K *container_type;
47  typedef K value_type;
48  typedef size_t size_type;
49  };
50 
51  template< class K, int n, int m >
52  struct DenseMatVecTraits< Fem::FlatFieldMatrix< K, n, m > >
53  : public DenseMatVecTraits< Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
54  {
55  };
56 
57 
58 
59  namespace Fem
60  {
61 
62  // derive from dense vector to inherit functionality
63  template< class K, int m >
65  : public Dune::DenseVector< FieldMatrixConverterRow< K, m > >
66  {
68  typedef Dune::DenseVector< FieldMatrixConverterRow< K, m > > Base;
69 
70  public:
71  typedef typename Base::size_type size_type;
72 
73  using Base::operator=;
74  using Base::operator*;
75 
77  : ptr_( ptr )
78  {
79  assert( ptr_ );
80  }
81 
82  FieldMatrixConverterRow ( const This &other )
83  : ptr_( other.ptr_ )
84  {}
85 
87  {
88  for( size_t i = 0; i < size(); ++i )
89  ptr_[ i ] = other[ i ];
90  return *this;
91  }
92 
93  template< class Impl >
94  FieldMatrixConverterRow &operator= ( const DenseVector< Impl > &other )
95  {
96  assert( other.size() == size() );
97  for( size_t i = 0; i < size(); ++i )
98  ptr_[ i ] = other[ i ];
99  return *this;
100  }
101 
102  template< class V >
103  K operator* ( const DenseVector< V > &x ) const
104  {
105  assert( size() == x.size() );
106  K result( 0 );
107  for( size_type i = 0; i < size(); ++i )
108  result += ptr_[ i ] * x[ i ];
109  return result;
110  }
111 
112  // make this thing a vector
113  size_t size () const { return m; }
114  K &operator[] ( size_t i ) { assert( i < size() ); return ptr_[ i ]; }
115  const K &operator[] ( size_t i ) const { assert( i < size() ); return ptr_[ i ]; }
116 
117  private:
118  K *ptr_;
119  };
120 
121 
122 
124  template< typename K, int n, int m >
125  class FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > >
126  : public Dune::DenseMatrix< FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
127  {
128  typedef Dune::DenseMatrix< FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > > Base;
129 
130  public:
132  typedef FieldVector< K, n *m > InteralVectorType;
133 
135  //typedef FieldMatrixConverterRow< K , m> RowType;
136 
137  typedef typename Base::row_type row_type;
138  typedef typename Base::row_reference row_reference;
139  typedef typename Base::const_row_reference const_row_reference;
140 
142  typedef K field_type;
143 
145  typedef K block_type;
146 
148  typedef std::size_t size_type;
149 
150  enum {
152  rows = n,
154  cols = m,
156  dimension = n * m
157  };
158 
160  : vec_( &v )
161 #ifndef NDEBUG
162  , mutableVec_( true )
163 #endif
164  {}
165 
167  : vec_( const_cast< InteralVectorType * >( &v ) )
168 #ifndef NDEBUG
169  , mutableVec_( false )
170 #endif
171  {}
172 
174  : vec_( other.vec_ )
175 #ifndef NDEBUG
176  , mutableVec_( other.mutableVec_ )
177 #endif
178  {}
179 
180  FieldMatrixConverter &operator= ( const FieldMatrixConverter &other )
181  {
182  assert( mutableVec_ );
183  vec_ = other.vec_;
184  #ifndef NDEBUG
185  mutableVec_ = other.mutableVec_;
186  #endif
187  return *this;
188  }
189 
190  FieldMatrixConverter &operator= ( const FieldMatrix< K, n, m > &matrix )
191  {
192  assert( mutableVec_ );
193  for( int i = 0; i<rows; ++i )
194  {
195  for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
196  {
197  (*vec_)[ ij ] = matrix[ i ][ j ];
198  }
199  }
200 
201  return *this;
202  }
203 
204  FieldMatrixConverter &operator+= ( const FieldMatrix< K, n, m > &matrix )
205  {
206  assert( mutableVec_ );
207  for( int i = 0; i<rows; ++i )
208  {
209  for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
210  {
211  (*vec_)[ ij ] += matrix[ i ][ j ];
212  }
213  }
214  return *this;
215  }
216 
218  friend std::ostream &operator<< ( std::ostream &s,
219  const FieldMatrixConverter &a )
220  {
221  for( size_type i = 0; i < n; ++i )
222  {
223  for( size_type j = 0; j < m; ++j )
224  s << a[ i ][ j ] << " ";
225  s << std::endl;
226  }
227  return s;
228  }
229 
230  // make this thing a matrix
231  size_type mat_rows () const { return rows; }
232  size_type mat_cols () const { return cols; }
233 
235  {
236  assert( i < rows );
237  return row_reference( (&(*vec_)[ i * cols ]) );
238  }
239 
241  {
242  assert( i < rows );
243  return const_row_reference( (&(*vec_)[ i * cols ]) );
244  }
245 
246  protected:
248  #ifndef NDEBUG
250  #endif
251  };
252 
253  template< typename K, int n, int m >
255  : public Dune::DenseMatrix< FlatFieldMatrix< K, n, m > >
256  {
257  typedef Dune::DenseMatrix< FlatFieldMatrix< K, n, m > > Base;
258 
259  public:
261  typedef FieldVector< K, n * m > InteralVectorType;
262 
264  typedef typename Base::row_type row_type;
265  typedef typename Base::row_reference row_reference;
266  typedef typename Base::const_row_reference const_row_reference;
267 
269  typedef K field_type;
270 
272  typedef K block_type;
273 
275  typedef std::size_t size_type;
276 
277  enum {
279  rows = n,
281  cols = m,
283  dimension = n * m
284  };
285 
286  public:
288  FlatFieldMatrix( const K& val )
289  {
290  // all entries == val
291  data_ = val;
292  }
293 
295  {
296  data_ = other.data_;
297  }
298 
299  FlatFieldMatrix( const FieldMatrix< K, n, m >& other )
300  {
301  assign( other );
302  }
303 
305  {
306  data_ = other.data_;
307  return *this;
308  }
309 
310  FlatFieldMatrix &operator= ( const K& val )
311  {
312  data_ = val;
313  return *this;
314  }
315 
316  FlatFieldMatrix &operator= ( const FieldMatrix< K, n, m > &other )
317  {
318  assign( other );
319  return *this;
320  }
321 
322  FlatFieldMatrix &operator+= ( const FieldMatrix< K, n, m > &other )
323  {
324  for( int i = 0; i<rows; ++i )
325  {
326  for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
327  data_[ ij ] += other[ i ][ j ];
328  }
329  return *this;
330  }
331 
332  void assign( const FieldMatrix< K, n, m > &other )
333  {
334  for( int i = 0; i<rows; ++i )
335  {
336  for( int j = 0, ij = i * cols; j<cols; ++j, ++ij )
337  data_[ ij ] = other[ i ][ j ];
338  }
339  }
340 
341  K* data() { return &data_[ 0 ]; }
342  const K* data() const { return &data_[ 0 ]; }
343 
344  // make this thing a matrix
345  size_type mat_rows () const { return rows; }
346  size_type mat_cols () const { return cols; }
347 
349  {
350  assert( i < rows );
351  return row_reference( &(data_[ i * cols ]) );
352  }
353 
355  {
356  assert( i < rows );
357  return const_row_reference( &(*data_[ i * cols ]) );
358  }
359 
361  friend std::ostream &operator<< ( std::ostream &s,
362  const FlatFieldMatrix &a )
363  {
364  for( size_type i = 0; i < n; ++i )
365  {
366  for( size_type j = 0; j < m; ++j )
367  s << a[ i ][ j ] << " ";
368  s << std::endl;
369  }
370  return s;
371  }
372 
373  protected:
375  };
376 
377 
378  } // namespace Fem
379 
380 
381  namespace detail {
382 
383  template < class K, int n, int m, class Converter>
384  inline void toMatrix( FieldMatrix< K, n, m>& A, const Converter& B )
385  {
386  for( int i = 0; i < n; ++i )
387  for( int j = 0; j < m; ++j )
388  A[ i ][ j ] = B[ i ][ j ];
389  }
390  }
391 
392  // method that implements the operator= of a FieldMatrix taking a FieldMatrixConverter
393  template< class K, int n, int m >
394  struct DenseMatrixAssigner< FieldMatrix< K, n, m >,
395  Fem::FieldMatrixConverter< FieldVector< K, n*m >, FieldMatrix< K, n, m > > >
396  {
397  static void apply ( FieldMatrix< K, n, m > &A,
398  const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B )
399  {
400  detail::toMatrix( A, B );
401  }
402  };
403 
404  template< class K, int n, int m >
405  struct DenseMatrixAssigner< DenseMatrix< FieldMatrix< K, n, m > >,
406  Fem::FieldMatrixConverter< FieldVector< K, n*m >, FieldMatrix< K, n, m > > >
407  {
408  static void apply ( DenseMatrix< FieldMatrix< K, n, m > > &A,
409  const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B )
410  {
411  detail::toMatrix( A, B );
412  }
413  };
414 
415  // method that implements the operator= of a FieldMatrix taking a FlatFieldMatrix
416  template< class K, int n, int m >
417  struct DenseMatrixAssigner< FieldMatrix< K, n, m >, Fem::FlatFieldMatrix< K, n, m > >
418  {
419  static void apply ( FieldMatrix< K, n, m > &A,
421  {
422  detail::toMatrix( A, B );
423  }
424  };
425 
426  template< class K, int n, int m >
427  struct DenseMatrixAssigner< DenseMatrix< FieldMatrix< K, n, m > >, Fem::FlatFieldMatrix< K, n, m > >
428  {
429  static void apply ( DenseMatrix< FieldMatrix< K, n, m > > &A,
431  {
432  detail::toMatrix( A, B );
433  }
434  };
435 
436 } // namespace Dune
437 
438 #endif // #ifndef DUNE_FEM_FIELDMATRIXCONVERTER_HH
Definition: bindguard.hh:11
OutStreamInterface< StreamTraits > & operator<<(OutStreamInterface< StreamTraits > &out, const DiscreteFunctionInterface< Impl > &df)
write a discrete function into an output stream
Definition: discretefunction_inline.hh:397
Definition: fmatrixconverter.hh:18
Definition: fmatrixconverter.hh:66
FieldMatrixConverterRow & operator=(const This &other)
Definition: fmatrixconverter.hh:86
FieldMatrixConverterRow(const This &other)
Definition: fmatrixconverter.hh:82
Base::size_type size_type
Definition: fmatrixconverter.hh:71
K operator*(const DenseVector< V > &x) const
Definition: fmatrixconverter.hh:103
K & operator[](size_t i)
Definition: fmatrixconverter.hh:114
size_t size() const
Definition: fmatrixconverter.hh:113
FieldMatrixConverterRow(K *ptr)
Definition: fmatrixconverter.hh:76
Definition: fmatrixconverter.hh:256
const K * data() const
Definition: fmatrixconverter.hh:342
Base::row_type row_type
type of class return upon operator [] which behaves like a reference
Definition: fmatrixconverter.hh:264
row_reference mat_access(size_type i)
Definition: fmatrixconverter.hh:348
size_type mat_rows() const
Definition: fmatrixconverter.hh:345
size_type mat_cols() const
Definition: fmatrixconverter.hh:346
K block_type
export the type representing the components
Definition: fmatrixconverter.hh:272
FlatFieldMatrix(const K &val)
Definition: fmatrixconverter.hh:288
FlatFieldMatrix()
Definition: fmatrixconverter.hh:287
void assign(const FieldMatrix< K, n, m > &other)
Definition: fmatrixconverter.hh:332
Base::const_row_reference const_row_reference
Definition: fmatrixconverter.hh:266
FlatFieldMatrix(const FlatFieldMatrix &other)
Definition: fmatrixconverter.hh:294
FlatFieldMatrix(const FieldMatrix< K, n, m > &other)
Definition: fmatrixconverter.hh:299
const_row_reference mat_access(size_type i) const
Definition: fmatrixconverter.hh:354
FieldVector< K, n *m > InteralVectorType
internal storage of matrix
Definition: fmatrixconverter.hh:261
FlatFieldMatrix & operator+=(const FieldMatrix< K, n, m > &other)
Definition: fmatrixconverter.hh:322
@ cols
The number of columns.
Definition: fmatrixconverter.hh:281
@ dimension
The total dimension.
Definition: fmatrixconverter.hh:283
@ rows
The number of rows.
Definition: fmatrixconverter.hh:279
K * data()
Definition: fmatrixconverter.hh:341
FlatFieldMatrix & operator=(const FlatFieldMatrix &other)
Definition: fmatrixconverter.hh:304
std::size_t size_type
The type used for the index access and size operations.
Definition: fmatrixconverter.hh:275
friend std::ostream & operator<<(std::ostream &s, const FlatFieldMatrix &a)
Sends the matrix to an output stream.
Definition: fmatrixconverter.hh:361
InteralVectorType data_
Definition: fmatrixconverter.hh:374
Base::row_reference row_reference
Definition: fmatrixconverter.hh:265
K field_type
export the type representing the field
Definition: fmatrixconverter.hh:269
Fem::FieldMatrixConverterRow< K, m > const_row_reference
Definition: fmatrixconverter.hh:39
Fem::FieldMatrixConverterRow< K, m > row_reference
Definition: fmatrixconverter.hh:38
DenseMatVecTraits< FieldMatrix< K, n, m > > Traits
Definition: fmatrixconverter.hh:32
Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > derived_type
Definition: fmatrixconverter.hh:31
size_t size_type
Definition: fmatrixconverter.hh:48
Fem::FieldMatrixConverterRow< K, m > derived_type
Definition: fmatrixconverter.hh:45
FieldMatrixConverter(const FieldMatrixConverter &other)
Definition: fmatrixconverter.hh:173
K block_type
export the type representing the components
Definition: fmatrixconverter.hh:145
FieldMatrixConverter(const InteralVectorType &v)
Definition: fmatrixconverter.hh:166
size_type mat_rows() const
Definition: fmatrixconverter.hh:231
const_row_reference mat_access(size_type i) const
Definition: fmatrixconverter.hh:240
InteralVectorType * vec_
Definition: fmatrixconverter.hh:247
row_reference mat_access(size_type i)
Definition: fmatrixconverter.hh:234
Base::row_reference row_reference
Definition: fmatrixconverter.hh:138
Base::const_row_reference const_row_reference
Definition: fmatrixconverter.hh:139
FieldMatrixConverter(InteralVectorType &v)
Definition: fmatrixconverter.hh:159
std::size_t size_type
The type used for the index access and size operations.
Definition: fmatrixconverter.hh:148
size_type mat_cols() const
Definition: fmatrixconverter.hh:232
K field_type
export the type representing the field
Definition: fmatrixconverter.hh:142
FieldVector< K, n *m > InteralVectorType
internal storage of matrix
Definition: fmatrixconverter.hh:132
Base::row_type row_type
type of class return upon operator [] which behaves like a reference
Definition: fmatrixconverter.hh:137
static void apply(FieldMatrix< K, n, m > &A, const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B)
Definition: fmatrixconverter.hh:397
static void apply(DenseMatrix< FieldMatrix< K, n, m > > &A, const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B)
Definition: fmatrixconverter.hh:408
static void apply(FieldMatrix< K, n, m > &A, const Fem::FlatFieldMatrix< K, n, m > &B)
Definition: fmatrixconverter.hh:419
static void apply(DenseMatrix< FieldMatrix< K, n, m > > &A, const Fem::FlatFieldMatrix< K, n, m > &B)
Definition: fmatrixconverter.hh:429