Dune Core Modules (2.9.0)

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 // SPDX-FileCopyrightInfo: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_DYNMATRIX_HH
6 #define DUNE_DYNMATRIX_HH
7 
8 #include <cmath>
9 #include <cstddef>
10 #include <iostream>
11 #include <initializer_list>
12 
15 #include <dune/common/dynvector.hh>
18 
19 namespace Dune
20 {
21 
31  template< class K > class DynamicMatrix;
32 
33  template< class K >
34  struct DenseMatVecTraits< DynamicMatrix<K> >
35  {
36  typedef DynamicMatrix<K> derived_type;
37 
38  typedef DynamicVector<K> row_type;
39 
40  typedef row_type &row_reference;
41  typedef const row_type &const_row_reference;
42 
43  typedef std::vector<K> container_type;
44  typedef K value_type;
45  typedef typename container_type::size_type size_type;
46  };
47 
48  template< class K >
49  struct FieldTraits< DynamicMatrix<K> >
50  {
51  typedef typename FieldTraits<K>::field_type field_type;
52  typedef typename FieldTraits<K>::real_type real_type;
53  };
54 
59  template<class K>
60  class DynamicMatrix : public DenseMatrix< DynamicMatrix<K> >
61  {
62  std::vector< DynamicVector<K> > _data;
64  public:
65  typedef typename Base::size_type size_type;
66  typedef typename Base::value_type value_type;
67  typedef typename Base::row_type row_type;
68 
69  //===== constructors
72 
74  DynamicMatrix (size_type r, size_type c, value_type v = value_type() ) :
75  _data(r, row_type(c, v) )
76  {}
77 
80  DynamicMatrix (std::initializer_list<DynamicVector<K>> const &ll)
81  : _data(ll)
82  {}
83 
84 
85  template <class T,
86  typename = std::enable_if_t<!Dune::IsNumber<T>::value && HasDenseMatrixAssigner<DynamicMatrix, T>::value>>
87  DynamicMatrix(T const& rhs)
88  {
89  *this = rhs;
90  }
91 
92  //==== resize related methods
106  void resize (size_type r, size_type c, value_type v = value_type() )
107  {
108  _data.resize(0);
109  _data.resize(r, row_type(c, v) );
110  }
111 
112  //===== assignment
113  // General assignment with resizing
114  template <typename T,
115  typename = std::enable_if_t<!Dune::IsNumber<T>::value>>
116  DynamicMatrix& operator=(T const& rhs) {
117  _data.resize(rhs.N());
118  std::fill(_data.begin(), _data.end(), row_type(rhs.M(), K(0)));
119  Base::operator=(rhs);
120  return *this;
121  }
122 
123  // Specialisation: scalar assignment (no resizing)
124  template <typename T,
125  typename = std::enable_if_t<Dune::IsNumber<T>::value>>
126  DynamicMatrix& operator=(T scalar) {
127  std::fill(_data.begin(), _data.end(), scalar);
128  return *this;
129  }
130 
133  {
134  DynamicMatrix AT(this->M(), this->N());
135  for( size_type i = 0; i < this->N(); ++i )
136  for( size_type j = 0; j < this->M(); ++j )
137  AT[j][i] = (*this)[i][j];
138  return AT;
139  }
140 
141  // make this thing a matrix
142  size_type mat_rows() const { return _data.size(); }
143  size_type mat_cols() const {
144  assert(this->rows());
145  return _data.front().size();
146  }
147  row_type & mat_access(size_type i) {
148  DUNE_ASSERT_BOUNDS(i < _data.size());
149  return _data[i];
150  }
151  const row_type & mat_access(size_type i) const {
152  DUNE_ASSERT_BOUNDS(i < _data.size());
153  return _data[i];
154  }
155  };
156 
159 } // end namespace
160 
161 #endif
Macro for wrapping boundary checks.
A dense n x m matrix.
Definition: densematrix.hh:140
size_type size() const
size method (number of rows)
Definition: densematrix.hh:200
constexpr size_type M() const
number of columns
Definition: densematrix.hh:703
Traits::value_type value_type
export the type representing the field
Definition: densematrix.hh:157
constexpr size_type rows() const
number of rows
Definition: densematrix.hh:709
constexpr size_type N() const
number of rows
Definition: densematrix.hh:697
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densematrix.hh:166
Construct a matrix with a dynamic size.
Definition: dynmatrix.hh:61
DynamicMatrix(std::initializer_list< DynamicVector< K >> const &ll)
Constructor initializing the matrix from a list of vector.
Definition: dynmatrix.hh:80
DynamicMatrix transposed() const
Return transposed of the matrix as DynamicMatrix.
Definition: dynmatrix.hh:132
void resize(size_type r, size_type c, value_type v=value_type())
resize matrix to r × c
Definition: dynmatrix.hh:106
DynamicMatrix()
Default constructor.
Definition: dynmatrix.hh:71
DynamicMatrix(size_type r, size_type c, value_type v=value_type())
Constructor initializing the whole matrix with a scalar.
Definition: dynmatrix.hh:74
Construct a vector with a dynamic size.
Definition: dynvector.hh:59
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
A few common exception classes.
Traits for type conversions and type information.
This file implements a dense vector with a dynamic size.
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:30
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 27, 22:29, 2024)