dune-common  2.6-git
dynmatrix.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 #ifndef DUNE_DYNMATRIX_HH
4 #define DUNE_DYNMATRIX_HH
5 
6 #include <cmath>
7 #include <cstddef>
8 #include <iostream>
9 #include <initializer_list>
10 
13 #include <dune/common/dynvector.hh>
16 
17 namespace Dune
18 {
19 
29  template< class K > class DynamicMatrix;
30 
31  template< class K >
33  {
35 
37 
39  typedef const row_type &const_row_reference;
40 
41  typedef std::vector<K> container_type;
42  typedef K value_type;
43  typedef typename container_type::size_type size_type;
44  };
45 
46  template< class K >
48  {
51  };
52 
57  template<class K>
58  class DynamicMatrix : public DenseMatrix< DynamicMatrix<K> >
59  {
60  std::vector< DynamicVector<K> > _data;
61  typedef DenseMatrix< DynamicMatrix<K> > Base;
62  public:
63  typedef typename Base::size_type size_type;
64  typedef typename Base::value_type value_type;
65  typedef typename Base::row_type row_type;
66 
67  //===== constructors
70 
73  _data(r, row_type(c, v) )
74  {}
75 
78  DynamicMatrix (std::initializer_list<DynamicVector<K>> const &ll)
79  : _data(ll)
80  {}
81 
82 
83  template <class T,
84  typename = std::enable_if_t<!Dune::IsNumber<T>::value && HasDenseMatrixAssigner<DynamicMatrix, T>::value>>
85  DynamicMatrix(T const& rhs)
86  {
87  *this = rhs;
88  }
89 
90  //==== resize related methods
105  {
106  _data.resize(0);
107  _data.resize(r, row_type(c, v) );
108  }
109 
110  //===== assignment
111  // General assignment with resizing
112  template <typename T,
113  typename = std::enable_if_t<!Dune::IsNumber<T>::value>>
114  DynamicMatrix& operator=(T const& rhs) {
115  _data.resize(rhs.N());
116  std::fill(_data.begin(), _data.end(), row_type(rhs.M(), K(0)));
117  Base::operator=(rhs);
118  return *this;
119  }
120 
121  // Specialisation: scalar assignment (no resizing)
122  template <typename T,
123  typename = std::enable_if_t<Dune::IsNumber<T>::value>>
125  std::fill(_data.begin(), _data.end(), scalar);
126  return *this;
127  }
128 
129  // make this thing a matrix
130  size_type mat_rows() const { return _data.size(); }
131  size_type mat_cols() const {
132  assert(this->rows());
133  return _data.front().size();
134  }
136  DUNE_ASSERT_BOUNDS(i < _data.size());
137  return _data[i];
138  }
139  const row_type & mat_access(size_type i) const {
140  DUNE_ASSERT_BOUNDS(i < _data.size());
141  return _data[i];
142  }
143  };
144 
147 } // end namespace
148 
149 #endif
Dune::DenseMatVecTraits< DynamicMatrix< K > >::size_type
container_type::size_type size_type
Definition: dynmatrix.hh:43
dynvector.hh
This file implements a dense vector with a dynamic size.
Dune::DynamicMatrix
Construct a matrix with a dynamic size.
Definition: dynmatrix.hh:29
Dune::DenseMatrix::value_type
Traits::value_type value_type
export the type representing the field
Definition: densematrix.hh:174
Dune::DenseMatVecTraits< DynamicMatrix< K > >::row_reference
row_type & row_reference
Definition: dynmatrix.hh:38
Dune::FieldTraits< DynamicMatrix< K > >::field_type
FieldTraits< K >::field_type field_type
Definition: dynmatrix.hh:49
Dune::DynamicMatrix::mat_rows
size_type mat_rows() const
Definition: dynmatrix.hh:130
Dune::DynamicMatrix::size_type
Base::size_type size_type
Definition: dynmatrix.hh:63
exceptions.hh
A few common exception classes.
Dune
Dune namespace.
Definition: alignedallocator.hh:9
Dune::DenseMatVecTraits< DynamicMatrix< K > >::const_row_reference
const typedef row_type & const_row_reference
Definition: dynmatrix.hh:39
Dune::DenseMatrix::row_type
Traits::row_type row_type
The type used to represent a row (must fulfill the Dune::DenseVector interface)
Definition: densematrix.hh:186
Dune::DenseMatVecTraits< DynamicMatrix< K > >::row_type
DynamicVector< K > row_type
Definition: dynmatrix.hh:36
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix(T const &rhs)
Definition: dynmatrix.hh:85
densematrix.hh
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix()
Default constructor.
Definition: dynmatrix.hh:69
Dune::DenseMatrix::operator=
derived_type & operator=(const RHS &rhs)
Definition: densematrix.hh:302
Dune::DynamicMatrix::value_type
Base::value_type value_type
Definition: dynmatrix.hh:64
Dune::DynamicMatrix::mat_access
const row_type & mat_access(size_type i) const
Definition: dynmatrix.hh:139
Dune::DynamicMatrix::row_type
Base::row_type row_type
Definition: dynmatrix.hh:65
Dune::DynamicMatrix::operator=
DynamicMatrix & operator=(T const &rhs)
Definition: dynmatrix.hh:114
Dune::FieldTraits::real_type
T real_type
export the type representing the real type of the field
Definition: ftraits.hh:28
Dune::DenseMatVecTraits< DynamicMatrix< K > >::derived_type
DynamicMatrix< K > derived_type
Definition: dynmatrix.hh:34
Dune::DynamicMatrix::mat_access
row_type & mat_access(size_type i)
Definition: dynmatrix.hh:135
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix(size_type r, size_type c, value_type v=value_type())
Constructor initializing the whole matrix with a scalar.
Definition: dynmatrix.hh:72
Dune::FieldTraits< DynamicMatrix< K > >::real_type
FieldTraits< K >::real_type real_type
Definition: dynmatrix.hh:50
boundschecking.hh
Macro for wrapping boundary checks.
Dune::DynamicMatrix::resize
void resize(size_type r, size_type c, value_type v=value_type())
resize matrix to r × c
Definition: dynmatrix.hh:104
Dune::FieldTraits
Definition: ftraits.hh:23
Dune::DynamicMatrix::operator=
DynamicMatrix & operator=(T scalar)
Definition: dynmatrix.hh:124
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix(std::initializer_list< DynamicVector< K >> const &ll)
Constructor initializing the matrix from a list of vector.
Definition: dynmatrix.hh:78
Dune::FieldTraits::field_type
T field_type
export the type representing the field
Definition: ftraits.hh:26
Dune::DynamicVector< K >
DUNE_ASSERT_BOUNDS
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
Dune::DenseMatVecTraits< DynamicMatrix< K > >::value_type
K value_type
Definition: dynmatrix.hh:42
Dune::DenseMatrix::size_type
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densematrix.hh:183
Dune::DenseMatrix
A dense n x m matrix.
Definition: densematrix.hh:26
Dune::DenseMatVecTraits< DynamicMatrix< K > >::container_type
std::vector< K > container_type
Definition: dynmatrix.hh:41
typetraits.hh
Traits for type conversions and type information.
Dune::DynamicMatrix::mat_cols
size_type mat_cols() const
Definition: dynmatrix.hh:131
Dune::DenseMatVecTraits
Definition: matvectraits.hh:29