dune-fem  2.4.1-rc
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  }
24 
25 
26  template< class K, int n, int m >
27  struct DenseMatVecTraits< Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
28  {
30  typedef DenseMatVecTraits< FieldMatrix< K, n, m > > Traits;
31  typedef typename Traits::container_type container_type;
32  typedef typename Traits::value_type value_type;
33  typedef typename Traits::size_type size_type;
34  typedef typename Traits::row_type row_type;
35 
38  };
39 
40  template< class K, int m >
41  struct DenseMatVecTraits< Fem::FieldMatrixConverterRow< K, m > >
42  {
44  typedef K *container_type;
45  typedef K value_type;
46  typedef size_t size_type;
47  };
48 
49 
50  namespace Fem
51  {
52 
53  // derive from dense vector to inherit functionality
54  template< class K, int m >
55  class FieldMatrixConverterRow
56  : public Dune::DenseVector< FieldMatrixConverterRow< K, m > >
57  {
58  typedef FieldMatrixConverterRow< K, m > This;
59  typedef Dune::DenseVector< FieldMatrixConverterRow< K, m > > Base;
60 
61  public:
62  typedef typename Base::size_type size_type;
63 
64  using Base::operator=;
65  using Base::operator*;
66 
68  : ptr_( ptr )
69  {
70  assert( ptr_ );
71  }
72 
73  FieldMatrixConverterRow ( const This &other )
74  : ptr_( other.ptr_ )
75  {}
76 
77  FieldMatrixConverterRow &operator= ( const This &other )
78  {
79  for( size_t i = 0; i < vec_size(); ++i )
80  vec_access( i ) = other[ i ];
81  return *this;
82  }
83 
84  template< class Impl >
85  FieldMatrixConverterRow &operator= ( const DenseVector< Impl > &other )
86  {
87  assert( other.size() == vec_size() );
88  for( size_t i = 0; i < vec_size(); ++i )
89  vec_access( i ) = other[ i ];
90  return *this;
91  }
92 
93  template< class V >
94  K operator* ( const DenseVector< V > &x ) const
95  {
96  assert( vec_size() == x.size() );
97  K result( 0 );
98  for( size_type i = 0; i < vec_size(); ++i )
99  result += vec_access( i ) * x[ i ];
100  return result;
101  }
102 
103  // make this thing a vector
104  size_t vec_size () const { return m; }
105  K &vec_access ( size_t i ) { assert( i < vec_size() ); return ptr_[ i ]; }
106  const K &vec_access ( size_t i ) const { assert( i < vec_size() ); return ptr_[ i ]; }
107 
108  private:
109  K *ptr_;
110  };
111 
112 
113 
114 
116  template< typename K, int n, int m >
117  class FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > >
118  : public Dune::DenseMatrix< FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
119  {
120  typedef Dune::DenseMatrix< FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > > Base;
121 
122  public:
124  typedef FieldVector< K, n *m > InteralVectorType;
125 
127  //typedef FieldMatrixConverterRow< K , m> RowType;
128 
129  typedef typename Base::row_type row_type;
130  typedef typename Base::row_reference row_reference;
131  typedef typename Base::const_row_reference const_row_reference;
132 
134  typedef K field_type;
135 
137  typedef K block_type;
138 
140  typedef std::size_t size_type;
141 
142  enum {
144  rows = n,
146  cols = m,
148  dimension = n * m
149  };
150 
151  FieldMatrixConverter ( InteralVectorType &v )
152  : vec_( &v )
153 #ifndef NDEBUG
154  , mutableVec_( true )
155 #endif
156  {}
157 
158  FieldMatrixConverter ( const InteralVectorType &v )
159  : vec_( const_cast< InteralVectorType * >( &v ) )
160 #ifndef NDEBUG
161  , mutableVec_( false )
162 #endif
163  {}
164 
166  : vec_( other.vec_ )
167 #ifndef NDEBUG
168  , mutableVec_( other.mutableVec_ )
169 #endif
170  {}
171 
172  FieldMatrixConverter &operator= ( const FieldMatrixConverter &other )
173  {
174  assert( mutableVec_ );
175  vec_ = other.vec_;
176  #ifndef NDEBUG
177  mutableVec_ = other.mutableVec_;
178  #endif
179  return *this;
180  }
181 
182  FieldMatrixConverter &operator= ( const FieldMatrix< K, n, m > &matrix )
183  {
184  assert( mutableVec_ );
185  for( size_t i = 0; i < rows; ++i )
186  for( size_t j = 0; j < cols; ++j )
187  (*vec_)[ i * cols + j ] = matrix[ i ][ j ];
188 
189  return *this;
190  }
191  FieldMatrixConverter &operator+= ( const FieldMatrix< K, n, m > &matrix )
192  {
193  assert( mutableVec_ );
194  for( size_t i = 0; i < rows; ++i )
195  for( size_t j = 0; j < cols; ++j )
196  (*vec_)[ i * cols + j ] += matrix[ i ][ j ];
197 
198  return *this;
199  }
200 
202  friend std::ostream &operator<< ( std::ostream &s,
203  const FieldMatrixConverter &a )
204  {
205  for( size_type i = 0; i < n; ++i )
206  {
207  for( size_type j = 0; j < m; ++j )
208  s << a[ i ][ j ] << " ";
209  s << std::endl;
210  }
211  return s;
212  }
213 
214  // make this thing a matrix
215  size_type mat_rows () const { return rows; }
216  size_type mat_cols () const { return cols; }
217 
218  row_reference mat_access ( size_type i )
219  {
220  assert( i < rows );
221  return row_reference( (&(*vec_)[ i * cols ]) );
222  }
223 
224  const_row_reference mat_access ( size_type i ) const
225  {
226  assert( i < rows );
227  return const_row_reference( (&(*vec_)[ i * cols ]) );
228  }
229 
230  protected:
231  InteralVectorType *vec_;
232  #ifndef NDEBUG
234  #endif
235  };
236 
237  } // namespace Fem
238 
239  // method that implements the operator= of a FieldMatrix taking a FieldMatrixConverter
240  template< class K, int n, int m >
241  inline void istl_assign_to_fmatrix ( FieldMatrix< K, n, m > &A,
242  const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B )
243  {
244  for( size_t i = 0; i < n; ++i )
245  for( size_t j = 0; j < m; ++j )
246  A[ i ][ j ] = B[ i ][ j ];
247 
248  }
249  template< class K, int n, int m >
250  inline void istl_assign_to_fmatrix ( DenseMatrix< FieldMatrix< K, n, m > > &A,
251  const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B )
252  {
253  for( size_t i = 0; i < n; ++i )
254  for( size_t j = 0; j < m; ++j )
255  A[ i ][ j ] = B[ i ][ j ];
256 
257  }
258 
259 } // namespace Dune
260 
261 #endif // #ifndef DUNE_FEM_FIELDMATRIXCONVERTER_HH
Base::row_type row_type
type of class return upon operator [] which behaves like a reference
Definition: fmatrixconverter.hh:129
Base::size_type size_type
Definition: fmatrixconverter.hh:62
size_t size_type
Definition: fmatrixconverter.hh:46
Definition: fmatrixconverter.hh:21
const_row_reference mat_access(size_type i) const
Definition: fmatrixconverter.hh:224
Fem::FieldMatrixConverterRow< K, m > row_reference
Definition: fmatrixconverter.hh:36
Base::const_row_reference const_row_reference
Definition: fmatrixconverter.hh:131
size_type mat_cols() const
Definition: fmatrixconverter.hh:216
K & vec_access(size_t i)
Definition: fmatrixconverter.hh:105
Fem::FieldMatrixConverterRow< K, m > const_row_reference
Definition: fmatrixconverter.hh:37
row_reference mat_access(size_type i)
Definition: fmatrixconverter.hh:218
InteralVectorType * vec_
Definition: fmatrixconverter.hh:231
size_type mat_rows() const
Definition: fmatrixconverter.hh:215
FieldMatrixConverterRow(K *ptr)
Definition: fmatrixconverter.hh:67
FieldMatrixConverter(InteralVectorType &v)
Definition: fmatrixconverter.hh:151
Definition: coordinate.hh:4
void istl_assign_to_fmatrix(FieldMatrix< K, n, m > &A, const Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > &B)
Definition: fmatrixconverter.hh:241
Double operator*(const Double &a, const Double &b)
Definition: double.hh:495
K block_type
export the type representing the components
Definition: fmatrixconverter.hh:137
K field_type
export the type representing the field
Definition: fmatrixconverter.hh:134
FieldMatrixConverter(const InteralVectorType &v)
Definition: fmatrixconverter.hh:158
Base::row_reference row_reference
Definition: fmatrixconverter.hh:130
DenseMatVecTraits< FieldMatrix< K, n, m > > Traits
Definition: fmatrixconverter.hh:30
Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > derived_type
Definition: fmatrixconverter.hh:29
size_t vec_size() const
Definition: fmatrixconverter.hh:104
FieldVector< K, n *m > InteralVectorType
internal storage of matrix
Definition: fmatrixconverter.hh:124
FieldMatrixConverter(const FieldMatrixConverter &other)
Definition: fmatrixconverter.hh:165
Definition: fmatrixconverter.hh:18
const K & vec_access(size_t i) const
Definition: fmatrixconverter.hh:106
Fem::FieldMatrixConverterRow< K, m > derived_type
Definition: fmatrixconverter.hh:43
OutStreamInterface< StreamTraits > & operator<<(OutStreamInterface< StreamTraits > &out, const DiscreteFunctionInterface< Impl > &df)
write a discrete function into an output stream
Definition: discretefunction_inline.hh:375
std::size_t size_type
The type used for the index access and size operations.
Definition: fmatrixconverter.hh:140
FieldMatrixConverterRow(const This &other)
Definition: fmatrixconverter.hh:73