dune-istl 2.12-git
Loading...
Searching...
No Matches
vectorview.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_ISTL_EXPERIMENTAL_VECTORVIEW_HH
7#define DUNE_ISTL_EXPERIMENTAL_VECTORVIEW_HH
8
9#include "indexedarrayview.hh"
10
11#include "../istlexception.hh"
12#include "../blocklevel.hh"
13
20
21#include <algorithm>
22#include <cfenv>
23#include <cmath>
24#include <complex>
25#include <initializer_list>
26#include <limits>
27#include <memory>
28#include <utility>
29#include <vector>
30
35namespace Dune {
36namespace Experimental {
37
57template<std::random_access_iterator B, Concepts::BorrowedOrderedIndexRange R>
58class VectorView : public IndexedArrayView<B, R>
59{
60public:
61
74
83
85 VectorView& operator=(const field_type& k) noexcept(
86 noexcept(std::declval<reference>() = std::declval<const field_type&>()))
87 {
88 for (auto& x_i : *this)
89 x_i = k;
90 return *this;
91 }
92
95 template<class OtherB, class OtherR>
97 noexcept(this->checkSizeMatch(std::declval<const VectorView<OtherB, OtherR>&>())) &&
98 noexcept(std::declval<reference>() +=
99 std::declval<typename VectorView<OtherB, OtherR>::const_reference>()))
100 {
101 forEachIncludedEntry(y, [](auto& x_i, const auto& y_i) { x_i += y_i; });
102 return *this;
103 }
104
107 template<class OtherB, class OtherR>
109 noexcept(this->checkSizeMatch(std::declval<const VectorView<OtherB, OtherR>&>())) &&
110 noexcept(std::declval<reference>() -=
111 std::declval<typename VectorView<OtherB, OtherR>::const_reference>()))
112 {
113 forEachIncludedEntry(y, [](auto& x_i, const auto& y_i) { x_i -= y_i; });
114 return *this;
115 }
116
118 VectorView& operator*=(const field_type& k) noexcept(
119 noexcept(std::declval<reference>() *= std::declval<const field_type&>()))
120 {
121 for (auto& x_i : *this)
122 x_i *= k;
123 return *this;
124 }
125
127 VectorView& operator/=(const field_type& k) noexcept(
128 noexcept(std::declval<reference>() /= std::declval<const field_type&>()))
129 {
130 for (auto& x_i : *this)
131 x_i /= k;
132 return *this;
133 }
134
137 template<class OtherB, class OtherR>
139 axpy(const field_type& a, const VectorView<OtherB, OtherR>& y) noexcept(
140 noexcept(this->checkSizeMatch(std::declval<const VectorView<OtherB, OtherR>&>())) &&
141 noexcept(Dune::Impl::asVector(std::declval<reference>())
142 .axpy(std::declval<const field_type&>(),
143 Dune::Impl::asVector(std::declval<typename VectorView<OtherB, OtherR>::const_reference>()))))
144 {
145 forEachIncludedEntry(y, [a](block_type& x_i, const auto& y_i) {
146 Dune::Impl::asVector(x_i).axpy(a, Dune::Impl::asVector(y_i));
147 });
148 return *this;
149 }
150
152 template<class OtherB, class OtherR>
153 [[nodiscard]]
154 auto operator*(const VectorView<OtherB, OtherR>& y) const noexcept(
155 noexcept(this->checkSizeMatch(std::declval<const VectorView<OtherB, OtherR>&>())) &&
156 noexcept(
157 Dune::Impl::asVector(std::declval<const_reference>()) *
158 Dune::Impl::asVector(
160 )
161 {
162 checkSizeMatch(y);
163 typedef typename PromotionTraits<
165 typename VectorView<OtherB, OtherR>::field_type>::PromotedType
166 PromotedType;
167 PromotedType sum(0);
168
169 auto itY = std::cbegin(y);
170 const auto itEndY = std::cend(y);
171 const auto itEndX = std::cend(*this);
172
173 for (auto itX = std::begin(*this); itX != itEndX; ++itX) {
174 auto indexX = itX.index();
175 while (itY != itEndY && itY.index() < indexX)
176 ++itY;
177 if (itY != itEndY && itY.index() == indexX)
178 sum += Dune::Impl::asVector(*itX) * Dune::Impl::asVector(*itY);
179 }
180
181 return sum;
182 }
183
185 template<class OtherB, class OtherR>
186 [[nodiscard]]
187 auto dot(const VectorView<OtherB, OtherR>& y) const noexcept(
188 noexcept(this->checkSizeMatch(std::declval<const VectorView<OtherB, OtherR>&>())) &&
189 noexcept(Dune::Impl::asVector(std::declval<const_reference>())
190 .dot(Dune::Impl::asVector(
193 {
194 checkSizeMatch(y);
195 typedef typename PromotionTraits<
197 typename VectorView<OtherB, OtherR>::field_type>::PromotedType
198 PromotedType;
199 PromotedType sum(0);
200
201 auto itY = std::cbegin(y);
202 const auto itEndY = std::cend(y);
203 const auto itEndX = std::cend(*this);
204
205 for (auto itX = std::begin(*this); itX != itEndX; ++itX) {
206 auto indexX = itX.index();
207 while (itY != itEndY && itY.index() < indexX)
208 ++itY;
209 if (itY != itEndY && itY.index() == indexX)
210 sum += Dune::Impl::asVector(*itX).dot(Dune::Impl::asVector(*itY));
211 }
212
213 return sum;
214 }
215
217 [[nodiscard]]
219 noexcept(Dune::Impl::asVector(std::declval<const_reference>()).one_norm()))
220 {
221 typename FieldTraits<field_type>::real_type sum = 0;
222 for (const auto& x_i : *this)
223 sum += Dune::Impl::asVector(x_i).one_norm();
224 return sum;
225 }
226
228 [[nodiscard]]
230 noexcept(Dune::Impl::asVector(std::declval<const_reference>()).one_norm_real()))
231 {
232 typename FieldTraits<field_type>::real_type sum = 0;
233 for (const auto& x_i : *this)
234 sum += Dune::Impl::asVector(x_i).one_norm_real();
235 return sum;
236 }
237
239 [[nodiscard]]
241 noexcept(noexcept(two_norm2()))
242 {
243 using std::sqrt;
244 return sqrt(two_norm2());
245 }
246
248 [[nodiscard]]
250 noexcept(Dune::Impl::asVector(std::declval<const_reference>()).two_norm2()))
251 {
252 typename FieldTraits<field_type>::real_type sum = 0;
253 for (const auto& x_i : *this)
254 sum += Dune::Impl::asVector(x_i).two_norm2();
255 return sum;
256 }
257
259 [[nodiscard]]
261 noexcept(Dune::Impl::asVector(std::declval<const_reference>()).infinity_norm()))
262 {
264 constexpr bool hasNaN = std::numeric_limits<real_type>::has_quiet_NaN ||
266
267 real_type norm = 0;
268 real_type isNaN = 1;
269 using std::max;
270
271 for (auto const& x_i : *this) {
272 real_type const a = Dune::Impl::asVector(x_i).infinity_norm();
273 norm = max(a, norm);
274 if constexpr (hasNaN)
275 isNaN += a;
276 }
277 if constexpr (hasNaN) {
278 // maintain NaN exception flags that raised the original NaN values.
279 std::fenv_t env;
280 int fpe = std::fegetenv(&env);
281 isNaN = isNaN / isNaN;
282 if (fpe != 0)
283 std::fesetenv(&env);
284 }
285
286 return (isNaN != isNaN) ? isNaN : norm;
287 }
288
290 [[nodiscard]]
292 noexcept(noexcept(
293 Dune::Impl::asVector(std::declval<const_reference>()).infinity_norm_real()))
294 {
296 constexpr bool hasNaN = std::numeric_limits<real_type>::has_quiet_NaN ||
298
299 real_type norm = 0;
300 real_type isNaN = 1;
301 using std::max;
302
303 for (auto const& x_i : *this) {
304 real_type const a = Dune::Impl::asVector(x_i).infinity_norm_real();
305 norm = max(a, norm);
306 if constexpr (hasNaN)
307 isNaN += a;
308 }
309 if constexpr (hasNaN) {
310 // maintain NaN exception flags that raised the original NaN values.
311 std::fenv_t env;
312 int fpe = std::fegetenv(&env);
313 isNaN = isNaN / isNaN;
314 if (fpe != 0)
315 std::fesetenv(&env);
316 }
317
318 return (isNaN != isNaN) ? isNaN : norm;
319 }
320
322 [[nodiscard]]
323 size_type N() const noexcept
324 {
325 return std::ranges::size(
327 }
328
329protected:
331 using IndexedArrayView<B, R>::IndexedArrayView;
332
333private:
334
336#if DUNE_ISTL_WITH_CHECKING
337 template<class OtherB, class OtherR>
338 void checkSizeMatch(const VectorView<OtherB, OtherR>& other) const
339 {
340 if (this->N() != other.N())
342 "vector size mismatch: " << this->N() << " vs " << other.N());
343 }
344#else
345 template<class OtherB, class OtherR>
346 void checkSizeMatch(const VectorView<OtherB, OtherR>&) const noexcept {}
347#endif
348
357 template<class OtherB, class OtherR>
358 void forEachIncludedEntry(
359 const VectorView<OtherB, OtherR>& y,
360 const std::invocable<
361 reference,
362 typename VectorView<OtherB, OtherR>::const_reference> auto& f) noexcept(
363 noexcept(this->checkSizeMatch(std::declval<const VectorView<OtherB, OtherR>&>())) &&
364 noexcept(f(std::declval<reference>(), std::declval<typename VectorView<OtherB, OtherR>::const_reference>()))
365 )
366 {
367 checkSizeMatch(y);
368 const auto checkIndex = [](const auto& index,
369 const auto& it,
370 const auto& itEnd,
371 const auto& expect_same) {
372#if DUNE_ISTL_WITH_CHECKING
373 if (it == itEnd || (expect_same && it.index() != index))
374 DUNE_THROW(ISTLError,
375 "index " << index << " not in base array index set");
376#endif
377 };
378
379 auto itX = std::begin(*this);
380 const auto itEndX = std::cend(*this);
381 const auto itEndY = std::cend(y);
382 for (auto itY = std::cbegin(y); itY != itEndY; ++itY) {
383 auto indexY = itY.index();
384 checkIndex(indexY, itX, itEndX, std::false_type());
385 while (itX.index() < indexY) {
386 ++itX;
387 checkIndex(indexY, itX, itEndX, std::false_type());
388 }
389 checkIndex(indexY, itX, itEndX, std::true_type());
390 f(*itX, *itY);
391 }
392 }
393};
394
395} // namespace Experimental
396
397template<typename B, typename R>
399 : public FieldTraits<typename Dune::Experimental::VectorView<B, R>::block_type>
400{};
401
402
403} // namespace Dune
404
405#endif // DUNE_ISTL_EXPERIMENTAL_VECTORVIEW_HH
Indexed view over random-access data using an explicit index set.
STL namespace.
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
Non-owning indexed view over random-access data.
Definition indexedarrayview.hh:36
ConstIterator const_iterator
Iterator type with const access to the underlying data.
Definition indexedarrayview.hh:204
Iterator iterator
Iterator type with mutable access to the underlying data.
Definition indexedarrayview.hh:202
constexpr index_range_type indexSet() const
The logical index range covered by the view, as determined by the index set.
Definition indexedarrayview.hh:268
R index_range_type
The type used for the index set.
Definition indexedarrayview.hh:39
typename OrderedIndexRangeTraits< index_range_type >::size_type size_type
The type used to count elements in the index set.
Definition indexedarrayview.hh:45
Traits for sets of indices.
Definition indexrange.hh:55
Non-owning block vector view with explicit indices.
Definition vectorview.hh:59
typename IndexedArrayView< B, R >::member_type block_type
The type of blocks in the vector.
Definition vectorview.hh:65
VectorView & operator*=(const field_type &k) noexcept(noexcept(std::declval< reference >() *=std::declval< const field_type & >()))
Scale all entries by k.
Definition vectorview.hh:118
typename IndexedArrayView< B, R >::const_iterator ConstIterator
Iterator type with const access to the underlying blocks.
Definition vectorview.hh:78
VectorView & axpy(const field_type &a, const VectorView< OtherB, OtherR > &y) noexcept(noexcept(this->checkSizeMatch(std::declval< const VectorView< OtherB, OtherR > & >())) &&noexcept(Dune::Impl::asVector(std::declval< reference >()) .axpy(std::declval< const field_type & >(), Dune::Impl::asVector(std::declval< typename VectorView< OtherB, OtherR >::const_reference >()))))
Definition vectorview.hh:139
FieldTraits< field_type >::real_type one_norm_real() const noexcept(noexcept(Dune::Impl::asVector(std::declval< const_reference >()).one_norm_real()))
Simplified one norm (uses Manhattan norm for complex values)
Definition vectorview.hh:229
FieldTraits< field_type >::real_type two_norm() const noexcept(noexcept(two_norm2()))
Two norm sqrt(sum over squared values of entries)
Definition vectorview.hh:240
FieldTraits< field_type >::real_type two_norm2() const noexcept(noexcept(Dune::Impl::asVector(std::declval< const_reference >()).two_norm2()))
Square of the two-norm (the sum over the squared values of the entries)
Definition vectorview.hh:249
typename IndexedArrayView< B, R >::index_range_type index_range_type
The type used for the index set.
Definition vectorview.hh:63
typename IndexedArrayView< B, R >::size_type size_type
The type used to count elements in the index set.
Definition vectorview.hh:67
typename IndexedArrayView< B, R >::member_type value_type
The type of blocks in the vector.
Definition vectorview.hh:69
auto operator*(const VectorView< OtherB, OtherR > &y) const noexcept(noexcept(this->checkSizeMatch(std::declval< const VectorView< OtherB, OtherR > & >())) &&noexcept(Dune::Impl::asVector(std::declval< const_reference >()) *Dune::Impl::asVector(std::declval< typename VectorView< OtherB, OtherR >::const_reference >())))
Indefinite vector dot product .
Definition vectorview.hh:154
typename IndexedArrayView< B, R >::const_reference const_reference
The type used for const references to the blocks.
Definition vectorview.hh:73
typename IndexedArrayView< B, R >::iterator Iterator
Iterator type with mutable access to the underlying blocks.
Definition vectorview.hh:76
VectorView & operator-=(const VectorView< OtherB, OtherR > &y) noexcept(noexcept(this->checkSizeMatch(std::declval< const VectorView< OtherB, OtherR > & >())) &&noexcept(std::declval< reference >() -=std::declval< typename VectorView< OtherB, OtherR >::const_reference >()))
Definition vectorview.hh:108
VectorView & operator+=(const VectorView< OtherB, OtherR > &y) noexcept(noexcept(this->checkSizeMatch(std::declval< const VectorView< OtherB, OtherR > & >())) &&noexcept(std::declval< reference >()+=std::declval< typename VectorView< OtherB, OtherR >::const_reference >()))
Definition vectorview.hh:96
VectorView & operator/=(const field_type &k) noexcept(noexcept(std::declval< reference >()/=std::declval< const field_type & >()))
Divide all entries by k.
Definition vectorview.hh:127
typename FieldTraits< block_type >::real_type real_type
The type of the real part of the field.
Definition vectorview.hh:82
VectorView & operator=(const field_type &k) noexcept(noexcept(std::declval< reference >()=std::declval< const field_type & >()))
Assign all entries to scalar k.
Definition vectorview.hh:85
auto dot(const VectorView< OtherB, OtherR > &y) const noexcept(noexcept(this->checkSizeMatch(std::declval< const VectorView< OtherB, OtherR > & >())) &&noexcept(Dune::Impl::asVector(std::declval< const_reference >()) .dot(Dune::Impl::asVector(std::declval< typename VectorView< OtherB, OtherR >::const_reference >()))))
Vector dot product .
Definition vectorview.hh:187
size_type N() const noexcept
Logical index span of the vector (size of index_set range), not stored entries.
Definition vectorview.hh:323
typename FieldTraits< block_type >::field_type field_type
The type of the field.
Definition vectorview.hh:80
FieldTraits< field_type >::real_type infinity_norm() const noexcept(noexcept(Dune::Impl::asVector(std::declval< const_reference >()).infinity_norm()))
Infinity norm (maximum of absolute values of entries)
Definition vectorview.hh:260
FieldTraits< field_type >::real_type infinity_norm_real() const noexcept(noexcept(Dune::Impl::asVector(std::declval< const_reference >()).infinity_norm_real()))
Simplified infinity norm (uses Manhattan norm for complex values)
Definition vectorview.hh:291
FieldTraits< field_type >::real_type one_norm() const noexcept(noexcept(Dune::Impl::asVector(std::declval< const_reference >()).one_norm()))
One norm (sum over absolute values of entries)
Definition vectorview.hh:218
typename IndexedArrayView< B, R >::reference reference
The type used for references to the blocks.
Definition vectorview.hh:71
derive error class from the base class in common
Definition istlexception.hh:19
T cbegin(T... args)
T declval(T... args)
T cend(T... args)
T fegetenv(T... args)
T max(T... args)
T sqrt(T... args)