00001
00002
00003 #ifndef DUNE_DYNMATRIX_HH
00004 #define DUNE_DYNMATRIX_HH
00005
00006 #include <cmath>
00007 #include <cstddef>
00008 #include <iostream>
00009 #include <initializer_list>
00010
00011 #include <dune/common/boundschecking.hh>
00012 #include <dune/common/exceptions.hh>
00013 #include <dune/common/dynvector.hh>
00014 #include <dune/common/densematrix.hh>
00015 #include <dune/common/typetraits.hh>
00016
00017 namespace Dune
00018 {
00019
00029 template< class K > class DynamicMatrix;
00030
00031 template< class K >
00032 struct DenseMatVecTraits< DynamicMatrix<K> >
00033 {
00034 typedef DynamicMatrix<K> derived_type;
00035
00036 typedef DynamicVector<K> row_type;
00037
00038 typedef row_type &row_reference;
00039 typedef const row_type &const_row_reference;
00040
00041 typedef std::vector<K> container_type;
00042 typedef K value_type;
00043 typedef typename container_type::size_type size_type;
00044 };
00045
00046 template< class K >
00047 struct FieldTraits< DynamicMatrix<K> >
00048 {
00049 typedef typename FieldTraits<K>::field_type field_type;
00050 typedef typename FieldTraits<K>::real_type real_type;
00051 };
00052
00057 template<class K>
00058 class DynamicMatrix : public DenseMatrix< DynamicMatrix<K> >
00059 {
00060 std::vector< DynamicVector<K> > _data;
00061 typedef DenseMatrix< DynamicMatrix<K> > Base;
00062 public:
00063 typedef typename Base::size_type size_type;
00064 typedef typename Base::value_type value_type;
00065 typedef typename Base::row_type row_type;
00066
00067
00069 DynamicMatrix () {}
00070
00072 DynamicMatrix (size_type r, size_type c, value_type v = value_type() ) :
00073 _data(r, row_type(c, v) )
00074 {}
00075
00078 DynamicMatrix (std::initializer_list<DynamicVector<K>> const &ll)
00079 : _data(ll)
00080 {}
00081
00082
00083 template <class T,
00084 typename = std::enable_if_t<!Dune::IsNumber<T>::value && HasDenseMatrixAssigner<DynamicMatrix, T>::value>>
00085 DynamicMatrix(T const& rhs)
00086 {
00087 *this = rhs;
00088 }
00089
00090
00104 void resize (size_type r, size_type c, value_type v = value_type() )
00105 {
00106 _data.resize(0);
00107 _data.resize(r, row_type(c, v) );
00108 }
00109
00110
00111
00112 template <typename T,
00113 typename = std::enable_if_t<!Dune::IsNumber<T>::value>>
00114 DynamicMatrix& operator=(T const& rhs) {
00115 _data.resize(rhs.N());
00116 std::fill(_data.begin(), _data.end(), row_type(rhs.M(), K(0)));
00117 Base::operator=(rhs);
00118 return *this;
00119 }
00120
00121
00122 template <typename T,
00123 typename = std::enable_if_t<Dune::IsNumber<T>::value>>
00124 DynamicMatrix& operator=(T scalar) {
00125 std::fill(_data.begin(), _data.end(), scalar);
00126 return *this;
00127 }
00128
00129
00130 size_type mat_rows() const { return _data.size(); }
00131 size_type mat_cols() const {
00132 assert(this->rows());
00133 return _data.front().size();
00134 }
00135 row_type & mat_access(size_type i) {
00136 DUNE_ASSERT_BOUNDS(i < _data.size());
00137 return _data[i];
00138 }
00139 const row_type & mat_access(size_type i) const {
00140 DUNE_ASSERT_BOUNDS(i < _data.size());
00141 return _data[i];
00142 }
00143 };
00144
00147 }
00148
00149 #endif