dune-common 2.9.0
Loading...
Searching...
No Matches
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
18
19namespace Dune
20{
21
31 template< class K > class DynamicMatrix;
32
33 template< class K >
35 {
37
39
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 >
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
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
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>>
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(); }
144 assert(this->rows());
145 return _data.front().size();
146 }
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
Traits for type conversions and type information.
This file implements a dense vector with a dynamic size.
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
A few common exception classes.
Macro for wrapping boundary checks.
#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
A dense n x m matrix.
Definition densematrix.hh:140
derived_type & operator=(const RHS &rhs)
Definition densematrix.hh:279
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
size_type mat_cols() const
Definition dynmatrix.hh:143
DynamicMatrix(std::initializer_list< DynamicVector< K > > const &ll)
Constructor initializing the matrix from a list of vector.
Definition dynmatrix.hh:80
Base::row_type row_type
Definition dynmatrix.hh:67
DynamicMatrix transposed() const
Return transposed of the matrix as DynamicMatrix.
Definition dynmatrix.hh:132
Base::value_type value_type
Definition dynmatrix.hh:66
row_type & mat_access(size_type i)
Definition dynmatrix.hh:147
Base::size_type size_type
Definition dynmatrix.hh:65
size_type mat_rows() const
Definition dynmatrix.hh:142
DynamicMatrix(T const &rhs)
Definition dynmatrix.hh:87
const row_type & mat_access(size_type i) const
Definition dynmatrix.hh:151
void resize(size_type r, size_type c, value_type v=value_type())
resize matrix to r × c
Definition dynmatrix.hh:106
DynamicMatrix & operator=(T const &rhs)
Definition dynmatrix.hh:116
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
DynamicMatrix & operator=(T scalar)
Definition dynmatrix.hh:126
container_type::size_type size_type
Definition dynmatrix.hh:45
DynamicVector< K > row_type
Definition dynmatrix.hh:38
row_type & row_reference
Definition dynmatrix.hh:40
K value_type
Definition dynmatrix.hh:44
DynamicMatrix< K > derived_type
Definition dynmatrix.hh:36
const row_type & const_row_reference
Definition dynmatrix.hh:41
std::vector< K > container_type
Definition dynmatrix.hh:43
FieldTraits< K >::real_type real_type
Definition dynmatrix.hh:52
FieldTraits< K >::field_type field_type
Definition dynmatrix.hh:51
Construct a vector with a dynamic size.
Definition dynvector.hh:59
Definition ftraits.hh:26
T field_type
export the type representing the field
Definition ftraits.hh:28
T real_type
export the type representing the real type of the field
Definition ftraits.hh:30
Definition matvectraits.hh:31