Dune Core Modules (unstable)

fvector.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 © 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_COMMON_FVECTOR_HH
6#define DUNE_COMMON_FVECTOR_HH
7
8#include <algorithm>
9#include <array>
10#include <cmath>
11#include <concepts>
12#include <cstdlib>
13#include <cstring>
14#include <type_traits>
15#include <utility>
16#include <initializer_list>
17
22#include <dune/common/math.hh>
26#include <dune/common/concepts/number.hh>
27#include <dune/common/std/algorithm.hh>
28#include <dune/common/std/compare.hh>
29
30namespace Dune {
31
41 template< class K, int SIZE > class FieldVector;
42 template< class K, int SIZE >
43 struct DenseMatVecTraits< FieldVector<K,SIZE> >
44 {
45 typedef FieldVector<K,SIZE> derived_type;
46 typedef std::array<K,SIZE> container_type;
47 typedef K value_type;
48 typedef typename container_type::size_type size_type;
49 };
50
51 template< class K, int SIZE >
52 struct FieldTraits< FieldVector<K,SIZE> >
53 {
54 typedef typename FieldTraits<K>::field_type field_type;
55 typedef typename FieldTraits<K>::real_type real_type;
56 };
57
66 template<typename C, int SIZE>
68 {
73 constexpr static bool value = true;
74 };
75
76 template<typename T, int SIZE>
77 struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE>,SIZE>
78 {
79 constexpr static bool value = true;
80 };
81
82 template<typename T, int SIZE, int SIZE1>
83 struct IsFieldVectorSizeCorrect<FieldVector<T,SIZE1>,SIZE>
84 {
85 constexpr static bool value = false;
86 };
87
88
94 template< class K, int SIZE >
96 public DenseVector< FieldVector<K,SIZE> >
97 {
99
101 std::array<K,SIZE> _data;
102
103 public:
104
106 static constexpr int dimension = SIZE;
107
109 using size_type = typename Base::size_type;
110
112 using value_type = typename Base::value_type;
113
116
119
120 public:
121
123 constexpr FieldVector ()
124 noexcept(std::is_nothrow_default_constructible_v<K>)
125 : _data{}
126 {}
127
129 explicit(SIZE != 1)
130 constexpr FieldVector (const value_type& value) noexcept
131 : _data{filledArray<SIZE>(value)}
132 {}
133
135 template<Concept::Number S>
136 requires (std::constructible_from<K,S>)
137 explicit(SIZE != 1)
138 constexpr FieldVector (const S& scalar)
139 noexcept(std::is_nothrow_constructible_v<K,S>)
140 : _data{filledArray<SIZE,K>(K(scalar))}
141 {}
142
144 constexpr FieldVector (const std::initializer_list<K>& l)
145 : _data{}
146 {
147 assert(l.size() == size());
148 for (size_type i = 0; i < size(); ++i)
149 _data[i] = std::data(l)[i];
150 }
151
153 template<class V>
155 std::is_assignable_v<K&, decltype(std::declval<const V&>()[0])>)
156 constexpr FieldVector (const DenseVector<V>& x)
157 {
158 assert(x.size() == size());
159 for (size_type i = 0; i < size(); ++i)
160 _data[i] = x[i];
161 }
162
164 template<class OtherK>
165 requires (std::is_assignable_v<K&, const OtherK&>)
166 explicit constexpr FieldVector (const FieldVector<OtherK, SIZE>& x)
167 noexcept(std::is_nothrow_assignable_v<K&, const OtherK&>)
168 {
169 for (size_type i = 0; i < size(); ++i)
170 _data[i] = x[i];
171 }
172
174 constexpr FieldVector (const FieldVector&) = default;
175
176
178 template<class V>
180 std::is_assignable_v<K&, decltype(std::declval<const V&>()[0])>)
182 {
183 assert(x.size() == size());
184 for (size_type i = 0; i < size(); ++i)
185 _data[i] = x[i];
186 return *this;
187 }
188
190 template<Concept::Number S>
191 requires std::constructible_from<K,S>
192 constexpr FieldVector& operator= (const S& scalar)
193 noexcept(std::is_nothrow_constructible_v<K,S>)
194 {
195 _data.fill(K(scalar));
196 return *this;
197 }
198
200 template<class OtherK>
201 requires (std::is_assignable_v<K&, const OtherK&>)
202 constexpr FieldVector& operator= (const FieldVector<OtherK, SIZE>& x)
203 noexcept(std::is_nothrow_assignable_v<K&, const OtherK&>)
204 {
205 for (size_type i = 0; i < size(); ++i)
206 _data[i] = x[i];
207 return *this;
208 }
209
211 constexpr FieldVector& operator= (const FieldVector&) = default;
212
213
216
218 static constexpr size_type size () noexcept { return SIZE; }
219
221
222
225
231 {
233 return _data[i];
234 }
235
241 {
243 return _data[i];
244 }
245
247 constexpr K* data () noexcept
248 {
249 return _data.data();
250 }
251
253 constexpr const K* data () const noexcept
254 {
255 return _data.data();
256 }
257
259 constexpr operator const_reference () const noexcept
260 requires(SIZE == 1)
261 {
262 return _data[0];
263 }
264
266 constexpr operator reference () noexcept
267 requires(SIZE == 1)
268 {
269 return _data[0];
270 }
271
273
274
277
279 template<Concept::Number S>
280 friend constexpr bool operator== (const FieldVector& a, const S& b) noexcept
281 requires(SIZE == 1)
282 {
283 return a._data[0] == b;
284 }
285
287 template<Concept::Number S>
288 friend constexpr bool operator== (const S& a, const FieldVector& b) noexcept
289 requires(SIZE == 1)
290 {
291 return a == b._data[0];
292 }
293
295 template<class T>
297 friend constexpr auto operator<=> (const FieldVector& a, const FieldVector<T,SIZE>& b) noexcept
298 {
299#if __cpp_lib_three_way_comparison
300 return a._data <=> b._data;
301#else
302 return Std::lexicographical_compare_three_way(a.begin(), a.end(), b.begin(), b.end());
303#endif
304 }
305
307 template<Concept::Number S>
308 friend constexpr auto operator<=> (const FieldVector& a, const S& b) noexcept
309 requires(SIZE == 1)
310 {
311 return a._data[0] <=> b;
312 }
313
315 template<Concept::Number S>
316 friend constexpr auto operator<=> (const S& a, const FieldVector& b) noexcept
317 requires(SIZE == 1)
318 {
319 return a <=> b._data[0];
320 }
321
323
324
327
329 template<Concept::Number S>
330 friend constexpr auto operator* (const FieldVector& a, const S& b) noexcept
331 {
332 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
334 for (size_type i = 0; i < size(); ++i)
335 result[i] = a[i] * b;
336 return result;
337 }
338
340 template<Concept::Number S>
341 friend constexpr auto operator* (const S& a, const FieldVector& b) noexcept
342 {
343 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
345 for (size_type i = 0; i < size(); ++i)
346 result[i] = a * b[i];
347 return result;
348 }
349
351 template<Concept::Number S>
352 friend constexpr auto operator/ (const FieldVector& a, const S& b) noexcept
353 {
354 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
356 for (size_type i = 0; i < size(); ++i)
357 result[i] = a[i] / b;
358 return result;
359 }
360
362 template<Concept::Number S>
363 friend constexpr FieldVector operator/ (const S& a, const FieldVector& b) noexcept
364 requires(SIZE == 1)
365 {
366 return FieldVector{a / b[0]};
367 }
368
370 template<Concept::Number S>
371 friend constexpr auto operator+ (const FieldVector& a, const S& b) noexcept
372 requires(SIZE == 1)
373 {
374 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
376 }
377
379 template<Concept::Number S>
380 friend constexpr auto operator+ (const S& a, const FieldVector& b) noexcept
381 requires(SIZE == 1)
382 {
383 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
385 }
386
388 template<Concept::Number S>
389 friend constexpr auto operator- (const FieldVector& a, const S& b) noexcept
390 requires(SIZE == 1)
391 {
392 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
394 }
395
397 template<Concept::Number S>
398 friend constexpr auto operator- (const S& a, const FieldVector& b) noexcept
399 requires(SIZE == 1)
400 {
401 using ResultValueType = typename PromotionTraits<K,S>::PromotedType;
403 }
404
406 };
407
419 template<class K, int SIZE>
420 std::istream& operator>> (std::istream& in, FieldVector<K, SIZE>& v)
421 {
423 for (int i = 0; i < SIZE; ++i)
424 in >> w[i];
425 if (in)
426 v = w;
427 return in;
428 }
429
430 /* Overloads for common classification functions */
431 namespace MathOverloads {
432
434 template<class K, int SIZE>
436 {
437 bool out = true;
438 for (int i = 0; i < SIZE; ++i) {
439 out &= Dune::isFinite(b[i]);
440 }
441 return out;
442 }
443
445 template<class K, int SIZE>
447 {
448 bool out = false;
449 for (int i = 0; i < SIZE; ++i) {
450 out |= Dune::isInf(b[i]);
451 }
452 return out;
453 }
454
456 template<class K, int SIZE,
457 std::enable_if_t<HasNaN<K>::value, int> = 0>
459 {
460 bool out = false;
461 for (int i = 0; i < SIZE; ++i) {
462 out |= Dune::isNaN(b[i]);
463 }
464 return out;
465 }
466
468 template<class K,
469 std::enable_if_t<HasNaN<K>::value, int> = 0>
472 {
473 return Dune::isUnordered(b[0],c[0]);
474 }
475
476 } // end namespace MathOverloads
477
480} // end namespace Dune
481
482#endif // DUNE_COMMON_FVECTOR_HH
Macro for wrapping boundary checks.
Interface for a class of dense vectors over a given field.
Definition: densevector.hh:230
Traits::value_type value_type
export the type representing the field
Definition: densevector.hh:251
constexpr derived_type operator-() const
Vector negation.
Definition: densevector.hh:455
constexpr Iterator end()
end iterator
Definition: densevector.hh:354
Traits::size_type size_type
The type used for the index access and size operation.
Definition: densevector.hh:260
constexpr Iterator begin()
begin iterator
Definition: densevector.hh:348
constexpr size_type size() const
size method
Definition: densevector.hh:337
vector space out of a tensor product of fields.
Definition: fvector.hh:97
constexpr FieldVector(const FieldVector &)=default
Copy constructor with default behavior.
typename Base::size_type size_type
The type used for the index access and size operation.
Definition: fvector.hh:109
constexpr FieldVector(const std::initializer_list< K > &l)
Construct from a std::initializer_list of values.
Definition: fvector.hh:144
static constexpr size_type size() noexcept
Obtain the number of elements stored in the vector.
Definition: fvector.hh:218
friend constexpr auto operator+(const FieldVector &a, const S &b) noexcept
Binary addition, when using FieldVector<K,1> like K.
Definition: fvector.hh:371
friend constexpr auto operator/(const FieldVector &a, const S &b) noexcept
Vector space division by scalar.
Definition: fvector.hh:352
typename Base::value_type value_type
The type of the elements stored in the vector.
Definition: fvector.hh:112
friend constexpr auto operator*(const FieldVector &a, const S &b) noexcept
Vector space multiplication with scalar.
Definition: fvector.hh:330
static constexpr int dimension
The size of this vector.
Definition: fvector.hh:106
constexpr FieldVector() noexcept(std::is_nothrow_default_constructible_v< K >)
Default constructor, making value-initialized vector with all components set to zero.
Definition: fvector.hh:123
friend constexpr auto operator<=>(const FieldVector &a, const FieldVector< T, SIZE > &b) noexcept
three-way comparison of FieldVectors
Definition: fvector.hh:297
friend constexpr bool operator==(const FieldVector &a, const S &b) noexcept
comparing FieldVectors<1> with scalar for equality
Definition: fvector.hh:280
constexpr FieldVector(const DenseVector< V > &x)
Constructor from another dense vector if the elements are assignable to K.
Definition: fvector.hh:156
constexpr FieldVector & operator=(const DenseVector< V > &x)
Assignment from another dense vector.
Definition: fvector.hh:181
constexpr const K * data() const noexcept
Return pointer to underlying array.
Definition: fvector.hh:253
value_type & reference
The type used for references to the vector entries.
Definition: fvector.hh:115
const value_type & const_reference
The type used for const references to the vector entries.
Definition: fvector.hh:118
constexpr reference operator[](size_type i)
Return a reference to the ith element.
Definition: fvector.hh:230
constexpr K * data() noexcept
Return pointer to underlying array.
Definition: fvector.hh:247
The concept std::three_way_comparable_with specifies that the three way comparison operator <=> on (p...
Definition: compare.hh:98
Implements the dense vector interface, with an exchangeable storage class.
Utility to generate an array with a certain value.
Type traits to determine the type of reals (when working with complex numbers)
std::istream & operator>>(std::istream &stream, std::tuple< Ts... > &t)
Read a std::tuple.
Definition: streamoperators.hh:43
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition: boundschecking.hh:30
Some useful basic math stuff.
bool isNaN(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is NaN.
Definition: fvector.hh:458
bool isInf(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether any entry is infinite.
Definition: fvector.hh:446
auto isFinite(const FieldVector< K, SIZE > &b, PriorityTag< 2 >, ADLTag)
Returns whether all entries are finite.
Definition: fvector.hh:435
bool isUnordered(const FieldVector< K, 1 > &b, const FieldVector< K, 1 > &c, PriorityTag< 2 >, ADLTag)
Returns true if either b or c is NaN.
Definition: fvector.hh:470
constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp={}) -> decltype(comp(*f1, *f2))
Lexicographically compares two ranges [first1, last1) and [first2, last2) using three-way comparison ...
Definition: algorithm.hh:37
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Compute type of the result of an arithmetic operation involving two different number types.
TMP to check the size of a DenseVectors statically, if possible.
Definition: fvector.hh:68
static constexpr bool value
True if C is not of type FieldVector or its dimension is not equal SIZE.
Definition: fvector.hh:73
Tag to make sure the functions in this namespace can be found by ADL.
Definition: math.hh:230
Helper class for tagging priorities.
Definition: typeutilities.hh:73
Traits for type conversions and type information.
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Jul 16, 22:39, 2025)