Dune Core Modules (2.8.0)

scalarmatrixview.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_COMMON_SCALARMATRIXVIEW_HH
4#define DUNE_COMMON_SCALARMATRIXVIEW_HH
5
6#include <cstddef>
7#include <type_traits>
8#include <ostream>
9
16
17
18namespace Dune {
19
20namespace Impl {
21
37 template<class K>
38 class ScalarMatrixView :
39 public DenseMatrix<ScalarMatrixView<K>>
40 {
41 ScalarVectorView<K> data_;
42 using Base = DenseMatrix<ScalarMatrixView<K>>;
43
44 template <class>
45 friend class ScalarMatrixView;
46 public:
47
48 //===== type definitions and constants
49
51 enum {
54 blocklevel = 1
55 };
56
57 using size_type = typename Base::size_type;
58 using row_type = typename Base::row_type;
59 using row_reference = typename Base::row_reference;
61
63 enum {
66 rows = 1,
69 cols = 1
70 };
71
72 //===== constructors
75 constexpr ScalarMatrixView ()
76 : data_()
77 {}
78
80 ScalarMatrixView (K* p) :
81 data_(p)
82 {}
83
85 ScalarMatrixView (const ScalarMatrixView &other) :
86 Base(),
87 data_(other.data_)
88 {}
89
91 ScalarMatrixView (ScalarMatrixView &&other) :
92 Base(),
93 data_( other.data_ )
94 {}
95
97 ScalarMatrixView& operator= (const ScalarMatrixView& other)
98 {
99 data_ = other.data_;
100 return *this;
101 }
102
103 template<class KK>
104 ScalarMatrixView& operator= (const ScalarMatrixView<KK>& other)
105 {
106 data_ = other.data_;
107 return *this;
108 }
109
111 template<typename T,
112 std::enable_if_t<std::is_convertible<T, K>::value, int> = 0>
113 inline ScalarMatrixView& operator= (const T& k)
114 {
115 data_ = k;
116 return *this;
117 }
118
119 // make this thing a matrix
120 static constexpr size_type mat_rows() { return 1; }
121 static constexpr size_type mat_cols() { return 1; }
122
123 row_reference mat_access ([[maybe_unused]] size_type i)
124 {
125 DUNE_ASSERT_BOUNDS(i == 0);
126 return data_;
127 }
128
129 const_row_reference mat_access ([[maybe_unused]] size_type i) const
130 {
131 DUNE_ASSERT_BOUNDS(i == 0);
132 return data_;
133 }
134 }; // class ScalarMatrixView
135
137 template<typename K>
138 std::ostream& operator<< (std::ostream& s, const ScalarMatrixView<K>& a)
139 {
140 s << a[0][0];
141 return s;
142 }
143
145 template<class T,
146 std::enable_if_t<IsNumber<T>::value, int> = 0>
147 auto asMatrix(T& t)
148 {
149 return ScalarMatrixView<T>{&t};
150 }
151
153 template<class T,
154 std::enable_if_t<IsNumber<T>::value, int> = 0>
155 auto asMatrix(const T& t)
156 {
157 return ScalarMatrixView<const T>{&t};
158 }
159
161 template<class T,
162 std::enable_if_t<not IsNumber<T>::value, int> = 0>
163 T& asMatrix(T& t)
164 {
165 return t;
166 }
167
169 template<class T,
170 std::enable_if_t<not IsNumber<T>::value, int> = 0>
171 const T& asMatrix(const T& t)
172 {
173 return t;
174 }
175
178} // end namespace Impl
179
180 template<class K>
181 struct FieldTraits<Impl::ScalarMatrixView<K>> : public FieldTraits<std::remove_const_t<K>> {};
182
183 template<class K>
184 struct DenseMatVecTraits<Impl::ScalarMatrixView<K>>
185 {
186 using derived_type = Impl::ScalarMatrixView<K>;
187 using row_type = Impl::ScalarVectorView<K>;
188 using row_reference = row_type&;
189 using const_row_reference = const row_type&;
190 using value_type = std::remove_const_t<K>;
191 using size_type = std::size_t;
192 };
193
194
195 template<class K>
196 struct AutonomousValueType<Impl::ScalarMatrixView<K>>
197 {
198 using type = FieldMatrix<std::remove_const_t<K>,1,1>;
199 };
200
201
202} // end namespace Dune
203
204#endif // DUNE_COMMON_SCALARMATRIXVIEW_HH
Macro for wrapping boundary checks.
constexpr size_type cols() const
number of columns
Definition: densematrix.hh:743
constexpr size_type rows() const
number of rows
Definition: densematrix.hh:737
@ blocklevel
The number of block levels we contain. This is 1.
Definition: densematrix.hh:205
Traits::row_type row_type
The type used to represent a row (must fulfill the Dune::DenseVector interface)
Definition: densematrix.hh:194
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densematrix.hh:191
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:200
Traits::row_reference row_reference
The type used to represent a reference to a row (usually row_type &)
Definition: densematrix.hh:197
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
Traits for type conversions and type information.
Implements a matrix constructed from a given type representing a field and compile-time given number ...
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:28
Documentation of the traits classes you need to write for each implementation of DenseVector or Dense...
Dune namespace.
Definition: alignedallocator.hh:11
Implements a scalar vector view wrapper around an existing scalar.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)