1#ifndef DUNE_FEM_FIELDMATRIXCONVERTER_HH
2#define DUNE_FEM_FIELDMATRIXCONVERTER_HH
17 template<
class VectorType,
class ConvertToType >
18 class FieldMatrixConverter;
20 template<
class K,
int m >
21 class FieldMatrixConverterRow;
23 template<
class K,
int n,
int m >
24 class FlatFieldMatrix;
28 template<
class K,
int n,
int m >
29 struct DenseMatVecTraits< Fem::FieldMatrixConverter< FieldVector< K, n *m >, FieldMatrix< K, n, m > > >
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;
38 typedef Fem::FieldMatrixConverterRow< K, m > row_reference;
39 typedef Fem::FieldMatrixConverterRow< K, m > const_row_reference;
42 template<
class K,
int m >
43 struct DenseMatVecTraits< Fem::FieldMatrixConverterRow< K, m > >
45 typedef Fem::FieldMatrixConverterRow< K, m > derived_type;
46 typedef K *container_type;
48 typedef size_t size_type;
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 > > >
63 template<
class K,
int m >
64 class FieldMatrixConverterRow
67 typedef FieldMatrixConverterRow< K, m > This;
73 using Base::operator=;
74 using Base::operator*;
76 FieldMatrixConverterRow ( K *ptr )
82 FieldMatrixConverterRow (
const This &other )
86 FieldMatrixConverterRow &operator= (
const This &other )
88 for(
size_t i = 0; i <
size(); ++i )
89 ptr_[ i ] = other[ i ];
93 template<
class Impl >
94 FieldMatrixConverterRow &operator= (
const DenseVector< Impl > &other )
96 assert( other.size() ==
size() );
97 for(
size_t i = 0; i <
size(); ++i )
98 ptr_[ i ] = other[ i ];
103 K operator* (
const DenseVector< V > &x )
const
105 assert(
size() == x.size() );
107 for( size_type i = 0; i <
size(); ++i )
108 result += ptr_[ i ] * x[ i ];
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 ]; }
124 template<
typename K,
int n,
int m >
159 FieldMatrixConverter ( InteralVectorType &v )
162 , mutableVec_( true )
166 FieldMatrixConverter (
const InteralVectorType &v )
167 : vec_( const_cast< InteralVectorType * >( &v ) )
169 , mutableVec_( false )
173 FieldMatrixConverter (
const FieldMatrixConverter &other )
176 , mutableVec_( other.mutableVec_ )
180 FieldMatrixConverter &operator= (
const FieldMatrixConverter &other )
182 assert( mutableVec_ );
185 mutableVec_ = other.mutableVec_;
192 assert( mutableVec_ );
193 for(
int i = 0; i<rows; ++i )
195 for(
int j = 0, ij = i * cols; j<cols; ++j, ++ij )
197 (*vec_)[ ij ] = matrix[ i ][ j ];
206 assert( mutableVec_ );
207 for(
int i = 0; i<rows; ++i )
209 for(
int j = 0, ij = i * cols; j<cols; ++j, ++ij )
211 (*vec_)[ ij ] += matrix[ i ][ j ];
218 friend std::ostream &operator<< ( std::ostream &s,
219 const FieldMatrixConverter &a )
224 s << a[ i ][ j ] <<
" ";
231 size_type mat_rows ()
const {
return rows; }
232 size_type mat_cols ()
const {
return cols; }
234 row_reference mat_access ( size_type i )
237 return row_reference( (&(*vec_)[ i * cols ]) );
240 const_row_reference mat_access ( size_type i )
const
243 return const_row_reference( (&(*vec_)[ i * cols ]) );
247 InteralVectorType *vec_;
253 template<
typename K,
int n,
int m >
254 class FlatFieldMatrix
261 typedef FieldVector< K, n * m > InteralVectorType;
269 typedef K field_type;
272 typedef K block_type;
275 typedef std::size_t size_type;
288 FlatFieldMatrix(
const K& val )
294 FlatFieldMatrix(
const FlatFieldMatrix& other )
304 FlatFieldMatrix &operator= (
const FlatFieldMatrix &other )
310 FlatFieldMatrix &operator= (
const K& val )
324 for(
int i = 0; i<rows; ++i )
326 for(
int j = 0, ij = i * cols; j<
cols; ++j, ++ij )
327 data_[ ij ] += other[ i ][ j ];
334 for(
int i = 0; i<rows; ++i )
336 for(
int j = 0, ij = i * cols; j<
cols; ++j, ++ij )
337 data_[ ij ] = other[ i ][ j ];
341 K* data() {
return &data_[ 0 ]; }
342 const K* data()
const {
return &data_[ 0 ]; }
345 size_type mat_rows ()
const {
return rows; }
346 size_type mat_cols ()
const {
return cols; }
348 row_reference mat_access ( size_type i )
351 return row_reference( &(data_[ i * cols ]) );
354 const_row_reference mat_access ( size_type i )
const
357 return const_row_reference( &(data_[ i * cols ]) );
361 friend std::ostream &operator<< ( std::ostream &s,
362 const FlatFieldMatrix &a )
364 for( size_type i = 0; i < n; ++i )
366 for( size_type j = 0; j < m; ++j )
367 s << a[ i ][ j ] <<
" ";
374 InteralVectorType data_;
383 template <
class K,
int n,
int m,
class Converter>
384 inline void toMatrix( FieldMatrix< K, n, m>& A,
const Converter& B )
386 for(
int i = 0; i < n; ++i )
387 for(
int j = 0; j < m; ++j )
388 A[ i ][ j ] = B[ i ][ j ];
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 > > >
400 detail::toMatrix( A, B );
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 > > >
411 detail::toMatrix( A, B );
416 template<
class K,
int n,
int m >
417 struct DenseMatrixAssigner< FieldMatrix< K, n, m >, Fem::FlatFieldMatrix< K, n, m > >
420 const Fem::FlatFieldMatrix< K, n, m > &B )
422 detail::toMatrix( A, B );
426 template<
class K,
int n,
int m >
427 struct DenseMatrixAssigner< DenseMatrix< FieldMatrix< K, n, m > >, Fem::FlatFieldMatrix< K, n, m > >
430 const Fem::FlatFieldMatrix< K, n, m > &B )
432 detail::toMatrix( A, B );
A dense n x m matrix.
Definition: densematrix.hh:140
constexpr size_type cols() const
number of columns
Definition: densematrix.hh:715
Traits::row_type row_type
The type used to represent a row (must fulfill the Dune::DenseVector interface)
Definition: densematrix.hh:169
Traits::const_row_reference const_row_reference
The type used to represent a reference to a constant row (usually const row_type &)
Definition: densematrix.hh:175
Traits::row_reference row_reference
The type used to represent a reference to a row (usually row_type &)
Definition: densematrix.hh:172
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:229
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:259
K block_type
export the type representing the components
Definition: fmatrixconverter.hh:145
std::size_t size_type
The type used for the index access and size operations.
Definition: fmatrixconverter.hh:148
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
vector space out of a tensor product of fields.
Definition: fvector.hh:91
Implements a matrix constructed from a given type representing a field and compile-time given number ...
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:447